From a6000fc2f920ad73fcc0d74d28e9292db6e9de87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Thu, 26 Dec 2024 14:04:07 +0100 Subject: [PATCH] Use class directly and remove static cache --- src/wp-includes/class-hierarchical-sort.php | 78 ++++--------------- .../class-wp-rest-posts-controller.php | 19 ++++- 2 files changed, 32 insertions(+), 65 deletions(-) diff --git a/src/wp-includes/class-hierarchical-sort.php b/src/wp-includes/class-hierarchical-sort.php index f4ac9c3d9942f..2b08ae4048443 100644 --- a/src/wp-includes/class-hierarchical-sort.php +++ b/src/wp-includes/class-hierarchical-sort.php @@ -14,19 +14,22 @@ */ class Hierarchical_Sort { - private static $post_ids = array(); - private static $levels = array(); - private static $instance; - - public static function get_instance() { - if ( null === self::$instance ) { - self::$instance = new self(); + /** + * Check if the request is eligible for hierarchical sorting. + * + * @param array $request The request data. + * + * @return bool Return true if the request is eligible for hierarchical sorting. + */ + public static function is_eligible( $request ) { + if ( ! isset( $request['orderby_hierarchy'] ) || true !== $request['orderby_hierarchy'] ) { + return false; } - return self::$instance; + return true; } - public function run( $args ) { + public static function run( $args ) { $new_args = array_merge( $args, array( @@ -36,28 +39,11 @@ public function run( $args ) { ); $query = new WP_Query( $new_args ); $posts = $query->posts; - $result = self::sort( $posts ); - - self::$post_ids = $result['post_ids']; - self::$levels = $result['levels']; - } - - /** - * Check if the request is eligible for hierarchical sorting. - * - * @param array $request The request data. - * - * @return bool Return true if the request is eligible for hierarchical sorting. - */ - public static function is_eligible( $request ) { - if ( ! isset( $request['orderby_hierarchy'] ) || true !== $request['orderby_hierarchy'] ) { - return false; - } - return true; + return self::sort( $posts ); } - public static function get_ancestor( $post_id ) { + private static function get_ancestor( $post_id ) { return get_post( $post_id )->post_parent ?? 0; } @@ -93,7 +79,7 @@ public static function get_ancestor( $post_id ) { * @type array $levels Array of levels for the corresponding post ID in the same index * } */ - public static function sort( $posts ) { + private static function sort( $posts ) { /* * Arrange pages in two arrays: * @@ -152,38 +138,4 @@ private static function add_hierarchical_ids( &$ids, &$levels, $level, $to_proce } } } - - public static function get_post_ids() { - return self::$post_ids; - } - - public static function get_levels() { - return self::$levels; - } -} - -function rest_page_query_hierarchical_sort_filter_by_post_in( $args, $request ) { - if ( ! Hierarchical_Sort::is_eligible( $request ) ) { - return $args; - } - - $hs = Hierarchical_Sort::get_instance(); - $hs->run( $args ); - - // Reconfigure the args to display only the ids in the list. - $args['post__in'] = $hs->get_post_ids(); - $args['orderby'] = 'post__in'; - - return $args; -} - -function rest_prepare_page_hierarchical_sort_add_levels( $response, $post, $request ) { - if ( ! Hierarchical_Sort::is_eligible( $request ) ) { - return $response; - } - - $hs = Hierarchical_Sort::get_instance(); - $response->data['level'] = $hs->get_levels()[ $post->ID ]; - - return $response; } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 9cbbb08836134..a7267a9c5afbb 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -47,6 +47,13 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { */ protected $allow_batch = array( 'v1' => true ); + /** + * Holds information about each post's level. + * Level means the depth of the post in the hierarchy: + * top-level posts have level 0, their children have level 1, and so on. + */ + protected $levels = array(); + /** * Constructor. * @@ -402,7 +409,13 @@ static function ( $format ) { // Force the post_type argument, since it's not a user input variable. $args['post_type'] = $this->post_type; - $args = rest_page_query_hierarchical_sort_filter_by_post_in( $args, $request ); + if ( Hierarchical_Sort::is_eligible( $request ) ) { + $result = Hierarchical_Sort::run( $args ); + $this->levels = $result[ 'levels' ]; + + $args[ 'post__in' ] = $result[ 'post_ids' ]; + $args[ 'orderby' ] = 'post__in'; + } /** * Filters WP_Query arguments when querying posts via the REST API. @@ -2092,7 +2105,9 @@ public function prepare_item_for_response( $item, $request ) { } } - $response = rest_prepare_page_hierarchical_sort_add_levels( $response, $post, $request ); + if ( Hierarchical_Sort::is_eligible( $request ) ) { + $response->data['level'] = $this->levels[ $post->ID ]; + } /** * Filters the post data for a REST API response.