Skip to content

Commit 60d6fb3

Browse files
authored
Merge pull request #404 from google/fix/401-prevent-menu-override
Ensure the Site Kit admin menu cannot be accidentally overridden by other plugins
2 parents 6842b00 + 1fa6a28 commit 60d6fb3

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

includes/Core/Admin/Screen.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ public function add( Context $context ) {
129129
function() use ( $context ) {
130130
$this->render( $context );
131131
},
132-
$context->url( 'dist/assets/images/logo-g_white_small.png' ),
133-
3
132+
$context->url( 'dist/assets/images/logo-g_white_small.png' )
134133
);
135134
$menu_slug = $this->slug;
136135
}

includes/Core/Admin/Screens.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ function() {
127127
add_action( 'admin_notices', $remove_notices_callback, -9999 );
128128
add_action( 'network_admin_notices', $remove_notices_callback, -9999 );
129129
add_action( 'all_admin_notices', $remove_notices_callback, -9999 );
130+
131+
add_filter( 'custom_menu_order', '__return_true' );
132+
add_filter(
133+
'menu_order',
134+
function( array $menu_order ) {
135+
$new_order = array();
136+
foreach ( $menu_order as $index => $item ) {
137+
if ( 'index.php' === $item || 0 === strpos( $item, self::PREFIX ) ) {
138+
$new_order[] = $item;
139+
unset( $menu_order[ $index ] );
140+
}
141+
}
142+
return array_values( array_merge( $new_order, $menu_order ) );
143+
}
144+
);
130145
}
131146

132147
/**

tests/phpunit/integration/Core/Admin/ScreensTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,31 @@ public function test_removal_of_admin_notices_outside_sitekit( $hookname ) {
9595
do_action( $hookname );
9696
$this->assertNotEmpty( ob_get_clean() );
9797
}
98+
99+
public function test_menu_order() {
100+
$menu_order = array(
101+
'index.php',
102+
'third-party-plugin',
103+
'edit.php',
104+
'options-general.php',
105+
'googlesitekit-dashboard',
106+
);
107+
108+
$this->screens->register();
109+
110+
// Imitate WordPress core running these filters.
111+
if ( apply_filters( 'custom_menu_order', false ) ) {
112+
$menu_order = apply_filters( 'menu_order', $menu_order );
113+
}
114+
115+
$expected_order = array(
116+
'index.php',
117+
'googlesitekit-dashboard',
118+
'third-party-plugin',
119+
'edit.php',
120+
'options-general.php',
121+
);
122+
123+
$this->assertEquals( $expected_order, $menu_order );
124+
}
98125
}

0 commit comments

Comments
 (0)