Skip to content

Commit

Permalink
PHPCS
Browse files Browse the repository at this point in the history
  • Loading branch information
martynmjones committed Mar 4, 2024
1 parent f0cf45b commit b2def8a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 53 deletions.
24 changes: 13 additions & 11 deletions includes/class-wc-abstract-google-analytics-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ public function __construct() {
public function attach_event_data(): void {
add_action(
'wp_head',
function() {
function () {
$this->set_script_data( 'cart', $this->get_formatted_cart() );
}
);

add_action(
'woocommerce_before_single_product',
function() {
function () {
global $product;
$this->set_script_data( 'product', $this->get_formatted_product( $product ) );
}
);

add_action(
'woocommerce_add_to_cart',
function( $cart_item_key, $product_id, $quantity, $variation_id, $variation ) {
function ( $cart_item_key, $product_id, $quantity, $variation_id, $variation ) {
$this->set_script_data( 'added_to_cart', $this->get_formatted_product( wc_get_product( $product_id ), $variation ) );
},
10,
Expand All @@ -88,15 +88,15 @@ function( $cart_item_key, $product_id, $quantity, $variation_id, $variation ) {

add_action(
'woocommerce_shop_loop_item_title',
function() {
function () {
global $product;
$this->append_script_data( 'products', $this->get_formatted_product( $product ) );
}
);

add_action(
'woocommerce_thankyou',
function( $order_id ) {
function ( $order_id ) {
$this->set_script_data( 'order', $this->get_formatted_order( $order_id ) );
}
);
Expand Down Expand Up @@ -162,12 +162,12 @@ public static function get_product_identifier( WC_Product $product ): string {
public function get_formatted_cart(): array {
return array(
'items' => array_map(
function( $item ) {
function ( $item ) {
return array_merge(
$this->get_formatted_product( $item['data'] ),
array(
'quantity' => $item['quantity'],
'prices' => array(
'prices' => array(
'price' => $this->get_formatted_price( $item['line_total'] ),
'currency_minor_unit' => wc_get_price_decimals(),
),
Expand Down Expand Up @@ -220,7 +220,7 @@ public function get_formatted_product( WC_Product $product, $variation = false )
$formatted['variation'] = implode(
', ',
array_map(
function( $attribute, $value ) {
function ( $attribute, $value ) {
return sprintf(
'%s: %s',
str_replace( 'attribute_', '', $attribute ),
Expand All @@ -239,7 +239,7 @@ function( $attribute, $value ) {
/**
* Returns an array of order data in the required format
*
* @param string $order_id The ID of the order
* @param int $order_id The ID of the order
*
* @return array
*/
Expand All @@ -250,7 +250,7 @@ public function get_formatted_order( int $order_id ): array {
'currency' => $order->get_currency(),
'value' => $this->get_formatted_price( $order->get_total() ),
'items' => array_map(
function( $item ) {
function ( $item ) {
return array_merge(
$this->get_formatted_product( $item->get_product() ),
array(
Expand All @@ -266,6 +266,8 @@ function( $item ) {
/**
* Formats a price the same way WooCommerce Blocks does
*
* @param mixed $value The price value for format
*
* @return int
*/
public function get_formatted_price( $value ): int {
Expand Down Expand Up @@ -303,7 +305,7 @@ public function schema_callback(): array {
'description' => __( 'The formatted product identifier to use in Google Analytics events.', 'woocommerce-google-analytics-integration' ),
'type' => 'string',
'readonly' => true,
)
),
);
}

Expand Down
14 changes: 8 additions & 6 deletions includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ public function __construct() {

// Dequeue the WooCommerce Blocks Google Analytics integration,
// not to let it register its `gtag` function so that we could provide a more detailed configuration.
add_action( 'wp_enqueue_scripts', function() {
wp_dequeue_script( 'wc-blocks-google-analytics' );
});

add_action(
'wp_enqueue_scripts',
function () {
wp_dequeue_script( 'wc-blocks-google-analytics' );
}
);
}

/**
Expand All @@ -80,7 +82,7 @@ public function __construct() {
*/
public function universal_analytics_upgrade_notice() {
if ( 'ua' === substr( strtolower( $this->get_option( 'ga_id' ) ), 0, 2 ) ) {
echo sprintf(
printf(
'<div class="%1$s"><p>%2$s</p></div>',
'notice notice-error',
sprintf(
Expand Down Expand Up @@ -244,7 +246,7 @@ public function show_options_info() {
* @return array Updated WC Tracker data.
*/
public function track_settings( $data ) {
$settings = $this->settings;
$settings = $this->settings;
$data['wc-google-analytics'] = array(
'standard_tracking_enabled' => $settings['ga_standard_tracking_enabled'],
'support_display_advertising' => $settings['ga_support_display_advertising'],
Expand Down
26 changes: 15 additions & 11 deletions includes/class-wc-google-gtag-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class WC_Google_Gtag_JS extends WC_Abstract_Google_Analytics_JS {

/** @var array $mappings A map of the GA4 events and the classic WooCommerce hooks that trigger them */
private $mappings = array(
'begin_checkout' => 'woocommerce_before_checkout_form',
'purchase' => 'woocommerce_thankyou',
'view_item_list' => 'woocommerce_before_shop_loop_item',
'add_to_cart' => 'woocommerce_add_to_cart',
'remove_from_cart' => 'woocommerce_cart_item_removed',
'view_item' => 'woocommerce_after_single_product',
'begin_checkout' => 'woocommerce_before_checkout_form',
'purchase' => 'woocommerce_thankyou',
'view_item_list' => 'woocommerce_before_shop_loop_item',
'add_to_cart' => 'woocommerce_add_to_cart',
'remove_from_cart' => 'woocommerce_cart_item_removed',
'view_item' => 'woocommerce_after_single_product',
);

/**
Expand Down Expand Up @@ -97,6 +97,10 @@ public function inline_script_data(): void {
$this->data_script_handle,
'',
array( $this->script_handle ),
null,
array(
'in_footer' => true,
)
);

wp_add_inline_script(
Expand All @@ -118,11 +122,11 @@ public function inline_script_data(): void {
public function map_actions(): void {
array_walk(
$this->mappings,
function( $hook, $gtag_event ) {
function ( $hook, $gtag_event ) {
add_action(
$hook,
function() use ( $gtag_event ) {
if ( ! in_array( $gtag_event, $this->script_data['events'] ?? [] ) ) {
function () use ( $gtag_event ) {
if ( ! in_array( $gtag_event, $this->script_data['events'] ?? [], true ) ) {
$this->append_script_data( 'events', $gtag_event );
}
}
Expand Down Expand Up @@ -179,7 +183,7 @@ public static function tracker_function_name(): string {
/**
* Return Google Analytics configuration, for JS to read.
*
* @return void
* @return array
*/
public function get_analytics_config(): array {
return array(
Expand Down Expand Up @@ -220,7 +224,7 @@ public static function get_enabled_events(): array {
'begin_checkout' => 'ga_enhanced_checkout_process_enabled',
);

foreach( $settings as $event => $setting_name ) {
foreach ( $settings as $event => $setting_name ) {
if ( 'yes' === self::get( $setting_name ) ) {
$events[] = $event;
}
Expand Down
53 changes: 28 additions & 25 deletions tests/unit-tests/WCGoogleGtagJS.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function test_get_product_identifier() {

add_filter(
'woocommerce_ga_product_identifier',
function( $product ) {
function ( $product ) {
return 'filtered';
}
);
Expand All @@ -78,27 +78,27 @@ function( $product ) {
* @return void
*/
public function test_map_actions(): void {
$gtag = new WC_Google_Gtag_JS;
$gtag = new WC_Google_Gtag_JS();
$mappings = array(
'begin_checkout' => 'woocommerce_before_checkout_form',
'purchase' => 'woocommerce_thankyou',
'view_item_list' => 'woocommerce_before_shop_loop_item',
'add_to_cart' => 'woocommerce_add_to_cart',
'remove_from_cart' => 'woocommerce_cart_item_removed',
'view_item' => 'woocommerce_after_single_product',
'begin_checkout' => 'woocommerce_before_checkout_form',
'purchase' => 'woocommerce_thankyou',
'view_item_list' => 'woocommerce_before_shop_loop_item',
'add_to_cart' => 'woocommerce_add_to_cart',
'remove_from_cart' => 'woocommerce_cart_item_removed',
'view_item' => 'woocommerce_after_single_product',
);

array_map( 'remove_all_actions', $mappings );

$gtag->map_actions();

foreach( $mappings as $event => $hook ) {
foreach ( $mappings as $event => $hook ) {
do_action( $hook );

$script_data = json_decode( $gtag->get_script_data(), true );
$this->assertTrue( in_array( $event, $script_data['events'] ) );

$this->assertTrue( in_array( $event, $script_data['events'], true ) );

// Reset event data
$gtag->set_script_data( 'events', array() );
}
Expand All @@ -110,11 +110,11 @@ public function test_map_actions(): void {
* @return void
*/
public function test_set_script_data(): void {
$gtag = new WC_Google_Gtag_JS;
$gtag = new WC_Google_Gtag_JS();
$example_data = array(
'key' => 'value'
'key' => 'value',
);

$gtag->set_script_data( 'test', $example_data );

$script_data = json_decode( $gtag->get_script_data(), true );
Expand All @@ -127,8 +127,8 @@ public function test_set_script_data(): void {
* @return void
*/
public function test_append_script_data(): void {
$gtag = new WC_Google_Gtag_JS;
$gtag = new WC_Google_Gtag_JS();

$gtag->append_script_data( 'test', 'first' );
$gtag->append_script_data( 'test', 'second' );

Expand All @@ -143,13 +143,16 @@ public function test_append_script_data(): void {
* @return void
*/
public function test_tracker_var(): void {
$gtag = new WC_Google_Gtag_JS;
$gtag = new WC_Google_Gtag_JS();

$this->assertEquals( $gtag->tracker_function_name(), 'gtag' );

add_filter( 'woocommerce_gtag_tracker_variable', function( $var ) {
return 'filtered';
} );

add_filter(
'woocommerce_gtag_tracker_variable',
function ( $variable ) {
return 'filtered';
}
);
$this->assertEquals( $gtag->tracker_function_name(), 'filtered' );
}

Expand All @@ -169,7 +172,7 @@ public function test_get_enabled_events(): void {
'begin_checkout' => 'ga_enhanced_checkout_process_enabled',
);

foreach( $settings as $event => $option_name ) {
foreach ( $settings as $event => $option_name ) {
$gtag = new WC_Google_Gtag_JS( array( $option_name => 'yes' ) );
$this->assertEquals( $gtag->get_enabled_events(), array( $event ) );
}
Expand Down

0 comments on commit b2def8a

Please sign in to comment.