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