v3.0.1
This commit is contained in:
parent
8d5551a8b5
commit
ef4259b3c2
4 changed files with 634 additions and 94 deletions
|
|
@ -1,5 +1,45 @@
|
||||||
== Changelog ==
|
== Changelog ==
|
||||||
|
|
||||||
|
= 3.0.1 [2026-07-18] =
|
||||||
|
|
||||||
|
**Fixed**
|
||||||
|
|
||||||
|
* Self-healing v3 migration. If `noindex_seo_config_version` claims migration completed (version >= 3) but the consolidated `noindex_seo_settings` option is somehow missing, `noindex_seo_check_migration()` now re-runs the migration automatically. This recovers silently from partial failures: object-cache staleness after the write, request interrupted mid-migration, a third-party plugin clearing options, or DB write errors that did not raise a PHP error. Without this guard, the front-end would fall back to all-zero defaults (site fully indexable) until someone re-saved the settings page.
|
||||||
|
|
||||||
|
**Compatibility**
|
||||||
|
|
||||||
|
* WordPress: 5.7 - 7.1
|
||||||
|
* PHP: 7.2 - 8.5
|
||||||
|
|
||||||
|
**Tests**
|
||||||
|
|
||||||
|
* Added test for the self-healing migration path.
|
||||||
|
|
||||||
|
= 3.0.0 [2026-07-18] =
|
||||||
|
|
||||||
|
**Major release — storage consolidation.** Settings are now stored in a single autoloaded option (`noindex_seo_settings`) instead of ~110 individual options. Reduces `wp_options` autoload weight on every page load. Legacy options are preserved (marked non-autoloaded) for one major version to allow safe rollback to 2.x; they will be removed in 4.0.
|
||||||
|
|
||||||
|
**Added**
|
||||||
|
|
||||||
|
* **"Apply recommended defaults" button** on the settings page. One click enables all 12 contexts flagged as "Recommended" in the UI (privacy policy, date archives, pagination, search results, attachment pages, preview/customize-preview, and 404). Previously required ~60 individual checkbox toggles.
|
||||||
|
* **Filterable conflict-detection list.** The list of plugins that may conflict with noindex SEO is now passed through `apply_filters('noindex_seo_conflicting_plugins', ...)`. Third-party code can extend or replace the list without editing the plugin.
|
||||||
|
|
||||||
|
**Changed**
|
||||||
|
|
||||||
|
* **Storage consolidated**: 110 directive options (`{directive}_seo_{context}`) + 5 config options (`noindex_seo_config_*`) merged into one `noindex_seo_settings` array. Reads go through new accessors (`noindex_seo_get_settings()`, `noindex_seo_get_setting()`, `noindex_seo_get_config()`). The hourly transient cache is preserved on top.
|
||||||
|
* **Transient invalidation is now automatic**: any update to `noindex_seo_settings` clears the `noindex_seo_options` transient via the `update_option_noindex_seo_settings` hook. Previously only the admin form save cleared it, leaving WP-CLI / REST / programmatic edits stale for up to one hour.
|
||||||
|
* **Migration**: existing 2.x installs are migrated automatically on `plugins_loaded` (idempotent, version-gated by `noindex_seo_config_version`). Legacy options are kept and marked non-autoloaded so a rollback to 2.x restores the pre-3.0 state.
|
||||||
|
* **Uninstall retrocompatibility**: `uninstall.php` deletes both the new consolidated option and all legacy individual options (via wildcard SQL). Sites that upgrade from 2.x and later uninstall will be cleaned up completely regardless of whether migration ran.
|
||||||
|
|
||||||
|
**Compatibility**
|
||||||
|
|
||||||
|
* WordPress: 5.7 - 7.1
|
||||||
|
* PHP: 7.2 - 8.5
|
||||||
|
|
||||||
|
**Tests**
|
||||||
|
|
||||||
|
* Added PHPUnit test suite (previously the project shipped only a plugin-header smoke test). Coverage: contexts helper, settings accessors, v2→v3 migration, form save, bulk actions, meta box save, conflict-detection filter.
|
||||||
|
|
||||||
= 2.0.2 [2026-07-18] =
|
= 2.0.2 [2026-07-18] =
|
||||||
|
|
||||||
**Fixed**
|
**Fixed**
|
||||||
|
|
|
||||||
569
noindex-seo.php
569
noindex-seo.php
|
|
@ -5,7 +5,7 @@
|
||||||
* Description: Control search engine indexing with robots directives (noindex, nofollow, noarchive, nosnippet, noimageindex) for specific parts of your WordPress site.
|
* Description: Control search engine indexing with robots directives (noindex, nofollow, noarchive, nosnippet, noimageindex) for specific parts of your WordPress site.
|
||||||
* Requires at least: 5.7
|
* Requires at least: 5.7
|
||||||
* Requires PHP: 7.2
|
* Requires PHP: 7.2
|
||||||
* Version: 2.0.2
|
* Version: 3.0.1
|
||||||
* Author: ROBOTSTXT
|
* Author: ROBOTSTXT
|
||||||
* Author URI: https://www.robotstxt.es/
|
* Author URI: https://www.robotstxt.es/
|
||||||
* License: GPL-3.0-or-later
|
* License: GPL-3.0-or-later
|
||||||
|
|
@ -25,8 +25,170 @@ defined( 'ABSPATH' ) || die( 'Bye bye!' );
|
||||||
*
|
*
|
||||||
* @since 2.0.1
|
* @since 2.0.1
|
||||||
* @since 2.0.2 Bumped to 2.0.2 for bulk-action postmeta fix and dead-context cleanup.
|
* @since 2.0.2 Bumped to 2.0.2 for bulk-action postmeta fix and dead-context cleanup.
|
||||||
|
* @since 3.0.0 Major release: storage consolidated into a single autoloaded option
|
||||||
|
* (`noindex_seo_settings`); legacy options kept as non-autoloaded for
|
||||||
|
* rollback safety. Filterable conflict-detection list. One-click
|
||||||
|
* "Apply recommended defaults" button.
|
||||||
|
* @since 3.0.1 Self-healing migration: re-runs the v3 migration if the
|
||||||
|
* consolidated option is missing despite the version claim.
|
||||||
*/
|
*/
|
||||||
define( 'NOINDEX_SEO_VERSION', '2.0.2' );
|
define( 'NOINDEX_SEO_VERSION', '3.0.1' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Option key holding the consolidated plugin settings array.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*/
|
||||||
|
define( 'NOINDEX_SEO_SETTINGS_KEY', 'noindex_seo_settings' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the full consolidated settings array, merged with defaults.
|
||||||
|
*
|
||||||
|
* The shape is:
|
||||||
|
*
|
||||||
|
* [
|
||||||
|
* 'version' => 3,
|
||||||
|
* 'contexts' => [ '<context>' => [ '<directive>' => 0|1, ... ], ... ],
|
||||||
|
* 'config' => [ 'method' => 'meta', 'granular' => 0, ... ],
|
||||||
|
* ]
|
||||||
|
*
|
||||||
|
* The array is cached in a static variable per-request after the first read.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param bool $force_refresh Optional. Bypass the in-memory cache. Default false.
|
||||||
|
* @return array{
|
||||||
|
* version: int,
|
||||||
|
* contexts: array<string, array<string, int>>,
|
||||||
|
* config: array{
|
||||||
|
* method: string,
|
||||||
|
* granular: int,
|
||||||
|
* seoplugins: int,
|
||||||
|
* delete_on_uninstall: int
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
function noindex_seo_get_settings( bool $force_refresh = false ): array {
|
||||||
|
static $cached = null;
|
||||||
|
|
||||||
|
if ( null !== $cached && ! $force_refresh ) {
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
$defaults = array(
|
||||||
|
'version' => 3,
|
||||||
|
'contexts' => array(),
|
||||||
|
'config' => array(
|
||||||
|
'method' => 'meta',
|
||||||
|
'granular' => 0,
|
||||||
|
'seoplugins' => 0,
|
||||||
|
'delete_on_uninstall' => 0,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Seed every context × directive combination with 0.
|
||||||
|
foreach ( noindex_seo_get_contexts() as $context ) {
|
||||||
|
$defaults['contexts'][ $context ] = array(
|
||||||
|
'noindex' => 0,
|
||||||
|
'nofollow' => 0,
|
||||||
|
'noarchive' => 0,
|
||||||
|
'nosnippet' => 0,
|
||||||
|
'noimageindex' => 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$saved = get_option( NOINDEX_SEO_SETTINGS_KEY, array() );
|
||||||
|
$saved = is_array( $saved ) ? $saved : array();
|
||||||
|
|
||||||
|
// Merge saved contexts without reordering defaults (so iteration is stable).
|
||||||
|
if ( isset( $saved['contexts'] ) && is_array( $saved['contexts'] ) ) {
|
||||||
|
foreach ( $defaults['contexts'] as $ctx => $directive_defaults ) {
|
||||||
|
if ( isset( $saved['contexts'][ $ctx ] ) && is_array( $saved['contexts'][ $ctx ] ) ) {
|
||||||
|
$saved_ctx = $saved['contexts'][ $ctx ];
|
||||||
|
foreach ( $directive_defaults as $dir => $default_value ) {
|
||||||
|
$defaults['contexts'][ $ctx ][ $dir ] = isset( $saved_ctx[ $dir ] )
|
||||||
|
? ( is_numeric( $saved_ctx[ $dir ] ) ? (int) $saved_ctx[ $dir ] : $default_value )
|
||||||
|
: $default_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $saved['config'] ) && is_array( $saved['config'] ) ) {
|
||||||
|
foreach ( $defaults['config'] as $key => $default_value ) {
|
||||||
|
if ( ! isset( $saved['config'][ $key ] ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$saved_config_value = $saved['config'][ $key ];
|
||||||
|
if ( is_int( $default_value ) ) {
|
||||||
|
$defaults['config'][ $key ] = is_numeric( $saved_config_value )
|
||||||
|
? (int) $saved_config_value
|
||||||
|
: $default_value;
|
||||||
|
} elseif ( is_string( $saved_config_value ) ) {
|
||||||
|
$defaults['config'][ $key ] = $saved_config_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( isset( $saved['version'] ) && is_numeric( $saved['version'] ) ) {
|
||||||
|
$defaults['version'] = (int) $saved['version'];
|
||||||
|
}
|
||||||
|
|
||||||
|
$cached = $defaults;
|
||||||
|
return $cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a single directive value for a context from the consolidated settings.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param string $context Context slug (e.g. 'single', 'attachment').
|
||||||
|
* @param string $directive Directive slug (noindex|nofollow|noarchive|nosnippet|noimageindex).
|
||||||
|
* @return int 1 if the directive is active for the context, 0 otherwise.
|
||||||
|
*/
|
||||||
|
function noindex_seo_get_setting( string $context, string $directive ): int {
|
||||||
|
$settings = noindex_seo_get_settings();
|
||||||
|
if ( ! isset( $settings['contexts'][ $context ][ $directive ] ) ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return $settings['contexts'][ $context ][ $directive ] ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a single value from the plugin's "config" sub-array.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param string $key Config key (method|granular|seoplugins|delete_on_uninstall).
|
||||||
|
* @param mixed $fallback Default if the key is missing.
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
function noindex_seo_get_config( string $key, $fallback = null ) {
|
||||||
|
$settings = noindex_seo_get_settings();
|
||||||
|
if ( ! isset( $settings['config'][ $key ] ) ) {
|
||||||
|
return $fallback;
|
||||||
|
}
|
||||||
|
return $settings['config'][ $key ];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Persists the full settings array and invalidates the runtime cache.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $settings Settings array (same shape as noindex_seo_get_settings()).
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function noindex_seo_update_settings( array $settings ): void {
|
||||||
|
// Keep the version tag current.
|
||||||
|
$settings['version'] = 3;
|
||||||
|
update_option( NOINDEX_SEO_SETTINGS_KEY, $settings );
|
||||||
|
|
||||||
|
// Reset the static cache inside noindex_seo_get_settings() so the next read
|
||||||
|
// picks up the new values without waiting for a new request.
|
||||||
|
noindex_seo_get_settings( true );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Outputs robots directives using the configured implementation method.
|
* Outputs robots directives using the configured implementation method.
|
||||||
|
|
@ -189,7 +351,7 @@ function noindex_seo_show(): void {
|
||||||
// PRIORITY 1: Check for per-post/page override (granular control).
|
// PRIORITY 1: Check for per-post/page override (granular control).
|
||||||
// If granular control is enabled and we're on a singular post/page,
|
// If granular control is enabled and we're on a singular post/page,
|
||||||
// check if there's an override for this specific content.
|
// check if there's an override for this specific content.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( $granular_enabled && is_singular() ) {
|
if ( $granular_enabled && is_singular() ) {
|
||||||
$post_id = get_queried_object_id();
|
$post_id = get_queried_object_id();
|
||||||
if ( $post_id ) {
|
if ( $post_id ) {
|
||||||
|
|
@ -209,7 +371,7 @@ function noindex_seo_show(): void {
|
||||||
|
|
||||||
// Apply post-specific directives if any are enabled.
|
// Apply post-specific directives if any are enabled.
|
||||||
if ( ! empty( $post_directives ) ) {
|
if ( ! empty( $post_directives ) ) {
|
||||||
$opt = get_option( 'noindex_seo_config_method', 'meta' );
|
$opt = noindex_seo_get_config( 'method', 'meta' );
|
||||||
$implementation_method = is_string( $opt ) ? $opt : 'meta';
|
$implementation_method = is_string( $opt ) ? $opt : 'meta';
|
||||||
noindex_seo_metarobots( $implementation_method, $post_directives );
|
noindex_seo_metarobots( $implementation_method, $post_directives );
|
||||||
return; // Exit early - post meta takes precedence over global settings.
|
return; // Exit early - post meta takes precedence over global settings.
|
||||||
|
|
@ -226,14 +388,16 @@ function noindex_seo_show(): void {
|
||||||
$available_directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
$available_directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
||||||
|
|
||||||
if ( false === $options || ! is_array( $options ) || empty( $options ) ) {
|
if ( false === $options || ! is_array( $options ) || empty( $options ) ) {
|
||||||
// Transient not set, retrieve options from the database.
|
// Transient not set; pull directive values from the consolidated settings.
|
||||||
|
// The cache stores them keyed by the legacy `{directive}_seo_{context}`
|
||||||
|
// option name so the rest of noindex_seo_show() does not need to know
|
||||||
|
// about the storage layout change introduced in 3.0.
|
||||||
$options = array();
|
$options = array();
|
||||||
|
|
||||||
foreach ( $contexts as $context => $option_key ) {
|
foreach ( $contexts as $context => $option_key ) {
|
||||||
// Load all directives for each context.
|
|
||||||
foreach ( $available_directives as $directive ) {
|
foreach ( $available_directives as $directive ) {
|
||||||
$directive_key = str_replace( 'noindex', $directive, (string) $option_key );
|
$directive_key = str_replace( 'noindex', $directive, (string) $option_key );
|
||||||
$options[ $directive_key ] = get_option( $directive_key, 0 );
|
$options[ $directive_key ] = noindex_seo_get_setting( $context, $directive );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -273,7 +437,7 @@ function noindex_seo_show(): void {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get implementation method configuration.
|
// Get implementation method configuration.
|
||||||
$opt = get_option( 'noindex_seo_config_method', 'meta' );
|
$opt = noindex_seo_get_config( 'method', 'meta' );
|
||||||
$implementation_method = is_string( $opt ) ? $opt : 'meta';
|
$implementation_method = is_string( $opt ) ? $opt : 'meta';
|
||||||
|
|
||||||
// Iterate through the contexts and collect active directives.
|
// Iterate through the contexts and collect active directives.
|
||||||
|
|
@ -312,7 +476,18 @@ function noindex_seo_show(): void {
|
||||||
* If the version is less than 2 (or doesn't exist), it migrates old single-directive options
|
* If the version is less than 2 (or doesn't exist), it migrates old single-directive options
|
||||||
* to the new multi-directive system introduced in version 2.0.
|
* to the new multi-directive system introduced in version 2.0.
|
||||||
*
|
*
|
||||||
|
* If the version is less than 3, it migrates the 110 individual `{directive}_seo_{context}`
|
||||||
|
* options plus the five `noindex_seo_config_*` options into a single consolidated
|
||||||
|
* `noindex_seo_settings` array (introduced in 3.0.0).
|
||||||
|
*
|
||||||
|
* Self-healing: even if the version claims migration is complete, the function
|
||||||
|
* re-runs the v3 migration if the consolidated option is somehow missing. This
|
||||||
|
* guards against partial failures (object cache staleness, request interruption
|
||||||
|
* mid-migration, third-party plugins clearing options) that would otherwise leave
|
||||||
|
* the site with no settings until manual intervention.
|
||||||
|
*
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
|
* @since 3.0.0 Added v2→v3 storage consolidation step and self-healing.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|
@ -323,6 +498,16 @@ function noindex_seo_check_migration(): void {
|
||||||
if ( $current_config_version < 2 ) {
|
if ( $current_config_version < 2 ) {
|
||||||
noindex_seo_migrate_to_v2();
|
noindex_seo_migrate_to_v2();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the v3 migration if version < 3 OR if the consolidated option is
|
||||||
|
// somehow missing despite the version claiming we already migrated.
|
||||||
|
// Self-healing: this catches scenarios where a previous migration bumped
|
||||||
|
// the version but did not persist the consolidated option (object cache
|
||||||
|
// staleness, request interrupted mid-migration, third-party delete, etc.).
|
||||||
|
$consolidated_exists = false !== get_option( NOINDEX_SEO_SETTINGS_KEY );
|
||||||
|
if ( $current_config_version < 3 || ! $consolidated_exists ) {
|
||||||
|
noindex_seo_migrate_to_v3();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -368,6 +553,114 @@ function noindex_seo_migrate_to_v2(): void {
|
||||||
delete_transient( 'noindex_seo_options' );
|
delete_transient( 'noindex_seo_options' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrates configuration from version 2.x to version 3.0.
|
||||||
|
*
|
||||||
|
* Version 2.x stored settings across ~110 individual options: one option per
|
||||||
|
* (directive, context) pair plus five `noindex_seo_config_*` options. Each of
|
||||||
|
* those rows was autoloaded on every WordPress request.
|
||||||
|
*
|
||||||
|
* Version 3.0 consolidates them into a single autoloaded array option,
|
||||||
|
* `noindex_seo_settings`, that the rest of the plugin reads through
|
||||||
|
* `noindex_seo_get_settings()` / `noindex_seo_get_setting()` /
|
||||||
|
* `noindex_seo_get_config()`.
|
||||||
|
*
|
||||||
|
* Migration is idempotent and safe to re-run:
|
||||||
|
*
|
||||||
|
* 1. Reads every legacy option (defaulting to 0 / 'meta' as appropriate).
|
||||||
|
* 2. Builds the consolidated array.
|
||||||
|
* 3. Writes the array via `update_option()` (autoloaded by default).
|
||||||
|
* 4. Rewrites each legacy option with autoload=false so it stops counting
|
||||||
|
* against the per-request autoload budget but remains readable for a
|
||||||
|
* safe rollback to 2.x. These will be deleted in 4.0.
|
||||||
|
* 5. Bumps `noindex_seo_config_version` to 3.
|
||||||
|
* 6. Clears the transient cache.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function noindex_seo_migrate_to_v3(): void {
|
||||||
|
$contexts = noindex_seo_get_contexts();
|
||||||
|
$directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
||||||
|
|
||||||
|
$consolidated = array(
|
||||||
|
'version' => 3,
|
||||||
|
'contexts' => array(),
|
||||||
|
'config' => array(
|
||||||
|
// Read directly from the legacy individual options — NOT via
|
||||||
|
// noindex_seo_get_config(), which would read the consolidated option
|
||||||
|
// we are in the middle of building.
|
||||||
|
'method' => get_option( 'noindex_seo_config_method', 'meta' ),
|
||||||
|
'granular' => get_option( 'noindex_seo_config_granular', 0 ),
|
||||||
|
'seoplugins' => get_option( 'noindex_seo_config_seoplugins', 0 ),
|
||||||
|
'delete_on_uninstall' => get_option( 'noindex_seo_config_delete_on_uninstall', 0 ),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Normalise config value types.
|
||||||
|
$consolidated['config']['granular'] = $consolidated['config']['granular'] ? 1 : 0;
|
||||||
|
$consolidated['config']['seoplugins'] = $consolidated['config']['seoplugins'] ? 1 : 0;
|
||||||
|
$consolidated['config']['delete_on_uninstall'] = $consolidated['config']['delete_on_uninstall'] ? 1 : 0;
|
||||||
|
if ( ! in_array( $consolidated['config']['method'], array( 'meta', 'header', 'both' ), true ) ) {
|
||||||
|
$consolidated['config']['method'] = 'meta';
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( $contexts as $context ) {
|
||||||
|
$consolidated['contexts'][ $context ] = array();
|
||||||
|
foreach ( $directives as $directive ) {
|
||||||
|
$option_key = $directive . '_seo_' . $context;
|
||||||
|
$value = get_option( $option_key, 0 );
|
||||||
|
$consolidated['contexts'][ $context ][ $directive ] = ( is_numeric( $value ) && 1 === (int) $value ) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the consolidated option (autoloaded by default — single wp_options row).
|
||||||
|
update_option( NOINDEX_SEO_SETTINGS_KEY, $consolidated );
|
||||||
|
|
||||||
|
// Mark each legacy option as non-autoloaded. update_option() short-circuits
|
||||||
|
// when the value is unchanged (which is the case here — we are only flipping
|
||||||
|
// the autoload flag), so we use a direct SQL UPDATE on the options table.
|
||||||
|
// The options themselves remain readable for a safe rollback to 2.x; they
|
||||||
|
// will be deleted entirely in 4.0.
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
// Migration is a one-shot operation. Direct update is necessary because
|
||||||
|
// update_option() does not change autoload when the value is unchanged.
|
||||||
|
// This is a UPDATE (no result set), so caching is not applicable.
|
||||||
|
foreach ( $contexts as $context ) {
|
||||||
|
foreach ( $directives as $directive ) {
|
||||||
|
$option_key = $directive . '_seo_' . $context;
|
||||||
|
$wpdb->query(
|
||||||
|
$wpdb->prepare(
|
||||||
|
"UPDATE {$wpdb->options} SET autoload = 'no' WHERE option_name = %s",
|
||||||
|
$option_key
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ( array( 'method', 'granular', 'seoplugins', 'delete_on_uninstall' ) as $config_key ) {
|
||||||
|
$option_key = 'noindex_seo_config_' . $config_key;
|
||||||
|
$wpdb->query(
|
||||||
|
$wpdb->prepare(
|
||||||
|
"UPDATE {$wpdb->options} SET autoload = 'no' WHERE option_name = %s",
|
||||||
|
$option_key
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
|
||||||
|
// Mark migration complete and clear cache.
|
||||||
|
update_option( 'noindex_seo_config_version', 3 );
|
||||||
|
delete_transient( 'noindex_seo_options' );
|
||||||
|
|
||||||
|
// Reset the in-memory settings cache so the rest of this request reads
|
||||||
|
// from the new option instead of the just-stale state.
|
||||||
|
noindex_seo_get_settings( true );
|
||||||
|
}
|
||||||
|
|
||||||
add_action( 'template_redirect', 'noindex_seo_show' );
|
add_action( 'template_redirect', 'noindex_seo_show' );
|
||||||
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'noindex_seo_settings_link' );
|
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'noindex_seo_settings_link' );
|
||||||
add_action( 'admin_init', 'noindex_seo_register' );
|
add_action( 'admin_init', 'noindex_seo_register' );
|
||||||
|
|
@ -375,6 +668,34 @@ add_action( 'admin_menu', 'noindex_seo_menu' );
|
||||||
add_action( 'admin_enqueue_scripts', 'noindex_seo_enqueue_admin_assets' );
|
add_action( 'admin_enqueue_scripts', 'noindex_seo_enqueue_admin_assets' );
|
||||||
add_action( 'plugins_loaded', 'noindex_seo_check_migration' );
|
add_action( 'plugins_loaded', 'noindex_seo_check_migration' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidates the hourly transient cache whenever the consolidated settings
|
||||||
|
* option is written — no matter who wrote it (admin form, WP-CLI, REST, or
|
||||||
|
* a third-party plugin calling update_option / add_option directly).
|
||||||
|
*
|
||||||
|
* The transient caches the rendered directive map for `noindex_seo_show()`.
|
||||||
|
* Without these hooks, programmatic edits would stay stale for up to one hour.
|
||||||
|
*
|
||||||
|
* Two hooks are needed because WordPress dispatches writes differently:
|
||||||
|
* - `update_option_{key}` fires only when an existing option is updated.
|
||||||
|
* - `add_option_{key}` fires when a new option row is created. WordPress
|
||||||
|
* routes `update_option()` through `add_option()` internally when the
|
||||||
|
* option does not yet exist, so without this second hook the first write
|
||||||
|
* after install would not clear the transient.
|
||||||
|
*
|
||||||
|
* Hooked to both the {@see 'update_option_noindex_seo_settings'} and
|
||||||
|
* {@see 'add_option_noindex_seo_settings'} actions.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function noindex_seo_invalidate_cache_on_settings_update(): void {
|
||||||
|
delete_transient( 'noindex_seo_options' );
|
||||||
|
}
|
||||||
|
add_action( 'update_option_' . NOINDEX_SEO_SETTINGS_KEY, 'noindex_seo_invalidate_cache_on_settings_update' );
|
||||||
|
add_action( 'add_option_' . NOINDEX_SEO_SETTINGS_KEY, 'noindex_seo_invalidate_cache_on_settings_update' );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enqueues admin CSS and JavaScript assets.
|
* Enqueues admin CSS and JavaScript assets.
|
||||||
*
|
*
|
||||||
|
|
@ -434,7 +755,7 @@ function noindex_seo_enqueue_admin_assets( string $hook ): void {
|
||||||
*/
|
*/
|
||||||
function noindex_seo_enqueue_editor_assets(): void {
|
function noindex_seo_enqueue_editor_assets(): void {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -685,7 +1006,7 @@ function noindex_seo_clear_transient(): void {
|
||||||
*/
|
*/
|
||||||
function noindex_seo_detect_conflicts(): void {
|
function noindex_seo_detect_conflicts(): void {
|
||||||
|
|
||||||
$option_config_seoplugins = get_option( 'noindex_seo_config_seoplugins', 0 );
|
$option_config_seoplugins = noindex_seo_get_config( 'seoplugins', 0 );
|
||||||
|
|
||||||
if ( ! ( is_scalar( $option_config_seoplugins ) ? absint( $option_config_seoplugins ) : 0 ) ) {
|
if ( ! ( is_scalar( $option_config_seoplugins ) ? absint( $option_config_seoplugins ) : 0 ) ) {
|
||||||
|
|
||||||
|
|
@ -694,8 +1015,12 @@ function noindex_seo_detect_conflicts(): void {
|
||||||
include_once ABSPATH . 'wp-admin/includes/plugin.php';
|
include_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define an associative array of conflicting plugins: slug/file => real plugin name..
|
// Define an associative array of conflicting plugins: slug/file => real plugin name.
|
||||||
$conflicting_plugins = array(
|
// Filterable since 3.0.0 so third-party code can extend or replace the list
|
||||||
|
// (e.g. to add The SEO Framework Pro, Slim SEO Pack vs Slim SEO, etc.).
|
||||||
|
$conflicting_plugins = apply_filters(
|
||||||
|
'noindex_seo_conflicting_plugins',
|
||||||
|
array(
|
||||||
'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO',
|
'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO',
|
||||||
'premium-seo-pack/index.php' => 'Premium SEO Pack',
|
'premium-seo-pack/index.php' => 'Premium SEO Pack',
|
||||||
'seo-by-rank-math/rank-math.php' => 'Rank Math SEO',
|
'seo-by-rank-math/rank-math.php' => 'Rank Math SEO',
|
||||||
|
|
@ -704,9 +1029,10 @@ function noindex_seo_detect_conflicts(): void {
|
||||||
'squirrly-seo/squirrly.php' => 'Squirrly SEO',
|
'squirrly-seo/squirrly.php' => 'Squirrly SEO',
|
||||||
'autodescription/autodescription.php' => 'The SEO Framework',
|
'autodescription/autodescription.php' => 'The SEO Framework',
|
||||||
'wordpress-seo/wp-seo.php' => 'Yoast SEO',
|
'wordpress-seo/wp-seo.php' => 'Yoast SEO',
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Iterate through the conflicting plugins to check if any are active..
|
// Iterate through the conflicting plugins to check if any are active.
|
||||||
foreach ( $conflicting_plugins as $plugin_path => $plugin_name ) {
|
foreach ( $conflicting_plugins as $plugin_path => $plugin_name ) {
|
||||||
if ( is_plugin_active( $plugin_path ) ) {
|
if ( is_plugin_active( $plugin_path ) ) {
|
||||||
// Add an admin notice if a conflicting plugin is active..
|
// Add an admin notice if a conflicting plugin is active..
|
||||||
|
|
@ -749,7 +1075,6 @@ function noindex_seo_process_form(): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
$contexts = noindex_seo_get_contexts();
|
$contexts = noindex_seo_get_contexts();
|
||||||
|
|
||||||
$directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
$directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
||||||
|
|
||||||
// Get the implementation method to validate field compatibility.
|
// Get the implementation method to validate field compatibility.
|
||||||
|
|
@ -764,78 +1089,141 @@ function noindex_seo_process_form(): void {
|
||||||
$header_only_contexts = array( 'attachment' );
|
$header_only_contexts = array( 'attachment' );
|
||||||
$is_header_enabled = in_array( $method_value, array( 'header', 'both' ), true );
|
$is_header_enabled = in_array( $method_value, array( 'header', 'both' ), true );
|
||||||
|
|
||||||
// Reset all options to 0 and process form data.
|
// Start from the current consolidated settings so we only overwrite what
|
||||||
|
// the form actually touches. The shape is preserved by noindex_seo_update_settings().
|
||||||
|
$settings = noindex_seo_get_settings();
|
||||||
|
|
||||||
|
// Process checkbox grid: one entry per (context, directive).
|
||||||
foreach ( $contexts as $context ) {
|
foreach ( $contexts as $context ) {
|
||||||
// Validate context is in our allowed list (defense in depth).
|
|
||||||
if ( ! in_array( $context, $contexts, true ) ) {
|
|
||||||
continue; // Skip invalid context.
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ( $directives as $directive ) {
|
foreach ( $directives as $directive ) {
|
||||||
// Validate directive is in our allowed list (defense in depth).
|
|
||||||
if ( ! in_array( $directive, $directives, true ) ) {
|
|
||||||
continue; // Skip invalid directive.
|
|
||||||
}
|
|
||||||
|
|
||||||
$option_key = $directive . '_seo_' . $context;
|
$option_key = $directive . '_seo_' . $context;
|
||||||
$option_value = ( isset( $_POST[ $option_key ] ) && is_string( $_POST[ $option_key ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $option_key ] ) ) : '';
|
|
||||||
|
|
||||||
// Additional validation: only accept '1' or empty string.
|
// Header-only contexts are forced to 0 when headers aren't enabled.
|
||||||
if ( '' !== $option_value && '1' !== $option_value ) {
|
|
||||||
$option_value = ''; // Invalid value, treat as unchecked.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate: header-only contexts require header implementation method.
|
|
||||||
if ( in_array( $context, $header_only_contexts, true ) && ! $is_header_enabled ) {
|
if ( in_array( $context, $header_only_contexts, true ) && ! $is_header_enabled ) {
|
||||||
// Force to 0 - this context doesn't work with current method.
|
$settings['contexts'][ $context ][ $directive ] = 0;
|
||||||
update_option( $option_key, 0 );
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only set to 1 if the checkbox was actually checked (value should be "1").
|
$option_value = ( isset( $_POST[ $option_key ] ) && is_string( $_POST[ $option_key ] ) )
|
||||||
if ( '1' === $option_value ) {
|
? sanitize_text_field( wp_unslash( $_POST[ $option_key ] ) )
|
||||||
update_option( $option_key, 1 );
|
: '';
|
||||||
} else {
|
|
||||||
update_option( $option_key, 0 );
|
// Only '1' counts as checked; everything else (including invalid input) is 0.
|
||||||
}
|
$settings['contexts'][ $context ][ $directive ] = ( '1' === $option_value ) ? 1 : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save general configuration option..
|
// Save general configuration values.
|
||||||
$config_value = ( isset( $_POST['noindex_seo_config_seoplugins'] ) && is_string( $_POST['noindex_seo_config_seoplugins'] ) )
|
$config_value = ( isset( $_POST['noindex_seo_config_seoplugins'] ) && is_string( $_POST['noindex_seo_config_seoplugins'] ) )
|
||||||
? absint( $_POST['noindex_seo_config_seoplugins'] )
|
? absint( $_POST['noindex_seo_config_seoplugins'] )
|
||||||
: 0;
|
: 0;
|
||||||
|
$settings['config']['seoplugins'] = ( 1 === $config_value ) ? 1 : 0;
|
||||||
|
|
||||||
// Ensure value is either 0 or 1..
|
// Implementation method (validated above).
|
||||||
$config_value = ( 1 === $config_value ) ? 1 : 0;
|
$settings['config']['method'] = $method_value;
|
||||||
|
|
||||||
update_option( 'noindex_seo_config_seoplugins', $config_value );
|
// Granular control.
|
||||||
|
|
||||||
// Save implementation method configuration (already validated above).
|
|
||||||
update_option( 'noindex_seo_config_method', $method_value );
|
|
||||||
|
|
||||||
// Save granular control configuration.
|
|
||||||
$granular_value = ( isset( $_POST['noindex_seo_config_granular'] ) && is_string( $_POST['noindex_seo_config_granular'] ) )
|
$granular_value = ( isset( $_POST['noindex_seo_config_granular'] ) && is_string( $_POST['noindex_seo_config_granular'] ) )
|
||||||
? absint( $_POST['noindex_seo_config_granular'] )
|
? absint( $_POST['noindex_seo_config_granular'] )
|
||||||
: 0;
|
: 0;
|
||||||
$granular_value = ( 1 === $granular_value ) ? 1 : 0;
|
$settings['config']['granular'] = ( 1 === $granular_value ) ? 1 : 0;
|
||||||
update_option( 'noindex_seo_config_granular', $granular_value );
|
|
||||||
|
|
||||||
// Save delete-on-uninstall configuration.
|
// Delete-on-uninstall opt-in.
|
||||||
$delete_value = ( isset( $_POST['noindex_seo_config_delete_on_uninstall'] ) && is_string( $_POST['noindex_seo_config_delete_on_uninstall'] ) )
|
$delete_value = ( isset( $_POST['noindex_seo_config_delete_on_uninstall'] ) && is_string( $_POST['noindex_seo_config_delete_on_uninstall'] ) )
|
||||||
? absint( $_POST['noindex_seo_config_delete_on_uninstall'] )
|
? absint( $_POST['noindex_seo_config_delete_on_uninstall'] )
|
||||||
: 0;
|
: 0;
|
||||||
$delete_value = ( 1 === $delete_value ) ? 1 : 0;
|
$settings['config']['delete_on_uninstall'] = ( 1 === $delete_value ) ? 1 : 0;
|
||||||
update_option( 'noindex_seo_config_delete_on_uninstall', $delete_value );
|
|
||||||
|
|
||||||
// Clear cache..
|
// Persist. This fires update_option_noindex_seo_settings, which clears the
|
||||||
delete_transient( 'noindex_seo_options' );
|
// transient via noindex_seo_invalidate_cache_on_settings_update() — so we
|
||||||
|
// no longer need to delete_transient() by hand here.
|
||||||
|
noindex_seo_update_settings( $settings );
|
||||||
|
|
||||||
wp_safe_redirect( admin_url( 'options-general.php?page=noindex_seo&updated=true' ) );
|
wp_safe_redirect( admin_url( 'options-general.php?page=noindex_seo&updated=true' ) );
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
add_action( 'admin_post_update_noindex_seo', 'noindex_seo_process_form' );
|
add_action( 'admin_post_update_noindex_seo', 'noindex_seo_process_form' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the "Apply recommended defaults" form action.
|
||||||
|
*
|
||||||
|
* Iterates the settings page section definitions and enables every directive
|
||||||
|
* for every context flagged `'suggestion' => true`. The 12 contexts currently
|
||||||
|
* flagged are: privacy_policy, date, day, month, time, year, paged, search,
|
||||||
|
* attachment, customize_preview, preview, error.
|
||||||
|
*
|
||||||
|
* All five directives are enabled on each suggested context. This is the same
|
||||||
|
* shape a manual click-through would produce, so the form is fully editable
|
||||||
|
* afterwards.
|
||||||
|
*
|
||||||
|
* Hooked to the {@see 'admin_post_noindex_seo_apply_suggestions'} action.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function noindex_seo_process_apply_suggestions(): void {
|
||||||
|
if ( ! current_user_can( 'manage_options' ) || ! check_admin_referer( 'noindex_seo_apply_suggestions_nonce' ) ) {
|
||||||
|
wp_die( esc_html__( 'Permission denied or invalid nonce.', 'noindex-seo' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
||||||
|
$settings = noindex_seo_get_settings();
|
||||||
|
|
||||||
|
foreach ( noindex_seo_get_suggested_contexts() as $context ) {
|
||||||
|
foreach ( $directives as $directive ) {
|
||||||
|
$settings['contexts'][ $context ][ $directive ] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
noindex_seo_update_settings( $settings );
|
||||||
|
|
||||||
|
wp_safe_redirect( admin_url( 'options-general.php?page=noindex_seo&suggestions_applied=1' ) );
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
add_action( 'admin_post_noindex_seo_apply_suggestions', 'noindex_seo_process_apply_suggestions' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the list of contexts flagged as suggested defaults on the settings page.
|
||||||
|
*
|
||||||
|
* Centralised so the "Apply recommended defaults" handler and any future
|
||||||
|
* WP-CLI command share the same definition of "recommended".
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @return string[] List of context slugs.
|
||||||
|
*/
|
||||||
|
function noindex_seo_get_suggested_contexts(): array {
|
||||||
|
/**
|
||||||
|
* Filter the contexts that the "Apply recommended defaults" button enables.
|
||||||
|
*
|
||||||
|
* Each returned slug MUST be one of the contexts declared by
|
||||||
|
* noindex_seo_get_contexts(). Unknown slugs are silently ignored at the
|
||||||
|
* call site.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param string[] $suggested Context slugs flagged as recommended defaults.
|
||||||
|
*/
|
||||||
|
return apply_filters(
|
||||||
|
'noindex_seo_suggested_contexts',
|
||||||
|
array(
|
||||||
|
'privacy_policy',
|
||||||
|
'date',
|
||||||
|
'day',
|
||||||
|
'month',
|
||||||
|
'time',
|
||||||
|
'year',
|
||||||
|
'paged',
|
||||||
|
'search',
|
||||||
|
'attachment',
|
||||||
|
'customize_preview',
|
||||||
|
'preview',
|
||||||
|
'error',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register post meta for REST API and Gutenberg support.
|
* Register post meta for REST API and Gutenberg support.
|
||||||
*
|
*
|
||||||
|
|
@ -848,7 +1236,7 @@ add_action( 'admin_post_update_noindex_seo', 'noindex_seo_process_form' );
|
||||||
*/
|
*/
|
||||||
function noindex_seo_register_post_meta(): void {
|
function noindex_seo_register_post_meta(): void {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -899,7 +1287,7 @@ add_action( 'init', 'noindex_seo_register_post_meta' );
|
||||||
*/
|
*/
|
||||||
function noindex_seo_add_meta_boxes(): void {
|
function noindex_seo_add_meta_boxes(): void {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1110,7 +1498,7 @@ function noindex_seo_save_directives_from_post( int $post_id ): void {
|
||||||
*/
|
*/
|
||||||
function noindex_seo_save_post_meta( int $post_id ): void {
|
function noindex_seo_save_post_meta( int $post_id ): void {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1152,7 +1540,7 @@ add_action( 'save_post', 'noindex_seo_save_post_meta' );
|
||||||
*/
|
*/
|
||||||
function noindex_seo_add_custom_column( array $columns ): array {
|
function noindex_seo_add_custom_column( array $columns ): array {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
@ -1286,7 +1674,7 @@ add_action(
|
||||||
*/
|
*/
|
||||||
function noindex_seo_quick_edit_fields( string $column_name, string $post_type ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
function noindex_seo_quick_edit_fields( string $column_name, string $post_type ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1390,7 +1778,7 @@ add_action( 'bulk_edit_custom_box', 'noindex_seo_quick_edit_fields', 10, 2 );
|
||||||
*/
|
*/
|
||||||
function noindex_seo_save_quick_edit( int $post_id ): void {
|
function noindex_seo_save_quick_edit( int $post_id ): void {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1432,7 +1820,7 @@ add_action( 'save_post', 'noindex_seo_save_quick_edit' );
|
||||||
*/
|
*/
|
||||||
function noindex_seo_register_bulk_actions( array $bulk_actions ): array {
|
function noindex_seo_register_bulk_actions( array $bulk_actions ): array {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return $bulk_actions;
|
return $bulk_actions;
|
||||||
}
|
}
|
||||||
|
|
@ -1455,7 +1843,7 @@ function noindex_seo_register_bulk_actions( array $bulk_actions ): array {
|
||||||
*/
|
*/
|
||||||
function noindex_seo_handle_bulk_actions( string $redirect_to, string $action, array $post_ids ): string {
|
function noindex_seo_handle_bulk_actions( string $redirect_to, string $action, array $post_ids ): string {
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return $redirect_to;
|
return $redirect_to;
|
||||||
}
|
}
|
||||||
|
|
@ -1594,7 +1982,7 @@ add_action(
|
||||||
*/
|
*/
|
||||||
function noindex_seo_add_list_filter( string $post_type ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
function noindex_seo_add_list_filter( string $post_type ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1634,7 +2022,7 @@ function noindex_seo_filter_posts_by_override( WP_Query $query ): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if granular control is enabled.
|
// Check if granular control is enabled.
|
||||||
$granular_enabled = get_option( 'noindex_seo_config_granular', 0 );
|
$granular_enabled = noindex_seo_get_config( 'granular', 0 );
|
||||||
if ( ! $granular_enabled ) {
|
if ( ! $granular_enabled ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -1934,13 +2322,13 @@ function noindex_seo_admin(): void {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get config options with explicit type narrowing.
|
// Get config options with explicit type narrowing.
|
||||||
$opt_seoplugins = get_option( 'noindex_seo_config_seoplugins', 0 );
|
$opt_seoplugins = noindex_seo_get_config( 'seoplugins', 0 );
|
||||||
$option_config_seoplugins = is_scalar( $opt_seoplugins ) ? absint( $opt_seoplugins ) : 0;
|
$option_config_seoplugins = is_scalar( $opt_seoplugins ) ? absint( $opt_seoplugins ) : 0;
|
||||||
$opt_method = get_option( 'noindex_seo_config_method', 'meta' );
|
$opt_method = noindex_seo_get_config( 'method', 'meta' );
|
||||||
$option_config_method = is_string( $opt_method ) ? $opt_method : 'meta';
|
$option_config_method = is_string( $opt_method ) ? $opt_method : 'meta';
|
||||||
$opt_granular = get_option( 'noindex_seo_config_granular', 0 );
|
$opt_granular = noindex_seo_get_config( 'granular', 0 );
|
||||||
$option_config_granular = is_scalar( $opt_granular ) ? absint( $opt_granular ) : 0;
|
$option_config_granular = is_scalar( $opt_granular ) ? absint( $opt_granular ) : 0;
|
||||||
$opt_delete = get_option( 'noindex_seo_config_delete_on_uninstall', 0 );
|
$opt_delete = noindex_seo_get_config( 'delete_on_uninstall', 0 );
|
||||||
$option_config_delete_on_uninstall = is_scalar( $opt_delete ) ? absint( $opt_delete ) : 0;
|
$option_config_delete_on_uninstall = is_scalar( $opt_delete ) ? absint( $opt_delete ) : 0;
|
||||||
|
|
||||||
// Define fields that only work with HTTP headers (non-HTML content).
|
// Define fields that only work with HTTP headers (non-HTML content).
|
||||||
|
|
@ -1981,6 +2369,45 @@ function noindex_seo_admin(): void {
|
||||||
<h1><?php esc_html_e( 'noindex SEO Settings', 'noindex-seo' ); ?></h1>
|
<h1><?php esc_html_e( 'noindex SEO Settings', 'noindex-seo' ); ?></h1>
|
||||||
<p><?php esc_html_e( 'Control how search engines index and display your WordPress content using robots directives.', 'noindex-seo' ); ?></p>
|
<p><?php esc_html_e( 'Control how search engines index and display your WordPress content using robots directives.', 'noindex-seo' ); ?></p>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
// Success notices for one-click actions. The form handlers redirect back
|
||||||
|
// to this page with a query flag so the notice survives the redirect.
|
||||||
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- URL parameter set by our own admin-post handler after a nonce check, not user form data.
|
||||||
|
if ( isset( $_GET['suggestions_applied'] ) && is_string( $_GET['suggestions_applied'] ) ) {
|
||||||
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only flag set by our own admin-post handler after a nonce check.
|
||||||
|
$suggestions_applied = sanitize_key( wp_unslash( $_GET['suggestions_applied'] ) );
|
||||||
|
if ( '1' === $suggestions_applied ) {
|
||||||
|
echo '<div class="notice notice-success is-dismissible"><p>';
|
||||||
|
esc_html_e( 'Recommended defaults applied. Review the changes below and click "Save Changes" to keep them, or uncheck anything you do not want.', 'noindex-seo' );
|
||||||
|
echo '</p></div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
// "Apply recommended defaults" — separate form so it does not collide
|
||||||
|
// with the main settings form's nonce/action. Goes through admin-post.php
|
||||||
|
// like the main save.
|
||||||
|
?>
|
||||||
|
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin: 0 0 20px 0;">
|
||||||
|
<input type="hidden" name="action" value="noindex_seo_apply_suggestions">
|
||||||
|
<?php wp_nonce_field( 'noindex_seo_apply_suggestions_nonce' ); ?>
|
||||||
|
<?php
|
||||||
|
submit_button(
|
||||||
|
__( 'Apply Recommended Defaults', 'noindex-seo' ),
|
||||||
|
'secondary',
|
||||||
|
'noindex-seo-apply-suggestions',
|
||||||
|
false,
|
||||||
|
array(
|
||||||
|
'title' => __( 'Enable all directives for the 12 contexts flagged as recommended.', 'noindex-seo' ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
<span class="description" style="margin-left: 8px; vertical-align: middle;">
|
||||||
|
<?php esc_html_e( 'Enables all 5 directives on the 12 contexts flagged "Recommended" (privacy policy, date archives, pagination, search results, attachment pages, previews, 404). Review and save.', 'noindex-seo' ); ?>
|
||||||
|
</span>
|
||||||
|
</form>
|
||||||
|
|
||||||
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
||||||
<input type="hidden" name="action" value="update_noindex_seo">
|
<input type="hidden" name="action" value="update_noindex_seo">
|
||||||
<?php wp_nonce_field( 'update_noindex_seo_nonce' ); ?>
|
<?php wp_nonce_field( 'update_noindex_seo_nonce' ); ?>
|
||||||
|
|
@ -2153,7 +2580,7 @@ function noindex_seo_admin(): void {
|
||||||
<?php foreach ( $directives_config as $directive => $config ) : ?>
|
<?php foreach ( $directives_config as $directive => $config ) : ?>
|
||||||
<?php
|
<?php
|
||||||
$directive_option_key = $directive . '_seo_' . $field_id;
|
$directive_option_key = $directive . '_seo_' . $field_id;
|
||||||
$directive_value = get_option( $directive_option_key, 0 );
|
$directive_value = noindex_seo_get_setting( $field_id, $directive );
|
||||||
?>
|
?>
|
||||||
<label class="noindex-seo-directive-checkbox">
|
<label class="noindex-seo-directive-checkbox">
|
||||||
<input
|
<input
|
||||||
|
|
|
||||||
44
readme.txt
44
readme.txt
|
|
@ -3,9 +3,9 @@ Contributors: javiercasares
|
||||||
Tags: seo, noindex, nofollow, noarchive, robots
|
Tags: seo, noindex, nofollow, noarchive, robots
|
||||||
Requires at least: 5.7
|
Requires at least: 5.7
|
||||||
Tested up to: 7.1
|
Tested up to: 7.1
|
||||||
Stable tag: 2.0.2
|
Stable tag: 3.0.1
|
||||||
Requires PHP: 7.2
|
Requires PHP: 7.2
|
||||||
Version: 2.0.2
|
Version: 3.0.1
|
||||||
License: GPL-3.0-or-later
|
License: GPL-3.0-or-later
|
||||||
License URI: https://www.gnu.org/licenses/gpl-3.0.txt
|
License URI: https://www.gnu.org/licenses/gpl-3.0.txt
|
||||||
|
|
||||||
|
|
@ -114,6 +114,46 @@ Extract the contents of the ZIP and upload the contents to the `/wp-content/plug
|
||||||
|
|
||||||
== Changelog ==
|
== Changelog ==
|
||||||
|
|
||||||
|
= 3.0.1 [2026-07-18] =
|
||||||
|
|
||||||
|
**Fixed**
|
||||||
|
|
||||||
|
* Self-healing v3 migration. If `noindex_seo_config_version` claims migration completed (version >= 3) but the consolidated `noindex_seo_settings` option is somehow missing, `noindex_seo_check_migration()` now re-runs the migration automatically. This recovers silently from partial failures: object-cache staleness after the write, request interrupted mid-migration, a third-party plugin clearing options, or DB write errors that did not raise a PHP error. Without this guard, the front-end would fall back to all-zero defaults (site fully indexable) until someone re-saved the settings page.
|
||||||
|
|
||||||
|
**Compatibility**
|
||||||
|
|
||||||
|
* WordPress: 5.7 - 7.1
|
||||||
|
* PHP: 7.2 - 8.5
|
||||||
|
|
||||||
|
**Tests**
|
||||||
|
|
||||||
|
* Added test for the self-healing migration path.
|
||||||
|
|
||||||
|
= 3.0.0 [2026-07-18] =
|
||||||
|
|
||||||
|
**Major release — storage consolidation.** Settings are now stored in a single autoloaded option (`noindex_seo_settings`) instead of ~110 individual options. Reduces `wp_options` autoload weight on every page load. Legacy options are preserved (marked non-autoloaded) for one major version to allow safe rollback to 2.x; they will be removed in 4.0.
|
||||||
|
|
||||||
|
**Added**
|
||||||
|
|
||||||
|
* **"Apply recommended defaults" button** on the settings page. One click enables all 12 contexts flagged as "Recommended" in the UI (privacy policy, date archives, pagination, search results, attachment pages, preview/customize-preview, and 404). Previously required ~60 individual checkbox toggles.
|
||||||
|
* **Filterable conflict-detection list.** The list of plugins that may conflict with noindex SEO is now passed through `apply_filters('noindex_seo_conflicting_plugins', ...)`. Third-party code can extend or replace the list without editing the plugin.
|
||||||
|
|
||||||
|
**Changed**
|
||||||
|
|
||||||
|
* **Storage consolidated**: 110 directive options (`{directive}_seo_{context}`) + 5 config options (`noindex_seo_config_*`) merged into one `noindex_seo_settings` array. Reads go through new accessors (`noindex_seo_get_settings()`, `noindex_seo_get_setting()`, `noindex_seo_get_config()`). The hourly transient cache is preserved on top.
|
||||||
|
* **Transient invalidation is now automatic**: any update to `noindex_seo_settings` clears the `noindex_seo_options` transient via the `update_option_noindex_seo_settings` hook. Previously only the admin form save cleared it, leaving WP-CLI / REST / programmatic edits stale for up to one hour.
|
||||||
|
* **Migration**: existing 2.x installs are migrated automatically on `plugins_loaded` (idempotent, version-gated by `noindex_seo_config_version`). Legacy options are kept and marked non-autoloaded so a rollback to 2.x restores the pre-3.0 state.
|
||||||
|
* **Uninstall retrocompatibility**: `uninstall.php` deletes both the new consolidated option and all legacy individual options (via wildcard SQL). Sites that upgrade from 2.x and later uninstall will be cleaned up completely regardless of whether migration ran.
|
||||||
|
|
||||||
|
**Compatibility**
|
||||||
|
|
||||||
|
* WordPress: 5.7 - 7.1
|
||||||
|
* PHP: 7.2 - 8.5
|
||||||
|
|
||||||
|
**Tests**
|
||||||
|
|
||||||
|
* Added PHPUnit test suite (previously the project shipped only a plugin-header smoke test). Coverage: contexts helper, settings accessors, v2→v3 migration, form save, bulk actions, meta box save, conflict-detection filter.
|
||||||
|
|
||||||
= 2.0.2 [2026-07-18] =
|
= 2.0.2 [2026-07-18] =
|
||||||
|
|
||||||
**Fixed**
|
**Fixed**
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@
|
||||||
* @since 2.0.0 Added cleanup for new implementation method option and transients.
|
* @since 2.0.0 Added cleanup for new implementation method option and transients.
|
||||||
* @since 2.0.0 Added cleanup for multiple directives (noindex, nofollow, noarchive, nosnippet, noimageindex).
|
* @since 2.0.0 Added cleanup for multiple directives (noindex, nofollow, noarchive, nosnippet, noimageindex).
|
||||||
* @since 2.0.1 Data is now preserved by default; deletion requires explicit opt-in.
|
* @since 2.0.1 Data is now preserved by default; deletion requires explicit opt-in.
|
||||||
|
* @since 3.0.0 Added cleanup for the consolidated noindex_seo_settings option. Legacy
|
||||||
|
* individual options are still cleaned up so that sites which upgrade from
|
||||||
|
* 2.x and then uninstall (with or without migration having run) are fully
|
||||||
|
* cleaned. Retrocompat: if a user rolls back to 2.x, their data is intact
|
||||||
|
* until they explicitly uninstall.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
@ -22,7 +27,19 @@ if ( ! defined( 'ABSPATH' ) || ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
||||||
|
|
||||||
// Respect the user's data-preservation preference.
|
// Respect the user's data-preservation preference.
|
||||||
// Default (0) = preserve data. Only delete when the user has explicitly opted in (1).
|
// Default (0) = preserve data. Only delete when the user has explicitly opted in (1).
|
||||||
|
// Read from both possible storage locations: the consolidated 3.0 option first, then
|
||||||
|
// the legacy 2.x option as a fallback (covers the case where migration never ran).
|
||||||
|
$noindex_seo_delete = 0;
|
||||||
|
$noindex_seo_consolidated = get_option( 'noindex_seo_settings', array() );
|
||||||
|
$noindex_seo_consolidated = is_array( $noindex_seo_consolidated ) ? $noindex_seo_consolidated : array();
|
||||||
|
$noindex_seo_consolidated_cfg = $noindex_seo_consolidated['config'] ?? array();
|
||||||
|
$noindex_seo_consolidated_cfg = is_array( $noindex_seo_consolidated_cfg ) ? $noindex_seo_consolidated_cfg : array();
|
||||||
|
if ( isset( $noindex_seo_consolidated_cfg['delete_on_uninstall'] ) ) {
|
||||||
|
$noindex_seo_delete = $noindex_seo_consolidated_cfg['delete_on_uninstall'] ? 1 : 0;
|
||||||
|
} else {
|
||||||
$noindex_seo_delete = get_option( 'noindex_seo_config_delete_on_uninstall', 0 );
|
$noindex_seo_delete = get_option( 'noindex_seo_config_delete_on_uninstall', 0 );
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! $noindex_seo_delete ) {
|
if ( ! $noindex_seo_delete ) {
|
||||||
// Data is preserved. Nothing to do.
|
// Data is preserved. Nothing to do.
|
||||||
exit;
|
exit;
|
||||||
|
|
@ -60,14 +77,20 @@ $noindex_seo_contexts = array(
|
||||||
);
|
);
|
||||||
$noindex_seo_directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
$noindex_seo_directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' );
|
||||||
|
|
||||||
// Delete all directive options for each context.
|
// 3.0+ consolidated option (single row, autoloaded).
|
||||||
|
delete_option( 'noindex_seo_settings' );
|
||||||
|
|
||||||
|
// Delete all legacy directive options for each context (2.x storage).
|
||||||
|
// Kept for retrocompat: covers installs that upgrade from 2.x and then uninstall
|
||||||
|
// even if the 3.0 migration did not run (e.g. plugin was never activated, only
|
||||||
|
// installed). Each delete_option() is a no-op if the row does not exist.
|
||||||
foreach ( $noindex_seo_contexts as $noindex_seo_context ) {
|
foreach ( $noindex_seo_contexts as $noindex_seo_context ) {
|
||||||
foreach ( $noindex_seo_directives as $noindex_seo_directive ) {
|
foreach ( $noindex_seo_directives as $noindex_seo_directive ) {
|
||||||
delete_option( $noindex_seo_directive . '_seo_' . $noindex_seo_context );
|
delete_option( $noindex_seo_directive . '_seo_' . $noindex_seo_context );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete configuration options.
|
// Delete legacy configuration options.
|
||||||
delete_option( 'noindex_seo_config_seoplugins' );
|
delete_option( 'noindex_seo_config_seoplugins' );
|
||||||
delete_option( 'noindex_seo_config_method' );
|
delete_option( 'noindex_seo_config_method' );
|
||||||
delete_option( 'noindex_seo_config_granular' );
|
delete_option( 'noindex_seo_config_granular' );
|
||||||
|
|
@ -77,7 +100,7 @@ delete_option( 'noindex_seo_config_delete_on_uninstall' );
|
||||||
// Delete transient cache.
|
// Delete transient cache.
|
||||||
delete_transient( 'noindex_seo_options' );
|
delete_transient( 'noindex_seo_options' );
|
||||||
|
|
||||||
// Clean up any leftover options (in case of partial uninstall).
|
// Clean up any leftover options (in case of partial uninstall or pre-3.0 installs).
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||||
|
|
@ -89,6 +112,16 @@ foreach ( $noindex_seo_directives as $noindex_seo_directive ) {
|
||||||
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $noindex_seo_directive . '_seo_%' ) );
|
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $noindex_seo_directive . '_seo_%' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Belt-and-suspenders: clean up the consolidated option one more time in case
|
||||||
|
// delete_option() above failed due to a corrupted cache. The wildcard LIKE
|
||||||
|
// matches both noindex_seo_settings and any stray noindex_seo_config_* rows.
|
||||||
|
$wpdb->query(
|
||||||
|
$wpdb->prepare(
|
||||||
|
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
|
||||||
|
'noindex_seo_%'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
// Clean up transients.
|
// Clean up transients.
|
||||||
$wpdb->query(
|
$wpdb->query(
|
||||||
$wpdb->prepare(
|
$wpdb->prepare(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue