WordPress 7.1.3

This commit is contained in:
2026-10-06 18:59:45 -06:00
parent e2c26b7f21
commit 5c09d6fa68
51 changed files with 1111 additions and 825 deletions
@@ -13,8 +13,6 @@ namespace RankMath\CLI;
use WP_CLI;
use WP_CLI_Command;
use RankMath\Helper;
use RankMath\Sitemap\Cache;
use RankMath\Sitemap\Sitemap_XML;
defined( 'ABSPATH' ) || exit;
@@ -35,10 +33,6 @@ class Commands extends WP_CLI_Command {
return;
}
Cache::invalidate_storage();
$generator = new Sitemap_XML( '1' );
$generator->get_output();
WP_CLI::success( 'Sitemap generated.' );
( new Sitemap_WPCLI() )->cache_sitemaps();
}
}
@@ -0,0 +1,225 @@
<?php
/**
* Rank Math sitemap CLI commands.
*
* @since 1.0.280
* @package RankMath
* @subpackage RankMath\WP_CLI
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\CLI;
use WP_CLI;
use RankMath\Traits\Hooker;
use RankMath\Sitemap\Cache;
defined( 'ABSPATH' ) || exit;
/**
* Class Sitemap_WPCLI.
* Generate sitemap cache files via CLI.
*
* Better used with the filter add_filter( 'rank_math/sitemap/invalidate_storage', '__return_false' );
* so cache files are only deleted and regenerated via WP CLI
*/
class Sitemap_WPCLI extends \RankMath\Sitemap\Generator {
use Hooker;
/**
* Sitemap cache instance.
*
* @var Cache
*/
private $cache;
/**
* Number of sitemaps that could not be generated.
*
* @var int
*/
private $failed = 0;
/**
* Holds index pages discovered through the sitemap/index/entry filter.
* In the form of type (eg post) -> object type (eg product) -> index count.
*
* @var array
*/
public $index_counts = [];
/**
* Disable sitemap redirection.
*
* Sitemap providers call Sitemap::maybe_redirect() while building links, which
* redirects and die()s on the front-end. That must never happen under WP-CLI.
*
* @return false
*/
public function disable_redirect() {
return false;
}
/**
* Force cache invalidation.
*
* Sites are advised to disable automatic invalidation via
* `rank_math/sitemap/invalidate_storage` so caches are only rebuilt from here.
* This runs at PHP_INT_MAX so it reliably wins over such a filter regardless of
* the priority it was registered at.
*
* @param bool $invalidate Whether to invalidate storage.
* @param null|string $type Type of storage to invalidate.
*
* @return true
*/
public function invalidate_storage( $invalidate, $type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
return true;
}
/**
* Cache sitemaps.
*
* @return void
*/
public function cache_sitemaps() {
$this->filter( 'rank_math/sitemap/invalidate_storage', 'invalidate_storage', PHP_INT_MAX, 2 );
try {
Cache::invalidate_storage();
} finally {
$this->remove_filter( 'rank_math/sitemap/invalidate_storage', 'invalidate_storage', PHP_INT_MAX );
}
$this->filter( 'rank_math/sitemap/maybe_redirect', 'disable_redirect' );
$this->filter( 'rank_math/sitemap/index/entry', 'get_indexes_listener', 100, 3 );
try {
$this->generate_sitemaps();
} finally {
$this->remove_filter( 'rank_math/sitemap/maybe_redirect', 'disable_redirect' );
$this->remove_filter( 'rank_math/sitemap/index/entry', 'get_indexes_listener', 100 );
}
}
/**
* Checks whether a sitemap can be built for the given object type.
*
* A type is buildable when a provider claims it through handles_type() - the
* canonical check used everywhere else in the sitemap code, which already accounts
* for filtered slugs and per-type settings - or when it is contributed purely
* through the `sitemap/{$type}/content` filter, as the Local SEO KML sitemap is.
*
* @param string $object_type The object type to check, eg product or local.
*
* @return bool
*/
protected function is_buildable_type( $object_type ) {
foreach ( $this->providers as $provider ) {
if ( $provider->handles_type( $object_type ) ) {
return true;
}
}
return has_filter( "rank_math/sitemap/{$object_type}/content" );
}
/**
* Listens to the sitemap/index/entry hook and records how many index pages each
* object type has, so the matching sitemaps can be generated afterwards.
*
* Providers do not pass a uniform third argument: post and taxonomy pass the object
* type as a string, author passes a WP_User, video passes an array, while news and
* the Local SEO KML file pass nothing at all. Anything that is not a usable string
* falls back to the provider type, which is also the sitemap slug for those cases.
*
* @param array $item The index URL entry.
* @param string $type The provider type, eg post or term.
* @param mixed $object_type The object type, eg product or category. Not always a string.
*
* @return array
*/
public function get_indexes_listener( $item, $type = '', $object_type = null ) {
$type = 'term' === $type ? 'taxonomy' : $type;
if ( ! is_string( $object_type ) || '' === $object_type ) {
$object_type = $type;
}
if ( '' === $object_type ) {
return $item;
}
if ( ! isset( $this->index_counts[ $type ][ $object_type ] ) ) {
$this->index_counts[ $type ][ $object_type ] = 1;
} else {
++$this->index_counts[ $type ][ $object_type ];
}
return $item;
}
/**
* Generates sitemaps and adds related cache files.
*
* @return void
*/
public function generate_sitemaps() {
$this->cache = new Cache();
$this->failed = 0;
$total_files = 0;
try {
// Saves the rootmap i.e sitemap_index.xml.
$total_files += $this->store( '1', 1, 'index' );
foreach ( $this->index_counts as $items ) {
foreach ( $items as $object_type => $count ) {
if ( ! $this->is_buildable_type( $object_type ) ) {
WP_CLI::warning( sprintf( 'Nothing can build a sitemap for "%s", skipping.', $object_type ) );
continue;
}
$pages = max( 1, $count );
for ( $page = 1; $page <= $pages; $page++ ) {
$total_files += $this->store( $object_type, $page, $object_type );
}
}
}
} catch ( \Throwable $ex ) {
WP_CLI::error( $ex->getMessage() );
}
if ( $this->failed > 0 ) {
WP_CLI::error( sprintf( 'Generated %d sitemap cache file(s), %d failed.', $total_files, $this->failed ) );
}
WP_CLI::success( sprintf( 'Generated %d sitemap cache file(s).', $total_files ) );
}
/**
* Builds a single sitemap and writes it to the cache.
*
* @param string $type Sitemap type, or '1' for the index sitemap.
* @param int $page Page number to generate.
* @param string $label Human readable name used in the CLI output.
*
* @return int 1 when the sitemap was stored, 0 otherwise.
*/
private function store( $type, $page, $label ) {
$sitemap = $this->get_output( $type, $page );
if ( '' === $sitemap ) {
WP_CLI::warning( sprintf( 'Empty output for "%s" page %d, nothing cached.', $label, $page ) );
++$this->failed;
return 0;
}
if ( false === $this->cache->store_sitemap( $type, $page, $sitemap ) ) {
WP_CLI::warning( sprintf( 'Could not cache "%s" page %d.', $label, $page ) );
++$this->failed;
return 0;
}
WP_CLI::log( sprintf( 'Generated sitemap for %s, page %d.', $label, $page ) );
return 1;
}
}
@@ -830,10 +830,12 @@ trait WordPress {
* @return array
*/
public static function filetype_and_ext( $types, $file, $filename ) {
if ( false !== strpos( $filename, '.json' ) ) {
// Validate the real trailing extension so names like `file.json.php` are rejected.
$extension = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
if ( 'json' === $extension ) {
$types['ext'] = 'json';
$types['type'] = 'application/json';
} elseif ( false !== strpos( $filename, '.txt' ) ) {
} elseif ( 'txt' === $extension ) {
$types['ext'] = 'txt';
$types['type'] = 'text/plain';
}
@@ -439,6 +439,9 @@ class DB {
return '';
}
$host = Helper::get_home_url();
$url = str_replace( $host, '', $url );
$url = urldecode( preg_replace( '/#.*$/', '', $url ) );
$url = self::remove_hash( $url );
@@ -446,22 +449,18 @@ class DB {
// Parse the URL to get the path component.
$parsed_url = wp_parse_url( $url );
if ( isset( $parsed_url['path'] ) ) {
return $parsed_url['path'];
$page = $parsed_url['path'];
} else {
// Remove ASCII domain. idn_to_ascii() needs ext-intl, which isn't
// guaranteed to be installed on every host — fall back to the
// unconverted host rather than fatal-erroring when it's missing.
$host_ascii = function_exists( 'idn_to_ascii' ) ? idn_to_ascii( $host ) : $host;
$url = str_replace( $host_ascii, '', $url );
$page = preg_replace( '#^https?://(www\.)?#i', '', $url );
}
// Fallback: try to extract path by removing domain.
$host = Helper::get_home_url();
$url = str_replace( $host, '', $url );
// Remove ASCII domain. idn_to_ascii() needs ext-intl, which isn't
// guaranteed to be installed on every host — fall back to the
// unconverted host rather than fatal-erroring when it's missing.
$host_ascii = function_exists( 'idn_to_ascii' ) ? idn_to_ascii( $host ) : $host;
$url = str_replace( $host_ascii, '', $url );
$url = preg_replace( '#^https?://(www\.)?#i', '', $url );
return apply_filters( 'rank_math/analytics/get_page', $url );
return apply_filters( 'rank_math/analytics/get_page', $page );
}
/**
@@ -61,8 +61,9 @@ class Objects extends Summary {
'noData' => $request->get_param( 'noData' ),
];
$field_name = 'seo_score';
$per_page = $request->get_param( 'per_page' ) ? sanitize_text_field( $request->get_param( 'per_page' ) ) : 25;
$offset = ( sanitize_text_field( $request->get_param( 'page' ) ) - 1 ) * $per_page;
$per_page = absint( $request->get_param( 'per_page' ) );
$per_page = $per_page ? min( $per_page, 100 ) : 25;
$offset = ( max( 1, absint( $request->get_param( 'page' ) ) ) - 1 ) * $per_page;
// Construct SQL condition based on filter parameters.
$conditions = [];
@@ -48,6 +48,20 @@ class Rest extends WP_REST_Controller {
],
'postsRowsByObjects' => [
'callback' => [ Stats::get(), 'get_posts_rows_by_objects' ],
'args' => [
'page' => [
'description' => esc_html__( 'Page number.', 'seo-by-rank-math' ),
'type' => 'integer',
'required' => false,
'sanitize_callback' => 'absint',
],
'per_page' => [
'description' => esc_html__( 'Results per page.', 'seo-by-rank-math' ),
'type' => 'integer',
'required' => false,
'sanitize_callback' => 'absint',
],
],
],
'post/(?P<id>\d+)' => [
'callback' => [ $this, 'get_post' ],
@@ -250,7 +250,7 @@ class Table extends List_Table {
'deactivate' => '<a href="' . $url . '" data-action="deactivate" class="rank-math-redirection-action">' . esc_html__( 'Deactivate', 'seo-by-rank-math' ) . '</a>',
'activate' => '<a href="' . $url . '" data-action="activate" class="rank-math-redirection-action">' . esc_html__( 'Activate', 'seo-by-rank-math' ) . '</a>',
'trash' => '<a href="' . $url . '" data-action="trash" class="rank-math-redirection-action">' . esc_html__( 'Trash', 'seo-by-rank-math' ) . '</a>',
'view' => '<a href="' . $view_url . '" rel="bookmark">' . esc_html__( 'View', 'seo-by-rank-math' ) . '</a>',
'view' => '<a href="' . esc_url( $view_url ) . '" rel="bookmark">' . esc_html__( 'View', 'seo-by-rank-math' ) . '</a>',
]
);
}
@@ -58,8 +58,11 @@ class DB {
'usermeta' => 'user_id',
];
$key = isset( $keys_by_table[ $table ] ) ? $keys_by_table[ $table ] : 'post_id';
$data = self::table( $table )
->select( 'meta_id' )
// The wp_usermeta table's primary key column is umeta_id, unlike post/term/comment meta tables which use meta_id.
$id_column = 'usermeta' === $table ? 'umeta_id' : 'meta_id';
$data = self::table( $table )
->select( [ $id_column => 'meta_id' ] )
->select( 'meta_value' )
->where( $key, $object_id )
->whereLike( 'meta_key', 'rank_math_schema', '' )
@@ -89,7 +89,7 @@ class SEO_Analyzer {
$this->analyse_url = home_url();
$this->action( 'init', 'set_url' );
$this->maybe_clear_storage();
$this->action( 'admin_init', 'maybe_clear_storage' );
$this->ajax( 'analyze', 'analyze_me' );
$this->ajax( 'enable_auto_update', 'enable_auto_update' );
@@ -235,13 +235,22 @@ class SEO_Analyzer {
/**
* Clear stored results if needed.
*/
private function maybe_clear_storage() {
if ( '1' === Param::request( 'clear_results' ) ) {
delete_option( 'rank_math_seo_analysis_results' );
delete_option( 'rank_math_seo_analysis_date' );
Helper::redirect( Security::remove_query_arg_raw( 'clear_results' ) );
exit;
public function maybe_clear_storage() {
if ( '1' !== Param::request( 'clear_results' ) ) {
return;
}
if (
! Helper::has_cap( 'site_analysis' ) ||
! wp_verify_nonce( Param::request( 'security' ), 'rank-math-ajax-nonce' )
) {
return;
}
delete_option( 'rank_math_seo_analysis_results' );
delete_option( 'rank_math_seo_analysis_date' );
Helper::redirect( Security::remove_query_arg_raw( [ 'clear_results', 'security' ] ) );
exit;
}
/**
@@ -307,6 +307,9 @@ class Sitemap {
* @param int $max_entries Entries per sitemap.
*/
public static function maybe_redirect( $count, $max_entries ) {
if ( ! apply_filters( 'rank_math/sitemap/maybe_redirect', true ) ) {
return;
}
$current_page = (int) get_query_var( 'sitemap_n' );
if ( ! $current_page && $count > $max_entries ) {
Helper::redirect( preg_replace( '/\.xml$/', '1.xml', Helper::get_current_page_url() ) );
@@ -50,6 +50,7 @@ class Import_Export_Settings {
// Parse Options.
$wp_filesystem = Helper::get_filesystem();
if ( is_null( $wp_filesystem ) || ! Helper::is_filesystem_direct() ) {
\wp_delete_file( $file['file'] );
return [
'error' => esc_html__( 'Uploaded file could not be read.', 'seo-by-rank-math' ),
];