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
Draft
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tribe-common",
"version": "5.2.4",
"version": "6.6.6",
"repository": "git@github.com:the-events-calendar/tribe-common.git",
"_resourcepath": "src/resources",
"_domainPath": "lang",
Expand Down
8 changes: 8 additions & 0 deletions src/Common/Telemetry/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function add_filters() {
add_filter( 'stellarwp/telemetry/event-tickets/optin_args', [ $this, 'filter_et_optin_args' ], 10 );
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 @@ -245,4 +249,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 );
}
}
120 changes: 119 additions & 1 deletion src/Common/Telemetry/Telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,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;
}

/**
* Get the slug of the plugin.
*
Expand Down Expand Up @@ -457,7 +468,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 @@ -651,4 +665,108 @@ 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 );

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

if ( tribe_is_truthy( $changed ) ) {
$args['telemetry'] = wp_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_by_reference The telemetry data object.
*
* @return bool If the data has changed.
*/
protected function add_licenses( &$telemetry_by_reference ): bool {
// No data sent, bail safely.
if ( empty( $telemetry_by_reference ) ) {
return false;
}

$telemetry = $telemetry_by_reference;
// 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;
}

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,
static function ( $key ) {
return str_starts_with( $key, 'pue_install_key_' );
},
ARRAY_FILTER_USE_KEY
);

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

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'] ) ) {
// Update telemetry to hold the new modifications.
$telemetry_by_reference = $telemetry;
return true;
}

// Fallback - no changes.
return false;
}
}
2 changes: 1 addition & 1 deletion src/Tribe/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Tribe__Main {
const OPTIONNAME = 'tribe_events_calendar_options';
const OPTIONNAMENETWORK = 'tribe_events_calendar_network_options';
const FEED_URL = 'https://theeventscalendar.com/feed/';
const VERSION = '5.2.4';
const VERSION = '6.6.6';

protected $plugin_context;
protected $plugin_context_class;
Expand Down
18 changes: 9 additions & 9 deletions tribe-common.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* Plugin Name: Tribe Common
* Description: An event settings framework for managing shared options
* Version: 5.2.4
* Author: The Events Calendar
* Author URI: http://evnt.is/1x
* Text Domain: tribe-common
* License: GPLv2 or later
*/
/*
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.FileComment.WrongStyle
You must use "/**" style comments for a file comment

Plugin Name: Tribe Common
Description: An event settings framework for managing shared options
Version: 6.6.6
Author: The Events Calendar
Author URI: http://evnt.is/1x
Text Domain: tribe-common
License: GPLv2 or later
*/

/*
Copyright 2009-2015 by The Events Calendar and the contributors
Expand Down
Loading