Skip to content

Commit 035dc70

Browse files
authored
Merge pull request #308 from plausible/not_in_footer
Fixed: add missing $in_footer parameter to wp_enqueue/register_script calls
2 parents 7a58f92 + 0cc6432 commit 035dc70

5 files changed

Lines changed: 44 additions & 40 deletions

File tree

.github/workflows/push.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ jobs:
9191
with:
9292
token: ${{ secrets.CODECOV_TOKEN }}
9393
slug: plausible/wordpress
94+
continue-on-error: true

src/Assets.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function maybe_enqueue_cloaked_affiliate_links_assets() {
4040
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-affiliate-links.js',
4141
[ 'plausible-analytics' ],
4242
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-affiliate-links.js' ),
43+
[ 'in_footer' => true ],
4344
);
4445

4546
$affiliate_links = Helpers::get_settings()['affiliate_links'] ?? [];

src/Integrations/FormSubmit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function add_js() {
5858
'plausible-form-submit-integration',
5959
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-form-submit-integration.js',
6060
[ 'plausible-analytics' ],
61-
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-form-submit-integration.js' )
61+
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-form-submit-integration.js' ),
62+
[ 'in_footer' => true ],
6263
);
6364

6465
wp_localize_script(

src/Integrations/WooCommerce.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public function add_js() {
118118
'plausible-woocommerce-integration',
119119
PLAUSIBLE_ANALYTICS_PLUGIN_URL . 'assets/dist/js/plausible-woocommerce-integration.js',
120120
[],
121-
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-woocommerce-integration.js' )
121+
filemtime( PLAUSIBLE_ANALYTICS_PLUGIN_DIR . 'assets/dist/js/plausible-woocommerce-integration.js' ),
122+
[ 'in_footer' => true ],
122123
);
123124
}
124125

