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