2016-12-08 18:59:07 +00:00
|
|
|
<?php
|
2018-03-06 12:20:58 +00:00
|
|
|
/**
|
|
|
|
* WP_CLI_Runner class file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\CLI
|
|
|
|
*/
|
2017-05-23 14:40:19 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
/**
|
|
|
|
* WC API to WC CLI Bridge.
|
|
|
|
*
|
|
|
|
* Hooks into the REST API, figures out which endpoints come from WC,
|
|
|
|
* and registers them as CLI commands.
|
|
|
|
*
|
2016-12-08 21:40:12 +00:00
|
|
|
* Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT).
|
|
|
|
* https://github.com/wp-cli/restful
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-12-08 18:59:07 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
*/
|
|
|
|
class WC_CLI_Runner {
|
|
|
|
/**
|
2017-07-10 05:56:28 +00:00
|
|
|
* Endpoints to disable (meaning they will not be available as CLI commands).
|
2016-12-08 18:59:07 +00:00
|
|
|
* Some of these can either be done via WP already, or are offered with
|
|
|
|
* some other changes (like tools).
|
2018-03-06 12:20:58 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2016-12-08 18:59:07 +00:00
|
|
|
*/
|
|
|
|
private static $disabled_endpoints = array(
|
|
|
|
'settings',
|
2017-03-23 23:37:42 +00:00
|
|
|
'settings/(?P<group_id>[\w-]+)',
|
|
|
|
'settings/(?P<group_id>[\w-]+)/batch',
|
|
|
|
'settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)',
|
2016-12-08 18:59:07 +00:00
|
|
|
'system_status',
|
|
|
|
'system_status/tools',
|
|
|
|
'system_status/tools/(?P<id>[\w-]+)',
|
|
|
|
'reports',
|
|
|
|
'reports/sales',
|
|
|
|
'reports/top_sellers',
|
|
|
|
);
|
|
|
|
|
2017-03-01 21:05:04 +00:00
|
|
|
/**
|
|
|
|
* The version of the REST API we should target to
|
|
|
|
* generate commands.
|
2018-03-06 12:20:58 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2017-03-01 21:05:04 +00:00
|
|
|
*/
|
|
|
|
private static $target_rest_version = 'v2';
|
|
|
|
|
2016-12-08 18:59:07 +00:00
|
|
|
/**
|
|
|
|
* Register's all endpoints as commands once WP and WC have all loaded.
|
|
|
|
*/
|
|
|
|
public static function after_wp_load() {
|
|
|
|
global $wp_rest_server;
|
2018-03-05 19:46:54 +00:00
|
|
|
$wp_rest_server = new WP_REST_Server();
|
2016-12-08 18:59:07 +00:00
|
|
|
do_action( 'rest_api_init', $wp_rest_server );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', '/' );
|
|
|
|
$request->set_param( 'context', 'help' );
|
|
|
|
$response = $wp_rest_server->dispatch( $request );
|
|
|
|
$response_data = $response->get_data();
|
|
|
|
if ( empty( $response_data ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all of our endpoints and register any valid WC endpoints.
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $response_data['routes'] as $route => $route_data ) {
|
2017-03-01 21:05:04 +00:00
|
|
|
// Only register endpoints for WC and our target version.
|
2017-03-07 17:57:28 +00:00
|
|
|
if ( substr( $route, 0, 4 + strlen( self::$target_rest_version ) ) !== '/wc/' . self::$target_rest_version ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-01 21:05:04 +00:00
|
|
|
|
2018-03-06 12:20:58 +00:00
|
|
|
// Only register endpoints with schemas.
|
2016-12-08 18:59:07 +00:00
|
|
|
if ( empty( $route_data['schema']['title'] ) ) {
|
2018-03-06 12:20:58 +00:00
|
|
|
/* translators: %s: Route to a given WC-API endpoint */
|
2016-12-08 18:59:07 +00:00
|
|
|
WP_CLI::debug( sprintf( __( 'No schema title found for %s, skipping REST command registration.', 'woocommerce' ), $route ), 'wc' );
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Ignore batch endpoints.
|
2016-12-08 18:59:07 +00:00
|
|
|
if ( 'batch' === $route_data['schema']['title'] ) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Disable specific endpoints.
|
2016-12-08 18:59:07 +00:00
|
|
|
$route_pieces = explode( '/', $route );
|
|
|
|
$endpoint_piece = str_replace( '/wc/' . $route_pieces[2] . '/', '', $route );
|
2018-03-06 12:20:58 +00:00
|
|
|
if ( in_array( $endpoint_piece, self::$disabled_endpoints, true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::register_route_commands( new WC_CLI_REST_Command( $route_data['schema']['title'], $route, $route_data['schema'] ), $route, $route_data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates command information and tells WP CLI about all
|
2017-07-10 05:56:28 +00:00
|
|
|
* commands available from a route.
|
2016-12-08 18:59:07 +00:00
|
|
|
*
|
2018-03-06 12:20:58 +00:00
|
|
|
* @param string $rest_command WC-API command.
|
|
|
|
* @param string $route Path to route endpoint.
|
|
|
|
* @param array $route_data Command data.
|
|
|
|
* @param array $command_args WP-CLI command arguments.
|
2016-12-15 17:11:08 +00:00
|
|
|
*/
|
2016-12-08 18:59:07 +00:00
|
|
|
private static function register_route_commands( $rest_command, $route, $route_data, $command_args = array() ) {
|
2017-02-09 18:29:24 +00:00
|
|
|
// Define IDs that we are looking for in the routes (in addition to id)
|
|
|
|
// so that we can pass it to the rest command, and use it here to generate documentation.
|
|
|
|
$supported_ids = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'product_id' => __( 'Product ID.', 'woocommerce' ),
|
|
|
|
'customer_id' => __( 'Customer ID.', 'woocommerce' ),
|
|
|
|
'order_id' => __( 'Order ID.', 'woocommerce' ),
|
|
|
|
'refund_id' => __( 'Refund ID.', 'woocommerce' ),
|
|
|
|
'attribute_id' => __( 'Attribute ID.', 'woocommerce' ),
|
|
|
|
'zone_id' => __( 'Zone ID.', 'woocommerce' ),
|
2020-07-10 14:21:05 +00:00
|
|
|
'instance_id' => __( 'Instance ID.', 'woocommerce' ),
|
2020-07-10 21:16:30 +00:00
|
|
|
'id' => __( 'The ID for the resource.', 'woocommerce' ),
|
2017-02-09 18:29:24 +00:00
|
|
|
);
|
|
|
|
$rest_command->set_supported_ids( $supported_ids );
|
2017-04-28 16:09:39 +00:00
|
|
|
$positional_args = array_keys( $supported_ids );
|
2017-02-09 18:29:24 +00:00
|
|
|
|
2018-03-05 19:46:54 +00:00
|
|
|
$parent = "wc {$route_data['schema']['title']}";
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands = array();
|
|
|
|
|
|
|
|
// Get a list of supported commands for each route.
|
|
|
|
foreach ( $route_data['endpoints'] as $endpoint ) {
|
2017-05-15 09:54:06 +00:00
|
|
|
preg_match_all( '#\([^\)]+\)#', $route, $matches );
|
2016-12-08 18:59:07 +00:00
|
|
|
$resource_id = ! empty( $matches[0] ) ? array_pop( $matches[0] ) : null;
|
|
|
|
$trimmed_route = rtrim( $route );
|
2016-12-15 17:11:08 +00:00
|
|
|
$is_singular = substr( $trimmed_route, - strlen( $resource_id ) ) === $resource_id;
|
2016-12-08 18:59:07 +00:00
|
|
|
|
2018-03-06 12:20:58 +00:00
|
|
|
// List a collection.
|
|
|
|
if ( array( 'GET' ) === $endpoint['methods'] && ! $is_singular ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands['list'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Create a specific resource.
|
|
|
|
if ( array( 'POST' ) === $endpoint['methods'] && ! $is_singular ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands['create'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Get a specific resource.
|
|
|
|
if ( array( 'GET' ) === $endpoint['methods'] && $is_singular ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands['get'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Update a specific resource.
|
|
|
|
if ( in_array( 'POST', $endpoint['methods'], true ) && $is_singular ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands['update'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
|
|
|
|
}
|
2018-03-06 12:20:58 +00:00
|
|
|
// Delete a specific resource.
|
|
|
|
if ( array( 'DELETE' ) === $endpoint['methods'] && $is_singular ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$supported_commands['delete'] = ! empty( $endpoint['args'] ) ? $endpoint['args'] : array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:11:08 +00:00
|
|
|
foreach ( $supported_commands as $command => $endpoint_args ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$synopsis = array();
|
|
|
|
$arg_regs = array();
|
2017-06-16 13:29:57 +00:00
|
|
|
$ids = array();
|
2016-12-08 18:59:07 +00:00
|
|
|
|
2017-02-09 18:29:24 +00:00
|
|
|
foreach ( $supported_ids as $id_name => $id_desc ) {
|
|
|
|
if ( strpos( $route, '<' . $id_name . '>' ) !== false ) {
|
|
|
|
$synopsis[] = array(
|
2017-02-10 16:38:43 +00:00
|
|
|
'name' => $id_name,
|
|
|
|
'type' => 'positional',
|
2017-02-09 18:29:24 +00:00
|
|
|
'description' => $id_desc,
|
2017-02-10 16:38:43 +00:00
|
|
|
'optional' => false,
|
2017-02-09 18:29:24 +00:00
|
|
|
);
|
2018-03-05 19:46:54 +00:00
|
|
|
$ids[] = $id_name;
|
2017-02-09 18:29:24 +00:00
|
|
|
}
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $endpoint_args as $name => $args ) {
|
2018-03-06 12:20:58 +00:00
|
|
|
if ( ! in_array( $name, $positional_args, true ) || strpos( $route, '<' . $id_name . '>' ) === false ) {
|
2017-02-10 16:38:43 +00:00
|
|
|
$arg_regs[] = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'name' => $name,
|
|
|
|
'type' => 'assoc',
|
2017-02-10 16:38:43 +00:00
|
|
|
'description' => ! empty( $args['description'] ) ? $args['description'] : '',
|
2018-03-21 22:57:10 +00:00
|
|
|
'optional' => empty( $args['required'] ),
|
2017-02-10 16:38:43 +00:00
|
|
|
);
|
|
|
|
}
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $arg_regs as $arg_reg ) {
|
|
|
|
$synopsis[] = $arg_reg;
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:20:58 +00:00
|
|
|
if ( in_array( $command, array( 'list', 'get' ), true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$synopsis[] = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'name' => 'fields',
|
|
|
|
'type' => 'assoc',
|
2016-12-08 18:59:07 +00:00
|
|
|
'description' => __( 'Limit response to specific fields. Defaults to all fields.', 'woocommerce' ),
|
2018-03-05 19:46:54 +00:00
|
|
|
'optional' => true,
|
2016-12-08 18:59:07 +00:00
|
|
|
);
|
|
|
|
$synopsis[] = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'name' => 'field',
|
|
|
|
'type' => 'assoc',
|
2016-12-08 18:59:07 +00:00
|
|
|
'description' => __( 'Get the value of an individual field.', 'woocommerce' ),
|
2018-03-05 19:46:54 +00:00
|
|
|
'optional' => true,
|
2016-12-08 18:59:07 +00:00
|
|
|
);
|
|
|
|
$synopsis[] = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'name' => 'format',
|
|
|
|
'type' => 'assoc',
|
2016-12-08 18:59:07 +00:00
|
|
|
'description' => __( 'Render response in a particular format.', 'woocommerce' ),
|
2018-03-05 19:46:54 +00:00
|
|
|
'optional' => true,
|
|
|
|
'default' => 'table',
|
|
|
|
'options' => array(
|
2016-12-08 18:59:07 +00:00
|
|
|
'table',
|
|
|
|
'json',
|
|
|
|
'csv',
|
|
|
|
'ids',
|
|
|
|
'yaml',
|
|
|
|
'count',
|
|
|
|
'headers',
|
|
|
|
'body',
|
|
|
|
'envelope',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-06 12:20:58 +00:00
|
|
|
if ( in_array( $command, array( 'create', 'update', 'delete' ), true ) ) {
|
2016-12-08 18:59:07 +00:00
|
|
|
$synopsis[] = array(
|
2018-03-05 19:46:54 +00:00
|
|
|
'name' => 'porcelain',
|
|
|
|
'type' => 'flag',
|
2016-12-08 18:59:07 +00:00
|
|
|
'description' => __( 'Output just the id when the operation is successful.', 'woocommerce' ),
|
2018-03-05 19:46:54 +00:00
|
|
|
'optional' => true,
|
2016-12-08 18:59:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$methods = array(
|
|
|
|
'list' => 'list_items',
|
|
|
|
'create' => 'create_item',
|
|
|
|
'delete' => 'delete_item',
|
2018-03-05 19:46:54 +00:00
|
|
|
'get' => 'get_item',
|
2016-12-08 18:59:07 +00:00
|
|
|
'update' => 'update_item',
|
|
|
|
);
|
|
|
|
|
|
|
|
$before_invoke = null;
|
|
|
|
if ( empty( $command_args['when'] ) && \WP_CLI::get_config( 'debug' ) ) {
|
|
|
|
$before_invoke = function() {
|
2017-09-05 19:52:39 +00:00
|
|
|
wc_maybe_define_constant( 'SAVEQUERIES', true );
|
2016-12-08 18:59:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-05 19:46:54 +00:00
|
|
|
WP_CLI::add_command(
|
|
|
|
"{$parent} {$command}",
|
|
|
|
array( $rest_command, $methods[ $command ] ),
|
|
|
|
array(
|
|
|
|
'synopsis' => $synopsis,
|
|
|
|
'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '',
|
|
|
|
'before_invoke' => $before_invoke,
|
|
|
|
)
|
|
|
|
);
|
2016-12-08 18:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|