Skip to content
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
20 changes: 5 additions & 15 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ private function init() {
/**
* Register main JS if this user should be tracked.
*
* @return void
* @throws \Exception
* @since 1.0.0
* @access public
* @return void
* @throws \Exception
*/
public function maybe_enqueue_main_script() {
$settings = Helpers::get_settings();
Expand All @@ -44,7 +44,7 @@ public function maybe_enqueue_main_script() {
/**
* This is a dummy script that will allow us to attach inline scripts further down the line.
*/
wp_register_script( 'plausible-analytics', '' );
wp_register_script( 'plausible-analytics', $this->get_js_url( true ), [], null, apply_filters( 'plausible_load_js_in_footer', false ) );

/**
* Bail if tracked_user_roles is empty (which means no roles should be tracked) or if the current role should not be tracked.
Expand All @@ -53,19 +53,9 @@ public function maybe_enqueue_main_script() {
return; // @codeCoverageIgnore
}

wp_enqueue_script(
'plausible-analytics',
'',
[],
null,
apply_filters( 'plausible_load_js_in_footer', false )
);
wp_enqueue_script( 'plausible-analytics' );

$url = $this->get_js_url( true );
$script = sprintf(
'window.plausible=window.plausible||function(){(window.plausible.q=window.plausible.q||[]).push(arguments)},window.plausible.init=function(i){window.plausible.o=i||{}};var script=document.createElement("script");script.type="text/javascript",script.defer=!0,script.src="%s";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(script,r);',
$url
);
$script = 'window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};';
$options = wp_json_encode( apply_filters( 'plausible_analytics_init_options', [] ) );
// transformRequest and customProperties can contain a JS function.
$options = preg_replace(
Expand Down
47 changes: 38 additions & 9 deletions src/Compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function __construct() {
// Cloudflare Rocket Loader
add_filter( 'script_loader_tag', [ $this, 'exclude_from_cloudflare_rocket_loader' ], 10, 2 );

// Global Exclusion from Minification
add_filter( 'plausible_analytics_script_params', [ $this, 'exclude_from_minification' ] );

// LiteSpeed Cache
if ( defined( 'LSCWP_V' ) ) {
add_filter( 'litespeed_optimize_js_excludes', [ $this, 'exclude_plausible_js' ] );
Expand Down Expand Up @@ -83,6 +86,7 @@ public function __construct() {
* @param mixed $exclude_js
*
* @return string
* @throws Exception
*/
public function exclude_plausible_js_as_string( $exclude_js ) {
$exclude_js .= ', window.plausible, ' . Helpers::get_js_url( true );
Expand All @@ -93,22 +97,21 @@ public function exclude_plausible_js_as_string( $exclude_js ) {
/**
* Add Plausible Analytics attributes.
*
* @param string $handle Script handle.
* @param string $tag Script tag.
*
* @return string
* @since 1.0.0
* @access public
*
* @param string $tag Script tag.
*
* @param string $handle Script handle.
*
* @return string
*/
public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) {
// Bail if it's not our script.
if ( 'plausible-analytics' !== $handle ) {
return $tag; // @codeCoverageIgnore
}

$settings = Helpers::get_settings();

/**
* the data-cfasync ensures this script isn't processed by CF Rocket Loader @see https://developers.cloudflare.com/speed/optimization/content/rocket-loader/ignore-javascripts/
*/
Expand All @@ -118,6 +121,30 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) {
return str_replace( ' src', " {$params} src", $tag );
}

/**
* Append attributes to the script tag, to make sure optimization plugins who respect this attribute don't minify our script.
*
* "data-no-minify" is respected by:
* - WP Rocket
* - WP Optimize
* - WP Fastest Cache
*
* "data-no-optimize" is respected by:
* - LiteSpeed Cache
*
* "data-noptimize" is respected by:
* - Autoptimize
*
* @param $params
*
* @return string
*/
public function exclude_from_minification( $params ) {
$params .= ' data-no-minify="true" data-no-optimize="1" data-noptimize="1"';

return $params;
}

/**
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine our inline JS, please.
*
Expand All @@ -129,7 +156,7 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) {
* @return array
*/
public function exclude_plausible_inline_js( $inline_js ) {
$inline_js[ 'plausible' ] = 'window.plausible';
$inline_js['plausible'] = 'window.plausible';

return $inline_js;
}
Expand All @@ -144,6 +171,7 @@ public function exclude_plausible_inline_js( $inline_js ) {
* @param array $excluded_js
*
* @return array
* @throws Exception
*/
public function exclude_plausible_js( $excluded_js ) {
$excluded_js[] = Helpers::get_js_url( true );
Expand Down Expand Up @@ -181,6 +209,7 @@ function ( $tag ) {
* @param array $excluded_js
*
* @return array
* @throws Exception
*/
public function exclude_plausible_js_by_relative_url( $excluded_js ) {
$excluded_js[] = preg_replace( '/http[s]?:\/\/.*?(\/)/', '$1', Helpers::get_js_url( true ) );
Expand Down Expand Up @@ -213,7 +242,7 @@ public function exclude_by_proxy_endpoint( $excluded_js ) {
* @filter rocket_minify_excluded_external_js
* @since 1.2.5
*
* @param array $excluded_js
* @param $excluded_handles
*
* @return array
*/
Expand All @@ -239,7 +268,7 @@ public function exclude_js_by_handle( $excluded_handles ) {
public function multilingual_compatibility( $url ) {
$rest_endpoint = Helpers::get_rest_endpoint( false );

if ( strpos( $url, $rest_endpoint ) !== false ) {
if ( str_contains( $url, $rest_endpoint ) ) {
return get_option( 'home' ) . $rest_endpoint;
}

Expand Down
Loading