PluginProbe ʕ •ᴥ•ʔ
Admin and Site Enhancements (ASE) / 6.2.4
Admin and Site Enhancements (ASE) v6.2.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
1210 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,.hidden),
159 #wpbody-content > .wrap > .notice-error,
160 #wpbody-content > .wrap > .error:not(.hidden),
161 #wpbody-content > .wrap > .notice-info,
162 #wpbody-content > .wrap > .notice-information,
163 #wpbody-content > .wrap > #message,
164 #wpbody-content > .wrap > .notice-warning:not(.hidden),
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,.hidden),
170 #wpbody-content > .wrap > div > .notice-error,
171 #wpbody-content > .wrap > div > .error:not(.hidden),
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:not(.hidden),
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,.hidden),
181 #wpbody-content > div > .wrap > .notice-error,
182 #wpbody-content > div > .wrap > .error:not(.hidden),
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:not(.hidden),
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_on_frontend()
295 {
296 $options = get_option( ASENHA_SLUG_U );
297 $hide_admin_bar = $options['hide_admin_bar'];
298 $for_roles_frontend = $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_frontend ) && count( $for_roles_frontend ) > 0 ) {
312 // Assemble single-dimensional array of roles for which admin bar would be hidden
313 $roles_admin_bar_hidden_frontend = array();
314 foreach ( $for_roles_frontend as $role_slug => $admin_bar_hidden ) {
315 if ( $admin_bar_hidden ) {
316 $roles_admin_bar_hidden_frontend[] = $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_frontend ) ) {
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 * Get and set default columns
553 *
554 * @since 6.0.9
555 */
556 public function get_default_columns( $post_type )
557 {
558 $available_columns = array(
559 'date_published' => 'Published',
560 'postid' => 'ID',
561 'modified' => 'Last Modified',
562 'password_protected' => 'Password Protected',
563 'permalink' => 'Permalink',
564 'slug' => 'Slug',
565 'status' => 'Status',
566 'date' => 'Date',
567 );
568 if ( 'post' == $post_type ) {
569 $available_columns['sticky'] = 'Sticky';
570 }
571
572 if ( post_type_supports( $post_type, 'title' ) ) {
573 $available_columns['title'] = 'Title';
574 // $available_columns['title_raw'] = 'Title Only';
575 }
576
577 if ( post_type_supports( $post_type, 'editor' ) || post_type_supports( $post_type, 'excerpt' ) ) {
578 $available_columns['excerpt'] = 'Excerpt';
579 }
580 if ( post_type_supports( $post_type, 'thumbnail' ) ) {
581 $available_columns['featured_image'] = 'Featured Image';
582 }
583
584 if ( post_type_supports( $post_type, 'author' ) ) {
585 $available_columns['author'] = 'Author';
586 // $available_columns['author_name'] = 'Author';
587 // $available_columns['last_modified_author'] = 'Last Modified Author';
588 }
589
590 if ( post_type_supports( $post_type, 'post-formats' ) ) {
591 $available_columns['post_formats'] = 'Post Format';
592 }
593
594 if ( post_type_supports( $post_type, 'comments' ) ) {
595 $available_columns['comments'] = 'Comments';
596 $available_columns['comment_count'] = 'Comment Count';
597 $available_columns['comment_status'] = 'Allow Comment';
598 }
599
600 if ( post_type_supports( $post_type, 'trackbacks' ) ) {
601 $available_columns['ping_status'] = 'Ping Status';
602 }
603 if ( post_type_supports( $post_type, 'post-formats' ) ) {
604 $available_columns['post_formats'] = 'Post Format';
605 }
606
607 if ( post_type_supports( $post_type, 'page-attributes' ) || is_post_type_hierarchical( $post_type ) ) {
608 $available_columns['post_parent'] = 'Post Parent';
609 $available_columns['menu_order'] = 'Menu Order';
610 }
611
612 // Add taxonomy columns according to the custom taxonomies assigned to each post type
613 $attached_taxonomies = get_object_taxonomies( $post_type );
614 if ( !empty($attached_taxonomies) ) {
615 foreach ( $attached_taxonomies as $taxonomy_slug ) {
616 $taxonomy_object = get_taxonomy( $taxonomy_slug );
617 $taxonomy_label = $taxonomy_object->labels->name;
618
619 if ( 'category' == $taxonomy_slug ) {
620 $taxonomy_slug = 'categories';
621 } elseif ( 'post_tag' == $taxonomy_slug ) {
622 $taxonomy_slug = 'tags';
623 }
624
625 $available_columns[$taxonomy_slug] = $taxonomy_label;
626 }
627 }
628 // Sort by array value in ascending order, so they are displayed in that order in the 'Default' pane
629 asort( $available_columns );
630 $options = get_option( ASENHA_SLUG_U . '_extra', array() );
631 $options['admin_columns_available'][$post_type] = $available_columns;
632 update_option( ASENHA_SLUG_U . '_extra', $options );
633 return $available_columns;
634 }
635
636 /**
637 * Reload list table after performing Quick Edit on post types with custom admin columns
638 * This is modified from wp_ajax_inline_save() by adding a script before wp_die() at the end
639 *
640 * @link https://github.com/WordPress/wordpress-develop/blob/6.3/src/wp-admin/includes/ajax-actions.php#L2049-L2159
641 * @since 6.0.6
642 */
643 public function wp_ajax_inline_save_with_page_reload()
644 {
645 global $mode ;
646 check_ajax_referer( 'inlineeditnonce', '_inline_edit' );
647 if ( !isset( $_POST['post_ID'] ) || !(int) $_POST['post_ID'] ) {
648 wp_die();
649 }
650 $post_id = (int) $_POST['post_ID'];
651
652 if ( 'page' === $_POST['post_type'] ) {
653 if ( !current_user_can( 'edit_page', $post_id ) ) {
654 wp_die( __( 'Sorry, you are not allowed to edit this page.' ) );
655 }
656 } else {
657 if ( !current_user_can( 'edit_post', $post_id ) ) {
658 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
659 }
660 }
661
662 $last = wp_check_post_lock( $post_id );
663
664 if ( $last ) {
665 $last_user = get_userdata( $last );
666 $last_user_name = ( $last_user ? $last_user->display_name : __( 'Someone' ) );
667 /* translators: %s: User's display name. */
668 $msg_template = __( 'Saving is disabled: %s is currently editing this post.' );
669 if ( 'page' === $_POST['post_type'] ) {
670 /* translators: %s: User's display name. */
671 $msg_template = __( 'Saving is disabled: %s is currently editing this page.' );
672 }
673 printf( $msg_template, esc_html( $last_user_name ) );
674 wp_die();
675 }
676
677 $data =& $_POST;
678 $post = get_post( $post_id, ARRAY_A );
679 // Since it's coming from the database.
680 $post = wp_slash( $post );
681 $data['content'] = $post['post_content'];
682 $data['excerpt'] = $post['post_excerpt'];
683 // Rename.
684 $data['user_ID'] = get_current_user_id();
685 if ( isset( $data['post_parent'] ) ) {
686 $data['parent_id'] = $data['post_parent'];
687 }
688 // Status.
689
690 if ( isset( $data['keep_private'] ) && 'private' === $data['keep_private'] ) {
691 $data['visibility'] = 'private';
692 $data['post_status'] = 'private';
693 } else {
694 $data['post_status'] = $data['_status'];
695 }
696
697 if ( empty($data['comment_status']) ) {
698 $data['comment_status'] = 'closed';
699 }
700 if ( empty($data['ping_status']) ) {
701 $data['ping_status'] = 'closed';
702 }
703 // Exclude terms from taxonomies that are not supposed to appear in Quick Edit.
704 if ( !empty($data['tax_input']) ) {
705 foreach ( $data['tax_input'] as $taxonomy => $terms ) {
706 $tax_object = get_taxonomy( $taxonomy );
707 /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
708 if ( !apply_filters(
709 'quick_edit_show_taxonomy',
710 $tax_object->show_in_quick_edit,
711 $taxonomy,
712 $post['post_type']
713 ) ) {
714 unset( $data['tax_input'][$taxonomy] );
715 }
716 }
717 }
718 // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published.
719
720 if ( !empty($data['post_name']) && in_array( $post['post_status'], array( 'draft', 'pending' ), true ) ) {
721 $post['post_status'] = 'publish';
722 $data['post_name'] = wp_unique_post_slug(
723 $data['post_name'],
724 $post['ID'],
725 $post['post_status'],
726 $post['post_type'],
727 $post['post_parent']
728 );
729 }
730
731 // Update the post.
732 edit_post();
733 $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array(
734 'screen' => $_POST['screen'],
735 ) );
736 $mode = ( 'excerpt' === $_POST['post_view'] ? 'excerpt' : 'list' );
737 $level = 0;
738
739 if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) {
740 $request_post = array( get_post( $_POST['post_ID'] ) );
741 $parent = $request_post[0]->post_parent;
742 while ( $parent > 0 ) {
743 $parent_post = get_post( $parent );
744 $parent = $parent_post->post_parent;
745 $level++;
746 }
747 }
748
749 $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ), $level );
750 // INTERCEPT: Add a script to reload the list table page
751 ?>
752 <script type="text/javascript">
753 jQuery('#post-<?php
754 echo $_POST['post_ID'] ;
755 ?>').css('opacity','0.3');
756 document.location.reload(true);
757 </script>
758 <?php
759 // end INTERCEPT
760 wp_die();
761 }
762
763 /**
764 * Hide the Help tab and drawer
765 *
766 * @since 4.5.0
767 */
768 public function hide_help_drawer()
769 {
770
771 if ( is_admin() ) {
772 $screen = get_current_screen();
773 $screen->remove_help_tabs();
774 }
775
776 }
777
778 /**
779 * Current post type. For Content Admin >> Show Custom Taxonomy Filters functionality.
780 */
781 public $post_type ;
782 /**
783 * Show featured images column in list tables for pages and post types that support featured image
784 *
785 * @since 1.0.0
786 */
787 public function show_featured_image_column()
788 {
789 $post_types = get_post_types( array(
790 'public' => true,
791 ), 'names' );
792 foreach ( $post_types as $post_type_key => $post_type_name ) {
793
794 if ( post_type_supports( $post_type_key, 'thumbnail' ) ) {
795 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_featured_image_column' ], 999 );
796 add_action(
797 "manage_{$post_type_name}_posts_custom_column",
798 [ $this, 'add_featured_image' ],
799 10,
800 2
801 );
802 }
803
804 }
805 }
806
807 /**
808 * Add a column called Featured Image as the first column
809 *
810 * @param mixed $columns
811 * @return void
812 * @since 1.0.0
813 */
814 public function add_featured_image_column( $columns )
815 {
816 $new_columns = array();
817 foreach ( $columns as $key => $value ) {
818 if ( 'title' == $key ) {
819 // We add featured image column before the 'title' column
820 $new_columns['asenha-featured-image'] = 'Featured Image';
821 }
822 if ( 'thumb' == $key ) {
823 // For WooCommerce products, we add featured image column before it's native thumbnail column
824 $new_columns['asenha-featured-image'] = 'Product Image';
825 }
826 $new_columns[$key] = $value;
827 }
828 // Replace WooCommerce thumbnail column with ASE featured image column
829 if ( array_key_exists( 'thumb', $new_columns ) ) {
830 unset( $new_columns['thumb'] );
831 }
832 return $new_columns;
833 }
834
835 /**
836 * Echo featured image's in thumbnail size to a column
837 *
838 * @param mixed $column_name
839 * @param mixed $id
840 * @since 1.0.0
841 */
842 public function add_featured_image( $column_name, $id )
843 {
844 if ( 'asenha-featured-image' === $column_name ) {
845
846 if ( has_post_thumbnail( $id ) ) {
847 $size = 'thumbnail';
848 echo get_the_post_thumbnail( $id, $size, '' ) ;
849 } else {
850 echo '<img src="' . esc_url( plugins_url( 'assets/img/default_featured_image.jpg', __DIR__ ) ) . '" />' ;
851 }
852
853 }
854 }
855
856 /**
857 * Show excerpt column in list tables for pages and post types that support excerpt.
858 *
859 * @since 1.0.0
860 */
861 public function show_excerpt_column()
862 {
863 $post_types = get_post_types( array(
864 'public' => true,
865 ), 'names' );
866 foreach ( $post_types as $post_type_key => $post_type_name ) {
867
868 if ( post_type_supports( $post_type_key, 'excerpt' ) ) {
869 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'add_excerpt_column' ] );
870 add_action(
871 "manage_{$post_type_name}_posts_custom_column",
872 [ $this, 'add_excerpt' ],
873 10,
874 2
875 );
876 }
877
878 }
879 }
880
881 /**
882 * Add a column called Featured Image as the first column
883 *
884 * @param mixed $columns
885 * @return void
886 * @since 1.0.0
887 */
888 public function add_excerpt_column( $columns )
889 {
890 $new_columns = array();
891 foreach ( $columns as $key => $value ) {
892 $new_columns[$key] = $value;
893 if ( $key == 'title' ) {
894 $new_columns['asenha-excerpt'] = 'Excerpt';
895 }
896 }
897 return $new_columns;
898 }
899
900 /**
901 * Echo featured image's in thumbnail size to a column
902 *
903 * @param mixed $column_name
904 * @param mixed $id
905 * @since 1.0.0
906 */
907 public function add_excerpt( $column_name, $id )
908 {
909
910 if ( 'asenha-excerpt' === $column_name ) {
911 $excerpt = get_the_excerpt( $id );
912 // about 310 characters
913 $excerpt = substr( $excerpt, 0, 160 );
914 // truncate to 160 characters
915 $short_excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
916 echo wp_kses_post( $short_excerpt ) ;
917 }
918
919 }
920
921 /**
922 * Add ID column list table of pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
923 *
924 * @since 1.0.0
925 */
926 public function show_id_column()
927 {
928 // For pages and hierarchical post types list table
929 add_filter( 'manage_pages_columns', [ $this, 'add_id_column' ] );
930 add_action(
931 'manage_pages_custom_column',
932 [ $this, 'add_id_echo_value' ],
933 10,
934 2
935 );
936 // For posts and non-hierarchical custom posts list table
937 add_filter( 'manage_posts_columns', [ $this, 'add_id_column' ] );
938 add_action(
939 'manage_posts_custom_column',
940 [ $this, 'add_id_echo_value' ],
941 10,
942 2
943 );
944 // For media list table
945 add_filter( 'manage_media_columns', [ $this, 'add_id_column' ] );
946 add_action(
947 'manage_media_custom_column',
948 [ $this, 'add_id_echo_value' ],
949 10,
950 2
951 );
952 // For list table of all taxonomies
953 $taxonomies = get_taxonomies( [
954 'public' => true,
955 ], 'names' );
956 foreach ( $taxonomies as $taxonomy ) {
957 add_filter( 'manage_edit-' . $taxonomy . '_columns', [ $this, 'add_id_column' ] );
958 add_action(
959 'manage_' . $taxonomy . '_custom_column',
960 [ $this, 'add_id_return_value' ],
961 10,
962 3
963 );
964 }
965 // For users list table
966 add_filter( 'manage_users_columns', [ $this, 'add_id_column' ] );
967 add_action(
968 'manage_users_custom_column',
969 [ $this, 'add_id_return_value' ],
970 10,
971 3
972 );
973 // For comments list table
974 add_filter( 'manage_edit-comments_columns', [ $this, 'add_id_column' ] );
975 add_action(
976 'manage_comments_custom_column',
977 [ $this, 'add_id_echo_value' ],
978 10,
979 3
980 );
981 }
982
983 /**
984 * Add a column called ID
985 *
986 * @param mixed $columns
987 * @return void
988 * @since 1.0.0
989 */
990 public function add_id_column( $columns )
991 {
992 $columns['asenha-id'] = 'ID';
993 return $columns;
994 }
995
996 /**
997 * Echo post ID value to a column
998 *
999 * @param mixed $column_name
1000 * @param mixed $id
1001 * @since 1.0.0
1002 */
1003 public function add_id_echo_value( $column_name, $id )
1004 {
1005 if ( 'asenha-id' === $column_name ) {
1006 echo esc_html( $id ) ;
1007 }
1008 }
1009
1010 /**
1011 * Return post ID value to a column
1012 *
1013 * @param mixed $value
1014 * @param mixed $column_name
1015 * @param mixed $id
1016 * @since 1.0.0
1017 */
1018 public function add_id_return_value( $value, $column_name, $id )
1019 {
1020 if ( 'asenha-id' === $column_name ) {
1021 $value = $id;
1022 }
1023 return $value;
1024 }
1025
1026 /**
1027 * Add ID in the action row of list tables for pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments
1028 *
1029 * @since 4.7.4
1030 */
1031 public function show_id_in_action_row()
1032 {
1033 add_filter(
1034 'page_row_actions',
1035 array( $this, 'add_id_in_action_row' ),
1036 10,
1037 2
1038 );
1039 add_filter(
1040 'post_row_actions',
1041 array( $this, 'add_id_in_action_row' ),
1042 10,
1043 2
1044 );
1045 add_filter(
1046 'cat_row_actions',
1047 array( $this, 'add_id_in_action_row' ),
1048 10,
1049 2
1050 );
1051 add_filter(
1052 'tag_row_actions',
1053 array( $this, 'add_id_in_action_row' ),
1054 10,
1055 2
1056 );
1057 add_filter(
1058 'media_row_actions',
1059 array( $this, 'add_id_in_action_row' ),
1060 10,
1061 2
1062 );
1063 add_filter(
1064 'comment_row_actions',
1065 array( $this, 'add_id_in_action_row' ),
1066 10,
1067 2
1068 );
1069 add_filter(
1070 'user_row_actions',
1071 array( $this, 'add_id_in_action_row' ),
1072 10,
1073 2
1074 );
1075 }
1076
1077 /**
1078 * Output the ID in the action row
1079 *
1080 * @since 4.7.4
1081 */
1082 public function add_id_in_action_row( $actions, $object )
1083 {
1084
1085 if ( current_user_can( 'edit_posts' ) ) {
1086 // For pages, posts, custom post types, media/attachments, users
1087 if ( property_exists( $object, 'ID' ) ) {
1088 $id = $object->ID;
1089 }
1090 // For taxonomies
1091 if ( property_exists( $object, 'term_id' ) ) {
1092 $id = $object->term_id;
1093 }
1094 // For comments
1095 if ( property_exists( $object, 'comment_ID' ) ) {
1096 $id = $object->comment_ID;
1097 }
1098 $actions['asenha-list-table-item-id'] = '<span class="asenha-list-table-item-id">ID: ' . $id . '</span>';
1099 }
1100
1101 return $actions;
1102 }
1103
1104 /**
1105 * Hide comments column in list tables for pages, post types that support comments, and alse media/attachments.
1106 *
1107 * @since 1.0.0
1108 */
1109 public function hide_comments_column()
1110 {
1111 $post_types = get_post_types( array(
1112 'public' => true,
1113 ), 'names' );
1114 foreach ( $post_types as $post_type_key => $post_type_name ) {
1115 if ( post_type_supports( $post_type_key, 'comments' ) ) {
1116
1117 if ( 'attachment' != $post_type_name ) {
1118 // For list tables of pages, posts and other post types
1119 add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'remove_comment_column' ] );
1120 } else {
1121 // For list table of media/attachment
1122 add_filter( 'manage_media_columns', [ $this, 'remove_comment_column' ] );
1123 }
1124
1125 }
1126 }
1127 }
1128
1129 /**
1130 * Add a column called ID
1131 *
1132 * @param mixed $columns
1133 * @return void
1134 * @since 1.0.0
1135 */
1136 public function remove_comment_column( $columns )
1137 {
1138 unset( $columns['comments'] );
1139 return $columns;
1140 }
1141
1142 /**
1143 * Hide tags column in list tables for posts.
1144 *
1145 * @since 1.0.0
1146 */
1147 public function hide_post_tags_column()
1148 {
1149 $post_types = get_post_types( array(
1150 'public' => true,
1151 ), 'names' );
1152 foreach ( $post_types as $post_type_key => $post_type_name ) {
1153 if ( $post_type_name == 'post' ) {
1154 add_filter( "manage_posts_columns", [ $this, 'remove_post_tags_column' ] );
1155 }
1156 }
1157 }
1158
1159 /**
1160 * Add a column called ID
1161 *
1162 * @param mixed $columns
1163 * @return void
1164 * @since 1.0.0
1165 */
1166 public function remove_post_tags_column( $columns )
1167 {
1168 unset( $columns['tags'] );
1169 return $columns;
1170 }
1171
1172 /**
1173 * Show custom (hierarchical) taxonomy filter(s) for all post types.
1174 *
1175 * @since 1.0.0
1176 */
1177 public function show_custom_taxonomy_filters( $post_type )
1178 {
1179 $post_taxonomies = get_object_taxonomies( $post_type, 'objects' );
1180 // Only show custom taxonomy filters for post types other than 'post'
1181 if ( 'post' != $post_type ) {
1182 array_walk( $post_taxonomies, [ $this, 'output_taxonomy_filter' ] );
1183 }
1184 }
1185
1186 /**
1187 * Output filter on the post type's list table for a taxonomy
1188 *
1189 * @since 1.0.0
1190 */
1191 public function output_taxonomy_filter( $post_taxonomy )
1192 {
1193 // Only show taxonomy filter when the taxonomy is hierarchical
1194 if ( true === $post_taxonomy->hierarchical ) {
1195 wp_dropdown_categories( array(
1196 'show_option_all' => sprintf( 'All %s', $post_taxonomy->label ),
1197 'orderby' => 'name',
1198 'order' => 'ASC',
1199 'hide_empty' => false,
1200 'hide_if_empty' => true,
1201 'selected' => sanitize_text_field( $_GET[$post_taxonomy->query_var] ),
1202 'hierarchical' => true,
1203 'name' => $post_taxonomy->query_var,
1204 'taxonomy' => $post_taxonomy->name,
1205 'value_field' => 'slug',
1206 ) );
1207 }
1208 }
1209
1210 }