class-activation.php
2 weeks ago
class-admin-menu-organizer.php
2 weeks ago
class-admin-menu-svg-icon-mask.php
2 weeks ago
class-auto-publish-posts-with-missed-schedule.php
2 weeks ago
class-avif-upload.php
2 weeks ago
class-captcha-protection.php
2 weeks ago
class-change-login-url.php
2 weeks ago
class-cleanup-admin-bar.php
2 weeks ago
class-common-methods.php
2 weeks ago
class-content-duplication.php
2 weeks ago
class-content-order.php
2 weeks ago
class-custom-admin-footer-text.php
2 weeks ago
class-custom-body-class.php
2 weeks ago
class-custom-css.php
2 weeks ago
class-custom-nav-menu-items-in-new-tab.php
2 weeks ago
class-deactivation.php
2 weeks ago
class-disable-author-archives.php
2 weeks ago
class-disable-comments.php
2 weeks ago
class-disable-dashboard-widgets.php
2 weeks ago
class-disable-embeds.php
2 weeks ago
class-disable-feeds.php
2 weeks ago
class-disable-gutenberg.php
2 weeks ago
class-disable-rest-api.php
2 weeks ago
class-disable-smaller-components.php
2 weeks ago
class-disable-updates.php
2 weeks ago
class-disable-user-account.php
2 weeks ago
class-disable-xml-rpc.php
2 weeks ago
class-display-system-summary.php
2 weeks ago
class-email-address-obfuscator.php
2 weeks ago
class-email-delivery.php
2 weeks ago
class-enhance-list-tables.php
2 weeks ago
class-external-permalinks.php
2 weeks ago
class-heartbeat-control.php
2 weeks ago
class-hide-admin-bar.php
2 weeks ago
class-hide-admin-notices.php
2 weeks ago
class-image-sizes-panel.php
2 weeks ago
class-image-upload-control.php
2 weeks ago
class-insert-head-body-footer-code.php
2 weeks ago
class-last-login-column.php
2 weeks ago
class-limit-login-attempts.php
2 weeks ago
class-login-id-type.php
2 weeks ago
class-login-logout-menu.php
2 weeks ago
class-maintenance-mode.php
2 weeks ago
class-manage-ads-appads-txt.php
2 weeks ago
class-manage-robots-txt.php
2 weeks ago
class-media-files-visibility-control.php
2 weeks ago
class-media-replacement.php
2 weeks ago
class-multiple-user-roles.php
2 weeks ago
class-navigation-menu-duplicator.php
2 weeks ago
class-obfuscate-author-slugs.php
2 weeks ago
class-open-external-links-in-new-tab.php
2 weeks ago
class-password-protection.php
2 weeks ago
class-redirect-after-login.php
2 weeks ago
class-redirect-after-logout.php
2 weeks ago
class-redirect-fourofour.php
2 weeks ago
class-registration-date-column.php
2 weeks ago
class-revisions-control.php
2 weeks ago
class-search-engines-visibility.php
2 weeks ago
class-settings-fields-render.php
2 weeks ago
class-settings-sanitization.php
2 weeks ago
class-settings-sections-fields.php
2 weeks ago
class-show-custom-taxonomy-filters.php
2 weeks ago
class-site-identity-on-login-page.php
2 weeks ago
class-svg-upload.php
2 weeks ago
class-third-party-compat.php
2 weeks ago
class-various-admin-ui-enhancements.php
2 weeks ago
class-view-admin-as-role.php
2 weeks ago
class-wider-admin-menu.php
2 weeks ago
class-wp-config-transformer.php
2 weeks ago
class-admin-menu-organizer.php
585 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Admin Menu Organizer module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Admin_Menu_Organizer { |
| 11 | /** |
| 12 | * Cached user objects for the current AMO save request. |
| 13 | * |
| 14 | * @since 8.8.6 |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | private $amo_save_user_object_cache__premium_only = array(); |
| 19 | |
| 20 | /** |
| 21 | * Cached user capability checks for the current AMO save request. |
| 22 | * |
| 23 | * @since 8.8.6 |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private $amo_save_user_capability_cache__premium_only = array(); |
| 28 | |
| 29 | /** |
| 30 | * Make the "Collapse Menu" toggler sticky at the bottom of the admin menu |
| 31 | * |
| 32 | * @since 8.2.3 |
| 33 | */ |
| 34 | public function make_collapse_menu_item_sticky() { |
| 35 | ?> |
| 36 | <style> |
| 37 | #adminmenu #collapse-menu { |
| 38 | position: sticky; |
| 39 | bottom: 0; |
| 40 | background: #1d2327; |
| 41 | z-index: 100; |
| 42 | box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.2); |
| 43 | border-top: 1px solid #3c4349; |
| 44 | } |
| 45 | #adminmenuwrap { |
| 46 | display: flex; |
| 47 | flex-direction: column; |
| 48 | height: 100%; |
| 49 | } |
| 50 | #adminmenu { |
| 51 | display: flex; |
| 52 | flex-direction: column; |
| 53 | flex: 1; |
| 54 | margin: 12px 0 0; |
| 55 | } |
| 56 | .folded #adminmenu #collapse-menu { |
| 57 | position: sticky; |
| 58 | bottom: 0; |
| 59 | background: #1d2327; |
| 60 | } |
| 61 | </style> |
| 62 | <?php |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Add Admin Menu item under Settings menu |
| 67 | * |
| 68 | * @since 7.8.5 |
| 69 | */ |
| 70 | public function add_menu_item() { |
| 71 | add_submenu_page( |
| 72 | 'options-general.php', |
| 73 | // Parent page/menu |
| 74 | __( 'Admin Menu Settings', 'admin-site-enhancements' ), |
| 75 | // Browser tab/window title |
| 76 | __( 'Admin Menu', 'admin-site-enhancements' ), |
| 77 | // Sube menu title |
| 78 | 'manage_options', |
| 79 | // Minimal user capabililty |
| 80 | 'admin-menu-organizer', |
| 81 | // Page slug. Shows up in URL. |
| 82 | array($this, 'add_admin_menu_settings_page') |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create settings page for Admin Menu |
| 88 | * |
| 89 | * @since 7.8.5 |
| 90 | */ |
| 91 | public function add_admin_menu_settings_page() { |
| 92 | $render_field = new Settings_Fields_Render(); |
| 93 | ?> |
| 94 | <div class="wrap admin-menu-organizer"> |
| 95 | <h1 class="wp-heading-inline"><?php |
| 96 | echo __( 'Admin Menu Organizer', 'admin-site-enhancements' ); |
| 97 | ?></h1> |
| 98 | <div class="admin-menu-organizer-main"> |
| 99 | <div class="admin-menu-sortables-wrapper"> |
| 100 | <?php |
| 101 | $render_field->render_sortable_menu(); |
| 102 | ?> |
| 103 | </div> |
| 104 | <div class="admin-menu-actions"> |
| 105 | <button id="amo-save-changes" class="button button-primary button-large"><?php |
| 106 | echo __( 'Save Changes', 'admin-site-enhancements' ); |
| 107 | ?></button> |
| 108 | <div class="asenha-saving-changes" style="display:none;"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="#2271b1" d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z" opacity=".25"/><path fill="#2271b1" d="M12,4a8,8,0,0,1,7.89,6.7A1.53,1.53,0,0,0,21.38,12h0a1.5,1.5,0,0,0,1.48-1.75,11,11,0,0,0-21.72,0A1.5,1.5,0,0,0,2.62,12h0a1.53,1.53,0,0,0,1.49-1.3A8,8,0,0,1,12,4Z"><animateTransform attributeName="transform" dur="0.75s" repeatCount="indefinite" type="rotate" values="0 12 12;360 12 12"/></path></svg></div> |
| 109 | <div class="asenha-changes-saved" style="display:none;"><svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24"><path fill="seagreen" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10s10-4.48 10-10S17.52 2 12 2zM9.29 16.29L5.7 12.7a.996.996 0 1 1 1.41-1.41L10 14.17l6.88-6.88a.996.996 0 1 1 1.41 1.41l-7.59 7.59a.996.996 0 0 1-1.41 0z"/></svg></div> |
| 110 | </div> |
| 111 | </div> |
| 112 | </div> |
| 113 | <?php |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Render custom menu order |
| 118 | * |
| 119 | * @param $menu_order array an ordered array of menu items |
| 120 | * @link https://developer.wordpress.org/reference/hooks/menu_order/ |
| 121 | * @since 2.0.0 |
| 122 | */ |
| 123 | public function render_custom_menu_order( $menu_order ) { |
| 124 | global $menu; |
| 125 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 126 | $options = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 127 | // Get deleted built-in separators |
| 128 | $deleted_separators = ( isset( $options['custom_menu_deleted_separators'] ) ? $options['custom_menu_deleted_separators'] : '' ); |
| 129 | $deleted_separators_array = array(); |
| 130 | if ( is_string( $deleted_separators ) && !empty( $deleted_separators ) ) { |
| 131 | $deleted_separators_array = json_decode( $deleted_separators, true ); |
| 132 | if ( !is_array( $deleted_separators_array ) ) { |
| 133 | $deleted_separators_array = array(); |
| 134 | } |
| 135 | } |
| 136 | $common_methods = new Common_Methods(); |
| 137 | // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values. |
| 138 | $current_menu_order = array(); |
| 139 | foreach ( $menu as $menu_key => $menu_info ) { |
| 140 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 141 | $menu_item_id = $menu_info[2]; |
| 142 | // Skip deleted built-in separators; Pro distinguishes ASE additional-separator rows. |
| 143 | $skip_deleted_separator = in_array( $menu_item_id, $deleted_separators_array, true ); |
| 144 | if ( $skip_deleted_separator ) { |
| 145 | continue; |
| 146 | } |
| 147 | } else { |
| 148 | $menu_item_id = $menu_info[5]; |
| 149 | } |
| 150 | $current_menu_order[] = array($menu_item_id, $menu_info[2]); |
| 151 | } |
| 152 | // Get custom menu order |
| 153 | $custom_menu_order = $options['custom_menu_order']; |
| 154 | // comma separated |
| 155 | $custom_menu_order = explode( ",", $custom_menu_order ); |
| 156 | // array of menu ID, e.g. menu-dashboard |
| 157 | // Return menu order for rendering |
| 158 | $rendered_menu_order = array(); |
| 159 | // Render menu based on items saved in custom menu order |
| 160 | foreach ( $custom_menu_order as $custom_menu_item_id ) { |
| 161 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 162 | // Standard comparison using menu ID from position [5] |
| 163 | if ( $custom_menu_item_id == $current_menu_item[0] ) { |
| 164 | $rendered_menu_order[] = $current_menu_item[1]; |
| 165 | } elseif ( $custom_menu_item_id == $current_menu_item[1] ) { |
| 166 | $rendered_menu_order[] = $current_menu_item[1]; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item |
| 171 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 172 | // Check both menu ID and slug to prevent duplicate entries for custom menus |
| 173 | if ( !in_array( $current_menu_item[0], $custom_menu_order ) && !in_array( $current_menu_item[1], $custom_menu_order ) ) { |
| 174 | $rendered_menu_order[] = $current_menu_item[1]; |
| 175 | } |
| 176 | } |
| 177 | return $rendered_menu_order; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Apply custom menu item titles |
| 182 | * |
| 183 | * @since 2.9.0 |
| 184 | */ |
| 185 | public function apply_custom_menu_item_titles() { |
| 186 | global $menu; |
| 187 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 188 | $options = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 189 | // Get custom menu item titles |
| 190 | $custom_menu_titles = $options['custom_menu_titles']; |
| 191 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 192 | foreach ( $menu as $menu_key => $menu_info ) { |
| 193 | // Determine menu item ID |
| 194 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 195 | $menu_item_id = $menu_info[2]; |
| 196 | } else { |
| 197 | // Check if this is a custom menu item (for pro version) |
| 198 | $is_custom_menu = false; |
| 199 | // For custom menus, use slug; for regular menus, use CSS ID |
| 200 | if ( $is_custom_menu ) { |
| 201 | $menu_item_id = $menu_info[2]; |
| 202 | // Use slug for custom menus |
| 203 | } else { |
| 204 | $menu_item_id = $menu_info[5]; |
| 205 | // Use CSS ID for regular menus |
| 206 | } |
| 207 | } |
| 208 | // Get defaul/custom menu item title |
| 209 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 210 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 211 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 212 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 213 | $menu_item_title = $custom_menu_title[1]; |
| 214 | // e.g. Code Snippets |
| 215 | break; |
| 216 | // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 217 | } else { |
| 218 | $menu_item_title = $menu_info[0]; |
| 219 | } |
| 220 | } |
| 221 | $menu[$menu_key][0] = $menu_item_title; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Apply custom menu item titles |
| 227 | * |
| 228 | * @since 7.9.7 |
| 229 | */ |
| 230 | public function apply_custom_title_for_posts_menu() { |
| 231 | global $wp_post_types; |
| 232 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 233 | $admin_menu_options = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 234 | // $admin_menu_organizer = new ASENHA\Classes\Admin_Menu_Organizer(); |
| 235 | // For 'Posts' menu, if the title has been changed, try changing the labels for it everywhere |
| 236 | $custom_menu_titles = explode( ',', $admin_menu_options['custom_menu_titles'] ); |
| 237 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 238 | if ( false !== strpos( $custom_menu_title, 'menu-posts__' ) ) { |
| 239 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 240 | $posts_custom_title = $custom_menu_title[1]; |
| 241 | $posts_default_title = __( 'Posts', 'admin-site-enhancements' ); |
| 242 | if ( is_array( $wp_post_types ) ) { |
| 243 | if ( isset( $wp_post_types['post'] ) && property_exists( $wp_post_types['post'], 'label' ) ) { |
| 244 | $posts_default_title = $wp_post_types['post']->label; |
| 245 | } |
| 246 | } |
| 247 | if ( $posts_default_title != $posts_custom_title ) { |
| 248 | add_filter( 'post_type_labels_post', [$this, 'change_post_labels'] ); |
| 249 | add_action( 'init', [$this, 'change_post_object_label'] ); |
| 250 | add_action( 'admin_menu', [$this, 'change_post_menu_label'], PHP_INT_MAX ); |
| 251 | add_action( 'admin_bar_menu', [$this, 'change_wp_admin_bar'], 80 ); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get custom title for 'Posts' menu item |
| 259 | * |
| 260 | * @since 6.9.13 |
| 261 | */ |
| 262 | public function get_posts_custom_title() { |
| 263 | $post_object = get_post_type_object( 'post' ); |
| 264 | // object |
| 265 | $posts_default_title = ''; |
| 266 | if ( is_object( $post_object ) ) { |
| 267 | if ( property_exists( $post_object, 'label' ) ) { |
| 268 | $posts_default_title = $post_object->label; |
| 269 | } else { |
| 270 | $posts_default_title = $post_object->labels->name; |
| 271 | } |
| 272 | } |
| 273 | $posts_custom_title = $posts_default_title; |
| 274 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 275 | $options = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 276 | $custom_menu_titles = ( isset( $options['custom_menu_titles'] ) ? explode( ',', $options['custom_menu_titles'] ) : array() ); |
| 277 | if ( !empty( $custom_menu_titles ) ) { |
| 278 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 279 | if ( false !== strpos( $custom_menu_title, 'menu-posts__' ) ) { |
| 280 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 281 | $posts_custom_title = $custom_menu_title[1]; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | return $posts_custom_title; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * For 'Posts', apply custom label |
| 290 | * |
| 291 | * @link https://developer.wordpress.org/reference/hooks/post_type_labels_post_type/ |
| 292 | * @since 6.9.13 |
| 293 | */ |
| 294 | public function change_post_labels( $labels ) { |
| 295 | $post_object = get_post_type_object( 'post' ); |
| 296 | // object |
| 297 | $posts_default_title_plural = ''; |
| 298 | if ( is_object( $post_object ) ) { |
| 299 | if ( property_exists( $post_object, 'label' ) ) { |
| 300 | $posts_default_title_plural = $post_object->label; |
| 301 | } else { |
| 302 | $posts_default_title_plural = $post_object->labels->name; |
| 303 | } |
| 304 | $posts_default_title_singular = $post_object->labels->singular_name; |
| 305 | $posts_custom_title = $this->get_posts_custom_title(); |
| 306 | foreach ( $labels as $key => $label ) { |
| 307 | if ( null === $label ) { |
| 308 | continue; |
| 309 | } |
| 310 | $labels->{$key} = str_replace( [$posts_default_title_plural, $posts_default_title_singular], $posts_custom_title, $label ); |
| 311 | } |
| 312 | } |
| 313 | return $labels; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * For 'Posts', apply custom label in post object |
| 318 | * |
| 319 | * @since 6.9.12 |
| 320 | */ |
| 321 | public function change_post_object_label() { |
| 322 | global $wp_post_types; |
| 323 | $posts_custom_title = $this->get_posts_custom_title(); |
| 324 | $labels =& $wp_post_types['post']->labels; |
| 325 | $labels->name = $posts_custom_title; |
| 326 | $labels->singular_name = $posts_custom_title; |
| 327 | $labels->all_items = sprintf( |
| 328 | /* translators: %s is post type or taxonomy label */ |
| 329 | __( 'All %s', 'admin-site-enhancements' ), |
| 330 | $posts_custom_title |
| 331 | ); |
| 332 | $labels->add_new = __( 'Add New', 'admin-site-enhancements' ); |
| 333 | $labels->add_new_item = __( 'Add New', 'admin-site-enhancements' ); |
| 334 | $labels->edit_item = __( 'Edit', 'admin-site-enhancements' ); |
| 335 | $labels->new_item = $posts_custom_title; |
| 336 | $labels->view_item = __( 'View', 'admin-site-enhancements' ); |
| 337 | $labels->search_items = sprintf( |
| 338 | /* translators: %s is post type or taxonomy label */ |
| 339 | __( 'Search %s', 'admin-site-enhancements' ), |
| 340 | $posts_custom_title |
| 341 | ); |
| 342 | $labels->not_found = sprintf( |
| 343 | /* translators: %s is the post type label */ |
| 344 | __( 'No %s found', 'admin-site-enhancements' ), |
| 345 | strtolower( $posts_custom_title ) |
| 346 | ); |
| 347 | $labels->not_found_in_trash = sprintf( |
| 348 | /* translators: %s is the post type label */ |
| 349 | __( 'No %s found in Trash', 'admin-site-enhancements' ), |
| 350 | strtolower( $posts_custom_title ) |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * For 'Posts', apply custom label in menu and submenu |
| 356 | * |
| 357 | * @since 6.9.12 |
| 358 | */ |
| 359 | public function change_post_menu_label() { |
| 360 | global $submenu; |
| 361 | $posts_custom_title = $this->get_posts_custom_title(); |
| 362 | if ( !empty( $posts_custom_title ) ) { |
| 363 | $submenu['edit.php'][5][0] = sprintf( |
| 364 | /* translators: %s is post type or taxonomy label */ |
| 365 | __( 'All %s', 'admin-site-enhancements' ), |
| 366 | $posts_custom_title |
| 367 | ); |
| 368 | } else { |
| 369 | $submenu['edit.php'][5][0] = sprintf( |
| 370 | /* translators: %s is post type or taxonomy label */ |
| 371 | __( 'All %s', 'admin-site-enhancements' ), |
| 372 | $posts_default_title |
| 373 | ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * For 'Posts', apply custom label in admin bar |
| 379 | * |
| 380 | * @since 6.9.12 |
| 381 | */ |
| 382 | public function change_wp_admin_bar( $wp_admin_bar ) { |
| 383 | $posts_custom_title = $this->get_posts_custom_title(); |
| 384 | $new_post_node = $wp_admin_bar->get_node( 'new-post' ); |
| 385 | if ( $new_post_node ) { |
| 386 | $new_post_node->title = $posts_custom_title; |
| 387 | $wp_admin_bar->add_node( $new_post_node ); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Hide parent menu items by adding class(es) to hide them |
| 393 | * |
| 394 | * @since 2.0.0 |
| 395 | */ |
| 396 | public function hide_menu_items() { |
| 397 | global $menu; |
| 398 | if ( !is_array( $menu ) ) { |
| 399 | return; |
| 400 | } |
| 401 | $common_methods = new Common_Methods(); |
| 402 | $menu_always_hidden = array(); |
| 403 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 404 | // indexed array |
| 405 | $yoast_alias_slugs_to_remove = array(); |
| 406 | foreach ( $menu as $menu_key => $menu_info ) { |
| 407 | $menu_slug = ( isset( $menu_info[2] ) ? $menu_info[2] : '' ); |
| 408 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 409 | $menu_item_id = $menu_info[2]; |
| 410 | } else { |
| 411 | $menu_item_id = ( isset( $menu_info[5] ) ? $menu_info[5] : '' ); |
| 412 | } |
| 413 | // Append 'hidden' class to hide menu item until toggled (match CSS ID or Yoast slug aliases). |
| 414 | if ( in_array( $menu_item_id, $menu_hidden_by_toggle, true ) || '' !== $menu_slug && in_array( $menu_slug, $menu_hidden_by_toggle, true ) ) { |
| 415 | if ( false === strpos( $menu[$menu_key][4], 'asenha_hidden_menu' ) ) { |
| 416 | $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu'; |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Add toggle to show hidden menu items |
| 424 | * |
| 425 | * @since 2.0.0 |
| 426 | */ |
| 427 | public function add_hidden_menu_toggle() { |
| 428 | global $current_user; |
| 429 | // Get menu items hidden by toggle |
| 430 | $common_methods = new Common_Methods(); |
| 431 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 432 | $submenu_hidden_by_toggle = array(); |
| 433 | // Get user capabilities the "Show All/Less" toggle should be shown for |
| 434 | $user_capabilities_to_show_menu_toggle_for = $common_methods->get_user_capabilities_to_show_menu_toggle_for(); |
| 435 | // Get current user's capabilities from the user's role(s) |
| 436 | $current_user_capabilities = ''; |
| 437 | $current_user_roles = $current_user->roles; |
| 438 | // indexed array of role slugs |
| 439 | foreach ( $current_user_roles as $current_user_role ) { |
| 440 | $current_user_role_capabilities = get_role( $current_user_role )->capabilities; |
| 441 | if ( is_array( $current_user_role_capabilities ) ) { |
| 442 | $current_user_role_capabilities = array_keys( $current_user_role_capabilities ); |
| 443 | // indexed array |
| 444 | $current_user_role_capabilities = implode( ",", $current_user_role_capabilities ); |
| 445 | $current_user_capabilities .= ',' . $current_user_role_capabilities; |
| 446 | } |
| 447 | } |
| 448 | // Maybe show "Show All/Less" toggle |
| 449 | $show_toggle_menu = false; |
| 450 | if ( !empty( $current_user_capabilities ) ) { |
| 451 | $current_user_capabilities = array_unique( explode( ",", $current_user_capabilities ) ); |
| 452 | foreach ( $user_capabilities_to_show_menu_toggle_for as $user_capability_to_show_menu_toggle_for ) { |
| 453 | if ( in_array( $user_capability_to_show_menu_toggle_for, $current_user_capabilities ) ) { |
| 454 | $show_toggle_menu = true; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | if ( (!empty( $menu_hidden_by_toggle ) || !empty( $submenu_hidden_by_toggle )) && $show_toggle_menu ) { |
| 460 | add_menu_page( |
| 461 | __( 'Show All', 'admin-site-enhancements' ), |
| 462 | __( 'Show All', 'admin-site-enhancements' ), |
| 463 | 'read', |
| 464 | 'asenha_show_hidden_menu', |
| 465 | function () { |
| 466 | return false; |
| 467 | }, |
| 468 | "dashicons-arrow-down-alt2", |
| 469 | 300 |
| 470 | ); |
| 471 | add_menu_page( |
| 472 | __( 'Show Less', 'admin-site-enhancements' ), |
| 473 | __( 'Show Less', 'admin-site-enhancements' ), |
| 474 | 'read', |
| 475 | 'asenha_hide_hidden_menu', |
| 476 | function () { |
| 477 | return false; |
| 478 | }, |
| 479 | "dashicons-arrow-up-alt2", |
| 480 | 301 |
| 481 | ); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Script to toggle hidden menu itesm |
| 487 | * |
| 488 | * @since 2.0.0 |
| 489 | */ |
| 490 | public function enqueue_toggle_hidden_menu_script() { |
| 491 | // Get menu items hidden by toggle |
| 492 | $common_methods = new Common_Methods(); |
| 493 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 494 | $submenu_hidden_by_toggle = array(); |
| 495 | if ( !empty( $menu_hidden_by_toggle ) || !empty( $submenu_hidden_by_toggle ) ) { |
| 496 | // Script to set behaviour and actions of the sortable menu |
| 497 | wp_enqueue_script( |
| 498 | 'asenha-toggle-hidden-menu', |
| 499 | ASENHA_URL . 'assets/js/toggle-hidden-menu.js', |
| 500 | array(), |
| 501 | ASENHA_VERSION, |
| 502 | false |
| 503 | ); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Save admin menu via AJAX |
| 509 | * |
| 510 | * @since 6.3.1 |
| 511 | */ |
| 512 | public function save_admin_menu() { |
| 513 | if ( isset( $_REQUEST ) ) { |
| 514 | if ( check_ajax_referer( 'save-menu-nonce', 'nonce', false ) ) { |
| 515 | if ( !current_user_can( 'manage_options' ) ) { |
| 516 | wp_send_json_error( array( |
| 517 | 'message' => __( 'You do not have permission to perform this action.', 'admin-site-enhancements' ), |
| 518 | ) ); |
| 519 | } |
| 520 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 521 | $options = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 522 | $options['custom_menu_order'] = ( isset( $_REQUEST['custom_menu_order'] ) ? wp_unslash( $_REQUEST['custom_menu_order'] ) : $options['custom_menu_order'] ); |
| 523 | $options['custom_menu_titles'] = ( isset( $_REQUEST['custom_menu_titles'] ) ? wp_unslash( $_REQUEST['custom_menu_titles'] ) : $options['custom_menu_titles'] ); |
| 524 | $options['custom_menu_hidden'] = ( isset( $_REQUEST['custom_menu_hidden'] ) ? wp_unslash( $_REQUEST['custom_menu_hidden'] ) : $options['custom_menu_hidden'] ); |
| 525 | $options_extra['admin_menu'] = $options; |
| 526 | // vi( $options_extra, '', 'save menu' ); |
| 527 | $updated = update_option( ASENHA_SLUG_U . '_extra', $options_extra, true ); |
| 528 | wp_send_json_success( array( |
| 529 | 'status' => ( $updated ? 'success' : 'unchanged' ), |
| 530 | 'updated' => (bool) $updated, |
| 531 | ) ); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Reset admin menu via AJAX |
| 538 | * |
| 539 | * Free resets order, titles, and hide keys. Pro also resets Pro-only organizer keys. |
| 540 | * |
| 541 | * @since 6.3.1 |
| 542 | */ |
| 543 | public function reset_admin_menu() { |
| 544 | if ( isset( $_REQUEST ) ) { |
| 545 | if ( check_ajax_referer( 'reset-menu-nonce', 'nonce', false ) ) { |
| 546 | if ( !current_user_can( 'manage_options' ) ) { |
| 547 | echo wp_json_encode( array( |
| 548 | 'status' => 'failed', |
| 549 | ) ); |
| 550 | return; |
| 551 | } |
| 552 | $options_extra = get_option( ASENHA_SLUG_U . '_extra', array() ); |
| 553 | $admin_menu = ( isset( $options_extra['admin_menu'] ) ? $options_extra['admin_menu'] : array() ); |
| 554 | // vi( $admin_menu, '', 'menu before reset' ); |
| 555 | if ( !empty( $admin_menu ) ) { |
| 556 | // Free (and Pro) organizer keys. |
| 557 | unset($admin_menu['custom_menu_order']); |
| 558 | unset($admin_menu['custom_menu_titles']); |
| 559 | unset($admin_menu['custom_menu_hidden']); |
| 560 | // vi( $admin_menu, '', 'menu after reset' ); |
| 561 | $options_extra['admin_menu'] = $admin_menu; |
| 562 | // vi( $options_extra, '', 'reset menu' ); |
| 563 | $updated = update_option( ASENHA_SLUG_U . '_extra', $options_extra, true ); |
| 564 | // vi( $updated ); |
| 565 | if ( $updated ) { |
| 566 | $response = array( |
| 567 | 'status' => 'success', |
| 568 | ); |
| 569 | } else { |
| 570 | $response = array( |
| 571 | 'status' => 'failed', |
| 572 | ); |
| 573 | } |
| 574 | } else { |
| 575 | $response = array( |
| 576 | 'status' => 'success', |
| 577 | ); |
| 578 | } |
| 579 | echo wp_json_encode( $response ); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | } |
| 585 |