Skip to content

Commit

Permalink
Merge branch 'master' into fix/phpstan-7-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Camwyn committed Nov 10, 2023
2 parents 99d69b1 + ed086b1 commit c88edb7
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"stellarwp/container-contract": "^1.0.4",
"stellarwp/db": "^1.0.3",
"stellarwp/installer": "^1.1.0",
"stellarwp/telemetry": "^2.3.0-rc.01"
"stellarwp/telemetry": "^2.3.1"
},
"require-dev": {
"automattic/vipwpcs": "^2.0",
Expand Down
40 changes: 21 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.1.12",
"version": "5.1.13",
"repository": "git@github.com:the-events-calendar/tribe-common.git",
"_resourcepath": "src/resources",
"_domainPath": "lang",
Expand Down
9 changes: 9 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

* Fix - Resolved several `Deprecated: Creation of dynamic property` warnings on: `\Tribe__Field::$allow_clear, $type, $class, $label, $label_attributes, $error, $tooltip, $size, $html, $options, $value, $conditional, $placeholder, $display_callback, $if_empty, $can_be_empty, $clear_after, $tooltip_first` and `\Tribe__Settings_Tab::$priority, public $fields, $show_save, $display_callback, $network_admin` [BTRIA-2088]

= [5.1.13.1] 2023-11-10 =

* Fix - Update Telemetry library to prevent potential fatals. [TEC-4978]
* Language - 0 new strings added, 0 updated, 1 fuzzied, and 0 obsoleted

= [5.1.13] 2023-11-08 =

* Tweak - Ensure stability of opt-in data.

= [5.1.12] 2023-11-01 =

* Tweak - Ticketing & RSVP tab selected by default when clicking Help from the Tickets menu. [ET-1837]
Expand Down
151 changes: 151 additions & 0 deletions src/Common/Telemetry/Opt_In.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* Handles Telemetry opt-in logic.
*
* @since 5.1.13
*
* @package TEC\Common\Telemetry
*/
namespace TEC\Common\Telemetry;

use TEC\Common\StellarWP\Telemetry\Config;
use TEC\Common\StellarWP\Telemetry\Opt_In\Status as Opt_In_Status;
use TEC\Common\StellarWP\DB\DB;
use WP_User;

/**
* Class Opt_In
*
* @since 5.1.13
* @package TEC\Common\Telemetry
*/
class Opt_In {
/**
* Build the opt-in user data, store it, and fetch it.
*
* @since 5.1.13
*
* @return array
*/
public function build_opt_in_user(): array {
$stellar_slug = Config::get_stellar_slug();

if ( empty( $stellar_slug ) ) {
return [];
}

$opt_in_user = get_option( Opt_In_Status::OPTION_NAME_USER_INFO, [] );

// If we already have a stored opt-in user, just return that.
if ( count( $opt_in_user ) > 0 && ! empty( $opt_in_user['user'] ) ) {
$stored_data = json_decode( $opt_in_user['user'], true );

if ( is_array( $stored_data ) ) {
return $stored_data;
}
}

$user = $this->get_generated_opt_in_user();

$opt_in_user_data = [
'name' => null,
'email' => null,
'opt_in_text' => null,
'plugin_slug' => $stellar_slug,
];

if ( ! empty( $user ) && ! empty( $user->user_email ) ) {
$opt_in_user_data['name'] = $user->display_name;
$opt_in_user_data['email'] = $user->user_email;
}

update_option( Opt_In_Status::OPTION_NAME_USER_INFO, [ 'user' => wp_json_encode( $opt_in_user_data ) ] );

return $opt_in_user_data;
}

/**
* Get the opt-in user to be used in the opt_in_user telemetry field.
*
* @since 5.1.13
*
* @return WP_User|null
*/
protected function get_generated_opt_in_user(): ?WP_User {
$admin_user = $this->get_admin_user_by_admin_email();

if ( $admin_user ) {
return $admin_user;
}

$admin_user = $this->get_first_admin_user();

return $admin_user;
}

/**
* Get an admin user based on the admin email for the site.
*
* @since 5.1.13
*
* @return WP_User|null
*/
protected function get_admin_user_by_admin_email(): ?WP_User {
$admin_email = get_option( 'admin_email' );

if ( empty( $admin_email ) ) {
return null;
}

$user = get_user_by( 'email', $admin_email );

if ( ! $user || ! $user->exists() ) {
return null;
}

return $user;
}

/**
* Get the first admin user from the first 5,000 users of the site.
*
* @since 5.1.13
*
* @return WP_User|null
*/
protected function get_first_admin_user(): ?WP_User {
global $wpdb;

$results = DB::table( 'usermeta' )
->select( 'user_id', 'meta_value' )
->where( 'meta_key', $wpdb->prefix . 'capabilities' )
->orderBy( 'user_id' )
->limit( 5000 )
->getAll();

// Let's only grab administrators.
$results = array_filter( $results, static function( $row ) {
return strpos( $row->meta_value, '"administrator"' ) !== false;
} );

if ( empty( $results ) ) {
return null;
}

$user_row = current( $results );

if ( empty( $user_row ) || empty( $user_row->user_id ) ) {
return null;
}

$user_id = absint( $user_row->user_id );
$user = get_userdata( $user_id );

if ( empty( $user ) || ! $user->exists() ) {
return null;
}

return $user;
}
}
37 changes: 37 additions & 0 deletions src/Common/Telemetry/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Provider extends Service_Provider {
*/
public function register() {
$this->container->bind( Telemetry::class, Telemetry::class );
$this->container->singleton( Opt_In::class, Opt_In::class );

$this->add_actions();
$this->add_filters();
Expand Down Expand Up @@ -86,13 +87,49 @@ public function filter_telemetry_http_request_args( $parsed_args, $url ) {
return $parsed_args;
}

/**
* Filters the arguments for telemetry data to add the opt-in user data if missing.
*
* @since 5.1.13
*
* @param array $args Telemetry args.
*
* @return array
*/
public function filter_send_data_args( $args ) {
if ( ! is_array( $args ) ) {
return $args;
}

if ( empty( $args['telemetry'] ) ) {
return $args;
}

$telemetry = json_decode( $args['telemetry'], true );

if ( ! empty( $telemetry['opt_in_user'] ) ) {
return $args;
}

/** @var Opt_In $opt_in */
$opt_in = $this->container->get( Opt_In::class );

$telemetry['opt_in_user'] = $opt_in->build_opt_in_user();

$args['telemetry'] = wp_json_encode( $telemetry );

return $args;
}

/**
* It's super important to make sure when hooking to WordPress actions that we don't do before we are sure that
* telemetry was properly booted into the system.
*
* @since 5.1.3
* @since 5.1.13 Added filter of send_data_args to include opt-in data.
*/
public function hook_telemetry_init(): void {
add_filter( "stellarwp/telemetry/tec/send_data_args", [ $this, 'filter_send_data_args' ] );
add_action( 'admin_init', [ $this, 'initialize_telemetry' ], 5 );
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tribe/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Tribe__Main {
const OPTIONNAME = 'tribe_events_calendar_options';
const OPTIONNAMENETWORK = 'tribe_events_calendar_network_options';

const VERSION = '5.1.12';
const VERSION = '5.1.13.1';

const FEED_URL = 'https://theeventscalendar.com/feed/';

Expand Down
Loading

0 comments on commit c88edb7

Please sign in to comment.