Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate UUID v4 for telemetry ID #1693

Merged
merged 12 commits into from
Jan 2, 2024
Merged
3 changes: 2 additions & 1 deletion plugins/faustwp/includes/rest/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use function WPE\FaustWP\Blocks\handle_uploaded_blockset;
use function WPE\FaustWP\Settings\faustwp_get_setting;
use function WPE\FaustWP\Settings\faustwp_update_setting;
use function WPE\FaustWP\Settings\is_telemetry_enabled;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -219,7 +220,7 @@ function handle_rest_telemetry_callback( \WP_REST_Request $request ) {
* @return mixed A \WP_REST_Response, array, or \WP_Error.
*/
function handle_rest_process_telemetry_callback( \WP_REST_Request $request ) {
if ( ! get_telemetry_client_id() ) {
if ( ! is_telemetry_enabled() ) {
return new \WP_REST_Response( null, 204 );
}

Expand Down
12 changes: 10 additions & 2 deletions plugins/faustwp/includes/settings/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace WPE\FaustWP\Settings;

use function WPE\FaustWP\Telemetry\generate_telemetry_client_id;
use function WPE\FaustWP\Telemetry\get_telemetry_client_id;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand Down Expand Up @@ -148,8 +151,9 @@ function faustwp_get_settings() {
* @return void
*/
function maybe_set_default_settings() {
$secret_key = get_secret_key();
$settings = faustwp_get_settings();
$secret_key = get_secret_key();
$settings = faustwp_get_settings();
$telemetry_client_id = get_telemetry_client_id();

if ( empty( $settings ) ) {
faustwp_update_setting( 'disable_theme', '0' );
Expand All @@ -166,6 +170,10 @@ function maybe_set_default_settings() {
if ( ! $secret_key ) {
faustwp_update_setting( 'secret_key', wp_generate_uuid4() );
}

if ( ! $telemetry_client_id ) {
generate_telemetry_client_id();
}
}

/**
Expand Down
29 changes: 16 additions & 13 deletions plugins/faustwp/includes/telemetry/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
is_themes_disabled,
is_image_source_replacement_enabled,
faustwp_get_setting,
faustwp_update_setting,
};

if ( ! defined( 'ABSPATH' ) ) {
Expand Down Expand Up @@ -107,18 +108,20 @@ function get_wpgraphql_content_blocks_plugin_version() {
/**
* Returns the anonymous client id for this site that has opted in for telemetry.
*
* @return string|null
* @return string
*/
function get_telemetry_client_id(): string {
// Use the default fallback param to generate and save the uuid if not already saved.
return faustwp_get_setting( 'telemetry_uuid', generate_telemetry_client_id() );
}

/**
* Generates a random uuidv4 and saves it for use with telemetry collection.
*
* @return string
*/
function get_telemetry_client_id(): string|null {
/**
* Upon saving the site's telemetry decision, if they accept, we'll
* also need to generate a unique, anonymous client ID for them to be sent
* with GA requests.
*
* If a string is returned, telemetry is enabled and a client id has been generated.
* If this function returns null, either telemetry is off, or a client ID is not created.
*
* @TODO
*/
return null;
function generate_telemetry_client_id(): string {
$id = wp_generate_uuid4();
faustwp_update_setting( 'telemetry_client_id', $id );
return $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use \WP_UnitTestCase;
use function WPE\FaustWP\Settings\get_secret_key;

class ProcessTelemetryRouteTest extends WP_UnitTestCase
class ProcessTelemetryRouteTests extends WP_UnitTestCase
{
private $request;
private $route_name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package FaustWP
*/

namespace WPE\FaustWP\Tests\Unit;
namespace WPE\FaustWP\Tests\Integration;

use \WP_UnitTestCase;
use function \wp_set_current_user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
};
use function WPE\FaustWP\Telemetry\{
has_frontend_uri,
generate_telemetry_client_id,
get_telemetry_client_id,
};

class TelemetryFunctionsTests extends WP_UnitTestCase {
Expand All @@ -33,4 +35,11 @@ public function test_has_frontend_uri_returns_true_if_frontend_uri_setting_has_v
$this->assertTrue( has_frontend_uri() );
}

public function test_generate_telemetry_client_id_generates_and_saves_a_valid_id_when_one_is_not_present(): void {
delete_option( 'faustwp_settings' );
self::assertNotEmpty( generate_telemetry_client_id() );
$id = get_telemetry_client_id();
self::assertTrue( wp_is_uuid( $id ) );
}

}
Loading