indexable-hierarchy-builder.php
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
namespace Yoast\WP\SEO\Builders;
use WP_Post;
use WP_Term;
use WPSEO_Meta;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Helpers\Post_Helper;
use Yoast\WP\SEO\Models\Indexable;
use Yoast\WP\SEO\Repositories\Indexable_Hierarchy_Repository;
use Yoast\WP\SEO\Repositories\Indexable_Repository;
use Yoast\WP\SEO\Repositories\Primary_Term_Repository;
/**
* Builder for the indexables hierarchy.
*
* Builds the indexable hierarchy for indexables.
*/
class Indexable_Hierarchy_Builder {
/**
* Holds a list of indexables where the ancestors are saved for.
*
* @var array
*/
protected $saved_ancestors = [];
/**
* The indexable repository.
*
* @var Indexable_Repository
*/
private $indexable_repository;
/**
* The indexable hierarchy repository.
*
* @var Indexable_Hierarchy_Repository
*/
private $indexable_hierarchy_repository;
/**
* The primary term repository.
*
* @var Primary_Term_Repository
*/
private $primary_term_repository;
/**
* The options helper.
*
* @var Options_Helper
*/
private $options;
/**
* Holds the Post_Helper instance.
*
* @var Post_Helper
*/
private $post;
/**
* Indexable_Author_Builder constructor.
*
* @param Indexable_Hierarchy_Repository $indexable_hierarchy_repository The indexable hierarchy repository.
* @param Primary_Term_Repository $primary_term_repository The primary term repository.
* @param Options_Helper $options The options helper.
* @param Post_Helper $post The post helper.
*/
public function __construct(
Indexable_Hierarchy_Repository $indexable_hierarchy_repository,
Primary_Term_Repository $primary_term_repository,
Options_Helper $options,
Post_Helper $post
) {
$this->indexable_hierarchy_repository = $indexable_hierarchy_repository;
$this->primary_term_repository = $primary_term_repository;
$this->options = $options;
$this->post = $post;
}
/**
* Sets the indexable repository. Done to avoid circular dependencies.
*
* @required
*
* @param Indexable_Repository $indexable_repository The indexable repository.
*/
public function set_indexable_repository( Indexable_Repository $indexable_repository ) {
$this->indexable_repository = $indexable_repository;
}
/**
* Builds the ancestor hierarchy for an indexable.
*
* @param Indexable $indexable The indexable.
*
* @return Indexable The indexable.
*/
public function build( Indexable $indexable ) {
if ( $this->hierarchy_is_built( $indexable ) ) {
return $indexable;
}
$this->indexable_hierarchy_repository->clear_ancestors( $indexable->id );
$indexable_id = $this->get_indexable_id( $indexable );
$ancestors = [];
if ( $indexable->object_type === 'post' ) {
$this->add_ancestors_for_post( $indexable_id, $indexable->object_id, $ancestors );
}
if ( $indexable->object_type === 'term' ) {
$this->add_ancestors_for_term( $indexable_id, $indexable->object_id, $ancestors );
}
$indexable->ancestors = \array_reverse( \array_values( $ancestors ) );
$indexable->has_ancestors = ! empty( $ancestors );
if ( $indexable->id ) {
$this->save_ancestors( $indexable );
}
return $indexable;
}
/**
* Checks if a hierarchy is built already for the given indexable.
*
* @param Indexable $indexable The indexable to check.
*
* @return bool True when indexable has a built hierarchy.
*/
protected function hierarchy_is_built( Indexable $indexable ) {
if ( \in_array( $indexable->id, $this->saved_ancestors, true ) ) {
return true;
}
$this->saved_ancestors[] = $indexable->id;
return false;
}
/**
* Saves the ancestors.
*
* @param Indexable $indexable The indexable.
*
* @return void
*/
private function save_ancestors( $indexable ) {
if ( empty( $indexable->ancestors ) ) {
$this->indexable_hierarchy_repository->add_ancestor( $indexable->id, 0, 0 );
return;
}
$depth = \count( $indexable->ancestors );
foreach ( $indexable->ancestors as $ancestor ) {
$this->indexable_hierarchy_repository->add_ancestor( $indexable->id, $ancestor->id, $depth );
--$depth;
}
}
/**
* Adds ancestors for a post.
*
* @param int $indexable_id The indexable id, this is the id of the original indexable.
* @param int $post_id The post id, this is the id of the post currently being evaluated.
* @param int[] $parents The indexable IDs of all parents.
*
* @return void
*/
private function add_ancestors_for_post( $indexable_id, $post_id, &$parents ) {
$post = $this->post->get_post( $post_id );
if ( ! isset( $post->post_parent ) ) {
return;
}
if ( $post->post_parent !== 0 && $this->post->get_post( $post->post_parent ) !== null ) {
$ancestor = $this->indexable_repository->find_by_id_and_type( $post->post_parent, 'post' );
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
return;
}
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
$this->add_ancestors_for_post( $indexable_id, $ancestor->object_id, $parents );
return;
}
$primary_term_id = $this->find_primary_term_id_for_post( $post );
if ( $primary_term_id === 0 ) {
return;
}
$ancestor = $this->indexable_repository->find_by_id_and_type( $primary_term_id, 'term' );
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
return;
}
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
$this->add_ancestors_for_term( $indexable_id, $ancestor->object_id, $parents );
}
/**
* Adds ancestors for a term.
*
* @param int $indexable_id The indexable id, this is the id of the original indexable.
* @param int $term_id The term id, this is the id of the term currently being evaluated.
* @param int[] $parents The indexable IDs of all parents.
*
* @return void
*/
private function add_ancestors_for_term( $indexable_id, $term_id, &$parents = [] ) {
$term = \get_term( $term_id );
$term_parents = $this->get_term_parents( $term );
foreach ( $term_parents as $parent ) {
$ancestor = $this->indexable_repository->find_by_id_and_type( $parent->term_id, 'term' );
if ( $this->is_invalid_ancestor( $ancestor, $indexable_id, $parents ) ) {
continue;
}
$parents[ $this->get_indexable_id( $ancestor ) ] = $ancestor;
}
}
/**
* Gets the primary term ID for a post.
*
* @param WP_Post $post The post.
*
* @return int The primary term ID. 0 if none exists.
*/
private function find_primary_term_id_for_post( $post ) {
$main_taxonomy = $this->options->get( 'post_types-' . $post->post_type . '-maintax' );
if ( ! $main_taxonomy || $main_taxonomy === '0' ) {
return 0;
}
$primary_term_id = $this->get_primary_term_id( $post->ID, $main_taxonomy );
if ( $primary_term_id ) {
$term = \get_term( $primary_term_id );
if ( $term !== null && ! \is_wp_error( $term ) ) {
return $primary_term_id;
}
}
$terms = \get_the_terms( $post->ID, $main_taxonomy );
if ( ! \is_array( $terms ) || empty( $terms ) ) {
return 0;
}
return $this->find_deepest_term_id( $terms );
}
/**
* Find the deepest term in an array of term objects.
*
* @param array $terms Terms set.
*
* @return int The deepest term ID.
*/
private function find_deepest_term_id( $terms ) {
/*
* Let's find the deepest term in this array, by looping through and then
* unsetting every term that is used as a parent by another one in the array.
*/
$terms_by_id = [];
foreach ( $terms as $term ) {
$terms_by_id[ $term->term_id ] = $term;
}
foreach ( $terms as $term ) {
unset( $terms_by_id[ $term->parent ] );
}
/*
* As we could still have two subcategories, from different parent categories,
* let's pick the one with the lowest ordered ancestor.
*/
$parents_count = -1;
$term_order = 9999; // Because ASC.
$deepest_term = \reset( $terms_by_id );
foreach ( $terms_by_id as $term ) {
$parents = $this->get_term_parents( $term );
$new_parents_count = \count( $parents );
if ( $new_parents_count < $parents_count ) {
continue;
}
$parent_order = 9999; // Set default order.
foreach ( $parents as $parent ) {
if ( $parent->parent === 0 && isset( $parent->term_order ) ) {
$parent_order = $parent->term_order;
}
}
// Check if parent has lowest order.
if ( $new_parents_count > $parents_count || $parent_order < $term_order ) {
$term_order = $parent_order;
$deepest_term = $term;
}
$parents_count = $new_parents_count;
}
return $deepest_term->term_id;
}
/**
* Get a term's parents.
*
* @param WP_Term $term Term to get the parents for.
*
* @return WP_Term[] An array of all this term's parents.
*/
private function get_term_parents( $term ) {
$tax = $term->taxonomy;
$parents = [];
while ( (int) $term->parent !== 0 ) {
$term = \get_term( $term->parent, $tax );
$parents[] = $term;
}
return $parents;
}
/**
* Checks if an ancestor is valid to add.
*
* @param Indexable $ancestor The ancestor (presumed indexable) to check.
* @param int $indexable_id The indexable id we're adding ancestors for.
* @param int[] $parents The indexable ids of the parents already added.
*
* @return bool
*/
private function is_invalid_ancestor( $ancestor, $indexable_id, $parents ) {
// If the ancestor is not an Indexable, it is invalid by default.
if ( ! \is_a( $ancestor, 'Yoast\WP\SEO\Models\Indexable' ) ) {
return true;
}
// Don't add ancestors if they're unindexed, already added or the same as the main object.
if ( $ancestor->post_status === 'unindexed' ) {
return true;
}
$ancestor_id = $this->get_indexable_id( $ancestor );
if ( \array_key_exists( $ancestor_id, $parents ) ) {
return true;
}
if ( $ancestor_id === $indexable_id ) {
return true;
}
return false;
}
/**
* Returns the ID for an indexable. Catches situations where the id is null due to errors.
*
* @param Indexable $indexable The indexable.
*
* @return string|int A unique ID for the indexable.
*/
private function get_indexable_id( Indexable $indexable ) {
if ( $indexable->id === 0 ) {
return "{$indexable->object_type}:{$indexable->object_id}";
}
return $indexable->id;
}
/**
* Returns the primary term id of a post.
*
* @param int $post_id The post ID.
* @param string $main_taxonomy The main taxonomy.
*
* @return int The ID of the primary term.
*/
private function get_primary_term_id( $post_id, $main_taxonomy ) {
$primary_term = $this->primary_term_repository->find_by_post_id_and_taxonomy( $post_id, $main_taxonomy, false );
if ( $primary_term ) {
return $primary_term->term_id;
}
return \get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'primary_' . $main_taxonomy, true );
}
}