Skip to content

Commit 38089cd

Browse files
tarecordoakesjosh
andauthored
Release - 2.2.0 (#90)
Co-authored-by: Josh Oakes <josh@joshoak.es>
1 parent 0678280 commit 38089cd

11 files changed

Lines changed: 485 additions & 96 deletions

File tree

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ A library for Opt-in and Telemetry data to be sent to the StellarWP Telemetry se
3030
- [stellarwp/telemetry/{hook-prefix}/last\_send\_expire\_seconds](#stellarwptelemetryhook-prefixlast_send_expire_seconds)
3131
- [stellarwp/telemetry/exit\_interview\_args](#stellarwptelemetryexit_interview_args)
3232
- [stellarwp/telemetry/{stellar\_slug}/exit\_interview\_args](#stellarwptelemetrystellar_slugexit_interview_args)
33+
- [stellarwp/telemetry/{hook-prefix}/event\_data](#stellarwptelemetryhook-prefixevent_data)
34+
- [stellarwp/telemetry/{hook-prefix}/events\_url](#stellarwptelemetryhook-prefixevents_url)
35+
- [Action Reference](#action-reference)
36+
- [stellarwp/telemetry/optin](#stellarwptelemetryoptin)
37+
- [stellarwp/telemetry/{hook-prefix}/optin](#stellarwptelemetryhook-prefixoptin)
38+
- [stellarwp/telemetry/{hook-prefix}/event](#stellarwptelemetryhook-prefixevent)
3339
- [Adding Plugin Data to Site Health](#adding-plugin-data-to-site-health)
3440
- [Capturing User Events](#capturing-user-events)
3541
## Installation
@@ -396,6 +402,42 @@ $args = [
396402
### stellarwp/telemetry/{stellar_slug}/exit_interview_args
397403
This filter will be deprecated in future versions. Use [stellarwp/telemetry/exit_interview_args](#stellarwptelemetrystellar_slugexit_interview_args) instead.
398404

405+
### stellarwp/telemetry/{hook-prefix}/event_data
406+
Filters the array of data sent along with the event.
407+
408+
**Parameters**: _array_ `$data`
409+
410+
**Default**:
411+
```php
412+
$data = [
413+
'token' => $this->telemetry->get_token(),
414+
'stellar_slug' => Config::get_stellar_slug(),
415+
'event' => $name,
416+
'event_data' => wp_json_encode( $data ),
417+
];
418+
```
419+
### stellarwp/telemetry/{hook-prefix}/events_url
420+
Filters the event URL used when sending events to the Telemetry server.
421+
422+
**Parameters**: _string_ `$url`
423+
424+
**Default**: `https://telemetry.stellarwp.com/api/v1/events`
425+
426+
## Action Reference
427+
428+
### stellarwp/telemetry/optin
429+
430+
**Parameters**: _string_ `$stellar_slug` The stellar slug of the plugin for which the modal should be shown.
431+
### stellarwp/telemetry/{hook-prefix}/optin
432+
This filter will be deprecated in future versions. Use [stellarwp/telemetry/optin](#stellarwptelemetryoptin) instead.
433+
434+
### stellarwp/telemetry/{hook-prefix}/event
435+
Sends a site event to the Telemetry server.
436+
437+
**Parameters**:
438+
- _string_ `$event` The name of the event.
439+
- _array_ `$data` A set of data that should be passed along with the event.
440+
399441
## Adding Plugin Data to Site Health
400442

401443
We collect the Site Health data as json on the server. In order to pass additional plugin specific items that can be reported on, you will need to add a section to the Site Health Data. The process for adding a section is documented on [developer.wordpress.org](https://developer.wordpress.org/reference/hooks/debug_information/).
@@ -455,7 +497,7 @@ $data = [
455497
'two' => 2,
456498
'three' => 3,
457499
];
458-
do_action( 'stellarwp/telemetry/event', 'your-event-name', $data );
500+
do_action( 'stellarwp/telemetry/{hook-prefix}/event', 'your-event-name', $data );
459501
```
460502

461503
Here is how you might log events when a user creates a new post:
@@ -490,6 +532,6 @@ function user_creates_post( $post_id, $post, $update ) {
490532
];
491533

492534
// Log the event with the telemetry server.
493-
do_action( 'stellarwp/telemetry/event', 'new_post', $event_data );
535+
do_action( 'stellarwp/telemetry/{hook-prefix}/event', 'new_post', $event_data );
494536
}
495537
```

dev/public/wp-content/plugins/library-testing/library-testing.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,43 @@ function () {
3131
do_action( 'stellarwp/telemetry/optin', 'telemetry-library' );
3232
}
3333
);
34+
35+
// If the 'Send Events' link was used, send some test events once.
36+
add_action(
37+
'init',
38+
function() {
39+
if ( ! isset( $_GET['send-events'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
40+
return;
41+
}
42+
43+
do_action( 'stellarwp/telemetry/telemetry-prefix/event', 'opt-in', [ 'one' => 1 ] );
44+
do_action( 'stellarwp/telemetry/telemetry-prefix/event', 'create-post', [ 'post-title' => 'This is my first post!' ] );
45+
do_action( 'stellarwp/telemetry/telemetry-prefix/event', 'opt-out', [ 'one' => 1 ] );
46+
47+
wp_safe_redirect( remove_query_arg( 'send-events' ) );
48+
exit;
49+
}
50+
);
51+
52+
/**
53+
* Adds a helper link to the admin bar for sending a group of events.
54+
*
55+
* @param WP_Admin_Bar $admin_bar The adminbar class.
56+
*
57+
* @return void
58+
*/
59+
function add_event_send_link( $admin_bar ) {
60+
global $wp;
61+
62+
$admin_bar->add_menu(
63+
[
64+
'id' => 'send-events',
65+
'title' => 'Send Events',
66+
'href' => add_query_arg( [ 'send-events' => true ], home_url( $wp->request ) ),
67+
'meta' => [
68+
'title' => __( 'Send Events' ),
69+
],
70+
]
71+
);
72+
}
73+
add_action( 'admin_bar_menu', 'add_event_send_link', 100, 1 );

src/Telemetry/Events/Event.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
*/
2222
class Event {
2323

24+
/**
25+
* The hook name for sending events asyncronously.
26+
*
27+
* @since 2.2.0
28+
*/
29+
public const AJAX_ACTION = 'stellarwp_telemetry_send_event';
30+
2431
/**
2532
* An instance of the Telemetry class.
2633
*
@@ -66,7 +73,31 @@ public function send( string $name, array $data = [] ) {
6673
*
6774
* @param array $data The data about to be sent.
6875
*/
69-
$data = apply_filters( 'stellarwp/telemetry/events_data', $data );
76+
$data = apply_filters( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'event_data', $data );
77+
78+
$response = $this->telemetry->send( $data, $this->get_url() );
79+
80+
if ( ! isset( $response['status'] ) ) {
81+
return false;
82+
}
83+
84+
return boolval( $response['status'] );
85+
}
86+
87+
/**
88+
* Send batched events.
89+
*
90+
* @since 2.2.0
91+
*
92+
* @param array $events An array of stored events to send to the telemetry server.
93+
*
94+
* @return bool
95+
*/
96+
public function send_batch( array $events ) {
97+
$data = [
98+
'token' => $this->telemetry->get_token(),
99+
'events' => $events,
100+
];
70101

71102
$response = $this->telemetry->send( $data, $this->get_url() );
72103

src/Telemetry/Events/Event_Subscriber.php

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace StellarWP\Telemetry\Events;
1111

12+
use StellarWP\Telemetry\Config;
1213
use StellarWP\Telemetry\Contracts\Abstract_Subscriber;
1314

1415
/**
@@ -28,19 +29,78 @@ class Event_Subscriber extends Abstract_Subscriber {
2829
* @return void
2930
*/
3031
public function register() {
31-
add_action( 'stellarwp/telemetry/event', [ $this, 'send_event' ], 10, 2 );
32+
add_action( 'shutdown', [ $this, 'send_cached_events' ] );
33+
add_action( 'stellarwp/telemetry/' . Config::get_hook_prefix() . 'event', [ $this, 'cache_event' ], 10, 2 );
34+
add_action( 'wp_ajax_' . Event::AJAX_ACTION, [ $this, 'send_events' ], 10, 1 );
35+
add_action( 'wp_ajax_nopriv_' . Event::AJAX_ACTION, [ $this, 'send_events' ], 10, 1 );
3236
}
3337

3438
/**
35-
* Sends an event request to the Telemetry server.
39+
* Caches an event to be sent during shutdown.
3640
*
37-
* @since 2.1.0
41+
* @since 2.2.0
42+
*
43+
* @param string $name The name of the event.
44+
* @param array $data The data sent along with the event.
3845
*
39-
* @param string $name The name of the event to send.
40-
* @param array $data Additional information to send with the event.
46+
* @return void
4147
*/
42-
public function send_event( string $name, array $data = [] ) {
43-
$this->container->get( Event::class )->send( $name, $data );
48+
public function cache_event( $name, $data ) {
49+
$events = [];
50+
51+
if ( $this->container->has( 'events' ) ) {
52+
$events = $this->container->get( 'events' );
53+
}
54+
55+
$events[] = [
56+
'name' => $name,
57+
'data' => wp_json_encode( $data ),
58+
'stellar_slug' => Config::get_stellar_slug(),
59+
];
60+
61+
$this->container->bind( 'events', $events );
4462
}
4563

64+
/**
65+
* Sends the events that have been stored for the current request.
66+
*
67+
* @since 2.2.0
68+
*
69+
* @return void
70+
*/
71+
public function send_cached_events() {
72+
if ( ! $this->container->has( 'events' ) ) {
73+
return;
74+
}
75+
76+
$url = admin_url( 'admin-ajax.php' );
77+
78+
wp_remote_post(
79+
$url,
80+
[
81+
'blocking' => false,
82+
'sslverify' => false,
83+
'body' => [
84+
'action' => Event::AJAX_ACTION,
85+
'events' => $this->container->get( 'events' ),
86+
],
87+
]
88+
);
89+
90+
$this->container->bind( 'events', [] );
91+
}
92+
93+
/**
94+
* Send the event to the telemetry server.
95+
*
96+
* @since 2.2.0
97+
*
98+
* @return void
99+
*/
100+
public function send_events() {
101+
// Get the passed event array.
102+
$events = filter_input( INPUT_POST, 'events', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); // phpcs:ignore WordPressVIPMinimum.Security.PHPFilterFunctions.RestrictedFilter
103+
104+
$this->container->get( Event::class )->send_batch( $events );
105+
}
46106
}

src/Telemetry/Opt_In/Opt_In_Subscriber.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ public function set_optin_status() {
7777
$stellar_slug = sanitize_text_field( $_POST['stellar_slug'] );
7878
}
7979

80+
$opt_in_text = '';
81+
82+
if ( isset( $_POST['opt_in_text'] ) ) {
83+
$opt_in_text = sanitize_text_field( $_POST['opt_in_text'] );
84+
}
85+
8086
// User agreed to opt-in to Telemetry.
8187
if ( 'true' === $_POST['optin-agreed'] ) {
82-
$this->opt_in( $stellar_slug );
88+
$this->opt_in( $stellar_slug, $opt_in_text );
8389
}
8490

8591
// Don't show the opt-in modal again.
@@ -134,17 +140,19 @@ public function initialize_optin_option() {
134140
*
135141
* @since 1.0.0
136142
* @since 2.0.0 - Updated to allow specifying the stellar slug.
143+
* @since 2.2.0 - Updated to add opt-in text.
137144
*
138145
* @param string $stellar_slug The slug to use when opting in.
146+
* @param string $opt_in_text The text displayed to the user when they agreed to opt-in.
139147
*
140148
* @return void
141149
*/
142-
public function opt_in( string $stellar_slug ) {
150+
public function opt_in( string $stellar_slug, string $opt_in_text = '' ) {
143151
$this->container->get( Status::class )->set_status( true, $stellar_slug );
144152

145153
try {
146154
$this->container->get( Telemetry::class )->register_site();
147-
$this->container->get( Telemetry::class )->register_user( $stellar_slug );
155+
$this->container->get( Telemetry::class )->register_user( $stellar_slug, $opt_in_text );
148156
} catch ( \Error $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
149157
// We don't want to throw errors if the server cannot be reached.
150158
}

src/Telemetry/Opt_In/Opt_In_Template.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function enqueue(): void {
6262
*
6363
* @return array
6464
*/
65-
protected function get_args( string $stellar_slug ) {
65+
public function get_args( string $stellar_slug ) {
6666

6767
$optin_args = [
6868
'plugin_logo' => Resources::get_asset_path() . 'resources/images/stellar-logo.svg',
@@ -85,18 +85,8 @@ protected function get_args( string $stellar_slug ) {
8585
__( 'We hope you love %s.', 'stellarwp-telemetry' ),
8686
$optin_args['plugin_name']
8787
);
88-
$optin_args['intro'] = sprintf(
89-
// Translators: The user name and the plugin name.
90-
__(
91-
'Hi, %1$s! This is an invitation to help our StellarWP community.
92-
If you opt-in, some data about your usage of %2$s and future StellarWP Products will be shared with our teams (so they can work their butts off to improve).
93-
We will also share some helpful info on WordPress, and our products from time to time.
94-
And if you skip this, that’s okay! Our products still work just fine.',
95-
'stellarwp-telemetry'
96-
),
97-
$optin_args['user_name'],
98-
$optin_args['plugin_name']
99-
);
88+
89+
$optin_args['intro'] = $this->get_intro( $optin_args['user_name'], $optin_args['plugin_name'] );
10090

10191
/**
10292
* Filters the arguments for rendering the Opt-In modal.
@@ -230,4 +220,27 @@ public function get_opted_in_plugin_names() {
230220

231221
return $opted_in_plugins;
232222
}
223+
224+
/**
225+
* Gets the primary message displayed on the opt-in modal.
226+
*
227+
* @param string $user_name The display name of the user.
228+
* @param string $plugin_name The name of the plugin.
229+
*
230+
* @return string
231+
*/
232+
public function get_intro( $user_name, $plugin_name ) {
233+
return sprintf(
234+
// Translators: The user name and the plugin name.
235+
esc_html__(
236+
'Hi, %1$s! This is an invitation to help our StellarWP community.
237+
If you opt-in, some data about your usage of %2$s and future StellarWP Products will be shared with our teams (so they can work their butts off to improve).
238+
We will also share some helpful info on WordPress, and our products from time to time.
239+
And if you skip this, that’s okay! Our products still work just fine.',
240+
'stellarwp-telemetry'
241+
),
242+
$user_name,
243+
$plugin_name
244+
);
245+
}
233246
}

0 commit comments

Comments
 (0)