|
| 1 | +<?php |
| 2 | +namespace Cbx\Careertoolkit\Factories\Misc; |
| 3 | + |
| 4 | +/** |
| 5 | + * Easy plugin checker command command |
| 6 | + * Class EasyPluginChecker |
| 7 | + * @since 1.0.1 |
| 8 | + */ |
| 9 | +class EasyPluginChecker{ |
| 10 | + |
| 11 | + /** |
| 12 | + * Register CLI command |
| 13 | + * @since 1.0.0 |
| 14 | + */ |
| 15 | + public function wp_cli_register_commands() { |
| 16 | + \WP_CLI::add_command( 'easy-plugin-check', [ $this, "run" ] ); |
| 17 | + } //end method wp_cli_register_commands |
| 18 | + |
| 19 | + /** |
| 20 | + * Command activity |
| 21 | + * |
| 22 | + * @param $args |
| 23 | + * @param $assoc_args |
| 24 | + * |
| 25 | + * @since 1.0.0 |
| 26 | + */ |
| 27 | + public function run( $args, $assoc_args ) { |
| 28 | + global $wpdb; |
| 29 | + |
| 30 | + $plugin = $args[0]; |
| 31 | + $exclude_directories = WP_CLI\Utils\get_flag_value($assoc_args, 'exclude-directories', ''); |
| 32 | + $format = WP_CLI\Utils\get_flag_value($assoc_args, 'format', 'table'); // Default format is table |
| 33 | + $output_file = WP_CLI\Utils\get_flag_value($assoc_args, 'output-file', ''); |
| 34 | + |
| 35 | + // Set default output file if not provided |
| 36 | + if (empty($output_file)) { |
| 37 | + $output_file = ABSPATH . 'plugin-check.log'; |
| 38 | + } |
| 39 | + |
| 40 | + // Build the command to check the plugin |
| 41 | + $command = sprintf( |
| 42 | + 'wp plugin check %s --exclude-directories=%s --format=%s', |
| 43 | + escapeshellarg($plugin), |
| 44 | + escapeshellarg($exclude_directories), |
| 45 | + escapeshellarg($format) |
| 46 | + ); |
| 47 | + |
| 48 | + // Execute the command and capture the output |
| 49 | + exec($command, $output, $return_var); |
| 50 | + |
| 51 | + if ($return_var !== 0) { |
| 52 | + WP_CLI::error('Error running the plugin check command.'); |
| 53 | + } |
| 54 | + |
| 55 | + // Combine the output into a single string |
| 56 | + $output_content = implode("\n", $output); |
| 57 | + |
| 58 | + // Clean the file (truncate) before writing new output |
| 59 | + if (file_put_contents($output_file, '') === false) { |
| 60 | + WP_CLI::error('Failed to clear the output file.'); |
| 61 | + } |
| 62 | + |
| 63 | + // Write the output to the file |
| 64 | + if (file_put_contents($output_file, $output_content) === false) { |
| 65 | + WP_CLI::error('Failed to write output to the file.'); |
| 66 | + } |
| 67 | + |
| 68 | + WP_CLI::success("Plugin check output written to {$output_file}"); |
| 69 | + } //end method run |
| 70 | +} //end class EasyPluginChecker |
0 commit comments