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

Adding wp_parsely_enable_cfasync_tag filter to display Cloudflare links #473

Merged
merged 13 commits into from
Nov 2, 2021
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ Some common use-cases for dynamic tracking are slideshows or articles loaded via

Tracking these events requires manually implementing additional JavaScript above [the standard Parse.ly include](http://www.parsely.com/help/integration/basic/) that the plugin injects into your page source. Please consult [the Parse.ly documentation on dynamic tracking](https://www.parsely.com/help/integration/dynamic/) for instructions on implementing dynamic tracking, or contact Parse.ly support for additional assistance.

### Cloudflare support

If the site is running behind a Cloudflare DNS, their Rocket Loader technology will alter how JavaScript files are loaded. [A JavaScript file can be ignored by Rocket Loader](https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts) by using `data-cfasync="false"`.

Previous versions of this plugin would mark all scripts with that tag by default. Starting in version 3.0, that behavior has become optional and scripts won't be annotated with `data-cfasync="false"`. The previous behavior can be restored by adding the following filter:

```
add_filter( 'wp_parsely_enable_cfasync_attribute', '__return_true' );
```

### How do I create a local dev environment to make changes to the `wp-parsely` code?

See [the wiki](https://github.com/Parsely/wp-parsely/wiki/Setting-up-a-WP-plugin-development-environment).
Expand Down
16 changes: 13 additions & 3 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,19 @@ public function script_loader_tag( string $tag, string $handle, string $src ): s
'wp-parsely-recommended-widget',
)
) ) {
// Have CloudFlare 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 /', '<script data-cfasync="false" ', $tag );
/**
* Filter whether to include the CloudFlare Rocket Loader attribute (`data-cfasync=false`) in the script.
* Only needed if the site being served is behind Cloudflare. Should return false otherwise.
* https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-specific-JavaScripts
*
* @since 3.0.0
*
* @param bool $enabled True if enabled, false if not.
* @param string $handle The script's registered handle.
*/
if ( apply_filters( 'wp_parsely_enable_cfasync_attribute', false, $handle ) ) {
$tag = preg_replace( '/^<script /', '<script data-cfasync="false" ', $tag );
}
}

if ( 'wp-parsely-tracker' === $handle ) {
Expand Down
38 changes: 36 additions & 2 deletions tests/Integration/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ public function test_load_js_tracker(): void {
wp_print_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",
$output,
'Failed to confirm script tag was printed correctly'
);
}

/**
* Test the tracker script enqueue.
*
* @covers \Parsely::load_js_tracker
* @uses \Parsely::get_asset_cache_buster
* @uses \Parsely::api_key_is_missing
* @uses \Parsely::api_key_is_set
* @uses \Parsely::get_options
* @uses \Parsely::post_has_trackable_status
* @uses \Parsely::register_js
* @uses \Parsely::script_loader_tag
* @uses \Parsely::update_metadata_endpoint
* @group insert-js
*/
public function test_load_js_tracker_with_cloudflare(): void {
add_filter( 'wp_parsely_enable_cfasync_attribute', '__return_true' );

ob_start();
$post_array = $this->create_test_post_array();
$post = $this->factory->post->create( $post_array );
$this->go_to( '/?p=' . $post );
self::$parsely->register_js();
self::$parsely->load_js_tracker();

wp_print_scripts();
$output = ob_get_clean();

self::assertSame(
"<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,
Expand Down Expand Up @@ -246,7 +280,7 @@ public function test_load_js_api_with_secret(): void {
);

self::assertStringContainsString(
"<script data-cfasync=\"false\" type='text/javascript' src='" . esc_url( plugin_dir_url( PARSELY_FILE ) ) . 'build/init-api.js?ver=' . \Parsely::VERSION . "' id='wp-parsely-api-js'></script>",
"<script type='text/javascript' src='" . esc_url( plugin_dir_url( PARSELY_FILE ) ) . 'build/init-api.js?ver=' . \Parsely::VERSION . "' id='wp-parsely-api-js'></script>",
$output,
'Failed to confirm script tag was printed correctly'
);
Expand Down Expand Up @@ -451,7 +485,7 @@ public function test_user_logged_in_multisite(): void {
$output = ob_get_clean();

self::assertSame(
"<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",
"<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",
$output,
'Failed to confirm script tags were printed correctly'
);
Expand Down