\esc_html(
sprintf(
/* translators: 1: tag kind (mfunc/mclude), 2: reason. */
'W3TC dynamic %1$s tag refused: %2$s. Raw PHP / file-path payloads are no longer dispatched; migrate to the registered-callback form () via the `w3tc_dynamic_callbacks` filter.',
$kind,
$reason
)
),
'2.10.0'
);
} elseif ( \function_exists( 'error_log' ) ) {
\error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
sprintf( '[W3TC] dynamic %s tag refused: %s', $kind, $reason )
);
}
}
/**
* Dispatches a verified, registered callback by slug.
*
* @since 2.10.0
*
* @param string $kind 'mfunc' or 'mclude'.
* @param string $slug Callback slug.
* @param array $args Decoded JSON args.
*
* @return string The callback's HTML output, or an empty string when the tag cannot be dispatched.
*/
private function _dispatch_dynamic_callback( $kind, $slug, $args ) {
$callbacks = $this->_get_dynamic_callbacks();
if ( empty( $callbacks ) ) {
if ( $this->_dynamic_callbacks_may_be_registered() ) {
$this->_log_dynamic_deprecation( $kind, sprintf( 'no callbacks registered (slug "%s")', $slug ) );
}
return '';
}
if ( ! isset( $callbacks[ $slug ] ) || ! \is_callable( $callbacks[ $slug ] ) ) {
$this->_log_dynamic_deprecation( $kind, sprintf( 'unregistered slug "%s"', $slug ) );
return '';
}
try {
$w3tc_output = \call_user_func( $callbacks[ $slug ], $args, $kind );
} catch ( \Throwable $ex ) {
// Log the exception class only (not its message) so the operator notice stays actionable without leaking detail.
$this->_log_dynamic_deprecation( $kind, sprintf( 'slug "%s" raised %s', $slug, \get_class( $ex ) ) );
return '';
}
if ( ! \is_string( $w3tc_output ) ) {
/**
* A callback that returns null/false/an array silently coerces to
* an empty (or garbled) string and hides the bug from site owners
* — the fragment just disappears. Surface it through the same
* log channel as the deprecation path so the breakage is visible
* (and rate-limited identically) before we coerce.
*/
$this->_log_dynamic_deprecation(
$kind,
sprintf( 'callback "%s" returned non-string %s', $slug, \gettype( $w3tc_output ) )
);
/**
* `(string)` on an array or a non-Stringable object raises a PHP
* warning and produces the literal `"Array"` / "Object of class …
* could not be converted" sentinel. Both are useless in cached
* HTML and noisy in the error log. Coerce scalars (which have
* useful string forms — `null` → `''`, `false` → `''`, numbers
* → the digits) but emit an empty string for the rest.
*/
if ( \is_scalar( $w3tc_output ) || ( \is_object( $w3tc_output ) && \method_exists( $w3tc_output, '__toString' ) ) ) {
return (string) $w3tc_output;
}
return '';
}
return $w3tc_output;
}
/**
* Common dispatcher for mfunc / mclude tags.
*
* Enforces the three security layers:
* 1. Refuses unless `W3TC_DYNAMIC_SECURITY` is defined (already gated
* upstream by `_parse_dynamic()`).
* 2. Refuses unless the payload is in the `call:` form with a
* valid HMAC envelope.
* 3. Refuses unless the slug is in the registered-callback registry.
*
* @since 2.10.0
*
* @param string $kind 'mfunc' or 'mclude'.
* @param array $matches preg_replace_callback matches: [0]=full, [1]=header, [2]=body.
*
* @return string Callback output, or an empty string when the tag is refused.
*/
private function _dispatch_dynamic( $kind, $matches ) {
$header = isset( $matches[1] ) ? trim( $matches[1] ) : '';
$body = isset( $matches[2] ) ? trim( $matches[2] ) : '';
/**
* Prefer the header for the call descriptor; fall back to the body for
* the alternate "call:slug ... hmac:hex" form.
*/
$candidate = '' !== $header ? $header : $body;
$parsed = $this->_parse_dynamic_header( $candidate );
if ( null === $parsed ) {
// Render nothing to visitors; the operator log above records the tag to migrate.
$this->_log_dynamic_deprecation( $kind, 'raw payload (no call:slug + hmac envelope)' );
return '';
}
$expected = $this->_dynamic_hmac( $kind, $parsed['slug'], $parsed['args_json'] );
if ( ! \hash_equals( $expected, $parsed['hmac'] ) ) {
$this->_log_dynamic_deprecation( $kind, 'HMAC mismatch' );
return '';
}
return $this->_dispatch_dynamic_callback( $kind, $parsed['slug'], $this->_decode_dynamic_args( $parsed['args_json'] ) );
}
/**
* Dispatches a dynamic mfunc tag.
*
* Previously this method `eval()`'d arbitrary PHP from the tag payload.
* That primitive has been removed; only registered callbacks dispatched
* via `call:` + HMAC are honored.
*
* @since 2.10.0
*
* @param array $matches The matches from the regular expression.
*
* @return string The callback's output, or an empty string when refused.
*/
public function _parse_dynamic_mfunc( $matches ) {
return $this->_dispatch_dynamic( 'mfunc', $matches );
}
/**
* Dispatches a dynamic mclude tag.
*
* Previously this method `include`'d a relative file path supplied
* inside the tag. That primitive has been removed; only registered
* callbacks dispatched via `call:` + HMAC are honored. Plugins
* that need to render an include's output should wrap it in a slug.
*
* @since 2.10.0
*
* @param array $matches The matches from the regular expression.
*
* @return string The callback's output, or an empty string when refused.
*/
public function _parse_dynamic_mclude( $matches ) {
return $this->_dispatch_dynamic( 'mclude', $matches );
}
/**
* Signs every dynamic mfunc / mclude tag in $buffer by appending an
* `hmac:` envelope to the `call:` descriptor. Called once
* before the buffer is written to the page cache so that later
* read-time dispatch can verify integrity.
*
* Both inline-header form (`...`)
* and between-tags / body form (`call:slug ...`)
* are signed in place, because dispatch accepts both shapes.
*
* Tags whose payload is not in the safe `call:` form are left
* unsigned — at dispatch time they fall through to the deprecation path,
* render as empty output, and emit a rate-limited operator notice naming
* the tag to migrate.
*
* @since 2.10.0
*
* @param string $buffer Buffer containing rendered HTML.
*
* @return string Buffer with HMAC envelopes added.
*/
public function _sign_dynamic_tags( $buffer ) {
if ( ! defined( 'W3TC_DYNAMIC_SECURITY' ) || empty( W3TC_DYNAMIC_SECURITY ) || 1 === (int) W3TC_DYNAMIC_SECURITY ) {
return $buffer;
}
$security = preg_quote( W3TC_DYNAMIC_SECURITY, '~' );
$sign_one = function ( $kind ) {
return function ( $matches ) use ( $kind ) {
$header = isset( $matches[1] ) ? trim( $matches[1] ) : '';
$body_raw = isset( $matches[2] ) ? $matches[2] : '';
$body = trim( $body_raw );
// If the header already has a complete `call:slug ... hmac:hex` envelope, leave it alone.
if ( null !== $this->_parse_dynamic_header( $header ) ) {
return $matches[0];
}
// Likewise, leave a fully-signed body-form alone.
if ( '' === $header && null !== $this->_parse_dynamic_header( $body ) ) {
return $matches[0];
}
/**
* Determine where the `call:` descriptor lives: in the header
* (inline form) or in the body (between-tags form). Both forms are
* honored at dispatch time, so both must be signed at write time.
*/
if ( preg_match( '~^call:([A-Za-z0-9_\-\.:]+)\s*(.*)$~is', $header, $m ) ) {
$source = 'header';
} elseif ( '' === $header && preg_match( '~^call:([A-Za-z0-9_\-\.:]+)\s*(.*)$~is', $body, $m ) ) {
$source = 'body';
} else {
// Raw-PHP / file-path payload — leave unsigned; dispatch will refuse.
return $matches[0];
}
$slug = $m[1];
$args_json = trim( $m[2] );
$hmac = $this->_dynamic_hmac( $kind, $slug, $args_json );
$w3tc_descriptor = 'call:' . $slug;
if ( '' !== $args_json ) {
$w3tc_descriptor .= ' ' . $args_json;
}
$w3tc_descriptor .= ' hmac:' . $hmac;
if ( 'header' === $source ) {
return '' . $body_raw . '';
}
return '' . $w3tc_descriptor . '';
};
};
/**
* `\s*` (NOT `\s+`) between the security token and the closing
* `-->` so we match the same set of tags as `_parse_dynamic` /
* `_has_dynamic` (both use `(.*)-->`, accepting zero whitespace).
* A tag like `call:slug...`
* would otherwise be detected as dynamic at parse time but never
* signed at write time — dispatch would then refuse it for a
* missing HMAC envelope.
*/
$buffer = preg_replace_callback(
'~(.*?)~is',
$sign_one( 'mfunc' ),
$buffer
);
$buffer = preg_replace_callback(
'~(.*?)~is',
$sign_one( 'mclude' ),
$buffer
);
return $buffer;
}
/**
* Checks if the buffer contains dynamic tags.
*
* @param string $buffer The content buffer to check.
*
* @return bool True if dynamic tags are present, false otherwise.
*/
public function _has_dynamic( $buffer ) {
if ( ! defined( 'W3TC_DYNAMIC_SECURITY' ) || empty( W3TC_DYNAMIC_SECURITY ) || 1 === (int) W3TC_DYNAMIC_SECURITY ) {
return false;
}
$security = preg_quote( W3TC_DYNAMIC_SECURITY, '~' );
return preg_match(
'~(.*)~Uis',
$buffer
);
}
/**
* Determines if the content type is cacheable.
*
* @return bool True if the content type is cacheable, false otherwise.
*/
private function _is_cacheable_content_type() {
$content_type = '';
$headers = headers_list();
foreach ( $headers as $header ) {
$header = strtolower( $header );
$m = null;
if ( preg_match( '~\s*content-type\s*:([^;]+)~', $header, $m ) ) {
$content_type = trim( $m[1] );
}
}
$cache_headers = apply_filters(
'w3tc_is_cacheable_content_type',
array(
'', // redirects, they have only Location header set.
'application/json',
'text/html',
'text/xml',
'text/xsl',
'application/xhtml+xml',
'application/rss+xml',
'application/atom+xml',
'application/rdf+xml',
'application/xml',
)
);
return in_array( $content_type, $cache_headers, true );
}
/**
* Preprocesses the request URI into its components.
*
* @return void
*/
private function _preprocess_request_uri() {
$w3tc_p = explode( '?', $this->_request_uri, 2 );
$this->_request_url_fragments['path'] = $w3tc_p[0];
$this->_request_url_fragments['querystring'] = ( empty( $w3tc_p[1] ) ? '' : '?' . $w3tc_p[1] );
$this->_request_url_fragments = $this->_normalize_url_fragments( $this->_request_url_fragments );
}
/**
* Normalizes URL fragments.
*
* @param array $fragments {
* The URL fragments to normalize.
*
* @type string $querystring The query string to normalize.
* }
*
* @return array The normalized URL fragments.
*/
private function _normalize_url_fragments( $fragments ) {
$fragments = w3tc_apply_filters( 'pagecache_normalize_url_fragments', $fragments );
$fragments['querystring'] = $this->_normalize_querystring( $fragments['querystring'] );
return $fragments;
}
/**
* Normalizes the query string in a URL.
*
* @param string $querystring The query string to normalize.
*
* @return string The normalized query string.
*/
private function _normalize_querystring( $querystring ) {
$ignore_qs = $this->_config->get_array( 'pgcache.accept.qs' );
$ignore_qs = w3tc_apply_filters( 'pagecache_extract_accept_qs', $ignore_qs );
Util_Rule::array_trim( $ignore_qs );
if ( empty( $ignore_qs ) || empty( $querystring ) ) {
return $querystring;
}
$querystring_naked = substr( $querystring, 1 );
foreach ( $ignore_qs as $qs ) {
$m = null;
if ( strpos( $qs, '=' ) === false ) {
$regexp = Util_Environment::preg_quote( str_replace( '+', ' ', $qs ) );
if ( @preg_match( "~^(.*?&|)$regexp(=[^&]*)?(&.*|)$~i", $querystring_naked, $m ) ) {
$querystring_naked = $m[1] . $m[3];
}
} else {
$regexp = Util_Environment::preg_quote( str_replace( '+', ' ', $qs ) );
if ( @preg_match( "~^(.*?&|)$regexp(&.*|)$~i", $querystring_naked, $m ) ) {
$querystring_naked = $m[1] . $m[2];
}
}
}
$querystring_naked = preg_replace( '~[&]+~', '&', $querystring_naked );
$querystring_naked = trim( $querystring_naked, '&' );
return empty( $querystring_naked ) ? '' : '?' . $querystring_naked;
}
/**
* Handles delayed cache printing when applicable.
*
* @return void
*/
public function delayed_cache_print() {
if ( $this->_late_caching && $this->_caching ) {
$this->_cached_data = $this->_extract_cached_page( true );
if ( $this->_cached_data ) {
global $w3tc_w3_late_caching_succeeded;
$w3tc_w3_late_caching_succeeded = true;
$this->process_status = 'hit';
$this->process_cached_page_and_exit( $this->_cached_data );
// if is passes here - exit is not possible now and will happen on init.
return;
}
}
if ( $this->_late_init && $this->_caching ) {
$this->process_status = 'hit';
$this->process_cached_page_and_exit( $this->_cached_data );
// if is passes here - exit is not possible now and will happen on init.
return;
}
}
/**
* Conditionally saves the cached result with optional compression.
*
* @param string $buffer The content buffer to cache.
* @param array $response_headers The response headers to include in the cache.
* @param bool $has_dynamic Whether the content includes dynamic tags.
*
* @return string The buffer, possibly modified for compression.
*/
private function _maybe_save_cached_result( $buffer, $response_headers, $has_dynamic ) {
$mobile_group = $this->_page_key_extension['useragent'];
$referrer_group = $this->_page_key_extension['referrer'];
$encryption = $this->_page_key_extension['encryption'];
$compression_header = $this->_page_key_extension['compression'];
$compressions_to_store = $this->_get_compressions();
// Don't compress here for debug mode or dynamic tags because we need to modify buffer before send it to client.
if ( $this->_debug || $has_dynamic ) {
$compressions_to_store = array( false );
}
// Right now dont return compressed buffer if we are dynamic that will happen on shutdown after processing dynamic stuff.
$compression_of_returned_content = ( $has_dynamic ? false : $compression_header );
$headers = $this->_get_cached_headers( $response_headers['plain'] );
if ( ! empty( $headers['Status-Code'] ) ) {
$is_404 = ( '404' === $headers['Status-Code'] );
} elseif ( function_exists( 'is_404' ) ) {
$is_404 = is_404();
} else {
$is_404 = false;
}
if ( $this->_enhanced_mode ) {
// Redirect issued, if we have some old cache entries they will be turned into fresh files and catch further requests.
if ( isset( $response_headers['kv']['location'] ) ) {
$cache = $this->_get_cache( $this->_page_key_extension['group'] );
foreach ( $compressions_to_store as $_compression ) {
$_page_key = $this->_get_page_key(
array_merge(
$this->_page_key_extension,
array(
'compression' => $_compression,
)
)
);
$cache->hard_delete( $_page_key, $this->_page_group );
}
return $buffer;
}
}
$content_type = '';
if ( $this->_enhanced_mode && ! $this->_late_init ) {
register_shutdown_function(
array(
$this,
'_check_rules_present',
)
);
if ( isset( $response_headers['kv']['content-type'] ) ) {
$content_type = $response_headers['kv']['content-type'];
}
}
$time = time();
$cache = $this->_get_cache( $this->_page_key_extension['group'] );
// Store different versions of cache.
$buffers = array();
$something_was_set = false;
do_action(
'w3tc_pagecache_before_set',
array(
'request_url_fragments' => $this->_request_url_fragments,
'page_key_extension' => $this->_page_key_extension,
)
);
foreach ( $compressions_to_store as $_compression ) {
$this->_set_extract_page_key(
array_merge(
$this->_page_key_extension,
array(
'compression' => $_compression,
'content_type' => $content_type,
)
),
true
);
if ( empty( $this->_page_key ) ) {
continue;
}
// Compress content.
$buffers[ $_compression ] = $this->_compress( $buffer, $_compression );
// Store cache data.
$_data = array(
'404' => $is_404,
'headers' => $headers,
'time' => $time,
'content' => $buffers[ $_compression ],
);
if ( ! empty( $_compression ) ) {
$_data['c'] = $_compression;
}
if ( $has_dynamic ) {
$_data['has_dynamic'] = true;
}
$_data = apply_filters( 'w3tc_pagecache_set', $_data, $this->_page_key, $this->_page_group );
if ( ! empty( $_data ) ) {
$cache->set( $this->_page_key, $_data, $this->_lifetime, $this->_page_group );
$something_was_set = true;
}
}
if ( $something_was_set ) {
$this->process_status = 'miss_fill';
} else {
$this->process_status = 'miss_third_party';
}
// Change buffer if using compression.
if ( defined( 'W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF' ) ) {
$compression_header = false;
} elseif ( $compression_of_returned_content &&
isset( $buffers[ $compression_of_returned_content ] ) ) {
$buffer = $buffers[ $compression_of_returned_content ];
}
// Calculate content etag.
$etag = md5( $buffer );
// Send headers.
$this->_send_headers( $is_404, $time, $etag, $compression_header, $headers );
return $buffer;
}
/**
* Collects usage statistics for the current request.
*
* @param object $storage The storage object for saving statistics.
*
* @return void
*/
public function w3tc_usage_statistics_of_request( $storage ) {
global $w3tc_start_microtime;
$time_ms = 0;
if ( ! empty( $w3tc_start_microtime ) ) {
$time_ms = (int) ( ( microtime( true ) - $w3tc_start_microtime ) * 1000 );
$storage->counter_add( 'pagecache_requests_time_10ms', (int) ( $time_ms / 10 ) );
}
if ( ! empty( $this->process_status ) ) {
// see registered keys in PgCache_Plugin.w3tc_usage_statistics_metrics.
$storage->counter_add( 'php_requests_pagecache_' . $this->process_status, 1 );
if ( $this->_debug ) {
self::log(
'finished in ' . $time_ms . ' size ' . $this->output_size . ' with process status ' .
$this->process_status . ' reason ' . $this->cache_reject_reason
);
}
}
}
/**
* Logs a message to the page cache log.
*
* @param string $msg The message to log.
*
* @return bool|int The number of bytes written, or false on failure.
*/
protected static function log( $msg ) {
$w3tc_data = sprintf(
'[%1$s] [%2$s] [%3$s] %4$s ' . "\n",
gmdate( 'r' ),
isset( $_SERVER['REQUEST_URI'] ) ? filter_var( stripslashes( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : '',
! empty( $_SERVER['HTTP_REFERER'] ) ? htmlspecialchars( $_SERVER['HTTP_REFERER'] ) : '-',
$msg
);
$w3tc_data = strtr( $w3tc_data, '<>', '..' );
/**
* Assignment used to land in `$date` (typo for `$w3tc_data`),
* so the redacted form was never written and the log file kept
* the raw `_wpnonce=` value. Keep the variable name on `$w3tc_data`
* so the file_put_contents below writes the redacted form.
* Full `redact()` covers nonces plus remaining assignment forms
* (query secrets, JSON, cookies) that appear on REQUEST_URI.
*/
$w3tc_data = Util_Debug::redact( $w3tc_data );
$filename = Util_Debug::log_filename( 'pagecache' );
return @file_put_contents( $filename, $w3tc_data, FILE_APPEND );
}
}