From e1545b5db1ccf6a4776f96edda2e9c6bf854d89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:07:25 +0100 Subject: [PATCH 1/7] Add logic to sort posts by hierarchy --- src/wp-includes/class-hierarchical-sort.php | 189 ++++++++++++++++ src/wp-includes/default-filters.php | 4 + .../class-wp-rest-posts-controller.php | 9 +- src/wp-settings.php | 1 + .../includes/class-hierarchical-sort-test.php | 207 ++++++++++++++++++ 5 files changed, 408 insertions(+), 2 deletions(-) create mode 100644 src/wp-includes/class-hierarchical-sort.php create mode 100644 tests/phpunit/includes/class-hierarchical-sort-test.php diff --git a/src/wp-includes/class-hierarchical-sort.php b/src/wp-includes/class-hierarchical-sort.php new file mode 100644 index 0000000000000..f4ac9c3d9942f --- /dev/null +++ b/src/wp-includes/class-hierarchical-sort.php @@ -0,0 +1,189 @@ + 'id=>parent', + 'posts_per_page' => -1, + ) + ); + $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; + } + + public static function get_ancestor( $post_id ) { + return get_post( $post_id )->post_parent ?? 0; + } + + /** + * Sort posts by hierarchy. + * + * Takes an array of posts and sorts them based on their parent-child relationships. + * It also tracks the level depth of each post in the hierarchy. + * + * Example input: + * ``` + * [ + * ['ID' => 4, 'post_parent' => 2], + * ['ID' => 2, 'post_parent' => 0], + * ['ID' => 3, 'post_parent' => 2], + * ] + * ``` + * + * Example output: + * ``` + * [ + * 'post_ids' => [2, 4, 3], + * 'levels' => [0, 1, 1] + * ] + * ``` + * + * @param array $posts Array of post objects containing ID and post_parent properties. + * + * @return array { + * Sorted post IDs and their hierarchical levels + * + * @type array $post_ids Array of post IDs + * @type array $levels Array of levels for the corresponding post ID in the same index + * } + */ + public static function sort( $posts ) { + /* + * Arrange pages in two arrays: + * + * - $top_level: posts whose parent is 0 + * - $children: post ID as the key and an array of children post IDs as the value. + * Example: $children[10][] contains all sub-pages whose parent is 10. + * + * Additionally, keep track of the levels of each post in $levels. + * Example: $levels[10] = 0 means the post ID is a top-level page. + * + */ + $top_level = array(); + $children = array(); + foreach ( $posts as $post ) { + if ( empty( $post->post_parent ) ) { + $top_level[] = $post->ID; + } else { + $children[ $post->post_parent ][] = $post->ID; + } + } + + $ids = array(); + $levels = array(); + self::add_hierarchical_ids( $ids, $levels, 0, $top_level, $children ); + + // Process remaining children. + if ( ! empty( $children ) ) { + foreach ( $children as $parent_id => $child_ids ) { + $level = 0; + $ancestor = $parent_id; + while ( 0 !== $ancestor ) { + ++$level; + $ancestor = self::get_ancestor( $ancestor ); + } + self::add_hierarchical_ids( $ids, $levels, $level, $child_ids, $children ); + } + } + + return array( + 'post_ids' => $ids, + 'levels' => $levels, + ); + } + + private static function add_hierarchical_ids( &$ids, &$levels, $level, $to_process, $children ) { + foreach ( $to_process as $id ) { + if ( in_array( $id, $ids, true ) ) { + continue; + } + $ids[] = $id; + $levels[ $id ] = $level; + + if ( isset( $children[ $id ] ) ) { + self::add_hierarchical_ids( $ids, $levels, $level + 1, $children[ $id ], $children ); + unset( $children[ $id ] ); + } + } + } + + 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/default-filters.php b/src/wp-includes/default-filters.php index 9eff6ecbeb67a..26477634ea581 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -772,4 +772,8 @@ add_filter( 'rest_prepare_wp_block', 'insert_hooked_blocks_into_rest_response', 10, 2 ); add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 ); +// Filter the pages endpoint to consider orderby_hierarchy. +add_filter( 'rest_page_query', 'rest_page_query_hierarchical_sort_filter_by_post_in', 10, 2 ); +add_filter( 'rest_prepare_page', 'rest_prepare_page_hierarchical_sort_add_levels', 10, 3 ); + unset( $filter, $action ); 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 95851199c6e4d..eaac5b37a6c88 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 @@ -2991,7 +2991,7 @@ public function get_collection_params() { $post_type = get_post_type_object( $this->post_type ); if ( $post_type->hierarchical || 'attachment' === $this->post_type ) { - $query_params['parent'] = array( + $query_params['parent'] = array( 'description' => __( 'Limit result set to items with particular parent IDs.' ), 'type' => 'array', 'items' => array( @@ -2999,7 +2999,7 @@ public function get_collection_params() { ), 'default' => array(), ); - $query_params['parent_exclude'] = array( + $query_params['parent_exclude'] = array( 'description' => __( 'Limit result set to all items except those of a particular parent ID.' ), 'type' => 'array', 'items' => array( @@ -3007,6 +3007,11 @@ public function get_collection_params() { ), 'default' => array(), ); + $query_params['orderby_hierarchy'] = array( + 'description' => 'Sort pages by hierarchy.', + 'type' => 'boolean', + 'default' => false, + ); } $query_params['search_columns'] = array( diff --git a/src/wp-settings.php b/src/wp-settings.php index 635f6de248dd5..16902b231ff49 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -404,6 +404,7 @@ require ABSPATH . WPINC . '/interactivity-api/class-wp-interactivity-api-directives-processor.php'; require ABSPATH . WPINC . '/interactivity-api/interactivity-api.php'; require ABSPATH . WPINC . '/class-wp-plugin-dependencies.php'; +require ABSPATH . WPINC . '/class-hierarchical-sort.php'; add_action( 'after_setup_theme', array( wp_script_modules(), 'add_hooks' ) ); add_action( 'after_setup_theme', array( wp_interactivity(), 'add_hooks' ) ); diff --git a/tests/phpunit/includes/class-hierarchical-sort-test.php b/tests/phpunit/includes/class-hierarchical-sort-test.php new file mode 100644 index 0000000000000..6be9e5d6c921d --- /dev/null +++ b/tests/phpunit/includes/class-hierarchical-sort-test.php @@ -0,0 +1,207 @@ + 11, + 'post_parent' => 9, + ), + (object) array( + 'ID' => 12, + 'post_parent' => 0, + ), + (object) array( + 'ID' => 8, + 'post_parent' => 0, + ), + (object) array( + 'ID' => 3, + 'post_parent' => 12, + ), + (object) array( + 'ID' => 6, + 'post_parent' => 3, + ), + (object) array( + 'ID' => 7, + 'post_parent' => 4, + ), + (object) array( + 'ID' => 9, + 'post_parent' => 8, + ), + (object) array( + 'ID' => 4, + 'post_parent' => 12, + ), + (object) array( + 'ID' => 5, + 'post_parent' => 3, + ), + (object) array( + 'ID' => 10, + 'post_parent' => 8, + ), + ); + + $hs = Hierarchical_Sort::get_instance(); + $result = $hs->sort( $input ); + $this->assertEquals( array( 12, 3, 6, 5, 4, 7, 8, 9, 11, 10 ), $result['post_ids'] ); + $this->assertEquals( + array( + 12 => 0, + 3 => 1, + 6 => 2, + 5 => 2, + 4 => 1, + 7 => 2, + 8 => 0, + 9 => 1, + 11 => 2, + 10 => 1, + ), + $result['levels'] + ); + } + + public function test_return_orphans() { + /* + * Keep this updated as the input array changes. + * The sorted hierarchy would be as follows: + * + * - 11 (orphan) + * - 4 (orphan) + * -- 7 + * + */ + $input = array( + (object) array( + 'ID' => 11, + 'post_parent' => 2, + ), + (object) array( + 'ID' => 7, + 'post_parent' => 4, + ), + (object) array( + 'ID' => 4, + 'post_parent' => 2, + ), + ); + + $hs = Hierarchical_Sort::get_instance(); + $result = $hs->sort( $input ); + $this->assertEquals( array( 11, 4, 7 ), $result['post_ids'] ); + $this->assertEquals( + array( + 11 => 1, + 4 => 1, + 7 => 2, + ), + $result['levels'] + ); + } + + public function test_post_with_empty_post_parent_are_considered_top_level() { + /* + * Keep this updated as the input array changes. + * The sorted hierarchy would be as follows: + * + * 2 + * - 3 + * -- 5 + * -- 6 + * - 4 + * -- 7 + * 8 + * - 9 + * -- 11 + * - 10 + * + */ + $input = array( + (object) array( + 'ID' => 11, + 'post_parent' => 9, + ), + (object) array( + 'ID' => 2, + 'post_parent' => 0, + ), + (object) array( + 'ID' => 8, + 'post_parent' => '', // Empty post parent, should be considered top-level. + ), + (object) array( + 'ID' => 3, + 'post_parent' => 2, + ), + (object) array( + 'ID' => 5, + 'post_parent' => 3, + ), + (object) array( + 'ID' => 7, + 'post_parent' => 4, + ), + (object) array( + 'ID' => 9, + 'post_parent' => 8, + ), + (object) array( + 'ID' => 4, + 'post_parent' => 2, + ), + (object) array( + 'ID' => 6, + 'post_parent' => 3, + ), + (object) array( + 'ID' => 10, + 'post_parent' => 8, + ), + ); + + $hs = Hierarchical_Sort::get_instance(); + $result = $hs->sort( $input ); + $this->assertEquals( array( 2, 3, 5, 6, 4, 7, 8, 9, 11, 10 ), $result['post_ids'] ); + $this->assertEquals( + array( + 2 => 0, + 3 => 1, + 5 => 2, + 6 => 2, + 4 => 1, + 7 => 2, + 8 => 0, + 9 => 1, + 11 => 2, + 10 => 1, + ), + $result['levels'] + ); + } +} From 09fb57baae052d58b663ba80aeff25201d8885fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:47:41 +0100 Subject: [PATCH 2/7] Declare orderby_hierarchy in post controller test --- .../rest-api/endpoints/class-wp-rest-posts-controller.php | 4 +++- tests/phpunit/tests/rest-api/rest-pages-controller.php | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) 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 eaac5b37a6c88..9750789d11a85 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 @@ -3007,8 +3007,10 @@ public function get_collection_params() { ), 'default' => array(), ); + } + if ( $post_type->hierarchical ) { $query_params['orderby_hierarchy'] = array( - 'description' => 'Sort pages by hierarchy.', + 'description' => __( 'Whether the post should be grouped by parent-child relationship (hierarchy).' ), 'type' => 'boolean', 'default' => false, ); diff --git a/tests/phpunit/tests/rest-api/rest-pages-controller.php b/tests/phpunit/tests/rest-api/rest-pages-controller.php index 9717a7fcda1c6..37478a1ec850c 100644 --- a/tests/phpunit/tests/rest-api/rest-pages-controller.php +++ b/tests/phpunit/tests/rest-api/rest-pages-controller.php @@ -79,6 +79,7 @@ public function test_registered_query_params() { 'offset', 'order', 'orderby', + 'orderby_hierarchy', 'page', 'parent', 'parent_exclude', From 48b3a5306abb34af5fa9d4edfaf1f522e91edb3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:52:03 +0100 Subject: [PATCH 3/7] Update wp-api-generated fixture --- tests/qunit/fixtures/wp-api-generated.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 448548aab0c07..3fc6df7d0128d 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -1808,6 +1808,12 @@ mockedApiResponse.Schema = { "default": [], "required": false }, + "orderby_hierarchy": { + "description": "Whether the post should be grouped by parent-child relationship (hierarchy).", + "type": "boolean", + "default": false, + "required": false + }, "search_columns": { "default": [], "description": "Array of column names to be searched.", From cd2f956064fd306225ffdd133e6cf3e40002d5ba 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 13:26:52 +0100 Subject: [PATCH 4/7] Do not use filters --- src/wp-includes/default-filters.php | 4 ---- .../rest-api/endpoints/class-wp-rest-posts-controller.php | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 26477634ea581..9eff6ecbeb67a 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -772,8 +772,4 @@ add_filter( 'rest_prepare_wp_block', 'insert_hooked_blocks_into_rest_response', 10, 2 ); add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 ); -// Filter the pages endpoint to consider orderby_hierarchy. -add_filter( 'rest_page_query', 'rest_page_query_hierarchical_sort_filter_by_post_in', 10, 2 ); -add_filter( 'rest_prepare_page', 'rest_prepare_page_hierarchical_sort_add_levels', 10, 3 ); - unset( $filter, $action ); 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 9750789d11a85..9cbbb08836134 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 @@ -402,6 +402,8 @@ 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 ); + /** * Filters WP_Query arguments when querying posts via the REST API. * @@ -2090,6 +2092,8 @@ public function prepare_item_for_response( $item, $request ) { } } + $response = rest_prepare_page_hierarchical_sort_add_levels( $response, $post, $request ); + /** * Filters the post data for a REST API response. * 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 5/7] 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. From b4a54ef382b6bcfbcd92e77cdb503e4d98cbd113 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:39:54 +0100 Subject: [PATCH 6/7] Fix PHP lint issues --- .../endpoints/class-wp-rest-posts-controller.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 a7267a9c5afbb..962dc72db42f1 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 @@ -411,10 +411,10 @@ static function ( $format ) { if ( Hierarchical_Sort::is_eligible( $request ) ) { $result = Hierarchical_Sort::run( $args ); - $this->levels = $result[ 'levels' ]; + $this->levels = $result['levels']; - $args[ 'post__in' ] = $result[ 'post_ids' ]; - $args[ 'orderby' ] = 'post__in'; + $args['post__in'] = $result['post_ids']; + $args['orderby'] = 'post__in'; } /** @@ -3010,7 +3010,7 @@ public function get_collection_params() { $post_type = get_post_type_object( $this->post_type ); if ( $post_type->hierarchical || 'attachment' === $this->post_type ) { - $query_params['parent'] = array( + $query_params['parent'] = array( 'description' => __( 'Limit result set to items with particular parent IDs.' ), 'type' => 'array', 'items' => array( @@ -3018,7 +3018,7 @@ public function get_collection_params() { ), 'default' => array(), ); - $query_params['parent_exclude'] = array( + $query_params['parent_exclude'] = array( 'description' => __( 'Limit result set to all items except those of a particular parent ID.' ), 'type' => 'array', 'items' => array( From ec420bb22e38ce744a44a18f2e2a1173738081fa 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:41:38 +0100 Subject: [PATCH 7/7] Update unit tests to latest changes --- tests/phpunit/includes/class-hierarchical-sort-test.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/includes/class-hierarchical-sort-test.php b/tests/phpunit/includes/class-hierarchical-sort-test.php index 6be9e5d6c921d..a0d17c9035c20 100644 --- a/tests/phpunit/includes/class-hierarchical-sort-test.php +++ b/tests/phpunit/includes/class-hierarchical-sort-test.php @@ -67,8 +67,7 @@ public function test_return_all_post_ids() { ), ); - $hs = Hierarchical_Sort::get_instance(); - $result = $hs->sort( $input ); + $result = Hierarchical_Sort::run( $input ); $this->assertEquals( array( 12, 3, 6, 5, 4, 7, 8, 9, 11, 10 ), $result['post_ids'] ); $this->assertEquals( array( @@ -112,8 +111,7 @@ public function test_return_orphans() { ), ); - $hs = Hierarchical_Sort::get_instance(); - $result = $hs->sort( $input ); + $result = Hierarchical_Sort::run( $input ); $this->assertEquals( array( 11, 4, 7 ), $result['post_ids'] ); $this->assertEquals( array( @@ -185,8 +183,7 @@ public function test_post_with_empty_post_parent_are_considered_top_level() { ), ); - $hs = Hierarchical_Sort::get_instance(); - $result = $hs->sort( $input ); + $result = Hierarchical_Sort::run( $input ); $this->assertEquals( array( 2, 3, 5, 6, 4, 7, 8, 9, 11, 10 ), $result['post_ids'] ); $this->assertEquals( array(