-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from alleyinteractive/feature/6/tidy-admin-bar
Remove Selected Admin Bar Links
- Loading branch information
Showing
4 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Class file for Clean_Admin_Bar | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Alley\WP\Alleyvate\Feature; | ||
|
||
/** | ||
* Cleans admin bar. | ||
*/ | ||
final class Clean_Admin_Bar implements Feature { | ||
/** | ||
* Boot the feature. | ||
*/ | ||
public function boot(): void { | ||
add_action( 'wp_before_admin_bar_render', [ $this, 'before_admin_bar_render' ], 9999 ); | ||
} | ||
|
||
/** | ||
* Disables specified menus in admin bar. | ||
* | ||
* @return void | ||
*/ | ||
public function before_admin_bar_render() { | ||
global $wp_admin_bar; | ||
|
||
foreach ( $this->get_disposable_nodes() as $node ) { | ||
$wp_admin_bar->remove_menu( $node ); | ||
} | ||
} | ||
|
||
/** | ||
* Set menus to be disabled. | ||
* | ||
* @return mixed|void | ||
*/ | ||
public function get_disposable_nodes() { | ||
$disposable_nodes = [ | ||
'comments', | ||
'themes', | ||
'updates', | ||
'wp-logo', | ||
]; | ||
|
||
return apply_filters( 'alleyvate_clean_admin_bar_menus', $disposable_nodes ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
tests/alley/wp/alleyvate/features/test-clean-admin-bar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* Class file for Test_Clean_Admin_Bar | ||
* | ||
* (c) Alley <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @package wp-alleyvate | ||
*/ | ||
|
||
namespace Alley\WP\Alleyvate\Features; | ||
|
||
use Alley\WP\Alleyvate\Feature; | ||
use Mantle\Testkit\Test_Case; | ||
|
||
/** | ||
* Tests for the cleaning of the admin bar. | ||
*/ | ||
final class Test_Clean_Admin_Bar extends Test_Case { | ||
use \Mantle\Testing\Concerns\Refresh_Database; | ||
|
||
/** | ||
* Feature instance. | ||
* | ||
* @var Feature | ||
*/ | ||
private Feature $feature; | ||
|
||
/** | ||
* Set up. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->feature = new Clean_Admin_Bar(); | ||
} | ||
|
||
/** | ||
* Test default admin bar cleaning. | ||
*/ | ||
public function test_remove_admin_bar_nodes() { | ||
|
||
$admin_bar = $this->apply_admin_bar(); | ||
|
||
// Get nodes to compare. | ||
$disposable_nodes = $this->feature->get_disposable_nodes(); | ||
$current_nodes = $admin_bar->get_nodes(); | ||
|
||
// Let's make sure the nodes exist before we remove them. | ||
foreach ( $disposable_nodes as $disposable_node ) { | ||
// Updates will not exist in a test context. | ||
if ( 'updates' === $disposable_node ) { | ||
continue; | ||
} | ||
$this->assertArrayHasKey( $disposable_node, $current_nodes, $disposable_node . ' should exist in $wp_admin_bar global prior to boot.' ); | ||
} | ||
|
||
// Boot feature. | ||
$this->feature->boot(); | ||
do_action( 'wp_before_admin_bar_render' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound | ||
|
||
// Get updated set of nodes. | ||
$current_nodes = $admin_bar->get_nodes(); | ||
|
||
// Compare again. | ||
foreach ( $disposable_nodes as $disposable_node ) { | ||
$this->assertArrayNotHasKey( $disposable_node, $current_nodes, $disposable_node . ' should not exist in $wp_admin_bar global after boot.' ); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Test admin bar cleaning using filter. | ||
*/ | ||
public function test_filter() { | ||
|
||
$admin_bar = $this->apply_admin_bar(); | ||
$node = 'my-account'; | ||
|
||
add_filter( | ||
'alleyvate_clean_admin_bar_menus', | ||
function ( $disposable_nodes ) use ( $node ) { | ||
$disposable_nodes[] = $node; | ||
|
||
return $disposable_nodes; | ||
} | ||
); | ||
|
||
// Get nodes to compare. | ||
$current_nodes = $admin_bar->get_nodes(); | ||
|
||
// Let's make sure the node exists before we remove it. | ||
$this->assertArrayHasKey( $node, $current_nodes, 'The filtered node ' . $node . ' should exist in $wp_admin_bar global prior to boot.' ); | ||
|
||
// Boot feature. | ||
$this->feature->boot(); | ||
do_action( 'wp_before_admin_bar_render' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound | ||
|
||
// Get updated set of nodes. | ||
$current_nodes = $admin_bar->get_nodes(); | ||
|
||
$this->assertArrayNotHasKey( $node, $current_nodes, 'The filtered node ' . $node . ' should not exist in $wp_admin_bar global after boot.' ); | ||
|
||
} | ||
|
||
/** | ||
* Apply the admin bar. | ||
*/ | ||
public function apply_admin_bar() { | ||
// Load file required to work with the admin bar. | ||
require_once ABSPATH . WPINC . '/class-wp-admin-bar.php'; | ||
|
||
// Set role. | ||
$this->acting_as( 'administrator' ); | ||
|
||
// Make admin bar go. | ||
global $wp_admin_bar; | ||
_wp_admin_bar_init(); | ||
do_action_ref_array( 'admin_bar_menu', [ &$wp_admin_bar ] ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound | ||
|
||
return $wp_admin_bar; | ||
} | ||
|
||
} |