PluginProbe ʕ •ᴥ•ʔ
Admin and Site Enhancements (ASE) / 6.0.4
Admin and Site Enhancements (ASE) v6.0.4
9.1.0 9.0.2 9.0.1 9.0.0 8.9.2 8.9.1 8.9.0 8.8.8 8.8.7 8.8.6 8.8.5 8.8.4 8.8.3 8.8.2 8.8.1 8.8.0 8.7.3 8.7.2 8.7.1 8.2.1 8.2.2 8.2.3 8.3.0 8.3.1 8.3.2 8.4.0 8.4.1 8.4.2 8.5.0 8.5.1 8.5.2 8.6.0 8.6.1 8.6.2 8.7.0 5.0.1 5.0.2 5.0.2.1 5.0.2.2 5.0.2.3 5.0.2.4 5.1.0 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1 5.5.0 5.5.1 5.5.2 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.8.0 5.8.1 6.0.0 6.0.3 6.0.4 6.0.5 6.0.5.1 6.0.6 6.0.7 6.0.8.1 6.1.0 6.1.3 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.3.0 6.3.1 6.3.2 6.4.0 6.5.0 6.5.1 6.6.0 6.7.0 6.8.0 6.8.2 6.8.3 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13.1 6.9.13.2 6.9.2 6.9.3 6.9.4 6.9.5 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.2 7.0.2.1 7.0.2.2 7.0.2.3 7.0.3 7.1.0 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2.0 7.2.1 7.3.0 7.3.1 7.3.2 7.3.3 7.4.0 7.4.2 7.4.4 7.4.5 7.4.6 7.4.7 7.4.8 7.5.0 7.5.1 7.5.2 7.5.3 7.5.4 7.6.0 7.6.1 7.6.1.1 7.6.10 7.6.11 7.6.2 7.6.3 7.6.4 7.6.5 7.6.6 7.6.7 7.6.7.1 7.6.8 7.6.9 7.7.0 7.8.0 7.8.1 7.8.10 7.8.11 7.8.12 7.8.13 7.8.14 7.8.15 7.8.16 7.8.17 7.8.18 7.8.2 7.8.3 7.8.4 7.8.5 7.8.5.1 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1 7.9.10 7.9.11 7.9.2 7.9.3 7.9.4 7.9.5 7.9.6 7.9.7 7.9.8 7.9.9 8.0.0 8.0.1 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.1.0 8.1.1 8.1.2 8.1.3 8.1.4 8.2.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.2.0 3.3.0 3.4.0 3.5.0 3.6.1 3.7.0 3.8.0 3.9.0 3.9.1 3.9.2 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.9.0 4.9.1 4.9.2 4.9.3 5.0.0
admin-site-enhancements / classes / class-admin-interface.php
admin-site-enhancements / classes Last commit date
class-activation.php 2 years ago class-admin-interface.php 2 years ago class-common-methods.php 2 years ago class-content-management.php 2 years ago class-custom-code.php 2 years ago class-deactivation.php 2 years ago class-disable-components.php 2 years ago class-login-logout.php 2 years ago class-optimizations.php 2 years ago class-security.php 2 years ago class-settings-fields-render.php 2 years ago class-settings-sanitization.php 2 years ago class-settings-sections-fields.php 2 years ago class-utilities.php 2 years ago
class-admin-interface.php
998 lines
1 <?php
2
3 namespace ASENHA\Classes;
4
5 use WP_Admin_Bar ;
6 use ArrayObject ;
7 /**
8 * Class related to Admin Interface features
9 *
10 * @since 1.2.0
11 */
12 class Admin_Interface
13 {
14 /**
15 * Modify admin bar menu for Admin Interface >> Hide or Modify Elements feature
16 *
17 * @param $wp_admin_bar object The admin bar.
18 * @since 1.9.0
19 */
20 public function modify_admin_bar_menu( $wp_admin_bar )
21 {
22 $options = get_option( ASENHA_SLUG_U, array() );
23 // Hide WP Logo Menu
24
25 if ( array_key_exists( 'hide_ab_wp_logo_menu', $options ) && $options['hide_ab_wp_logo_menu'] ) {
26 remove_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
27 // priority needs to match default value. Use QM to reference.
28 }
29
30 // Hide Customize Menu
31
32 if ( array_key_exists( 'hide_ab_customize_menu', $options ) && $options['hide_ab_customize_menu'] ) {
33 remove_action( 'admin_bar_menu', 'wp_admin_bar_customize_menu', 40 );
34 // priority needs to match default value. Use QM to reference.
35 }
36
37 // Hide Updates Counter/Link
38
39 if ( array_key_exists( 'hide_ab_updates_menu', $options ) && $options['hide_ab_updates_menu'] ) {
40 remove_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 50 );
41 // priority needs to match default value. Use QM to reference.
42 }
43
44 // Hide Comments Counter/Link
45
46 if ( array_key_exists( 'hide_ab_comments_menu', $options ) && $options['hide_ab_comments_menu'] ) {
47 remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
48 // priority needs to match default value. Use QM to reference.
49 }
50
51 // Hide New Content Menu
52
53 if ( array_key_exists( 'hide_ab_new_content_menu', $options ) && $options['hide_ab_new_content_menu'] ) {
54 remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
55 // priority needs to match default value. Use QM to reference.
56 }
57
58 // Hide 'Howdy' text
59
60 if ( array_key_exists( 'hide_ab_howdy', $options ) && $options['hide_ab_howdy'] ) {
61 // Remove the whole my account sectino and later rebuild it
62 remove_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
63 $current_user = wp_get_current_user();
64 $user_id = get_current_user_id();
65 $profile_url = get_edit_profile_url( $user_id );
66 $avatar = get_avatar( $user_id, 26 );
67 // size 26x26 pixels
68 $display_name = $current_user->display_name;
69 $class = 'with-avatar';
70 $wp_admin_bar->add_menu( array(
71 'id' => 'my-account',
72 'parent' => 'top-secondary',
73 'title' => $display_name . $avatar,
74 'href' => $profile_url,
75 'meta' => array(
76 'class' => $class,
77 ),
78 ) );
79 }
80
81 }
82
83 /**
84 * Wrapper for admin notices being output on admin screens
85 *
86 * @since 1.2.0
87 */
88 public function admin_notices_wrapper()
89 {
90 if ( current_user_can( 'manage_options' ) ) {
91 echo '<div class="asenha-admin-notices-drawer" style="display:none;"><h2>Admin Notices</h2></div>' ;
92 }
93 }
94
95 /**
96 * Admin bar menu item for the hidden admin notices
97 *
98 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/
99 * @link https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
100 * @since 1.2.0
101 */
102 public function admin_notices_menu( WP_Admin_Bar $wp_admin_bar )
103 {
104 if ( current_user_can( 'manage_options' ) ) {
105 $wp_admin_bar->add_menu( array(
106 'id' => 'asenha-hide-admin-notices',
107 'parent' => 'top-secondary',
108 'grou' => null,
109 'title' => 'Notices<span class="asenha-admin-notices-counter" style="opacity:0;">0</span>',
110 'meta' => array(
111 'class' => 'asenha-admin-notices-menu',
112 'title' => 'Click to view hidden admin notices',
113 ),
114 ) );
115 }
116 }
117
118 /**
119 * Inline CSS for the admin bar notices menu
120 *
121 * @since 1.2.0
122 */
123 public function admin_notices_menu_inline_css()
124 {
125
126 if ( current_user_can( 'manage_options' ) ) {
127 wp_add_inline_style( 'admin-bar', '
128
129 #wpadminbar .asenha-admin-notices-counter {
130 box-sizing: border-box;
131 margin: 1px 0 -1px 6px ;
132 padding: 2px 6px 3px 5px;
133 min-width: 18px;
134 height: 18px;
135 border-radius: 50%;
136 background-color: #ca4a1f;
137 color: #fff;
138 font-size: 11px;
139 line-height: 1.6;
140 text-align: center;
141 }
142
143 ' );
144 // Below we pre-emptively hide notices to avoid having them shown briefly before being moved into the notices panel via JS
145 ?>
146 <style type="text/css">
147 /* #wpbody-content .notice:not(.system-notice,.update-message),
148 #wpbody-content .notice-error,
149 #wpbody-content .error,
150 #wpbody-content .notice-info,
151 #wpbody-content .notice-information,
152 #wpbody-content #message,
153 #wpbody-content .notice-warning:not(.update-message),
154 #wpbody-content .notice-success:not(.update-message),
155 #wpbody-content .notice-updated,
156 #wpbody-content .updated:not(.active, .inactive, .plugin-update-tr),
157 #wpbody-content .update-nag, */
158 #wpbody-content > .wrap > .notice:not(.system-notice),
159 #wpbody-content > .wrap > .notice-error,
160 #wpbody-content > .wrap > .error,
161 #wpbody-content > .wrap > .notice-info,
162 #wpbody-content > .wrap > .notice-information,
163 #wpbody-content > .wrap > #message,
164 #wpbody-content > .wrap > .notice-warning,
165 #wpbody-content > .wrap > .notice-success,
166 #wpbody-content > .wrap > .notice-updated,
167 #wpbody-content > .wrap > .updated,
168 #wpbody-content > .wrap > .update-nag,
169 #wpbody-content > .wrap > div > .notice:not(.system-notice),
170 #wpbody-content > .wrap > div > .notice-error,
171 #wpbody-content > .wrap > div > .error,
172 #wpbody-content > .wrap > div > .notice-info,
173 #wpbody-content > .wrap > div > .notice-information,
174 #wpbody-content > .wrap > div > #message,
175 #wpbody-content > .wrap > div > .notice-warning,
176 #wpbody-content > .wrap > div > .notice-success,
177 #wpbody-content > .wrap > div > .notice-updated,
178 #wpbody-content > .wrap > div > .updated,
179 #wpbody-content > .wrap > div > .update-nag,
180 #wpbody-content > div > .wrap > .notice:not(.system-notice),
181 #wpbody-content > div > .wrap > .notice-error,
182 #wpbody-content > div > .wrap > .error,
183 #wpbody-content > div > .wrap > .notice-info,
184 #wpbody-content > div > .wrap > .notice-information,
185 #wpbody-content > div > .wrap > #message,
186 #wpbody-content > div > .wrap > .notice-warning,
187 #wpbody-content > div > .wrap > .notice-success,
188 #wpbody-content > div > .wrap > .notice-updated,
189 #wpbody-content > div > .wrap > .updated,
190 #wpbody-content > div > .wrap > .update-nag,
191 #wpbody-content > .notice,
192 #wpbody-content > .error,
193 #wpbody-content > .updated,
194 #wpbody-content > .update-nag,
195 #wpbody-content > .jp-connection-banner,
196 #wpbody-content > .jitm-banner,
197 #wpbody-content > .jetpack-jitm-message,
198 #wpbody-content > .ngg_admin_notice,
199 #wpbody-content > .imagify-welcome,
200 #wpbody-content #wordfenceAutoUpdateChoice,
201 #wpbody-content #easy-updates-manager-dashnotice {
202 display: none !important;
203 }
204 </style>
205 <?php
206 }
207
208 }
209
210 /**
211 * Disable dashboard widgets
212 *
213 * @since 4.2.0
214 */
215 public function disable_dashboard_widgets()
216 {
217 global $wp_meta_boxes ;
218 // Get list of disabled widgets
219 $options = get_option( ASENHA_SLUG_U, array() );
220 $disabled_dashboard_widgets = $options['disabled_dashboard_widgets'];
221 // Store default widgets in extra options. This will be referenced from settings field.
222 $dashboard_widgets = $this->get_dashboard_widgets();
223 $extra_options = get_option( 'admin_site_enhancements_extra', array() );
224 $extra_options['dashboard_widgets'] = $dashboard_widgets;
225 update_option( 'admin_site_enhancements_extra', $extra_options );
226 // Disable widgets
227 if ( is_array( $disabled_dashboard_widgets ) || is_object( $disabled_dashboard_widgets ) ) {
228 foreach ( $disabled_dashboard_widgets as $disabled_widget_id_context_priority => $is_disabled ) {
229 // e.g. dashboard_activity__normal__core => true/false
230
231 if ( $is_disabled ) {
232 $disabled_widget = explode( '__', $disabled_widget_id_context_priority );
233 $widget_id = $disabled_widget[0];
234 $widget_context = $disabled_widget[1];
235 $widget_priority = $disabled_widget[2];
236 // remove_meta_box( $widget_id, get_current_screen()->base, $widget_context );
237 unset( $wp_meta_boxes['dashboard'][$widget_context][$widget_priority][$widget_id] );
238 }
239
240 }
241 }
242 }
243
244 /**
245 * Get dashboard widgets
246 *
247 * @since 4.2.0
248 */
249 public function get_dashboard_widgets()
250 {
251 global $wp_meta_boxes ;
252 $dashboard_widgets = array();
253
254 if ( !isset( $wp_meta_boxes['dashboard'] ) ) {
255 $extra_options = get_option( 'admin_site_enhancements_extra', array() );
256
257 if ( !array_key_exists( 'dashboard_widgets', $extra_options ) ) {
258 require_once ABSPATH . '/wp-admin/includes/dashboard.php';
259 set_current_screen( 'dashboard' );
260 wp_dashboard_setup();
261 }
262
263 }
264
265 if ( isset( $wp_meta_boxes['dashboard'] ) ) {
266 foreach ( $wp_meta_boxes['dashboard'] as $context => $priorities ) {
267 foreach ( $priorities as $priority => $widgets ) {
268 foreach ( $widgets as $widget_id => $data ) {
269 $widget_title = ( isset( $data['title'] ) ? wp_strip_all_tags( preg_replace( '/ <span.*span>/im', '', $data['title'] ) ) : 'Undetectable' );
270 $dashboard_widgets[$widget_id] = array(
271 'id' => $widget_id,
272 'title' => $widget_title,
273 'context' => $context,
274 'priority' => $priority,
275 );
276 }
277 }
278 }
279 }
280 $dashboard_widgets = wp_list_sort(
281 $dashboard_widgets,
282 'title',
283 'ASC',
284 true
285 );
286 return $dashboard_widgets;
287 }
288
289 /**
290 * Hide admin bar on the frontend for the user roles selected
291 *
292 * @since 1.3.0
293 */
294 public function hide_admin_bar_for_roles()
295 {
296 $options = get_option( ASENHA_SLUG_U );
297 $hide_admin_bar = $options['hide_admin_bar'];
298 $for_roles = $options['hide_admin_bar_for'];
299 $current_user = wp_get_current_user();
300 $current_user_roles = (array) $current_user->roles;
301 // single dimensional array of role slugs
302 // User has no role, i.e. logged-out
303
304 if ( count( $current_user_roles ) == 0 ) {
305 return false;
306 // hide admin bar
307 }
308
309 // User has role(s). Do further checks.
310
311 if ( isset( $for_roles ) && count( $for_roles ) > 0 ) {
312 // Assemble single-dimensional array of roles for which admin bar would be hidden
313 $roles_admin_bar_hidden = array();
314 foreach ( $for_roles as $role_slug => $admin_bar_hidden ) {
315 if ( $admin_bar_hidden ) {
316 $roles_admin_bar_hidden[] = $role_slug;
317 }
318 }
319 // Check if any of the current user roles is one for which admin bar should be hidden
320 foreach ( $current_user_roles as $role ) {
321
322 if ( in_array( $role, $roles_admin_bar_hidden ) ) {
323 return false;
324 // hide admin bar
325 }
326
327 }
328 }
329
330 return true;
331 // show admin bar
332 }
333
334 /**
335 * Set custom admin menu width
336 *
337 * @since 5.2.0
338 */
339 public function set_custom_menu_width()
340 {
341 $options = get_option( ASENHA_SLUG_U, array() );
342 $custom_width = $options['admin_menu_width'];
343 $wp_version = get_bloginfo( 'version' );
344
345 if ( version_compare( $wp_version, '5', '>' ) ) {
346 require_once ASENHA_PATH . 'includes/admin-menu-width/wp-v5-greater.php';
347 } elseif ( version_compare( $wp_version, '4', '>=' ) ) {
348 require_once ASENHA_PATH . 'includes/admin-menu-width/wp-v4-greater.php';
349 } else {
350 }
351
352 }
353
354 /**
355 * Render custom menu order
356 *
357 * @param $menu_order array an ordered array of menu items
358 * @link https://developer.wordpress.org/reference/hooks/menu_order/
359 * @since 2.0.0
360 */
361 public function render_custom_menu_order( $menu_order )
362 {
363 global $menu ;
364 $options = get_option( ASENHA_SLUG_U );
365 // Get current menu order. We're not using the default $menu_order which uses index.php, edit.php as array values.
366 $current_menu_order = array();
367 foreach ( $menu as $menu_key => $menu_info ) {
368
369 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
370 $menu_item_id = $menu_info[2];
371 } else {
372 $menu_item_id = $menu_info[5];
373 }
374
375 $current_menu_order[] = array( $menu_item_id, $menu_info[2] );
376 }
377 // Get custom menu order
378 $custom_menu_order = $options['custom_menu_order'];
379 // comma separated
380 $custom_menu_order = explode( ",", $custom_menu_order );
381 // array of menu ID, e.g. menu-dashboard
382 // Return menu order for rendering
383 $rendered_menu_order = array();
384 // Render menu based on items saved in custom menu order
385 foreach ( $custom_menu_order as $custom_menu_item_id ) {
386 foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) {
387 if ( $custom_menu_item_id == $current_menu_item[0] ) {
388 $rendered_menu_order[] = $current_menu_item[1];
389 }
390 }
391 }
392 // Add items from current menu not already part of custom menu order, e.g. new plugin activated and adds new menu item
393 foreach ( $current_menu_order as $current_menu_item_id => $current_menu_item ) {
394 if ( !in_array( $current_menu_item[0], $custom_menu_order ) ) {
395 $rendered_menu_order[] = $current_menu_item[1];
396 }
397 }
398 return $rendered_menu_order;
399 }
400
401 /**
402 * Apply custom menu item titles
403 *
404 * @since 2.9.0
405 */
406 public function apply_custom_menu_item_titles()
407 {
408 global $menu ;
409 $options = get_option( ASENHA_SLUG_U );
410 // Get custom menu item titles
411 $custom_menu_titles = $options['custom_menu_titles'];
412 $custom_menu_titles = explode( ',', $custom_menu_titles );
413 foreach ( $menu as $menu_key => $menu_info ) {
414
415 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
416 $menu_item_id = $menu_info[2];
417 } else {
418 $menu_item_id = $menu_info[5];
419 }
420
421 // Get defaul/custom menu item title
422 foreach ( $custom_menu_titles as $custom_menu_title ) {
423 // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets
424 $custom_menu_title = explode( '__', $custom_menu_title );
425
426 if ( $custom_menu_title[0] == $menu_item_id ) {
427 $menu_item_title = $custom_menu_title[1];
428 // e.g. Code Snippets
429 break;
430 // stop foreach loop so $menu_item_title is not overwritten in the next iteration
431 } else {
432 $menu_item_title = $menu_info[0];
433 }
434
435 }
436 $menu[$menu_key][0] = $menu_item_title;
437 }
438 }
439
440 /**
441 * Hide menu items by adding a class to hide them (part of WP Core's common.css)
442 *
443 * @since 2.0.0
444 */
445 public function hide_menu_items()
446 {
447 global $menu ;
448 $common_methods = new Common_Methods();
449 $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle();
450 // indexed array
451 foreach ( $menu as $menu_key => $menu_info ) {
452
453 if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) {
454 $menu_item_id = $menu_info[2];
455 } else {
456 $menu_item_id = $menu_info[5];
457 }
458
459 // Append 'hidden' class to hide menu item until toggled
460 if ( in_array( $menu_item_id, $menu_hidden_by_toggle ) ) {
461 $menu[$menu_key][4] = $menu_info[4] . ' hidden asenha_hidden_menu';
462 }
463 }
464 }
465
466 /**
467 * Add toggle to show hidden menu items
468 *
469 * @since 2.0.0
470 */
471 public function add_hidden_menu_toggle()
472 {
473 global $current_user ;
474 // Get menu items hidden by toggle
475 $common_methods = new Common_Methods();
476 $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle();
477 // Get user capabilities the "Show All/Less" toggle should be shown for
478 $user_capabilities_to_show_menu_toggle_for = $common_methods->get_user_capabilities_to_show_menu_toggle_for();
479 // Get current user's capabilities from the user's role(s)
480 $current_user_capabilities = '';
481 $current_user_roles = $current_user->roles;
482 // indexed array of role slugs
483 foreach ( $current_user_roles as $current_user_role ) {
484 $current_user_role_capabilities = get_role( $current_user_role )->capabilities;
485 $current_user_role_capabilities = array_keys( $current_user_role_capabilities );
486 // indexed array
487 $current_user_role_capabilities = implode( ",", $current_user_role_capabilities );
488 $current_user_capabilities .= $current_user_role_capabilities;
489 }
490 $current_user_capabilities = array_unique( explode( ",", $current_user_capabilities ) );
491 // Maybe show "Show All/Less" toggle
492 $show_toggle_menu = false;
493 foreach ( $user_capabilities_to_show_menu_toggle_for as $user_capability_to_show_menu_toggle_for ) {
494
495 if ( in_array( $user_capability_to_show_menu_toggle_for, $current_user_capabilities ) ) {
496 $show_toggle_menu = true;
497 break;
498 }
499
500 }
501
502 if ( !empty($menu_hidden_by_toggle) && $show_toggle_menu ) {
503 add_menu_page(
504 'Show All',
505 'Show All',
506 'read',
507 'asenha_show_hidden_menu',
508 function () {
509 return false;
510 },
511 "dashicons-arrow-down-alt2",
512 300
513 );
514 add_menu_page(
515 'Show Less',
516 'Show Less',
517 'read',
518 'asenha_hide_hidden_menu',
519 function () {
520 return false;
521 },
522 "dashicons-arrow-up-alt2",
523 301
524 );
525 }
526
527 }
528
529 /**
530 * Script to toggle hidden menu itesm
531 *
532 * @since 2.0.0
533 */
534 public function enqueue_toggle_hidden_menu_script()
535 {
536 // Get menu items hidden by toggle
537 $common_methods = new Common_Methods();
538 $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle();
539 if ( !empty($menu_hidden_by_toggle) ) {
540 // Script to set behaviour and actions of the sortable menu
541 wp_enqueue_script(
542 'asenha-toggle-hidden-menu',
543 ASENHA_URL . 'assets/js/toggle-hidden-menu.js',
544 array(),
545 ASENHA_VERSION,
546 false
547 );
548 }
549 }
550
551 /**
552 * Hide the Help tab and drawer
553 *
554 * @since 4.5.0
555 */
556 public function hide_help_drawer()
557 {
558
559 if ( is_admin() ) {
560 $screen = get_current_screen();
561 $screen->remove_help_tabs();
562 }
563
564 }
565
566 /**
567 * Current post type. For Content Admin >> Show Custom Taxonomy Filters functionality.
568 */
569 public $post_type ;
570 /**
571 * Show featured images column in list tables for pages and post types that support featured image
572 *
573 * @since 1.0.0
574 */
575 public function show_featured_image_column()
576 {
577 $post_types = get_post_types( array(
578 'public' => true,
579 ), 'names' );
580 foreach ( $post_types as $post_type_key => $post_type_name ) {
581
582 if ( post_type_supports( $post_type_key, 'thumbnail' ) ) {
583 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_featured_image_column' ], 999 );
584 add_action(
585 "manage_{$post_type_name}_posts_custom_column",
586 [ $this, 'add_featured_image' ],
587 10,
588 2
589 );
590 }
591
592 }
593 }
594
595 /**
596 * Add a column called Featured Image as the first column
597 *
598 * @param mixed $columns
599 * @return void
600 * @since 1.0.0
601 */
602 public function add_featured_image_column( $columns )
603 {
604 $new_columns = array();
605 foreach ( $columns as $key => $value ) {
606 if ( 'title' == $key ) {
607 // We add featured image column before the 'title' column
608 $new_columns['asenha-featured-image'] = 'Featured Image';
609 }
610 if ( 'thumb' == $key ) {
611 // For WooCommerce products, we add featured image column before it's native thumbnail column
612 $new_columns['asenha-featured-image'] = 'Product Image';
613 }
614 $new_columns[$key] = $value;
615 }
616 // Replace WooCommerce thumbnail column with ASE featured image column
617 if ( array_key_exists( 'thumb', $new_columns ) ) {
618 unset( $new_columns['thumb'] );
619 }
620 return $new_columns;
621 }
622
623 /**
624 * Echo featured image's in thumbnail size to a column
625 *
626 * @param mixed $column_name
627 * @param mixed $id
628 * @since 1.0.0
629 */
630 public function add_featured_image( $column_name, $id )
631 {
632 if ( 'asenha-featured-image' === $column_name ) {
633
634 if ( has_post_thumbnail( $id ) ) {
635 $size = 'thumbnail';
636 echo get_the_post_thumbnail( $id, $size, '' ) ;
637 } else {
638 echo '<img src="' . esc_url( plugins_url( 'assets/img/default_featured_image.jpg', __DIR__ ) ) . '" />' ;
639 }
640
641 }
642 }
643
644 /**
645 * Show excerpt column in list tables for pages and post types that support excerpt.
646 *
647 * @since 1.0.0
648 */
649 public function show_excerpt_column()
650 {
651 $post_types = get_post_types( array(
652 'public' => true,
653 ), 'names' );
654 foreach ( $post_types as $post_type_key => $post_type_name ) {
655
656 if ( post_type_supports( $post_type_key, 'excerpt' ) ) {
657 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_excerpt_column' ] );
658 add_action(
659 "manage_{$post_type_name}_posts_custom_column",
660 [ $this, 'add_excerpt' ],
661 10,
662 2
663 );
664 }
665
666 }
667 }
668
669 /**
670 * Add a column called Featured Image as the first column
671 *
672 * @param mixed $columns
673 * @return void
674 * @since 1.0.0
675 */
676 public function add_excerpt_column( $columns )
677 {
678 $new_columns = array();
679 foreach ( $columns as $key => $value ) {
680 $new_columns[$key] = $value;
681 if ( $key == 'title' ) {
682 $new_columns['asenha-excerpt'] = 'Excerpt';
683 }
684 }
685 return $new_columns;
686 }
687
688 /**
689 * Echo featured image's in thumbnail size to a column
690 *
691 * @param mixed $column_name
692 * @param mixed $id
693 * @since 1.0.0
694 */
695 public function add_excerpt( $column_name, $id )
696 {
697
698 if ( 'asenha-excerpt' === $column_name ) {
699 $excerpt = get_the_excerpt( $id );
700 // about 310 characters
701 $excerpt = substr( $excerpt, 0, 160 );
702 // truncate to 160 characters
703 $short_excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
704 echo wp_kses_post( $short_excerpt ) ;
705 }
706
707 }
708
709 /**
710 * Add ID column list table of pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
711 *
712 * @since 1.0.0
713 */
714 public function show_id_column()
715 {
716 // For pages and hierarchical post types list table
717 add_filter( 'manage_pages_columns', [ $this, 'add_id_column' ] );
718 add_action(
719 'manage_pages_custom_column',
720 [ $this, 'add_id_echo_value' ],
721 10,
722 2
723 );
724 // For posts and non-hierarchical custom posts list table
725 add_filter( 'manage_posts_columns', [ $this, 'add_id_column' ] );
726 add_action(
727 'manage_posts_custom_column',
728 [ $this, 'add_id_echo_value' ],
729 10,
730 2
731 );
732 // For media list table
733 add_filter( 'manage_media_columns', [ $this, 'add_id_column' ] );
734 add_action(
735 'manage_media_custom_column',
736 [ $this, 'add_id_echo_value' ],
737 10,
738 2
739 );
740 // For list table of all taxonomies
741 $taxonomies = get_taxonomies( [
742 'public' => true,
743 ], 'names' );
744 foreach ( $taxonomies as $taxonomy ) {
745 add_filter( 'manage_edit-' . $taxonomy . '_columns', [ $this, 'add_id_column' ] );
746 add_action(
747 'manage_' . $taxonomy . '_custom_column',
748 [ $this, 'add_id_return_value' ],
749 10,
750 3
751 );
752 }
753 // For users list table
754 add_filter( 'manage_users_columns', [ $this, 'add_id_column' ] );
755 add_action(
756 'manage_users_custom_column',
757 [ $this, 'add_id_return_value' ],
758 10,
759 3
760 );
761 // For comments list table
762 add_filter( 'manage_edit-comments_columns', [ $this, 'add_id_column' ] );
763 add_action(
764 'manage_comments_custom_column',
765 [ $this, 'add_id_echo_value' ],
766 10,
767 3
768 );
769 }
770
771 /**
772 * Add a column called ID
773 *
774 * @param mixed $columns
775 * @return void
776 * @since 1.0.0
777 */
778 public function add_id_column( $columns )
779 {
780 $columns['asenha-id'] = 'ID';
781 return $columns;
782 }
783
784 /**
785 * Echo post ID value to a column
786 *
787 * @param mixed $column_name
788 * @param mixed $id
789 * @since 1.0.0
790 */
791 public function add_id_echo_value( $column_name, $id )
792 {
793 if ( 'asenha-id' === $column_name ) {
794 echo esc_html( $id ) ;
795 }
796 }
797
798 /**
799 * Return post ID value to a column
800 *
801 * @param mixed $value
802 * @param mixed $column_name
803 * @param mixed $id
804 * @since 1.0.0
805 */
806 public function add_id_return_value( $value, $column_name, $id )
807 {
808 if ( 'asenha-id' === $column_name ) {
809 $value = $id;
810 }
811 return $value;
812 }
813
814 /**
815 * Add ID in the action row of list tables for pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
816 *
817 * @since 4.7.4
818 */
819 public function show_id_in_action_row()
820 {
821 add_filter(
822 'page_row_actions',
823 array( $this, 'add_id_in_action_row' ),
824 10,
825 2
826 );
827 add_filter(
828 'post_row_actions',
829 array( $this, 'add_id_in_action_row' ),
830 10,
831 2
832 );
833 add_filter(
834 'cat_row_actions',
835 array( $this, 'add_id_in_action_row' ),
836 10,
837 2
838 );
839 add_filter(
840 'tag_row_actions',
841 array( $this, 'add_id_in_action_row' ),
842 10,
843 2
844 );
845 add_filter(
846 'media_row_actions',
847 array( $this, 'add_id_in_action_row' ),
848 10,
849 2
850 );
851 add_filter(
852 'comment_row_actions',
853 array( $this, 'add_id_in_action_row' ),
854 10,
855 2
856 );
857 add_filter(
858 'user_row_actions',
859 array( $this, 'add_id_in_action_row' ),
860 10,
861 2
862 );
863 }
864
865 /**
866 * Output the ID in the action row
867 *
868 * @since 4.7.4
869 */
870 public function add_id_in_action_row( $actions, $object )
871 {
872
873 if ( current_user_can( 'edit_posts' ) ) {
874 // For pages, posts, custom post types, media/attachments, users
875 if ( property_exists( $object, 'ID' ) ) {
876 $id = $object->ID;
877 }
878 // For taxonomies
879 if ( property_exists( $object, 'term_id' ) ) {
880 $id = $object->term_id;
881 }
882 // For comments
883 if ( property_exists( $object, 'comment_ID' ) ) {
884 $id = $object->comment_ID;
885 }
886 $actions['asenha-list-table-item-id'] = '<span class="asenha-list-table-item-id">ID: ' . $id . '</span>';
887 }
888
889 return $actions;
890 }
891
892 /**
893 * Hide comments column in list tables for pages, post types that support comments, and alse media/attachments.
894 *
895 * @since 1.0.0
896 */
897 public function hide_comments_column()
898 {
899 $post_types = get_post_types( array(
900 'public' => true,
901 ), 'names' );
902 foreach ( $post_types as $post_type_key => $post_type_name ) {
903 if ( post_type_supports( $post_type_key, 'comments' ) ) {
904
905 if ( 'attachment' != $post_type_name ) {
906 // For list tables of pages, posts and other post types
907 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'remove_comment_column' ] );
908 } else {
909 // For list table of media/attachment
910 add_filter( 'manage_media_columns', [ $this, 'remove_comment_column' ] );
911 }
912
913 }
914 }
915 }
916
917 /**
918 * Add a column called ID
919 *
920 * @param mixed $columns
921 * @return void
922 * @since 1.0.0
923 */
924 public function remove_comment_column( $columns )
925 {
926 unset( $columns['comments'] );
927 return $columns;
928 }
929
930 /**
931 * Hide tags column in list tables for posts.
932 *
933 * @since 1.0.0
934 */
935 public function hide_post_tags_column()
936 {
937 $post_types = get_post_types( array(
938 'public' => true,
939 ), 'names' );
940 foreach ( $post_types as $post_type_key => $post_type_name ) {
941 if ( $post_type_name == 'post' ) {
942 add_filter( "manage_posts_columns", [ $this, 'remove_post_tags_column' ] );
943 }
944 }
945 }
946
947 /**
948 * Add a column called ID
949 *
950 * @param mixed $columns
951 * @return void
952 * @since 1.0.0
953 */
954 public function remove_post_tags_column( $columns )
955 {
956 unset( $columns['tags'] );
957 return $columns;
958 }
959
960 /**
961 * Show custom (hierarchical) taxonomy filter(s) for all post types.
962 *
963 * @since 1.0.0
964 */
965 public function show_custom_taxonomy_filters( $post_type )
966 {
967 $post_taxonomies = get_object_taxonomies( $post_type, 'objects' );
968 // Only show custom taxonomy filters for post types other than 'post'
969 if ( 'post' != $post_type ) {
970 array_walk( $post_taxonomies, [ $this, 'output_taxonomy_filter' ] );
971 }
972 }
973
974 /**
975 * Output filter on the post type's list table for a taxonomy
976 *
977 * @since 1.0.0
978 */
979 public function output_taxonomy_filter( $post_taxonomy )
980 {
981 // Only show taxonomy filter when the taxonomy is hierarchical
982 if ( true === $post_taxonomy->hierarchical ) {
983 wp_dropdown_categories( array(
984 'show_option_all' => sprintf( 'All %s', $post_taxonomy->label ),
985 'orderby' => 'name',
986 'order' => 'ASC',
987 'hide_empty' => false,
988 'hide_if_empty' => true,
989 'selected' => filter_input( INPUT_GET, $post_taxonomy->query_var, FILTER_SANITIZE_STRING ),
990 'hierarchical' => true,
991 'name' => $post_taxonomy->query_var,
992 'taxonomy' => $post_taxonomy->name,
993 'value_field' => 'slug',
994 ) );
995 }
996 }
997
998 }