Skip to content

Commit

Permalink
Admin Menu: Improve menu item url generator. (#17955)
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland authored Dec 7, 2020
1 parent b0dbe54 commit 9309b6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private function prepare_menu_item_icon( $icon ) {
}

/**
* Prepares a menu icon for consumption by Calypso.
* Prepares a menu item url for consumption by Calypso.
*
* @param string $url Menu slug.
* @param string $parent_slug Optional. Parent menu item slug. Default empty string.
Expand All @@ -279,8 +279,9 @@ private function prepare_menu_item_url( $url, $parent_slug = '' ) {
if ( 0 === strpos( $url, 'https://wordpress.com' ) ) {
$url = str_replace( 'https://wordpress.com', '', $url );
} else {
$menu_hook = get_plugin_page_hook( $url, 'admin.php' );
$menu_file = wp_parse_url( $url, PHP_URL_PATH ); // Removes query args to get a file name.
$menu_hook = get_plugin_page_hook( $url, $parent_slug );
$menu_file = wp_parse_url( $url, PHP_URL_PATH ); // Removes query args to get a file name.
$parent_file = wp_parse_url( $parent_slug, PHP_URL_PATH );

if (
! empty( $menu_hook ) ||
Expand All @@ -291,19 +292,19 @@ private function prepare_menu_item_url( $url, $parent_slug = '' ) {
)
) {
if (
( 'admin.php' !== $parent_slug && file_exists( WP_PLUGIN_DIR . "/$parent_slug" ) && ! is_dir( WP_PLUGIN_DIR . "/$parent_slug" ) ) ||
( file_exists( ABSPATH . "/wp-admin/$parent_slug" ) && ! is_dir( ABSPATH . "/wp-admin/$parent_slug" ) )
( 'admin.php' !== $parent_file && file_exists( WP_PLUGIN_DIR . "/$parent_file" ) && ! is_dir( WP_PLUGIN_DIR . "/$parent_file" ) ) ||
( file_exists( ABSPATH . "/wp-admin/$parent_file" ) && ! is_dir( ABSPATH . "/wp-admin/$parent_file" ) )
) {
$url = add_query_arg( array( 'page' => $url ), admin_url( $parent_slug ) );
} else {
$url = add_query_arg( array( 'page' => $url ), admin_url( 'admin.php' ) );
}
} else {
} elseif ( file_exists( ABSPATH . "/wp-admin/$menu_file" ) ) {
$url = admin_url( $url );
}
}

return esc_url( $url );
return $url;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,35 @@ public function menu_item_icon_data() {
*
* @param string $url Menu item url as generated in wp-admin/menu.php.
* @param string $parent_slug Menu parent slug as generated in wp-admin/menu.php.
* @param string $callback Menu callback as generated in wp-admin/menu.php.
* @param string $expected Menu item url ready for API response.
*
* @throws \ReflectionException Noop.
* @dataProvider menu_item_url_data
* @covers ::prepare_menu_item_url
*/
public function test_prepare_menu_item_url( $url, $parent_slug, $expected ) {
public function test_prepare_menu_item_url( $url, $parent_slug, $callback, $expected ) {
$class = new ReflectionClass( 'WPCOM_REST_API_V2_Endpoint_Admin_Menu' );

$prepare_menu_item_url = $class->getMethod( 'prepare_menu_item_url' );
$prepare_menu_item_url->setAccessible( true );

// Fake plugin page.
add_action( 'admin_page_' . $url, '__return_false' );
if ( empty( $parent_slug ) ) {
add_menu_page( 'Title', 'Title', 'read', $url, $callback );
} else {
add_submenu_page( $parent_slug, 'Title', 'Title', 'read', $url, $callback );
}

$this->assertEquals(
$expected,
$prepare_menu_item_url->invokeArgs( new WPCOM_REST_API_V2_Endpoint_Admin_Menu(), array( $url, $parent_slug ) )
);

if ( empty( $parent_slug ) ) {
remove_menu_page( $url );
} else {
remove_submenu_page( $parent_slug, $url );
}
}

/**
Expand All @@ -300,32 +310,63 @@ public function menu_item_url_data() {
array(
'https://wordpress.com/me',
'',
null,
'/me',
),
// Core menu item URL.
array(
'uploads.php',
'upload.php',
'',
'http://example.org/wp-admin/uploads.php',
null,
'http://example.org/wp-admin/upload.php',
),
// Submenu item URL.
array(
'custom_settings',
'upload.php',
'__return_true',
'http://example.org/wp-admin/upload.php?page=custom_settings',
),
// Plugin menu item URL.
array(
'custom_settings',
'admin.php',
'',
'__return_true',
'http://example.org/wp-admin/admin.php?page=custom_settings',
),
// Plugin menu item URL without a parent.
array(
'custom_settings',
'',
'admin.php',
'__return_true',
'http://example.org/wp-admin/admin.php?page=custom_settings',
),
// Jetpack.
array(
'https://jetpack.com/redirect/?source=calypso-backups&site=example.org',
'jetpack',
null,
'https://jetpack.com/redirect/?source=calypso-backups&site=example.org',
),
// WooCommerce URLs.
array(
'product_attributes',
'edit.php?post_type=product',
'__return_true',
'http://example.org/wp-admin/edit.php?post_type=product&page=product_attributes',
),
array(
'wc-admin&path=/analytics/products',
'wc-admin&path=/analytics/overview',
'__return_true',
'http://example.org/wp-admin/admin.php?page=wc-admin&path=/analytics/products',
),
array(
'wc-admin&path=customers',
'woocommerce',
'__return_true',
'http://example.org/wp-admin/admin.php?page=wc-admin&path=customers',
),
);
}
}

0 comments on commit 9309b6b

Please sign in to comment.