-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof of concept: visualize hierarchical data #66479
Merged
+481
−9
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
95943cf
Visualize parent
oandregal 65dd563
Sort by title asc
oandregal 0fb533b
Return posts sorted by hierarchy
oandregal a058995
Visualize hierarchy
oandregal 4ef527c
Add view config control to switch on/off hierarchical vizualization
oandregal ba3ce47
Revert "Add view config control to switch on/off hierarchical vizuali…
oandregal 1fb12e3
Control automatically when hierarchical sort is active
oandregal 404d764
Display parent fields when hierarchy is active
oandregal 3e8946e
Revert "Display parent fields when hierarchy is active"
oandregal 3c753ac
Hide parent field by default
oandregal 6983694
Rename 'init' to 'get_instance' to reflect singleton pattern
mcsf 1c10d7e
GutenbergHierarchicalSortTest: satisfy linter
mcsf c04da3d
Gutenberg_Hierarchical_Sort::get_ancestor: Refactor using `??`
mcsf affe071
Gutenberg_Hierarchical_Sort::sort: Refactor for clarity
mcsf ef44147
Rename hierchicalSort to showLevels
oandregal e6c708e
Set showLevels internally using onChangeView instead of effect
oandregal e53e69b
Do not provide a default implementation for getItemLevel
oandregal da863ad
Remove unnecessary change
oandregal 307944d
Cover against empty post parent
oandregal 33c093f
Clarify PHPDoc and tests
oandregal c1961db
Update comment
oandregal bf876a8
Make PHP linter happy
oandregal 9cd80ee
Add backport changelog file
oandregal 28e8449
Limit to 9 items
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/8014 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/66479 |
205 changes: 205 additions & 0 deletions
205
lib/compat/wordpress-6.8/class-gutenberg-hierarchical-sort.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
<?php | ||
|
||
/** | ||
* Modifies the Post controller endpoint to support orderby_hierarchy. | ||
* | ||
* @package gutenberg | ||
* @since 6.8.0 | ||
*/ | ||
|
||
class Gutenberg_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(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
public function run( $args ) { | ||
$new_args = array_merge( | ||
$args, | ||
array( | ||
'fields' => '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; | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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; | ||
mcsf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
$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; | ||
} | ||
} | ||
|
||
add_filter( | ||
'rest_page_collection_params', | ||
function ( $params ) { | ||
$params['orderby_hierarchy'] = array( | ||
'description' => 'Sort pages by hierarchy.', | ||
'type' => 'boolean', | ||
'default' => false, | ||
); | ||
return $params; | ||
} | ||
); | ||
|
||
add_filter( | ||
'rest_page_query', | ||
function ( $args, $request ) { | ||
if ( ! Gutenberg_Hierarchical_Sort::is_eligible( $request ) ) { | ||
return $args; | ||
} | ||
|
||
$hs = Gutenberg_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; | ||
}, | ||
10, | ||
2 | ||
); | ||
|
||
add_filter( | ||
'rest_prepare_page', | ||
function ( $response, $post, $request ) { | ||
if ( ! Gutenberg_Hierarchical_Sort::is_eligible( $request ) ) { | ||
return $response; | ||
} | ||
|
||
$hs = Gutenberg_Hierarchical_Sort::get_instance(); | ||
$response->data['level'] = $hs->get_levels()[ $post->ID ]; | ||
|
||
return $response; | ||
}, | ||
10, | ||
3 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using static variables instead of returning the results instead? Personally I don't like this kind of caching. Actually, it's really not clear to me why this whole class exists. It can be just a function no (or a couple)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason for this class to exist is to make this PR performant. Given this PR uses two different filters, we'd have to calculate the info twice if we don't have a way to hold the results in the interim. The implementation of the backport PR would be a bit different that this: it can just modify the get_items method directly, so this can be simplified to be a simple function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More context: I used the filter approach in this PR instead of modifying the
get_items
method directly because I wanted reviewers to understand what changed. If this modified theget_items
method it'd have been hundreds of lines of (untouched) code with a few tweaks in between.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the review-friendly approach, especially when approaching something new to DataViews like hierarchy. The current approach with the singleton class isn't perfect, but I think it's acceptable for now. Thinking about Riad's question, what crossed my mind was to use global functions with an explicit cache:
But, honestly, this comes with other problems (a false impression that nothing could have mutated in between the two function calls?) and I don't think we should invest more time in anything other than "the real thing", as André points out, i.e.
get_items
. Until then, this class is fine. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't like the class to be honest. The same reason I don't like the current theme_json class. We know by experience now that this architecture created a lot of bugs (outdates styles...). I don't think using classes properties to cache things like that is good.
You call a run function with
args
but the cache is actually independent of the "args", so one calling the run function later with another set of args will conflict with the initial call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of performance penalty are we talking about if we remove all caching?
If indeed there is a penalty to avoid, how palatable would something like this be to both of you? (This works in my local Pages view.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad @mcsf I'd like to land the backport PR and it doesn't use filters or the static cache in the
HierarchicalSort
class. WordPress/wordpress-develop#8014Note the
HierarchicalSort
class still exist and exposes two public static methods. I think this is nice to prevent polluting the namespace with intermediate helper functions. If that's a blocker for you, I'm happy to make everything functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a class is not a blocker, but I would love for us to have a system in place for when to use functions and when to use classes rather than each one coming up with its own approach to things and second guessing ourselves.