| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Routes; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use WP_Error; |
| 7 |
use WP_REST_Response; |
| 8 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_General_Indexation_Action; |
| 9 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Indexing_Complete_Action; |
| 10 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action; |
| 11 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action; |
| 12 |
use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action; |
| 13 |
use Yoast\WP\SEO\Actions\Indexing\Indexation_Action_Interface; |
| 14 |
use Yoast\WP\SEO\Actions\Indexing\Indexing_Complete_Action; |
| 15 |
use Yoast\WP\SEO\Actions\Indexing\Indexing_Prepare_Action; |
| 16 |
use Yoast\WP\SEO\Actions\Indexing\Post_Link_Indexing_Action; |
| 17 |
use Yoast\WP\SEO\Actions\Indexing\Term_Link_Indexing_Action; |
| 18 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 19 |
use Yoast\WP\SEO\Exceptions\Indexable\Indexing_Failed_Exception; |
| 20 |
use Yoast\WP\SEO\Helpers\Indexing_Helper; |
| 21 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 22 |
use Yoast\WP\SEO\Main; |
| 23 |
|
| 24 |
/** |
| 25 |
* Indexing_Route class. |
| 26 |
* |
| 27 |
* Indexing route for indexables. |
| 28 |
*/ |
| 29 |
class Indexing_Route extends Abstract_Indexation_Route { |
| 30 |
|
| 31 |
use No_Conditionals; |
| 32 |
|
| 33 |
/** |
| 34 |
* The indexing complete route constant. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public const COMPLETE_ROUTE = 'indexing/complete'; |
| 39 |
|
| 40 |
/** |
| 41 |
* The full indexing complete route constant. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public const FULL_COMPLETE_ROUTE = Main::API_V1_NAMESPACE . '/' . self::COMPLETE_ROUTE; |
| 46 |
|
| 47 |
/** |
| 48 |
* The indexables complete route constant. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public const INDEXABLES_COMPLETE_ROUTE = 'indexing/indexables-complete'; |
| 53 |
|
| 54 |
/** |
| 55 |
* The full indexing complete route constant. |
| 56 |
* |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
public const FULL_INDEXABLES_COMPLETE_ROUTE = Main::API_V1_NAMESPACE . '/' . self::INDEXABLES_COMPLETE_ROUTE; |
| 60 |
|
| 61 |
/** |
| 62 |
* The indexing prepare route constant. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
public const PREPARE_ROUTE = 'indexing/prepare'; |
| 67 |
|
| 68 |
/** |
| 69 |
* The full indexing prepare route constant. |
| 70 |
* |
| 71 |
* @var string |
| 72 |
*/ |
| 73 |
public const FULL_PREPARE_ROUTE = Main::API_V1_NAMESPACE . '/' . self::PREPARE_ROUTE; |
| 74 |
|
| 75 |
/** |
| 76 |
* The posts route constant. |
| 77 |
* |
| 78 |
* @var string |
| 79 |
*/ |
| 80 |
public const POSTS_ROUTE = 'indexing/posts'; |
| 81 |
|
| 82 |
/** |
| 83 |
* The full posts route constant. |
| 84 |
* |
| 85 |
* @var string |
| 86 |
*/ |
| 87 |
public const FULL_POSTS_ROUTE = Main::API_V1_NAMESPACE . '/' . self::POSTS_ROUTE; |
| 88 |
|
| 89 |
/** |
| 90 |
* The terms route constant. |
| 91 |
* |
| 92 |
* @var string |
| 93 |
*/ |
| 94 |
public const TERMS_ROUTE = 'indexing/terms'; |
| 95 |
|
| 96 |
/** |
| 97 |
* The full terms route constant. |
| 98 |
* |
| 99 |
* @var string |
| 100 |
*/ |
| 101 |
public const FULL_TERMS_ROUTE = Main::API_V1_NAMESPACE . '/' . self::TERMS_ROUTE; |
| 102 |
|
| 103 |
/** |
| 104 |
* The terms route constant. |
| 105 |
* |
| 106 |
* @var string |
| 107 |
*/ |
| 108 |
public const POST_TYPE_ARCHIVES_ROUTE = 'indexing/post-type-archives'; |
| 109 |
|
| 110 |
/** |
| 111 |
* The full terms route constant. |
| 112 |
* |
| 113 |
* @var string |
| 114 |
*/ |
| 115 |
public const FULL_POST_TYPE_ARCHIVES_ROUTE = Main::API_V1_NAMESPACE . '/' . self::POST_TYPE_ARCHIVES_ROUTE; |
| 116 |
|
| 117 |
/** |
| 118 |
* The general route constant. |
| 119 |
* |
| 120 |
* @var string |
| 121 |
*/ |
| 122 |
public const GENERAL_ROUTE = 'indexing/general'; |
| 123 |
|
| 124 |
/** |
| 125 |
* The full general route constant. |
| 126 |
* |
| 127 |
* @var string |
| 128 |
*/ |
| 129 |
public const FULL_GENERAL_ROUTE = Main::API_V1_NAMESPACE . '/' . self::GENERAL_ROUTE; |
| 130 |
|
| 131 |
/** |
| 132 |
* The posts route constant. |
| 133 |
* |
| 134 |
* @var string |
| 135 |
*/ |
| 136 |
public const POST_LINKS_INDEXING_ROUTE = 'link-indexing/posts'; |
| 137 |
|
| 138 |
/** |
| 139 |
* The full posts route constant. |
| 140 |
* |
| 141 |
* @var string |
| 142 |
*/ |
| 143 |
public const FULL_POST_LINKS_INDEXING_ROUTE = Main::API_V1_NAMESPACE . '/' . self::POST_LINKS_INDEXING_ROUTE; |
| 144 |
|
| 145 |
/** |
| 146 |
* The terms route constant. |
| 147 |
* |
| 148 |
* @var string |
| 149 |
*/ |
| 150 |
public const TERM_LINKS_INDEXING_ROUTE = 'link-indexing/terms'; |
| 151 |
|
| 152 |
/** |
| 153 |
* The full terms route constant. |
| 154 |
* |
| 155 |
* @var string |
| 156 |
*/ |
| 157 |
public const FULL_TERM_LINKS_INDEXING_ROUTE = Main::API_V1_NAMESPACE . '/' . self::TERM_LINKS_INDEXING_ROUTE; |
| 158 |
|
| 159 |
/** |
| 160 |
* The post indexing action. |
| 161 |
* |
| 162 |
* @var Indexable_Post_Indexation_Action |
| 163 |
*/ |
| 164 |
protected $post_indexation_action; |
| 165 |
|
| 166 |
/** |
| 167 |
* The term indexing action. |
| 168 |
* |
| 169 |
* @var Indexable_Term_Indexation_Action |
| 170 |
*/ |
| 171 |
protected $term_indexation_action; |
| 172 |
|
| 173 |
/** |
| 174 |
* The post type archive indexing action. |
| 175 |
* |
| 176 |
* @var Indexable_Post_Type_Archive_Indexation_Action |
| 177 |
*/ |
| 178 |
protected $post_type_archive_indexation_action; |
| 179 |
|
| 180 |
/** |
| 181 |
* Represents the general indexing action. |
| 182 |
* |
| 183 |
* @var Indexable_General_Indexation_Action |
| 184 |
*/ |
| 185 |
protected $general_indexation_action; |
| 186 |
|
| 187 |
/** |
| 188 |
* The prepare indexing action. |
| 189 |
* |
| 190 |
* @var Indexing_Prepare_Action |
| 191 |
*/ |
| 192 |
protected $prepare_indexing_action; |
| 193 |
|
| 194 |
/** |
| 195 |
* The indexable indexing complete action. |
| 196 |
* |
| 197 |
* @var Indexable_Indexing_Complete_Action |
| 198 |
*/ |
| 199 |
protected $indexable_indexing_complete_action; |
| 200 |
|
| 201 |
/** |
| 202 |
* The indexing complete action. |
| 203 |
* |
| 204 |
* @var Indexing_Complete_Action |
| 205 |
*/ |
| 206 |
protected $indexing_complete_action; |
| 207 |
|
| 208 |
/** |
| 209 |
* The post link indexing action. |
| 210 |
* |
| 211 |
* @var Post_Link_Indexing_Action |
| 212 |
*/ |
| 213 |
protected $post_link_indexing_action; |
| 214 |
|
| 215 |
/** |
| 216 |
* The term link indexing action. |
| 217 |
* |
| 218 |
* @var Term_Link_Indexing_Action |
| 219 |
*/ |
| 220 |
protected $term_link_indexing_action; |
| 221 |
|
| 222 |
/** |
| 223 |
* The options helper. |
| 224 |
* |
| 225 |
* @var Options_Helper |
| 226 |
*/ |
| 227 |
protected $options_helper; |
| 228 |
|
| 229 |
/** |
| 230 |
* The indexing helper. |
| 231 |
* |
| 232 |
* @var Indexing_Helper |
| 233 |
*/ |
| 234 |
protected $indexing_helper; |
| 235 |
|
| 236 |
/** |
| 237 |
* Indexing_Route constructor. |
| 238 |
* |
| 239 |
* @param Indexable_Post_Indexation_Action $post_indexation_action The post indexing action. |
| 240 |
* @param Indexable_Term_Indexation_Action $term_indexation_action The term indexing action. |
| 241 |
* @param Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation_action The post type archive indexing action. |
| 242 |
* @param Indexable_General_Indexation_Action $general_indexation_action The general indexing action. |
| 243 |
* @param Indexable_Indexing_Complete_Action $indexable_indexing_complete_action The complete indexing action. |
| 244 |
* @param Indexing_Complete_Action $indexing_complete_action The complete indexing action. |
| 245 |
* @param Indexing_Prepare_Action $prepare_indexing_action The prepare indexing action. |
| 246 |
* @param Post_Link_Indexing_Action $post_link_indexing_action The post link indexing action. |
| 247 |
* @param Term_Link_Indexing_Action $term_link_indexing_action The term link indexing action. |
| 248 |
* @param Options_Helper $options_helper The options helper. |
| 249 |
* @param Indexing_Helper $indexing_helper The indexing helper. |
| 250 |
*/ |
| 251 |
public function __construct( |
| 252 |
Indexable_Post_Indexation_Action $post_indexation_action, |
| 253 |
Indexable_Term_Indexation_Action $term_indexation_action, |
| 254 |
Indexable_Post_Type_Archive_Indexation_Action $post_type_archive_indexation_action, |
| 255 |
Indexable_General_Indexation_Action $general_indexation_action, |
| 256 |
Indexable_Indexing_Complete_Action $indexable_indexing_complete_action, |
| 257 |
Indexing_Complete_Action $indexing_complete_action, |
| 258 |
Indexing_Prepare_Action $prepare_indexing_action, |
| 259 |
Post_Link_Indexing_Action $post_link_indexing_action, |
| 260 |
Term_Link_Indexing_Action $term_link_indexing_action, |
| 261 |
Options_Helper $options_helper, |
| 262 |
Indexing_Helper $indexing_helper |
| 263 |
) { |
| 264 |
$this->post_indexation_action = $post_indexation_action; |
| 265 |
$this->term_indexation_action = $term_indexation_action; |
| 266 |
$this->post_type_archive_indexation_action = $post_type_archive_indexation_action; |
| 267 |
$this->general_indexation_action = $general_indexation_action; |
| 268 |
$this->indexable_indexing_complete_action = $indexable_indexing_complete_action; |
| 269 |
$this->indexing_complete_action = $indexing_complete_action; |
| 270 |
$this->prepare_indexing_action = $prepare_indexing_action; |
| 271 |
$this->options_helper = $options_helper; |
| 272 |
$this->post_link_indexing_action = $post_link_indexing_action; |
| 273 |
$this->term_link_indexing_action = $term_link_indexing_action; |
| 274 |
$this->indexing_helper = $indexing_helper; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Registers the routes used to index indexables. |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function register_routes() { |
| 283 |
$route_args = [ |
| 284 |
'methods' => 'POST', |
| 285 |
'callback' => [ $this, 'index_posts' ], |
| 286 |
'permission_callback' => [ $this, 'can_index' ], |
| 287 |
]; |
| 288 |
\register_rest_route( Main::API_V1_NAMESPACE, self::POSTS_ROUTE, $route_args ); |
| 289 |
|
| 290 |
$route_args['callback'] = [ $this, 'index_terms' ]; |
| 291 |
\register_rest_route( Main::API_V1_NAMESPACE, self::TERMS_ROUTE, $route_args ); |
| 292 |
|
| 293 |
$route_args['callback'] = [ $this, 'index_post_type_archives' ]; |
| 294 |
\register_rest_route( Main::API_V1_NAMESPACE, self::POST_TYPE_ARCHIVES_ROUTE, $route_args ); |
| 295 |
|
| 296 |
$route_args['callback'] = [ $this, 'index_general' ]; |
| 297 |
\register_rest_route( Main::API_V1_NAMESPACE, self::GENERAL_ROUTE, $route_args ); |
| 298 |
|
| 299 |
$route_args['callback'] = [ $this, 'prepare' ]; |
| 300 |
\register_rest_route( Main::API_V1_NAMESPACE, self::PREPARE_ROUTE, $route_args ); |
| 301 |
|
| 302 |
$route_args['callback'] = [ $this, 'indexables_complete' ]; |
| 303 |
\register_rest_route( Main::API_V1_NAMESPACE, self::INDEXABLES_COMPLETE_ROUTE, $route_args ); |
| 304 |
|
| 305 |
$route_args['callback'] = [ $this, 'complete' ]; |
| 306 |
\register_rest_route( Main::API_V1_NAMESPACE, self::COMPLETE_ROUTE, $route_args ); |
| 307 |
|
| 308 |
$route_args['callback'] = [ $this, 'index_post_links' ]; |
| 309 |
\register_rest_route( Main::API_V1_NAMESPACE, self::POST_LINKS_INDEXING_ROUTE, $route_args ); |
| 310 |
|
| 311 |
$route_args['callback'] = [ $this, 'index_term_links' ]; |
| 312 |
\register_rest_route( Main::API_V1_NAMESPACE, self::TERM_LINKS_INDEXING_ROUTE, $route_args ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Indexes a number of unindexed posts. |
| 317 |
* |
| 318 |
* @return WP_REST_Response The response. |
| 319 |
*/ |
| 320 |
public function index_posts() { |
| 321 |
return $this->run_indexation_action( $this->post_indexation_action, self::FULL_POSTS_ROUTE ); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Indexes a number of unindexed terms. |
| 326 |
* |
| 327 |
* @return WP_REST_Response The response. |
| 328 |
*/ |
| 329 |
public function index_terms() { |
| 330 |
return $this->run_indexation_action( $this->term_indexation_action, self::FULL_TERMS_ROUTE ); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Indexes a number of unindexed post type archive pages. |
| 335 |
* |
| 336 |
* @return WP_REST_Response The response. |
| 337 |
*/ |
| 338 |
public function index_post_type_archives() { |
| 339 |
return $this->run_indexation_action( $this->post_type_archive_indexation_action, self::FULL_POST_TYPE_ARCHIVES_ROUTE ); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Indexes a number of unindexed general items. |
| 344 |
* |
| 345 |
* @return WP_REST_Response The response. |
| 346 |
*/ |
| 347 |
public function index_general() { |
| 348 |
return $this->run_indexation_action( $this->general_indexation_action, self::FULL_GENERAL_ROUTE ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Indexes a number of posts for post links. |
| 353 |
* |
| 354 |
* @return WP_REST_Response The response. |
| 355 |
*/ |
| 356 |
public function index_post_links() { |
| 357 |
return $this->run_indexation_action( $this->post_link_indexing_action, self::FULL_POST_LINKS_INDEXING_ROUTE ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Indexes a number of terms for term links. |
| 362 |
* |
| 363 |
* @return WP_REST_Response The response. |
| 364 |
*/ |
| 365 |
public function index_term_links() { |
| 366 |
return $this->run_indexation_action( $this->term_link_indexing_action, self::FULL_TERM_LINKS_INDEXING_ROUTE ); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Prepares the indexation. |
| 371 |
* |
| 372 |
* @return WP_REST_Response The response. |
| 373 |
*/ |
| 374 |
public function prepare() { |
| 375 |
$this->prepare_indexing_action->prepare(); |
| 376 |
|
| 377 |
return $this->respond_with( [], false ); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Completes the indexable indexation. |
| 382 |
* |
| 383 |
* @return WP_REST_Response The response. |
| 384 |
*/ |
| 385 |
public function indexables_complete() { |
| 386 |
$this->indexable_indexing_complete_action->complete(); |
| 387 |
|
| 388 |
return $this->respond_with( [], false ); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Completes the indexation. |
| 393 |
* |
| 394 |
* @return WP_REST_Response The response. |
| 395 |
*/ |
| 396 |
public function complete() { |
| 397 |
$this->indexing_complete_action->complete(); |
| 398 |
|
| 399 |
return $this->respond_with( [], false ); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Whether or not the current user is allowed to index. |
| 404 |
* |
| 405 |
* @return bool Whether or not the current user is allowed to index. |
| 406 |
*/ |
| 407 |
public function can_index() { |
| 408 |
return \current_user_can( 'edit_posts' ); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Runs an indexing action and returns the response. |
| 413 |
* |
| 414 |
* @param Indexation_Action_Interface $indexation_action The indexing action. |
| 415 |
* @param string $url The url of the indexing route. |
| 416 |
* |
| 417 |
* @return WP_REST_Response|WP_Error The response, or an error when running the indexing action failed. |
| 418 |
*/ |
| 419 |
protected function run_indexation_action( Indexation_Action_Interface $indexation_action, $url ) { |
| 420 |
try { |
| 421 |
return parent::run_indexation_action( $indexation_action, $url ); |
| 422 |
} catch ( Indexing_Failed_Exception $exception ) { |
| 423 |
$this->indexing_helper->indexing_failed(); |
| 424 |
|
| 425 |
$previous = $exception->getPrevious(); |
| 426 |
|
| 427 |
return new WP_Error( |
| 428 |
'wpseo_error_indexing', |
| 429 |
$exception->getMessage(), |
| 430 |
[ |
| 431 |
'stackTrace' => ( $previous !== null ) ? $previous->getTraceAsString() : $exception->getTraceAsString(), |
| 432 |
'object_id' => $exception->get_object_id(), |
| 433 |
'object_type' => $exception->get_object_type(), |
| 434 |
], |
| 435 |
); |
| 436 |
} catch ( Exception $exception ) { |
| 437 |
$this->indexing_helper->indexing_failed(); |
| 438 |
|
| 439 |
return new WP_Error( |
| 440 |
'wpseo_error_indexing', |
| 441 |
$exception->getMessage(), |
| 442 |
[ 'stackTrace' => $exception->getTraceAsString() ], |
| 443 |
); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
|