← All changes
|
src/integrations/watchers/indexable-ancestor-watcher.php
+45
-49
18.4
→
trunk
View file →
| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 | 4 | |
| 5 | -use wpdb; | |
| 6 | 5 | use Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder; |
| 7 | 6 | use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 7 | +use Yoast\WP\SEO\Helpers\Indexable_Helper; | |
| 8 | 8 | use Yoast\WP\SEO\Helpers\Permalink_Helper; |
| 9 | 9 | use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 10 | 10 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 11 | 11 | use Yoast\WP\SEO\Models\Indexable; |
| @@ -40,13 +40,13 @@ | ||
| 40 | 40 | */ |
| 41 | 41 | protected $indexable_hierarchy_repository; |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Represents the WordPress database object. | |
| 44 | + * The indexable helper. | |
| 45 | 45 | * |
| 46 | - * @var wpdb | |
| 46 | + * @var Indexable_Helper | |
| 47 | 47 | */ |
| 48 | - protected $wpdb; | |
| 48 | + private $indexable_helper; | |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Represents the permalink helper. |
| 52 | 52 | * |
| @@ -66,9 +66,9 @@ | ||
| 66 | 66 | * |
| 67 | 67 | * @param Indexable_Repository $indexable_repository The indexable repository. |
| 68 | 68 | * @param Indexable_Hierarchy_Builder $indexable_hierarchy_builder The indexable hierarchy builder. |
| 69 | 69 | * @param Indexable_Hierarchy_Repository $indexable_hierarchy_repository The indexable hierarchy repository. |
| 70 | - * @param wpdb $wpdb The wpdb object. | |
| 70 | + * @param Indexable_Helper $indexable_helper The indexable helper. | |
| 71 | 71 | * @param Permalink_Helper $permalink_helper The permalink helper. |
| 72 | 72 | * @param Post_Type_Helper $post_type_helper The post type helper. |
| 73 | 73 | */ |
| 74 | 74 | public function __construct( |
| @@ -74,16 +74,16 @@ | ||
| 74 | 74 | public function __construct( |
| 75 | 75 | Indexable_Repository $indexable_repository, |
| 76 | 76 | Indexable_Hierarchy_Builder $indexable_hierarchy_builder, |
| 77 | 77 | Indexable_Hierarchy_Repository $indexable_hierarchy_repository, |
| 78 | - wpdb $wpdb, | |
| 78 | + Indexable_Helper $indexable_helper, | |
| 79 | 79 | Permalink_Helper $permalink_helper, |
| 80 | 80 | Post_Type_Helper $post_type_helper |
| 81 | 81 | ) { |
| 82 | 82 | $this->indexable_repository = $indexable_repository; |
| 83 | 83 | $this->indexable_hierarchy_builder = $indexable_hierarchy_builder; |
| 84 | - $this->wpdb = $wpdb; | |
| 85 | 84 | $this->indexable_hierarchy_repository = $indexable_hierarchy_repository; |
| 85 | + $this->indexable_helper = $indexable_helper; | |
| 86 | 86 | $this->permalink_helper = $permalink_helper; |
| 87 | 87 | $this->post_type_helper = $post_type_helper; |
| 88 | 88 | } |
| 89 | 89 | |
| @@ -88,8 +88,10 @@ | ||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | 91 | * Registers the appropriate hooks. |
| 92 | + * | |
| 93 | + * @return void | |
| 92 | 94 | */ |
| 93 | 95 | public function register_hooks() { |
| 94 | 96 | \add_action( 'wpseo_save_indexable', [ $this, 'reset_children' ], \PHP_INT_MAX, 2 ); |
| 95 | 97 | } |
| @@ -96,9 +98,9 @@ | ||
| 96 | 98 | |
| 97 | 99 | /** |
| 98 | 100 | * Returns the conditionals based on which this loadable should be active. |
| 99 | 101 | * |
| 100 | - * @return array | |
| 102 | + * @return array<Migrations_Conditional> | |
| 101 | 103 | */ |
| 102 | 104 | public static function get_conditionals() { |
| 103 | 105 | return [ Migrations_Conditional::class ]; |
| 104 | 106 | } |
| @@ -116,9 +118,9 @@ | ||
| 116 | 118 | return false; |
| 117 | 119 | } |
| 118 | 120 | |
| 119 | 121 | // If the permalink was null it means it was reset instead of changed. |
| 120 | - if ( $indexable->permalink === $indexable_before->permalink || \is_null( $indexable_before->permalink ) ) { | |
| 122 | + if ( $indexable->permalink === $indexable_before->permalink || $indexable_before->permalink === null ) { | |
| 121 | 123 | return false; |
| 122 | 124 | } |
| 123 | 125 | |
| 124 | 126 | $child_indexable_ids = $this->indexable_hierarchy_repository->find_children( $indexable ); |
| @@ -124,9 +126,8 @@ | ||
| 124 | 126 | $child_indexable_ids = $this->indexable_hierarchy_repository->find_children( $indexable ); |
| 125 | 127 | $child_indexables = $this->indexable_repository->find_by_ids( $child_indexable_ids ); |
| 126 | 128 | |
| 127 | 129 | \array_walk( $child_indexables, [ $this, 'update_hierarchy_and_permalink' ] ); |
| 128 | - | |
| 129 | 130 | if ( $indexable->object_type === 'term' ) { |
| 130 | 131 | $child_indexables_for_term = $this->get_children_for_term( $indexable->object_id, $child_indexables ); |
| 131 | 132 | |
| 132 | 133 | \array_walk( $child_indexables_for_term, [ $this, 'update_hierarchy_and_permalink' ] ); |
| @@ -137,12 +138,12 @@ | ||
| 137 | 138 | |
| 138 | 139 | /** |
| 139 | 140 | * Finds all child indexables for the given term. |
| 140 | 141 | * |
| 141 | - * @param int $term_id Term to fetch the indexable for. | |
| 142 | - * @param Indexable[] $child_indexables The already known child indexables. | |
| 142 | + * @param int $term_id Term to fetch the indexable for. | |
| 143 | + * @param array<Indexable> $child_indexables The already known child indexables. | |
| 143 | 144 | * |
| 144 | - * @return array The list of additional child indexables for a given term. | |
| 145 | + * @return array<Indexable> The list of additional child indexables for a given term. | |
| 145 | 146 | */ |
| 146 | 147 | public function get_children_for_term( $term_id, array $child_indexables ) { |
| 147 | 148 | // Finds object_ids (posts) for the term. |
| 148 | 149 | $post_object_ids = $this->get_object_ids_for_term( $term_id, $child_indexables ); |
| @@ -149,11 +150,11 @@ | ||
| 149 | 150 | |
| 150 | 151 | // Removes the objects that are already present in the children. |
| 151 | 152 | $existing_post_indexables = \array_filter( |
| 152 | 153 | $child_indexables, |
| 153 | - static function( $indexable ) { | |
| 154 | + static function ( $indexable ) { | |
| 154 | 155 | return $indexable->object_type === 'post'; |
| 155 | - } | |
| 156 | + }, | |
| 156 | 157 | ); |
| 157 | 158 | |
| 158 | 159 | $existing_post_object_ids = \wp_list_pluck( $existing_post_indexables, 'object_id' ); |
| 159 | 160 | $post_object_ids = \array_diff( $post_object_ids, $existing_post_object_ids ); |
| @@ -175,58 +176,51 @@ | ||
| 175 | 176 | return \array_merge( $post_indexables, $additional_indexables ); |
| 176 | 177 | } |
| 177 | 178 | |
| 178 | 179 | /** |
| 179 | - * Builds the hierarchy for a post. | |
| 180 | - * | |
| 181 | - * @deprecated 16.4 | |
| 182 | - * | |
| 183 | - * @codeCoverageIgnore | |
| 184 | - * | |
| 185 | - * @param int $object_id The post id. | |
| 186 | - * @param int $post_type The post type. | |
| 187 | - */ | |
| 188 | - public function build_post_hierarchy( $object_id, $post_type ) { | |
| 189 | - \_deprecated_function( __METHOD__, '16.4', 'Primary_Category_Quick_Edit_Watcher::build_post_hierarchy' ); | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | 180 | * Updates the indexable hierarchy and indexable permalink. |
| 194 | 181 | * |
| 195 | 182 | * @param Indexable $indexable The indexable to update the hierarchy and permalink for. |
| 183 | + * | |
| 184 | + * @return void | |
| 196 | 185 | */ |
| 197 | 186 | protected function update_hierarchy_and_permalink( $indexable ) { |
| 198 | - $this->indexable_hierarchy_builder->build( $indexable ); | |
| 187 | + if ( \is_a( $indexable, Indexable::class ) ) { | |
| 188 | + $this->indexable_hierarchy_builder->build( $indexable ); | |
| 199 | 189 | |
| 200 | - $indexable->permalink = $this->permalink_helper->get_permalink_for_indexable( $indexable ); | |
| 201 | - $indexable->save(); | |
| 190 | + $indexable->permalink = $this->permalink_helper->get_permalink_for_indexable( $indexable ); | |
| 191 | + $this->indexable_helper->save_indexable( $indexable ); | |
| 192 | + } | |
| 202 | 193 | } |
| 203 | 194 | |
| 204 | 195 | /** |
| 205 | 196 | * Retrieves the object id's for a term based on the term-post relationship. |
| 206 | 197 | * |
| 207 | - * @param int $term_id The term to get the object id's for. | |
| 208 | - * @param Indexable[] $child_indexables The child indexables. | |
| 198 | + * @param int $term_id The term to get the object id's for. | |
| 199 | + * @param array<Indexable> $child_indexables The child indexables. | |
| 209 | 200 | * |
| 210 | - * @return array List with object ids for the term. | |
| 201 | + * @return array<int> List with object ids for the term. | |
| 211 | 202 | */ |
| 212 | 203 | protected function get_object_ids_for_term( $term_id, $child_indexables ) { |
| 213 | - $filter_terms = static function( $child ) { | |
| 204 | + global $wpdb; | |
| 205 | + | |
| 206 | + $filter_terms = static function ( $child ) { | |
| 214 | 207 | return $child->object_type === 'term'; |
| 215 | 208 | }; |
| 216 | 209 | |
| 217 | 210 | $child_terms = \array_filter( $child_indexables, $filter_terms ); |
| 218 | - $child_object_ids = \wp_list_pluck( $child_terms, 'object_id' ); | |
| 211 | + $child_object_ids = \array_merge( [ $term_id ], \wp_list_pluck( $child_terms, 'object_id' ) ); | |
| 219 | 212 | |
| 220 | 213 | // Get the term-taxonomy id's for the term and its children. |
| 221 | - $term_taxonomy_ids = $this->wpdb->get_col( | |
| 222 | - $this->wpdb->prepare( | |
| 214 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 215 | + $term_taxonomy_ids = $wpdb->get_col( | |
| 216 | + $wpdb->prepare( | |
| 223 | 217 | 'SELECT term_taxonomy_id |
| 224 | - FROM ' . $this->wpdb->term_taxonomy . ' | |
| 225 | - WHERE term_id IN( ' . \implode( ', ', \array_fill( 0, ( \count( $child_object_ids ) + 1 ), '%s' ) ) . ' )', | |
| 226 | - $term_id, | |
| 227 | - ...$child_object_ids | |
| 228 | - ) | |
| 218 | + FROM %i | |
| 219 | + WHERE term_id IN( ' . \implode( ', ', \array_fill( 0, ( \count( $child_object_ids ) ), '%s' ) ) . ' )', | |
| 220 | + $wpdb->term_taxonomy, | |
| 221 | + ...$child_object_ids, | |
| 222 | + ), | |
| 229 | 223 | ); |
| 230 | 224 | |
| 231 | 225 | // In the case of faulty data having been saved the above query can return 0 results. |
| 232 | 226 | if ( empty( $term_taxonomy_ids ) ) { |
| @@ -233,14 +227,16 @@ | ||
| 233 | 227 | return []; |
| 234 | 228 | } |
| 235 | 229 | |
| 236 | 230 | // Get the (post) object id's that are attached to the term. |
| 237 | - return $this->wpdb->get_col( | |
| 238 | - $this->wpdb->prepare( | |
| 231 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 232 | + return $wpdb->get_col( | |
| 233 | + $wpdb->prepare( | |
| 239 | 234 | 'SELECT DISTINCT object_id |
| 240 | - FROM ' . $this->wpdb->term_relationships . ' | |
| 235 | + FROM %i | |
| 241 | 236 | WHERE term_taxonomy_id IN( ' . \implode( ', ', \array_fill( 0, \count( $term_taxonomy_ids ), '%s' ) ) . ' )', |
| 242 | - ...$term_taxonomy_ids | |
| 243 | - ) | |
| 237 | + $wpdb->term_relationships, | |
| 238 | + ...$term_taxonomy_ids, | |
| 239 | + ), | |
| 244 | 240 | ); |
| 245 | 241 | } |
| 246 | 242 | } |