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