| @@ -13,8 +13,10 @@ | ||
| 13 | 13 | use Yoast\WP\SEO\Actions\Indexing\Indexation_Action_Interface; |
| 14 | 14 | use Yoast\WP\SEO\Actions\Indexing\Indexing_Prepare_Action; |
| 15 | 15 | use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action; |
| 16 | 16 | use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action; |
| 17 | +use Yoast\WP\SEO\Exceptions\Indexable\Indexing_Failed_Exception; | |
| 18 | +use Yoast\WP\SEO\Helpers\Indexable_Helper; | |
| 17 | 19 | use Yoast\WP\SEO\Main; |
| 18 | 20 | |
| 19 | 21 | /** |
| 20 | 22 | * Command to generate indexables for all posts and terms. |
| @@ -77,8 +79,15 @@ | ||
| 77 | 79 | */ |
| 78 | 80 | private $prepare_indexing_action; |
| 79 | 81 | |
| 80 | 82 | /** |
| 83 | + * Represents the indexable helper. | |
| 84 | + * | |
| 85 | + * @var Indexable_Helper | |
| 86 | + */ | |
| 87 | + protected $indexable_helper; | |
| 88 | + | |
| 89 | + /** | |
| 81 | 90 | * Generate_Indexables_Command constructor. |
| 82 | 91 | * |
| 83 | 92 | * @param Indexable_Post_Indexation_Action $post_indexation_action The post indexation |
| 84 | 93 | * action. |
| @@ -95,8 +104,9 @@ | ||
| 95 | 104 | * @param Post_Link_Indexing_Action $post_link_indexing_action The post link indexation |
| 96 | 105 | * action. |
| 97 | 106 | * @param Term_Link_Indexing_Action $term_link_indexing_action The term link indexation |
| 98 | 107 | * action. |
| 108 | + * @param Indexable_Helper $indexable_helper The indexable helper. | |
| 99 | 109 | */ |
| 100 | 110 | public function __construct( |
| 101 | 111 | Indexable_Post_Indexation_Action $post_indexation_action, |
| 102 | 112 | Indexable_Term_Indexation_Action $term_indexation_action, |
| @@ -104,9 +114,10 @@ | ||
| 104 | 114 | Indexable_General_Indexation_Action $general_indexation_action, |
| 105 | 115 | Indexable_Indexing_Complete_Action $complete_indexation_action, |
| 106 | 116 | Indexing_Prepare_Action $prepare_indexing_action, |
| 107 | 117 | Post_Link_Indexing_Action $post_link_indexing_action, |
| 108 | - Term_Link_Indexing_Action $term_link_indexing_action | |
| 118 | + Term_Link_Indexing_Action $term_link_indexing_action, | |
| 119 | + Indexable_Helper $indexable_helper | |
| 109 | 120 | ) { |
| 110 | 121 | $this->post_indexation_action = $post_indexation_action; |
| 111 | 122 | $this->term_indexation_action = $term_indexation_action; |
| 112 | 123 | $this->post_type_archive_indexation_action = $post_type_archive_indexation_action; |
| @@ -114,8 +125,9 @@ | ||
| 114 | 125 | $this->complete_indexation_action = $complete_indexation_action; |
| 115 | 126 | $this->prepare_indexing_action = $prepare_indexing_action; |
| 116 | 127 | $this->post_link_indexing_action = $post_link_indexing_action; |
| 117 | 128 | $this->term_link_indexing_action = $term_link_indexing_action; |
| 129 | + $this->indexable_helper = $indexable_helper; | |
| 118 | 130 | } |
| 119 | 131 | |
| 120 | 132 | /** |
| 121 | 133 | * Gets the namespace. |
| @@ -151,14 +163,22 @@ | ||
| 151 | 163 | * wp yoast index |
| 152 | 164 | * |
| 153 | 165 | * @when after_wp_load |
| 154 | 166 | * |
| 155 | - * @param array|null $args The arguments. | |
| 156 | - * @param array|null $assoc_args The associative arguments. | |
| 167 | + * @param array<string>|null $args The arguments. | |
| 168 | + * @param array<string, string|bool>|null $assoc_args The associative arguments. | |
| 157 | 169 | * |
| 158 | 170 | * @return void |
| 159 | 171 | */ |
| 160 | 172 | public function index( $args = null, $assoc_args = null ) { |
| 173 | + if ( ! $this->indexable_helper->should_index_indexables() ) { | |
| 174 | + WP_CLI::log( | |
| 175 | + \__( 'Your WordPress environment is running on a non-production site. Indexables can only be created on production environments. Please check your `WP_ENVIRONMENT_TYPE` settings.', 'wordpress-seo' ), | |
| 176 | + ); | |
| 177 | + | |
| 178 | + return; | |
| 179 | + } | |
| 180 | + | |
| 161 | 181 | if ( ! isset( $assoc_args['network'] ) ) { |
| 162 | 182 | $this->run_indexation_actions( $assoc_args ); |
| 163 | 183 | |
| 164 | 184 | return; |
| @@ -182,9 +202,9 @@ | ||
| 182 | 202 | |
| 183 | 203 | /** |
| 184 | 204 | * Runs all indexation actions. |
| 185 | 205 | * |
| 186 | - * @param array $assoc_args The associative arguments. | |
| 206 | + * @param array<string, string|bool> $assoc_args The associative arguments. | |
| 187 | 207 | * |
| 188 | 208 | * @return void |
| 189 | 209 | */ |
| 190 | 210 | protected function run_indexation_actions( $assoc_args ) { |
| @@ -238,12 +258,28 @@ | ||
| 238 | 258 | if ( $total > 0 ) { |
| 239 | 259 | $limit = $indexation_action->get_limit(); |
| 240 | 260 | $progress = Utils\make_progress_bar( 'Indexing ' . $name, $total ); |
| 241 | 261 | do { |
| 242 | - $indexables = $indexation_action->index(); | |
| 243 | - $count = \count( $indexables ); | |
| 262 | + try { | |
| 263 | + $indexables = $indexation_action->index(); | |
| 264 | + } catch ( Indexing_Failed_Exception $exception ) { | |
| 265 | + $progress->finish(); | |
| 266 | + | |
| 267 | + $previous = $exception->getPrevious(); | |
| 268 | + WP_CLI::error( | |
| 269 | + \sprintf( | |
| 270 | + 'Could not optimize %1$s while indexing %2$s: %3$s', | |
| 271 | + $exception->get_object_description(), | |
| 272 | + $name, | |
| 273 | + ( $previous !== null ) ? $previous->getMessage() : $exception->getMessage(), | |
| 274 | + ), | |
| 275 | + ); | |
| 276 | + | |
| 277 | + return; | |
| 278 | + } | |
| 279 | + $count = \count( $indexables ); | |
| 244 | 280 | $progress->tick( $count ); |
| 245 | - usleep( $interval ); | |
| 281 | + \usleep( $interval ); | |
| 246 | 282 | Utils\wp_clear_object_cache(); |
| 247 | 283 | } while ( $count >= $limit ); |
| 248 | 284 | $progress->finish(); |
| 249 | 285 | } |
| @@ -250,8 +286,10 @@ | ||
| 250 | 286 | } |
| 251 | 287 | |
| 252 | 288 | /** |
| 253 | 289 | * Clears the database related to the indexables. |
| 290 | + * | |
| 291 | + * @return void | |
| 254 | 292 | */ |
| 255 | 293 | protected function clear() { |
| 256 | 294 | global $wpdb; |
| 257 | 295 | |
| @@ -260,16 +298,16 @@ | ||
| 260 | 298 | // phpcs:disable WordPress.DB -- Table names should not be quoted and truncate queries can not be cached. |
| 261 | 299 | $wpdb->query( |
| 262 | 300 | $wpdb->prepare( |
| 263 | 301 | 'TRUNCATE TABLE %1$s', |
| 264 | - Model::get_table_name( 'Indexable' ) | |
| 265 | - ) | |
| 302 | + Model::get_table_name( 'Indexable' ), | |
| 303 | + ), | |
| 266 | 304 | ); |
| 267 | 305 | $wpdb->query( |
| 268 | 306 | $wpdb->prepare( |
| 269 | 307 | 'TRUNCATE TABLE %1$s', |
| 270 | - Model::get_table_name( 'Indexable_Hierarchy' ) | |
| 271 | - ) | |
| 308 | + Model::get_table_name( 'Indexable_Hierarchy' ), | |
| 309 | + ), | |
| 272 | 310 | ); |
| 273 | 311 | // phpcs:enable |
| 274 | 312 | } |
| 275 | 313 | } |