From 5057fe6322cf844a4637ca95e68aad627b8cb930 Mon Sep 17 00:00:00 2001 From: Peter Downey Date: Fri, 27 Nov 2026 21:53:41 -0500 Subject: [PATCH 1/2] Add link, Heartbeat, pattern directory, and AI support settings Six new settings, each with the three-part help text: - Remove REST API Link: the API address in the page head and the Link response header. The REST API keeps working. - Remove oEmbed Discovery Links: removed at both priorities WordPress adds them (4 and 10). - Remove Feed Links: the feed discovery links in the page head. The feeds keep working. - Limit Heartbeat: deregistered on the front end; every 120 seconds instead of core's 60 on admin screens, except the post editor, the site editor, and the Customizer. A slower interval is kept. - Disable Pattern Directory Patterns: should_load_remote_block_patterns. - Disable WordPress AI Support: wp_supports_ai, read from init on. The header removals are now one table, so each option lists the hook, callback, and priority it removes. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/wp-compatibility-test.yml | 70 +++++ CHANGELOG.md | 9 + README.md | 6 +- includes/admin.php | 36 +++ includes/bootstrap.php | 4 + includes/frontend.php | 119 ++++++++- includes/options.php | 12 + languages/enginescript-site-optimizer.pot | 281 +++++++++++++------- readme.txt | 12 +- tests/bootstrap.php | 15 +- tests/unit/FrontendTest.php | 84 ++++++ tests/unit/OptionsAndNoticesTest.php | 2 +- tests/unit/PluginWiringTest.php | 4 + tests/unit/SettingsPageTest.php | 8 +- 14 files changed, 548 insertions(+), 114 deletions(-) diff --git a/.github/workflows/wp-compatibility-test.yml b/.github/workflows/wp-compatibility-test.yml index 5aa65ea..23604d6 100644 --- a/.github/workflows/wp-compatibility-test.yml +++ b/.github/workflows/wp-compatibility-test.yml @@ -2724,6 +2724,76 @@ jobs: wp_dequeue_style('wp-emoji-styles'); } + /** + * The REST API, oEmbed discovery, and feed link options remove core's own registrations. + * + * @since 2.4.0 + * @return void + */ + public function test_link_removals_match_core_registrations(): void { + $core = array( + array('wp_head', 'rest_output_link_wp_head', 10), + array('template_redirect', 'rest_output_link_header', 11), + array('wp_head', 'wp_oembed_add_discovery_links', 4), + array('wp_head', 'feed_links', 2), + array('wp_head', 'feed_links_extra', 3), + ); + foreach ($core as $row) { + $this->assertSame($row[2], has_action($row[0], $row[1]), 'Control: core registers ' . $row[1] . '.'); + } + update_option('es_optimizer_options', array('remove_rest_api_link' => 0, 'remove_oembed_links' => 0, 'remove_feed_links' => 0)); + es_optimizer_remove_header_items(); + $this->assertSame(10, has_action('wp_head', 'rest_output_link_wp_head'), 'Options off: nothing is removed.'); + update_option('es_optimizer_options', array('remove_rest_api_link' => 1, 'remove_oembed_links' => 1, 'remove_feed_links' => 1)); + es_optimizer_remove_header_items(); + foreach ($core as $row) { + $this->assertFalse(has_action($row[0], $row[1]), $row[1] . ' is removed.'); + } + $this->assertNotFalse(has_action('rest_api_init', 'create_initial_rest_routes'), 'The REST API itself stays.'); + } + + /** + * The pattern-directory, AI, and Heartbeat options act through core's own APIs. + * + * @since 2.4.0 + * @return void + */ + public function test_patterns_ai_and_heartbeat_with_core(): void { + $ai_by_constant = !defined('WP_AI_SUPPORT') || WP_AI_SUPPORT; + update_option('es_optimizer_options', array('disable_remote_patterns' => 0, 'disable_ai_features' => 0, 'heartbeat_control' => 0)); + $this->assertTrue(apply_filters('should_load_remote_block_patterns', true)); + $this->assertSame($ai_by_constant, wp_supports_ai()); + update_option('es_optimizer_options', array('disable_remote_patterns' => 1, 'disable_ai_features' => 1, 'heartbeat_control' => 1)); + $this->assertFalse(apply_filters('should_load_remote_block_patterns', true)); + $this->assertFalse(wp_supports_ai()); + + $this->assertTrue(wp_script_is('heartbeat', 'registered'), 'Control: core registers Heartbeat.'); + es_optimizer_disable_frontend_heartbeat(); + $this->assertFalse(wp_script_is('heartbeat', 'registered'), 'Deregistered on the front end.'); + unset($GLOBALS['wp_scripts']); + + // is_admin() asks the current screen; a minimal admin screen avoids loading admin files. + $previous_page = $GLOBALS['pagenow'] ?? null; + $GLOBALS['current_screen'] = new class() { + public function in_admin($admin = null): bool { + return true; + } + }; + try { + $this->assertTrue(is_admin()); + $GLOBALS['pagenow'] = 'index.php'; + $this->assertSame(120, apply_filters('heartbeat_settings', array())['interval']); + $this->assertSame(300, apply_filters('heartbeat_settings', array('interval' => 300))['interval'], 'A slower interval is kept.'); + $GLOBALS['pagenow'] = 'post.php'; + $this->assertArrayNotHasKey('interval', apply_filters('heartbeat_settings', array()), 'The post editor keeps core timing.'); + } finally { + $GLOBALS['pagenow'] = $previous_page; + unset($GLOBALS['current_screen']); + } + $this->assertFalse(is_admin()); + $this->assertArrayNotHasKey('interval', apply_filters('heartbeat_settings', array()), 'Not in the admin area.'); + } + /** * Create sites and give each site, the current one first, a stored option. * diff --git a/CHANGELOG.md b/CHANGELOG.md index 591d697..50e4694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- "Remove REST API Link": remove the REST API address from the page head and the Link response header. The REST API keeps working. +- "Remove oEmbed Discovery Links": remove the links other sites read to embed your posts, at both priorities WordPress adds them. +- "Remove Feed Links": remove the RSS feed links from the page head. The feeds keep working. +- "Limit Heartbeat": stop the Heartbeat script on the front end, and send it every 120 seconds instead of 60 on admin screens other than the post editor, the site editor, and the Customizer. A slower interval set by another plugin is kept. +- "Disable Pattern Directory Patterns": stop the block editor from loading patterns from the WordPress.org Pattern Directory. +- "Disable WordPress AI Support": answer `false` to WordPress 7's `wp_supports_ai` filter. + ### Fixed - On the settings page, only the checkbox now turns a setting on or off. A click on the setting's title or its description, or a drag across it to select the text, no longer changes the setting. diff --git a/README.md b/README.md index 0fcf910..f06c549 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,13 @@ A WordPress plugin for disabling selected features and configuring resource hint ## Features -- **Header Cleanup:** Remove generator output from the page head and feeds, RSD links from the page head, and shortlinks from the page head and the HTTP response header -- **Script Optimization:** Disable WordPress emojis and remove jQuery Migrate +- **Header Cleanup:** Remove generator output from the page head and feeds, RSD links from the page head, shortlinks and the REST API link from the page head and the HTTP response header, and oEmbed discovery and feed links from the page head +- **Script Optimization:** Disable WordPress emojis, remove jQuery Migrate, and limit the Heartbeat (off on the front end, every 120 seconds on admin screens outside the editors) - **Style Optimization:** Remove inline styles from recent comments widget and disable classic theme styles +- **Editor and Admin:** Stop loading patterns from the WordPress.org Pattern Directory, and turn off WordPress's AI support - **Resource Hints:** Configure DNS prefetch and preconnect for HTTPS domains used by your site - **Jetpack Promotions:** Disable Jetpack promotional messages and Blaze promotions +- **Explained Settings:** Every setting says what it does, its effect on the site, and its performance gain or cost ## Installation diff --git a/includes/admin.php b/includes/admin.php index 95508eb..b31df53 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -97,6 +97,8 @@ function es_optimizer_get_performance_settings_fields(): array { es_optimizer_get_checkbox_field_definition( $section, 'disable_emojis', __( 'Disable WordPress Emojis', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'remove_jquery_migrate', __( 'Remove jQuery Migrate', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'disable_classic_theme_styles', __( 'Disable Classic Theme Styles', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'heartbeat_control', __( 'Limit Heartbeat', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'disable_remote_patterns', __( 'Disable Pattern Directory Patterns', 'enginescript-site-optimizer' ) ), ]; } @@ -114,6 +116,9 @@ function es_optimizer_get_header_cleanup_settings_fields(): array { es_optimizer_get_checkbox_field_definition( $section, 'remove_rsd_link', __( 'Remove RSD Link', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'remove_shortlink', __( 'Remove Shortlink', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'remove_recent_comments_style', __( 'Remove Recent Comments Style', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'remove_rest_api_link', __( 'Remove REST API Link', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'remove_oembed_links', __( 'Remove oEmbed Discovery Links', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'remove_feed_links', __( 'Remove Feed Links', 'enginescript-site-optimizer' ) ), ]; } @@ -129,6 +134,7 @@ function es_optimizer_get_additional_settings_fields(): array { return [ es_optimizer_get_checkbox_field_definition( $section, 'disable_jetpack_ads', __( 'Disable Jetpack Promotions', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'disable_post_via_email', __( 'Disable Post via Email', 'enginescript-site-optimizer' ) ), + es_optimizer_get_checkbox_field_definition( $section, 'disable_ai_features', __( 'Disable WordPress AI Support', 'enginescript-site-optimizer' ) ), es_optimizer_get_checkbox_field_definition( $section, 'enable_preconnect', __( 'Enable Preconnect', 'enginescript-site-optimizer' ) ), es_optimizer_get_textarea_field_definition( $section, @@ -227,6 +233,16 @@ function es_optimizer_get_option_help(): array { 'effect' => __( 'Buttons and File blocks in your content may lose their default spacing and colors if the theme does not style them itself. A stylesheet that another plugin loads on top of this one will not load either.', 'enginescript-site-optimizer' ), 'performance' => __( 'Removes a few lines of CSS, usually printed inline in the page head, on classic themes: a very small gain for visitors. No change on the server.', 'enginescript-site-optimizer' ), ], + 'heartbeat_control' => [ + 'what' => __( 'Limits the Heartbeat, a script that keeps a page in touch with the server through regular background requests. On the front end it stops the script from loading; WordPress loads it there only when a theme or plugin asks for it. On admin screens it sends a request every 120 seconds instead of every 60. The post editor, the site editor, and the Customizer keep the usual timing.', 'enginescript-site-optimizer' ), + 'effect' => __( 'Front-end features that rely on the Heartbeat, such as live chat, live notifications, or front-end editing in some plugins, stop updating, and their scripts may not load. In the admin area, dashboard widgets that refresh through the Heartbeat update half as often, and the notice that your login has expired can appear up to two minutes later. Post locking and autosave in the editors are not changed.', 'enginescript-site-optimizer' ), + 'performance' => __( 'Each Heartbeat request loads WordPress in full on the server and is never served from a page cache. Halving them on admin screens, and removing them from the front end, saves server work, which adds up when several people keep the admin area open. Visitors see no difference in page speed.', 'enginescript-site-optimizer' ), + ], + 'disable_remote_patterns' => [ + 'what' => __( 'Stops the block editor from loading patterns from the WordPress.org Pattern Directory: the general and featured patterns, and the directory patterns a theme asks for. Patterns that come with your theme, your plugins, or WordPress itself, and the patterns you create, stay available.', 'enginescript-site-optimizer' ), + 'effect' => __( 'The pattern inserter shows far fewer ready-made patterns, because most of the patterns it offers come from the directory. Patterns already used in your content are not changed.', 'enginescript-site-optimizer' ), + 'performance' => __( 'Saves the requests the server makes to WordPress.org when the editor loads patterns, and the stored copies of their results, which are refreshed about every hour. The editor\'s pattern list can open faster. No change on the front end.', 'enginescript-site-optimizer' ), + ], 'remove_wp_version' => [ 'what' => __( 'Removes the generator tag that names your WordPress version from the page head and from your RSS, Atom, and other feeds. Version numbers on script and stylesheet addresses, and export files, are left as they are.', 'enginescript-site-optimizer' ), 'effect' => __( 'Hides the version from casual inspection and from scanners that read the tag. It is not real protection, because the version can be worked out in other ways; keeping WordPress up to date matters far more. Nothing on the site looks different.', 'enginescript-site-optimizer' ), @@ -247,6 +263,26 @@ function es_optimizer_get_option_help(): array { 'effect' => __( 'The widget\'s links may look slightly different if your theme relied on that style. Block themes and the Latest Comments block are not affected.', 'enginescript-site-optimizer' ), 'performance' => __( 'Removes a few bytes of CSS from pages that show the widget. No measurable speed change.', 'enginescript-site-optimizer' ), ], + 'remove_rest_api_link' => [ + 'what' => __( 'Removes the link to the site\'s REST API address from the page head and from the Link header that WordPress sends with each page.', 'enginescript-site-optimizer' ), + 'effect' => __( 'The REST API keeps working: the block editor, mobile apps, and plugins that use it are not affected. Only the advertised address is removed, which a few tools and services use to find the API on their own.', 'enginescript-site-optimizer' ), + 'performance' => __( 'Removes one line from each page and one response header. No measurable speed change.', 'enginescript-site-optimizer' ), + ], + 'remove_oembed_links' => [ + 'what' => __( 'Removes the oEmbed discovery links from the page head of posts and pages. Other sites read them to show one of your posts as an embedded card when someone pastes its link.', 'enginescript-site-optimizer' ), + 'effect' => __( 'A link to one of your posts pasted into another WordPress site, or into some apps and services, shows as a plain link instead of a card. Embedding videos, posts, and other content from other sites in your own posts keeps working.', 'enginescript-site-optimizer' ), + 'performance' => __( 'Removes two lines from each post and page. No measurable speed change.', 'enginescript-site-optimizer' ), + ], + 'remove_feed_links' => [ + 'what' => __( 'Removes the links to the site\'s RSS feeds, for posts, comments, and categories, tags, or authors, from the page head.', 'enginescript-site-optimizer' ), + 'effect' => __( 'The feeds keep working at their usual addresses, but browsers and feed readers can no longer find them from a page, so someone who subscribes by entering the site\'s address may need the feed\'s address instead. Leave this off if people follow the site in a feed reader.', 'enginescript-site-optimizer' ), + 'performance' => __( 'Removes a few lines from each page. No measurable speed change.', 'enginescript-site-optimizer' ), + ], + 'disable_ai_features' => [ + 'what' => __( 'Tells WordPress that AI features are not supported on this site. WordPress then does not register its built-in AI provider connectors, and refuses AI provider connectors from plugins.', 'enginescript-site-optimizer' ), + 'effect' => __( 'AI features that rely on WordPress\'s AI support are unavailable, including those in plugins that check for it. Connectors that are not AI providers, such as Akismet, are not affected. Setting the WP_AI_SUPPORT constant to false in wp-config.php has the same effect.', 'enginescript-site-optimizer' ), + 'performance' => __( 'No measurable speed change. WordPress only skips registering its AI provider connectors on each request.', 'enginescript-site-optimizer' ), + ], 'disable_jetpack_ads' => [ 'what' => __( 'Turns off Jetpack\'s promotional notices, its Just-in-Time upgrade messages, and its Blaze prompts to advertise your posts. It does nothing unless Jetpack is installed.', 'enginescript-site-optimizer' ), 'effect' => __( 'Jetpack keeps working; you no longer see its upgrade and advertising prompts in the admin area. Visitors see no difference.', 'enginescript-site-optimizer' ), diff --git a/includes/bootstrap.php b/includes/bootstrap.php index 9721ae1..1e1846c 100644 --- a/includes/bootstrap.php +++ b/includes/bootstrap.php @@ -51,6 +51,10 @@ function es_optimizer_init_frontend_optimizations(): void { add_filter( 'wp_resource_hints', 'es_optimizer_add_dns_prefetch_resource_hints', 10, 2 ); add_filter( 'tiny_mce_plugins', 'es_optimizer_disable_emojis_tinymce' ); add_action( 'init', 'es_optimizer_disable_post_via_email' ); + add_filter( 'should_load_remote_block_patterns', 'es_optimizer_filter_remote_patterns', PHP_INT_MAX ); + add_filter( 'wp_supports_ai', 'es_optimizer_filter_ai_support', PHP_INT_MAX ); + add_action( 'wp_enqueue_scripts', 'es_optimizer_disable_frontend_heartbeat', 100 ); + add_filter( 'heartbeat_settings', 'es_optimizer_filter_heartbeat_settings', PHP_INT_MAX ); } /** diff --git a/includes/frontend.php b/includes/frontend.php index ed839d5..1494117 100644 --- a/includes/frontend.php +++ b/includes/frontend.php @@ -9,6 +9,13 @@ return; } +/** + * Heartbeat interval, in seconds, on admin screens other than the editors. + * + * @since 2.4.0 + */ +const ES_SITE_OPTIMIZER_ADMIN_HEARTBEAT_INTERVAL = 120; + /** * Determine whether frontend-only optimizations should run. * @@ -117,25 +124,46 @@ function es_optimizer_disable_classic_theme_styles(): void { * es_optimizer_remove_feed_generator() for the one feed action it shares. * * @since 1.0.0 + * @since 2.4.0 Also removes the REST API, oEmbed discovery, and feed links. */ function es_optimizer_remove_header_items(): void { - if ( es_optimizer_is_option_enabled( 'remove_wp_version' ) ) { - remove_action( 'wp_head', 'wp_generator' ); + foreach ( es_optimizer_get_header_removals() as $option_key => $callbacks ) { + if ( ! es_optimizer_is_option_enabled( $option_key ) ) { + continue; + } - // The feed actions that WordPress gives a generator tag, except the one an export file also runs. - foreach ( [ 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head' ] as $feed_action ) { - remove_action( $feed_action, 'the_generator' ); + foreach ( $callbacks as [ $hook, $callback, $priority ] ) { + remove_action( $hook, $callback, $priority ); } } +} - if ( es_optimizer_is_option_enabled( 'remove_rsd_link' ) ) { - remove_action( 'wp_head', 'rsd_link' ); - } +/** + * Get the core callbacks that each header option removes. + * + * WordPress adds the oEmbed discovery links twice, at priorities 4 and 10, + * and the second registration unhooks itself once the first has run; both + * are removed. The RSS 2 generator is left to es_optimizer_remove_feed_generator(). + * + * @since 2.4.0 + * @return array> Hook, callback, and priority for each option. + */ +function es_optimizer_get_header_removals(): array { + $generator = [ [ 'wp_head', 'wp_generator', 10 ] ]; - if ( es_optimizer_is_option_enabled( 'remove_shortlink' ) ) { - remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 ); - remove_action( 'template_redirect', 'wp_shortlink_header', 11 ); + // The feed actions that WordPress gives a generator tag, except the one an export file also runs. + foreach ( [ 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head' ] as $feed_action ) { + $generator[] = [ $feed_action, 'the_generator', 10 ]; } + + return [ + 'remove_wp_version' => $generator, + 'remove_rsd_link' => [ [ 'wp_head', 'rsd_link', 10 ] ], + 'remove_shortlink' => [ [ 'wp_head', 'wp_shortlink_wp_head', 10 ], [ 'template_redirect', 'wp_shortlink_header', 11 ] ], + 'remove_rest_api_link' => [ [ 'wp_head', 'rest_output_link_wp_head', 10 ], [ 'template_redirect', 'rest_output_link_header', 11 ] ], + 'remove_oembed_links' => [ [ 'wp_head', 'wp_oembed_add_discovery_links', 4 ], [ 'wp_head', 'wp_oembed_add_discovery_links', 10 ] ], + 'remove_feed_links' => [ [ 'wp_head', 'feed_links', 2 ], [ 'wp_head', 'feed_links_extra', 3 ] ], + ]; } /** @@ -324,3 +352,72 @@ function es_optimizer_disable_post_via_email(): void { function es_optimizer_filter_post_via_email( mixed $enabled ): mixed { return es_optimizer_is_option_enabled( 'disable_post_via_email' ) ? false : $enabled; } + +/** + * Apply the current site's policy for patterns from the WordPress.org pattern directory. + * + * @since 2.4.0 + * @param mixed $should_load Incoming value from WordPress or another plugin. + * @return mixed False when disabled; otherwise the original value. + */ +function es_optimizer_filter_remote_patterns( mixed $should_load ): mixed { + return es_optimizer_is_option_enabled( 'disable_remote_patterns' ) ? false : $should_load; +} + +/** + * Apply the current site's policy for WordPress's AI support. + * + * WordPress reads it from init on, when it registers its AI provider connectors. + * + * @since 2.4.0 + * @param mixed $supported Incoming value from WordPress or another plugin. + * @return mixed False when disabled; otherwise the original value. + */ +function es_optimizer_filter_ai_support( mixed $supported ): mixed { + return es_optimizer_is_option_enabled( 'disable_ai_features' ) ? false : $supported; +} + +/** + * Stop the Heartbeat script from loading on the front end. + * + * Core loads Heartbeat on the front end only when a theme or plugin asks for + * it. Scripts that list it as a dependency are then not loaded either. + * + * @since 2.4.0 + */ +function es_optimizer_disable_frontend_heartbeat(): void { + if ( ! es_optimizer_is_option_enabled( 'heartbeat_control' ) || ! es_optimizer_is_frontend_request() ) { + return; + } + + wp_deregister_script( 'heartbeat' ); +} + +/** + * Slow Heartbeat to every 120 seconds in the admin area, outside the editors. + * + * Core's interval is 60 seconds, and 120 seconds while the tab is hidden. The + * post, site, and Customizer editors keep core's interval, because post locks + * and changeset locks rely on it. A longer interval set by another plugin is kept. + * + * @since 2.4.0 + * @global string $pagenow The admin page being loaded. + * @param mixed $settings Heartbeat settings from WordPress or another plugin. + * @return mixed Settings with the slower interval, or the incoming value when it is not changed. + */ +function es_optimizer_filter_heartbeat_settings( mixed $settings ): mixed { + global $pagenow; + + if ( ! is_array( $settings ) || ! is_admin() || ! es_optimizer_is_option_enabled( 'heartbeat_control' ) ) { + return $settings; + } + + if ( in_array( $pagenow, [ 'post.php', 'post-new.php', 'site-editor.php', 'customize.php' ], true ) ) { + return $settings; + } + + $current = isset( $settings['interval'] ) && is_numeric( $settings['interval'] ) ? (int) $settings['interval'] : 0; + $settings['interval'] = max( ES_SITE_OPTIMIZER_ADMIN_HEARTBEAT_INTERVAL, $current ); + + return $settings; +} diff --git a/includes/options.php b/includes/options.php index c39b9b8..695d8e7 100644 --- a/includes/options.php +++ b/includes/options.php @@ -51,6 +51,12 @@ function es_optimizer_get_default_options(): array { 'dns_prefetch_domains' => '', 'disable_jetpack_ads' => 0, 'disable_post_via_email' => 0, + 'remove_rest_api_link' => 0, + 'remove_oembed_links' => 0, + 'remove_feed_links' => 0, + 'disable_remote_patterns' => 0, + 'heartbeat_control' => 0, + 'disable_ai_features' => 0, ]; } @@ -73,6 +79,12 @@ function es_optimizer_get_boolean_option_keys(): array { 'enable_dns_prefetch', 'disable_jetpack_ads', 'disable_post_via_email', + 'remove_rest_api_link', + 'remove_oembed_links', + 'remove_feed_links', + 'disable_remote_patterns', + 'heartbeat_control', + 'disable_ai_features', ]; } diff --git a/languages/enginescript-site-optimizer.pot b/languages/enginescript-site-optimizer.pot index 622ffa6..4ddbddf 100644 --- a/languages/enginescript-site-optimizer.pot +++ b/languages/enginescript-site-optimizer.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2026-11-28T02:27:37+00:00\n" +"POT-Creation-Date: 2026-11-28T02:46:02+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.12.0\n" "X-Domain: enginescript-site-optimizer\n" @@ -58,332 +58,425 @@ msgstr "" msgid "Disable Classic Theme Styles" msgstr "" -#: includes/admin.php:113 +#: includes/admin.php:100 +msgid "Limit Heartbeat" +msgstr "" + +#: includes/admin.php:101 +msgid "Disable Pattern Directory Patterns" +msgstr "" + +#: includes/admin.php:115 msgid "Remove WordPress Version" msgstr "" -#: includes/admin.php:114 +#: includes/admin.php:116 msgid "Remove RSD Link" msgstr "" -#: includes/admin.php:115 +#: includes/admin.php:117 msgid "Remove Shortlink" msgstr "" -#: includes/admin.php:116 +#: includes/admin.php:118 msgid "Remove Recent Comments Style" msgstr "" -#: includes/admin.php:130 +#: includes/admin.php:119 +msgid "Remove REST API Link" +msgstr "" + +#: includes/admin.php:120 +msgid "Remove oEmbed Discovery Links" +msgstr "" + +#: includes/admin.php:121 +msgid "Remove Feed Links" +msgstr "" + +#: includes/admin.php:135 msgid "Disable Jetpack Promotions" msgstr "" -#: includes/admin.php:131 +#: includes/admin.php:136 msgid "Disable Post via Email" msgstr "" -#: includes/admin.php:132 +#: includes/admin.php:137 +msgid "Disable WordPress AI Support" +msgstr "" + +#: includes/admin.php:138 msgid "Enable Preconnect" msgstr "" -#: includes/admin.php:136 +#: includes/admin.php:142 msgid "Preconnect Domains" msgstr "" -#: includes/admin.php:137 +#: includes/admin.php:143 msgid "Use preconnect for domains that host critical resources. Enter one HTTPS domain per line, with no port or port 443 only. Do not include file paths, query parameters, fragments, or credentials. Invalid lines are removed on save; an all-invalid list is cleared. Domain checks do not verify DNS destinations." msgstr "" -#: includes/admin.php:139 +#: includes/admin.php:145 msgid "Enable DNS Prefetch" msgstr "" -#: includes/admin.php:143 +#: includes/admin.php:149 msgid "DNS Prefetch Domains" msgstr "" -#: includes/admin.php:144 +#: includes/admin.php:150 msgid "DNS prefetch performs only a DNS lookup of the domain name; ports do not affect the hint. Enter one HTTPS domain per line without file paths, query parameters, fragments, or credentials. Invalid lines are removed on save; an all-invalid list is cleared. Domain checks do not verify DNS destinations." msgstr "" #. translators: 1: Maximum bytes per domain list, 2: Maximum nonblank lines, 3: Maximum bytes per nonblank line. -#: includes/admin.php:158 +#: includes/admin.php:164 #, php-format msgid "Limits: %1$d bytes per list, %2$d nonblank lines, and %3$d bytes per nonblank line. Duplicate and invalid lines count toward the limit. Oversize submissions keep your previous list." msgstr "" -#: includes/admin.php:216 +#: includes/admin.php:222 msgid "Stops WordPress from adding its emoji detection script and emoji styles to every page, the admin area, and embedded posts, and from replacing emoji in feeds and emails with images from WordPress.org." msgstr "" -#: includes/admin.php:217 +#: includes/admin.php:223 msgid "Emoji still appear, drawn by the visitor's own device, which every current browser and operating system supports. On very old systems without emoji fonts, some emoji may show as empty boxes. Emoji in feeds and emails stay as characters instead of images." msgstr "" -#: includes/admin.php:218 +#: includes/admin.php:224 msgid "Removes a small inline script and its styles from every page, and the check that script runs in the browser: a small gain for visitors. No change on the server." msgstr "" -#: includes/admin.php:221 +#: includes/admin.php:227 msgid "Stops loading jQuery Migrate on the front end when a theme or plugin loads jQuery. jQuery Migrate restores functions that newer jQuery versions removed. The admin area is not changed." msgstr "" -#: includes/admin.php:222 +#: includes/admin.php:228 msgid "Front-end code written for old jQuery versions can stop working, which shows as broken sliders, menus, or forms and as errors in the browser console. Check the interactive parts of the site after turning this on. Pages that do not load jQuery are not affected." msgstr "" -#: includes/admin.php:223 +#: includes/admin.php:229 msgid "One script file fewer to download and run on every page that loads jQuery: a small gain for visitors. No change on the server." msgstr "" -#: includes/admin.php:226 +#: includes/admin.php:232 msgid "Removes the small classic-theme stylesheet that WordPress adds on the front end for themes without a theme.json file. It gives the Button and File blocks their default look. Block themes never load it, so on them this setting changes nothing. The editor is not changed." msgstr "" -#: includes/admin.php:227 +#: includes/admin.php:233 msgid "Buttons and File blocks in your content may lose their default spacing and colors if the theme does not style them itself. A stylesheet that another plugin loads on top of this one will not load either." msgstr "" -#: includes/admin.php:228 +#: includes/admin.php:234 msgid "Removes a few lines of CSS, usually printed inline in the page head, on classic themes: a very small gain for visitors. No change on the server." msgstr "" -#: includes/admin.php:231 +#: includes/admin.php:237 +msgid "Limits the Heartbeat, a script that keeps a page in touch with the server through regular background requests. On the front end it stops the script from loading; WordPress loads it there only when a theme or plugin asks for it. On admin screens it sends a request every 120 seconds instead of every 60. The post editor, the site editor, and the Customizer keep the usual timing." +msgstr "" + +#: includes/admin.php:238 +msgid "Front-end features that rely on the Heartbeat, such as live chat, live notifications, or front-end editing in some plugins, stop updating, and their scripts may not load. In the admin area, dashboard widgets that refresh through the Heartbeat update half as often, and the notice that your login has expired can appear up to two minutes later. Post locking and autosave in the editors are not changed." +msgstr "" + +#: includes/admin.php:239 +msgid "Each Heartbeat request loads WordPress in full on the server and is never served from a page cache. Halving them on admin screens, and removing them from the front end, saves server work, which adds up when several people keep the admin area open. Visitors see no difference in page speed." +msgstr "" + +#: includes/admin.php:242 +msgid "Stops the block editor from loading patterns from the WordPress.org Pattern Directory: the general and featured patterns, and the directory patterns a theme asks for. Patterns that come with your theme, your plugins, or WordPress itself, and the patterns you create, stay available." +msgstr "" + +#: includes/admin.php:243 +msgid "The pattern inserter shows far fewer ready-made patterns, because most of the patterns it offers come from the directory. Patterns already used in your content are not changed." +msgstr "" + +#: includes/admin.php:244 +msgid "Saves the requests the server makes to WordPress.org when the editor loads patterns, and the stored copies of their results, which are refreshed about every hour. The editor's pattern list can open faster. No change on the front end." +msgstr "" + +#: includes/admin.php:247 msgid "Removes the generator tag that names your WordPress version from the page head and from your RSS, Atom, and other feeds. Version numbers on script and stylesheet addresses, and export files, are left as they are." msgstr "" -#: includes/admin.php:232 +#: includes/admin.php:248 msgid "Hides the version from casual inspection and from scanners that read the tag. It is not real protection, because the version can be worked out in other ways; keeping WordPress up to date matters far more. Nothing on the site looks different." msgstr "" -#: includes/admin.php:233 +#: includes/admin.php:249 msgid "Removes one line from each page and feed. No measurable speed change." msgstr "" -#: includes/admin.php:236 +#: includes/admin.php:252 msgid "Removes the Really Simple Discovery (RSD) link from the page head. Old desktop blogging programs used it to find the site's XML-RPC address." msgstr "" -#: includes/admin.php:237 +#: includes/admin.php:253 msgid "A blogging program that relies on this link may need the XML-RPC address entered by hand. XML-RPC itself keeps working; only the link is removed. Most sites notice no difference." msgstr "" -#: includes/admin.php:238 +#: includes/admin.php:254 msgid "Removes one line from each page. No measurable speed change." msgstr "" -#: includes/admin.php:241 +#: includes/admin.php:257 msgid "Removes the shortlink, the short ?p=123 address of a post, from the page head and from the Link header that WordPress sends with each page." msgstr "" -#: includes/admin.php:242 +#: includes/admin.php:258 msgid "Shortlinks still work when someone uses them; they are only no longer advertised. A tool that reads the shortlink from the page uses the full address instead." msgstr "" -#: includes/admin.php:243 +#: includes/admin.php:259 +#: includes/admin.php:269 msgid "Removes one line from each page and one response header. No measurable speed change." msgstr "" -#: includes/admin.php:246 +#: includes/admin.php:262 msgid "Stops WordPress from printing a small inline style for the classic Recent Comments widget. WordPress prints it only on classic themes that support widgets." msgstr "" -#: includes/admin.php:247 +#: includes/admin.php:263 msgid "The widget's links may look slightly different if your theme relied on that style. Block themes and the Latest Comments block are not affected." msgstr "" -#: includes/admin.php:248 +#: includes/admin.php:264 msgid "Removes a few bytes of CSS from pages that show the widget. No measurable speed change." msgstr "" -#: includes/admin.php:251 +#: includes/admin.php:267 +msgid "Removes the link to the site's REST API address from the page head and from the Link header that WordPress sends with each page." +msgstr "" + +#: includes/admin.php:268 +msgid "The REST API keeps working: the block editor, mobile apps, and plugins that use it are not affected. Only the advertised address is removed, which a few tools and services use to find the API on their own." +msgstr "" + +#: includes/admin.php:272 +msgid "Removes the oEmbed discovery links from the page head of posts and pages. Other sites read them to show one of your posts as an embedded card when someone pastes its link." +msgstr "" + +#: includes/admin.php:273 +msgid "A link to one of your posts pasted into another WordPress site, or into some apps and services, shows as a plain link instead of a card. Embedding videos, posts, and other content from other sites in your own posts keeps working." +msgstr "" + +#: includes/admin.php:274 +msgid "Removes two lines from each post and page. No measurable speed change." +msgstr "" + +#: includes/admin.php:277 +msgid "Removes the links to the site's RSS feeds, for posts, comments, and categories, tags, or authors, from the page head." +msgstr "" + +#: includes/admin.php:278 +msgid "The feeds keep working at their usual addresses, but browsers and feed readers can no longer find them from a page, so someone who subscribes by entering the site's address may need the feed's address instead. Leave this off if people follow the site in a feed reader." +msgstr "" + +#: includes/admin.php:279 +msgid "Removes a few lines from each page. No measurable speed change." +msgstr "" + +#: includes/admin.php:282 +msgid "Tells WordPress that AI features are not supported on this site. WordPress then does not register its built-in AI provider connectors, and refuses AI provider connectors from plugins." +msgstr "" + +#: includes/admin.php:283 +msgid "AI features that rely on WordPress's AI support are unavailable, including those in plugins that check for it. Connectors that are not AI providers, such as Akismet, are not affected. Setting the WP_AI_SUPPORT constant to false in wp-config.php has the same effect." +msgstr "" + +#: includes/admin.php:284 +msgid "No measurable speed change. WordPress only skips registering its AI provider connectors on each request." +msgstr "" + +#: includes/admin.php:287 msgid "Turns off Jetpack's promotional notices, its Just-in-Time upgrade messages, and its Blaze prompts to advertise your posts. It does nothing unless Jetpack is installed." msgstr "" -#: includes/admin.php:252 +#: includes/admin.php:288 msgid "Jetpack keeps working; you no longer see its upgrade and advertising prompts in the admin area. Visitors see no difference." msgstr "" -#: includes/admin.php:253 +#: includes/admin.php:289 msgid "Jetpack skips loading and showing these messages, which saves a little work and some requests in the admin area. No change on the front end." msgstr "" -#: includes/admin.php:256 +#: includes/admin.php:292 msgid "Turns off publishing by email: the mail server settings disappear from Settings > Writing and can no longer be saved, and the wp-mail.php address that collects the mail refuses requests." msgstr "" -#: includes/admin.php:257 +#: includes/admin.php:293 msgid "Nobody can publish posts by sending email to the configured mailbox. Existing posts are not affected. Leave this off if you post by email." msgstr "" -#: includes/admin.php:258 +#: includes/admin.php:294 msgid "No speed change. It closes a rarely used way into the site." msgstr "" -#: includes/admin.php:261 +#: includes/admin.php:297 msgid "Adds a preconnect hint to the page head for each domain in the Preconnect Domains list, so the browser opens a connection to those servers before it needs them. Front-end pages only; the admin area and the login page are skipped." msgstr "" -#: includes/admin.php:262 +#: includes/admin.php:298 msgid "Files from those domains, such as web fonts, can start downloading sooner. A listed domain that the page does not use wastes a connection." msgstr "" -#: includes/admin.php:263 +#: includes/admin.php:299 msgid "Can save one or more network round trips the first time each listed domain is used, which helps most on slow or distant connections. Each hint costs the browser a connection, so list only the few domains that serve files a page needs early. No measurable cost on the server." msgstr "" -#: includes/admin.php:266 +#: includes/admin.php:302 msgid "The domains that get a preconnect hint while Enable Preconnect is on." msgstr "" -#: includes/admin.php:267 +#: includes/admin.php:303 msgid "Only the domains entered here get hints. The suggestions below are examples and are never added on their own." msgstr "" -#: includes/admin.php:268 +#: includes/admin.php:304 msgid "Two or three domains are usually enough. More hints compete for the browser's connections and can slow down the files that matter most." msgstr "" -#: includes/admin.php:271 +#: includes/admin.php:307 msgid "Adds a DNS prefetch hint to the page head for each domain in the DNS Prefetch Domains list, so the browser looks up those domain names before it needs them. Front-end pages only; the admin area and the login page are skipped." msgstr "" -#: includes/admin.php:272 +#: includes/admin.php:308 msgid "Requests to those domains start slightly sooner. Unlike preconnect, no connection is opened, so it suits domains that are used later or only on some pages." msgstr "" -#: includes/admin.php:273 +#: includes/admin.php:309 msgid "Saves the name lookup, often a few tens of milliseconds, the first time each listed domain is used. It costs the browser very little, even for a domain that ends up unused. No measurable cost on the server." msgstr "" -#: includes/admin.php:276 +#: includes/admin.php:312 msgid "The domains that get a DNS prefetch hint while Enable DNS Prefetch is on." msgstr "" -#: includes/admin.php:277 +#: includes/admin.php:313 msgid "Only the domains entered here get hints. The suggestion below is an example and is never added on its own." msgstr "" -#: includes/admin.php:278 +#: includes/admin.php:314 msgid "Lookups are cheap, so a longer list does little harm, but each entry only helps if pages request files from that domain." msgstr "" -#: includes/admin.php:297 +#: includes/admin.php:333 msgid "What it does:" msgstr "" -#: includes/admin.php:298 +#: includes/admin.php:334 msgid "Effect on your site:" msgstr "" -#: includes/admin.php:299 +#: includes/admin.php:335 msgid "Performance:" msgstr "" -#: includes/admin.php:495 -#: includes/admin.php:514 +#: includes/admin.php:531 +#: includes/admin.php:550 msgid "Site Optimizer Settings" msgstr "" -#: includes/admin.php:496 +#: includes/admin.php:532 msgid "Site Optimizer" msgstr "" -#: includes/admin.php:510 +#: includes/admin.php:546 msgid "You do not have sufficient permissions to access this page." msgstr "" -#: includes/admin.php:515 +#: includes/admin.php:551 msgid "Select which optimizations you want to enable and customize the resource hint domains." msgstr "" -#: includes/admin.php:521 +#: includes/admin.php:557 msgid "Save Changes" msgstr "" -#: includes/admin.php:527 +#: includes/admin.php:563 msgid "This plugin is part of the EngineScript project." msgstr "" -#: includes/admin.php:529 +#: includes/admin.php:565 msgid "Visit the EngineScript GitHub page" msgstr "" #. translators: Hidden accessibility text. -#: includes/admin.php:533 +#: includes/admin.php:569 msgid "(opens in a new tab)" msgstr "" -#: includes/admin.php:594 +#: includes/admin.php:630 msgid "This saved list exceeds a size limit. It remains displayed here but adds no resource hints until you shorten and save it." msgstr "" -#: includes/admin.php:596 +#: includes/admin.php:632 msgid "Some saved preconnect entries use unsupported ports. They remain displayed here but are skipped in page hints. Saving removes invalid entries; if none are valid, the list is cleared." msgstr "" -#: includes/admin.php:602 +#: includes/admin.php:638 msgid "Format:" msgstr "" -#: includes/admin.php:604 +#: includes/admin.php:640 msgid "Optional suggestions: copy only the domains your site uses into the field above, one per line. This list starts empty; suggestions are not enabled automatically. Enable the setting and save your list to add hints." msgstr "" -#: includes/admin.php:623 +#: includes/admin.php:659 msgid "Settings" msgstr "" -#: includes/options.php:159 +#: includes/options.php:171 msgid "The submitted settings have an invalid format. Your previous settings were retained." msgstr "" -#: includes/options.php:181 +#: includes/options.php:193 msgid "Enter domains as text. Your previous list was retained." msgstr "" -#: includes/options.php:208 +#: includes/options.php:220 msgid "Your previous list was retained." msgstr "" #. translators: 1: Line number in the submitted domain list, 2: Validation reason, a complete sentence without submitted text. -#: includes/options.php:245 +#: includes/options.php:257 #, php-format msgid "Line %1$d: %2$s" msgstr "" #. translators: %d: Maximum raw bytes allowed in one domain list. -#: includes/options.php:273 +#: includes/options.php:285 #, php-format msgid "Each domain list is limited to %d bytes." msgstr "" -#: includes/options.php:281 +#: includes/options.php:293 msgid "The domain list could not be read." msgstr "" #. translators: %d: Maximum raw bytes allowed in a nonblank domain line. -#: includes/options.php:296 +#: includes/options.php:308 #, php-format msgid "Each nonblank domain line is limited to %d bytes." msgstr "" #. translators: %d: Maximum nonblank lines allowed before removing duplicates or invalid domains. -#: includes/options.php:306 +#: includes/options.php:318 #, php-format msgid "Each domain list is limited to %d nonblank lines, including duplicates and invalid entries." msgstr "" -#: includes/options.php:328 +#: includes/options.php:340 msgid "Preconnect supports HTTPS port 443 only." msgstr "" -#: includes/options.php:348 +#: includes/options.php:360 msgid "Further rejected lines are not listed." msgstr "" #. translators: 1: Total number of rejected lines, 2: At most three reasons with their line numbers, each a complete sentence. -#: includes/options.php:354 +#: includes/options.php:366 #, php-format msgid "%1$d line was rejected. %2$s" msgid_plural "%1$d lines were rejected. %2$s" @@ -391,55 +484,55 @@ msgstr[0] "" msgstr[1] "" #. translators: %s: Safe validation reasons and line numbers, without submitted domains. -#: includes/options.php:365 +#: includes/options.php:377 #, php-format msgid "The preconnect list could not be fully accepted: %s" msgstr "" #. translators: %s: Safe validation reasons and line numbers, without submitted domains. -#: includes/options.php:377 +#: includes/options.php:389 #, php-format msgid "The DNS prefetch list could not be fully accepted: %s" msgstr "" -#: includes/options.php:454 +#: includes/options.php:466 msgid "Encoded characters, backslashes, markup, and control characters are not allowed." msgstr "" -#: includes/options.php:461 +#: includes/options.php:473 msgid "Empty domain." msgstr "" -#: includes/options.php:467 -#: includes/options.php:486 +#: includes/options.php:479 +#: includes/options.php:498 msgid "Invalid domain entry." msgstr "" -#: includes/options.php:557 +#: includes/options.php:569 msgid "HTTPS is required." msgstr "" -#: includes/options.php:561 +#: includes/options.php:573 msgid "No domain name found." msgstr "" -#: includes/options.php:565 +#: includes/options.php:577 msgid "File paths are not allowed; use domains only." msgstr "" -#: includes/options.php:570 +#: includes/options.php:582 msgid "Query parameters, fragments, and credentials are not allowed." msgstr "" -#: includes/options.php:575 +#: includes/options.php:587 msgid "Invalid port." msgstr "" -#: includes/options.php:594 +#: includes/options.php:606 msgid "IP addresses and private, local, or reserved domain names are not allowed." msgstr "" -#: includes/options.php:599 +#: includes/options.php:611 msgid "Invalid domain name." msgstr "" diff --git a/readme.txt b/readme.txt index d4ee854..fe52e32 100644 --- a/readme.txt +++ b/readme.txt @@ -19,10 +19,14 @@ Key features: * Disable WordPress emojis * Remove jQuery Migrate * Disable classic theme styles -* Remove selected header output (version, RSD, shortlinks, recent comments style) +* Remove selected header output (version, RSD, shortlinks, REST API link, oEmbed discovery links, feed links, recent comments style) +* Limit the Heartbeat: off on the front end, every 120 seconds on admin screens outside the editors +* Stop loading patterns from the WordPress.org Pattern Directory +* Turn off WordPress's AI support * DNS prefetch and preconnect management * Disable Jetpack promotional messages and Blaze promotions * Disable post via email +* Every setting explains what it does, its effect on your site, and its performance gain or cost == Installation == @@ -68,6 +72,12 @@ No. The option removes WordPress's generator output from the page head and from == Changelog == = Unreleased = +* Add "Remove REST API Link": remove the REST API address from the page head and the Link response header. The REST API keeps working. +* Add "Remove oEmbed Discovery Links": remove the links other sites read to embed your posts, at both priorities WordPress adds them. +* Add "Remove Feed Links": remove the RSS feed links from the page head. The feeds keep working. +* Add "Limit Heartbeat": stop the Heartbeat script on the front end, and send it every 120 seconds instead of 60 on admin screens other than the post editor, the site editor, and the Customizer. A slower interval set by another plugin is kept. +* Add "Disable Pattern Directory Patterns": stop the block editor from loading patterns from the WordPress.org Pattern Directory. +* Add "Disable WordPress AI Support": answer `false` to WordPress 7's `wp_supports_ai` filter. * On the settings page, only the checkbox now turns a setting on or off. A click on the setting's title or its description, or a drag across it to select the text, no longer changes the setting. * Explain every setting below its control: what it does, its effect on the site, and its performance gain or cost. The explanations replace the one-line descriptions. * Name each checkbox for screen readers by its title alone, with the explanation read as its description. diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f826bd6..9ebbf5a 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -29,6 +29,7 @@ function es_optimizer_test_reset(): void { 'sections' => array(), 'fields' => array(), 'styles' => array(), + 'scripts' => array(), 'capabilities' => array( 'manage_options' ), 'is_admin' => false, 'ajax' => false, @@ -47,7 +48,7 @@ function es_optimizer_test_reset(): void { 'on_delete' => null, 'flushes' => 0, ); - unset( $GLOBALS['_wp_switched_stack'], $GLOBALS['switched'] ); + unset( $GLOBALS['_wp_switched_stack'], $GLOBALS['switched'], $GLOBALS['pagenow'] ); } /** @@ -901,6 +902,18 @@ function wp_deregister_style( string $handle ): void { } } +if ( ! function_exists( 'wp_deregister_script' ) ) { + /** + * Record a deregistered script. + * + * @param string $handle Script handle. + * @return void + */ + function wp_deregister_script( string $handle ): void { + $GLOBALS['es_optimizer_test']['scripts'][] = array( 'deregister', $handle ); + } +} + if ( ! class_exists( 'wpdb' ) ) { /** * Minimal wpdb test double for the uninstall tests. diff --git a/tests/unit/FrontendTest.php b/tests/unit/FrontendTest.php index a8b6634..12a76d7 100644 --- a/tests/unit/FrontendTest.php +++ b/tests/unit/FrontendTest.php @@ -151,6 +151,90 @@ public function test_header_cleanup_per_option(): void { $this->store_options( array( 'remove_shortlink' => 1 ) ); es_optimizer_remove_header_items(); $this->assertSame( array( 'wp_head wp_shortlink_wp_head 10', 'template_redirect wp_shortlink_header 11' ), $this->removals() ); + + es_optimizer_test_reset(); + $this->store_options( array( 'remove_rest_api_link' => 1 ) ); + es_optimizer_remove_header_items(); + $this->assertSame( array( 'wp_head rest_output_link_wp_head 10', 'template_redirect rest_output_link_header 11' ), $this->removals() ); + + es_optimizer_test_reset(); + $this->store_options( array( 'remove_oembed_links' => 1 ) ); + es_optimizer_remove_header_items(); + $this->assertSame( array( 'wp_head wp_oembed_add_discovery_links 4', 'wp_head wp_oembed_add_discovery_links 10' ), $this->removals(), 'WordPress hooks the discovery links twice.' ); + + es_optimizer_test_reset(); + $this->store_options( array( 'remove_feed_links' => 1 ) ); + es_optimizer_remove_header_items(); + $this->assertSame( array( 'wp_head feed_links 2', 'wp_head feed_links_extra 3' ), $this->removals() ); + } + + /** + * The pattern-directory and AI filters answer false when their option is on, and pass the value through when it is off. + * + * @return void + */ + public function test_remote_patterns_and_ai_support_filters(): void { + foreach ( array( true, null, 'foreign' ) as $incoming ) { + $this->assertSame( $incoming, es_optimizer_filter_remote_patterns( $incoming ) ); + $this->assertSame( $incoming, es_optimizer_filter_ai_support( $incoming ) ); + } + + $this->store_options( array( 'disable_remote_patterns' => 1 ) ); + $this->assertFalse( es_optimizer_filter_remote_patterns( true ) ); + $this->assertTrue( es_optimizer_filter_ai_support( true ), 'Each filter follows its own option.' ); + + $this->store_options( array( 'disable_ai_features' => 1 ) ); + $this->assertFalse( es_optimizer_filter_ai_support( true ) ); + $this->assertTrue( es_optimizer_filter_remote_patterns( true ) ); + } + + /** + * Heartbeat is deregistered on the front end only, with the option on. + * + * @return void + */ + public function test_frontend_heartbeat(): void { + es_optimizer_disable_frontend_heartbeat(); + $this->assertSame( array(), $GLOBALS['es_optimizer_test']['scripts'], 'Option off.' ); + + $this->store_options( array( 'heartbeat_control' => 1 ) ); + $GLOBALS['es_optimizer_test']['is_admin'] = true; + es_optimizer_disable_frontend_heartbeat(); + $this->assertSame( array(), $GLOBALS['es_optimizer_test']['scripts'], 'Not in the admin area.' ); + + $GLOBALS['es_optimizer_test']['is_admin'] = false; + es_optimizer_disable_frontend_heartbeat(); + $this->assertSame( array( array( 'deregister', 'heartbeat' ) ), $GLOBALS['es_optimizer_test']['scripts'] ); + } + + /** + * In the admin area, outside the editors, Heartbeat slows to 120 seconds; a slower setting is kept. + * + * @return void + */ + public function test_admin_heartbeat_interval(): void { + global $pagenow; + + $GLOBALS['es_optimizer_test']['is_admin'] = true; + $pagenow = 'index.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- The test sets the admin page WordPress would set. + + $this->assertSame( array( 'nonce' => 'n' ), es_optimizer_filter_heartbeat_settings( array( 'nonce' => 'n' ) ), 'Option off.' ); + + $this->store_options( array( 'heartbeat_control' => 1 ) ); + $this->assertSame( array( 'nonce' => 'n', 'interval' => 120 ), es_optimizer_filter_heartbeat_settings( array( 'nonce' => 'n' ) ) ); + $this->assertSame( array( 'interval' => 120 ), es_optimizer_filter_heartbeat_settings( array( 'interval' => 15 ) ) ); + $this->assertSame( array( 'interval' => 300 ), es_optimizer_filter_heartbeat_settings( array( 'interval' => 300 ) ), 'A slower interval from another plugin is kept.' ); + $this->assertSame( array( 'interval' => 120 ), es_optimizer_filter_heartbeat_settings( array( 'interval' => 'fast' ) ) ); + $this->assertNull( es_optimizer_filter_heartbeat_settings( null ), 'A foreign non-array passes through.' ); + + foreach ( array( 'post.php', 'post-new.php', 'site-editor.php', 'customize.php' ) as $editor ) { + $pagenow = $editor; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- The test sets the admin page WordPress would set. + $this->assertSame( array( 'interval' => 15 ), es_optimizer_filter_heartbeat_settings( array( 'interval' => 15 ) ), $editor ); + } + + $GLOBALS['es_optimizer_test']['is_admin'] = false; + $pagenow = 'index.php'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- The test sets the admin page WordPress would set. + $this->assertSame( array(), es_optimizer_filter_heartbeat_settings( array() ), 'The front end is handled by deregistering the script.' ); } /** diff --git a/tests/unit/OptionsAndNoticesTest.php b/tests/unit/OptionsAndNoticesTest.php index 3007d46..8cc50c7 100644 --- a/tests/unit/OptionsAndNoticesTest.php +++ b/tests/unit/OptionsAndNoticesTest.php @@ -20,7 +20,7 @@ final class OptionsAndNoticesTest extends EsOptimizerTestCase { public function test_defaults(): void { $defaults = es_optimizer_get_default_options(); - $this->assertCount( 13, $defaults ); + $this->assertCount( 19, $defaults ); $this->assertSame( es_optimizer_get_boolean_option_keys(), array_keys( array_diff_key( $defaults, array_flip( array( 'preconnect_domains', 'dns_prefetch_domains' ) ) ) ) ); $this->assertSame( array( 0 ), array_values( array_unique( array_intersect_key( $defaults, array_flip( es_optimizer_get_boolean_option_keys() ) ) ) ) ); $this->assertSame( '', $defaults['preconnect_domains'] ); diff --git a/tests/unit/PluginWiringTest.php b/tests/unit/PluginWiringTest.php index a61e7b9..60f53c1 100644 --- a/tests/unit/PluginWiringTest.php +++ b/tests/unit/PluginWiringTest.php @@ -51,6 +51,10 @@ public function test_front_end_start_up_registers_every_callback(): void { array( 'wp_resource_hints', 'es_optimizer_add_preconnect_resource_hints', 10, 2 ), array( 'wp_resource_hints', 'es_optimizer_add_dns_prefetch_resource_hints', 10, 2 ), array( 'tiny_mce_plugins', 'es_optimizer_disable_emojis_tinymce', 10, 1 ), + array( 'should_load_remote_block_patterns', 'es_optimizer_filter_remote_patterns', PHP_INT_MAX, 1 ), + array( 'wp_supports_ai', 'es_optimizer_filter_ai_support', PHP_INT_MAX, 1 ), + array( 'wp_enqueue_scripts', 'es_optimizer_disable_frontend_heartbeat', 100, 1 ), + array( 'heartbeat_settings', 'es_optimizer_filter_heartbeat_settings', PHP_INT_MAX, 1 ), array( 'plugin_action_links_enginescript-site-optimizer.php', 'es_optimizer_add_settings_link', 10, 1 ), ); diff --git a/tests/unit/SettingsPageTest.php b/tests/unit/SettingsPageTest.php index 4e07534..006adef 100644 --- a/tests/unit/SettingsPageTest.php +++ b/tests/unit/SettingsPageTest.php @@ -95,9 +95,9 @@ public function test_sections_and_fields_cover_every_option(): void { $this->assertSame( array( 'option' => $field['id'], 'class' => 'es-optimizer-field' ), $field['args'], $field['id'] ); $this->assertSame( 'es_optimizer_render_checkbox_field', $field['callback'], $field['id'] ); } - $this->assertSame( array( 'disable_emojis', 'remove_jquery_migrate', 'disable_classic_theme_styles' ), $by_section['es_optimizer_performance'] ); - $this->assertSame( array( 'remove_wp_version', 'remove_rsd_link', 'remove_shortlink', 'remove_recent_comments_style' ), $by_section['es_optimizer_header_cleanup'] ); - $this->assertSame( array( 'disable_jetpack_ads', 'disable_post_via_email', 'enable_preconnect', 'preconnect_domains', 'enable_dns_prefetch', 'dns_prefetch_domains' ), $by_section['es_optimizer_additional_features'] ); + $this->assertSame( array( 'disable_emojis', 'remove_jquery_migrate', 'disable_classic_theme_styles', 'heartbeat_control', 'disable_remote_patterns' ), $by_section['es_optimizer_performance'] ); + $this->assertSame( array( 'remove_wp_version', 'remove_rsd_link', 'remove_shortlink', 'remove_recent_comments_style', 'remove_rest_api_link', 'remove_oembed_links', 'remove_feed_links' ), $by_section['es_optimizer_header_cleanup'] ); + $this->assertSame( array( 'disable_jetpack_ads', 'disable_post_via_email', 'disable_ai_features', 'enable_preconnect', 'preconnect_domains', 'enable_dns_prefetch', 'dns_prefetch_domains' ), $by_section['es_optimizer_additional_features'] ); } /** @@ -147,7 +147,7 @@ public function test_page_prints_the_form_and_the_footer_link(): void { $this->assertStringContainsString( '

Site Optimizer Settings

', $page ); $this->assertStringContainsString( '
', $page ); $this->assertStringContainsString( 'name="option_page" value="es_optimizer_settings"', $page ); - $this->assertSame( 11, substr_count( $page, 'type="checkbox"' ) ); + $this->assertSame( 17, substr_count( $page, 'type="checkbox"' ) ); $this->assertSame( 2, substr_count( $page, '