Skip to content

Commit

Permalink
Generate UUID v4 for telemetry ID (#1693)
Browse files Browse the repository at this point in the history
* Generate UUID v4 for telemetry ID

* chore: change test namespace from Unit to Integration

* chore: fix test class name to match file name

* fix: return 204 if telemetry not enabled

* test: confirm behavior of `generate_telemetry_client_id()`

* Support `telemetry_client_id` in settings sanitization function

* test: confirm generated and saved id matches retrieved id.

* fix: add telemetry id to settings page as hidden input.

If this ID is missing from the $_POST data when the settings page is saved, it gets removed. This ensures it's always in the saved payload.
  • Loading branch information
mindctrl authored Jan 2, 2024
1 parent 17df6c2 commit b6c1967
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
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
8 changes: 8 additions & 0 deletions plugins/faustwp/includes/settings/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace WPE\FaustWP\Settings;

use function WPE\FaustWP\Telemetry\get_telemetry_client_id;
use function WPE\FaustWP\Utilities\{
plugin_version,
};
Expand Down Expand Up @@ -239,6 +240,12 @@ function sanitize_faustwp_settings( $settings, $option ) {
}
break;

case 'telemetry_client_id':
if ( $value ) {
$settings[ $name ] = sanitize_text_field( $value );
}
break;

default:
// Remove any settings we don't expect.
unset( $settings[ $name ] );
Expand Down Expand Up @@ -442,6 +449,7 @@ function display_enable_disable_fields() {
<input type="checkbox" id="enable_telemetry" name="faustwp_settings[enable_telemetry]" value="1" <?php checked( $enable_telemetry ); ?> />
<?php esc_html_e( 'Enable anonymous telemetry', 'faustwp' ); ?>
</label>
<input type="hidden" id="telemetry_client_id" name="faustwp_settings[telemetry_client_id]" value="<?php echo esc_attr( get_telemetry_client_id() ); ?>" />
</fieldset>
<?php
}
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
34 changes: 21 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,25 @@ 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.
$id = faustwp_get_setting( 'telemetry_client_id', false );
if ( empty( $id ) ) {
$id = generate_telemetry_client_id();
}

return $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
12 changes: 11 additions & 1 deletion plugins/faustwp/tests/integration/TelemetryFunctionsTests.php
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,12 @@ 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' );
$id = generate_telemetry_client_id();
self::assertNotEmpty( $id );
self::assertTrue( wp_is_uuid( $id ) );
self::assertSame( $id, get_telemetry_client_id() );
}

}

0 comments on commit b6c1967

Please sign in to comment.