Skip to content

Commit

Permalink
Closes #6152: Add license type to what is reported to Mixpanel
Browse files Browse the repository at this point in the history
Adds the license type to data sent to Mixpanel.
  • Loading branch information
Gael Robin committed Sep 27, 2023
1 parent 539ecea commit 9917482
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 19 deletions.
20 changes: 1 addition & 19 deletions inc/Engine/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,25 +260,7 @@ public function customer_data() {
'is_from_one_dot_com' => false,
];

if (
false === $user
||
! isset( $user->licence_account, $user->licence_expiration )
) {
return $data;
}

if (
1 <= $user->licence_account
&&
$user->licence_account < 3
) {
$data['license_type'] = 'Single';
} elseif ( -1 === (int) $user->licence_account ) {
$data['license_type'] = 'Infinite';
} else {
$data['license_type'] = 'Plus';
}
$data['license_type'] = rocket_get_license_type( $user );

$data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid';
$data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration );
Expand Down
5 changes: 5 additions & 0 deletions inc/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ function rocket_analytics_data() {
$data['cdn_cnames'] = 0;
}

$customer_data = get_transient( 'wp_rocket_customer_data' );
if ( false !== $customer_data ) {
$data['license_type'] = rocket_get_license_type( $customer_data );
}

return $data;
}

Expand Down
26 changes: 26 additions & 0 deletions inc/functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,29 @@ function rocket_create_options_hash( $value ) {

return md5( wp_json_encode( $value_diff ) );
}

/**
* This function returns the license type for a customer.
*
* @param object $customer_data customer data as an object.
* @return string the type of the license the user has.
*/
function rocket_get_license_type( $customer_data ) {
if ( false === $customer_data
||
! isset( $customer_data->licence_account )
) {
return __( 'Unavailable', 'rocket' );
}

if ( 1 <= $customer_data->licence_account
&&
$customer_data->licence_account < 3
) {
return 'Single';
} elseif ( -1 === (int) $customer_data->licence_account ) {
return 'Infinite';
}

return 'Plus';
}
28 changes: 28 additions & 0 deletions tests/Fixtures/inc/functions/admin/rocketGetLicenseType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

return [
'singleLicense' => [
'config' => [
'customer_data' => (object)['licence_account' => 1],
],
'expected' => 'Single',
],
'infiniteLicense' => [
'config' => [
'customer_data' => (object)['licence_account' => -1],
],
'expected' => 'Infinite',
],
'plusLicense' => [
'config' => [
'customer_data' => (object)['licence_account' => 3],
],
'expected' => 'Plus',
],
'unavailableLicense' => [
'config' => [
'customer_data' => null,
],
'expected' => 'Unavailable',
],
];
24 changes: 24 additions & 0 deletions tests/Unit/inc/functions/admin/rocketGetLicenseType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace WP_Rocket\Tests\Unit\inc\functions\admin;

use WP_Rocket\Tests\Unit\TestCase;

class Test_RocketGetLicenseType extends TestCase {

public function setUp(): void {
parent::setUp();
}

/**
* @dataProvider configTestData
*/
public function testReturnAsExpected($config, $expected) {
$this->stubTranslationFunctions();
// Extract 'customer_data' from the config
$customer_data = $config['customer_data'];
$result = rocket_get_license_type($customer_data);

$this->assertEquals($expected, $result);
}
}

0 comments on commit 9917482

Please sign in to comment.