( is_ssl() ? 'https://' : 'http://' ) . self::site_host() . (string) wp_parse_url( isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/', PHP_URL_PATH ), 'time' => time(), 'mobile' => AIOM_Settings::get( 'page_cache_mobile' ) && self::is_mobile(), 'headers' => $replay, ); return AIOM_Cache_Dir::write( $file . '.meta', wp_json_encode( $meta ) ) && AIOM_Cache_Dir::write( $file, $html ); } /** * Purge every variant of a URL. * * @param string $url URL. * @return int Files deleted. */ public static function purge_url( $url ) { $p = wp_parse_url( $url ); if ( empty( $p['host'] ) ) { return 0; } $host = strtolower( $p['host'] ) . ( isset( $p['port'] ) ? ':' . (int) $p['port'] : '' ); $path = isset( $p['path'] ) && '' !== $p['path'] ? $p['path'] : '/'; $count = 0; foreach ( array( true, false ) as $https ) { foreach ( array( true, false ) as $mobile ) { $file = self::file( self::key( $https, $host, $path, $mobile ) ); if ( is_file( $file ) && @unlink( $file ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink ++$count; } if ( is_file( $file . '.meta' ) ) { @unlink( $file . '.meta' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink } } } return $count; } /** * Purge all cached pages. * * @return int Files deleted. */ public static function purge_all() { $dir = AIOM_Cache_Dir::base() . 'pages'; return is_dir( $dir ) ? AIOM_Cache_Dir::delete_tree( $dir, true ) : 0; } /** * Cached state of a URL (desktop variant). * * @param string $url URL. * @return int|false Cache time, or false. */ public static function cached_at( $url ) { $p = wp_parse_url( $url ); if ( empty( $p['host'] ) ) { return false; } $host = strtolower( $p['host'] ) . ( isset( $p['port'] ) ? ':' . (int) $p['port'] : '' ); $file = self::file( self::key( 'https' === ( isset( $p['scheme'] ) ? $p['scheme'] : '' ), $host, isset( $p['path'] ) && '' !== $p['path'] ? $p['path'] : '/', false ) ); $time = @filemtime( $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return ( $time && $time + (int) AIOM_Settings::get( 'page_cache_ttl' ) * HOUR_IN_SECONDS >= time() ) ? $time : false; } /** * Delete expired pages. * * @return int Files deleted. */ public static function expire() { $dir = AIOM_Cache_Dir::base() . 'pages'; if ( ! is_dir( $dir ) ) { return 0; } $limit = time() - (int) AIOM_Settings::get( 'page_cache_ttl' ) * HOUR_IN_SECONDS; $count = 0; foreach ( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ) ) as $f ) { if ( $f->isFile() && '.html' === substr( $f->getFilename(), -5 ) && $f->getMTime() < $limit ) { @unlink( $f->getPathname() ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink @unlink( $f->getPathname() . '.meta' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink ++$count; } } return $count; } }