14
14
*/
15
15
class Hierarchical_Sort {
16
16
17
- private static $ post_ids = array ();
18
- private static $ levels = array ();
19
- private static $ instance ;
20
-
21
- public static function get_instance () {
22
- if ( null === self ::$ instance ) {
23
- self ::$ instance = new self ();
17
+ /**
18
+ * Check if the request is eligible for hierarchical sorting.
19
+ *
20
+ * @param array $request The request data.
21
+ *
22
+ * @return bool Return true if the request is eligible for hierarchical sorting.
23
+ */
24
+ public static function is_eligible ( $ request ) {
25
+ if ( ! isset ( $ request ['orderby_hierarchy ' ] ) || true !== $ request ['orderby_hierarchy ' ] ) {
26
+ return false ;
24
27
}
25
28
26
- return self :: $ instance ;
29
+ return true ;
27
30
}
28
31
29
- public function run ( $ args ) {
32
+ public static function run ( $ args ) {
30
33
$ new_args = array_merge (
31
34
$ args ,
32
35
array (
@@ -36,28 +39,11 @@ public function run( $args ) {
36
39
);
37
40
$ query = new WP_Query ( $ new_args );
38
41
$ posts = $ query ->posts ;
39
- $ result = self ::sort ( $ posts );
40
-
41
- self ::$ post_ids = $ result ['post_ids ' ];
42
- self ::$ levels = $ result ['levels ' ];
43
- }
44
-
45
- /**
46
- * Check if the request is eligible for hierarchical sorting.
47
- *
48
- * @param array $request The request data.
49
- *
50
- * @return bool Return true if the request is eligible for hierarchical sorting.
51
- */
52
- public static function is_eligible ( $ request ) {
53
- if ( ! isset ( $ request ['orderby_hierarchy ' ] ) || true !== $ request ['orderby_hierarchy ' ] ) {
54
- return false ;
55
- }
56
42
57
- return true ;
43
+ return self :: sort ( $ posts ) ;
58
44
}
59
45
60
- public static function get_ancestor ( $ post_id ) {
46
+ private static function get_ancestor ( $ post_id ) {
61
47
return get_post ( $ post_id )->post_parent ?? 0 ;
62
48
}
63
49
@@ -93,7 +79,7 @@ public static function get_ancestor( $post_id ) {
93
79
* @type array $levels Array of levels for the corresponding post ID in the same index
94
80
* }
95
81
*/
96
- public static function sort ( $ posts ) {
82
+ private static function sort ( $ posts ) {
97
83
/*
98
84
* Arrange pages in two arrays:
99
85
*
@@ -152,38 +138,4 @@ private static function add_hierarchical_ids( &$ids, &$levels, $level, $to_proce
152
138
}
153
139
}
154
140
}
155
-
156
- public static function get_post_ids () {
157
- return self ::$ post_ids ;
158
- }
159
-
160
- public static function get_levels () {
161
- return self ::$ levels ;
162
- }
163
- }
164
-
165
- function rest_page_query_hierarchical_sort_filter_by_post_in ( $ args , $ request ) {
166
- if ( ! Hierarchical_Sort::is_eligible ( $ request ) ) {
167
- return $ args ;
168
- }
169
-
170
- $ hs = Hierarchical_Sort::get_instance ();
171
- $ hs ->run ( $ args );
172
-
173
- // Reconfigure the args to display only the ids in the list.
174
- $ args ['post__in ' ] = $ hs ->get_post_ids ();
175
- $ args ['orderby ' ] = 'post__in ' ;
176
-
177
- return $ args ;
178
- }
179
-
180
- function rest_prepare_page_hierarchical_sort_add_levels ( $ response , $ post , $ request ) {
181
- if ( ! Hierarchical_Sort::is_eligible ( $ request ) ) {
182
- return $ response ;
183
- }
184
-
185
- $ hs = Hierarchical_Sort::get_instance ();
186
- $ response ->data ['level ' ] = $ hs ->get_levels ()[ $ post ->ID ];
187
-
188
- return $ response ;
189
141
}
0 commit comments