Skip to content

Commit

Permalink
Use class directly and remove static cache
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 26, 2024
1 parent cd2f956 commit a6000fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 65 deletions.
78 changes: 15 additions & 63 deletions src/wp-includes/class-hierarchical-sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}

Expand Down Expand Up @@ -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:
*
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' ];

Check failure on line 414 in src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

$args[ 'post__in' ] = $result[ 'post_ids' ];

Check failure on line 416 in src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 416 in src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
$args[ 'orderby' ] = 'post__in';

Check failure on line 417 in src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
}

/**
* Filters WP_Query arguments when querying posts via the REST API.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a6000fc

Please sign in to comment.