class-activation.php
1 year ago
class-admin-menu-organizer.php
1 year ago
class-auto-publish-posts-with-missed-schedule.php
1 year ago
class-avif-upload.php
1 year ago
class-captcha-protection.php
1 year ago
class-change-login-url.php
1 year ago
class-cleanup-admin-bar.php
1 year ago
class-common-methods.php
1 year ago
class-content-duplication.php
1 year ago
class-content-order.php
1 year ago
class-custom-admin-footer-text.php
1 year ago
class-custom-body-class.php
1 year ago
class-custom-css.php
1 year ago
class-custom-nav-menu-items-in-new-tab.php
1 year ago
class-deactivation.php
1 year ago
class-disable-comments.php
1 year ago
class-disable-dashboard-widgets.php
1 year ago
class-disable-feeds.php
1 year ago
class-disable-gutenberg.php
1 year ago
class-disable-rest-api.php
1 year ago
class-disable-smaller-components.php
1 year ago
class-disable-updates.php
1 year ago
class-disable-xml-rpc.php
1 year ago
class-display-system-summary.php
1 year ago
class-email-address-obfuscator.php
1 year ago
class-email-delivery.php
1 year ago
class-enhance-list-tables.php
1 year ago
class-external-permalinks.php
1 year ago
class-heartbeat-control.php
1 year ago
class-hide-admin-bar.php
1 year ago
class-hide-admin-notices.php
1 year ago
class-image-sizes-panel.php
1 year ago
class-image-upload-control.php
1 year ago
class-insert-head-body-footer-code.php
1 year ago
class-last-login-column.php
1 year ago
class-limit-login-attempts.php
1 year ago
class-login-id-type.php
1 year ago
class-login-logout-menu.php
1 year ago
class-maintenance-mode.php
1 year ago
class-manage-ads-appads-txt.php
1 year ago
class-manage-robots-txt.php
1 year ago
class-media-replacement.php
1 year ago
class-multiple-user-roles.php
1 year ago
class-obfuscate-author-slugs.php
1 year ago
class-open-external-links-in-new-tab.php
1 year ago
class-password-protection.php
1 year ago
class-redirect-after-login.php
1 year ago
class-redirect-after-logout.php
1 year ago
class-redirect-fourofour.php
1 year ago
class-registration-date-column.php
1 year ago
class-revisions-control.php
1 year ago
class-search-engines-visibility.php
1 year ago
class-settings-fields-render.php
1 year ago
class-settings-sanitization.php
1 year ago
class-settings-sections-fields.php
1 year ago
class-show-custom-taxonomy-filters.php
1 year ago
class-site-identity-on-login-page.php
1 year ago
class-svg-upload.php
1 year ago
class-various-admin-ui-enhancements.php
1 year ago
class-view-admin-as-role.php
1 year ago
class-wider-admin-menu.php
1 year ago
class-wp-config-transformer.php
1 year ago
class-admin-menu-organizer.php
322 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 | * Render custom menu order |
| 13 | * |
| 14 | * @param $menu_order array an ordered array of menu items |
| 15 | * @link https://developer.wordpress.org/reference/hooks/menu_order/ |
| 16 | * @since 2.0.0 |
| 17 | */ |
| 18 | public function render_custom_menu_order( $menu_order ) { |
| 19 | global $menu; |
| 20 | $options = get_option( ASENHA_SLUG_U ); |
| 21 | // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values. |
| 22 | $current_menu_order = array(); |
| 23 | foreach ( $menu as $menu_key => $menu_info ) { |
| 24 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 25 | $menu_item_id = $menu_info[2]; |
| 26 | } else { |
| 27 | $menu_item_id = $menu_info[5]; |
| 28 | } |
| 29 | $current_menu_order[] = array($menu_item_id, $menu_info[2]); |
| 30 | } |
| 31 | // Get custom menu order |
| 32 | $custom_menu_order = $options['custom_menu_order']; |
| 33 | // comma separated |
| 34 | $custom_menu_order = explode( ",", $custom_menu_order ); |
| 35 | // array of menu ID, e.g. menu-dashboard |
| 36 | // Return menu order for rendering |
| 37 | $rendered_menu_order = array(); |
| 38 | // Render menu based on items saved in custom menu order |
| 39 | foreach ( $custom_menu_order as $custom_menu_item_id ) { |
| 40 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 41 | if ( $custom_menu_item_id == $current_menu_item[0] ) { |
| 42 | $rendered_menu_order[] = $current_menu_item[1]; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item |
| 47 | foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) { |
| 48 | if ( !in_array( $current_menu_item[0], $custom_menu_order ) ) { |
| 49 | $rendered_menu_order[] = $current_menu_item[1]; |
| 50 | } |
| 51 | } |
| 52 | return $rendered_menu_order; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Apply custom menu item titles |
| 57 | * |
| 58 | * @since 2.9.0 |
| 59 | */ |
| 60 | public function apply_custom_menu_item_titles() { |
| 61 | global $menu; |
| 62 | $options = get_option( ASENHA_SLUG_U ); |
| 63 | // Get custom menu item titles |
| 64 | $custom_menu_titles = $options['custom_menu_titles']; |
| 65 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 66 | foreach ( $menu as $menu_key => $menu_info ) { |
| 67 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 68 | $menu_item_id = $menu_info[2]; |
| 69 | } else { |
| 70 | $menu_item_id = $menu_info[5]; |
| 71 | } |
| 72 | // Get defaul/custom menu item title |
| 73 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 74 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 75 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 76 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 77 | $menu_item_title = $custom_menu_title[1]; |
| 78 | // e.g. Code Snippets |
| 79 | break; |
| 80 | // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 81 | } else { |
| 82 | $menu_item_title = $menu_info[0]; |
| 83 | } |
| 84 | } |
| 85 | $menu[$menu_key][0] = $menu_item_title; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get custom title for 'Posts' menu item |
| 91 | * |
| 92 | * @since 6.9.13 |
| 93 | */ |
| 94 | public function get_posts_custom_title() { |
| 95 | $post_object = get_post_type_object( 'post' ); |
| 96 | // object |
| 97 | $posts_default_title = ''; |
| 98 | if ( is_object( $post_object ) ) { |
| 99 | if ( property_exists( $post_object, 'label' ) ) { |
| 100 | $posts_default_title = $post_object->label; |
| 101 | } else { |
| 102 | $posts_default_title = $post_object->labels->name; |
| 103 | } |
| 104 | } |
| 105 | $posts_custom_title = $posts_default_title; |
| 106 | $options = get_option( ASENHA_SLUG_U ); |
| 107 | $custom_menu_titles = ( isset( $options['custom_menu_titles'] ) ? explode( ',', $options['custom_menu_titles'] ) : array() ); |
| 108 | if ( !empty( $custom_menu_titles ) ) { |
| 109 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 110 | if ( false !== strpos( $custom_menu_title, 'menu-posts__' ) ) { |
| 111 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 112 | $posts_custom_title = $custom_menu_title[1]; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return $posts_custom_title; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * For 'Posts', apply custom label |
| 121 | * |
| 122 | * @link https://developer.wordpress.org/reference/hooks/post_type_labels_post_type/ |
| 123 | * @since 6.9.13 |
| 124 | */ |
| 125 | public function change_post_labels( $labels ) { |
| 126 | $post_object = get_post_type_object( 'post' ); |
| 127 | // object |
| 128 | $posts_default_title_plural = ''; |
| 129 | if ( is_object( $post_object ) ) { |
| 130 | if ( property_exists( $post_object, 'label' ) ) { |
| 131 | $posts_default_title_plural = $post_object->label; |
| 132 | } else { |
| 133 | $posts_default_title_plural = $post_object->labels->name; |
| 134 | } |
| 135 | $posts_default_title_singular = $post_object->labels->singular_name; |
| 136 | $posts_custom_title = $this->get_posts_custom_title(); |
| 137 | foreach ( $labels as $key => $label ) { |
| 138 | if ( null === $label ) { |
| 139 | continue; |
| 140 | } |
| 141 | $labels->{$key} = str_replace( [$posts_default_title_plural, $posts_default_title_singular], $posts_custom_title, $label ); |
| 142 | } |
| 143 | } |
| 144 | return $labels; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * For 'Posts', apply custom label in post object |
| 149 | * |
| 150 | * @since 6.9.12 |
| 151 | */ |
| 152 | public function change_post_object_label() { |
| 153 | global $wp_post_types; |
| 154 | $posts_custom_title = $this->get_posts_custom_title(); |
| 155 | $labels =& $wp_post_types['post']->labels; |
| 156 | $labels->name = $posts_custom_title; |
| 157 | $labels->singular_name = $posts_custom_title; |
| 158 | $labels->add_new = __( 'Add New', 'admin-site-enhancements' ); |
| 159 | $labels->add_new_item = __( 'Add New', 'admin-site-enhancements' ); |
| 160 | $labels->edit_item = __( 'Edit', 'admin-site-enhancements' ); |
| 161 | $labels->new_item = $posts_custom_title; |
| 162 | $labels->view_item = __( 'View', 'admin-site-enhancements' ); |
| 163 | $labels->search_items = sprintf( |
| 164 | /* translators: %s is the post type label */ |
| 165 | 'Search %s', |
| 166 | $posts_custom_title |
| 167 | ); |
| 168 | $labels->not_found = sprintf( |
| 169 | /* translators: %s is the post type label */ |
| 170 | 'No %s found', |
| 171 | strtolower( $posts_custom_title ) |
| 172 | ); |
| 173 | $labels->not_found_in_trash = sprintf( |
| 174 | /* translators: %s is the post type label */ |
| 175 | 'No %s found in Trash', |
| 176 | strtolower( $posts_custom_title ) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * For 'Posts', apply custom label in menu and submenu |
| 182 | * |
| 183 | * @since 6.9.12 |
| 184 | */ |
| 185 | public function change_post_menu_label() { |
| 186 | global $submenu; |
| 187 | $posts_custom_title = $this->get_posts_custom_title(); |
| 188 | if ( !empty( $posts_custom_title ) ) { |
| 189 | $submenu['edit.php'][5][0] = sprintf( |
| 190 | /* translators: %s is the post type label */ |
| 191 | 'All %s', |
| 192 | $posts_custom_title |
| 193 | ); |
| 194 | } else { |
| 195 | $submenu['edit.php'][5][0] = sprintf( |
| 196 | /* translators: %s is the post type label */ |
| 197 | 'All %s', |
| 198 | $posts_default_title |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * For 'Posts', apply custom label in admin bar |
| 205 | * |
| 206 | * @since 6.9.12 |
| 207 | */ |
| 208 | public function change_wp_admin_bar( $wp_admin_bar ) { |
| 209 | $posts_custom_title = $this->get_posts_custom_title(); |
| 210 | $new_post_node = $wp_admin_bar->get_node( 'new-post' ); |
| 211 | if ( $new_post_node ) { |
| 212 | $new_post_node->title = $posts_custom_title; |
| 213 | $wp_admin_bar->add_node( $new_post_node ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Hide parent menu items by adding class(es) to hide them |
| 219 | * |
| 220 | * @since 2.0.0 |
| 221 | */ |
| 222 | public function hide_menu_items() { |
| 223 | global $menu; |
| 224 | $common_methods = new Common_Methods(); |
| 225 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 226 | // indexed array |
| 227 | foreach ( $menu as $menu_key => $menu_info ) { |
| 228 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 229 | $menu_item_id = $menu_info[2]; |
| 230 | } else { |
| 231 | $menu_item_id = $menu_info[5]; |
| 232 | } |
| 233 | // Append 'hidden' class to hide menu item until toggled |
| 234 | if ( in_array( $menu_item_id, $menu_hidden_by_toggle ) ) { |
| 235 | $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu'; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Add toggle to show hidden menu items |
| 242 | * |
| 243 | * @since 2.0.0 |
| 244 | */ |
| 245 | public function add_hidden_menu_toggle() { |
| 246 | global $current_user; |
| 247 | // Get menu items hidden by toggle |
| 248 | $common_methods = new Common_Methods(); |
| 249 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 250 | $submenu_hidden_by_toggle = array(); |
| 251 | // Get user capabilities the "Show All/Less" toggle should be shown for |
| 252 | $user_capabilities_to_show_menu_toggle_for = $common_methods->get_user_capabilities_to_show_menu_toggle_for(); |
| 253 | // Get current user's capabilities from the user's role(s) |
| 254 | $current_user_capabilities = ''; |
| 255 | $current_user_roles = $current_user->roles; |
| 256 | // indexed array of role slugs |
| 257 | foreach ( $current_user_roles as $current_user_role ) { |
| 258 | $current_user_role_capabilities = get_role( $current_user_role )->capabilities; |
| 259 | $current_user_role_capabilities = array_keys( $current_user_role_capabilities ); |
| 260 | // indexed array |
| 261 | $current_user_role_capabilities = implode( ",", $current_user_role_capabilities ); |
| 262 | $current_user_capabilities .= $current_user_role_capabilities; |
| 263 | } |
| 264 | $current_user_capabilities = array_unique( explode( ",", $current_user_capabilities ) ); |
| 265 | // Maybe show "Show All/Less" toggle |
| 266 | $show_toggle_menu = false; |
| 267 | foreach ( $user_capabilities_to_show_menu_toggle_for as $user_capability_to_show_menu_toggle_for ) { |
| 268 | if ( in_array( $user_capability_to_show_menu_toggle_for, $current_user_capabilities ) ) { |
| 269 | $show_toggle_menu = true; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | if ( (!empty( $menu_hidden_by_toggle ) || !empty( $submenu_hidden_by_toggle )) && $show_toggle_menu ) { |
| 274 | add_menu_page( |
| 275 | __( 'Show All', 'admin-site-enhancements' ), |
| 276 | __( 'Show All', 'admin-site-enhancements' ), |
| 277 | 'read', |
| 278 | 'asenha_show_hidden_menu', |
| 279 | function () { |
| 280 | return false; |
| 281 | }, |
| 282 | "dashicons-arrow-down-alt2", |
| 283 | 300 |
| 284 | ); |
| 285 | add_menu_page( |
| 286 | __( 'Show Less', 'admin-site-enhancements' ), |
| 287 | __( 'Show Less', 'admin-site-enhancements' ), |
| 288 | 'read', |
| 289 | 'asenha_hide_hidden_menu', |
| 290 | function () { |
| 291 | return false; |
| 292 | }, |
| 293 | "dashicons-arrow-up-alt2", |
| 294 | 301 |
| 295 | ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Script to toggle hidden menu itesm |
| 301 | * |
| 302 | * @since 2.0.0 |
| 303 | */ |
| 304 | public function enqueue_toggle_hidden_menu_script() { |
| 305 | // Get menu items hidden by toggle |
| 306 | $common_methods = new Common_Methods(); |
| 307 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 308 | $submenu_hidden_by_toggle = array(); |
| 309 | if ( !empty( $menu_hidden_by_toggle ) || !empty( $submenu_hidden_by_toggle ) ) { |
| 310 | // Script to set behaviour and actions of the sortable menu |
| 311 | wp_enqueue_script( |
| 312 | 'asenha-toggle-hidden-menu', |
| 313 | ASENHA_URL . 'assets/js/toggle-hidden-menu.js', |
| 314 | array(), |
| 315 | ASENHA_VERSION, |
| 316 | false |
| 317 | ); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | } |
| 322 |