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