Skip to content

Commit a6000fc

Browse files
committed
Use class directly and remove static cache
1 parent cd2f956 commit a6000fc

File tree

2 files changed

+32
-65
lines changed

2 files changed

+32
-65
lines changed

src/wp-includes/class-hierarchical-sort.php

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
*/
1515
class Hierarchical_Sort {
1616

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;
2427
}
2528

26-
return self::$instance;
29+
return true;
2730
}
2831

29-
public function run( $args ) {
32+
public static function run( $args ) {
3033
$new_args = array_merge(
3134
$args,
3235
array(
@@ -36,28 +39,11 @@ public function run( $args ) {
3639
);
3740
$query = new WP_Query( $new_args );
3841
$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-
}
5642

57-
return true;
43+
return self::sort( $posts );
5844
}
5945

60-
public static function get_ancestor( $post_id ) {
46+
private static function get_ancestor( $post_id ) {
6147
return get_post( $post_id )->post_parent ?? 0;
6248
}
6349

@@ -93,7 +79,7 @@ public static function get_ancestor( $post_id ) {
9379
* @type array $levels Array of levels for the corresponding post ID in the same index
9480
* }
9581
*/
96-
public static function sort( $posts ) {
82+
private static function sort( $posts ) {
9783
/*
9884
* Arrange pages in two arrays:
9985
*
@@ -152,38 +138,4 @@ private static function add_hierarchical_ids( &$ids, &$levels, $level, $to_proce
152138
}
153139
}
154140
}
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;
189141
}

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
4747
*/
4848
protected $allow_batch = array( 'v1' => true );
4949

50+
/**
51+
* Holds information about each post's level.
52+
* Level means the depth of the post in the hierarchy:
53+
* top-level posts have level 0, their children have level 1, and so on.
54+
*/
55+
protected $levels = array();
56+
5057
/**
5158
* Constructor.
5259
*
@@ -402,7 +409,13 @@ static function ( $format ) {
402409
// Force the post_type argument, since it's not a user input variable.
403410
$args['post_type'] = $this->post_type;
404411

405-
$args = rest_page_query_hierarchical_sort_filter_by_post_in( $args, $request );
412+
if ( Hierarchical_Sort::is_eligible( $request ) ) {
413+
$result = Hierarchical_Sort::run( $args );
414+
$this->levels = $result[ 'levels' ];
415+
416+
$args[ 'post__in' ] = $result[ 'post_ids' ];
417+
$args[ 'orderby' ] = 'post__in';
418+
}
406419

407420
/**
408421
* Filters WP_Query arguments when querying posts via the REST API.
@@ -2092,7 +2105,9 @@ public function prepare_item_for_response( $item, $request ) {
20922105
}
20932106
}
20942107

2095-
$response = rest_prepare_page_hierarchical_sort_add_levels( $response, $post, $request );
2108+
if ( Hierarchical_Sort::is_eligible( $request ) ) {
2109+
$response->data['level'] = $this->levels[ $post->ID ];
2110+
}
20962111

20972112
/**
20982113
* Filters the post data for a REST API response.

0 commit comments

Comments
 (0)