class-wc-cli-tool-command.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * WC_CLI_Tool_Command class file.
  4. *
  5. * @package WooCommerce\CLI
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * Hooks up our system status tools to the CLI.
  12. *
  13. * Forked from wp-cli/restful (by Daniel Bachhuber, released under the MIT license https://opensource.org/licenses/MIT).
  14. * https://github.com/wp-cli/restful
  15. *
  16. * @version 3.0.0
  17. * @package WooCommerce
  18. */
  19. class WC_CLI_Tool_Command {
  20. /**
  21. * Registers just a 'list' and 'run' command to the WC CLI
  22. * since we only want to enable certain actions on the system status
  23. * tools endpoints.
  24. */
  25. public static function register_commands() {
  26. global $wp_rest_server;
  27. $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' );
  28. $response = $wp_rest_server->dispatch( $request );
  29. $response_data = $response->get_data();
  30. if ( empty( $response_data ) ) {
  31. return;
  32. }
  33. $parent = 'wc tool';
  34. $supported_commands = array( 'list', 'run' );
  35. foreach ( $supported_commands as $command ) {
  36. $synopsis = array();
  37. if ( 'run' === $command ) {
  38. $synopsis[] = array(
  39. 'name' => 'id',
  40. 'type' => 'positional',
  41. 'description' => __( 'The id for the resource.', 'woocommerce' ),
  42. 'optional' => false,
  43. );
  44. $method = 'update_item';
  45. $route = '/wc/v2/system_status/tools/(?P<id>[\w-]+)';
  46. } elseif ( 'list' === $command ) {
  47. $synopsis[] = array(
  48. 'name' => 'fields',
  49. 'type' => 'assoc',
  50. 'description' => __( 'Limit response to specific fields. Defaults to all fields.', 'woocommerce' ),
  51. 'optional' => true,
  52. );
  53. $synopsis[] = array(
  54. 'name' => 'field',
  55. 'type' => 'assoc',
  56. 'description' => __( 'Get the value of an individual field.', 'woocommerce' ),
  57. 'optional' => true,
  58. );
  59. $synopsis[] = array(
  60. 'name' => 'format',
  61. 'type' => 'assoc',
  62. 'description' => __( 'Render response in a particular format.', 'woocommerce' ),
  63. 'optional' => true,
  64. 'default' => 'table',
  65. 'options' => array(
  66. 'table',
  67. 'json',
  68. 'csv',
  69. 'ids',
  70. 'yaml',
  71. 'count',
  72. 'headers',
  73. 'body',
  74. 'envelope',
  75. ),
  76. );
  77. $method = 'list_items';
  78. $route = '/wc/v2/system_status/tools';
  79. }
  80. $before_invoke = null;
  81. if ( empty( $command_args['when'] ) && WP_CLI::get_config( 'debug' ) ) {
  82. $before_invoke = function() {
  83. wc_maybe_define_constant( 'SAVEQUERIES', true );
  84. };
  85. }
  86. $rest_command = new WC_CLI_REST_Command( 'system_status_tool', $route, $response_data['schema'] );
  87. WP_CLI::add_command(
  88. "{$parent} {$command}", array( $rest_command, $method ), array(
  89. 'synopsis' => $synopsis,
  90. 'when' => ! empty( $command_args['when'] ) ? $command_args['when'] : '',
  91. 'before_invoke' => $before_invoke,
  92. )
  93. );
  94. }
  95. }
  96. }