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