Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use class directly and remove static cache
  • Loading branch information
oandregal committed Dec 26, 2024
commit a6000fc2f920ad73fcc0d74d28e9292db6e9de87
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do any of the methods in this class need to be static? Static methods have a number of downside, including performance as static methods are not garbage collected in the same way that other methods are. For this one place where the class is used, we could just create an instance of the class and use public methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be another class? Why can't this be part of the WP_REST_Posts_Controller class?


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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPdocs required.

$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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Php docs required.

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 @@
*/
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 @@
// 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 @@
}
}

$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
Loading