Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions includes/class-linkforty-pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public static function attributes() {
/**
* Adds the configuration attributes to the enqueued script tag.
*
* @param string $tag The `<script>` HTML.
* `$tag` also holds the inline bootstrap printed before the pixel, so the
* attributes must go on the pixel's own `<script src>` tag: the pixel reads
* them from `document.currentScript` and sends nothing without a site key.
*
* @param string $tag The `<script>` HTML, inline scripts included.
* @param string $handle Script handle.
* @return string
*/
Expand All @@ -149,6 +153,21 @@ public static function add_attributes( $tag, $handle ) {
$extra .= ' ' . $name . '="' . esc_attr( (string) $value ) . '"';
}

return preg_replace( '/<script\b/', '<script' . $extra, $tag, 1 );
// WordPress gives the pixel tag the id `{handle}-js`, quoted with ' up to
// 6.3 and " from 6.4. Fall back to the first tag with a src if an
// optimization plugin has rewritten the id.
$patterns = array(
'/<script\b(?=[^>]*\sid=([\'"])' . preg_quote( self::HANDLE . '-js', '/' ) . '\1)/',
'/<script\b(?=[^>]*\ssrc=)/',
);
foreach ( $patterns as $pattern ) {
$count = 0;
$tagged = preg_replace( $pattern, '<script' . $extra, $tag, 1, $count );
if ( $count > 0 ) {
return $tagged;
}
}

return $tag;
}
}
Loading