From fb0ef03b7336461e2bc6c68ed69182aa56c868f0 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Tue, 3 Nov 2026 17:05:48 +0000 Subject: [PATCH] v3.4.0 --- changelog.txt | 35 +++++ noindex-seo.php | 406 +++++++++++++++++++++++++++++++++++++++++++++++- readme.txt | 27 +++- 3 files changed, 459 insertions(+), 9 deletions(-) diff --git a/changelog.txt b/changelog.txt index c0f0266..357bc5a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,8 +1,43 @@ == Changelog == += 3.4.0 = + +_Release date: 2026-09-19_ +**Highlights** + +* WP-CLI support (TODO #7): a `wp noindex-seo` command namespace with list, set, unset, export, import and bulk per-post overrides — automation-friendly and scaling past the ~200-post ceiling of the bulk UI. + +**Added** + +* **WP-CLI commands.** `wp noindex-seo list [--context=] [--format=table|csv|json|yaml]` (every context × directive pair with its status), `wp noindex-seo set [...]` / `wp noindex-seo unset [...]` (default: all five directives; saving clears the transient cache), `wp noindex-seo export []` (JSON payload to STDOUT or a file, same as the tools tab), `wp noindex-seo import [--dry-run]` (same validation and sanitization as the import form; `--dry-run` reports the number of values that would change), and `wp noindex-seo override ... [--off]` (bulk per-post granular overrides without the UI's selection limit; `--off` clears the override and directive meta). The commands enforce the same rules as the settings page: unknown contexts/directives are rejected and the attachment context refuses to enable while the implementation method is meta-only. +* `stubs/wp-cli-stubs.php`: minimal WP-CLI signatures for PHPStan and a recording stub for the test suite (never shipped; excluded from the release ZIP and from PHPCS). +* The undefined "migrate-attachments" command from the original TODO was replaced by the general-purpose `override` bulk command, which covers the same need for every post type. + +**Changed** + +* Nothing. + +**Fixed** + +* Nothing. + +**Security** + +* No security changes; the CLI handlers reuse the existing validated/sanitized helpers, operator-supplied file paths are checked with `is_readable()` before reading, and PHPCS/PHPStan/composer audit stay clean. + +**Compatibility** + +* WordPress: 4.7 - 7.2 +* PHP: 5.6 - 8.5 + +**Tests** + +* PHPUnit: 9.6.36 (185 tests, all passing): command registration, set/unset round-trips (including transient invalidation and defaults), unknown context/directive rejection, the attachment header-only invariant, list rows and filtering, export to STDOUT and file, import dry-run/apply/rejection, and override enable/disable/skip-missing. + = 3.3.0 = _Release date: 2026-09-19_ + **Highlights** * The settings page is reorganized with the native WordPress tab navigation (General / SEO Settings / Import-Export) and titled sections, built exclusively on core `nav-tab` classes with URL-based switching — no proprietary tab CSS or JavaScript. diff --git a/noindex-seo.php b/noindex-seo.php index 7f9ccb4..cfad957 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -3,7 +3,7 @@ * Plugin Name: noindex SEO * Plugin URI: https://www.robotstxt.software/plugins/noindex-seo/ * Description: Control search engine indexing with robots directives (noindex, nofollow, noarchive, nosnippet, noimageindex) for specific parts of your WordPress site. - * Version: 3.3.0 + * Version: 3.4.0 * Requires at least: 4.7 * Requires PHP: 5.6 * Author: ROBOTSTXT @@ -69,7 +69,7 @@ defined( 'ABSPATH' ) || die( 'Bye bye!' ); * form-handler wp_die() message corrected (I2); unreachable * REST fallbacks removed (I5). */ -define( 'NOINDEX_SEO_VERSION', '3.3.0' ); +define( 'NOINDEX_SEO_VERSION', '3.4.0' ); /** * Option key holding the consolidated plugin settings array. @@ -100,6 +100,7 @@ define( 'NOINDEX_SEO_SETTINGS_KEY', 'noindex_seo_settings' ); * config: array{ * method: string, * granular: int, + * taxonomies_granular: int, * seoplugins: int, * delete_on_uninstall: int * } @@ -2103,6 +2104,407 @@ function noindex_seo_handle_import() { } add_action( 'admin_post_noindex_seo_import', 'noindex_seo_handle_import' ); +/** + * Applies a directive change to a context in the consolidated settings. + * + * Shared logic of the `wp noindex-seo set|unset` commands: validates the + * context and the directives, enforces the header-only invariant for the + * attachment context, persists via noindex_seo_update_settings() (which + * also invalidates the transient cache) and reports what changed. + * + * @since 3.4.0 + * + * @param string $context Context slug. + * @param string[] $directives Directive slugs. + * @param bool $enable True to enable, false to disable. + * @return true|WP_Error True on success, WP_Error describing the first problem found. + */ +function noindex_seo_cli_apply_directives( $context, array $directives, $enable ) { + if ( ! in_array( $context, noindex_seo_get_contexts(), true ) ) { + return new WP_Error( 'noindex_seo_cli_unknown_context', sprintf( "Unknown context '%s'. Run 'wp noindex-seo list' to see the available contexts.", $context ) ); + } + + $valid_directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' ); + foreach ( $directives as $directive ) { + if ( ! in_array( $directive, $valid_directives, true ) ) { + return new WP_Error( 'noindex_seo_cli_unknown_directive', sprintf( "Unknown directive '%s'. Valid directives: %s.", $directive, implode( ', ', $valid_directives ) ) ); + } + } + + // The attachment context only works with HTTP headers (same rule as the + // settings page), so refuse enabling it under a meta-only method. + $method = noindex_seo_get_config( 'method', 'meta' ); + $method = is_string( $method ) ? $method : 'meta'; + if ( $enable && 'attachment' === $context && ! in_array( $method, array( 'header', 'both' ), true ) ) { + return new WP_Error( + 'noindex_seo_cli_header_only', + sprintf( + "The attachment context only works with HTTP headers; the current implementation method is '%s'. Switch the method first: wp option update noindex_seo_settings ... or use the settings page.", + $method + ) + ); + } + + $settings = noindex_seo_get_settings(); + foreach ( $directives as $directive ) { + $settings['contexts'][ $context ][ $directive ] = $enable ? 1 : 0; + } + + noindex_seo_update_settings( $settings ); + + return true; +} + +/** + * Builds the rows for the `wp noindex-seo list` command. + * + * @since 3.4.0 + * + * @param string $context_filter Optional context slug to filter by. Empty for all. + * @return array Rows. + */ +function noindex_seo_cli_get_rows( $context_filter = '' ) { + $directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' ); + $rows = array(); + + foreach ( noindex_seo_get_contexts() as $context ) { + if ( '' !== $context_filter && $context !== $context_filter ) { + continue; + } + foreach ( $directives as $directive ) { + $rows[] = array( + 'context' => $context, + 'directive' => $directive, + 'status' => noindex_seo_get_setting( $context, $directive ) ? 'on' : 'off', + ); + } + } + + return $rows; +} + +/** + * WP-CLI command: lists every context x directive pair and its status. + * + * ## OPTIONS + * + * [--context=] + * : Filter by a single context slug. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - json + * - yaml + * --- + * + * ## EXAMPLES + * + * wp noindex-seo list + * wp noindex-seo list --context=search --format=json + * + * @since 3.4.0 + * + * @param array $args Positional arguments (unused). + * @param array $assoc_args Associative arguments. + * @return void + */ +function noindex_seo_cli_list( $args, $assoc_args ) { + $context_filter = ''; + if ( isset( $assoc_args['context'] ) && is_string( $assoc_args['context'] ) && '' !== $assoc_args['context'] ) { + // Validate the raw value against the known contexts: sanitize_key() + // alone would silently turn an invalid value into '' and list everything. + if ( ! in_array( $assoc_args['context'], noindex_seo_get_contexts(), true ) ) { + WP_CLI::error( sprintf( "Unknown context '%s'. Run 'wp noindex-seo list' to see the available contexts.", $assoc_args['context'] ) ); + return; + } + $context_filter = $assoc_args['context']; + } + + $rows = noindex_seo_cli_get_rows( $context_filter ); + + $format = isset( $assoc_args['format'] ) && is_string( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; + WP_CLI\Utils::format_items( $format, $rows, array( 'context', 'directive', 'status' ) ); +} + +/** + * WP-CLI command: enables directives on a context. + * + * ## OPTIONS + * + * + * : Context slug (see `wp noindex-seo list`). + * + * [...] + * : One or more directives to enable. Default: all five. + * + * ## EXAMPLES + * + * wp noindex-seo set search noindex + * wp noindex-seo set date noindex noarchive + * + * @since 3.4.0 + * + * @param array $args [context, directive...]. + * @param array $assoc_args Associative arguments (unused). + * @return void + */ +function noindex_seo_cli_set( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- required by the WP-CLI callback signature + if ( empty( $args ) ) { + WP_CLI::error( 'Usage: wp noindex-seo set [...]' ); + return; + } + + $context = sanitize_key( (string) array_shift( $args ) ); + $directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' ); + if ( ! empty( $args ) ) { + $directives = array_values( array_map( 'sanitize_key', $args ) ); + } + + $result = noindex_seo_cli_apply_directives( $context, $directives, true ); + if ( is_wp_error( $result ) ) { + WP_CLI::error( $result->get_error_message() ); + return; + } + + WP_CLI::success( sprintf( "Enabled %s on '%s'.", implode( ', ', $directives ), $context ) ); +} + +/** + * WP-CLI command: disables directives on a context. + * + * ## OPTIONS + * + * + * : Context slug (see `wp noindex-seo list`). + * + * [...] + * : One or more directives to disable. Default: all five. + * + * ## EXAMPLES + * + * wp noindex-seo unset search noindex + * + * @since 3.4.0 + * + * @param array $args [context, directive...]. + * @param array $assoc_args Associative arguments (unused). + * @return void + */ +function noindex_seo_cli_unset( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- required by the WP-CLI callback signature + if ( empty( $args ) ) { + WP_CLI::error( 'Usage: wp noindex-seo unset [...]' ); + return; + } + + $context = sanitize_key( (string) array_shift( $args ) ); + $directives = array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' ); + if ( ! empty( $args ) ) { + $directives = array_values( array_map( 'sanitize_key', $args ) ); + } + + $result = noindex_seo_cli_apply_directives( $context, $directives, false ); + if ( is_wp_error( $result ) ) { + WP_CLI::error( $result->get_error_message() ); + return; + } + + WP_CLI::success( sprintf( "Disabled %s on '%s'.", implode( ', ', $directives ), $context ) ); +} + +/** + * WP-CLI command: exports the settings as JSON. + * + * Writes the same payload as the "Export settings" button. + * + * ## OPTIONS + * + * [] + * : Path of the JSON file to write. Default: print to STDOUT. + * + * ## EXAMPLES + * + * wp noindex-seo export > noindex-seo-backup.json + * wp noindex-seo export backup.json + * + * @since 3.4.0 + * + * @param array $args []. + * @param array $assoc_args Associative arguments (unused). + * @return void + */ +function noindex_seo_cli_export( $args, $assoc_args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- required by the WP-CLI callback signature + $json = wp_json_encode( noindex_seo_build_export_payload() ); + if ( false === $json ) { + WP_CLI::error( 'Could not encode the settings as JSON.' ); + return; + } + + if ( empty( $args ) ) { + WP_CLI::line( $json ); + return; + } + + $file = (string) $args[0]; + $written = file_put_contents( $file, $json . "\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- CLI context, the path is operator-supplied. + if ( false === $written ) { + WP_CLI::error( sprintf( "Could not write to '%s'.", $file ) ); + } + + WP_CLI::success( sprintf( "Exported settings to '%s'.", $file ) ); +} + +/** + * WP-CLI command: imports settings from a previously exported JSON file. + * + * Applies the same validation and sanitization as the "Import settings" + * form; per-post and per-term overrides are never touched. + * + * ## OPTIONS + * + * + * : Path of a JSON file produced by `wp noindex-seo export` or the tools tab. + * + * [--dry-run] + * : Validate and report the number of values that would change, without saving. + * + * ## EXAMPLES + * + * wp noindex-seo import noindex-seo-backup.json --dry-run + * wp noindex-seo import noindex-seo-backup.json + * + * @since 3.4.0 + * + * @param array $args []. + * @param array $assoc_args Associative arguments. + * @return void + */ +function noindex_seo_cli_import( $args, $assoc_args ) { + if ( empty( $args ) ) { + WP_CLI::error( 'Usage: wp noindex-seo import [--dry-run]' ); + return; + } + + $file = (string) $args[0]; + if ( ! is_readable( $file ) ) { + WP_CLI::error( sprintf( "Cannot read '%s'.", $file ) ); + return; + } + + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- CLI context, local operator-supplied file, existence checked above. + $raw = file_get_contents( $file ); + $data = is_string( $raw ) ? json_decode( $raw, true ) : null; + + if ( ! is_array( $data ) ) { + WP_CLI::error( sprintf( "'%s' is not a valid noindex SEO export.", $file ) ); + return; + } + + $marker = isset( $data['plugin'] ) && is_string( $data['plugin'] ) ? $data['plugin'] : ''; + if ( 'noindex-seo' !== $marker ) { + WP_CLI::error( sprintf( "'%s' is not a valid noindex SEO export.", $file ) ); + return; + } + + $settings = noindex_seo_sanitize_imported_settings( isset( $data['settings'] ) ? $data['settings'] : null ); + if ( null === $settings ) { + WP_CLI::error( sprintf( "'%s' does not contain valid settings.", $file ) ); + return; + } + + if ( isset( $assoc_args['dry-run'] ) ) { + $current = noindex_seo_get_settings(); + $changes = 0; + foreach ( $settings['contexts'] as $context => $directives ) { + foreach ( $directives as $directive => $value ) { + if ( $current['contexts'][ $context ][ $directive ] !== $value ) { + ++$changes; + } + } + } + foreach ( $settings['config'] as $key => $value ) { + if ( $current['config'][ $key ] !== $value ) { + ++$changes; + } + } + WP_CLI::line( sprintf( 'Dry run: %d setting(s) would change. Re-run without --dry-run to apply.', $changes ) ); + return; + } + + noindex_seo_update_settings( $settings ); + WP_CLI::success( 'Settings imported.' ); +} + +/** + * WP-CLI command: bulk per-post granular overrides. + * + * Scales beyond the ~200-post ceiling of the bulk UI: enables or disables + * the robots override for the given post IDs, mirroring the "Enable / + * Disable Robots Override" bulk actions. + * + * ## OPTIONS + * + * ... + * : One or more post IDs. + * + * [--off] + * : Disable the override (default: enable). Disabling also clears the + * directive meta so the posts fall back to the global settings. + * + * ## EXAMPLES + * + * wp noindex-seo override 123 456 + * wp noindex-seo override $(wp post list --post_type=page --field=ID) --off + * + * @since 3.4.0 + * + * @param array $args Post IDs. + * @param array $assoc_args Associative arguments. + * @return void + */ +function noindex_seo_cli_override( $args, $assoc_args ) { + if ( empty( $args ) ) { + WP_CLI::error( 'Usage: wp noindex-seo override ... [--off]' ); + return; + } + + $disable = isset( $assoc_args['off'] ); + $done = 0; + + foreach ( $args as $raw_id ) { + $post_id = (int) $raw_id; + $post = get_post( $post_id ); + if ( ! $post ) { + WP_CLI::warning( sprintf( 'Skipping %d: post does not exist.', $post_id ) ); + continue; + } + + if ( $disable ) { + delete_post_meta( $post_id, '_noindex_seo_override' ); + noindex_seo_clear_post_directives( $post_id ); + } else { + update_post_meta( $post_id, '_noindex_seo_override', 1 ); + } + ++$done; + } + + WP_CLI::success( sprintf( '%s override for %d post(s).', $disable ? 'Disabled' : 'Enabled', $done ) ); +} + +// Register the WP-CLI commands only when running under WP-CLI. +if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI' ) ) { + WP_CLI::add_command( 'noindex-seo list', 'noindex_seo_cli_list' ); + WP_CLI::add_command( 'noindex-seo set', 'noindex_seo_cli_set' ); + WP_CLI::add_command( 'noindex-seo unset', 'noindex_seo_cli_unset' ); + WP_CLI::add_command( 'noindex-seo export', 'noindex_seo_cli_export' ); + WP_CLI::add_command( 'noindex-seo import', 'noindex_seo_cli_import' ); + WP_CLI::add_command( 'noindex-seo override', 'noindex_seo_cli_override' ); +} + /** * Returns the list of contexts flagged as suggested defaults on the settings page. * diff --git a/readme.txt b/readme.txt index 55f4eb7..34e47bb 100644 --- a/readme.txt +++ b/readme.txt @@ -3,9 +3,9 @@ Contributors: robotstxt, javiercasares Tags: seo, noindex, nofollow, noarchive, robots Requires at least: 4.7 Tested up to: 7.2 -Stable tag: 3.3.0 +Stable tag: 3.4.0 Requires PHP: 5.6 -Version: 3.3.0 +Version: 3.4.0 License: GPL-3.0-or-later License URI: https://www.gnu.org/licenses/gpl-3.0.txt @@ -99,6 +99,19 @@ Important note: if you have any doubt about any of the following items it is bes == Using the plugin == += WP-CLI = + +The plugin ships a `wp noindex-seo` command namespace (WP-CLI 2.x required): + +* `wp noindex-seo list [--context=] [--format=]` — lists every context × directive pair and its on/off status (table, csv, json or yaml). +* `wp noindex-seo set [...]` — enables directives on a context (default: all five). Example: `wp noindex-seo set search noindex noarchive`. +* `wp noindex-seo unset [...]` — disables directives on a context. +* `wp noindex-seo export []` — prints the settings payload as JSON to STDOUT, or writes it to a file. +* `wp noindex-seo import [--dry-run]` — restores settings from an export; `--dry-run` reports how many values would change without saving. +* `wp noindex-seo override ... [--off]` — bulk per-post granular overrides; scales past the ~200-post ceiling of the bulk UI. `--off` also clears the directive meta. + +The commands enforce the same rules as the settings page (unknown contexts and directives are rejected; the attachment context refuses to enable while the implementation method is meta-only). + = Export / Import = The settings page includes an "Export settings" button that downloads the current global settings as a JSON file, and an "Import settings" form that restores them on the same or another site (useful for migrations and staging-to-production copies). Imports are strictly validated, and per-post / per-term overrides are never touched. @@ -143,6 +156,10 @@ No. By default all plugin data is preserved on uninstall. If you want a complete == Changelog == += 3.4.0 = + +WP-CLI support: a `wp noindex-seo` command namespace with `list`, `set`, `unset`, `export`, `import` and bulk per-post `override` — automation-friendly and scaling past the ~200-post ceiling of the bulk UI. + = 3.3.0 = Settings page reorganized with the native WordPress tab navigation (General / SEO Settings / Import-Export, no custom tab code) and titled sections; saving is tab-scoped so hidden tabs never reset each other's values. @@ -151,10 +168,6 @@ Settings page reorganized with the native WordPress tab navigation (General / SE Feature release: JSON export/import of the settings (validated, sanitized, per-post and per-term overrides untouched) and runtime conflict verification against the `wp_robots` API (foreign callbacks named in a dismissible notice). -= 3.1.6 = - -Admin UI modernization: all inline styles removed in favour of WordPress admin color-scheme-aware, RTL-ready classes (follows the admin color schemes instead of hardcoded colors); stale form-handler message corrected; unreachable REST fallbacks removed; Catalan and Spanish translations updated. - = Previous versions = If you want to see the full changelog, visit the [noindex SEO plugin page](https://www.robotstxt.software/plugins/noindex-seo/). @@ -175,6 +188,6 @@ This plugin adheres to the following security measures and review protocols for == Vulnerabilities == -* No vulnerabilities have been published up to version 3.3.0. +* No vulnerabilities have been published up to version 3.4.0. Found a security vulnerability? Please report it to us privately at the [noindex SEO GitHub repository](https://github.com/javiercasares/noindex-seo/security/advisories/new).