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

New telemetry functionality #1992

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions src/Common/Telemetry/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function add_filters() {
add_filter( 'stellarwp/telemetry/optin_args', [ $this, 'filter_optin_args' ] );
add_filter( 'stellarwp/telemetry/exit_interview_args', [ $this, 'filter_exit_interview_args' ] );
add_filter( 'http_request_args', [ $this, 'filter_telemetry_http_request_args' ], 10, 2 );

/* Prefixed filters - should be 'tec' but best to grab it to be sure. */
$prefix = Telemetry::get_hook_prefix();
add_filter( "stellarwp/telemetry/{$prefix}/send_data_args", [ $this, 'filter_data_args' ] );
}

/**
Expand Down Expand Up @@ -177,4 +181,8 @@ public function filter_exit_interview_args( $args ) {
public function maybe_enqueue_admin_modal_assets(): void {
$this->container->make( Asset_Subscriber::class )->maybe_enqueue_admin_assets();
}

public function filter_data_args( $args ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
Squiz.Commenting.FunctionComment.Missing
Missing doc comment for function filter_data_args()

return $this->container->make( Telemetry::class )->filter_data_args( $args );
}
}
129 changes: 127 additions & 2 deletions src/Common/Telemetry/Telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class Telemetry {
];

/**
* Path to main pugin file
* Path to main plugin file
*
* @since 5.1.0
*
Expand Down Expand Up @@ -190,6 +190,17 @@ public static function clean_up(): void {
}
}

/**
* Get the hook prefix we are using.
*
* @since TBD
*
* @return string The hook prefix. Note there is no trailing slash!
*/
public static function get_hook_prefix(): string {
return self::$hook_prefix;
}

public static function get_plugin_slug() {
if ( empty( self::$plugin_slug ) ) {
self::$plugin_slug = self::get_parent_plugin_slug();
Expand Down Expand Up @@ -447,7 +458,10 @@ public static function get_tec_telemetry_slugs() {
*
* @param array<string,string> $slugs An array of plugins in the format [ 'plugin_slug' => 'plugin_path' ]
*/
return apply_filters( 'tec_telemetry_slugs', [] );
$slugs = apply_filters( 'tec_telemetry_slugs', [] );

// Remove any potential duplicates.
return array_unique( $slugs, SORT_STRING );
}

/**
Expand Down Expand Up @@ -639,4 +653,115 @@ public static function disable_modal( $slug, $enable = false ) {
$option_slug = Config::get_container()->get( Opt_In_Template::class )->get_option_name( $slug );
update_option( $option_slug, $enable );
}

/**
* Filters the data arguments for TEC plugins.
*
* @since TBD
*
* @param array<string,mixed> $args The array of data arguments to filter.
*
* @return array<string,mixed> $args The array of filtered data arguments.
*/
public function filter_data_args( $args ): array {
// We only want to mess with the data block.
$telemetry = json_decode( $args['telemetry'], true );

list( $changed, $telemetry ) = $this->add_licenses( $telemetry );

if ( tribe_is_truthy( $changed ) ) {
$args['telemetry'] = json_encode( $telemetry );
}

/**
* Allows overriding the data arguments for TEC plugins.
*
* @since TBD
*
* @param array<string,mixed> $args The data arguments to filter.
*
* @return array<string,mixed> $args The filtered data arguments.
*/
return apply_filters( 'tec_telemetry_data_arguments', $args );
}

/**
* Add premium plugin licenses to the telemetry data.
*
* @since TBD
*
* @param object $telemetry The telemetry data object.
*
* @return array<bool,object> and array in the format:
* [
* bool $changed If the data has changed.
* obj $telemetry The (potentially modified) telemetry data object.
* ]
*/
protected function add_licenses( $telemetry ) {
// No data sent, bail safely.
if ( empty( $telemetry ) ){
return [ false, $telemetry ];
}

// We don't need the path info for this.
$tec_slugs = array_keys( self::get_tec_telemetry_slugs() );
// Remove parent slugs - they don't have licenses.
$tec_slugs = array_diff( $tec_slugs, self::$base_parent_slugs );

// Nothing to add, bail safely.
if ( empty( $tec_slugs ) ){
return [ false, $telemetry ];
}

if ( ! isset( $telemetry['stellar_licenses'] ) ) {
// Set up if it doesn't exist.
$telemetry['stellar_licenses'] = [];
} elseif( ! is_array( $telemetry['stellar_licenses'] ) ) {
// Make sure it's an array if it does exist.
$telemetry['stellar_licenses'] = (array) $telemetry['stellar_licenses'];
}

// Grab all the options, cached.
$options = wp_load_alloptions();

// Filter out everything but our license keys.
$options = array_filter(
$options,
function( $key ) {
return str_starts_with( $key, "pue_install_key_");
Camwyn marked this conversation as resolved.
Show resolved Hide resolved
Camwyn marked this conversation as resolved.
Show resolved Hide resolved
},
ARRAY_FILTER_USE_KEY
);

// No keys found.
if ( empty( $options ) ) {
return [ false, $telemetry ];
}

foreach ( $options as $slug => $key ) {
$slug = str_replace( 'pue_install_key_', '', $slug );
$slug = str_replace( '-', '_', $slug );

/**
* Filter the license slug.
* This allows for some plugins that use older nomenclature (like Filter Bar) to add a more "modern" slug.
*
* @since TBD
*
* @param string $slug The stored license slug.
*/
$slug = apply_filters( 'tec_telemetry_license_slug_' . $slug, $slug );

$telemetry['stellar_licenses'][$slug] = $key;
}

if ( ! empty( $telemetry['stellar_licenses'] ) ) {
// return changed data set and flag.
return [ true, $telemetry ];
}

// Fallback - no changes.
return [ false, $telemetry ];
}
Camwyn marked this conversation as resolved.
Show resolved Hide resolved
}
Loading