wordpress-seo
/
src
/
schema-aggregator
/
user-interface
/
site-schema-aggregator-cache-cli-command.php
site-schema-aggregator-cache-cli-command.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/schema-aggregator/user-interface/site-schema-aggregator-cache-cli-command.php
| 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 Yoast\WP\SEO\Commands\Command_Interface; |
| 8 | use Yoast\WP\SEO\Main; |
| 9 | use Yoast\WP\SEO\Schema_Aggregator\Application\Cache\Manager; |
| 10 | use Yoast\WP\SEO\Schema_Aggregator\Application\Cache\Xml_Manager; |
| 11 | use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Config; |
| 12 | |
| 13 | /** |
| 14 | * Handles the CLI command to represent a site's schema as JSON. |
| 15 | * |
| 16 | * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 17 | */ |
| 18 | class Site_Schema_Aggregator_Cache_Cli_Command implements Command_Interface { |
| 19 | |
| 20 | /** |
| 21 | * The configuration instance. |
| 22 | * |
| 23 | * @var Config |
| 24 | */ |
| 25 | private $config; |
| 26 | |
| 27 | /** |
| 28 | * The The cache manager instance. |
| 29 | * |
| 30 | * @var Manager |
| 31 | */ |
| 32 | private $cache_manager; |
| 33 | |
| 34 | /** |
| 35 | * The XML cache manager instance. |
| 36 | * |
| 37 | * @var Xml_Manager |
| 38 | */ |
| 39 | private $xml_manager; |
| 40 | |
| 41 | /** |
| 42 | * Site_Schema_Aggregator_Cache_Cli_Command constructor. |
| 43 | * |
| 44 | * @param Config $config The config object. |
| 45 | * @param Manager $cache_manager The cache manager. |
| 46 | * @param Xml_Manager $xml_manager The XML cache manager. |
| 47 | */ |
| 48 | public function __construct( Config $config, Manager $cache_manager, Xml_Manager $xml_manager ) { |
| 49 | $this->config = $config; |
| 50 | $this->cache_manager = $cache_manager; |
| 51 | $this->xml_manager = $xml_manager; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the namespace of this command. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public static function get_namespace() { |
| 60 | return Main::WP_CLI_NAMESPACE; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Aggregates the schema for a certain site. |
| 65 | * |
| 66 | * ## OPTIONS |
| 67 | * |
| 68 | * [--post_type=<post_type>] |
| 69 | * : The current page to process. |
| 70 | * [--page=<page>] |
| 71 | * : The current page to process. |
| 72 | * --- |
| 73 | * ## EXAMPLES |
| 74 | * |
| 75 | * wp yoast aggregate_site_schema_clear_cache |
| 76 | * |
| 77 | * @when after_wp_load |
| 78 | * |
| 79 | * @param array<string>|null $args The arguments. |
| 80 | * @param array<string>|null $assoc_args The associative arguments. |
| 81 | * |
| 82 | * @throws ExitException When the input args are invalid. |
| 83 | * @return void |
| 84 | */ |
| 85 | public function aggregate_site_schema_clear_cache( $args = null, $assoc_args = null ) { |
| 86 | if ( ( isset( $assoc_args['page'] ) && (int) $assoc_args['page'] >= 1 ) && isset( $assoc_args['post_type'] ) ) { |
| 87 | $this->cache_manager->invalidate( $assoc_args['post_type'], $assoc_args['page'] ); |
| 88 | $this->xml_manager->invalidate(); |
| 89 | WP_CLI::log( |
| 90 | \__( 'The site schema cache has been cleared successfully.', 'wordpress-seo' ), |
| 91 | ); |
| 92 | |
| 93 | return; |
| 94 | } |
| 95 | $this->cache_manager->invalidate_all(); |
| 96 | $this->xml_manager->invalidate(); |
| 97 | |
| 98 | WP_CLI::log( |
| 99 | \__( 'All site schema cache has been cleared successfully.', 'wordpress-seo' ), |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 |