Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Selected Admin Bar Links #52

Merged
merged 10 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ add_filter( 'alleyvate_load_disable_comments', '__return_false' );

Each feature's handle is listed below, along with a description of what it does.

### `clean_admin_bar`

This feature removes selected nodes from the admin bar.

### `disable_comments`

This feature disables WordPress comments entirely, including the ability to post, view, edit, list, count, modify settings for, or access URLs that are related to comments completely.

### `disable_dashboard_widgets`

This feature removes clutter from the dashboard. Its handle is `dashboard_widget_removal`.
This feature removes clutter from the dashboard.

### `disable_sticky_posts`

Expand Down
56 changes: 56 additions & 0 deletions src/alley/wp/alleyvate/features/class-clean-admin-bar.php
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 );
}
}
1 change: 1 addition & 0 deletions src/alley/wp/alleyvate/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function load(): void {
* @var Feature[] $features
*/
$features = [
'clean_admin_bar' => new Features\Clean_Admin_Bar(),
'disable_comments' => new Features\Disable_Comments(),
'disable_dashboard_widgets' => new Features\Disable_Dashboard_Widgets(),
'disable_sticky_posts' => new Features\Disable_Sticky_Posts(),
Expand Down
126 changes: 126 additions & 0 deletions tests/alley/wp/alleyvate/features/test-clean-admin-bar.php
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();
kevinfodness marked this conversation as resolved.
Show resolved Hide resolved
$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;
}

}
Loading