-
Notifications
You must be signed in to change notification settings - Fork 885
/
indexable-hierarchy-repository.php
171 lines (148 loc) · 3.89 KB
/
indexable-hierarchy-repository.php
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
<?php
namespace Yoast\WP\SEO\Repositories;
use Yoast\WP\Lib\Model;
use Yoast\WP\Lib\ORM;
use Yoast\WP\SEO\Builders\Indexable_Hierarchy_Builder;
use Yoast\WP\SEO\Helpers\Indexable_Helper;
use Yoast\WP\SEO\Models\Indexable;
/**
* Class Indexable_Hierarchy_Repository.
*/
class Indexable_Hierarchy_Repository {
/**
* Represents the indexable hierarchy builder.
*
* @var Indexable_Hierarchy_Builder
*/
protected $builder;
/**
* Represents the indexable helper.
*
* @var Indexable_Helper
*/
protected $indexable_helper;
/**
* Sets the hierarchy builder.
*
* @required
*
* @param Indexable_Hierarchy_Builder $builder The indexable hierarchy builder.
*
* @return void
*/
public function set_builder( Indexable_Hierarchy_Builder $builder ) {
$this->builder = $builder;
}
/**
* Sets the indexable helper.
*
* @required
*
* @param Indexable_Helper $indexable_helper The indexable helper.
*
* @return void
*/
public function set_helper( Indexable_Helper $indexable_helper ) {
$this->indexable_helper = $indexable_helper;
}
/**
* Removes all ancestors for an indexable.
*
* @param int $indexable_id The indexable id.
*
* @return bool Whether or not the indexables were successfully deleted.
*/
public function clear_ancestors( $indexable_id ) {
return $this->query()->where( 'indexable_id', $indexable_id )->delete_many();
}
/**
* Adds an ancestor to an indexable.
*
* @param int $indexable_id The indexable id.
* @param int $ancestor_id The ancestor id.
* @param int $depth The depth.
*
* @return bool Whether or not the ancestor was added successfully.
*/
public function add_ancestor( $indexable_id, $ancestor_id, $depth ) {
if ( ! $this->indexable_helper->should_index_indexables() ) {
return false;
}
$hierarchy = $this->query()->create(
[
'indexable_id' => $indexable_id,
'ancestor_id' => $ancestor_id,
'depth' => $depth,
'blog_id' => \get_current_blog_id(),
]
);
return $hierarchy->save();
}
/**
* Retrieves the ancestors. Create them when empty.
*
* @param Indexable $indexable The indexable to get the ancestors for.
*
* @return int[] The indexable id's of the ancestors in order of grandparent to child.
*/
public function find_ancestors( Indexable $indexable ) {
$ancestors = $this->query()
->select( 'ancestor_id' )
->where( 'indexable_id', $indexable->id )
->order_by_desc( 'depth' )
->find_array();
if ( ! empty( $ancestors ) ) {
if ( \count( $ancestors ) === 1 && $ancestors[0]['ancestor_id'] === '0' ) {
return [];
}
return \wp_list_pluck( $ancestors, 'ancestor_id' );
}
$indexable = $this->builder->build( $indexable );
return \wp_list_pluck( $indexable->ancestors, 'id' );
}
/**
* Finds the children for a given indexable.
*
* @param Indexable $indexable The indexable to find the children for.
*
* @return array Array with indexable id's for the children.
*/
public function find_children( Indexable $indexable ) {
$children = $this->query()
->select( 'indexable_id' )
->where( 'ancestor_id', $indexable->id )
->find_array();
if ( empty( $children ) ) {
return [];
}
return \wp_list_pluck( $children, 'indexable_id' );
}
/**
* Starts a query for this repository.
*
* @return ORM
*/
public function query() {
return Model::of_type( 'Indexable_Hierarchy' );
}
/**
* Finds all the children by given ancestor id's.
*
* @param array $object_ids List of id's to get the children for.
*
* @return array List of indexable id's for the children.
*/
public function find_children_by_ancestor_ids( array $object_ids ) {
if ( empty( $object_ids ) ) {
return [];
}
$children = $this->query()
->select( 'indexable_id' )
->where_in( 'ancestor_id', $object_ids )
->find_array();
if ( empty( $children ) ) {
return [];
}
return \wp_list_pluck( $children, 'indexable_id' );
}
}