| 1 |
<?php |
| 2 |
/** |
| 3 |
* Disables wp-admin menu items that aren't supported in a headless environment. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Admin_Menus; |
| 9 |
|
| 10 |
use function WPE\FaustWP\Settings\is_themes_disabled; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
add_action( 'admin_menu', __NAMESPACE__ . '\\remove_admin_menu_pages', 1000 ); |
| 17 |
/** |
| 18 |
* Remove wp-admin menu items not needed in a headless environment. |
| 19 |
* |
| 20 |
* @global $submenu |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
function remove_admin_menu_pages() { |
| 25 |
/** |
| 26 |
* Global submenu data |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
global $submenu; |
| 31 |
|
| 32 |
if ( ! is_themes_disabled() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Remove Appearance > Themes. |
| 37 |
remove_submenu_page( 'themes.php', 'themes.php' ); |
| 38 |
|
| 39 |
// Remove Appearance > Theme Editor. |
| 40 |
remove_submenu_page( 'themes.php', 'theme-editor.php' ); |
| 41 |
|
| 42 |
// Remove Appearance > Widgets. |
| 43 |
remove_submenu_page( 'themes.php', 'widgets.php' ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Remove features that require the Customizer. |
| 47 |
* Loop through the themes.php submenu to remove features in Customizer |
| 48 |
* since there's no direct way to remove them via the submenu_slug. |
| 49 |
* Targeting 'hide-if-no-customize' removes all the features that |
| 50 |
* rely on the Customizer. Otherwise you must target each one |
| 51 |
* specifically by name (which might be a good idea?). |
| 52 |
*/ |
| 53 |
if ( isset( $submenu['themes.php'] ) ) { |
| 54 |
foreach ( $submenu['themes.php'] as $key => $item ) { |
| 55 |
if ( in_array( 'hide-if-no-customize', $item, true ) ) { |
| 56 |
unset( $submenu['themes.php'][ $key ] ); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
add_action( 'wp_before_admin_bar_render', __NAMESPACE__ . '\\remove_admin_bar_items' ); |
| 63 |
/** |
| 64 |
* Removes admin bar items not needed in a headless environment. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
function remove_admin_bar_items() { |
| 69 |
/** |
| 70 |
* WP Admin Bar global |
| 71 |
* |
| 72 |
* @var WP_Admin_Bar $wp_admin_bar |
| 73 |
*/ |
| 74 |
global $wp_admin_bar; |
| 75 |
|
| 76 |
if ( ! is_themes_disabled() ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$wp_admin_bar->remove_menu( 'customize' ); |
| 81 |
$wp_admin_bar->remove_node( 'themes' ); |
| 82 |
$wp_admin_bar->remove_node( 'widgets' ); |
| 83 |
} |
| 84 |
|
| 85 |
add_action( 'current_screen', __NAMESPACE__ . '\\prevent_admin_page_access' ); |
| 86 |
/** |
| 87 |
* Prevents access to named pages by redirecting to the admin root. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
function prevent_admin_page_access() { |
| 92 |
if ( ! is_themes_disabled() ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$screen = get_current_screen(); |
| 97 |
|
| 98 |
if ( is_object( $screen ) && 'themes' === $screen->id ) { |
| 99 |
wp_safe_redirect( admin_url() ); |
| 100 |
exit; |
| 101 |
} |
| 102 |
} |
| 103 |
|