Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-cfasync="false" to script tags #286

Merged
merged 4 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ public function insert_parsely_javascript() {
true
);

add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10, 3 );
add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 3 );

$dependencies = array();

Expand All @@ -1160,10 +1160,14 @@ public function insert_parsely_javascript() {

public function script_loader_tag( $tag, $handle, $src ) {
$parsely_options = $this->get_options();
if ( in_array( $handle, [ 'wp-parsely', 'wp-parsely-tracker' ] ) ) {
if ( in_array( $handle, array(
'wp-parsely',
'wp-parsely-api',
'wp-parsely-tracker',
) ) ) {
// Have ClouldFlare Rocket Loader ignore these scripts:
// https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts-
$tag = preg_replace( '/^<script src=/', '<script data-cfasync="false" src=', $tag );
$tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag );
}

if ( $handle === 'wp-parsely-tracker' ) {
Expand Down
9 changes: 4 additions & 5 deletions tests/class-all-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function test_parsely_default_scripts() {
$output = ob_get_clean();

self::assertSame(
"<script type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>\n",
"<script data-cfasync=\"false\" type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>\n",
$output,
'Failed to confirm script tags were printed correctly'
);
Expand Down Expand Up @@ -146,14 +146,13 @@ public function test_parsely_api_enabled_scripts() {

wp_print_scripts();
$output = ob_get_clean();

self::assertContains(
"/* <![CDATA[ */
var wpParsely = {\"apikey\":\"blog.parsely.com\"};
/* ]]> */
</script>
<script type='text/javascript' src='" . esc_url( PARSELY_PLUGIN_URL ) . "build/init-api.js?ver=" . PARSELY_VERSION . "' id='wp-parsely-api-js'></script>
<script type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>
<script data-cfasync=\"false\" type='text/javascript' src='" . esc_url( PARSELY_PLUGIN_URL ) . "build/init-api.js?ver=" . PARSELY_VERSION . "' id='wp-parsely-api-js'></script>
<script data-cfasync=\"false\" type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>
",
$output,
'Failed to confirm script tags were printed correctly'
Expand Down Expand Up @@ -869,7 +868,7 @@ public function test_user_logged_in_multisite() {
$output = ob_get_clean();

self::assertSame(
"<script type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>\n",
"<script data-cfasync=\"false\" type='text/javascript' data-parsely-site=\"blog.parsely.com\" src='https://cdn.parsely.com/keys/blog.parsely.com/p.js?ver=" . PARSELY_VERSION . "' id=\"parsely-cfg\"></script>\n",
$output,
'Failed to confirm script tags were printed correctly'
);
Expand Down