Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes/cli
File: class-wc-cli-runner.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WP_CLI_Runner class file.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WooCommerce\CLI
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[7] Fix | Delete
exit;
[8] Fix | Delete
}
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* WC API to WC CLI Bridge.
[12] Fix | Delete
*
[13] Fix | Delete
* Hooks into the REST API, figures out which endpoints come from WC,
[14] Fix | Delete
* and registers them as CLI commands.
[15] Fix | Delete
*
[16] Fix | Delete
* Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT).
[17] Fix | Delete
* https://github.com/wp-cli/restful
[18] Fix | Delete
*
[19] Fix | Delete
* @version 3.0.0
[20] Fix | Delete
* @package WooCommerce
[21] Fix | Delete
*/
[22] Fix | Delete
class WC_CLI_Runner {
[23] Fix | Delete
/**
[24] Fix | Delete
* Endpoints to disable (meaning they will not be available as CLI commands).
[25] Fix | Delete
* Some of these can either be done via WP already, or are offered with
[26] Fix | Delete
* some other changes (like tools).
[27] Fix | Delete
*
[28] Fix | Delete
* @var array
[29] Fix | Delete
*/
[30] Fix | Delete
private static $disabled_endpoints = array(
[31] Fix | Delete
'settings',
[32] Fix | Delete
'settings/(?P<group_id>[\w-]+)',
[33] Fix | Delete
'settings/(?P<group_id>[\w-]+)/batch',
[34] Fix | Delete
'settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)',
[35] Fix | Delete
'system_status',
[36] Fix | Delete
'system_status/tools',
[37] Fix | Delete
'system_status/tools/(?P<id>[\w-]+)',
[38] Fix | Delete
'reports',
[39] Fix | Delete
'reports/sales',
[40] Fix | Delete
'reports/top_sellers',
[41] Fix | Delete
);
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* The version of the REST API we should target to
[45] Fix | Delete
* generate commands.
[46] Fix | Delete
*
[47] Fix | Delete
* @var string
[48] Fix | Delete
*/
[49] Fix | Delete
private static $target_rest_version = 'v2';
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Register's all endpoints as commands once WP and WC have all loaded.
[53] Fix | Delete
*/
[54] Fix | Delete
public static function after_wp_load() {
[55] Fix | Delete
global $wp_rest_server;
[56] Fix | Delete
$wp_rest_server = new WP_REST_Server();
[57] Fix | Delete
do_action( 'rest_api_init', $wp_rest_server );
[58] Fix | Delete
[59] Fix | Delete
$request = new WP_REST_Request( 'GET', '/' );
[60] Fix | Delete
$request->set_param( 'context', 'help' );
[61] Fix | Delete
$response = $wp_rest_server->dispatch( $request );
[62] Fix | Delete
$response_data = $response->get_data();
[63] Fix | Delete
if ( empty( $response_data ) ) {
[64] Fix | Delete
return;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
// Loop through all of our endpoints and register any valid WC endpoints.
[68] Fix | Delete
foreach ( $response_data['routes'] as $route => $route_data ) {
[69] Fix | Delete
// Only register endpoints for WC and our target version.
[70] Fix | Delete
if ( substr( $route, 0, 4 + strlen( self::$target_rest_version ) ) !== '/wc/' . self::$target_rest_version ) {
[71] Fix | Delete
continue;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
// Only register endpoints with schemas.
[75] Fix | Delete
if ( empty( $route_data['schema']['title'] ) ) {
[76] Fix | Delete
/* translators: %s: Route to a given WC-API endpoint */
[77] Fix | Delete
WP_CLI::debug( sprintf( __( 'No schema title found for %s, skipping REST command registration.', 'woocommerce' ), $route ), 'wc' );
[78] Fix | Delete
continue;
[79] Fix | Delete
}
[80] Fix | Delete
// Ignore batch endpoints.
[81] Fix | Delete
if ( 'batch' === $route_data['schema']['title'] ) {
[82] Fix | Delete
continue;
[83] Fix | Delete
}
[84] Fix | Delete
// Disable specific endpoints.
[85] Fix | Delete
$route_pieces = explode( '/', $route );
[86] Fix | Delete
$endpoint_piece = str_replace( '/wc/' . $route_pieces[2] . '/', '', $route );
[87] Fix | Delete
if ( in_array( $endpoint_piece, self::$disabled_endpoints, true ) ) {
[88] Fix | Delete
continue;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
self::register_route_commands( new WC_CLI_REST_Command( $route_data['schema']['title'], $route, $route_data['schema'] ), $route, $route_data );
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Generates command information and tells WP CLI about all
[97] Fix | Delete
* commands available from a route.
[98] Fix | Delete
*
[99] Fix | Delete
* @param WC_CLI_REST_Command $rest_command WC-API command.
[100] Fix | Delete
* @param string $route Path to route endpoint.
[101] Fix | Delete
* @param array $route_data Command data.
[102] Fix | Delete
* @param array $command_args WP-CLI command arguments.
[103] Fix | Delete
*/
[104] Fix | Delete
private static function register_route_commands( $rest_command, $route, $route_data, $command_args = array() ) {
[105] Fix | Delete
// Define IDs that we are looking for in the routes (in addition to id)
[106] Fix | Delete
// so that we can pass it to the rest command, and use it here to generate documentation.
[107] Fix | Delete
$supported_ids = array(
[108] Fix | Delete
'product_id' => __( 'Product ID.', 'woocommerce' ),
[109] Fix | Delete
'customer_id' => __( 'Customer ID.', 'woocommerce' ),
[110] Fix | Delete
'order_id' => __( 'Order ID.', 'woocommerce' ),
[111] Fix | Delete
'refund_id' => __( 'Refund ID.', 'woocommerce' ),
[112] Fix | Delete
'attribute_id' => __( 'Attribute ID.', 'woocommerce' ),
[113] Fix | Delete
'zone_id' => __( 'Zone ID.', 'woocommerce' ),
[114] Fix | Delete
'instance_id' => __( 'Instance ID.', 'woocommerce' ),
[115] Fix | Delete
'id' => __( 'The ID for the resource.', 'woocommerce' ),
[116] Fix | Delete
'slug' => __( 'The slug for the resource.', 'woocommerce' ),
[117] Fix | Delete
);
[118] Fix | Delete
$rest_command->set_supported_ids( $supported_ids );
[119] Fix | Delete
$positional_args = array_keys( $supported_ids );
[120] Fix | Delete
$parent = "wc {$route_data['schema']['title']}";
[121] Fix | Delete
$supported_commands = array();
[122] Fix | Delete
[123] Fix | Delete
// Get a list of supported commands for each route.
[124] Fix | Delete
foreach ( $route_data['endpoints'] as $endpoint ) {
[125] Fix | Delete
preg_match_all( '#\([^\)]+\)#', $route, $matches );
[126] Fix | Delete
$resource_id = ! empty( $matches[0] ) ? array_pop( $matches[0] ) : null;
[127] Fix | Delete
$trimmed_route = rtrim( $route );
[128] Fix | Delete
$is_singular = substr( $trimmed_route, - strlen( $resource_id ?? '' ) ) === $resource_id;
[129] Fix | Delete
[130] Fix | Delete
// List a collection.
[131] Fix | Delete
if ( array( 'GET' ) === $endpoint['methods'] && ! $is_singular ) {
[132] Fix | Delete
$supported_commands['list'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
[133] Fix | Delete
}
[134] Fix | Delete
// Create a specific resource.
[135] Fix | Delete
if ( array( 'POST' ) === $endpoint['methods'] && ! $is_singular ) {
[136] Fix | Delete
$supported_commands['create'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
[137] Fix | Delete
}
[138] Fix | Delete
// Get a specific resource.
[139] Fix | Delete
if ( array( 'GET' ) === $endpoint['methods'] && $is_singular ) {
[140] Fix | Delete
$supported_commands['get'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
[141] Fix | Delete
}
[142] Fix | Delete
// Update a specific resource.
[143] Fix | Delete
if ( in_array( 'POST', $endpoint['methods'], true ) && $is_singular ) {
[144] Fix | Delete
$supported_commands['update'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
[145] Fix | Delete
}
[146] Fix | Delete
// Delete a specific resource.
[147] Fix | Delete
if ( array( 'DELETE' ) === $endpoint['methods'] && $is_singular ) {
[148] Fix | Delete
$supported_commands['delete'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
[149] Fix | Delete
}
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
foreach ( $supported_commands as $command => $endpoint_args ) {
[153] Fix | Delete
$synopsis = array();
[154] Fix | Delete
$arg_regs = array();
[155] Fix | Delete
$ids = array();
[156] Fix | Delete
[157] Fix | Delete
foreach ( $supported_ids as $id_name => $id_desc ) {
[158] Fix | Delete
if ( strpos( $route, '<' . $id_name . '>' ) !== false ) {
[159] Fix | Delete
$synopsis[] = array(
[160] Fix | Delete
'name' => $id_name,
[161] Fix | Delete
'type' => 'positional',
[162] Fix | Delete
'description' => $id_desc,
[163] Fix | Delete
'optional' => false,
[164] Fix | Delete
);
[165] Fix | Delete
$ids[] = $id_name;
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
foreach ( $endpoint_args as $name => $args ) {
[170] Fix | Delete
if ( ! in_array( $name, $positional_args, true ) || strpos( $route, '<' . $id_name . '>' ) === false ) {
[171] Fix | Delete
$arg_regs[] = array(
[172] Fix | Delete
'name' => $name,
[173] Fix | Delete
'type' => 'assoc',
[174] Fix | Delete
'description' => ! empty( $args['description'] ) ? $args['description'] : '',
[175] Fix | Delete
'optional' => empty( $args['required'] ),
[176] Fix | Delete
);
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
foreach ( $arg_regs as $arg_reg ) {
[181] Fix | Delete
$synopsis[] = $arg_reg;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
if ( in_array( $command, array( 'list', 'get' ), true ) ) {
[185] Fix | Delete
$synopsis[] = array(
[186] Fix | Delete
'name' => 'fields',
[187] Fix | Delete
'type' => 'assoc',
[188] Fix | Delete
'description' => __( 'Limit response to specific fields. Defaults to all fields.', 'woocommerce' ),
[189] Fix | Delete
'optional' => true,
[190] Fix | Delete
);
[191] Fix | Delete
$synopsis[] = array(
[192] Fix | Delete
'name' => 'field',
[193] Fix | Delete
'type' => 'assoc',
[194] Fix | Delete
'description' => __( 'Get the value of an individual field.', 'woocommerce' ),
[195] Fix | Delete
'optional' => true,
[196] Fix | Delete
);
[197] Fix | Delete
$synopsis[] = array(
[198] Fix | Delete
'name' => 'format',
[199] Fix | Delete
'type' => 'assoc',
[200] Fix | Delete
'description' => __( 'Render response in a particular format.', 'woocommerce' ),
[201] Fix | Delete
'optional' => true,
[202] Fix | Delete
'default' => 'table',
[203] Fix | Delete
'options' => array(
[204] Fix | Delete
'table',
[205] Fix | Delete
'json',
[206] Fix | Delete
'csv',
[207] Fix | Delete
'ids',
[208] Fix | Delete
'yaml',
[209] Fix | Delete
'count',
[210] Fix | Delete
'headers',
[211] Fix | Delete
'body',
[212] Fix | Delete
'envelope',
[213] Fix | Delete
),
[214] Fix | Delete
);
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
if ( in_array( $command, array( 'create', 'update', 'delete' ), true ) ) {
[218] Fix | Delete
$synopsis[] = array(
[219] Fix | Delete
'name' => 'porcelain',
[220] Fix | Delete
'type' => 'flag',
[221] Fix | Delete
'description' => __( 'Output just the id when the operation is successful.', 'woocommerce' ),
[222] Fix | Delete
'optional' => true,
[223] Fix | Delete
);
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
$methods = array(
[227] Fix | Delete
'list' => 'list_items',
[228] Fix | Delete
'create' => 'create_item',
[229] Fix | Delete
'delete' => 'delete_item',
[230] Fix | Delete
'get' => 'get_item',
[231] Fix | Delete
'update' => 'update_item',
[232] Fix | Delete
);
[233] Fix | Delete
[234] Fix | Delete
$before_invoke = null;
[235] Fix | Delete
if ( empty( $command_args['when'] ) && \WP_CLI::get_config( 'debug' ) ) {
[236] Fix | Delete
$before_invoke = function() {
[237] Fix | Delete
wc_maybe_define_constant( 'SAVEQUERIES', true );
[238] Fix | Delete
};
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
WP_CLI::add_command(
[242] Fix | Delete
"{$parent} {$command}",
[243] Fix | Delete
array( $rest_command, $methods[ $command ] ),
[244] Fix | Delete
array(
[245] Fix | Delete
'synopsis' => $synopsis,
[246] Fix | Delete
'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '',
[247] Fix | Delete
'before_invoke' => $before_invoke,
[248] Fix | Delete
)
[249] Fix | Delete
);
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function