Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordpres.../inc/sitemaps
File: class-sitemaps-cache-validator.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WPSEO plugin file.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WPSEO\XML_Sitemaps
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Handles storage keys for sitemaps caching and invalidation.
[8] Fix | Delete
*
[9] Fix | Delete
* @since 3.2
[10] Fix | Delete
*/
[11] Fix | Delete
class WPSEO_Sitemaps_Cache_Validator {
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Prefix of the transient key for sitemap caches.
[15] Fix | Delete
*
[16] Fix | Delete
* @var string
[17] Fix | Delete
*/
[18] Fix | Delete
public const STORAGE_KEY_PREFIX = 'yst_sm_';
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Name of the option that holds the global validation value.
[22] Fix | Delete
*
[23] Fix | Delete
* @var string
[24] Fix | Delete
*/
[25] Fix | Delete
public const VALIDATION_GLOBAL_KEY = 'wpseo_sitemap_cache_validator_global';
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* The format which creates the key of the option that holds the type validation value.
[29] Fix | Delete
*
[30] Fix | Delete
* @var string
[31] Fix | Delete
*/
[32] Fix | Delete
public const VALIDATION_TYPE_KEY_FORMAT = 'wpseo_sitemap_%s_cache_validator';
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Get the cache key for a certain type and page.
[36] Fix | Delete
*
[37] Fix | Delete
* A type of cache would be something like 'page', 'post' or 'video'.
[38] Fix | Delete
*
[39] Fix | Delete
* Example key format for sitemap type "post", page 1: wpseo_sitemap_post_1:akfw3e_23azBa .
[40] Fix | Delete
*
[41] Fix | Delete
* @since 3.2
[42] Fix | Delete
*
[43] Fix | Delete
* @param string|null $type The type to get the key for. Null or self::SITEMAP_INDEX_TYPE for index cache.
[44] Fix | Delete
* @param int $page The page of cache to get the key for.
[45] Fix | Delete
*
[46] Fix | Delete
* @return bool|string The key where the cache is stored on. False if the key could not be generated.
[47] Fix | Delete
*/
[48] Fix | Delete
public static function get_storage_key( $type = null, $page = 1 ) {
[49] Fix | Delete
[50] Fix | Delete
// Using SITEMAP_INDEX_TYPE for sitemap index cache.
[51] Fix | Delete
$type = is_null( $type ) ? WPSEO_Sitemaps::SITEMAP_INDEX_TYPE : $type;
[52] Fix | Delete
[53] Fix | Delete
$global_cache_validator = self::get_validator();
[54] Fix | Delete
$type_cache_validator = self::get_validator( $type );
[55] Fix | Delete
[56] Fix | Delete
$prefix = self::STORAGE_KEY_PREFIX;
[57] Fix | Delete
$postfix = sprintf( '_%d:%s_%s', $page, $global_cache_validator, $type_cache_validator );
[58] Fix | Delete
[59] Fix | Delete
try {
[60] Fix | Delete
$type = self::truncate_type( $type, $prefix, $postfix );
[61] Fix | Delete
} catch ( OutOfBoundsException $exception ) {
[62] Fix | Delete
// Maybe do something with the exception, for now just mark as invalid.
[63] Fix | Delete
return false;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
// Build key.
[67] Fix | Delete
$full_key = $prefix . $type . $postfix;
[68] Fix | Delete
[69] Fix | Delete
return $full_key;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* If the type is over length make sure we compact it so we don't have any database problems.
[74] Fix | Delete
*
[75] Fix | Delete
* When there are more 'extremely long' post types, changes are they have variations in either the start or ending.
[76] Fix | Delete
* Because of this, we cut out the excess in the middle which should result in less chance of collision.
[77] Fix | Delete
*
[78] Fix | Delete
* @since 3.2
[79] Fix | Delete
*
[80] Fix | Delete
* @param string $type The type of sitemap to be used.
[81] Fix | Delete
* @param string $prefix The part before the type in the cache key. Only the length is used.
[82] Fix | Delete
* @param string $postfix The part after the type in the cache key. Only the length is used.
[83] Fix | Delete
*
[84] Fix | Delete
* @return string The type with a safe length to use
[85] Fix | Delete
*
[86] Fix | Delete
* @throws OutOfRangeException When there is less than 15 characters of space for a key that is originally longer.
[87] Fix | Delete
*/
[88] Fix | Delete
public static function truncate_type( $type, $prefix = '', $postfix = '' ) {
[89] Fix | Delete
/*
[90] Fix | Delete
* This length has been restricted by the database column length of 64 in the past.
[91] Fix | Delete
* The prefix added by WordPress is '_transient_' because we are saving to a transient.
[92] Fix | Delete
* We need to use a timeout on the transient, otherwise the values get autoloaded, this adds
[93] Fix | Delete
* another restriction to the length.
[94] Fix | Delete
*/
[95] Fix | Delete
$max_length = 45; // 64 - 19 ('_transient_timeout_')
[96] Fix | Delete
$max_length -= strlen( $prefix );
[97] Fix | Delete
$max_length -= strlen( $postfix );
[98] Fix | Delete
[99] Fix | Delete
if ( strlen( $type ) > $max_length ) {
[100] Fix | Delete
[101] Fix | Delete
if ( $max_length < 15 ) {
[102] Fix | Delete
/*
[103] Fix | Delete
* If this happens the most likely cause is a page number that is too high.
[104] Fix | Delete
*
[105] Fix | Delete
* So this would not happen unintentionally.
[106] Fix | Delete
* Either by trying to cause a high server load, finding backdoors or misconfiguration.
[107] Fix | Delete
*/
[108] Fix | Delete
throw new OutOfRangeException(
[109] Fix | Delete
__(
[110] Fix | Delete
'Trying to build the sitemap cache key, but the postfix and prefix combination leaves too little room to do this. You are probably requesting a page that is way out of the expected range.',
[111] Fix | Delete
'wordpress-seo'
[112] Fix | Delete
)
[113] Fix | Delete
);
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
$half = ( $max_length / 2 );
[117] Fix | Delete
[118] Fix | Delete
$first_part = substr( $type, 0, ( ceil( $half ) - 1 ) );
[119] Fix | Delete
$last_part = substr( $type, ( 1 - floor( $half ) ) );
[120] Fix | Delete
[121] Fix | Delete
$type = $first_part . '..' . $last_part;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
return $type;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
/**
[128] Fix | Delete
* Invalidate sitemap cache.
[129] Fix | Delete
*
[130] Fix | Delete
* @since 3.2
[131] Fix | Delete
*
[132] Fix | Delete
* @param string|null $type The type to get the key for. Null for all caches.
[133] Fix | Delete
*
[134] Fix | Delete
* @return void
[135] Fix | Delete
*/
[136] Fix | Delete
public static function invalidate_storage( $type = null ) {
[137] Fix | Delete
[138] Fix | Delete
// Global validator gets cleared when no type is provided.
[139] Fix | Delete
$old_validator = null;
[140] Fix | Delete
[141] Fix | Delete
// Get the current type validator.
[142] Fix | Delete
if ( ! is_null( $type ) ) {
[143] Fix | Delete
$old_validator = self::get_validator( $type );
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
// Refresh validator.
[147] Fix | Delete
self::create_validator( $type );
[148] Fix | Delete
[149] Fix | Delete
if ( ! wp_using_ext_object_cache() ) {
[150] Fix | Delete
// Clean up current cache from the database.
[151] Fix | Delete
self::cleanup_database( $type, $old_validator );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
// External object cache pushes old and unretrieved items out by itself so we don't have to do anything for that.
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Cleanup invalidated database cache.
[159] Fix | Delete
*
[160] Fix | Delete
* @since 3.2
[161] Fix | Delete
*
[162] Fix | Delete
* @param string|null $type The type of sitemap to clear cache for.
[163] Fix | Delete
* @param string|null $validator The validator to clear cache of.
[164] Fix | Delete
*
[165] Fix | Delete
* @return void
[166] Fix | Delete
*/
[167] Fix | Delete
public static function cleanup_database( $type = null, $validator = null ) {
[168] Fix | Delete
[169] Fix | Delete
global $wpdb;
[170] Fix | Delete
[171] Fix | Delete
if ( is_null( $type ) ) {
[172] Fix | Delete
// Clear all cache if no type is provided.
[173] Fix | Delete
$like = sprintf( '%s%%', self::STORAGE_KEY_PREFIX );
[174] Fix | Delete
}
[175] Fix | Delete
else {
[176] Fix | Delete
// Clear type cache for all type keys.
[177] Fix | Delete
$like = sprintf( '%1$s%2$s_%%', self::STORAGE_KEY_PREFIX, $type );
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
/*
[181] Fix | Delete
* Add slashes to the LIKE "_" single character wildcard.
[182] Fix | Delete
*
[183] Fix | Delete
* We can't use `esc_like` here because we need the % in the query.
[184] Fix | Delete
*/
[185] Fix | Delete
$where = [];
[186] Fix | Delete
$where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_' . $like, '_' ) );
[187] Fix | Delete
$where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_timeout_' . $like, '_' ) );
[188] Fix | Delete
[189] Fix | Delete
// Delete transients.
[190] Fix | Delete
//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to use a direct query here.
[191] Fix | Delete
//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
[192] Fix | Delete
$wpdb->query(
[193] Fix | Delete
$wpdb->prepare(
[194] Fix | Delete
//phpcs:disable WordPress.DB.PreparedSQLPlaceholders -- %i placeholder is still not recognized.
[195] Fix | Delete
'DELETE FROM %i WHERE ' . implode( ' OR ', array_fill( 0, count( $where ), '%s' ) ),
[196] Fix | Delete
array_merge( [ $wpdb->options ], $where )
[197] Fix | Delete
)
[198] Fix | Delete
);
[199] Fix | Delete
[200] Fix | Delete
wp_cache_delete( 'alloptions', 'options' );
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Get the current cache validator.
[205] Fix | Delete
*
[206] Fix | Delete
* Without the type the global validator is returned.
[207] Fix | Delete
* This can invalidate -all- keys in cache at once.
[208] Fix | Delete
*
[209] Fix | Delete
* With the type parameter the validator for that specific type can be invalidated.
[210] Fix | Delete
*
[211] Fix | Delete
* @since 3.2
[212] Fix | Delete
*
[213] Fix | Delete
* @param string $type Provide a type for a specific type validator, empty for global validator.
[214] Fix | Delete
*
[215] Fix | Delete
* @return string|null The validator for the supplied type.
[216] Fix | Delete
*/
[217] Fix | Delete
public static function get_validator( $type = '' ) {
[218] Fix | Delete
[219] Fix | Delete
$key = self::get_validator_key( $type );
[220] Fix | Delete
[221] Fix | Delete
$current = get_option( $key, null );
[222] Fix | Delete
if ( ! is_null( $current ) ) {
[223] Fix | Delete
return $current;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
if ( self::create_validator( $type ) ) {
[227] Fix | Delete
return self::get_validator( $type );
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
return null;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Get the cache validator option key for the specified type.
[235] Fix | Delete
*
[236] Fix | Delete
* @since 3.2
[237] Fix | Delete
*
[238] Fix | Delete
* @param string $type Provide a type for a specific type validator, empty for global validator.
[239] Fix | Delete
*
[240] Fix | Delete
* @return string Validator to be used to generate the cache key.
[241] Fix | Delete
*/
[242] Fix | Delete
public static function get_validator_key( $type = '' ) {
[243] Fix | Delete
[244] Fix | Delete
if ( empty( $type ) ) {
[245] Fix | Delete
return self::VALIDATION_GLOBAL_KEY;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
return sprintf( self::VALIDATION_TYPE_KEY_FORMAT, $type );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* Refresh the cache validator value.
[253] Fix | Delete
*
[254] Fix | Delete
* @since 3.2
[255] Fix | Delete
*
[256] Fix | Delete
* @param string $type Provide a type for a specific type validator, empty for global validator.
[257] Fix | Delete
*
[258] Fix | Delete
* @return bool True if validator key has been saved as option.
[259] Fix | Delete
*/
[260] Fix | Delete
public static function create_validator( $type = '' ) {
[261] Fix | Delete
[262] Fix | Delete
$key = self::get_validator_key( $type );
[263] Fix | Delete
[264] Fix | Delete
// Generate new validator.
[265] Fix | Delete
$microtime = microtime();
[266] Fix | Delete
[267] Fix | Delete
// Remove space.
[268] Fix | Delete
list( $milliseconds, $seconds ) = explode( ' ', $microtime );
[269] Fix | Delete
[270] Fix | Delete
// Transients are purged every 24h.
[271] Fix | Delete
$seconds = ( $seconds % DAY_IN_SECONDS );
[272] Fix | Delete
$milliseconds = intval( substr( $milliseconds, 2, 3 ), 10 );
[273] Fix | Delete
[274] Fix | Delete
// Combine seconds and milliseconds and convert to integer.
[275] Fix | Delete
$validator = intval( $seconds . '' . $milliseconds, 10 );
[276] Fix | Delete
[277] Fix | Delete
// Apply base 61 encoding.
[278] Fix | Delete
$compressed = self::convert_base10_to_base61( $validator );
[279] Fix | Delete
[280] Fix | Delete
return update_option( $key, $compressed, false );
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Encode to base61 format.
[285] Fix | Delete
*
[286] Fix | Delete
* This is base64 (numeric + alpha + alpha upper case) without the 0.
[287] Fix | Delete
*
[288] Fix | Delete
* @since 3.2
[289] Fix | Delete
*
[290] Fix | Delete
* @param int $base10 The number that has to be converted to base 61.
[291] Fix | Delete
*
[292] Fix | Delete
* @return string Base 61 converted string.
[293] Fix | Delete
*
[294] Fix | Delete
* @throws InvalidArgumentException When the input is not an integer.
[295] Fix | Delete
*/
[296] Fix | Delete
public static function convert_base10_to_base61( $base10 ) {
[297] Fix | Delete
[298] Fix | Delete
if ( ! is_int( $base10 ) ) {
[299] Fix | Delete
throw new InvalidArgumentException( __( 'Expected an integer as input.', 'wordpress-seo' ) );
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
// Characters that will be used in the conversion.
[303] Fix | Delete
$characters = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
[304] Fix | Delete
$length = strlen( $characters );
[305] Fix | Delete
[306] Fix | Delete
$remainder = $base10;
[307] Fix | Delete
$output = '';
[308] Fix | Delete
[309] Fix | Delete
do {
[310] Fix | Delete
// Building from right to left in the result.
[311] Fix | Delete
$index = ( $remainder % $length );
[312] Fix | Delete
[313] Fix | Delete
// Prepend the character to the output.
[314] Fix | Delete
$output = $characters[ $index ] . $output;
[315] Fix | Delete
[316] Fix | Delete
// Determine the remainder after removing the applied number.
[317] Fix | Delete
$remainder = floor( $remainder / $length );
[318] Fix | Delete
[319] Fix | Delete
// Keep doing it until we have no remainder left.
[320] Fix | Delete
} while ( $remainder );
[321] Fix | Delete
[322] Fix | Delete
return $output;
[323] Fix | Delete
}
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function