PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/cli/class-wc-cli-runner.php

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