| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\User_Interface; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
use WP_CLI\ExitException; |
| 7 |
use WPSEO_Utils; |
| 8 |
use Yoast\WP\SEO\Commands\Command_Interface; |
| 9 |
use Yoast\WP\SEO\Main; |
| 10 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Command; |
| 11 |
use Yoast\WP\SEO\Schema_Aggregator\Application\Aggregate_Site_Schema_Command_Handler; |
| 12 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Config; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handles the CLI command to represent a site's schema as JSON. |
| 16 |
* |
| 17 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 18 |
*/ |
| 19 |
class Site_Schema_Aggregator_Cli_Command implements Command_Interface { |
| 20 |
|
| 21 |
/** |
| 22 |
* The configuration instance. |
| 23 |
* |
| 24 |
* @var Config |
| 25 |
*/ |
| 26 |
private $config; |
| 27 |
|
| 28 |
/** |
| 29 |
* The command handler instance. |
| 30 |
* |
| 31 |
* @var Aggregate_Site_Schema_Command_Handler |
| 32 |
*/ |
| 33 |
private $aggregate_site_schema_command_handler; |
| 34 |
|
| 35 |
/** |
| 36 |
* Site_Schema_Aggregator_Cli_Command constructor. |
| 37 |
* |
| 38 |
* @param Config $config The config object. |
| 39 |
* @param Aggregate_Site_Schema_Command_Handler $aggregate_site_schema_command_handler The command handler. |
| 40 |
*/ |
| 41 |
public function __construct( Config $config, Aggregate_Site_Schema_Command_Handler $aggregate_site_schema_command_handler ) { |
| 42 |
$this->config = $config; |
| 43 |
$this->aggregate_site_schema_command_handler = $aggregate_site_schema_command_handler; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns the namespace of this command. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public static function get_namespace() { |
| 52 |
return Main::WP_CLI_NAMESPACE; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Aggregates the schema for a certain site. |
| 57 |
* |
| 58 |
* ## OPTIONS |
| 59 |
* |
| 60 |
* [--page=<page>] |
| 61 |
* : The current page to process. |
| 62 |
* --- |
| 63 |
* default: 1 |
| 64 |
* --- |
| 65 |
* |
| 66 |
* [--per_page=<per_page>] |
| 67 |
* : How many items to process per page. |
| 68 |
* --- |
| 69 |
* default: 100 |
| 70 |
* --- |
| 71 |
* |
| 72 |
* [--post_type=<post_type>] |
| 73 |
* : The post type to aggregate schema for. |
| 74 |
* --- |
| 75 |
* default: 'post' |
| 76 |
* --- |
| 77 |
* |
| 78 |
* ## EXAMPLES |
| 79 |
* |
| 80 |
* wp yoast aggregate_site_schema |
| 81 |
* |
| 82 |
* @when after_wp_load |
| 83 |
* |
| 84 |
* @param array<string>|null $args The arguments. |
| 85 |
* @param array<string>|null $assoc_args The associative arguments. |
| 86 |
* |
| 87 |
* @throws ExitException When the input args are invalid. |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function aggregate_site_schema( $args = null, $assoc_args = null ) { |
| 91 |
if ( isset( $assoc_args['page'] ) && (int) $assoc_args['page'] < 1 ) { |
| 92 |
WP_CLI::error( \__( 'The value for \'page\' must be a positive integer higher than equal to 1.', 'wordpress-seo' ) ); |
| 93 |
} |
| 94 |
if ( isset( $assoc_args['per_page'] ) && (int) $assoc_args['per_page'] < 1 ) { |
| 95 |
WP_CLI::error( \__( 'The value for \'per_page\' must be a positive integer higher than equal to 1.', 'wordpress-seo' ) ); |
| 96 |
} |
| 97 |
$page = (int) $assoc_args['page']; |
| 98 |
$per_page = (int) $assoc_args['per_page']; |
| 99 |
$post_type = $assoc_args['post_type']; |
| 100 |
try { |
| 101 |
$result = $this->aggregate_site_schema_command_handler->handle( new Aggregate_Site_Schema_Command( $page, $per_page, $post_type ) ); |
| 102 |
} catch ( Exception $exception ) { |
| 103 |
WP_CLI::error( \__( 'An error occurred while aggregating the site schema.', 'wordpress-seo' ) ); |
| 104 |
} |
| 105 |
$output = WPSEO_Utils::format_json_encode( $result ); |
| 106 |
$output = \str_replace( "\n", \PHP_EOL . "\t", $output ); |
| 107 |
WP_CLI::log( |
| 108 |
$output, |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
|