Skip to content

Commit 90b6eed

Browse files
authored
Merge pull request #8871 from google/dont-add-action-inside-action-8814
Ensure hooks for conversion tracking classes can be registered.
2 parents 59b19cf + bc6f924 commit 90b6eed

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,20 @@ public function register_script() {
6666

6767
$script->register( $this->context );
6868

69-
$this->add_purchase_event_hook();
70-
7169
return $script;
7270
}
7371

7472
/**
7573
* Adds a hook for a purchase event.
74+
*
75+
* @since 1.129.0
7676
*/
77-
public function add_purchase_event_hook() {
77+
public function register_hooks() {
78+
$input = $this->context->input();
79+
7880
add_action(
7981
'woocommerce_thankyou',
80-
function ( $order_id ) {
81-
// Don't output this script tag if conversion tracking is
82-
// disabled.
83-
if ( ! $this->is_active() ) {
84-
return;
85-
}
86-
82+
function ( $order_id ) use ( $input ) {
8783
$order = wc_get_order( $order_id );
8884

8985
// If there isn't a valid order for this ID, or if this order
@@ -95,7 +91,6 @@ function ( $order_id ) {
9591

9692
// Ensure the order key in the query param is valid for this
9793
// order.
98-
$input = $this->context->input();
9994
$order_key = $input->filter( INPUT_GET, 'key' );
10095

10196
// Don't output the script tag if the order key is invalid.
@@ -115,5 +110,4 @@ function ( $order_id ) {
115110
1
116111
);
117112
}
118-
119113
}

includes/Core/Conversion_Tracking/Conversion_Events_Provider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public function is_active() {
6262
*/
6363
abstract public function get_event_names();
6464

65+
/**
66+
* Registers any actions/hooks for this provider.
67+
*
68+
* @since 1.129.0
69+
*/
70+
public function register_hooks() {
71+
// No-op by default, but left here so subclasses can implement
72+
// their own `add_action`/hook calls.
73+
}
74+
6575
/**
6676
* Registers the script for the provider.
6777
*

includes/Core/Conversion_Tracking/Conversion_Tracking.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public function register() {
9393
$this->rest_conversion_tracking_controller->register();
9494

9595
add_action( 'wp_enqueue_scripts', fn () => $this->maybe_enqueue_scripts(), 30 );
96+
97+
$active_providers = $this->get_active_providers();
98+
99+
array_walk(
100+
$active_providers,
101+
function( Conversion_Events_Provider $active_provider ) {
102+
$active_provider->register_hooks();
103+
}
104+
);
96105
}
97106

98107
/**

tests/phpunit/includes/Core/Conversion_Tracking/Conversion_Event_Providers/FakeConversionEventProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ public function register_script() {
5050
return $script_asset;
5151
}
5252

53+
/**
54+
* Registers any actions/hooks for this provider.
55+
*
56+
* @since 1.129.0
57+
*/
58+
public function register_hooks() {
59+
// Register a fake action.
60+
add_action( 'fake_provider_action', '__return_true' );
61+
}
62+
5363
}

tests/phpunit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerceTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public function test_register_script() {
4949
$this->assertTrue( wp_script_is( $handle, 'registered' ) );
5050
}
5151

52+
public function test_register_hooks() {
53+
$this->assertFalse( has_action( 'woocommerce_thankyou' ) );
54+
$this->woocommerce->register_hooks();
55+
$this->assertTrue( has_action( 'woocommerce_thankyou' ) );
56+
}
57+
5258
}

tests/phpunit/integration/Core/Conversion_Tracking/Conversion_TrackingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,17 @@ public function test_register__not_enqueued_when_tracking_disabled() {
8282
* @dataProvider data_modules
8383
*/
8484
public function test_register__enqueued_when_snippet_inserted( $module_slug ) {
85+
$this->assertFalse( has_action( 'fake_provider_action' ) );
86+
8587
$this->conversion_tracking->register();
8688

8789
do_action( "googlesitekit_{$module_slug}_init_tag" );
8890
do_action( 'wp_enqueue_scripts' );
8991

9092
$this->assertTrue( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider_Active::CONVERSION_EVENT_PROVIDER_SLUG ) );
9193
$this->assertFalse( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider::CONVERSION_EVENT_PROVIDER_SLUG ) );
94+
95+
$this->assertTrue( has_action( 'fake_provider_action' ) );
9296
}
9397

9498
public function data_modules() {

0 commit comments

Comments
 (0)