← All changes
|
src/actions/importing/aioseo/aioseo-posts-importing-action.php
+64
-52
18.4
→
trunk
View file →
| @@ -10,9 +10,8 @@ | ||
| 10 | 10 | use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 11 | 11 | use Yoast\WP\SEO\Helpers\Indexable_To_Postmeta_Helper; |
| 12 | 12 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 13 | 13 | use Yoast\WP\SEO\Helpers\Sanitization_Helper; |
| 14 | -use Yoast\WP\SEO\Helpers\Wpdb_Helper; | |
| 15 | 14 | use Yoast\WP\SEO\Models\Indexable; |
| 16 | 15 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 17 | 16 | use Yoast\WP\SEO\Services\Importing\Aioseo\Aioseo_Replacevar_Service; |
| 18 | 17 | use Yoast\WP\SEO\Services\Importing\Aioseo\Aioseo_Robots_Provider_Service; |
| @@ -20,10 +19,8 @@ | ||
| 20 | 19 | use Yoast\WP\SEO\Services\Importing\Aioseo\Aioseo_Social_Images_Provider_Service; |
| 21 | 20 | |
| 22 | 21 | /** |
| 23 | 22 | * Importing action for AIOSEO post data. |
| 24 | - * | |
| 25 | - * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded | |
| 26 | 23 | */ |
| 27 | 24 | class Aioseo_Posts_Importing_Action extends Abstract_Aioseo_Importing_Action { |
| 28 | 25 | |
| 29 | 26 | /** |
| @@ -28,19 +25,19 @@ | ||
| 28 | 25 | |
| 29 | 26 | /** |
| 30 | 27 | * The plugin of the action. |
| 31 | 28 | */ |
| 32 | - const PLUGIN = 'aioseo'; | |
| 29 | + public const PLUGIN = 'aioseo'; | |
| 33 | 30 | |
| 34 | 31 | /** |
| 35 | 32 | * The type of the action. |
| 36 | 33 | */ |
| 37 | - const TYPE = 'posts'; | |
| 34 | + public const TYPE = 'posts'; | |
| 38 | 35 | |
| 39 | 36 | /** |
| 40 | 37 | * The map of aioseo to yoast meta. |
| 41 | 38 | * |
| 42 | - * @var array | |
| 39 | + * @var array<string, array<string, string|bool|array<string, string|bool>>> | |
| 43 | 40 | */ |
| 44 | 41 | protected $aioseo_to_yoast_map = [ |
| 45 | 42 | 'title' => [ |
| 46 | 43 | 'yoast_name' => 'title', |
| @@ -139,9 +136,9 @@ | ||
| 139 | 136 | * The image helper. |
| 140 | 137 | * |
| 141 | 138 | * @var Image_Helper |
| 142 | 139 | */ |
| 143 | - protected $image_helper; | |
| 140 | + protected $image; | |
| 144 | 141 | |
| 145 | 142 | /** |
| 146 | 143 | * The indexable_to_postmeta helper. |
| 147 | 144 | * |
| @@ -156,15 +153,8 @@ | ||
| 156 | 153 | */ |
| 157 | 154 | protected $indexable_helper; |
| 158 | 155 | |
| 159 | 156 | /** |
| 160 | - * The wpdb helper. | |
| 161 | - * | |
| 162 | - * @var Wpdb_Helper | |
| 163 | - */ | |
| 164 | - protected $wpdb_helper; | |
| 165 | - | |
| 166 | - /** | |
| 167 | 157 | * The social images provider service. |
| 168 | 158 | * |
| 169 | 159 | * @var Aioseo_Social_Images_Provider_Service |
| 170 | 160 | */ |
| @@ -180,9 +170,8 @@ | ||
| 180 | 170 | * @param Indexable_To_Postmeta_Helper $indexable_to_postmeta The indexable_to_postmeta helper. |
| 181 | 171 | * @param Options_Helper $options The options helper. |
| 182 | 172 | * @param Image_Helper $image The image helper. |
| 183 | 173 | * @param Sanitization_Helper $sanitization The sanitization helper. |
| 184 | - * @param Wpdb_Helper $wpdb_helper The wpdb_helper helper. | |
| 185 | 174 | * @param Aioseo_Replacevar_Service $replacevar_handler The replacevar handler. |
| 186 | 175 | * @param Aioseo_Robots_Provider_Service $robots_provider The robots provider service. |
| 187 | 176 | * @param Aioseo_Robots_Transformer_Service $robots_transformer The robots transfomer service. |
| 188 | 177 | * @param Aioseo_Social_Images_Provider_Service $social_images_provider The social images provider service. |
| @@ -195,13 +184,13 @@ | ||
| 195 | 184 | Indexable_To_Postmeta_Helper $indexable_to_postmeta, |
| 196 | 185 | Options_Helper $options, |
| 197 | 186 | Image_Helper $image, |
| 198 | 187 | Sanitization_Helper $sanitization, |
| 199 | - Wpdb_Helper $wpdb_helper, | |
| 200 | 188 | Aioseo_Replacevar_Service $replacevar_handler, |
| 201 | 189 | Aioseo_Robots_Provider_Service $robots_provider, |
| 202 | 190 | Aioseo_Robots_Transformer_Service $robots_transformer, |
| 203 | - Aioseo_Social_Images_Provider_Service $social_images_provider ) { | |
| 191 | + Aioseo_Social_Images_Provider_Service $social_images_provider | |
| 192 | + ) { | |
| 204 | 193 | parent::__construct( $import_cursor, $options, $sanitization, $replacevar_handler, $robots_provider, $robots_transformer ); |
| 205 | 194 | |
| 206 | 195 | $this->indexable_repository = $indexable_repository; |
| 207 | 196 | $this->wpdb = $wpdb; |
| @@ -207,30 +196,11 @@ | ||
| 207 | 196 | $this->wpdb = $wpdb; |
| 208 | 197 | $this->image = $image; |
| 209 | 198 | $this->indexable_helper = $indexable_helper; |
| 210 | 199 | $this->indexable_to_postmeta = $indexable_to_postmeta; |
| 211 | - $this->wpdb_helper = $wpdb_helper; | |
| 212 | 200 | $this->social_images_provider = $social_images_provider; |
| 213 | 201 | } |
| 214 | 202 | |
| 215 | - /** | |
| 216 | - * Retrieves the AIOSEO table name along with the db prefix. | |
| 217 | - * | |
| 218 | - * @return string The AIOSEO table name along with the db prefix. | |
| 219 | - */ | |
| 220 | - protected function get_table() { | |
| 221 | - return $this->wpdb->prefix . 'aioseo_posts'; | |
| 222 | - } | |
| 223 | - | |
| 224 | - /** | |
| 225 | - * Determines if the AIOSEO database table exists. | |
| 226 | - * | |
| 227 | - * @return bool True if the table is found. | |
| 228 | - */ | |
| 229 | - protected function aioseo_exists() { | |
| 230 | - return $this->wpdb_helper->table_exists( $this->get_table() ) === true; | |
| 231 | - } | |
| 232 | - | |
| 233 | 203 | // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: They are already prepared. |
| 234 | 204 | |
| 235 | 205 | /** |
| 236 | 206 | * Returns the total number of unimported objects. |
| @@ -237,9 +207,9 @@ | ||
| 237 | 207 | * |
| 238 | 208 | * @return int The total number of unimported objects. |
| 239 | 209 | */ |
| 240 | 210 | public function get_total_unindexed() { |
| 241 | - if ( ! $this->aioseo_exists() ) { | |
| 211 | + if ( ! $this->aioseo_helper->aioseo_exists() ) { | |
| 242 | 212 | return 0; |
| 243 | 213 | } |
| 244 | 214 | |
| 245 | 215 | $limit = false; |
| @@ -260,9 +230,9 @@ | ||
| 260 | 230 | * |
| 261 | 231 | * @return int|false The limited number of unindexed posts. False if the query fails. |
| 262 | 232 | */ |
| 263 | 233 | public function get_limited_unindexed_count( $limit ) { |
| 264 | - if ( ! $this->aioseo_exists() ) { | |
| 234 | + if ( ! $this->aioseo_helper->aioseo_exists() ) { | |
| 265 | 235 | return 0; |
| 266 | 236 | } |
| 267 | 237 | |
| 268 | 238 | $just_detect = true; |
| @@ -280,9 +250,9 @@ | ||
| 280 | 250 | * |
| 281 | 251 | * @return Indexable[]|false An array of created indexables or false if aioseo data was not found. |
| 282 | 252 | */ |
| 283 | 253 | public function index() { |
| 284 | - if ( ! $this->aioseo_exists() ) { | |
| 254 | + if ( ! $this->aioseo_helper->aioseo_exists() ) { | |
| 285 | 255 | return false; |
| 286 | 256 | } |
| 287 | 257 | |
| 288 | 258 | $limit = $this->get_limit(); |
| @@ -313,9 +283,9 @@ | ||
| 313 | 283 | } |
| 314 | 284 | |
| 315 | 285 | if ( $this->indexable_helper->check_if_default_indexable( $indexable, $check_defaults_fields ) ) { |
| 316 | 286 | $indexable = $this->map( $indexable, $aioseo_indexable ); |
| 317 | - $indexable->save(); | |
| 287 | + $this->indexable_helper->save_indexable( $indexable ); | |
| 318 | 288 | |
| 319 | 289 | // To ensure that indexables can be rebuild after a reset, we have to store the data in the postmeta table too. |
| 320 | 290 | $this->indexable_to_postmeta->map_to_postmeta( $indexable ); |
| 321 | 291 | } |
| @@ -409,9 +379,9 @@ | ||
| 409 | 379 | public function get_limit() { |
| 410 | 380 | /** |
| 411 | 381 | * Filter 'wpseo_aioseo_post_indexation_limit' - Allow filtering the number of posts indexed during each indexing pass. |
| 412 | 382 | * |
| 413 | - * @api int The maximum number of posts indexed. | |
| 383 | + * @param int $max_posts The maximum number of posts indexed. | |
| 414 | 384 | */ |
| 415 | 385 | $limit = \apply_filters( 'wpseo_aioseo_post_indexation_limit', 25 ); |
| 416 | 386 | |
| 417 | 387 | if ( ! \is_int( $limit ) || $limit < 1 ) { |
| @@ -421,23 +391,52 @@ | ||
| 421 | 391 | return $limit; |
| 422 | 392 | } |
| 423 | 393 | |
| 424 | 394 | /** |
| 395 | + * Populates the needed data array based on which columns we use from the AIOSEO indexable table. | |
| 396 | + * | |
| 397 | + * @return array The needed data array that contains all the needed columns. | |
| 398 | + */ | |
| 399 | + public function get_needed_data() { | |
| 400 | + $needed_data = \array_keys( $this->aioseo_to_yoast_map ); | |
| 401 | + \array_push( $needed_data, 'id', 'post_id', 'robots_default', 'og_image_custom_url', 'og_image_type', 'twitter_image_custom_url', 'twitter_image_type', 'twitter_use_og' ); | |
| 402 | + | |
| 403 | + return $needed_data; | |
| 404 | + } | |
| 405 | + | |
| 406 | + /** | |
| 407 | + * Populates the needed robot data array to be used in validating against its structure. | |
| 408 | + * | |
| 409 | + * @return array The needed data array that contains all the needed columns. | |
| 410 | + */ | |
| 411 | + public function get_needed_robot_data() { | |
| 412 | + $needed_robot_data = []; | |
| 413 | + | |
| 414 | + foreach ( $this->aioseo_to_yoast_map as $yoast_mapping ) { | |
| 415 | + if ( isset( $yoast_mapping['robot_type'] ) ) { | |
| 416 | + $needed_robot_data[] = $yoast_mapping['robot_type']; | |
| 417 | + } | |
| 418 | + } | |
| 419 | + | |
| 420 | + return $needed_robot_data; | |
| 421 | + } | |
| 422 | + | |
| 423 | + /** | |
| 425 | 424 | * Creates a query for gathering AiOSEO data from the database. |
| 426 | 425 | * |
| 427 | - * @param int $limit The maximum number of unimported objects to be returned. | |
| 428 | - * @param bool $just_detect Whether we want to just detect if there are unimported objects. If false, we want to actually import them too. | |
| 426 | + * @param int|false $limit The maximum number of unimported objects to be returned. | |
| 427 | + * False for "no limit". | |
| 428 | + * @param bool $just_detect Whether we want to just detect if there are unimported objects. If false, we want to actually import them too. | |
| 429 | 429 | * |
| 430 | 430 | * @return string The query to use for importing or counting the number of items to import. |
| 431 | 431 | */ |
| 432 | 432 | public function query( $limit = false, $just_detect = false ) { |
| 433 | - $table = $this->get_table(); | |
| 433 | + $table = $this->aioseo_helper->get_table(); | |
| 434 | 434 | |
| 435 | 435 | $select_statement = 'id'; |
| 436 | 436 | if ( ! $just_detect ) { |
| 437 | 437 | // If we want to import too, we need the actual needed data from AIOSEO indexables. |
| 438 | - $needed_data = \array_keys( $this->aioseo_to_yoast_map ); | |
| 439 | - \array_push( $needed_data, 'id', 'post_id', 'robots_default', 'og_image_custom_url', 'og_image_type', 'twitter_image_custom_url', 'twitter_image_type', 'twitter_use_og' ); | |
| 438 | + $needed_data = $this->get_needed_data(); | |
| 440 | 439 | |
| 441 | 440 | $select_statement = \implode( ', ', $needed_data ); |
| 442 | 441 | } |
| 443 | 442 | |
| @@ -446,9 +445,9 @@ | ||
| 446 | 445 | |
| 447 | 446 | /** |
| 448 | 447 | * Filter 'wpseo_aioseo_post_cursor' - Allow filtering the value of the aioseo post import cursor. |
| 449 | 448 | * |
| 450 | - * @api int The value of the aioseo post import cursor. | |
| 449 | + * @param int $import_cursor The value of the aioseo post import cursor. | |
| 451 | 450 | */ |
| 452 | 451 | $cursor = \apply_filters( 'wpseo_aioseo_post_import_cursor', $cursor ); |
| 453 | 452 | |
| 454 | 453 | $replacements = [ $cursor ]; |
| @@ -461,9 +460,9 @@ | ||
| 461 | 460 | |
| 462 | 461 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input. |
| 463 | 462 | return $this->wpdb->prepare( |
| 464 | 463 | "SELECT {$select_statement} FROM {$table} WHERE id > %d ORDER BY id{$limit_statement}", |
| 465 | - $replacements | |
| 464 | + $replacements, | |
| 466 | 465 | ); |
| 467 | 466 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 468 | 467 | } |
| 469 | 468 | |
| @@ -516,9 +515,9 @@ | ||
| 516 | 515 | * @return bool|null The value of Yoast's noindex setting for the post. |
| 517 | 516 | */ |
| 518 | 517 | public function post_robots_noindex_import( $aioseo_robots_settings ) { |
| 519 | 518 | // If robot settings defer to default settings, we have null in the is_robots_noindex field. |
| 520 | - if ( isset( $aioseo_robots_settings['robots_default'] ) && $aioseo_robots_settings['robots_default'] ) { | |
| 519 | + if ( $aioseo_robots_settings['robots_default'] ) { | |
| 521 | 520 | return null; |
| 522 | 521 | } |
| 523 | 522 | |
| 524 | 523 | return $aioseo_robots_settings['robots_noindex']; |
| @@ -533,12 +532,11 @@ | ||
| 533 | 532 | * |
| 534 | 533 | * @return bool|null The value of Yoast's noindex setting for the post. |
| 535 | 534 | */ |
| 536 | 535 | public function post_general_robots_import( $aioseo_robots_settings, $aioseo_key, $mapping ) { |
| 537 | - $mapping['type'] = 'postTypes'; | |
| 538 | - $mapping['option_name'] = 'aioseo_options_dynamic'; | |
| 536 | + $mapping = $this->enhance_mapping( $mapping ); | |
| 539 | 537 | |
| 540 | - if ( isset( $aioseo_robots_settings['robots_default'] ) && $aioseo_robots_settings['robots_default'] ) { | |
| 538 | + if ( $aioseo_robots_settings['robots_default'] ) { | |
| 541 | 539 | // Let's first get the subtype's setting value and then transform it taking into consideration whether it defers to global defaults. |
| 542 | 540 | $subtype_setting = $this->robots_provider->get_subtype_robot_setting( $mapping ); |
| 543 | 541 | return $this->robots_transformer->transform_robot_setting( $mapping['robot_type'], $subtype_setting, $mapping ); |
| 544 | 542 | } |
| @@ -543,8 +541,22 @@ | ||
| 543 | 541 | return $this->robots_transformer->transform_robot_setting( $mapping['robot_type'], $subtype_setting, $mapping ); |
| 544 | 542 | } |
| 545 | 543 | |
| 546 | 544 | return $aioseo_robots_settings[ $aioseo_key ]; |
| 545 | + } | |
| 546 | + | |
| 547 | + /** | |
| 548 | + * Enhances the mapping of the setting we're working with, with type and the option name, so that we can retrieve the settings for the object we're working with. | |
| 549 | + * | |
| 550 | + * @param array $mapping The mapping of the setting we're working with. | |
| 551 | + * | |
| 552 | + * @return array The enhanced mapping. | |
| 553 | + */ | |
| 554 | + public function enhance_mapping( $mapping = [] ) { | |
| 555 | + $mapping['type'] = 'postTypes'; | |
| 556 | + $mapping['option_name'] = 'aioseo_options_dynamic'; | |
| 557 | + | |
| 558 | + return $mapping; | |
| 547 | 559 | } |
| 548 | 560 | |
| 549 | 561 | /** |
| 550 | 562 | * Imports the og and twitter image url. |