Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 312 additions & 0 deletions .github/workflows/wp-compatibility-test.yml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- An Autoloaded Options tab on the Site Optimizer settings page:
- lists the options WordPress loads on every page, largest first, with each option's size, its autoload setting, and the plugin or theme it probably belongs to;
- stops loading the selected options on every page, and loads them again later with the exact setting each had before;
- changes only the autoload setting, never a value, and keeps options that WordPress and Site Optimizer need locked;
- shows when other code has loaded a switched-off option again or deleted it.
- Evidence of who owns an option, which WordPress does not record:
- the guess matches installed plugins and themes by folder name and text domain;
- on request, a search of the installed plugins' and themes' PHP files shows which one uses each name on the page, and marks names found nowhere as probably left behind by a removed plugin;
- an optional recording, for 10 minutes or an hour, counts which options the pages loaded meanwhile read, and which plugin or theme read each one.
- After options are switched off, the plugin loads the home page and the dashboard, and undoes the change at once if either answers with a server error.
- WP-CLI commands `wp site-optimizer autoload list` and `wp site-optimizer autoload restore [<option>...] [--all]`. The restore also works on a site a plugin breaks, with every plugin and theme skipped (see the FAQ).
- Deleting the plugin first restores every option it switched off, on every site of a network. Deactivating changes nothing. While changes exist, the Autoloaded Options tab and the plugin's row on the Plugins screen say so.
- "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.
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A WordPress plugin for disabling selected features and configuring resource hint
- **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
- **Autoloaded Options:** A tab on the settings page that lists the options WordPress loads on every page, largest first, with the plugin or theme each probably belongs to (from folder names, a search of the installed code, or a recording of which options pages read); stop loading the ones you select, with an automatic check that undoes a change that breaks the home page or the dashboard, and WP-CLI commands to restore everything
- **Explained Settings:** Every setting says what it does, its effect on the site, and its performance gain or cost

## Installation
Expand Down Expand Up @@ -126,6 +127,28 @@ fails, the plugin files stay in place and the message gives the reason and, on
a network, the IDs of the sites that could not be cleaned. Resolve the reported
problem and retry removal.

Options you stopped from loading on every page on the Autoloaded Options tab
are loaded on every page again before anything is deleted, with the exact
autoload setting each one had, on every site of a network. An option whose
setting other code has changed since is left alone. If an option cannot be
restored, nothing on that site is deleted and the uninstall stops. Deactivation
changes nothing. While any option is switched off, the Autoloaded Options
tab and the plugin's row on the Plugins screen say so.

### What if switching an autoloaded option off breaks my site?

It rarely does, because code that needs the option still gets it. After each
change the plugin loads the home page and the dashboard, and undoes the change
if either answers with a server error. To undo every change from the command
line, run `wp site-optimizer autoload restore --all`. If a plugin keeps WP-CLI
from starting, skip every plugin and theme and load only the command:

```bash
wp --skip-plugins --skip-themes \
--require=wp-content/plugins/enginescript-site-optimizer/includes/class-es-optimizer-autoload-command.php \
site-optimizer autoload restore --all
```

## Development

### Requirements
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"automattic/vipwpcs": "^3.0.1",
"z4kn4fein/php-semver": "^3.0",
"rector/rector": "^2.6",
"slevomat/coding-standard": "^8.22.1"
"slevomat/coding-standard": "^8.22.1",
"php-stubs/wp-cli-stubs": "^2.12"
},
"scripts": {
"phpcs": "phpcs --standard=phpcs-wordpress.xml",
Expand Down
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions enginescript-site-optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
}

