class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-common-methods.php
3 years ago
class-content-management.php
3 years ago
class-custom-code.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-login-logout.php
3 years ago
class-optimizations.php
3 years ago
class-security.php
3 years ago
class-settings-fields-render.php
3 years ago
class-settings-sanitization.php
3 years ago
class-settings-sections-fields.php
3 years ago
class-utilities.php
3 years ago
class-disable-components.php
494 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | use WP_Error; |
| 5 | |
| 6 | /** |
| 7 | * Class related to the Disable Components feature |
| 8 | * |
| 9 | * @since 2.2.0 |
| 10 | */ |
| 11 | class Disable_Components { |
| 12 | |
| 13 | /** |
| 14 | * Disable comments for post types |
| 15 | * |
| 16 | * @since 2.7.0 |
| 17 | */ |
| 18 | public function disable_comments_for_post_types_edit() { |
| 19 | |
| 20 | $options = get_option( ASENHA_SLUG_U ); |
| 21 | $disable_comments_for = $options['disable_comments_for']; |
| 22 | |
| 23 | foreach ( $disable_comments_for as $post_type_slug => $is_commenting_disabled ) { |
| 24 | if ( $is_commenting_disabled ) { |
| 25 | remove_post_type_support( $post_type_slug, 'comments' ); |
| 26 | remove_post_type_support( $post_type_slug, 'trackbacks' ); |
| 27 | remove_meta_box( 'commentstatusdiv', $post_type_slug, 'normal' ); |
| 28 | remove_meta_box( 'commentstatusdiv', $post_type_slug, 'side' ); |
| 29 | remove_meta_box( 'commentsdiv', $post_type_slug, 'normal' ); |
| 30 | remove_meta_box( 'commentsdiv', $post_type_slug, 'side' ); |
| 31 | remove_meta_box( 'trackbacksdiv', $post_type_slug, 'normal' ); |
| 32 | remove_meta_box( 'trackbacksdiv', $post_type_slug, 'side' ); |
| 33 | // edit-comments.js |
| 34 | wp_dequeue_script( 'admin-comments' ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Hide existing comments from the frontend post |
| 42 | * |
| 43 | * @since 2.7.0 |
| 44 | */ |
| 45 | public function hide_existing_comments_on_frontend( $comments, $post_id ) { |
| 46 | $options = get_option( ASENHA_SLUG_U ); |
| 47 | $disable_comments_for = $options['disable_comments_for']; |
| 48 | $current_post_type = get_post_type(); |
| 49 | |
| 50 | foreach ( $disable_comments_for as $post_type_slug => $is_commenting_disabled ) { |
| 51 | if ( ( $current_post_type === $post_type_slug ) && $is_commenting_disabled ) { |
| 52 | return array(); // return an empty array instead of the existing comments array |
| 53 | } else { |
| 54 | return $comments; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Close commenting on the frontend |
| 61 | * |
| 62 | * @since 2.7.0 |
| 63 | */ |
| 64 | public function close_commenting_on_frontend() { |
| 65 | $options = get_option( ASENHA_SLUG_U ); |
| 66 | $disable_comments_for = $options['disable_comments_for']; |
| 67 | $current_post_type = get_post_type(); |
| 68 | |
| 69 | foreach ( $disable_comments_for as $post_type_slug => $is_commenting_disabled ) { |
| 70 | if ( ( $current_post_type === $post_type_slug ) && $is_commenting_disabled ) { |
| 71 | return false; // close commenting |
| 72 | } else { |
| 73 | return true; // keep commenting open |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Show blank template on singular views when comment is disabled |
| 80 | * |
| 81 | * @since 4.9.2 |
| 82 | */ |
| 83 | public function show_blank_comment_template() { |
| 84 | |
| 85 | $options = get_option( ASENHA_SLUG_U ); |
| 86 | $disable_comments_for = $options['disable_comments_for']; |
| 87 | $current_post_type = get_post_type(); |
| 88 | |
| 89 | foreach ( $disable_comments_for as $post_type_slug => $is_commenting_disabled ) { |
| 90 | if ( ( $current_post_type === $post_type_slug ) && $is_commenting_disabled ) { |
| 91 | |
| 92 | if ( is_singular() ) { |
| 93 | add_filter( 'comments_template', [ $this, 'load_blank_comment_template' ], 20 ); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Load the actual blank comment template |
| 103 | * |
| 104 | * @since 4.9.2 |
| 105 | */ |
| 106 | public function load_blank_comment_template() { |
| 107 | return ASENHA_PATH . 'includes/blank-comment-template.php'; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Disable Gutenberg in wp-admin for some or all post types |
| 112 | * |
| 113 | * @since 2.8.0 |
| 114 | */ |
| 115 | public function disable_gutenberg_for_post_types_admin() { |
| 116 | |
| 117 | // Get current page's post type from WP core globals and query parameters |
| 118 | |
| 119 | global $pagenow, $typenow; |
| 120 | |
| 121 | $post_type = null; |
| 122 | |
| 123 | if ( 'edit.php' === $pagenow ) { // on the list table screen, $typenow returns correct post type |
| 124 | |
| 125 | $post_type = $typenow; |
| 126 | |
| 127 | } elseif ( 'post.php' === $pagenow ) { // on the edit screen, $typenow is empty, so we detect it |
| 128 | |
| 129 | $post_type = isset( $_GET['post'] ) ? get_post_type( $_GET['post'] ) : 'post'; |
| 130 | |
| 131 | } elseif ( 'post-new.php' === $pagenow ) { // on the add new screen, best to get post type from GET parameter |
| 132 | |
| 133 | $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post'; |
| 134 | |
| 135 | } else {} |
| 136 | |
| 137 | // Check if Gutenberg feature is enabled for the site |
| 138 | |
| 139 | // Before/after WP v5.0.0 via feature plugin |
| 140 | $gutenberg = function_exists( 'gutenberg_register_scripts_and_styles' ); |
| 141 | |
| 142 | // Since WP v5.0.0, gutenberg is in core |
| 143 | $block_editor = has_action( 'enqueue_block_assets' ); |
| 144 | |
| 145 | // Gutenberg feature is not enabled for the site |
| 146 | if ( ! $gutenberg && ( false === $block_editor ) ) { |
| 147 | return; // do nothing |
| 148 | } |
| 149 | |
| 150 | // Assemble single-dimensional array of post types for which Gutenberg should be disabled |
| 151 | $options = get_option( ASENHA_SLUG_U ); |
| 152 | $disable_gutenberg_for = $options['disable_gutenberg_for']; |
| 153 | $post_types_for_disable_gutenberg = array(); |
| 154 | |
| 155 | foreach( $disable_gutenberg_for as $post_type_slug => $is_gutenberg_disabled ) { |
| 156 | if ( $is_gutenberg_disabled ) { |
| 157 | $post_types_for_disable_gutenberg[] = $post_type_slug; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Selectively disable Gutenberg |
| 162 | if ( in_array( $post_type, $post_types_for_disable_gutenberg ) ) { |
| 163 | |
| 164 | // For WP v5.0.0 upwards |
| 165 | add_filter( 'use_block_editor_for_post_type', '__return_false', 100 ); |
| 166 | |
| 167 | // If Gutenberg feature plugin is activated |
| 168 | if ( $gutenberg ) { |
| 169 | add_filter( 'gutenberg_can_edit_post_type', '__return_false', 100 ); |
| 170 | $this->remove_all_gutenberg_hook(); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Remove Gutenberg hooks added via feature plugin. |
| 179 | * |
| 180 | * @link https://plugins.trac.wordpress.org/browser/classic-editor/tags/1.6.2/classic-editor.php#L138 |
| 181 | * @since 2.8.0 |
| 182 | */ |
| 183 | public function remove_all_gutenberg_hooks() { |
| 184 | |
| 185 | remove_action( 'admin_menu', 'gutenberg_menu' ); |
| 186 | remove_action( 'admin_init', 'gutenberg_redirect_demo' ); |
| 187 | |
| 188 | // Gutenberg 5.3+ |
| 189 | remove_action( 'wp_enqueue_scripts', 'gutenberg_register_scripts_and_styles' ); |
| 190 | remove_action( 'admin_enqueue_scripts', 'gutenberg_register_scripts_and_styles' ); |
| 191 | remove_action( 'admin_notices', 'gutenberg_wordpress_version_notice' ); |
| 192 | remove_action( 'rest_api_init', 'gutenberg_register_rest_widget_updater_routes' ); |
| 193 | remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' ); |
| 194 | remove_action( 'admin_print_scripts', 'gutenberg_block_editor_admin_print_scripts' ); |
| 195 | remove_action( 'admin_print_footer_scripts', 'gutenberg_block_editor_admin_print_footer_scripts' ); |
| 196 | remove_action( 'admin_footer', 'gutenberg_block_editor_admin_footer' ); |
| 197 | remove_action( 'admin_enqueue_scripts', 'gutenberg_widgets_init' ); |
| 198 | remove_action( 'admin_notices', 'gutenberg_build_files_notice' ); |
| 199 | |
| 200 | remove_filter( 'load_script_translation_file', 'gutenberg_override_translation_file' ); |
| 201 | remove_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' ); |
| 202 | remove_filter( 'default_content', 'gutenberg_default_demo_content' ); |
| 203 | remove_filter( 'default_title', 'gutenberg_default_demo_title' ); |
| 204 | remove_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' ); |
| 205 | remove_filter( 'rest_request_after_callbacks', 'gutenberg_filter_oembed_result' ); |
| 206 | |
| 207 | // Previously used, compat for older Gutenberg versions. |
| 208 | remove_filter( 'wp_refresh_nonces', 'gutenberg_add_rest_nonce_to_heartbeat_response_headers' ); |
| 209 | remove_filter( 'get_edit_post_link', 'gutenberg_revisions_link_to_editor' ); |
| 210 | remove_filter( 'wp_prepare_revision_for_js', 'gutenberg_revisions_restore' ); |
| 211 | |
| 212 | remove_action( 'rest_api_init', 'gutenberg_register_rest_routes' ); |
| 213 | remove_action( 'rest_api_init', 'gutenberg_add_taxonomy_visibility_field' ); |
| 214 | remove_filter( 'registered_post_type', 'gutenberg_register_post_prepare_functions' ); |
| 215 | |
| 216 | remove_action( 'do_meta_boxes', 'gutenberg_meta_box_save' ); |
| 217 | remove_action( 'submitpost_box', 'gutenberg_intercept_meta_box_render' ); |
| 218 | remove_action( 'submitpage_box', 'gutenberg_intercept_meta_box_render' ); |
| 219 | remove_action( 'edit_page_form', 'gutenberg_intercept_meta_box_render' ); |
| 220 | remove_action( 'edit_form_advanced', 'gutenberg_intercept_meta_box_render' ); |
| 221 | remove_filter( 'redirect_post_location', 'gutenberg_meta_box_save_redirect' ); |
| 222 | remove_filter( 'filter_gutenberg_meta_boxes', 'gutenberg_filter_meta_boxes' ); |
| 223 | |
| 224 | remove_filter( 'body_class', 'gutenberg_add_responsive_body_class' ); |
| 225 | remove_filter( 'admin_url', 'gutenberg_modify_add_new_button_url' ); // old |
| 226 | remove_action( 'admin_enqueue_scripts', 'gutenberg_check_if_classic_needs_warning_about_blocks' ); |
| 227 | remove_filter( 'register_post_type_args', 'gutenberg_filter_post_type_labels' ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Disable Gutenberg styles and scripts on the front end for all or some post types |
| 232 | * |
| 233 | * @since 2.8.0 |
| 234 | */ |
| 235 | public function disable_gutenberg_for_post_types_frontend() { |
| 236 | |
| 237 | $post = get_queried_object(); |
| 238 | $post_type = $post->post_type; |
| 239 | |
| 240 | // Assemble single-dimensional array of post types for which Gutenberg should be disabled |
| 241 | $options = get_option( ASENHA_SLUG_U ); |
| 242 | $disable_gutenberg_for = $options['disable_gutenberg_for']; |
| 243 | $post_types_for_disable_gutenberg = array(); |
| 244 | |
| 245 | foreach( $disable_gutenberg_for as $post_type_slug => $is_gutenberg_disabled ) { |
| 246 | if ( $is_gutenberg_disabled ) { |
| 247 | $post_types_for_disable_gutenberg[] = $post_type_slug; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Selectively disable for the selected post types |
| 252 | if ( in_array( $post_type, $post_types_for_disable_gutenberg ) ) { |
| 253 | |
| 254 | global $wp_styles; |
| 255 | |
| 256 | // As needed, exclude some block styles from dequeuing |
| 257 | $keep_enqueued = array(); // e.g. array( 'wp-block-navigation' ); |
| 258 | |
| 259 | foreach ( $wp_styles->queue as $handle ) { |
| 260 | |
| 261 | // For all stye handles that starts with 'wp-block' |
| 262 | if ( false !== strpos( $handle, 'wp-block' ) ) { |
| 263 | |
| 264 | if ( ! in_array( $handle, $keep_enqueued ) ) { |
| 265 | wp_dequeue_style( $handle ); |
| 266 | } |
| 267 | |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | // Additional dequeuing |
| 273 | wp_dequeue_style( 'core-block-supports' ); |
| 274 | wp_dequeue_style( 'global-styles' ); // theme.json |
| 275 | wp_dequeue_style( 'classic-theme-styles' ); // classic theme |
| 276 | |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Disable REST API for non-authenticated users. This is for WP v4.7 or later. |
| 283 | * |
| 284 | * @since 2.9.0 |
| 285 | */ |
| 286 | public function disable_rest_api() { |
| 287 | |
| 288 | if ( ! is_user_logged_in() ) { |
| 289 | |
| 290 | return new WP_Error( |
| 291 | 'rest_api_authentication_required', |
| 292 | 'The REST API has been restricted to authenticated users.', |
| 293 | array( |
| 294 | 'status' => rest_authorization_required_code() |
| 295 | ) |
| 296 | ); |
| 297 | |
| 298 | } |
| 299 | |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Disable updates and related functionalities |
| 304 | * |
| 305 | * @since 4.0.0 |
| 306 | */ |
| 307 | public function disable_update_notices_version_checks() { |
| 308 | |
| 309 | // Remove nags |
| 310 | remove_action( 'admin_notices', 'update_nag', 3 ); |
| 311 | remove_action( 'admin_notices', 'maintenance_nag' ); |
| 312 | |
| 313 | // Disable WP version check |
| 314 | remove_action( 'wp_version_check', 'wp_version_check' ); |
| 315 | remove_action( 'admin_init', 'wp_version_check' ); |
| 316 | wp_clear_scheduled_hook( 'wp_version_check' ); |
| 317 | |
| 318 | add_filter( 'pre_option_update_core', '__return_null' ); |
| 319 | |
| 320 | // Disable theme version checks |
| 321 | remove_action( 'wp_update_themes', 'wp_update_themes' ); |
| 322 | remove_action( 'admin_init', '_maybe_update_themes' ); |
| 323 | wp_clear_scheduled_hook( 'wp_update_themes' ); |
| 324 | |
| 325 | remove_action( 'load-themes.php', 'wp_update_themes' ); |
| 326 | remove_action( 'load-update.php', 'wp_update_themes' ); |
| 327 | remove_action( 'load-update-core.php', 'wp_update_themes' ); |
| 328 | |
| 329 | // Disable plugin version checks |
| 330 | remove_action( 'wp_update_plugins', 'wp_update_plugins' ); |
| 331 | remove_action( 'admin_init', '_maybe_update_plugins' ); |
| 332 | wp_clear_scheduled_hook( 'wp_update_plugins' ); |
| 333 | |
| 334 | remove_action( 'load-plugins.php', 'wp_update_plugins' ); |
| 335 | remove_action( 'load-update.php', 'wp_update_plugins' ); |
| 336 | remove_action( 'load-update-core.php', 'wp_update_plugins' ); |
| 337 | |
| 338 | // Disable auto updates |
| 339 | wp_clear_scheduled_hook( 'wp_maybe_auto_update' ); |
| 340 | |
| 341 | remove_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' ); |
| 342 | remove_action( 'admin_init', 'wp_maybe_auto_update' ); |
| 343 | remove_action( 'admin_init', 'wp_auto_update_core' ); |
| 344 | |
| 345 | // Disable Site Health checks |
| 346 | add_filter( 'site_status_tests', [ $this, 'disable_update_checks_in_site_health' ] ); |
| 347 | |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Override version check info stored in transients named update_core, update_plugins, update_themes. |
| 352 | * |
| 353 | * @since 4.0.0 |
| 354 | */ |
| 355 | public function override_version_check_info() { |
| 356 | |
| 357 | include( ABSPATH . WPINC . '/version.php' ); // get $wp_version from here |
| 358 | |
| 359 | $current = (object)array(); // create empty object |
| 360 | $current->updates = array(); |
| 361 | $current->response = array(); |
| 362 | $current->version_checked = $wp_version; |
| 363 | $current->last_checked = time(); |
| 364 | |
| 365 | return $current; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Disable Background Updates and Auto-Updates tests in Site Health tests |
| 371 | * |
| 372 | * @since 4.0.0 |
| 373 | */ |
| 374 | public function disable_update_checks_in_site_health( $tests ) { |
| 375 | |
| 376 | unset( $tests['async']['background_updates'] ); |
| 377 | unset( $tests['direct']['plugin_theme_auto_updates'] ); |
| 378 | |
| 379 | return $tests; |
| 380 | |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Remove Dashboard >> Updates menu item |
| 385 | * |
| 386 | * @since 4.0.0 |
| 387 | */ |
| 388 | public function remove_updates_menu() { |
| 389 | global $submenu; |
| 390 | remove_submenu_page( 'index.php', 'update-core.php' ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Disable loading of frontend public assets of dashicons |
| 395 | * |
| 396 | * @since 4.5.0 |
| 397 | */ |
| 398 | public function disable_dashicons_public_assets() { |
| 399 | if ( ! is_user_logged_in() ) { |
| 400 | |
| 401 | // This will get /path/file.php?param=val portion of the full URL |
| 402 | $current_request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); |
| 403 | |
| 404 | if ( empty( $current_request_uri ) ) { |
| 405 | // On the homepage |
| 406 | wp_dequeue_style( 'dashicons' ); |
| 407 | wp_deregister_style( 'dashicons' ); |
| 408 | } else { |
| 409 | // Exclude the login page, where dashicon assets are requred to properly style the page |
| 410 | if ( false !== strpos( $current_request_uri, 'wp-login.php' ) ) { |
| 411 | // On wp-login.php, so, do nothing |
| 412 | } |
| 413 | // Exclude password protection form |
| 414 | elseif ( false !== strpos( $current_request_uri, 'protected-page=view' ) ) { |
| 415 | // On protected-page=view, so, do nothing |
| 416 | } |
| 417 | else { |
| 418 | // NOT on wp-login.php, e.g. www.example.com/an-article/, so, dequeue dashicons |
| 419 | wp_dequeue_style( 'dashicons' ); |
| 420 | wp_deregister_style( 'dashicons' ); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Disable emoji support |
| 428 | * |
| 429 | * @since 4.5.0 |
| 430 | */ |
| 431 | public function disable_emoji_support() { |
| 432 | |
| 433 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 434 | remove_action( 'embed_head', 'print_emoji_detection_script' ); |
| 435 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 436 | remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
| 437 | remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
| 438 | remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
| 439 | add_action( 'admin_init', [ $this, 'disable_admin_emojis' ] ); |
| 440 | add_filter( 'emoji_svg_url', '__return_false' ); |
| 441 | add_filter( 'tiny_mce_plugins', [ $this, 'disable_emoji_for_tinymce' ] ); |
| 442 | add_filter( 'wp_resource_hints', [ $this, 'disable_emoji_remove_dns_prefetch' ], 10, 2 ); |
| 443 | |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Remove the tinymce emoji plugin |
| 448 | * |
| 449 | * @since 4.5.0 |
| 450 | */ |
| 451 | public function disable_emoji_for_tinymce( $plugins ) { |
| 452 | |
| 453 | if ( is_array( $plugins ) ) { |
| 454 | return array_diff( $plugins, array( 'wpemoji' ) ); |
| 455 | } |
| 456 | |
| 457 | return array(); |
| 458 | |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Remove emoji CDN hostname from DNS prefetching hints. |
| 463 | * |
| 464 | * @since 4.5.0 |
| 465 | */ |
| 466 | public function disable_emoji_remove_dns_prefetch( $urls, $relation_type ) { |
| 467 | |
| 468 | if ( 'dns-prefetch' == $relation_type ) { |
| 469 | |
| 470 | // Strip out any URLs referencing the WordPress.org emoji location |
| 471 | $emoji_svg_url_base = 'https://s.w.org/images/core/emoji/'; |
| 472 | foreach ( $urls as $key => $url ) { |
| 473 | if ( false !== strpos( $url, $emoji_svg_url_base ) ) { |
| 474 | unset( $urls[$key] ); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | } |
| 479 | |
| 480 | return $urls; |
| 481 | |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Disable emojis in wp-admin |
| 486 | * |
| 487 | * @since 4.7.2 |
| 488 | */ |
| 489 | public function disable_admin_emojis() { |
| 490 | remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 491 | remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
| 492 | } |
| 493 | |
| 494 | } |