PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.1
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / repositories / indexable-hierarchy-repository.php

indexable-hierarchy-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.1, at src/repositories/indexable-hierarchy-repository.php

172 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Repositories;
4
5 use Yoast\WP\Lib\Model;
6 use Yoast\WP\Lib\ORM;
7 use Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder;
8 use Yoast\WP\SEO\Helpers\Indexable_Helper;
9 use Yoast\WP\SEO\Models\Indexable;
10
11 /**
12 * Class Indexable_Hierarchy_Repository.
13 */
14 class Indexable_Hierarchy_Repository {
15
16 /**
17 * Represents the indexable hierarchy builder.
18 *
19 * @var Indexable_Hierarchy_Builder
20 */
21 protected $builder;
22
23 /**
24 * Represents the indexable helper.
25 *
26 * @var Indexable_Helper
27 */
28 protected $indexable_helper;
29
30 /**
31 * Sets the hierarchy builder.
32 *
33 * @required
34 *
35 * @param Indexable_Hierarchy_Builder $builder The indexable hierarchy builder.
36 *
37 * @return void
38 */
39 public function set_builder( Indexable_Hierarchy_Builder $builder ) {
40 $this->builder = $builder;
41 }
42
43 /**
44 * Sets the indexable helper.
45 *
46 * @required
47 *
48 * @param Indexable_Helper $indexable_helper The indexable helper.
49 *
50 * @return void
51 */
52 public function set_helper( Indexable_Helper $indexable_helper ) {
53 $this->indexable_helper = $indexable_helper;
54 }
55
56 /**
57 * Removes all ancestors for an indexable.
58 *
59 * @param int $indexable_id The indexable id.
60 *
61 * @return bool Whether or not the indexables were successfully deleted.
62 */
63 public function clear_ancestors( $indexable_id ) {
64 return $this->query()->where( 'indexable_id', $indexable_id )->delete_many();
65 }
66
67 /**
68 * Adds an ancestor to an indexable.
69 *
70 * @param int $indexable_id The indexable id.
71 * @param int $ancestor_id The ancestor id.
72 * @param int $depth The depth.
73 *
74 * @return bool Whether or not the ancestor was added successfully.
75 */
76 public function add_ancestor( $indexable_id, $ancestor_id, $depth ) {
77 if ( ! $this->indexable_helper->should_index_indexables() ) {
78 return false;
79 }
80
81 $hierarchy = $this->query()->create(
82 [
83 'indexable_id' => $indexable_id,
84 'ancestor_id' => $ancestor_id,
85 'depth' => $depth,
86 'blog_id' => \get_current_blog_id(),
87 ],
88 );
89
90 return $hierarchy->save();
91 }
92
93 /**
94 * Retrieves the ancestors. Create them when empty.
95 *
96 * @param Indexable $indexable The indexable to get the ancestors for.
97 *
98 * @return int[] The indexable id's of the ancestors in order of grandparent to child.
99 */
100 public function find_ancestors( Indexable $indexable ) {
101 $ancestors = $this->query()
102 ->select( 'ancestor_id' )
103 ->where( 'indexable_id', $indexable->id )
104 ->order_by_desc( 'depth' )
105 ->find_array();
106
107 if ( ! empty( $ancestors ) ) {
108 if ( \count( $ancestors ) === 1 && $ancestors[0]['ancestor_id'] === '0' ) {
109 return [];
110 }
111 return \wp_list_pluck( $ancestors, 'ancestor_id' );
112 }
113
114 $indexable = $this->builder->build( $indexable );
115
116 return \wp_list_pluck( $indexable->ancestors, 'id' );
117 }
118
119 /**
120 * Finds the children for a given indexable.
121 *
122 * @param Indexable $indexable The indexable to find the children for.
123 *
124 * @return array Array with indexable id's for the children.
125 */
126 public function find_children( Indexable $indexable ) {
127 $children = $this->query()
128 ->select( 'indexable_id' )
129 ->where( 'ancestor_id', $indexable->id )
130 ->find_array();
131
132 if ( empty( $children ) ) {
133 return [];
134 }
135
136 return \wp_list_pluck( $children, 'indexable_id' );
137 }
138
139 /**
140 * Starts a query for this repository.
141 *
142 * @return ORM
143 */
144 public function query() {
145 return Model::of_type( 'Indexable_Hierarchy' );
146 }
147
148 /**
149 * Finds all the children by given ancestor id's.
150 *
151 * @param array $object_ids List of id's to get the children for.
152 *
153 * @return array List of indexable id's for the children.
154 */
155 public function find_children_by_ancestor_ids( array $object_ids ) {
156 if ( empty( $object_ids ) ) {
157 return [];
158 }
159
160 $children = $this->query()
161 ->select( 'indexable_id' )
162 ->where_in( 'ancestor_id', $object_ids )
163 ->find_array();
164
165 if ( empty( $children ) ) {
166 return [];
167 }
168
169 return \wp_list_pluck( $children, 'indexable_id' );
170 }
171 }
172