PluginProbe
Admin and Site Enhancements (ASE) / 6.2.5
Admin and Site Enhancements (ASE) v6.2.5
9.1.1 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 All 273 releases
admin-site-enhancements / bootstrap.php
bootstrap.php
861 lines 41.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // We're using the singleton design pattern
4 // https://code.tutsplus.com/articles/design-patterns-in-wordpress-the-singleton-pattern--wp-31621
5 // https://carlalexander.ca/singletons-in-wordpress/
6 // https://torquemag.io/2016/11/singletons-wordpress-good-evil/
7 /**
8 * Main class of the plugin used to add functionalities
9 *
10 * @since 1.0.0
11 */
12 class Admin_Site_Enhancements
13 {
14 // Refers to a single instance of this class
15 private static $instance = null ;
16 /**
17 * Creates or returns a single instance of this class
18 *
19 * @return Admin_Site_Enhancements a single instance of this class
20 * @since 1.0.0
21 */
22 public static function get_instance()
23 {
24 if ( null == self::$instance ) {
25 self::$instance = new self();
26 }
27 return self::$instance;
28 }
29
30 /**
31 * Initialize plugin functionalities
32 */
33 private function __construct()
34 {
35 global $pagenow ;
36 // Setup admin menu, admin page, settings, settings sections, sections fields, admin scripts, plugin action links, etc.
37 // Register admin menu and add the settings page.
38 add_action( 'admin_menu', 'asenha_register_admin_menu' );
39 // Register plugin settings
40 // Instantiate object for registration of settings section and fields
41 $settings = new ASENHA\Classes\Settings_Sections_Fields();
42 add_action( 'admin_init', [ $settings, 'register_sections_fields' ] );
43 // Suppress all notices on the plugin's main page. Then add notification for successful settings update.
44 add_action( 'admin_notices', 'asenha_suppress_add_notices', 5 );
45 add_action( 'all_admin_notices', 'asenha_suppress_generic_notices', 5 );
46 // Enqueue admin scripts and styles
47 add_action( 'admin_enqueue_scripts', 'asenha_admin_scripts' );
48 // Enqueue public scripts and styles
49 add_action( 'wp_enqueue_scripts', 'asenha_public_scripts' );
50 // Add admin bar inline styles
51 add_action( 'admin_head', 'asenha_admin_bar_item_js_css' );
52 add_action( 'wp_head', 'asenha_admin_bar_item_js_css' );
53 add_filter( 'plugin_action_links_' . ASENHA_SLUG . '/' . ASENHA_SLUG . '.php', 'asenha_plugin_action_links' );
54 // Update footer text
55 add_filter( 'admin_footer_text', 'asenha_footer_text', 20 );
56 // Update footer version text
57 if ( is_asenha() ) {
58 add_filter( 'update_footer', 'asenha_footer_version_text', 20 );
59 }
60 // Mark that a user have sponsored ASE (via AJAX)
61 add_action( 'wp_ajax_have_sponsored', 'asenha_have_sponsored' );
62 // Dismiss upgrade nudge (via AJAX)
63 add_action( 'wp_ajax_dismiss_upgrade_nudge', 'asenha_dismiss_upgrade_nudge' );
64 // Dismiss sponsorship nudge (via AJAX)
65 add_action( 'wp_ajax_dismiss_sponsorship_nudge', 'asenha_dismiss_sponsorship_nudge' );
66 if ( function_exists( 'bwasenha_fs' ) ) {
67 bwasenha_fs()->add_filter( 'plugin_icon', 'fs_custom_optin_icon__premium_only' );
68 }
69 // ===== Activate features based on settings =====
70 // Get all WP Enhancements options, default to empty array in case it's not been created yet
71 $options = get_option( ASENHA_SLUG_U, array() );
72 // =================================================================
73 // CONTENT MANAGEMENT
74 // =================================================================
75 // Instantiate object for Content Management features
76 $content_management = new ASENHA\Classes\Content_Management();
77 // Content Duplication
78
79 if ( array_key_exists( 'enable_duplication', $options ) && $options['enable_duplication'] ) {
80 add_action( 'admin_action_asenha_enable_duplication', [ $content_management, 'asenha_enable_duplication' ] );
81 add_filter(
82 'page_row_actions',
83 [ $content_management, 'add_duplication_action_link' ],
84 10,
85 2
86 );
87 add_filter(
88 'post_row_actions',
89 [ $content_management, 'add_duplication_action_link' ],
90 10,
91 2
92 );
93 }
94
95 // Content Order
96 if ( array_key_exists( 'content_order', $options ) && $options['content_order'] ) {
97
98 if ( array_key_exists( 'content_order_for', $options ) && !empty($options['content_order_for']) ) {
99 add_action( 'admin_menu', [ $content_management, 'add_content_order_submenu' ] );
100 add_action( 'wp_ajax_save_custom_order', [ $content_management, 'save_custom_content_order' ] );
101 add_filter( 'pre_get_posts', [ $content_management, 'orderby_menu_order' ] );
102 add_filter(
103 'save_post',
104 [ $content_management, 'set_menu_order_for_new_posts' ],
105 10,
106 3
107 );
108 }
109
110 }
111 // Media Replacement
112
113 if ( array_key_exists( 'enable_media_replacement', $options ) && $options['enable_media_replacement'] ) {
114 add_filter(
115 'media_row_actions',
116 [ $content_management, 'modify_media_list_table_edit_link' ],
117 10,
118 2
119 );
120 add_filter(
121 'attachment_fields_to_edit',
122 [ $content_management, 'add_media_replacement_button' ],
123 10,
124 2
125 );
126 add_action( 'edit_attachment', [ $content_management, 'replace_media' ] );
127 add_filter( 'post_updated_messages', [ $content_management, 'attachment_updated_custom_message' ] );
128 // Bust browser cache of old/replaced images
129 add_filter(
130 'wp_calculate_image_srcset',
131 [ $content_management, 'append_cache_busting_param_to_image_srcset' ],
132 10,
133 5
134 );
135 add_filter(
136 'wp_get_attachment_image_src',
137 [ $content_management, 'append_cache_busting_param_to_attachment_image_src' ],
138 10,
139 2
140 );
141 add_filter(
142 'wp_prepare_attachment_for_js',
143 [ $content_management, 'append_cache_busting_param_to_attachment_for_js' ],
144 10,
145 2
146 );
147 }
148
149 // Enable SVG Upload
150
151 if ( array_key_exists( 'enable_svg_upload', $options ) && $options['enable_svg_upload'] && array_key_exists( 'enable_svg_upload_for', $options ) && isset( $options['enable_svg_upload_for'] ) ) {
152 global $roles_svg_upload_enabled ;
153 $enable_svg_upload = $options['enable_svg_upload'];
154 $for_roles = $options['enable_svg_upload_for'];
155 // User has role(s). Do further checks.
156
157 if ( isset( $for_roles ) && count( $for_roles ) > 0 ) {
158 // Assemble single-dimensional array of roles for which SVG upload would be enabled
159 $roles_svg_upload_enabled = array();
160 foreach ( $for_roles as $role_slug => $svg_upload_enabled ) {
161 if ( $svg_upload_enabled ) {
162 $roles_svg_upload_enabled[] = $role_slug;
163 }
164 }
165 }
166
167 add_filter( 'upload_mimes', [ $content_management, 'add_svg_mime' ] );
168 add_filter(
169 'wp_check_filetype_and_ext',
170 [ $content_management, 'confirm_file_type_is_svg' ],
171 10,
172 4
173 );
174 add_filter( 'wp_handle_upload_prefilter', [ $content_management, 'sanitize_and_maybe_allow_svg_upload' ] );
175 add_filter(
176 'wp_generate_attachment_metadata',
177 [ $content_management, 'generate_svg_metadata' ],
178 10,
179 3
180 );
181 add_action( 'wp_ajax_svg_get_attachment_url', [ $content_management, 'get_svg_attachment_url' ] );
182 add_filter( 'wp_prepare_attachment_for_js', [ $content_management, 'get_svg_url_in_media_library' ] );
183 }
184
185 // Enable External Permalinks
186 if ( array_key_exists( 'enable_external_permalinks', $options ) && $options['enable_external_permalinks'] ) {
187
188 if ( array_key_exists( 'enable_external_permalinks_for', $options ) && !empty($options['enable_external_permalinks_for']) ) {
189 add_action(
190 'add_meta_boxes',
191 [ $content_management, 'add_external_permalink_meta_box' ],
192 10,
193 2
194 );
195 add_action( 'save_post', [ $content_management, 'save_external_permalink' ] );
196 // Filter the permalink for use by get_permalink()
197 add_filter(
198 'page_link',
199 [ $content_management, 'use_external_permalink_for_pages' ],
200 20,
201 2
202 );
203 add_filter(
204 'post_link',
205 [ $content_management, 'use_external_permalink_for_posts' ],
206 20,
207 2
208 );
209 add_filter(
210 'post_type_link',
211 [ $content_management, 'use_external_permalink_for_posts' ],
212 20,
213 2
214 );
215 // Enable redirection to external permalink when page/post is opened directly via it's WP permalink
216 add_action( 'wp', [ $content_management, 'redirect_to_external_permalink' ] );
217 }
218
219 }
220 // Open All External Links in New Tab
221 if ( array_key_exists( 'external_links_new_tab', $options ) && $options['external_links_new_tab'] ) {
222 add_filter( 'the_content', [ $content_management, 'add_target_and_rel_atts_to_content_links' ] );
223 }
224 // Allow Custom Menu Links to Open in New Tab
225
226 if ( array_key_exists( 'custom_nav_menu_items_new_tab', $options ) && $options['custom_nav_menu_items_new_tab'] ) {
227 add_filter(
228 'wp_nav_menu_item_custom_fields',
229 [ $content_management, 'add_custom_nav_menu_open_in_new_tab_field' ],
230 10,
231 4
232 );
233 add_action(
234 'wp_update_nav_menu_item',
235 [ $content_management, 'save_custom_nav_menu_open_in_new_tab_status' ],
236 10,
237 3
238 );
239 add_action(
240 'nav_menu_link_attributes',
241 [ $content_management, 'add_attributes_to_custom_nav_menu_item' ],
242 10,
243 3
244 );
245 }
246
247 // Enable Auto-Publishing of Posts with Missed Schedules
248
249 if ( array_key_exists( 'enable_missed_schedule_posts_auto_publish', $options ) && $options['enable_missed_schedule_posts_auto_publish'] ) {
250 add_action( 'wp_head', [ $content_management, 'publish_missed_schedule_posts' ] );
251 add_action( 'admin_head', [ $content_management, 'publish_missed_schedule_posts' ] );
252 }
253
254 // =================================================================
255 // ADMIN INTERFACE
256 // =================================================================
257 // Instantiate object for Admin Interface features
258 $admin_interface = new ASENHA\Classes\Admin_Interface();
259 // Hide or Modify Elements / Clean Up Admin Bar
260
261 if ( array_key_exists( 'hide_modify_elements', $options ) && $options['hide_modify_elements'] ) {
262 // Priority 5 to execute earlier than the normal 10. This is for removing default items.
263 add_filter( 'admin_bar_menu', [ $admin_interface, 'modify_admin_bar_menu' ], 5 );
264 if ( array_key_exists( 'hide_help_drawer', $options ) && $options['hide_help_drawer'] ) {
265 add_action( 'admin_head', [ $admin_interface, 'hide_help_drawer' ] );
266 }
267 }
268
269 // Hide Admin Notices
270
271 if ( array_key_exists( 'hide_admin_notices', $options ) && $options['hide_admin_notices'] ) {
272 add_action( 'admin_notices', [ $admin_interface, 'admin_notices_wrapper' ], 9 );
273 // add_action( 'all_admin_notices', [ $admin_interface, 'admin_notices_wrapper' ] );
274 add_action( 'admin_bar_menu', [ $admin_interface, 'admin_notices_menu' ] );
275 add_action( 'admin_enqueue_scripts', [ $admin_interface, 'admin_notices_menu_inline_css' ] );
276 // wp-admin
277 }
278
279 // Disable Dashboard Widgets
280 if ( array_key_exists( 'disable_dashboard_widgets', $options ) && $options['disable_dashboard_widgets'] ) {
281 add_action( 'wp_dashboard_setup', [ $admin_interface, 'disable_dashboard_widgets' ], 99 );
282 }
283 // Hide Admin Bar
284 // On the frontend
285 if ( array_key_exists( 'hide_admin_bar', $options ) && $options['hide_admin_bar'] && array_key_exists( 'hide_admin_bar_for', $options ) && isset( $options['hide_admin_bar_for'] ) ) {
286 add_filter( 'show_admin_bar', [ $admin_interface, 'hide_admin_bar_for_roles_on_frontend' ] );
287 }
288 // Wider Admin Menu
289 if ( array_key_exists( 'wider_admin_menu', $options ) && $options['wider_admin_menu'] ) {
290 add_action( 'admin_head', [ $admin_interface, 'set_custom_menu_width' ], 99 );
291 }
292 // Admin Menu Organizer
293
294 if ( array_key_exists( 'customize_admin_menu', $options ) && $options['customize_admin_menu'] ) {
295 // add_action( 'wp_ajax_save_custom_menu_order', [ $admin_interface, 'save_custom_menu_order' ] );
296 // add_action( 'wp_ajax_save_hidden_menu_items', [ $admin_interface, 'save_hidden_menu_items' ] );
297
298 if ( array_key_exists( 'custom_menu_order', $options ) ) {
299 add_filter( 'custom_menu_order', '__return_true' );
300 add_filter( 'menu_order', [ $admin_interface, 'render_custom_menu_order' ], PHP_INT_MAX );
301 }
302
303 if ( array_key_exists( 'custom_menu_titles', $options ) ) {
304 add_action( 'admin_menu', [ $admin_interface, 'apply_custom_menu_item_titles' ], 999995 );
305 }
306
307 if ( array_key_exists( 'custom_menu_hidden', $options ) || array_key_exists( 'custom_menu_always_hidden', $options ) ) {
308 add_action( 'admin_menu', [ $admin_interface, 'hide_menu_items' ], 999996 );
309 add_action( 'admin_menu', [ $admin_interface, 'add_hidden_menu_toggle' ], 999997 );
310 add_action( 'admin_enqueue_scripts', [ $admin_interface, 'enqueue_toggle_hidden_menu_script' ] );
311 }
312
313 }
314
315 // Enhance List Tables
316
317 if ( array_key_exists( 'enhance_list_tables', $options ) && $options['enhance_list_tables'] ) {
318 // Show Featured Image Column
319 if ( array_key_exists( 'show_featured_image_column', $options ) && $options['show_featured_image_column'] ) {
320 add_action( 'admin_init', [ $admin_interface, 'show_featured_image_column' ] );
321 }
322 // Show Excerpt Column
323 if ( array_key_exists( 'show_excerpt_column', $options ) && $options['show_excerpt_column'] ) {
324 add_action( 'admin_init', [ $admin_interface, 'show_excerpt_column' ] );
325 }
326 // Show ID Column
327 if ( array_key_exists( 'show_id_column', $options ) && $options['show_id_column'] ) {
328 add_action( 'admin_init', [ $admin_interface, 'show_id_column' ] );
329 }
330 // Show ID in Action Row
331 if ( array_key_exists( 'show_id_in_action_row', $options ) && $options['show_id_in_action_row'] ) {
332 add_action( 'admin_init', [ $admin_interface, 'show_id_in_action_row' ] );
333 }
334 // Show Custom Taxonomy Filters
335 if ( array_key_exists( 'show_custom_taxonomy_filters', $options ) && $options['show_custom_taxonomy_filters'] ) {
336 add_action( 'restrict_manage_posts', [ $admin_interface, 'show_custom_taxonomy_filters' ] );
337 }
338 // Hide Comments Column
339 if ( array_key_exists( 'hide_comments_column', $options ) && $options['hide_comments_column'] ) {
340 add_action( 'admin_init', [ $admin_interface, 'hide_comments_column' ] );
341 }
342 // Hide Post Tags Column
343 if ( array_key_exists( 'hide_post_tags_column', $options ) && $options['hide_post_tags_column'] ) {
344 add_action( 'admin_init', [ $admin_interface, 'hide_post_tags_column' ] );
345 }
346 }
347
348 // =================================================================
349 // LOG IN | LOG OUT
350 // =================================================================
351 // Instantiate object for Log In Log Out features
352 $login_logout = new ASENHA\Classes\Login_Logout();
353 // Change Login URL
354 if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
355
356 if ( array_key_exists( 'custom_login_slug', $options ) && !empty($options['custom_login_slug']) ) {
357 add_action( 'init', [ $login_logout, 'redirect_on_custom_login_url' ] );
358 add_filter( 'lostpassword_url', [ $login_logout, 'customize_lost_password_url' ] );
359 add_filter( 'register_url', [ $login_logout, 'customize_register_url' ] );
360 add_action( 'wp_loaded', [ $login_logout, 'redirect_on_default_login_urls' ] );
361 add_action( 'wp_login_failed', [ $login_logout, 'redirect_to_custom_login_url_on_login_fail' ] );
362 add_action( 'wp_logout', [ $login_logout, 'redirect_to_custom_login_url_on_logout_success' ] );
363 }
364
365 }
366 // Use Site Identity on the Login Page
367
368 if ( array_key_exists( 'site_identity_on_login', $options ) && $options['site_identity_on_login'] ) {
369 add_action( 'login_head', [ $login_logout, 'use_site_icon_on_login' ] );
370 add_filter( 'login_headerurl', [ $login_logout, 'use_site_url_on_login' ] );
371 }
372
373 // Enable Login Logout Menu
374
375 if ( array_key_exists( 'enable_login_logout_menu', $options ) && $options['enable_login_logout_menu'] ) {
376 add_action( 'admin_head-nav-menus.php', [ $login_logout, 'add_login_logout_metabox' ] );
377 add_filter( 'wp_setup_nav_menu_item', [ $login_logout, 'set_login_logout_menu_item_dynamic_url' ] );
378 add_filter( 'wp_nav_menu_objects', [ $login_logout, 'maybe_remove_login_or_logout_menu_item' ] );
379 }
380
381 // Enable Last Login Column
382
383 if ( array_key_exists( 'enable_last_login_column', $options ) && $options['enable_last_login_column'] ) {
384 add_action( 'wp_login', [ $login_logout, 'log_login_datetime' ] );
385 add_filter( 'manage_users_columns', [ $login_logout, 'add_last_login_column' ] );
386 add_filter(
387 'manage_users_custom_column',
388 [ $login_logout, 'show_last_login_info' ],
389 10,
390 3
391 );
392 add_action( 'admin_print_styles-users.php', [ $login_logout, 'add_column_style' ] );
393 }
394
395 // Redirect After Login
396 if ( array_key_exists( 'redirect_after_login', $options ) && $options['redirect_after_login'] ) {
397 if ( array_key_exists( 'redirect_after_login_to_slug', $options ) && !empty($options['redirect_after_login_to_slug']) ) {
398 if ( array_key_exists( 'redirect_after_login_for', $options ) && !empty($options['redirect_after_login_for']) ) {
399 add_filter(
400 'wp_login',
401 [ $login_logout, 'redirect_for_roles_after_login' ],
402 5,
403 2
404 );
405 }
406 }
407 }
408 // Redirect After Logout
409 if ( array_key_exists( 'redirect_after_logout', $options ) && $options['redirect_after_logout'] ) {
410 if ( array_key_exists( 'redirect_after_logout_to_slug', $options ) && !empty($options['redirect_after_logout_to_slug']) ) {
411
412 if ( array_key_exists( 'redirect_after_logout_for', $options ) && !empty($options['redirect_after_logout_for']) ) {
413 add_action(
414 'wp_logout',
415 [ $login_logout, 'redirect_after_logout' ],
416 5,
417 1
418 );
419 // load earlier than Change Login URL add_action
420 }
421
422 }
423 }
424 // =================================================================
425 // CUSTOM CODE
426 // =================================================================
427 // Instantiate object for Custom Code features
428 $custom_code = new ASENHA\Classes\Custom_Code();
429 // Enable Custom Admin / Frontend CSS
430 if ( array_key_exists( 'enable_custom_admin_css', $options ) && $options['enable_custom_admin_css'] ) {
431 add_filter( 'admin_enqueue_scripts', [ $custom_code, 'custom_admin_css' ] );
432 }
433 if ( array_key_exists( 'enable_custom_frontend_css', $options ) && $options['enable_custom_frontend_css'] ) {
434 add_filter( 'wp_head', [ $custom_code, 'custom_frontend_css' ] );
435 }
436 // Custom Body Class
437 if ( array_key_exists( 'enable_custom_body_class', $options ) && $options['enable_custom_body_class'] ) {
438
439 if ( array_key_exists( 'enable_custom_body_class_for', $options ) && !empty($options['enable_custom_body_class_for']) ) {
440 add_action(
441 'add_meta_boxes',
442 [ $custom_code, 'add_custom_body_class_meta_box' ],
443 10,
444 2
445 );
446 add_action( 'save_post', [ $custom_code, 'save_custom_body_class' ], 99 );
447 add_filter( 'body_class', [ $custom_code, 'append_custom_body_class' ], 99 );
448 }
449
450 }
451 // Manage ads.txt and app-ads.txt
452 if ( array_key_exists( 'manage_ads_appads_txt', $options ) && $options['manage_ads_appads_txt'] ) {
453 add_action( 'init', [ $custom_code, 'show_ads_appads_txt_content' ] );
454 }
455 // Manage robots.txt
456 if ( array_key_exists( 'manage_robots_txt', $options ) && $options['manage_robots_txt'] ) {
457 add_filter(
458 'robots_txt',
459 [ $custom_code, 'maybe_show_custom_robots_txt_content' ],
460 10,
461 2
462 );
463 }
464 // Insert <head>, <body> and <footer> code
465
466 if ( array_key_exists( 'insert_head_body_footer_code', $options ) && $options['insert_head_body_footer_code'] ) {
467
468 if ( isset( $options['head_code_priority'] ) ) {
469 add_action( 'wp_head', [ $custom_code, 'insert_head_code' ], $options['head_code_priority'] );
470 } else {
471 add_action( 'wp_head', [ $custom_code, 'insert_head_code' ], 10 );
472 }
473
474 if ( function_exists( 'wp_body_open' ) && version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ) {
475
476 if ( isset( $options['body_code_priority'] ) ) {
477 add_action( 'wp_body_open', [ $custom_code, 'insert_body_code' ], $options['body_code_priority'] );
478 } else {
479 add_action( 'wp_body_open', [ $custom_code, 'insert_body_code' ], 10 );
480 }
481
482 }
483
484 if ( isset( $options['footer_code_priority'] ) ) {
485 add_action( 'wp_footer', [ $custom_code, 'insert_footer_code' ], $options['footer_code_priority'] );
486 } else {
487 add_action( 'wp_footer', [ $custom_code, 'insert_footer_code' ], 10 );
488 }
489
490 }
491
492 // =================================================================
493 // DISABLE COMPONENTS
494 // =================================================================
495 // Instantiate object for Disable Components features
496 $disable_components = new ASENHA\Classes\Disable_Components();
497 // Disable Gutenberg
498 if ( array_key_exists( 'disable_gutenberg', $options ) && $options['disable_gutenberg'] ) {
499
500 if ( array_key_exists( 'disable_gutenberg_for', $options ) && !empty($options['disable_gutenberg_for']) ) {
501 add_action( 'admin_init', [ $disable_components, 'disable_gutenberg_for_post_types_admin' ] );
502 if ( array_key_exists( 'disable_gutenberg_frontend_styles', $options ) && $options['disable_gutenberg_frontend_styles'] ) {
503 add_action( 'wp_enqueue_scripts', [ $disable_components, 'disable_gutenberg_for_post_types_frontend' ], 100 );
504 }
505 }
506
507 }
508 // Disable Block-Based Widgets Screen
509
510 if ( array_key_exists( 'disable_block_widgets', $options ) && $options['disable_block_widgets'] ) {
511 // Disables the block editor from managing widgets in the Gutenberg plugin.
512 add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
513 // Disables the block editor from managing widgets.
514 add_filter( 'use_widgets_block_editor', '__return_false' );
515 }
516
517 // Disable Comments
518 if ( array_key_exists( 'disable_comments', $options ) && $options['disable_comments'] ) {
519
520 if ( array_key_exists( 'disable_comments_for', $options ) && !empty($options['disable_comments_for']) ) {
521 add_action( 'admin_init', [ $disable_components, 'disable_comments_for_post_types_edit' ] );
522 // also work with 'init', 'admin_init', 'wp_loaded', 'do_meta_boxes' hooks
523 add_action( 'template_redirect', [ $disable_components, 'show_blank_comment_template' ] );
524 add_action( 'wp_loaded', [ $disable_components, 'hide_existing_comments_on_frontend' ] );
525 // hide comments
526 add_filter(
527 'comments_open',
528 [ $disable_components, 'close_comments_pings_on_frontend' ],
529 20,
530 2
531 );
532 // close commenting
533 add_filter(
534 'pings_open',
535 [ $disable_components, 'close_comments_pings_on_frontend' ],
536 20,
537 2
538 );
539 // close commenting
540 add_filter( 'xmlrpc_allow_anonymous_comments', '__return_false' );
541 }
542
543 }
544 // Disable REST API
545
546 if ( array_key_exists( 'disable_rest_api', $options ) && $options['disable_rest_api'] ) {
547
548 if ( version_compare( get_bloginfo( 'version' ), '4.7', '>=' ) ) {
549 add_filter( 'rest_authentication_errors', [ $disable_components, 'disable_rest_api' ] );
550 } else {
551 // REST API 1.x
552 add_filter( 'json_enabled', '__return_false' );
553 add_filter( 'json_jsonp_enabled', '__return_false' );
554 // REST API 2.x
555 add_filter( 'rest_enabled', '__return_false' );
556 add_filter( 'rest_jsonp_enabled', '__return_false' );
557 }
558
559 remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
560 // Disable REST API links in HTML <head>
561 remove_action(
562 'template_redirect',
563 'rest_output_link_header',
564 11,
565 0
566 );
567 // Disable REST API link in HTTP headers
568 remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
569 // Remove REST API URL from the WP RSD endpoint.
570 }
571
572 // Disable Feeds
573
574 if ( array_key_exists( 'disable_feeds', $options ) && $options['disable_feeds'] ) {
575 remove_action( 'wp_head', 'feed_links', 2 );
576 // Remove feed links in <head>
577 remove_action( 'wp_head', 'feed_links_extra', 3 );
578 // Remove feed links in <head>
579 remove_action(
580 'do_feed_rdf',
581 'do_feed_rdf',
582 10,
583 0
584 );
585 remove_action(
586 'do_feed_rss',
587 'do_feed_rss',
588 10,
589 0
590 );
591 remove_action(
592 'do_feed_rss2',
593 'do_feed_rss2',
594 10,
595 1
596 );
597 remove_action(
598 'do_feed_atom',
599 'do_feed_atom',
600 10,
601 1
602 );
603 add_action(
604 'template_redirect',
605 [ $disable_components, 'redirect_feed_to_403' ],
606 10,
607 1
608 );
609 }
610
611 // Disable All Updates
612
613 if ( array_key_exists( 'disable_all_updates', $options ) && $options['disable_all_updates'] ) {
614 add_action( 'admin_init', [ $disable_components, 'disable_update_notices_version_checks' ] );
615 // Disable core update
616 add_filter( 'pre_transient_update_core', [ $disable_components, 'override_version_check_info' ] );
617 add_filter( 'pre_site_transient_update_core', [ $disable_components, 'override_version_check_info' ] );
618 // Disable theme updates
619 add_filter( 'pre_transient_update_themes', [ $disable_components, 'override_version_check_info' ] );
620 add_filter( 'pre_site_transient_update_themes', [ $disable_components, 'override_version_check_info' ] );
621 add_action( 'pre_set_site_transient_update_themes', [ $disable_components, 'override_version_check_info' ], 20 );
622 // Disable plugin updates
623 add_filter( 'pre_transient_update_plugins', [ $disable_components, 'override_version_check_info' ] );
624 add_filter( 'pre_site_transient_update_plugins', [ $disable_components, 'override_version_check_info' ] );
625 add_action( 'pre_set_site_transient_update_plugins', [ $disable_components, 'override_version_check_info' ], 20 );
626 // Disable auto updates
627 add_filter( 'automatic_updater_disabled', '__return_true' );
628 if ( !defined( 'AUTOMATIC_UPDATER_DISABLED' ) ) {
629 define( 'AUTOMATIC_UPDATER_DISABLED', true );
630 }
631 if ( !defined( 'WP_AUTO_UPDATE_CORE' ) ) {
632 define( 'WP_AUTO_UPDATE_CORE', false );
633 }
634 add_filter( 'auto_update_core', '__return_false' );
635 add_filter( 'wp_auto_update_core', '__return_false' );
636 add_filter( 'allow_minor_auto_core_updates', '__return_false' );
637 add_filter( 'allow_major_auto_core_updates', '__return_false' );
638 add_filter( 'allow_dev_auto_core_updates', '__return_false' );
639 add_filter( 'auto_update_plugin', '__return_false' );
640 add_filter( 'auto_update_theme', '__return_false' );
641 add_filter( 'auto_update_translation', '__return_false' );
642 remove_action( 'init', 'wp_schedule_update_checks' );
643 // Disable update emails
644 add_filter( 'auto_core_update_send_email', '__return_false' );
645 add_filter( 'send_core_update_notification_email', '__return_false' );
646 add_filter( 'automatic_updates_send_debug_email', '__return_false' );
647 // Remove Dashboard >> Updates menu
648 add_action( 'admin_menu', [ $disable_components, 'remove_updates_menu' ] );
649 }
650
651 // Disable Smaller Components
652
653 if ( array_key_exists( 'disable_smaller_components', $options ) && $options['disable_smaller_components'] ) {
654 if ( array_key_exists( 'disable_head_generator_tag', $options ) && $options['disable_head_generator_tag'] ) {
655 remove_action( 'wp_head', 'wp_generator' );
656 }
657
658 if ( array_key_exists( 'disable_resource_version_number', $options ) && $options['disable_resource_version_number'] ) {
659 add_filter( 'style_loader_src', [ $disable_components, 'remove_resource_version_number' ], PHP_INT_MAX );
660 add_filter( 'script_loader_src', [ $disable_components, 'remove_resource_version_number' ], PHP_INT_MAX );
661 }
662
663 if ( array_key_exists( 'disable_head_wlwmanifest_tag', $options ) && $options['disable_head_wlwmanifest_tag'] ) {
664 remove_action( 'wp_head', 'wlwmanifest_link' );
665 }
666 if ( array_key_exists( 'disable_head_rsd_tag', $options ) && $options['disable_head_rsd_tag'] ) {
667 remove_action( 'wp_head', 'rsd_link' );
668 }
669
670 if ( array_key_exists( 'disable_head_shortlink_tag', $options ) && $options['disable_head_shortlink_tag'] ) {
671 remove_action( 'wp_head', 'wp_shortlink_wp_head' );
672 remove_action(
673 'template_redirect',
674 'wp_shortlink_header',
675 100,
676 0
677 );
678 }
679
680 if ( array_key_exists( 'disable_frontend_dashicons', $options ) && $options['disable_frontend_dashicons'] ) {
681 add_action( 'init', [ $disable_components, 'disable_dashicons_public_assets' ] );
682 }
683 if ( array_key_exists( 'disable_emoji_support', $options ) && $options['disable_emoji_support'] ) {
684 add_action( 'init', [ $disable_components, 'disable_emoji_support' ] );
685 }
686 if ( array_key_exists( 'disable_jquery_migrate', $options ) && $options['disable_jquery_migrate'] ) {
687 add_action( 'wp_default_scripts', [ $disable_components, 'disable_jquery_migrate' ] );
688 }
689 }
690
691 // =================================================================
692 // SECURITY
693 // =================================================================
694 // Instantiate object for Security features
695 $security = new ASENHA\Classes\Security();
696 // Limit Login Attempts
697
698 if ( array_key_exists( 'limit_login_attempts', $options ) && $options['limit_login_attempts'] ) {
699 add_filter(
700 'authenticate',
701 [ $security, 'maybe_allow_login' ],
702 999,
703 3
704 );
705 // Very low priority so it is processed last
706 add_action(
707 'wp_login_errors',
708 [ $security, 'login_error_handler' ],
709 999,
710 2
711 );
712 add_action( 'login_enqueue_scripts', [ $security, 'maybe_hide_login_form' ] );
713 add_filter( 'login_message', [ $security, 'add_failed_login_message' ] );
714 add_action( 'wp_login_failed', [ $security, 'log_failed_login' ], 5 );
715 // Higher priority than one in Change Login URL
716 add_action( 'wp_login', [ $security, 'clear_failed_login_log' ] );
717 }
718
719 // Obfuscate Author Slugs
720
721 if ( array_key_exists( 'obfuscate_author_slugs', $options ) && $options['obfuscate_author_slugs'] ) {
722 add_action( 'pre_get_posts', [ $security, 'alter_author_query' ], 10 );
723 add_filter(
724 'author_link',
725 [ $security, 'alter_author_link' ],
726 10,
727 3
728 );
729 add_filter(
730 'rest_prepare_user',
731 [ $security, 'alter_json_users' ],
732 10,
733 3
734 );
735 }
736
737 // Obfuscate Email Address
738
739 if ( array_key_exists( 'obfuscate_email_address', $options ) && $options['obfuscate_email_address'] ) {
740 add_shortcode( 'obfuscate', [ $security, 'obfuscate_string' ] );
741 add_filter( 'widget_text', 'shortcode_unautop' );
742 add_filter( 'widget_text', 'do_shortcode' );
743 if ( array_key_exists( 'obfuscate_email_address_in_content', $options ) && $options['obfuscate_email_address_in_content'] ) {
744 add_filter( 'the_content', [ $security, 'obfuscate_emails_in_content__premium_only' ] );
745 }
746 }
747
748 // Disable XML-RPC
749
750 if ( array_key_exists( 'disable_xmlrpc', $options ) && $options['disable_xmlrpc'] ) {
751 add_filter( 'xmlrpc_enabled', '__return_false' );
752 add_action( 'wp', [ $security, 'remove_xmlrpc_link' ], 11 );
753 add_filter( 'wp_xmlrpc_server_class', [ $security, 'maybe_disable_xmlrpc' ] );
754 }
755
756 // =================================================================
757 // OPTIMIZATIONS
758 // =================================================================
759 // Instantiate object for Optimizations features
760 $optimizations = new ASENHA\Classes\Optimizations();
761 // Image Upload Control
762 if ( array_key_exists( 'image_upload_control', $options ) && $options['image_upload_control'] ) {
763 add_filter( 'wp_handle_upload', [ $optimizations, 'image_upload_handler' ] );
764 }
765 // Revisions Control
766 if ( array_key_exists( 'enable_revisions_control', $options ) && $options['enable_revisions_control'] ) {
767 add_filter(
768 'wp_revisions_to_keep',
769 [ $optimizations, 'limit_revisions_to_max_number' ],
770 10,
771 2
772 );
773 }
774 // Heartbeat Control
775
776 if ( array_key_exists( 'enable_heartbeat_control', $options ) && $options['enable_heartbeat_control'] ) {
777 add_filter(
778 'heartbeat_settings',
779 [ $optimizations, 'maybe_modify_heartbeat_frequency' ],
780 99,
781 2
782 );
783 add_action( 'admin_enqueue_scripts', [ $optimizations, 'maybe_disable_heartbeat' ], 99 );
784 add_action( 'wp_enqueue_scripts', [ $optimizations, 'maybe_disable_heartbeat' ], 99 );
785 }
786
787 // =================================================================
788 // UTILITIES
789 // =================================================================
790 // Instantiate object for Utilities features
791 $utilities = new ASENHA\Classes\Utilities();
792 // SMTP Email Delivery
793
794 if ( array_key_exists( 'smtp_email_delivery', $options ) && $options['smtp_email_delivery'] ) {
795 add_action( 'phpmailer_init', [ $utilities, 'deliver_email_via_smtp' ], 99999 );
796 add_action( 'wp_ajax_send_test_email', [ $utilities, 'send_test_email' ] );
797 }
798
799 // Multiple User Roles
800
801 if ( array_key_exists( 'multiple_user_roles', $options ) && $options['multiple_user_roles'] ) {
802 // Show roles checkboxes
803 add_action( 'show_user_profile', [ $utilities, 'add_multiple_roles_ui' ] );
804 // for when user edits their own profile
805 add_action( 'edit_user_profile', [ $utilities, 'add_multiple_roles_ui' ] );
806 // for when editing other user's profile
807 add_action( 'user_new_form', [ $utilities, 'add_multiple_roles_ui' ] );
808 // new user creation
809 // Save roles selections
810 add_action( 'personal_options_update', [ $utilities, 'save_roles_assignment' ] );
811 // for when user edits their own profile
812 add_action( 'edit_user_profile_update', [ $utilities, 'save_roles_assignment' ] );
813 // for when editing other user's profile
814 add_action( 'user_register', [ $utilities, 'save_roles_assignment' ] );
815 // new user creation
816 }
817
818 // View Admin as Role
819
820 if ( array_key_exists( 'view_admin_as_role', $options ) && $options['view_admin_as_role'] ) {
821 add_action( 'admin_bar_menu', [ $utilities, 'view_admin_as_admin_bar_menu' ], 8 );
822 // Priority 8 so it is next to username section
823 add_action( 'init', [ $utilities, 'role_switcher_to_view_admin_as' ] );
824 // add_action( 'wp_die_handler', [ $utilities, 'custom_error_page_on_switch_failure' ] );
825 add_action( 'admin_footer', [ $utilities, 'add_floating_reset_button' ] );
826 }
827
828 // Password Protection
829
830 if ( array_key_exists( 'enable_password_protection', $options ) && $options['enable_password_protection'] ) {
831 add_action( 'plugins_loaded', [ $utilities, 'show_password_protection_admin_bar_icon' ] );
832 add_action( 'init', [ $utilities, 'maybe_disable_page_caching' ], 1 );
833 add_action( 'template_redirect', [ $utilities, 'maybe_show_login_form' ], 0 );
834 // load early
835 add_action( 'init', [ $utilities, 'maybe_process_login' ], 1 );
836 add_action( 'asenha_password_protection_error_messages', [ $utilities, 'add_login_error_messages' ] );
837 if ( function_exists( 'wp_site_icon' ) ) {
838 // WP v4.3+
839 add_action( 'asenha_password_protection_login_head', 'wp_site_icon' );
840 }
841 }
842
843 // Maintenance Mode
844
845 if ( array_key_exists( 'maintenance_mode', $options ) && $options['maintenance_mode'] ) {
846 add_action( 'send_headers', [ $utilities, 'maintenance_mode_redirect' ] );
847 add_action( 'plugins_loaded', [ $utilities, 'show_maintenance_mode_admin_bar_icon' ] );
848 }
849
850 // Redirect 404 to Homepage
851 if ( array_key_exists( 'redirect_404_to_homepage', $options ) && $options['redirect_404_to_homepage'] ) {
852 add_filter( 'wp', [ $utilities, 'redirect_404_to_homepage' ] );
853 }
854 // Display System Summary
855 if ( array_key_exists( 'display_system_summary', $options ) && $options['display_system_summary'] ) {
856 add_action( 'rightnow_end', [ $utilities, 'display_system_summary' ] );
857 }
858 }
859
860 }
861 Admin_Site_Enhancements::get_instance();