| @@ -185,9 +185,9 @@ | ||
| 185 | 185 | // so carry it — and everything else the late filter set in the |
| 186 | 186 | // same pass — over. A consumer reading the rendered HTML (or |
| 187 | 187 | // our own buffer passes) must still see the opt-out it asked |
| 188 | 188 | // for. |
| 189 | - $tag = self::copy_late_attributes( $tag, $pristine ); | |
| 189 | + $tag = self::copy_late_attributes( $tag, $pristine, $handle ); | |
| 190 | 190 | } |
| 191 | 191 | // The snapshot was taken on `script_loader_tag`, by which point |
| 192 | 192 | // `script_loader_src` (priority 10) had ALREADY swapped in the |
| 193 | 193 | // hashed cache URL — so reverting the tag alone still leaves the |
| @@ -230,9 +230,9 @@ | ||
| 230 | 230 | * |
| 231 | 231 | * @param string $from Tag as the late filter left it. |
| 232 | 232 | * @param string $to Snapshot tag to stamp onto. |
| 233 | 233 | */ |
| 234 | - private static function copy_late_attributes( string $from, string $to ): string { | |
| 234 | + private static function copy_late_attributes( string $from, string $to, string $handle ): string { | |
| 235 | 235 | $late_tags = self::open_tags( $from ); |
| 236 | 236 | $to_tags = self::open_tags( $to ); |
| 237 | 237 | if ( empty( $late_tags ) || empty( $to_tags ) || count( $late_tags ) !== count( $to_tags ) ) { |
| 238 | 238 | // Counts differ when a stamper/blocker injected or replaced a |
| @@ -254,9 +254,9 @@ | ||
| 254 | 254 | // later. (#469) |
| 255 | 255 | $out = $to; |
| 256 | 256 | // Right to left: an earlier splice would shift every later offset. |
| 257 | 257 | for ( $i = count( $to_tags ) - 1; $i >= 0; $i-- ) { |
| 258 | - $add = self::late_attribute_delta( $late_tags[ $i ]['attrs'], $to_tags[ $i ]['attrs'] ); | |
| 258 | + $add = self::late_attribute_delta( $late_tags[ $i ]['attrs'], $to_tags[ $i ]['attrs'], $handle ); | |
| 259 | 259 | if ( '' !== $add ) { |
| 260 | 260 | $out = substr_replace( $out, $add, $to_tags[ $i ]['attrs_end'], 0 ); |
| 261 | 261 | } |
| 262 | 262 | } |
| @@ -305,14 +305,25 @@ | ||
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | /** |
| 308 | 308 | * The attributes present on the late tag but not the snapshot, minus |
| 309 | - * the ones our own transforms add. | |
| 309 | + * the ones OUR transforms put there for this handle. | |
| 310 | 310 | * |
| 311 | + * Only attributes recorded in $our_late_attrs are dropped — a blanket | |
| 312 | + * defer/type skip threw away a defer the STAMPER set in the same pass | |
| 313 | + * as its marker. Themify prints main.js with defer + data-no-optimize | |
| 314 | + * together, and its config rides a deferred data: URI script printed | |
| 315 | + * just before it; stripping the theme's defer made main.js | |
| 316 | + * parser-blocking, so it ran ahead of its config and the theme died | |
| 317 | + * with "themify_vars is not defined". `src` is still never copied: | |
| 318 | + * it belongs to the snapshot, and restore_marked_script_src() owns | |
| 319 | + * undoing a minified URL. | |
| 320 | + * | |
| 311 | 321 | * @param string $late_attrs Attribute string from the transformed tag. |
| 312 | 322 | * @param string $to_attrs Attribute string from the snapshot tag. |
| 323 | + * @param string $handle Script handle the tags belong to. | |
| 313 | 324 | */ |
| 314 | - private static function late_attribute_delta( string $late_attrs, string $to_attrs ): string { | |
| 325 | + private static function late_attribute_delta( string $late_attrs, string $to_attrs, string $handle ): string { | |
| 315 | 326 | $pattern = '#\s([-\w:]+)(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[^\s>]*))?#'; |
| 316 | 327 | if ( ! preg_match_all( $pattern, $late_attrs, $late, PREG_SET_ORDER ) ) { |
| 317 | 328 | return ''; |
| 318 | 329 | } |
| @@ -324,9 +335,9 @@ | ||
| 324 | 335 | } |
| 325 | 336 | $add = ''; |
| 326 | 337 | foreach ( $late as $attr ) { |
| 327 | 338 | $name = strtolower( $attr[1] ); |
| 328 | - if ( isset( $have[ $name ] ) || in_array( $name, self::OUR_TRANSFORM_ATTRS, true ) ) { | |
| 339 | + if ( isset( $have[ $name ] ) || 'src' === $name || isset( self::$our_late_attrs[ $handle ][ $name ] ) ) { | |
| 329 | 340 | continue; |
| 330 | 341 | } |
| 331 | 342 | if ( 0 === strpos( $name, 'data-xs-' ) ) { |
| 332 | 343 | continue; |
| @@ -336,21 +347,20 @@ | ||
| 336 | 347 | return $add; |
| 337 | 348 | } |
| 338 | 349 | |
| 339 | 350 | /** |
| 340 | - * Attributes our own transforms add. Copying any of these from the | |
| 341 | - * transformed tag back onto the snapshot would re-apply the very | |
| 342 | - * transform we are undoing: | |
| 351 | + * Attribute names OUR transforms added in this request, keyed by | |
| 352 | + * handle: defer_script_tag() records `defer`, delay_script_tag() | |
| 353 | + * records `type` when it parks an inline block. Copying one of these | |
| 354 | + * from the late tag back onto the snapshot would re-apply the very | |
| 355 | + * transform the revert is undoing — but the same names coming from a | |
| 356 | + * STAMPER are the author's intent and must survive, so the skip is | |
| 357 | + * per-handle, never by name alone. `data-xs-*` is handled by prefix | |
| 358 | + * separately. (#469) | |
| 343 | 359 | * |
| 344 | - * defer/async — defer_script_tag() | |
| 345 | - * type — delay_script_tag() parks an inline block as | |
| 346 | - * text/xspeed-delayed; a type the author set is on the | |
| 347 | - * snapshot already and matches by name before we get here | |
| 348 | - * src — belongs to the snapshot, never to the late tag | |
| 349 | - * | |
| 350 | - * `data-xs-*` is handled by prefix separately. (#469) | |
| 360 | + * @var array<string,array<string,true>> | |
| 351 | 361 | */ |
| 352 | - private const OUR_TRANSFORM_ATTRS = array( 'defer', 'async', 'type', 'src' ); | |
| 362 | + private static $our_late_attrs = array(); | |
| 353 | 363 | |
| 354 | 364 | /** |
| 355 | 365 | * Locate the opening `<script>` that carries the src, falling back to |
| 356 | 366 | * the last one when none does. |
| @@ -548,9 +558,15 @@ | ||
| 548 | 558 | // dependent that touches a global its dependency defines. (#234) |
| 549 | 559 | // |
| 550 | 560 | // The lookahead scans only within the tag (`[^>]*`) for ` src=`, so |
| 551 | 561 | // an inline `<script id="…-js-before">` can never match. |
| 552 | - return (string) preg_replace( '#<script\b(?=[^>]*\ssrc\s*=)#i', '<script defer="defer"', $tag, 1 ); | |
| 562 | + $deferred = (string) preg_replace( '#<script\b(?=[^>]*\ssrc\s*=)#i', '<script defer="defer"', $tag, 1 ); | |
| 563 | + if ( $deferred !== $tag ) { | |
| 564 | + // Remember that THIS defer is ours, so the late opt-out revert | |
| 565 | + // drops it — and only it, never a stamper's own defer. | |
| 566 | + self::$our_late_attrs[ (string) $handle ]['defer'] = true; | |
| 567 | + } | |
| 568 | + return $deferred; | |
| 553 | 569 | } |
| 554 | 570 | |
| 555 | 571 | /** |
| 556 | 572 | * Filter: `script_loader_tag` — rewrite src= to data-xs-src= so the |
| @@ -592,9 +608,16 @@ | ||
| 592 | 608 | // the moment jquery-core is delayed. A handle the user NAMED in |
| 593 | 609 | // delay_js_targets is still delayed — an explicit entry is the user |
| 594 | 610 | // saying they know the inline consumer is safe to break or absent. |
| 595 | 611 | if ( isset( self::inline_bound_handles()[ (string) $handle ] ) |
| 596 | - && ! self::is_user_named_target( (string) $handle, (string) $src ) ) { | |
| 612 | + && ! self::is_user_named_target( (string) $handle, (string) $src ) | |
| 613 | + // Smart Delay inverts this protection: the handle is delayed and | |
| 614 | + // its own before/after snippets are parked WITH it (see | |
| 615 | + // park_smart_inline()), so the consumer no longer runs against a | |
| 616 | + // missing global — it replays after its provider, in page order. | |
| 617 | + // On a builder page nearly every script is inline-bound, which is | |
| 618 | + // why delay-all without this delayed almost nothing. | |
| 619 | + && ! self::smart_delay_enabled() ) { | |
| 597 | 620 | return $tag; |
| 598 | 621 | } |
| 599 | 622 | // A non-executable type means this tag is data, or is being held by |
| 600 | 623 | // somebody else on purpose. The buffer pass has always checked this; |
| @@ -637,9 +660,9 @@ | ||
| 637 | 660 | // would win over a marker appended behind it and keep executing. |
| 638 | 661 | // A non-default original type is stashed in data-xs-type so the |
| 639 | 662 | // bootstrap can restore it on replay (#274 — type is what a script |
| 640 | 663 | // IS; a parked `type="module"` must come back as a module). |
| 641 | - $tag = (string) preg_replace_callback( | |
| 664 | + $parked = (string) preg_replace_callback( | |
| 642 | 665 | '#<script\b([^>]*)>#i', |
| 643 | 666 | static function ( array $m ): string { |
| 644 | 667 | return '<script' . self::park_type_attrs( $m[1] ) . '>'; |
| 645 | 668 | }, |
| @@ -645,9 +668,14 @@ | ||
| 645 | 668 | }, |
| 646 | 669 | $tag, |
| 647 | 670 | 1 |
| 648 | 671 | ); |
| 649 | - return $tag; | |
| 672 | + if ( $parked !== $tag ) { | |
| 673 | + // The parked type is ours to drop on a late opt-out revert; an | |
| 674 | + // author-set type sits on the snapshot and survives regardless. | |
| 675 | + self::$our_late_attrs[ (string) $handle ]['type'] = true; | |
| 676 | + } | |
| 677 | + return $parked; | |
| 650 | 678 | } |
| 651 | 679 | |
| 652 | 680 | /** |
| 653 | 681 | * Script types the buffer pass must never touch. `<script>` carries |
| @@ -1088,10 +1116,13 @@ | ||
| 1088 | 1116 | // tag with a URL and catastrophic here: it would park every |
| 1089 | 1117 | // inline script on the page. Inline code is delayed only on a |
| 1090 | 1118 | // positive identification — the body names a known vendor |
| 1091 | 1119 | // host, or a fragment the user targeted — and the exclusion |
| 1092 | - // list still wins first. | |
| 1093 | - if ( self::is_excluded_script( '', $body ) ) { | |
| 1120 | + // list still wins first. A target naming a consent manager | |
| 1121 | + // lifts the floor here the same way it does for a src tag; | |
| 1122 | + // without it the same entry delayed the file and left the | |
| 1123 | + // vendor's inline code eager. | |
| 1124 | + if ( self::is_excluded_script( '', $body, true ) ) { | |
| 1094 | 1125 | return $whole; |
| 1095 | 1126 | } |
| 1096 | 1127 | if ( ! self::matches_known_third_party( $body ) && ! self::matches_user_targets( $body ) ) { |
| 1097 | 1128 | return $whole; |
| @@ -1133,70 +1164,49 @@ | ||
| 1133 | 1164 | $opts = self::opts(); |
| 1134 | 1165 | $timeout = isset( $opts['delay_js_timeout'] ) ? (int) $opts['delay_js_timeout'] : 8000; |
| 1135 | 1166 | $timeout = max( 0, min( 60000, $timeout ) ); |
| 1136 | 1167 | |
| 1137 | - // Tiny vanilla bootstrap; keep it self-contained so the page | |
| 1138 | - // has no JS dependencies before the first interaction. | |
| 1139 | - ?> | |
| 1140 | -<script id="xspeed-delay-bootstrap"> | |
| 1141 | -(function(){ | |
| 1142 | - var events=['mousemove','keydown','touchstart','scroll','wheel']; | |
| 1143 | - var fired=false; | |
| 1144 | - function load(){ | |
| 1145 | - if(fired)return;fired=true; | |
| 1146 | - events.forEach(function(e){window.removeEventListener(e,load,{passive:true,capture:true});}); | |
| 1147 | - var delayed=document.querySelectorAll('script[data-xs-delay]'); | |
| 1148 | - delayed.forEach(function(s){ | |
| 1149 | - var n=document.createElement('script'); | |
| 1150 | - // A dynamically-created script is async by default, so replayed | |
| 1151 | - // EXTERNALS would race each other; async=false restores document | |
| 1152 | - // order among the externals. Narrower guarantee, stated plainly: | |
| 1153 | - // a replayed INLINE script still executes synchronously at its | |
| 1154 | - // replaceChild, i.e. possibly before an earlier external has | |
| 1155 | - // finished LOADING — so an inline consumer of a delayed external | |
| 1156 | - // is only safe when both were delayed by explicit user targeting | |
| 1157 | - // (the inline-bound guard keeps the implicit case eager). | |
| 1158 | - n.async=false; | |
| 1159 | - // Nonce hiding: a connected element's nonce CONTENT attribute reads | |
| 1160 | - // as "", so copying it via the attribute loop would hand the clone | |
| 1161 | - // an empty nonce and a nonce-based CSP would block the replay. The | |
| 1162 | - // IDL property still carries the real value. | |
| 1163 | - if(s.nonce){n.nonce=s.nonce;} | |
| 1164 | - Array.prototype.slice.call(s.attributes).forEach(function(a){ | |
| 1165 | - if(a.name==='data-xs-src'){n.setAttribute('src',a.value);return;} | |
| 1166 | - if(a.name==='data-xs-delay')return; | |
| 1167 | - if(a.name==='nonce')return; | |
| 1168 | - // A parked inline tag's ORIGINAL type (module, mostly) rides in | |
| 1169 | - // data-xs-type — restore it, or the replay runs a module as a | |
| 1170 | - // classic script and its imports throw. (#274) | |
| 1171 | - if(a.name==='data-xs-type'){n.setAttribute('type',a.value);return;} | |
| 1172 | - // `type` is what a script IS, not decoration, so it is carried over | |
| 1173 | - // — with ONE exception: our own inline parking marker, which exists | |
| 1174 | - // only to stop the browser executing the original and must not be | |
| 1175 | - // copied onto the replacement. Dropping type wholesale broke two | |
| 1176 | - // things: `type="module"` became a classic script (core's Script | |
| 1177 | - // Modules — Navigation, lightbox, Query Loop — threw "Cannot use | |
| 1178 | - // import statement outside a module" on the default theme), and | |
| 1179 | - // `type="text/plain"`, which is precisely how a consent manager | |
| 1180 | - // parks a blocked third-party script, became executable again. The | |
| 1181 | - // second is a privacy failure, not a broken feature. (#274) | |
| 1182 | - if(a.name==='type'&&a.value==='text/xspeed-delayed')return; | |
| 1183 | - n.setAttribute(a.name,a.value); | |
| 1184 | - }); | |
| 1185 | - if(!s.hasAttribute('data-xs-src')){n.text=s.text;} | |
| 1186 | - s.parentNode.replaceChild(n,s); | |
| 1187 | - }); | |
| 1188 | - } | |
| 1189 | - events.forEach(function(e){window.addEventListener(e,load,{passive:true,capture:true});}); | |
| 1190 | -<?php if ( $timeout > 0 ) : ?> | |
| 1191 | - setTimeout(load,<?php echo (int) $timeout; ?>); | |
| 1192 | -<?php endif; ?> | |
| 1193 | -})(); | |
| 1194 | -</script> | |
| 1195 | - <?php | |
| 1168 | + // The script is includes/js/delay-bootstrap.js, which also carries | |
| 1169 | + // the design notes for the lifecycle replay (#494). `npm run build` | |
| 1170 | + // minifies it into assets/delay-bootstrap.min.js; the timeout goes in | |
| 1171 | + // place of its one placeholder. | |
| 1172 | + // | |
| 1173 | + // The tag goes out through wp_print_inline_script_tag(), like Free's | |
| 1174 | + // other inline scripts, so a CSP plugin's wp_inline_script_attributes | |
| 1175 | + // filter can give it a nonce. Printed bare, a nonce CSP blocked it | |
| 1176 | + // and nothing was ever replayed. | |
| 1177 | + $js = str_replace( 'XSPEED_DELAY_TIMEOUT', (string) $timeout, self::delay_bootstrap_js() ); | |
| 1178 | + wp_print_inline_script_tag( $js, array( 'id' => 'xspeed-delay-bootstrap' ) ); | |
| 1196 | 1179 | } |
| 1197 | 1180 | |
| 1181 | + /** The delay bootstrap's code, read once per request. */ | |
| 1182 | + private static $delay_bootstrap_js = null; | |
| 1183 | + | |
| 1198 | 1184 | /** |
| 1185 | + * The built delay bootstrap, without the line that records its source. | |
| 1186 | + * If the build is missing, the readable source is valid JS too, only | |
| 1187 | + * larger: printing nothing would leave every delayed script parked for | |
| 1188 | + * good, because the tags are already rewritten by the time this runs. | |
| 1189 | + */ | |
| 1190 | + private static function delay_bootstrap_js(): string { | |
| 1191 | + if ( null !== self::$delay_bootstrap_js ) { | |
| 1192 | + return self::$delay_bootstrap_js; | |
| 1193 | + } | |
| 1194 | + $root = dirname( __DIR__ ); | |
| 1195 | + $built = $root . '/assets/delay-bootstrap.min.js'; | |
| 1196 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local plugin file, not a remote URL. | |
| 1197 | + $js = is_readable( $built ) ? (string) file_get_contents( $built ) : ''; | |
| 1198 | + if ( '' !== $js ) { | |
| 1199 | + $js = (string) preg_replace( '#\A/\*[^\n]*\*/\n#', '', $js ); | |
| 1200 | + } else { | |
| 1201 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a local plugin file, not a remote URL. | |
| 1202 | + $js = (string) file_get_contents( $root . '/includes/js/delay-bootstrap.js' ); | |
| 1203 | + } | |
| 1204 | + self::$delay_bootstrap_js = trim( $js ); | |
| 1205 | + return self::$delay_bootstrap_js; | |
| 1206 | + } | |
| 1207 | + | |
| 1208 | + /** | |
| 1199 | 1209 | * Filter: `style_loader_tag` — wrap stylesheets in the |
| 1200 | 1210 | * print → onload="all" pattern so they download non-blocking. |
| 1201 | 1211 | * Pairs with critical CSS workflows. Adds a <noscript> fallback so |
| 1202 | 1212 | * users with JS disabled still get styles applied (via media="all"). |
| @@ -1807,11 +1817,11 @@ | ||
| 1807 | 1817 | ); |
| 1808 | 1818 | |
| 1809 | 1819 | /** |
| 1810 | 1820 | * Consent managers are never deferred, and never delayed by a broad |
| 1811 | - * setting — only an EXPLICIT delay_js_targets entry naming one lifts | |
| 1812 | - * the floor (see is_excluded_script()); the `xspeed_js_exclusion_floor` | |
| 1813 | - * filter remains the code-level override. | |
| 1821 | + * setting — only a delay_js_targets entry that NAMES the vendor lifts | |
| 1822 | + * the floor (see user_named_consent_manager()); the | |
| 1823 | + * `xspeed_js_exclusion_floor` filter remains the code-level override. | |
| 1814 | 1824 | * |
| 1815 | 1825 | * A consent banner is drawn by JavaScript, and it is the one thing on |
| 1816 | 1826 | * the page that has to appear before anything else happens. Delay it |
| 1817 | 1827 | * and a visitor who lands, reads and leaves without touching the page |
| @@ -1893,8 +1903,62 @@ | ||
| 1893 | 1903 | 'surecookie', |
| 1894 | 1904 | ); |
| 1895 | 1905 | |
| 1896 | 1906 | /** |
| 1907 | + * What a site owner types to name each floor entry, and what the admin | |
| 1908 | + * shows them. Keyed by CONSENT_MANAGER_FLOOR token; a test holds the two | |
| 1909 | + * in step. | |
| 1910 | + * | |
| 1911 | + * The keyword is the part of the token a person would actually write | |
| 1912 | + * (`cookiebot`, not `consent.cookiebot.com`), and it is always a substring | |
| 1913 | + * of the token, so an entry that names the vendor this way still matches | |
| 1914 | + * the vendor's URL. A brand name that appears nowhere in the URL | |
| 1915 | + * (`cookieyes`, `onetrust`, `cmplz`) is deliberately not a keyword: it | |
| 1916 | + * could never match the tag, so offering it would promise a lift that | |
| 1917 | + * cannot happen. The exact handle always works as well. | |
| 1918 | + * | |
| 1919 | + * Cookiebot in Usercentrics CMP mode loads from web.cmp.usercentrics.eu, | |
| 1920 | + * so the floor catches it as Usercentrics and `usercentrics` names it, | |
| 1921 | + * not `cookiebot`. | |
| 1922 | + */ | |
| 1923 | + private const CONSENT_MANAGER_NAMES = array( | |
| 1924 | + 'cookie-law-info' => array( 'label' => 'CookieYes', 'keyword' => 'cookie-law-info' ), | |
| 1925 | + 'complianz' => array( 'label' => 'Complianz', 'keyword' => 'complianz' ), | |
| 1926 | + 'notificationx' => array( 'label' => 'NotificationX', 'keyword' => 'notificationx' ), | |
| 1927 | + 'consent.cookiebot.com' => array( 'label' => 'Cookiebot', 'keyword' => 'cookiebot' ), | |
| 1928 | + 'cookie-notice' => array( 'label' => 'Cookie Notice', 'keyword' => 'cookie-notice' ), | |
| 1929 | + 'hu-banner' => array( 'label' => 'Cookie Notice (Cookie Compliance)', 'keyword' => 'hu-banner' ), | |
| 1930 | + 'gdpr-cookie-compliance' => array( 'label' => 'GDPR Cookie Compliance (Moove)', 'keyword' => 'gdpr-cookie-compliance' ), | |
| 1931 | + 'app.termly.io' => array( 'label' => 'Termly', 'keyword' => 'termly' ), | |
| 1932 | + 'usercentrics.eu' => array( 'label' => 'Usercentrics', 'keyword' => 'usercentrics' ), | |
| 1933 | + 'cdn.iubenda.com' => array( 'label' => 'Iubenda', 'keyword' => 'iubenda' ), | |
| 1934 | + 'cdn.cookielaw.org' => array( 'label' => 'OneTrust', 'keyword' => 'cookielaw' ), | |
| 1935 | + 'borlabs-cookie' => array( 'label' => 'Borlabs Cookie', 'keyword' => 'borlabs' ), | |
| 1936 | + 'real-cookie-banner' => array( 'label' => 'Real Cookie Banner', 'keyword' => 'real-cookie-banner' ), | |
| 1937 | + 'surecookie' => array( 'label' => 'SureCookie', 'keyword' => 'surecookie' ), | |
| 1938 | + ); | |
| 1939 | + | |
| 1940 | + /** | |
| 1941 | + * "Label (keyword)" for every built-in consent manager, for the admin. | |
| 1942 | + * | |
| 1943 | + * Reads the built-in list, not the filtered floor: the admin describes | |
| 1944 | + * what ships, and a site that trimmed the floor in code knows it did. | |
| 1945 | + * | |
| 1946 | + * @return string[] | |
| 1947 | + */ | |
| 1948 | + public static function consent_manager_labels(): array { | |
| 1949 | + $out = array(); | |
| 1950 | + foreach ( self::CONSENT_MANAGER_FLOOR as $token ) { | |
| 1951 | + $name = self::CONSENT_MANAGER_NAMES[ $token ] ?? array( | |
| 1952 | + 'label' => $token, | |
| 1953 | + 'keyword' => $token, | |
| 1954 | + ); | |
| 1955 | + $out[] = $name['label'] . ' (' . $name['keyword'] . ')'; | |
| 1956 | + } | |
| 1957 | + return $out; | |
| 1958 | + } | |
| 1959 | + | |
| 1960 | + /** | |
| 1897 | 1961 | * Per-request memo for exclusion_floor(). Null = not resolved. |
| 1898 | 1962 | * |
| 1899 | 1963 | * @var string[]|null |
| 1900 | 1964 | */ |
| @@ -1928,11 +1992,12 @@ | ||
| 1928 | 1992 | /** |
| 1929 | 1993 | * @param string $handle Script handle ('' on the buffer sweep |
| 1930 | 1994 | * when no id survived). |
| 1931 | 1995 | * @param string $src Script URL. |
| 1932 | - * @param bool $named_lifts_floor Delay paths only: a handle/URL the user | |
| 1933 | - * EXPLICITLY typed into delay_js_targets | |
| 1934 | - * passes the consent floor. Typing a | |
| 1996 | + * @param bool $named_lifts_floor Delay paths only: a delay_js_targets | |
| 1997 | + * entry that NAMES the consent manager | |
| 1998 | + * passes the floor (see | |
| 1999 | + * user_named_consent_manager()). Typing a | |
| 1935 | 2000 | * consent manager's name into an |
| 1936 | 2001 | * allow-list is the site owner taking the |
| 1937 | 2002 | * consent-timing decision back — GDPR is |
| 1938 | 2003 | * theirs to weigh, not ours; the floor |
| @@ -1964,10 +2029,13 @@ | ||
| 1964 | 2029 | // one that cleared the textarea has no list at all. Neither may |
| 1965 | 2030 | // hide the banner. (#275) |
| 1966 | 2031 | foreach ( self::exclusion_floor() as $needle ) { |
| 1967 | 2032 | if ( self::target_matches( $needle, $handle, $src ) ) { |
| 1968 | - if ( $named_lifts_floor && self::is_user_named_target( $handle, $src ) ) { | |
| 1969 | - break; | |
| 2033 | + // `continue`, not `break`: a second floor token matching the | |
| 2034 | + // same tag has to be named too, or a filter-added token | |
| 2035 | + // would be lifted by an entry that names only the first. | |
| 2036 | + if ( $named_lifts_floor && self::user_named_consent_manager( $needle, $handle, $src ) ) { | |
| 2037 | + continue; | |
| 1970 | 2038 | } |
| 1971 | 2039 | return true; |
| 1972 | 2040 | } |
| 1973 | 2041 | } |
| @@ -2038,8 +2106,131 @@ | ||
| 2038 | 2106 | } |
| 2039 | 2107 | return false; |
| 2040 | 2108 | } |
| 2041 | 2109 | |
| 2110 | + /** | |
| 2111 | + * Whether a delay_js_targets entry NAMES the consent manager whose floor | |
| 2112 | + * token matched this tag, and so lifts the floor for it. | |
| 2113 | + * | |
| 2114 | + * is_user_named_target() is not enough here: it asks whether any entry | |
| 2115 | + * matches the tag, and a delay target is a URL substring. `/plugins/`, | |
| 2116 | + * `.js`, `min.js`, `frontend` or the site's own host each match every | |
| 2117 | + * consent banner on the page, so one broad entry switched the floor off | |
| 2118 | + * for all of them and brought #275 back. An entry names the vendor when | |
| 2119 | + * it is the exact handle, or when it contains the vendor's keyword (or | |
| 2120 | + * its floor token) and still matches the tag, so `notificationx` and | |
| 2121 | + * `/plugins/notificationx/` lift NotificationX, `termly` cannot lift | |
| 2122 | + * Cookiebot, and `/plugins/` lifts nothing. | |
| 2123 | + * | |
| 2124 | + * A token added through `xspeed_js_exclusion_floor` has no keyword, so | |
| 2125 | + * only an entry containing that token, or the exact handle, names it. | |
| 2126 | + * | |
| 2127 | + * @param string $floor_token The floor entry that matched this tag. | |
| 2128 | + * @param string $handle Script handle ('' when unknown). | |
| 2129 | + * @param string $src Script URL, or the body on the inline pass. | |
| 2130 | + */ | |
| 2131 | + private static function user_named_consent_manager( string $floor_token, string $handle, string $src ): bool { | |
| 2132 | + $opts = self::opts(); | |
| 2133 | + $targets = is_array( $opts['delay_js_targets'] ?? null ) ? $opts['delay_js_targets'] : array(); | |
| 2134 | + $names = array( $floor_token ); | |
| 2135 | + if ( isset( self::CONSENT_MANAGER_NAMES[ $floor_token ] ) ) { | |
| 2136 | + $names[] = self::CONSENT_MANAGER_NAMES[ $floor_token ]['keyword']; | |
| 2137 | + } | |
| 2138 | + foreach ( $targets as $needle ) { | |
| 2139 | + $needle = trim( (string) $needle ); | |
| 2140 | + if ( '' === $needle ) { | |
| 2141 | + continue; | |
| 2142 | + } | |
| 2143 | + $names_it = '' !== $handle && $handle === $needle; | |
| 2144 | + foreach ( $names as $name ) { | |
| 2145 | + if ( false !== stripos( $needle, $name ) ) { | |
| 2146 | + $names_it = true; | |
| 2147 | + break; | |
| 2148 | + } | |
| 2149 | + } | |
| 2150 | + if ( $names_it && self::target_matches( $needle, $handle, $src ) ) { | |
| 2151 | + return true; | |
| 2152 | + } | |
| 2153 | + } | |
| 2154 | + return false; | |
| 2155 | + } | |
| 2156 | + | |
| 2157 | + /** Both toggles on: delay_js and its carry-the-inline-snippets mode. */ | |
| 2158 | + private static function smart_delay_enabled(): bool { | |
| 2159 | + $opts = self::opts(); | |
| 2160 | + return ! empty( $opts['delay_js'] ) && ! empty( $opts['delay_js_smart'] ); | |
| 2161 | + } | |
| 2162 | + | |
| 2163 | + /** | |
| 2164 | + * Would Smart Delay postpone this handle's tag? | |
| 2165 | + * | |
| 2166 | + * The snippet-parking filter runs when WordPress prints a handle's | |
| 2167 | + * `before` snippet — BEFORE script_loader_tag sees the tag itself — so | |
| 2168 | + * the decision cannot be read back from what happened to the tag; both | |
| 2169 | + * sides evaluate this same predicate. It mirrors the handle/src checks | |
| 2170 | + * of delay_script_tag() only: the tag-level outs there (an optimizer | |
| 2171 | + * opt-out attribute, a non-executable type) are invisible here, so a | |
| 2172 | + * tag that keeps itself eager through one of those can still have its | |
| 2173 | + * snippets parked. That parks an init until first interaction rather | |
| 2174 | + * than throwing, and Smart Delay is opt-in — acceptable, and documented | |
| 2175 | + * on the setting. | |
| 2176 | + */ | |
| 2177 | + private static function smart_delays_handle( string $handle ): bool { | |
| 2178 | + if ( '' === $handle ) { | |
| 2179 | + return false; | |
| 2180 | + } | |
| 2181 | + $src = self::original_src( $handle ); | |
| 2182 | + if ( self::is_excluded_script( $handle, $src, true ) ) { | |
| 2183 | + return false; | |
| 2184 | + } | |
| 2185 | + return self::is_delay_target( $handle, $src ); | |
| 2186 | + } | |
| 2187 | + | |
| 2188 | + /** | |
| 2189 | + * Park a delayed handle's own before/after snippet, in Smart Delay mode. | |
| 2190 | + * | |
| 2191 | + * Runs on `wp_inline_script_attributes`, which fires for every inline | |
| 2192 | + * script WordPress prints itself — so it works on pages the HTML buffer | |
| 2193 | + * never filters (a BYPASS route like /cart), where the handle's tag is | |
| 2194 | + * still delayed by script_loader_tag. `-js-extra` stays eager on | |
| 2195 | + * purpose: it is data assignments, harmless early and sometimes read by | |
| 2196 | + * eager code. | |
| 2197 | + * | |
| 2198 | + * @param mixed $attributes Inline script attributes. | |
| 2199 | + * @param string $javascript The snippet body. | |
| 2200 | + * @return mixed | |
| 2201 | + */ | |
| 2202 | + public static function park_smart_inline( $attributes, $javascript = '' ) { | |
| 2203 | + if ( ! is_array( $attributes ) || ! self::smart_delay_enabled() || self::skip_in_non_frontend_context() ) { | |
| 2204 | + return $attributes; | |
| 2205 | + } | |
| 2206 | + $id = isset( $attributes['id'] ) ? (string) $attributes['id'] : ''; | |
| 2207 | + if ( ! preg_match( '#^(.+)-js-(?:before|after)$#', $id, $m ) ) { | |
| 2208 | + return $attributes; | |
| 2209 | + } | |
| 2210 | + if ( ! self::smart_delays_handle( $m[1] ) ) { | |
| 2211 | + return $attributes; | |
| 2212 | + } | |
| 2213 | + $type = isset( $attributes['type'] ) ? (string) $attributes['type'] : ''; | |
| 2214 | + if ( in_array( $type, self::NON_EXECUTABLE_TYPES, true ) ) { | |
| 2215 | + return $attributes; // data, or parked by someone else on purpose. | |
| 2216 | + } | |
| 2217 | + // A delayed document.write replays after the document has closed | |
| 2218 | + // and replaces the page. Same rule as delay_inline_snippets(). | |
| 2219 | + if ( false !== stripos( (string) $javascript, 'document.write' ) ) { | |
| 2220 | + return $attributes; | |
| 2221 | + } | |
| 2222 | + if ( '' !== $type && ! in_array( $type, self::DEFAULT_JS_TYPES, true ) ) { | |
| 2223 | + $stash = (string) preg_replace( '#[^a-z0-9/+.\-]#', '', $type ); | |
| 2224 | + if ( '' !== $stash ) { | |
| 2225 | + $attributes['data-xs-type'] = $stash; | |
| 2226 | + } | |
| 2227 | + } | |
| 2228 | + $attributes['type'] = 'text/xspeed-delayed'; | |
| 2229 | + $attributes['data-xs-delay'] = '1'; | |
| 2230 | + return $attributes; | |
| 2231 | + } | |
| 2232 | + | |
| 2042 | 2233 | private static function is_delay_target( string $handle, string $src ): bool { |
| 2043 | 2234 | $opts = self::opts(); |
| 2044 | 2235 | $targets = is_array( $opts['delay_js_targets'] ?? null ) ? $opts['delay_js_targets'] : array(); |
| 2045 | 2236 | $targets = array_filter( array_map( 'strval', $targets ), static fn( $t ) => '' !== $t ); |
| @@ -2179,8 +2370,9 @@ | ||
| 2179 | 2370 | self::$js_measured_layout = null; |
| 2180 | 2371 | self::$exclusion_floor = null; |
| 2181 | 2372 | self::$inline_bound_handles = null; |
| 2182 | 2373 | self::$pristine_tag = array(); |
| 2374 | + self::$our_late_attrs = array(); | |
| 2183 | 2375 | } |
| 2184 | 2376 | |
| 2185 | 2377 | /** |
| 2186 | 2378 | * Per-request memo for inline_bound_handles(). Null = not resolved. |