Skip to content

Commit

Permalink
Merge pull request #404 from google/fix/401-prevent-menu-override
Browse files Browse the repository at this point in the history
Ensure the Site Kit admin menu cannot be accidentally overridden by other plugins
  • Loading branch information
felixarntz authored Aug 14, 2019
2 parents 6842b00 + 1fa6a28 commit 60d6fb3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 1 addition & 2 deletions includes/Core/Admin/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ public function add( Context $context ) {
function() use ( $context ) {
$this->render( $context );
},
$context->url( 'dist/assets/images/logo-g_white_small.png' ),
3
$context->url( 'dist/assets/images/logo-g_white_small.png' )
);
$menu_slug = $this->slug;
}
Expand Down
15 changes: 15 additions & 0 deletions includes/Core/Admin/Screens.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,21 @@ function() {
add_action( 'admin_notices', $remove_notices_callback, -9999 );
add_action( 'network_admin_notices', $remove_notices_callback, -9999 );
add_action( 'all_admin_notices', $remove_notices_callback, -9999 );

add_filter( 'custom_menu_order', '__return_true' );
add_filter(
'menu_order',
function( array $menu_order ) {
$new_order = array();
foreach ( $menu_order as $index => $item ) {
if ( 'index.php' === $item || 0 === strpos( $item, self::PREFIX ) ) {
$new_order[] = $item;
unset( $menu_order[ $index ] );
}
}
return array_values( array_merge( $new_order, $menu_order ) );
}
);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/phpunit/integration/Core/Admin/ScreensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,31 @@ public function test_removal_of_admin_notices_outside_sitekit( $hookname ) {
do_action( $hookname );
$this->assertNotEmpty( ob_get_clean() );
}

public function test_menu_order() {
$menu_order = array(
'index.php',
'third-party-plugin',
'edit.php',
'options-general.php',
'googlesitekit-dashboard',
);

$this->screens->register();

// Imitate WordPress core running these filters.
if ( apply_filters( 'custom_menu_order', false ) ) {
$menu_order = apply_filters( 'menu_order', $menu_order );
}

$expected_order = array(
'index.php',
'googlesitekit-dashboard',
'third-party-plugin',
'edit.php',
'options-general.php',
);

$this->assertEquals( $expected_order, $menu_order );
}
}

0 comments on commit 60d6fb3

Please sign in to comment.