Clean up menu callback methods (https://github.com/woocommerce/woocommerce-admin/pull/5679)
* Refactor menu item callback check * Migrate parent items that have first child item migrated * Allow wp menu items to be hidden with public method * Rename hide menu item method
This commit is contained in:
parent
479eb2c850
commit
91ac789393
|
@ -441,6 +441,36 @@ class Menu {
|
||||||
return $menu;
|
return $menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a menu item's callback is registered in the menu.
|
||||||
|
*
|
||||||
|
* @param array $menu_item Menu item args.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function has_callback( $menu_item ) {
|
||||||
|
if ( ! $menu_item || ! isset( $menu_item[ self::CALLBACK ] ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$callback = $menu_item[ self::CALLBACK ];
|
||||||
|
|
||||||
|
if (
|
||||||
|
isset( self::$callbacks[ $callback ] ) &&
|
||||||
|
self::$callbacks[ $callback ]
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
isset( self::$callbacks[ self::get_callback_url( $callback ) ] ) &&
|
||||||
|
self::$callbacks[ self::get_callback_url( $callback ) ]
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hides all WP admin menus items and adds screen IDs to check for new items.
|
* Hides all WP admin menus items and adds screen IDs to check for new items.
|
||||||
*
|
*
|
||||||
|
@ -451,26 +481,22 @@ class Menu {
|
||||||
global $submenu;
|
global $submenu;
|
||||||
|
|
||||||
foreach ( $menu as $key => $menu_item ) {
|
foreach ( $menu as $key => $menu_item ) {
|
||||||
if (
|
if ( self::has_callback( $menu_item ) ) {
|
||||||
isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) &&
|
$menu[ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
|
||||||
self::$callbacks[ $menu_item[ self::CALLBACK ] ]
|
continue;
|
||||||
) {
|
}
|
||||||
|
|
||||||
|
// WordPress core menus make the parent item the same URL as the first child.
|
||||||
|
$has_children = isset( $submenu[ $menu_item[ self::CALLBACK ] ] ) && isset( $submenu[ $menu_item[ self::CALLBACK ] ][0] );
|
||||||
|
$first_child = $has_children ? $submenu[ $menu_item[ self::CALLBACK ] ][0] : null;
|
||||||
|
if ( self::has_callback( $first_child ) ) {
|
||||||
$menu[ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
|
$menu[ $key ][ self::CSS_CLASSES ] .= ' hide-if-js';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $submenu as $parent_key => $parent ) {
|
foreach ( $submenu as $parent_key => $parent ) {
|
||||||
foreach ( $parent as $key => $menu_item ) {
|
foreach ( $parent as $key => $menu_item ) {
|
||||||
if (
|
if ( self::has_callback( $menu_item ) ) {
|
||||||
(
|
|
||||||
isset( self::$callbacks[ $menu_item[ self::CALLBACK ] ] ) &&
|
|
||||||
self::$callbacks[ $menu_item[ self::CALLBACK ] ]
|
|
||||||
) ||
|
|
||||||
(
|
|
||||||
isset( self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ] ) &&
|
|
||||||
self::$callbacks[ self::get_callback_url( $menu_item[ self::CALLBACK ] ) ]
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Disable phpcs since we need to override submenu classes.
|
// Disable phpcs since we need to override submenu classes.
|
||||||
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.
|
// Note that `phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited` does not work to disable this check.
|
||||||
// phpcs:disable
|
// phpcs:disable
|
||||||
|
@ -494,6 +520,13 @@ class Menu {
|
||||||
return $menu;
|
return $menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a callback to identify and hide pages in the WP menu.
|
||||||
|
*/
|
||||||
|
public static function hide_wp_menu_item( $callback ) {
|
||||||
|
self::$callbacks[ $callback ] = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get registered menu items.
|
* Get registered menu items.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue