Move tracks _ui and _ut properties out of filtered tracks properties (#33621)

* Move tracks _ui and _ut properties out of filtered tracks properties

* Add tests around tracks properties

* Fix broken variable name

* Add changelog entry
This commit is contained in:
Joshua T Flowers 2022-06-28 09:43:17 -04:00 committed by GitHub
parent b178e8a1f6
commit 0a5b912288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 13 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Move tracks _ui and _ut properties out of filtered tracks properties #33621

View File

@ -262,9 +262,8 @@ class WC_Site_Tracking {
: array();
$server_details = self::get_server_details();
$identity = WC_Tracks_Client::get_identity( $user->ID );
$blog_details = self::get_blog_details( $user->ID );
return array_merge( $data, $properties, $server_details, $identity, $blog_details );
return array_merge( $data, $properties, $server_details, $blog_details );
}
}

View File

@ -20,10 +20,10 @@ class WC_Tracks {
* Note: the event request won't be made if $properties has a member called `error`.
*
* @param string $event_name The name of the event.
* @param array $properties Custom properties to send with the event.
* @param array $event_properties Custom properties to send with the event.
* @return bool|WP_Error True for success or WP_Error if the event pixel could not be fired.
*/
public static function record_event( $event_name, $properties = array() ) {
public static function record_event( $event_name, $event_properties = array() ) {
/**
* Don't track users who don't have tracking enabled.
*/
@ -38,15 +38,8 @@ class WC_Tracks {
return false;
}
$prefixed_event_name = self::PREFIX . $event_name;
// Allow event props to be filtered to enable adding site-wide props.
$filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $properties, $prefixed_event_name );
// Delete _ui and _ut protected properties.
unset( $filtered_properties['_ui'] );
unset( $filtered_properties['_ut'] );
$event_obj = new WC_Tracks_Event( $filtered_properties );
$properties = self::get_properties( $prefixed_event_name, $event_properties );
$event_obj = new WC_Tracks_Event( $properties );
if ( is_wp_error( $event_obj->error ) ) {
return $event_obj->error;
@ -54,4 +47,28 @@ class WC_Tracks {
return $event_obj->record();
}
/**
* Get all properties for the event including filtered and identity properties.
*
* @param string $event_name Event name.
* @param array $event_properties Event specific properties.
* @return array
*/
public static function get_properties( $event_name, $event_properties ) {
/**
* Allow event props to be filtered to enable adding site-wide props.
*
* @since 4.1.0
*/
$properties = apply_filters( 'woocommerce_tracks_event_properties', $event_properties, $event_name );
$user = wp_get_current_user();
$identity = WC_Tracks_Client::get_identity( $user->ID );
// Delete _ui and _ut protected properties.
unset( $properties['_ui'] );
unset( $properties['_ut'] );
return array_merge( $properties, $identity );
}
}

View File

@ -0,0 +1,63 @@
<?php
/**
* Class WC_Tracks_Test.
*/
class WC_Tracks_Test extends \WC_Unit_Test_Case {
/**
* Set up test
*
* @return void
*/
public function setUp(): void {
parent::setUp();
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks.php';
include_once WC_ABSPATH . 'includes/tracks/class-wc-tracks-client.php';
}
/**
* Test that custom event properties are returned when passed.
*/
public function test_get_properties() {
$properties = \WC_Tracks::get_properties(
'test_event',
array(
'test_property' => 5,
)
);
$this->assertContains( 'test_property', array_keys( $properties ) );
$this->assertEquals( 5, $properties['test_property'] );
}
/**
* Test that identity properties are added to the properties.
*/
public function test_addition_of_identity() {
$properties = \WC_Tracks::get_properties(
'test_event',
array(
'test_property' => 5,
)
);
$this->assertContains( '_ui', array_keys( $properties ) );
$this->assertContains( '_ut', array_keys( $properties ) );
}
/**
* Test that custom identity properties cannot be added.
*/
public function test_invalid_identity() {
$properties = \WC_Tracks::get_properties(
'test_event',
array(
'_ui' => 'bad',
'_ut' => 'bad',
)
);
$this->assertNotEquals( 'bad', $properties['_ui'] );
$this->assertNotEquals( 'bad', $properties['_ut'] );
}
}