Skip to content
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

refactor: wizards menu order handling #3501

Merged
merged 1 commit into from
Oct 25, 2024

Conversation

miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented Oct 25, 2024

All Submissions:

Changes proposed in this Pull Request:

I started investigating the different ways we're managing menu order for wizard, prompted by @ronchambers comment regarding the Newsletters' wizard menu order:

I was having a hard time setting the Newsletters CPT parent menu item to a decimal value. It seems that CPTs only support an integer value for menu_positon. So I created a function that will loop through global $menu and then move the CPT to position 3.3 or "3.33" if something is already at 3.3. See the current code in function move_cpt_menu(). You may want to look at function registered_post_type_newsletters( $post_type ) to see how I'm changing the menu_icon but when I try to change the menu_position to 3.3 in that function it doesn't work.

Turns out we have multiple strategies in place:

This is hard to maintain and unpredictable. In this refactor PR I propose a consolidated strategy applied in the class-wizards.php:

/**
* Update menu order for wizards with parent menu items.
*
* @param array $menu_order The current menu order.
*
* @return array The updated menu order.
*/
public static function menu_order( $menu_order ) {
$index = array_search( 'newspack-dashboard', $menu_order, true );
if ( false === $index ) {
return $menu_order;
}
$ordered_wizards = [];
foreach ( self::$wizards as $slug => $wizard ) {
if ( ! empty( $wizard->parent_menu ) && ! empty( $wizard->menu_order ) ) {
$ordered_wizards[ $wizard->menu_order ] = $wizard->parent_menu;
}
}
if ( empty( $ordered_wizards ) ) {
return $menu_order;
}
ksort( $ordered_wizards );
foreach ( array_reverse( $ordered_wizards ) as $menu_item ) {
$key = array_search( $menu_item, $menu_order, true );
if ( false === $key ) {
continue;
}
array_splice( $menu_order, $key, 1 );
array_splice( $menu_order, $index + 1, 0, $menu_item );
}
return $menu_order;
}

Each wizard with a parent menu item can use the public vars parent_menu and menu_order to determine how it should render relative to the newspack-dashboard wizard:

/**
* The parent menu item name.
*
* @var string
*/
public $parent_menu = 'newspack-network';
/**
* Order relative to the Newspack Dashboard menu item.
*
* @var int
*/
public $menu_order = 5;

Result:

image

This PR also deprecates the menu_priority variable from the abstract class. It's a bit misleading, as it only applies the hook priority to the admin_menu hook. This is not a guaranteed way of setting the menu order because it gets overridden by the add_menu_page argument.

How to test the changes in this Pull Request:

  1. Checkout this PR, make sure you have Ads, Network, and Newsletters plugin active
  2. Confirm the menu order reflects the image above
  3. Click each menu and confirm it behaves as expected

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@miguelpeixe miguelpeixe added the [Status] Needs Review The issue or pull request needs to be reviewed label Oct 25, 2024
@miguelpeixe miguelpeixe self-assigned this Oct 25, 2024
@miguelpeixe miguelpeixe requested a review from a team as a code owner October 25, 2024 17:04
Copy link
Collaborator

@jaredrethman jaredrethman left a comment

Choose a reason for hiding this comment

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

@miguelpeixe have tested the code and works as described.

Additionally: I really like your approach, first time i’ve come across custom_menu_order hook - looks like the right tool for the job!

Approved!

@github-actions github-actions bot added [Status] Approved The pull request has been reviewed and is ready to merge and removed [Status] Needs Review The issue or pull request needs to be reviewed labels Oct 25, 2024
@miguelpeixe
Copy link
Member Author

Thanks, @jaredrethman! Since this touches on a core aspect of the project, I'll also wait for @ronchambers feedback before merging.

@leogermani
Copy link
Contributor

Not sure if this has any impact here, but for awareness: https://core.trac.wordpress.org/ticket/52035

I've been meaning to circle back to this and help it get to the finish line

@ronchambers
Copy link
Collaborator

I didn't test locally, but I trust the two of you 😃 Overall, I agree that moving the menus around for the 3 different plugins (Listings, Newsletters, and Network - my original tasks ) became cumbersome where each plugin had a different approach... So yes, a standard approach is great! Since Listings and Newsletters were re-assigned to @miguelpeixe I won't worry about testing those. I do need to do some final testing on the Network plugin wizard so I'll be sure to merge this (or epic/ia after it's merged) to my Network branch and verify it's all good there too. Approved for me! Thanks!

@miguelpeixe
Copy link
Member Author

Not sure if this has any impact here, but for awareness: https://core.trac.wordpress.org/ticket/52035

Thanks for flagging! The strategy doesn't cover submenu pages. I'm assuming we have tighter control over the execution order for submenu pages so it's less problematic.

@miguelpeixe miguelpeixe merged commit 435cf72 into feat/ia-newsletters Oct 25, 2024
9 checks passed
@miguelpeixe miguelpeixe deleted the refactor/ia-wizard-menu-order branch October 25, 2024 18:36
@miguelpeixe miguelpeixe mentioned this pull request Oct 29, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] Approved The pull request has been reviewed and is ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants