From b188e44aaffe29866cb8ea336e340e835f4b0b30 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Fri, 6 Oct 2023 16:09:11 -0400 Subject: [PATCH 1/9] initial commit --- src/Common/Telemetry/Provider.php | 8 +++++ src/Common/Telemetry/Telemetry.php | 58 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/Common/Telemetry/Provider.php b/src/Common/Telemetry/Provider.php index 440cc02751..350bb88f13 100644 --- a/src/Common/Telemetry/Provider.php +++ b/src/Common/Telemetry/Provider.php @@ -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 */ + $prefix = Telemetry::get_hook_prefix(); + add_filter( "stellarwp/telemetry/{$prefix}/send_data_args", [ $this, 'filter_data_args' ] ); } /** @@ -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 ) { + return $this->container->make( Telemetry::class )->filter_data_args( $args ); + } } diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index f164b59d48..54dc5642bd 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -190,6 +190,10 @@ public static function clean_up(): void { } } + public static function get_hook_prefix() { + return self::$hook_prefix; + } + public static function get_plugin_slug() { if ( empty( self::$plugin_slug ) ) { self::$plugin_slug = self::get_parent_plugin_slug(); @@ -640,4 +644,58 @@ 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 $args The data arguments to filter. + * + * @return array $args The filtered data arguments. + */ + public function filter_data_args( $args ): array { + error_log( 'filter_data_args' ); + $tec_slugs = self::get_tec_telemetry_slugs(); + $fnord = false; + + foreach ( $tec_slugs as $slug => $path ) { + if ( in_array( $slug, self::$base_parent_slugs ) ) { + error_log( 'discard ' . $slug ); + continue; + } + + + + $modified_slug = 'pue_install_key_' . str_replace( '-', '_', $slug ); + // pue_install_key_events_calendar_pro + error_log( 'looking for ' . $modified_slug ); + $key = get_option( $modified_slug, null ); + + if ( empty( $key ) ) { + error_log( 'no key found' ); + continue; + } + + + error_log( 'inserting key ' . $key ); + $fnord = true; + $args['plugins'][ $slug ]['license'] = $key; + } + + if ( $fnord ) { + error_log( json_encode( $args ) ); + } + + /** + * Allows overriding the data arguments for TEC plugins. + * + * @since TBD + * + * @param array $args The data arguments to filter. + * + * @return array $args The filtered data arguments. + */ + return apply_filters( 'tec_telemetry_data_arguments', $args ); + } } From 00876f779097b2a4177d24fd15ef430c9efbf752 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Fri, 6 Oct 2023 16:13:22 -0400 Subject: [PATCH 2/9] remove logging and add docblock --- src/Common/Telemetry/Telemetry.php | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 54dc5642bd..37a166c7bd 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -190,7 +190,14 @@ public static function clean_up(): void { } } - public static function get_hook_prefix() { + /** + * 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; } @@ -613,7 +620,6 @@ public static function calculate_modal_status(): bool { // No entries - show modal. if ( count( $shows ) < 1 ) { - error_log('no entries'); return true; } @@ -655,38 +661,24 @@ public static function disable_modal( $slug, $enable = false ) { * @return array $args The filtered data arguments. */ public function filter_data_args( $args ): array { - error_log( 'filter_data_args' ); $tec_slugs = self::get_tec_telemetry_slugs(); - $fnord = false; foreach ( $tec_slugs as $slug => $path ) { if ( in_array( $slug, self::$base_parent_slugs ) ) { - error_log( 'discard ' . $slug ); continue; } - - $modified_slug = 'pue_install_key_' . str_replace( '-', '_', $slug ); - // pue_install_key_events_calendar_pro - error_log( 'looking for ' . $modified_slug ); + // pue_install_key_events_calendar_pro, etc $key = get_option( $modified_slug, null ); if ( empty( $key ) ) { - error_log( 'no key found' ); continue; } - - error_log( 'inserting key ' . $key ); - $fnord = true; $args['plugins'][ $slug ]['license'] = $key; } - if ( $fnord ) { - error_log( json_encode( $args ) ); - } - /** * Allows overriding the data arguments for TEC plugins. * From 626fcbcec369a8191db34097e5072f9ae078237f Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Mon, 9 Oct 2023 09:37:22 -0400 Subject: [PATCH 3/9] interim commit --- src/Common/Telemetry/Telemetry.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 37a166c7bd..7a35542909 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -656,14 +656,19 @@ public static function disable_modal( $slug, $enable = false ) { * * @since TBD * - * @param array $args The data arguments to filter. + * @param array $args The array of data arguments to filter. * - * @return array $args The filtered data arguments. + * @return array $args The array of filtered data arguments. */ public function filter_data_args( $args ): array { $tec_slugs = self::get_tec_telemetry_slugs(); + $tec_slugs = array_keys( $tec_slugs ); // we don't need the path info for this. + $telemetry = json_decode( $args['telemetry'] ); + error_log(gettype($telemetry)); - foreach ( $tec_slugs as $slug => $path ) { + $telemetry->stellar_licenses = []; + + foreach ( $tec_slugs as $slug ) { if ( in_array( $slug, self::$base_parent_slugs ) ) { continue; } @@ -676,7 +681,12 @@ public function filter_data_args( $args ): array { continue; } - $args['plugins'][ $slug ]['license'] = $key; + $changed = true; + $telemetry->stellar_licenses[] = [ $slug => $key ]; + } + + if ( ! isset( $changed ) ) { + $args['telemetry'] = json_encode( $telemetry ); } /** From 9606ce475845350ec328115d0725ec54d780e229 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Fri, 13 Oct 2023 11:32:52 -0400 Subject: [PATCH 4/9] remove logging, alter logic --- src/Common/Telemetry/Telemetry.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 7a35542909..6fcb7a27aa 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -664,10 +664,10 @@ public function filter_data_args( $args ): array { $tec_slugs = self::get_tec_telemetry_slugs(); $tec_slugs = array_keys( $tec_slugs ); // we don't need the path info for this. $telemetry = json_decode( $args['telemetry'] ); - error_log(gettype($telemetry)); $telemetry->stellar_licenses = []; + foreach ( $tec_slugs as $slug ) { if ( in_array( $slug, self::$base_parent_slugs ) ) { continue; @@ -685,7 +685,7 @@ public function filter_data_args( $args ): array { $telemetry->stellar_licenses[] = [ $slug => $key ]; } - if ( ! isset( $changed ) ) { + if ( isset( $changed ) ) { $args['telemetry'] = json_encode( $telemetry ); } From 9fe916a3a43405a2588b08cf91c9816c9da52903 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Fri, 13 Oct 2023 12:07:15 -0400 Subject: [PATCH 5/9] Break licensing into it's own function. --- src/Common/Telemetry/Provider.php | 2 +- src/Common/Telemetry/Telemetry.php | 83 ++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/src/Common/Telemetry/Provider.php b/src/Common/Telemetry/Provider.php index 350bb88f13..391b706a8d 100644 --- a/src/Common/Telemetry/Provider.php +++ b/src/Common/Telemetry/Provider.php @@ -65,7 +65,7 @@ public function add_filters() { 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 */ + /* 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' ] ); } diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 6fcb7a27aa..32020ac000 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -72,7 +72,7 @@ final class Telemetry { ]; /** - * Path to main pugin file + * Path to main plugin file * * @since 5.1.0 * @@ -661,18 +661,65 @@ public static function disable_modal( $slug, $enable = false ) { * @return array $args The array of filtered data arguments. */ public function filter_data_args( $args ): array { - $tec_slugs = self::get_tec_telemetry_slugs(); - $tec_slugs = array_keys( $tec_slugs ); // we don't need the path info for this. - $telemetry = json_decode( $args['telemetry'] ); + // WE only want to mess with the data block. + $telemetry = json_decode( $args['telemetry'], true ); - $telemetry->stellar_licenses = []; + list( $changed, $telemetry ) = $this->add_licenses( $telemetry ); + if ( tribe_is_truthy( $changed ) ) { + $args['telemetry'] = json_encode( $telemetry ); + } - foreach ( $tec_slugs as $slug ) { - if ( in_array( $slug, self::$base_parent_slugs ) ) { - continue; - } + /** + * Allows overriding the data arguments for TEC plugins. + * + * @since TBD + * + * @param array $args The data arguments to filter. + * + * @return array $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 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; + } + + foreach ( $tec_slugs as $slug ) { $modified_slug = 'pue_install_key_' . str_replace( '-', '_', $slug ); // pue_install_key_events_calendar_pro, etc $key = get_option( $modified_slug, null ); @@ -681,23 +728,15 @@ public function filter_data_args( $args ): array { continue; } - $changed = true; $telemetry->stellar_licenses[] = [ $slug => $key ]; } - if ( isset( $changed ) ) { - $args['telemetry'] = json_encode( $telemetry ); + if ( ! empty( $telemetry->stellar_licenses ) ) { + // return changed data set and flag. + return [ true, $telemetry ]; } - /** - * Allows overriding the data arguments for TEC plugins. - * - * @since TBD - * - * @param array $args The data arguments to filter. - * - * @return array $args The filtered data arguments. - */ - return apply_filters( 'tec_telemetry_data_arguments', $args ); + // Fallback - no changes. + return [ false, $telemetry ]; } } From 2c26e50943cdd35f5d68b032c10a82ca9c691d14 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Fri, 13 Oct 2023 17:09:38 -0400 Subject: [PATCH 6/9] Restructure the license fetch to just fetch all and loop through them. Less logic and less code that way. Also less brittle. --- src/Common/Telemetry/Telemetry.php | 55 ++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 32020ac000..4a43ab5fc1 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -458,7 +458,10 @@ public static function get_tec_telemetry_slugs() { * * @param array $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 ); } /** @@ -661,7 +664,7 @@ public static function disable_modal( $slug, $enable = false ) { * @return array $args The array of filtered data arguments. */ public function filter_data_args( $args ): array { - // WE only want to mess with the data block. + // We only want to mess with the data block. $telemetry = json_decode( $args['telemetry'], true ); list( $changed, $telemetry ) = $this->add_licenses( $telemetry ); @@ -711,27 +714,49 @@ protected function add_licenses( $telemetry ) { return [ false, $telemetry ]; } - if ( ! isset( $telemetry->stellar_licenses ) ) { + if ( ! isset( $telemetry['stellar_licenses'] ) ) { // Set up if it doesn't exist. - $telemetry->stellar_licenses = []; - } elseif( ! is_array( $telemetry->stellar_licenses ) ) { + $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; + $telemetry['stellar_licenses'] = (array) $telemetry['stellar_licenses']; } - foreach ( $tec_slugs as $slug ) { - $modified_slug = 'pue_install_key_' . str_replace( '-', '_', $slug ); - // pue_install_key_events_calendar_pro, etc - $key = get_option( $modified_slug, null ); + // Grab all the options, cached. + $options = wp_load_alloptions(); - if ( empty( $key ) ) { - continue; - } + // Filter out everything but our license keys. + $options = array_filter( + $options, + function( $key ) { + return str_starts_with( $key, "pue_install_key_"); + }, + ARRAY_FILTER_USE_KEY + ); + + // No keys found. + if ( empty( $options ) ) { + return [ false, $telemetry ]; + } - $telemetry->stellar_licenses[] = [ $slug => $key ]; + 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 ) ) { + if ( ! empty( $telemetry['stellar_licenses'] ) ) { // return changed data set and flag. return [ true, $telemetry ]; } From b1de49197e5d3b71790c3dcdb5a2450d09b413a6 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Wed, 18 Oct 2023 16:22:20 -0400 Subject: [PATCH 7/9] version shenanigans --- package.json | 2 +- src/Tribe/Main.php | 2 +- tribe-common.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 19b864974e..ac8b4ebe8e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tribe-common", - "version": "5.1.10", + "version": "6.6.6", "repository": "git@github.com:the-events-calendar/tribe-common.git", "_resourcepath": "src/resources", "_domainPath": "lang", diff --git a/src/Tribe/Main.php b/src/Tribe/Main.php index 598242ebda..bbbc7d8b3c 100644 --- a/src/Tribe/Main.php +++ b/src/Tribe/Main.php @@ -23,7 +23,7 @@ class Tribe__Main { const OPTIONNAME = 'tribe_events_calendar_options'; const OPTIONNAMENETWORK = 'tribe_events_calendar_network_options'; - const VERSION = '5.1.10.1'; + const VERSION = '6.6.6'; const FEED_URL = 'https://theeventscalendar.com/feed/'; diff --git a/tribe-common.php b/tribe-common.php index 57a462e4a3..2a3c99f9cf 100644 --- a/tribe-common.php +++ b/tribe-common.php @@ -2,7 +2,7 @@ /* Plugin Name: Tribe Common Description: An event settings framework for managing shared options -Version: 5.1.10.1 +Version: 6.6.6 Author: The Events Calendar Author URI: http://evnt.is/1x Text Domain: tribe-common From d176c10f4b1b2042de634b2d38abc155082ab7a9 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Tue, 31 Oct 2023 10:40:24 -0400 Subject: [PATCH 8/9] PHP 7.4 compat --- src/Common/Telemetry/Telemetry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index 4a43ab5fc1..a55e59670a 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -729,7 +729,7 @@ protected function add_licenses( $telemetry ) { $options = array_filter( $options, function( $key ) { - return str_starts_with( $key, "pue_install_key_"); + return false !== stripos( $key, "pue_install_key_"); }, ARRAY_FILTER_USE_KEY ); From cc30ec7da0620858328e0d70c14101daa6222f54 Mon Sep 17 00:00:00 2001 From: Stephen Page Date: Tue, 31 Oct 2023 10:53:54 -0400 Subject: [PATCH 9/9] Update add_license to use $telemetry by reference and just return a boolean. --- src/Common/Telemetry/Telemetry.php | 41 +++++++++++++----------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/Common/Telemetry/Telemetry.php b/src/Common/Telemetry/Telemetry.php index a55e59670a..47e7be1ed2 100644 --- a/src/Common/Telemetry/Telemetry.php +++ b/src/Common/Telemetry/Telemetry.php @@ -667,7 +667,7 @@ 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 ); + $changed = $this->add_licenses( $telemetry ); if ( tribe_is_truthy( $changed ) ) { $args['telemetry'] = json_encode( $telemetry ); @@ -690,28 +690,24 @@ public function filter_data_args( $args ): array { * * @since TBD * - * @param object $telemetry The telemetry data object. + * @param object $telemetry_by_reference The telemetry data object. * - * @return array and array in the format: - * [ - * bool $changed If the data has changed. - * obj $telemetry The (potentially modified) telemetry data object. - * ] + * @return bool If the data has changed. */ - protected function add_licenses( $telemetry ) { + protected function add_licenses( &$telemetry_by_reference ): bool { // No data sent, bail safely. - if ( empty( $telemetry ) ){ - return [ false, $telemetry ]; + 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, $telemetry ]; + if ( empty( $tec_slugs ) ) { + return false; } if ( ! isset( $telemetry['stellar_licenses'] ) ) { @@ -721,28 +717,25 @@ protected function add_licenses( $telemetry ) { // 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 false !== stripos( $key, "pue_install_key_"); + static function( $key ) { + return str_starts_with( $key, 'pue_install_key_' ); }, ARRAY_FILTER_USE_KEY ); // No keys found. if ( empty( $options ) ) { - return [ false, $telemetry ]; + 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. @@ -752,16 +745,16 @@ function( $key ) { * @param string $slug The stored license slug. */ $slug = apply_filters( 'tec_telemetry_license_slug_' . $slug, $slug ); - - $telemetry['stellar_licenses'][$slug] = $key; + $telemetry['stellar_licenses'][ $slug ] = $key; } if ( ! empty( $telemetry['stellar_licenses'] ) ) { - // return changed data set and flag. - return [ true, $telemetry ]; + // Update telemetry to hold the new modifications. + $telemetry_by_reference = $telemetry; + return true; } // Fallback - no changes. - return [ false, $telemetry ]; + return false; } }