require_once __DIR__ . '/includes/options.php';
require_once __DIR__ . '/includes/autoload.php';
require_once __DIR__ . '/includes/autoload-insight.php';
require_once __DIR__ . '/includes/autoload-page.php';
require_once __DIR__ . '/includes/admin.php';
require_once __DIR__ . '/includes/frontend.php';
require_once __DIR__ . '/includes/bootstrap.php';

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/includes/class-es-optimizer-autoload-command.php';
}
85 changes: 76 additions & 9 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,58 @@ function es_optimizer_register_textarea_field( string $page, string $section, st
* @since 1.0.0
*/
function es_optimizer_add_settings_page(): void {
add_options_page(
$hook = add_options_page(
esc_html__( 'Site Optimizer Settings', 'enginescript-site-optimizer' ),
esc_html__( 'Site Optimizer', 'enginescript-site-optimizer' ),
'manage_options',
'es-optimizer-settings',
'es_optimizer_settings_page'
);

// The Autoloaded Options tab posts its changes back to this page.
if ( is_string( $hook ) && '' !== $hook ) {
add_action( "load-{$hook}", 'es_optimizer_autoload_handle_request' );
}
}

/**
* Get the tab of the settings page that is shown.
*
* @since 2.5.0
* @SuppressWarnings("PHPMD.Superglobals") Reads only which tab to show, reduced to one of two known values.
* @return string 'settings' or 'autoload'.
*/
function es_optimizer_get_settings_tab(): string {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only choice of tab.
return isset( $_GET['tab'] ) && is_string( $_GET['tab'] ) && 'autoload' === sanitize_key( wp_unslash( $_GET['tab'] ) ) ? 'autoload' : 'settings';
}

/**
* Print the tabs of the settings page.
*
* @since 2.5.0
* @param string $current Tab shown.
*/
function es_optimizer_render_settings_tabs( string $current ): void {
$tabs = [
'settings' => [ admin_url( 'options-general.php?page=es-optimizer-settings' ), __( 'Settings', 'enginescript-site-optimizer' ) ],
'autoload' => [ es_optimizer_autoload_page_url(), __( 'Autoloaded Options', 'enginescript-site-optimizer' ) ],
];

printf( '<nav class="nav-tab-wrapper" aria-label="%s">', esc_attr__( 'Site Optimizer sections', 'enginescript-site-optimizer' ) );

foreach ( $tabs as $key => [ $url, $label ] ) {
$active = $key === $current;
printf(
'<a href="%1$s" class="nav-tab%2$s"%3$s>%4$s</a>',
esc_url( $url ),
$active ? ' nav-tab-active' : '',
$active ? ' aria-current="page"' : '',
esc_html( $label )
);
}

echo '</nav>';
}

/**
Expand All @@ -545,18 +590,22 @@ function es_optimizer_settings_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'enginescript-site-optimizer' ) );
}

$tab = es_optimizer_get_settings_tab();
?>
<div class="wrap">
<h1><?php esc_html_e( 'Site Optimizer Settings', 'enginescript-site-optimizer' ); ?></h1>
<p><?php esc_html_e( 'Select which optimizations you want to enable and customize the resource hint domains.', 'enginescript-site-optimizer' ); ?></p>
<?php
es_optimizer_render_settings_tabs( $tab );

<form method="post" action="options.php">
<?php
settings_fields( 'es_optimizer_settings' );
do_settings_sections( 'es-optimizer-settings' );
submit_button( __( 'Save Changes', 'enginescript-site-optimizer' ) );
?>
</form>
if ( 'autoload' === $tab ) {
es_optimizer_autoload_render_tab();
}

if ( 'settings' === $tab ) {
es_optimizer_render_settings_form();
}
?>

<hr>
<p>
Expand All @@ -575,6 +624,24 @@ function es_optimizer_settings_page(): void {
<?php
}

/**
* Print the settings form.
*
* @since 2.5.0
*/
function es_optimizer_render_settings_form(): void {
?>
<p><?php esc_html_e( 'Select which optimizations you want to enable and customize the resource hint domains.', 'enginescript-site-optimizer' ); ?></p>
<form method="post" action="options.php">
<?php
settings_fields( 'es_optimizer_settings' );
do_settings_sections( 'es-optimizer-settings' );
submit_button( __( 'Save Changes', 'enginescript-site-optimizer' ) );
?>
</form>
<?php
}

/**
* Render a checkbox option field.
*
Expand Down
Loading
Loading