Skip to content

Commit

Permalink
Merge pull request #8871 from google/dont-add-action-inside-action-8814
Browse files Browse the repository at this point in the history
Ensure hooks for conversion tracking classes can be registered.
  • Loading branch information
kuasha420 authored Jun 14, 2024
2 parents 59b19cf + bc6f924 commit 90b6eed
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,20 @@ public function register_script() {

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

$this->add_purchase_event_hook();

return $script;
}

/**
* Adds a hook for a purchase event.
*
* @since 1.129.0
*/
public function add_purchase_event_hook() {
public function register_hooks() {
$input = $this->context->input();

add_action(
'woocommerce_thankyou',
function ( $order_id ) {
// Don't output this script tag if conversion tracking is
// disabled.
if ( ! $this->is_active() ) {
return;
}

function ( $order_id ) use ( $input ) {
$order = wc_get_order( $order_id );

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

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

// Don't output the script tag if the order key is invalid.
Expand All @@ -115,5 +110,4 @@ function ( $order_id ) {
1
);
}

}
10 changes: 10 additions & 0 deletions includes/Core/Conversion_Tracking/Conversion_Events_Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public function is_active() {
*/
abstract public function get_event_names();

/**
* Registers any actions/hooks for this provider.
*
* @since 1.129.0
*/
public function register_hooks() {
// No-op by default, but left here so subclasses can implement
// their own `add_action`/hook calls.
}

/**
* Registers the script for the provider.
*
Expand Down
9 changes: 9 additions & 0 deletions includes/Core/Conversion_Tracking/Conversion_Tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public function register() {
$this->rest_conversion_tracking_controller->register();

add_action( 'wp_enqueue_scripts', fn () => $this->maybe_enqueue_scripts(), 30 );

$active_providers = $this->get_active_providers();

array_walk(
$active_providers,
function( Conversion_Events_Provider $active_provider ) {
$active_provider->register_hooks();
}
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public function register_script() {
return $script_asset;
}

/**
* Registers any actions/hooks for this provider.
*
* @since 1.129.0
*/
public function register_hooks() {
// Register a fake action.
add_action( 'fake_provider_action', '__return_true' );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public function test_register_script() {
$this->assertTrue( wp_script_is( $handle, 'registered' ) );
}

public function test_register_hooks() {
$this->assertFalse( has_action( 'woocommerce_thankyou' ) );
$this->woocommerce->register_hooks();
$this->assertTrue( has_action( 'woocommerce_thankyou' ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ public function test_register__not_enqueued_when_tracking_disabled() {
* @dataProvider data_modules
*/
public function test_register__enqueued_when_snippet_inserted( $module_slug ) {
$this->assertFalse( has_action( 'fake_provider_action' ) );

$this->conversion_tracking->register();

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

$this->assertTrue( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider_Active::CONVERSION_EVENT_PROVIDER_SLUG ) );
$this->assertFalse( wp_script_is( 'gsk-cep-' . FakeConversionEventProvider::CONVERSION_EVENT_PROVIDER_SLUG ) );

$this->assertTrue( has_action( 'fake_provider_action' ) );
}

public function data_modules() {
Expand Down

0 comments on commit 90b6eed

Please sign in to comment.