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