=' ); } /** * Row key of a bucket and key. * * @param string $bucket Bucket. * @param string $key Key. * @return string 32 hex characters. */ private static function row_key( $bucket, $key ) { return md5( sanitize_key( $bucket ) . '|' . (string) $key ); } /** * Read a value. * * @param string $bucket Bucket (md, mdbody, render…). * @param string $key Key. * @return mixed|null Null on a miss. */ public static function get( $bucket, $key ) { if ( self::object_cache() ) { $found = false; $value = wp_cache_get( $bucket . ':' . $key, self::GROUP, false, $found ); return $found ? $value : null; } if ( ! self::table_ready() ) { return null; } global $wpdb; $quiet = $wpdb->suppress_errors( true ); // Never print a DB error into a crawler's response. $row = $wpdb->get_row( $wpdb->prepare( 'SELECT value, expires FROM %i WHERE cache_key = %s', self::table(), self::row_key( $bucket, $key ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own cache table. $wpdb->suppress_errors( $quiet ); if ( ! is_array( $row ) ) { return null; } if ( (int) $row['expires'] < time() ) { self::delete( $bucket, $key ); return null; } $data = json_decode( (string) $row['value'], true ); return is_array( $data ) && array_key_exists( 'v', $data ) ? $data['v'] : null; } /** * Store a value (strings and arrays of scalars). * * @param string $bucket Bucket. * @param string $key Key. * @param mixed $value Value. * @param int $ttl Seconds. */ public static function set( $bucket, $key, $value, $ttl ) { $ttl = max( 60, (int) $ttl ); if ( self::object_cache() ) { wp_cache_set( $bucket . ':' . $key, $value, self::GROUP, $ttl ); return; } if ( ! self::table_ready() ) { return; } $json = wp_json_encode( array( 'v' => $value ) ); if ( false === $json ) { return; } global $wpdb; $quiet = $wpdb->suppress_errors( true ); $wpdb->replace( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own cache table. self::table(), array( 'cache_key' => self::row_key( $bucket, $key ), 'bucket' => substr( sanitize_key( $bucket ), 0, 20 ), 'value' => $json, 'expires' => time() + $ttl, ), array( '%s', '%s', '%s', '%d' ) ); $wpdb->suppress_errors( $quiet ); } /** * Delete a value. * * @param string $bucket Bucket. * @param string $key Key. */ public static function delete( $bucket, $key ) { if ( self::object_cache() ) { wp_cache_delete( $bucket . ':' . $key, self::GROUP ); return; } if ( ! self::table_ready() ) { return; } global $wpdb; $quiet = $wpdb->suppress_errors( true ); $wpdb->delete( self::table(), array( 'cache_key' => self::row_key( $bucket, $key ) ), array( '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own cache table. $wpdb->suppress_errors( $quiet ); } /** * Delete expired rows (daily), for at most $budget seconds. * * @param int $budget Seconds. * @return int Rows deleted. */ public static function cleanup( $budget = 15 ) { if ( self::object_cache() || ! self::table_ready() ) { return 0; } global $wpdb; $start = microtime( true ); $deleted = 0; $quiet = $wpdb->suppress_errors( true ); do { $n = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE expires < %d LIMIT 5000', self::table(), time() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own cache table. $deleted += $n; } while ( $n >= 5000 && microtime( true ) - $start < $budget ); $wpdb->suppress_errors( $quiet ); return $deleted; } /** * Empty the cache (after a purge of all data). */ public static function flush() { if ( self::object_cache() ) { if ( function_exists( 'wp_cache_flush_group' ) && wp_cache_supports( 'flush_group' ) ) { wp_cache_flush_group( self::GROUP ); } return; } if ( ! self::table_ready() ) { return; } global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', self::table() ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own cache table. } /** * Remove the file cache of aeoora 1.0 and 1.1 (uploads/aeoora/cache/), * once, on upgrade. */ public static function remove_file_cache() { $uploads = wp_upload_dir( null, false ); if ( ! empty( $uploads['error'] ) ) { return; } $dir = trailingslashit( $uploads['basedir'] ) . 'aeoora/cache'; if ( ! is_dir( $dir ) ) { return; } global $wp_filesystem; require_once ABSPATH . 'wp-admin/includes/file.php'; if ( WP_Filesystem() && $wp_filesystem ) { $wp_filesystem->rmdir( $dir, true ); } } }