| @@ -36,9 +36,17 @@ | ||
| 36 | 36 | && (bool) apply_filters( 'ablocks/perf/bypass_optimizations_for_editors', true ) ) { |
| 37 | 37 | return; |
| 38 | 38 | } |
| 39 | 39 | $self = new self(); |
| 40 | - add_filter( 'script_loader_tag', [ $self, 'delay_tag' ], 20, 3 ); | |
| 40 | + // The tag rewriting is shared with consent gating; see ScriptGate. | |
| 41 | + ( new ScriptGate( | |
| 42 | + 'ablocks/delayed', | |
| 43 | + function ( $handle ) use ( $self ) { | |
| 44 | + return $self->should_delay( $handle ); | |
| 45 | + }, | |
| 46 | + null, | |
| 47 | + true | |
| 48 | + ) )->hook( 20 ); | |
| 41 | 49 | add_action( 'wp_footer', [ $self, 'print_loader' ], 99 ); |
| 42 | 50 | } |
| 43 | 51 | |
| 44 | 52 | private function handles() { |
| @@ -48,26 +56,16 @@ | ||
| 48 | 56 | ); |
| 49 | 57 | } |
| 50 | 58 | |
| 51 | 59 | /** |
| 52 | - * Rewrite the target script tag so the browser doesn't execute it yet. | |
| 53 | - */ | |
| 54 | - public function delay_tag( $tag, $handle, $src ) { | |
| 55 | - if ( ! $this->should_delay( $handle ) ) { | |
| 56 | - return $tag; | |
| 57 | - } | |
| 58 | - // Move src out of the way and mark the tag as delayed. | |
| 59 | - $tag = preg_replace( '/\ssrc=/', ' data-ablocks-src=', $tag, 1 ); | |
| 60 | - $tag = preg_replace( '/^<script\s/', '<script type="ablocks/delayed" ', $tag, 1 ); | |
| 61 | - return $tag; | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | 60 | * Whether a script handle should be delayed. Covers the combined per-page |
| 66 | 61 | * script (assets-generation on) and the per-block frontend scripts |
| 67 | 62 | * (assets-generation off), so delay works in both modes. |
| 63 | + * | |
| 64 | + * @param string $handle Script handle. | |
| 65 | + * @return bool | |
| 68 | 66 | */ |
| 69 | - private function should_delay( $handle ) { | |
| 67 | + public function should_delay( $handle ) { | |
| 70 | 68 | if ( in_array( $handle, $this->handles(), true ) ) { |
| 71 | 69 | return true; |
| 72 | 70 | } |
| 73 | 71 | return (bool) preg_match( '/^ablocks-.+-block-static-script$/', (string) $handle ); |