From 7abd9fefbb9cb9961eea497948c3fef940fce9aa Mon Sep 17 00:00:00 2001 From: Josh Betz Date: Mon, 3 Mar 2025 20:55:40 -0600 Subject: [PATCH 1/4] feat: defer plausible-analytics script Since WordPress 6.3, we've been able to defer scripts. The plausible-analytic script is a good candidate for this. https://make.wordpress.org/core/2023/07/14/registering-scripts-with-async-and-defer-attributes-in-wordpress-6-3/ --- src/Actions.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Actions.php b/src/Actions.php index 257f1ff..77110f7 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -60,7 +60,10 @@ public function maybe_register_assets() { Helpers::get_js_url( true ), '', $version, - apply_filters( 'plausible_load_js_in_footer', false ) + [ + 'in_footer' => apply_filters( 'plausible_load_js_in_footer', false ), + 'strategy' => 'defer', + ] ); // Goal tracking inline script (Don't disable this as it is required by 404). From 95193170ecd9551db0d10a615777425ade7a88db Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Wed, 5 Mar 2025 23:58:58 +0100 Subject: [PATCH 2/4] Fixed: Uncaught Error: Call to a member function enable_custom_property() on bool if no token was entered. --- src/Admin/Upgrades.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Admin/Upgrades.php b/src/Admin/Upgrades.php index 6ee7206..5639cf7 100644 --- a/src/Admin/Upgrades.php +++ b/src/Admin/Upgrades.php @@ -11,6 +11,7 @@ use Exception; use Plausible\Analytics\WP\Admin\Provisioning\Integrations; +use Plausible\Analytics\WP\Client; use Plausible\Analytics\WP\Helpers; /** @@ -292,6 +293,11 @@ public function upgrade_to_230() { $edd_provisioning = new Provisioning\Integrations\EDD( new Integrations() ); $provisioning = new Provisioning(); + // No token entered. + if ( ! $provisioning->client instanceof Client ) { + return; + } + $provisioning->maybe_create_custom_properties( [], $settings ); $edd_provisioning->maybe_create_edd_funnel( [], $settings ); } From 62b2918161a95867e854bd067b0d21fcf4cacdfb Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:57:53 +0100 Subject: [PATCH 3/4] Remove defer attribute already present. --- src/Filters.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filters.php b/src/Filters.php index 8255123..b8e9178 100644 --- a/src/Filters.php +++ b/src/Filters.php @@ -56,7 +56,7 @@ public function add_plausible_attributes( $tag, $handle ) { /** * 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/ */ - $params = "defer data-domain='{$domain_name}' data-api='{$api_url}' data-cfasync='false'"; + $params = "data-domain='{$domain_name}' data-api='{$api_url}' data-cfasync='false'"; // Triggered when exclude pages is enabled. if ( ! empty( $settings[ 'excluded_pages' ] ) && $settings[ 'excluded_pages' ] ) { From fe4dfdfde6192be0b13f346229364cf12e4d2e3a Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Thu, 13 Mar 2025 14:58:00 +0100 Subject: [PATCH 4/4] Formatting. --- src/Actions.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Actions.php b/src/Actions.php index 77110f7..3c0b12b 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -46,14 +46,11 @@ public function maybe_register_assets() { * Bail if tracked_user_roles is empty (which means no roles should be tracked) or, * if current role should not be tracked. */ - if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) || - ( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) { + if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) || ( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) { return; // @codeCoverageIgnore } - $version = - Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) : - PLAUSIBLE_ANALYTICS_VERSION; + $version = Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) : PLAUSIBLE_ANALYTICS_VERSION; wp_enqueue_script( 'plausible-analytics', @@ -62,7 +59,7 @@ public function maybe_register_assets() { $version, [ 'in_footer' => apply_filters( 'plausible_load_js_in_footer', false ), - 'strategy' => 'defer', + 'strategy' => 'defer', ] ); @@ -134,13 +131,13 @@ public function admin_bar_node( $admin_bar ) { return; // @codeCoverageIgnore } - $settings = Helpers::get_settings(); + $settings = Helpers::get_settings(); $current_user = wp_get_current_user(); $has_access = false; $user_roles_have_access = array_merge( [ 'administrator' ], - $settings['expand_dashboard_access'] ?? [] + $settings[ 'expand_dashboard_access' ] ?? [] ); foreach ( $current_user->roles as $role ) { @@ -160,9 +157,7 @@ public function admin_bar_node( $admin_bar ) { 'title' => 'Plausible Analytics', ]; - - if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || - ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) { + if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) { $args[] = [ 'id' => 'view-analytics', 'title' => esc_html__( 'View Analytics', 'plausible-analytics' ),