| 1 |
<?php |
| 2 |
/** |
| 3 |
* Telemetry: Records events related to plugin options and settings page |
| 4 |
* |
| 5 |
* @package Parsely\Telemetry |
| 6 |
* @since 3.12.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Telemetry; |
| 12 |
|
| 13 |
/** |
| 14 |
* Records an event whenever the Parsely option gets updated. The changed |
| 15 |
* values are included in the event. |
| 16 |
* |
| 17 |
* @since 3.12.0 |
| 18 |
* |
| 19 |
* @param array<string, mixed> $old_values The old option values. |
| 20 |
* @param array<string, mixed> $values The new option values. |
| 21 |
* @param Telemetry_System $telemetry_system The telemetry system to use. |
| 22 |
*/ |
| 23 |
function record_parsely_option_updated( |
| 24 |
array $old_values, |
| 25 |
array $values, |
| 26 |
Telemetry_System $telemetry_system |
| 27 |
): void { |
| 28 |
$all_keys = array_unique( array_merge( array_keys( $old_values ), array_keys( $values ) ) ); |
| 29 |
|
| 30 |
// Get the option keys that got updated. |
| 31 |
$updated_keys = array_reduce( |
| 32 |
$all_keys, |
| 33 |
function ( array $carry, string $key ) use ( $old_values, $values ) { |
| 34 |
if ( ( |
| 35 |
// The old key and the new key have the same value. |
| 36 |
isset( $old_values[ $key ] ) === isset( $values[ $key ] ) && |
| 37 |
wp_json_encode( $old_values[ $key ] ) === wp_json_encode( $values[ $key ] ) |
| 38 |
) || |
| 39 |
'plugin_version' === $key // Ignoring the `plugin_version` key. |
| 40 |
) { |
| 41 |
return $carry; |
| 42 |
} |
| 43 |
|
| 44 |
// The values are different, we're marking the current key as changed. |
| 45 |
$carry[] = $key; |
| 46 |
|
| 47 |
return $carry; |
| 48 |
}, |
| 49 |
array() |
| 50 |
); |
| 51 |
|
| 52 |
if ( count( $updated_keys ) === 0 ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$telemetry_system->record_event( |
| 57 |
'wpparsely_option_updated', |
| 58 |
array( |
| 59 |
'updated_keys' => $updated_keys, |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Records an event whenever the plugin settings page gets loaded. |
| 66 |
* |
| 67 |
* @since 3.12.0 |
| 68 |
* |
| 69 |
* @param Telemetry_System $telemetry_system The telemetry system to use. |
| 70 |
*/ |
| 71 |
function record_settings_page_loaded( Telemetry_System $telemetry_system ): void { |
| 72 |
if ( |
| 73 |
! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) || |
| 74 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 75 |
( isset( $_GET['settings-updated'] ) && 'true' === $_GET['settings-updated'] ) |
| 76 |
) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$telemetry_system->record_event( 'wpparsely_settings_page_loaded' ); |
| 81 |
} |
| 82 |
|