is_html_response() ) {
return $html;
}
$dry_run = (bool) Helper::get( 'dry_run', false );
if ( Helper::get( 'buffer_gating', true ) ) {
$filtered = preg_replace_callback(
'##is',
function ( $match ) use ( $dry_run ) {
return $this->process( $match, $dry_run );
},
$html
);
// A backtrack limit or a catastrophic pattern returns null.
// Serving the original page ungated is bad; serving an empty page
// is worse.
$html = null === $filtered ? $html : $filtered;
}
if ( Helper::get( 'embed_gating', true ) ) {
$html = $this->gate_elements( $html, $dry_run );
}
return $html;
}
/**
* The iframe and pixel pass.
*
* Runs on everything *between* the script elements rather than on the whole
* document. An inline script is perfectly entitled to contain the text
* `
` — in a template string, in a JSON blob,
* in an example — and rewriting it there would not gate a request, it would
* corrupt the script. Splitting on script blocks and skipping the captured
* halves costs one more pass and removes the whole class of problem.
*
* @param string $html The document.
* @param bool $dry_run Report instead of rewrite.
* @return string
*/
private function gate_elements( $html, $dry_run ) {
$has_iframe = false !== stripos( $html, '