$max_length ) { if ( $max_length < 15 ) { /** * If this happens the most likely cause is a page number that is too high. * * So this would not happen unintentionally.. * Either by trying to cause a high server load, finding backdoors or misconfiguration. */ throw new OutOfRangeException( __( '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.', 'wordpress-seo' ) ); } $half = ( $max_length / 2 ); $first_part = substr( $type, 0, ( ceil( $half ) - 1 ) ); $last_part = substr( $type, ( 1 - floor( $half ) ) ); $type = $first_part . '..' . $last_part; } return $type; } /** * Invalidate sitemap cache * * @param null|string $type The type to get the key for. Null for all caches. * * @return void */ public static function invalidate_storage( $type = null ) { // Global validator gets cleared when no type is provided. $old_validator = null; // Get the current type validator. if ( ! is_null( $type ) ) { $old_validator = self::get_validator( $type ); } // Refresh validator. self::create_validator( $type ); if ( ! wp_using_ext_object_cache() ) { // Clean up current cache from the database. self::cleanup_database( $type, $old_validator ); } // External object cache pushes old and unretrieved items out by itself so we don't have to do anything for that. } /** * Cleanup invalidated database cache * * @param null|string $type The type of sitemap to clear cache for. * @param null|string $validator The validator to clear cache of. * * @return void */ public static function cleanup_database( $type = null, $validator = null ) { global $wpdb; if ( is_null( $type ) ) { // Clear all cache if no type is provided. $like = sprintf( '%s%%', self::STORAGE_KEY_PREFIX ); } else { if ( ! is_null( $validator ) ) { // Clear all cache for provided type-validator. $like = sprintf( '%%_%s', $validator ); } else { // Clear type cache for all type keys. $like = sprintf( '%1$s%2$s_%%', self::STORAGE_KEY_PREFIX, $type ); } } /** * Add slashes to the LIKE "_" single character wildcard. * * We can't use `esc_like` here because we need the % in the query. */ $where = array(); $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_' . $like, '_' ) ); $where[] = sprintf( "option_name LIKE '%s'", addcslashes( '_transient_timeout_' . $like, '_' ) ); // Delete transients. $query = sprintf( 'DELETE FROM %1$s WHERE %2$s', $wpdb->options, implode( ' OR ', $where ) ); $wpdb->query( $query ); } /** * Get the current cache validator * * Without the type the global validator is returned. * This can invalidate -all- keys in cache at once * * With the type parameter the validator for that specific * type can be invalidated * * @param string $type Provide a type for a specific type validator, empty for global validator. * * @return null|string The validator for the supplied type. */ public static function get_validator( $type = '' ) { $key = self::get_validator_key( $type ); $current = get_option( $key, null ); if ( ! is_null( $current ) ) { return $current; } if ( self::create_validator( $type ) ) { return self::get_validator( $type ); } return null; } /** * Get the cache validator option key for the specified type * * @param string $type Provide a type for a specific type validator, empty for global validator. * * @return string Validator to be used to generate the cache key. */ public static function get_validator_key( $type = '' ) { if ( empty( $type ) ) { return self::VALIDATION_GLOBAL_KEY; } return sprintf( self::VALIDATION_TYPE_KEY_FORMAT, $type ); } /** * Refresh the cache validator value * * @param string $type Provide a type for a specific type validator, empty for global validator. * * @return bool True if validator key has been saved as option. */ public static function create_validator( $type = '' ) { $key = self::get_validator_key( $type ); // Generate new validator. $microtime = microtime(); // Remove space. list( $milliseconds, $seconds ) = explode( ' ', $microtime ); // Transients are purged every 24h. $seconds = ( $seconds % DAY_IN_SECONDS ); $milliseconds = intval( substr( $milliseconds, 2, 3 ), 10 ); // Combine seconds and milliseconds and convert to integer. $validator = intval( $seconds . '' . $milliseconds, 10 ); // Apply base 61 encoding. $compressed = self::convert_base10_to_base61( $validator ); return update_option( $key, $compressed, false ); } /** * Encode to base61 format. * * This is base64 (numeric + alpha + alpha upper case) without the 0. * * @param int $base10 The number that has to be converted to base 61. * * @return string Base 61 converted string. * * @throws InvalidArgumentException When the input is not an integer. */ public static function convert_base10_to_base61( $base10 ) { if ( ! is_int( $base10 ) ) { throw new InvalidArgumentException( __( 'Expected an integer as input.', 'wordpress-seo' ) ); } // Characters that will be used in the conversion. $characters = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $length = strlen( $characters ); $remainder = $base10; $output = ''; do { // Building from right to left in the result. $index = ( $remainder % $length ); // Prepend the character to the output. $output = $characters[ $index ] . $output; // Determine the remainder after removing the applied number. $remainder = floor( $remainder / $length ); // Keep doing it until we have no remainder left. } while ( $remainder ); return $output; } }