array( 'WP Content Copy Protection Pro', 'Dont use cookies' ), ); /** * Explains one address. * * @param string $url Address on this site. * @param bool $visit When it is not cached and nothing says why, visit it once as warming does, then answer. * @return array{cached:bool, code:string, sentence:string} */ public static function url( $url, $visit = false ) { $found = self::locate( $url ); if ( isset( $found['answer'] ) ) { return $found['answer']; } $answer = self::on_disk( $found ); if ( null !== $answer ) { return $answer; } if ( empty( $found['decision']['store'] ) ) { return self::answer( false, 'tracking', self::reason_sentence( 'tracking' ) ); } if ( $visit && class_exists( 'GorillaCache_Preload' ) ) { GorillaCache_Preload::visit( (string) $url ); clearstatcache(); $answer = self::on_disk( $found ); if ( null !== $answer ) { return $answer; } } return self::answer( false, 'not_yet', __( 'Not cached yet: it will be stored on the next visit, or when the cache is warmed.', 'gorilla-cache' ) ); } /** * What became of an address after a visit, for warming: stored, or the reason it was not (empty when unknown). * * @param string $url Address on this site. * @return array{stored:bool, reason:string, detail:string} */ public static function outcome( $url ) { $found = self::locate( $url ); if ( isset( $found['answer'] ) ) { return array( 'stored' => false, 'reason' => $found['answer']['code'], 'detail' => '', ); } $copy = self::fresh_copy( $found ); $note = $copy ? null : GorillaCache_Engine_Store::read_note( $found['file'], (int) $found['config']['lifespan'], time() ); return array( 'stored' => $copy, 'reason' => null === $note ? '' : $note['reason'], 'detail' => null === $note ? '' : $note['detail'], ); } /** * The sentence for a page the engine refused to store, from its note. * * @param string $reason Reason from GorillaCache_Engine_Rules::storable(). * @param string $detail Its detail (a cookie name, a status and target, a header value). * @return string */ public static function refusal_sentence( $reason, $detail ) { $detail = (string) $detail; switch ( $reason ) { case 'set_cookie': /* translators: %s: cookie name(s). */ $sentence = sprintf( __( 'Not cached: the page sets the cookie %s, and a stored copy would hand it to every visitor.', 'gorilla-cache' ), $detail ); $name = trim( explode( ',', $detail )[0] ); if ( isset( self::KNOWN_COOKIES[ $name ] ) ) { /* translators: 1: plugin name, 2: the name of that plugin's setting. */ $sentence .= ' ' . sprintf( __( 'It comes from %1$s: turn on its setting "%2$s".', 'gorilla-cache' ), self::KNOWN_COOKIES[ $name ][0], self::KNOWN_COOKIES[ $name ][1] ); } return $sentence; case 'status': $parts = explode( ' ', $detail, 2 ); if ( isset( $parts[1] ) && (int) $parts[0] >= 300 && (int) $parts[0] < 400 ) { /* translators: 1: HTTP status code, 2: the address it redirects to. */ return sprintf( __( 'Not cached: this address redirects (%1$s) to %2$s, and redirects are not stored.', 'gorilla-cache' ), $parts[0], $parts[1] ); } /* translators: %s: HTTP status code. */ return sprintf( __( 'Not cached: the page answers with status %s, and only pages that answer 200 are stored.', 'gorilla-cache' ), $parts[0] ); case 'donotcachepage': if ( 'store_page' === $detail ) { return __( 'Not cached: this is a store, checkout, account or member page, found from your plugins\' own settings.', 'gorilla-cache' ); } if ( 'recently_viewed' === $detail ) { return __( 'Not cached: the page shows each visitor\'s own recently viewed products.', 'gorilla-cache' ); } if ( in_array( $detail, array( 'search', 'comment_page' ), true ) ) { return self::reason_sentence( $detail ); } return __( 'Not cached: a plugin or the theme asks for this page not to be cached (DONOTCACHEPAGE).', 'gorilla-cache' ); case 'no_cache_header': /* translators: %s: the Cache-Control header's value. */ return sprintf( __( 'Not cached: the page sends a header that forbids caching (Cache-Control: %s).', 'gorilla-cache' ), $detail ); case 'short_max_age': /* translators: %s: the Cache-Control header's value. */ return sprintf( __( 'Not cached: the page asks to be kept for less time than Gorilla Cache keeps pages (Cache-Control: %s).', 'gorilla-cache' ), $detail ); case 'not_html': /* translators: %s: the Content-Type header's value. */ return sprintf( __( 'Not cached: the answer is not a web page (Content-Type: %s).', 'gorilla-cache' ), $detail ); case 'incomplete': return __( 'Not cached: the page was not complete (it has no closing tag).', 'gorilla-cache' ); case 'marker': return __( 'Not cached: the page carries a visitor\'s own session, so one copy cannot serve everyone.', 'gorilla-cache' ); case 'php_error': return __( 'Not cached: the page shows a PHP warning or error, which must not be shown to every visitor.', 'gorilla-cache' ); case 'outer_buffer': $owner = self::buffer_owner( $detail ); if ( '' !== $owner ) { /* translators: 1: plugin or theme name, 2: the name of its output buffer's handler. */ return sprintf( __( 'Not cached: %1$s changes the page after WordPress has finished it (its output buffer %2$s), so Gorilla Cache never sees the page visitors get.', 'gorilla-cache' ), $owner, $detail ); } /* translators: %s: the name of the output buffer's handler. */ return sprintf( __( 'Not cached: another plugin or the theme changes the page after WordPress has finished it (output buffer %s), so Gorilla Cache never sees the page visitors get.', 'gorilla-cache' ), $detail ); case 'no_template': return __( 'Not cached: the page was not built from a theme template (a plugin answered it early), so WordPress did not hand it over to be stored.', 'gorilla-cache' ); } return self::reason_sentence( $reason ); // A reason the rules decide before any visit (an excluded path, say). } /** * The plugin or theme behind an output buffer's handler, or '' when that cannot be told. A closure is named by * its file from the plugins or themes folder on ("{closure:plugins/name/file.php:12}", PHP 8.4 and later); a * class method by the folder its class was loaded from; a closure before PHP 8.4 or a plain function by nothing. * * @param string $handler Handler name, as the engine noted it. * @return string */ public static function buffer_owner( $handler ) { $handler = (string) $handler; if ( preg_match( '#^\{closure:(plugins|themes)/([^/]+)/#', $handler, $m ) ) { return 'plugins' === $m[1] ? self::plugin_name( $m[2] ) : self::theme_name( $m[2] ); } $class = (string) strtok( $handler, ':' ); if ( '' === $class || 0 === strpos( $class, '{' ) || 'Closure' === $class || ! class_exists( $class ) ) { return ''; } $file = ( new ReflectionClass( $class ) )->getFileName(); if ( ! is_string( $file ) ) { return ''; } $file = wp_normalize_path( $file ); $theme = wp_normalize_path( (string) get_theme_root() ) . '/'; if ( 0 === strpos( $file, $theme ) ) { return self::theme_name( (string) strtok( substr( $file, strlen( $theme ) ), '/' ) ); } $folder = (string) strtok( plugin_basename( $file ), '/' ); return '' !== $folder && $folder !== $file ? self::plugin_name( $folder ) : ''; } /** * An installed plugin's name from its folder, or the folder itself. * * @param string $folder Plugin folder. * @return string */ private static function plugin_name( $folder ) { if ( function_exists( 'get_plugins' ) ) { foreach ( get_plugins() as $basename => $data ) { if ( 0 === strpos( $basename, $folder . '/' ) && ! empty( $data['Name'] ) ) { return (string) $data['Name']; } } } return $folder; } /** * An installed theme's name from its folder, or the folder itself. * * @param string $folder Theme folder. * @return string */ private static function theme_name( $folder ) { $theme = wp_get_theme( $folder ); return $theme->exists() ? (string) $theme->get( 'Name' ) : $folder; } /** * Finds where an address's copy would be, or the answer when the rules already decide. * * @param string $url Address on this site. * @return array{answer?:array, config?:array, decision?:array, file?:string} */ private static function locate( $url ) { $state = GorillaCache_Installer::state(); if ( 'on' !== $state['verdict'] ) { return array( 'answer' => self::answer( false, 'off', GorillaCache_Environment::message( $state ) ) ); } $parts = wp_parse_url( (string) $url ); $home = wp_parse_url( home_url() ); if ( empty( $parts['host'] ) || strtolower( $parts['host'] ) !== strtolower( (string) $home['host'] ) ) { return array( 'answer' => self::answer( false, 'other_site', __( 'That address is not on this site.', 'gorilla-cache' ) ) ); } $host = strtolower( $parts['host'] ) . ( isset( $parts['port'] ) ? ':' . (int) $parts['port'] : '' ); $config = self::config( $host ); if ( ! is_array( $config ) ) { return array( 'answer' => self::answer( false, 'no_config', __( 'Not cached: the cache has no configuration for this address yet. Click "Check again".', 'gorilla-cache' ) ) ); } $request = GorillaCache_Engine_Request::from_arrays( array( 'REQUEST_METHOD' => 'GET', 'HTTP_HOST' => $host, 'REQUEST_URI' => ( isset( $parts['path'] ) ? $parts['path'] : '/' ) . ( isset( $parts['query'] ) ? '?' . $parts['query'] : '' ), 'HTTPS' => isset( $parts['scheme'] ) && 'https' === $parts['scheme'] ? 'on' : '', 'HTTP_ACCEPT' => 'text/html', ), array() ); $decision = GorillaCache_Engine_Rules::explain( $request, $config ); if ( ! $decision['cache'] ) { return array( 'answer' => self::answer( false, $decision['reason'], self::reason_sentence( $decision['reason'] ) ) ); } global $wp_rewrite; $search = '/' . trim( (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ), '/' ) . '/' . ( $wp_rewrite instanceof WP_Rewrite ? $wp_rewrite->search_base : 'search' ) . '/'; if ( 0 === strpos( str_replace( '//', '/', isset( $parts['path'] ) ? $parts['path'] . '/' : '/' ), str_replace( '//', '/', $search ) ) ) { return array( 'answer' => self::answer( false, 'search', self::reason_sentence( 'search' ) ) ); } $post_id = url_to_postid( $url ); if ( $post_id && ( in_array( $post_id, GorillaCache_Registry::page_ids(), true ) || in_array( get_post_type( $post_id ), GorillaCache_Registry::post_types(), true ) ) ) { return array( 'answer' => self::answer( false, 'store_page', __( 'Not cached: this is a store, checkout, account or member page, found from your plugins\' own settings.', 'gorilla-cache' ) ) ); } return array( 'config' => $config, 'decision' => $decision, 'file' => rtrim( (string) $config['cache_dir'], '/' ) . '/' . $decision['key'], ); } /** * The answer from what is on disk: a fresh copy, or the note of why there is none; null when neither. * * @param array $found Result of locate(). * @return array{cached:bool, code:string, sentence:string}|null */ private static function on_disk( array $found ) { $file = $found['file']; if ( self::fresh_copy( $found ) ) { return self::answer( true, 'cached', sprintf( /* translators: 1: how long ago, 2: file size. */ __( 'Cached: stored %1$s ago (%2$s).', 'gorilla-cache' ), human_time_diff( (int) filemtime( $file ) ), size_format( (int) filesize( $file ) ) ) ); } $note = GorillaCache_Engine_Store::read_note( $file, (int) $found['config']['lifespan'], time() ); if ( null !== $note ) { return self::answer( false, 'refused_' . $note['reason'], self::refusal_sentence( $note['reason'], $note['detail'] ) ); } return null; } /** * Whether a fresh copy is on disk. * * @param array $found Result of locate(). * @return bool */ private static function fresh_copy( array $found ) { return file_exists( $found['file'] ) && filemtime( $found['file'] ) + (int) $found['config']['lifespan'] > time(); } /** * The engine's configuration for a host, read once per request. * * @param string $host Host, with its port when not the default. * @return array|null */ private static function config( $host ) { static $configs = array(); if ( ! array_key_exists( $host, $configs ) ) { $config = gorilla_cache_engine_parse_config( self::read( GorillaCache_Installer::config_dir() . '/' . str_replace( ':', '_', $host ) . '.php' ) ); $configs[ $host ] = is_array( $config ) ? $config : null; } return $configs[ $host ]; } /** * The sentence for an engine reason. * * @param string $reason Reason code from GorillaCache_Engine_Rules::explain(). * @return string */ public static function reason_sentence( $reason ) { $sentences = array( 'disabled' => __( 'Not cached: caching is off.', 'gorilla-cache' ), 'store_action' => __( 'Not cached: the address changes a cart or an order (for example add-to-cart).', 'gorilla-cache' ), 'unknown_host' => __( 'Not cached: the address uses a host name this site does not answer to.', 'gorilla-cache' ), 'system_path' => __( 'Not cached: this is part of WordPress itself (admin, REST API, feeds or files), never a page.', 'gorilla-cache' ), 'not_page' => __( 'Not cached: this address is a file, not a page.', 'gorilla-cache' ), 'unsafe_path' => __( 'Not cached: the address cannot be stored safely.', 'gorilla-cache' ), 'query' => __( 'Not cached: the link has a parameter that is not on the cached list (Advanced tab).', 'gorilla-cache' ), 'tracking' => __( 'Answered from the cached copy of the plain address when there is one; a page built for a tracking link is never stored, because its links could carry the tracking values to every visitor.', 'gorilla-cache' ), 'not_html' => __( 'Not cached: the request does not ask for a web page.', 'gorilla-cache' ), 'cookie' => __( 'Not cached for this visitor: a login, cart or similar cookie means personal content.', 'gorilla-cache' ), 'excluded' => __( 'Not cached: this path is on your "Never cache these pages" list.', 'gorilla-cache' ), 'path_case' => __( 'Not cached: the address has capital letters. WordPress\'s own addresses are lower case, so each other spelling would be one more copy.', 'gorilla-cache' ), 'comment_page' => __( 'Not cached: the post has no comment page with that number.', 'gorilla-cache' ), 'search' => __( 'Not cached: search results are never stored, since every search is a new address.', 'gorilla-cache' ), 'settings_changed' => __( 'Not cached this time: the site\'s setup changed (a plugin was activated or updated), so the next visit stores it under the new rules.', 'gorilla-cache' ), ); return isset( $sentences[ $reason ] ) ? $sentences[ $reason ] : __( 'Not cached.', 'gorilla-cache' ); } /** * Reads a file, or false. * * @param string $file File. * @return string|false */ private static function read( $file ) { return is_readable( $file ) ? GorillaCache_Installer::filesystem()->get_contents( $file ) : false; } /** * Builds an answer. * * @param bool $cached Whether a fresh copy is on disk. * @param string $code Short code. * @param string $sentence Sentence for the owner. * @return array{cached:bool, code:string, sentence:string} */ private static function answer( $cached, $code, $sentence ) { return array( 'cached' => $cached, 'code' => $code, 'sentence' => $sentence, ); } }