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