| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP-CLI commands for Cooked |
| 4 |
* |
| 5 |
* @package Cooked |
| 6 |
* @subpackage CLI |
| 7 |
* @since 1.13.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Run Cooked maintenance and migration tools. |
| 20 |
*/ |
| 21 |
class Cooked_CLI_Tools_Command { |
| 22 |
|
| 23 |
/** |
| 24 |
* Run a Cooked migration/maintenance tool by name. |
| 25 |
* |
| 26 |
* ## OPTIONS |
| 27 |
* |
| 28 |
* <tool> |
| 29 |
* : The tool to run. Use `wp cooked tools list` to see available tools. |
| 30 |
* |
| 31 |
* ## EXAMPLES |
| 32 |
* |
| 33 |
* wp cooked tools run remove_recipes_from_cooked_user_meta |
| 34 |
* wp cooked tools run update_rewrite_rules |
| 35 |
* |
| 36 |
* @param array $args Positional args. 0 = tool name. |
| 37 |
* @param array $assoc_args Associative args. |
| 38 |
*/ |
| 39 |
public function run( $args, $assoc_args ) { |
| 40 |
if ( empty( $args[0] ) ) { |
| 41 |
\WP_CLI::error( __( 'Please provide a tool name. Use `wp cooked tools list` to see available tools.', 'cooked' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
$tool_name = $args[0]; |
| 45 |
$result = Cooked_Updates::run_tool( $tool_name ); |
| 46 |
|
| 47 |
if ( is_wp_error( $result ) ) { |
| 48 |
\WP_CLI::error( $result->get_error_message() ); |
| 49 |
} |
| 50 |
|
| 51 |
\WP_CLI::success( __( 'Tool completed successfully.', 'cooked' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* List available Cooked tools that can be run on demand. |
| 56 |
* |
| 57 |
* ## EXAMPLES |
| 58 |
* |
| 59 |
* wp cooked tools list |
| 60 |
* |
| 61 |
* @param array $args Positional args. |
| 62 |
* @param array $assoc_args Associative args. |
| 63 |
*/ |
| 64 |
public function list_tools( $args, $assoc_args ) { |
| 65 |
$tools = Cooked_Updates::get_runnable_tools(); |
| 66 |
|
| 67 |
if ( empty( $tools ) ) { |
| 68 |
\WP_CLI::log( __( 'No tools available.', 'cooked' ) ); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$rows = []; |
| 73 |
foreach ( $tools as $tool ) { |
| 74 |
$rows[] = [ |
| 75 |
'id' => $tool['id'], |
| 76 |
'name' => $tool['name'], |
| 77 |
'description' => $tool['description'], |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
\WP_CLI\Utils\format_items( 'table', $rows, [ 'id', 'name', 'description' ] ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
\WP_CLI::add_command( 'cooked tools', 'Cooked_CLI_Tools_Command' ); |
| 86 |
|