tests/integration/AssetsTest.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
class AssetsTest extends TestCase {
1212
/**
13+
* @see Assets::maybe_enqueue_cloaked_affiliate_links_assets()
1314
* @return void
1415
* @throws \Exception
15-
* @see Assets::maybe_enqueue_main_script()
1616
*/
17-
public function testEnqueueMainScript() {
17+
public function testEnqueueCloakedAffiliateLinksScript() {
1818
try {
19-
add_filter( 'plausible_analytics_settings', [ $this, 'enableAdministratorTracking' ] );
19+
add_filter( 'plausible_analytics_settings', [ $this, 'enableCloakedAffiliateLinks' ] );
2020

2121
$class = $this->getMockBuilder( Assets::class )
2222
->disableOriginalConstructor()
@@ -29,94 +29,94 @@ public function testEnqueueMainScript() {
2929
$class->method( 'get_js_url' )
3030
->willReturn( 'https://plausible.test/js/plausible.js' );
3131

32-
wp_set_current_user( 1 );
33-
$user = wp_get_current_user();
34-
$user->add_role( 'administrator' );
32+
ob_start();
3533

3634
$class->maybe_enqueue_main_script();
35+
$class->maybe_enqueue_cloaked_affiliate_links_assets();
3736

38-
global $wp_scripts;
39-
$data = $wp_scripts->get_data( 'plausible-analytics', 'after' );
37+
do_action( 'wp_footer' );
4038

41-
$this->assertStringContainsString( 'window.plausible', implode( '', $data ) );
42-
$this->assertStringContainsString( 'plausible.init', implode( '', $data ) );
39+
$output = ob_get_clean();
40+
41+
$this->assertStringContainsString( 'plausible-affiliate-links.js', $output );
42+
$this->assertStringContainsString( 'const plausibleAffiliateLinks', $output );
4343
} finally {
44-
remove_filter( 'plausible_analytics_settings', [ $this, 'enableAdministratorTracking' ] );
45-
wp_set_current_user( null );
44+
remove_filter( 'plausible_analytics_settings', [ $this, 'enableCloakedAffiliateLinks' ] );
4645
}
4746
}
4847

4948
/**
49+
* @see Assets::maybe_enqueue_four_o_four_script()
5050
* @return void
5151
* @throws \Exception
52-
* @see Assets::maybe_enqueue_cloaked_affiliate_links_assets()
5352
*/
54-
public function testEnqueueCloakedAffiliateLinksScript() {
53+
public function testEnqueueFourOFourScript() {
5554
try {
56-
add_filter( 'plausible_analytics_settings', [ $this, 'enableCloakedAffiliateLinks' ] );
55+
add_filter( 'plausible_analytics_settings', [ $this, 'enableFourOFour' ] );
56+
add_filter( 'plausible_analytics_is_404', '__return_true' );
5757

5858
$class = $this->getMockBuilder( Assets::class )
5959
->disableOriginalConstructor()
6060
->onlyMethods( [ 'get_js_url' ] )
6161
->getMock();
6262

63-
$this->removeAction( 'wp_enqueue_scripts', 'maybe_enqueue' );
64-
$this->removeAction( 'wp_enqueue_scripts', 'maybe_enqueue', 11 );
65-
6663
$class->method( 'get_js_url' )
6764
->willReturn( 'https://plausible.test/js/plausible.js' );
6865

69-
ob_start();
70-
7166
$class->maybe_enqueue_main_script();
72-
$class->maybe_enqueue_cloaked_affiliate_links_assets();
73-
74-
do_action( 'wp_head' );
67+
$class->maybe_enqueue_four_o_four_script();
7568

76-
$output = ob_get_clean();
69+
global $wp_scripts;
7770

78-
$this->assertStringContainsString( 'plausible-affiliate-links.js', $output );
79-
$this->assertStringContainsString( 'const plausibleAffiliateLinks', $output );
71+
$this->assertArrayHasKey( 'plausible-analytics', $wp_scripts->registered );
72+
$this->assertTrue( $this->arrayHasString( '404', $wp_scripts->registered['plausible-analytics']->extra['after'] ) );
8073
} finally {
81-
remove_filter( 'plausible_analytics_settings', [ $this, 'enableCloakedAffiliateLinks' ] );
74+
remove_filter( 'plausible_analytics_settings', [ $this, 'enableFourOFour' ] );
75+
remove_filter( 'plausible_analytics_is_404', '__return_true' );
8276
}
8377
}
8478

8579
/**
80+
* @see Assets::maybe_enqueue_main_script()
8681
* @return void
8782
* @throws \Exception
88-
* @see Assets::maybe_enqueue_four_o_four_script()
8983
*/
90-
public function testEnqueueFourOFourScript() {
84+
public function testEnqueueMainScript() {
9185
try {
92-
add_filter( 'plausible_analytics_settings', [ $this, 'enableFourOFour' ] );
93-
add_filter( 'plausible_analytics_is_404', '__return_true' );
86+
add_filter( 'plausible_analytics_settings', [ $this, 'enableAdministratorTracking' ] );
9487

9588
$class = $this->getMockBuilder( Assets::class )
9689
->disableOriginalConstructor()
9790
->onlyMethods( [ 'get_js_url' ] )
9891
->getMock();
9992

93+
$this->removeAction( 'wp_enqueue_scripts', 'maybe_enqueue' );
94+
$this->removeAction( 'wp_enqueue_scripts', 'maybe_enqueue', 11 );
95+
10096
$class->method( 'get_js_url' )
10197
->willReturn( 'https://plausible.test/js/plausible.js' );
10298

99+
wp_set_current_user( 1 );
100+
$user = wp_get_current_user();
101+
$user->add_role( 'administrator' );
102+
103103
$class->maybe_enqueue_main_script();
104-
$class->maybe_enqueue_four_o_four_script();
105104

106105
global $wp_scripts;
106+
$data = $wp_scripts->get_data( 'plausible-analytics', 'after' );
107107

108-
$this->assertArrayHasKey( 'plausible-analytics', $wp_scripts->registered );
109-
$this->assertTrue( $this->arrayHasString( '404', $wp_scripts->registered['plausible-analytics']->extra['after'] ) );
108+
$this->assertStringContainsString( 'window.plausible', implode( '', $data ) );
109+
$this->assertStringContainsString( 'plausible.init', implode( '', $data ) );
110110
} finally {
111-
remove_filter( 'plausible_analytics_settings', [ $this, 'enableFourOFour' ] );
112-
remove_filter( 'plausible_analytics_is_404', '__return_true' );
111+
remove_filter( 'plausible_analytics_settings', [ $this, 'enableAdministratorTracking' ] );
112+
wp_set_current_user( null );
113113
}
114114
}
115115

116116
/**
117+
* @see Assets::maybe_enqueue_query_params_script()
117118
* @return void
118119
* @throws \Exception
119-
* @see Assets::maybe_enqueue_query_params_script()
120120
*/
121121
public function testEnqueueQueryParamsScript() {
122122
try {
@@ -146,9 +146,9 @@ public function testEnqueueQueryParamsScript() {
146146
}
147147

148148
/**
149+
* @see Assets::maybe_enqueue_search_queries_script()
149150
* @return void
150151
* @throws \Exception
151-
* @see Assets::maybe_enqueue_search_queries_script()
152152
*/
153153
public function testEnqueueSearchQueriesScript() {
154154
try {

0 commit comments

Comments
 (0)