Skip to content

Commit fa85ea0

Browse files
committed
refactor: telemetry classes are now restructured
1 parent d60d90c commit fa85ea0

File tree

12 files changed

+309
-202
lines changed

12 files changed

+309
-202
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
},
3434
"scripts": {
35-
"test:unit": "./vendor/bin/pest --testdox --colors=always tests/ --exclude-group db",
35+
"test": "./vendor/bin/pest",
3636
"compat": "./vendor/bin/phpcs -p ./src --standard=PHPCompatibility --runtime-set testVersion 5.6-",
3737
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
3838
"post-update-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility"

readme.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ composer require bitapps/wp-telemetry
1717
Initialize the telemetry client in your plugin.
1818

1919
```php
20-
use BitApps\Telemetry\Telemetry;
21-
2220
function initialize_telemetry_client() {
23-
$telemetryClient = new Client( $name, $slug, $prefix, $version );
21+
TelemetryConfig::setSlug($title);
22+
TelemetryConfig::setTitle($slug);
23+
TelemetryConfig::setPrefix($prefix);
24+
TelemetryConfig::setVersion($version);
2425

2526
// initialize tracking and reporting
26-
$telemetryClient->report();
27+
Telemetry::report()->init();
2728

2829
// initialize deactivation feedback survey
29-
$telemetryClient->feedback();
30+
Telemetry::feedback()->init();
3031
}
3132

3233
initialize_telemetry_client();
@@ -43,37 +44,39 @@ All the configuration should be done in the `initialize_telemetry_client()` func
4344
Set custom server URL
4445

4546
```php
46-
$telemetryClient->setServerUrl( 'https://example.com' );
47+
TelemetryConfig::setServerBaseUrl( 'https://example.com' );
4748
```
4849

4950
Set custom terms URL
5051

5152
```php
52-
$telemetryClient->setTermsUrl( 'https://example.com/terms' );
53+
TelemetryConfig::setTermsUrl( 'https://example.com/terms' );
5354
```
5455

5556
Set custom privacy policy URL
5657

5758
```php
58-
$telemetryClient->setPolicyUrl( 'https://example.com/privacy' );
59+
TelemetryConfig::setPolicyUrl( 'https://example.com/privacy' );
5960
```
6061

6162
### # Tracking Report Config
6263

6364
Add plugin information in tracking data
6465

6566
```php
66-
$telemetryClient->report()
67-
->addPluginData();
67+
TelemetryConfig::report()
68+
->addPluginData()
69+
->init();
6870
```
6971

7072
Add extra information in tracking data
7173

7274
```php
73-
$telemetryClient->report()
75+
TelemetryConfig::report()
7476
->addExtraInfo([
7577
'my_plugin_logs' => Log::get(),
76-
]);
78+
])
79+
->init();
7780
```
7881

7982
### # Deactivate Feedback Config

src/Telemetry/Client.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/Telemetry/Feedback/Feedback.php

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22

33
namespace BitApps\WPTelemetry\Telemetry\Feedback;
44

5-
use BitApps\WPTelemetry\Telemetry\Client;
5+
use BitApps\WPTelemetry\Telemetry\Telemetry;
6+
use BitApps\WPTelemetry\Telemetry\TelemetryConfig;
67

78
class Feedback
89
{
9-
private $client;
10-
11-
public function __construct(Client $client)
12-
{
13-
$this->client = $client;
14-
15-
$this->init();
16-
}
17-
1810
public function init()
1911
{
20-
add_action('wp_ajax_' . $this->client->prefix . 'deactivate_feedback', [$this, 'handleDeactivateFeedback']);
12+
add_action('wp_ajax_' . TelemetryConfig::getPrefix() . 'deactivate_feedback', [$this, 'handleDeactivateFeedback']);
2113

2214
add_action('current_screen', [$this, 'loadAllScripts']);
2315
}
@@ -43,8 +35,8 @@ public function enqueueFeedbackDialogScripts()
4335
add_action('admin_footer', [$this, 'printDeactivateFeedbackDialog']);
4436

4537
$cssFilePath = $this->getAssetPath() . 'resources/css/deactivateModalStyle.css';
46-
wp_register_style($this->client->prefix . 'deactivate_modal', $cssFilePath, [], $this->client->version);
47-
wp_enqueue_style($this->client->prefix . 'deactivate_modal');
38+
wp_register_style(TelemetryConfig::getPrefix() . 'deactivate_modal', $cssFilePath, [], TelemetryConfig::getVersion());
39+
wp_enqueue_style(TelemetryConfig::getPrefix() . 'deactivate_modal');
4840
}
4941

5042
public static function getAssetPath()
@@ -61,10 +53,10 @@ public static function getAssetPath()
6153
*/
6254
public function printDeactivateFeedbackDialog()
6355
{
64-
$this->client->view('deactivateModal', [
65-
'slug' => $this->client->slug,
66-
'prefix' => $this->client->prefix,
67-
'title' => $this->client->title,
56+
Telemetry::view('deactivateModal', [
57+
'slug' => TelemetryConfig::getSlug(),
58+
'prefix' => TelemetryConfig::getPrefix(),
59+
'title' => TelemetryConfig::getTitle(),
6860
'reasons' => $this->getDeactivateReasons(),
6961
]);
7062
}
@@ -73,37 +65,37 @@ public function getDeactivateReasons()
7365
{
7466
$reasons = [
7567
'found_a_better_plugin' => [
76-
'title' => esc_html__('Found a better plugin', $this->client->slug),
77-
'placeholder' => esc_html__('Which plugin?', $this->client->slug),
68+
'title' => esc_html__('Found a better plugin', TelemetryConfig::getSlug()),
69+
'placeholder' => esc_html__('Which plugin?', TelemetryConfig::getSlug()),
7870
],
7971
'missing_specific_feature' => [
80-
'title' => esc_html__('Missing a specific feature', $this->client->slug),
81-
'placeholder' => esc_html__('Could you tell us more about that feature?', $this->client->slug),
72+
'title' => esc_html__('Missing a specific feature', TelemetryConfig::getSlug()),
73+
'placeholder' => esc_html__('Could you tell us more about that feature?', TelemetryConfig::getSlug()),
8274
],
8375
'not_working' => [
84-
'title' => esc_html__('Not working', $this->client->slug),
85-
'placeholder' => esc_html__('Could you tell us what is not working?', $this->client->slug),
76+
'title' => esc_html__('Not working', TelemetryConfig::getSlug()),
77+
'placeholder' => esc_html__('Could you tell us what is not working?', TelemetryConfig::getSlug()),
8678
],
8779
'not_working_as_expected' => [
88-
'title' => esc_html__('Not working as expected', $this->client->slug),
89-
'placeholder' => esc_html__('Could you tell us what do you expect?', $this->client->slug),
80+
'title' => esc_html__('Not working as expected', TelemetryConfig::getSlug()),
81+
'placeholder' => esc_html__('Could you tell us what do you expect?', TelemetryConfig::getSlug()),
9082
],
9183
'temporary_deactivation' => [
92-
'title' => esc_html__('It\'s a temporary deactivation', $this->client->slug),
84+
'title' => esc_html__('It\'s a temporary deactivation', TelemetryConfig::getSlug()),
9385
'placeholder' => '',
9486
],
95-
$this->client->prefix . 'pro' => [
96-
'title' => esc_html__('I have ' . $this->client->title . ' Pro', $this->client->slug),
87+
TelemetryConfig::getPrefix() . 'pro' => [
88+
'title' => esc_html__('I have ' . TelemetryConfig::getTitle() . ' Pro', TelemetryConfig::getSlug()),
9789
'placeholder' => '',
98-
'alert' => esc_html__('Wait! Don\'t deactivate ' . $this->client->title . '. You have to activate both ' . $this->client->title . ' and ' . $this->client->title . ' Pro in order to work the plugin.', $this->client->slug),
90+
'alert' => esc_html__('Wait! Don\'t deactivate ' . TelemetryConfig::getTitle() . '. You have to activate both ' . TelemetryConfig::getTitle() . ' and ' . TelemetryConfig::getTitle() . ' Pro in order to work the plugin.', TelemetryConfig::getSlug()),
9991
],
10092
'other' => [
101-
'title' => esc_html__('Other', $this->client->slug),
102-
'placeholder' => esc_html__('Please share the reason', $this->client->slug),
93+
'title' => esc_html__('Other', TelemetryConfig::getSlug()),
94+
'placeholder' => esc_html__('Please share the reason', TelemetryConfig::getSlug()),
10395
],
10496
];
10597

106-
return apply_filters($this->client->prefix . 'deactivate_reasons', $reasons, $this->client);
98+
return apply_filters(TelemetryConfig::getPrefix() . 'deactivate_reasons', $reasons);
10799
}
108100

109101
/**
@@ -119,20 +111,20 @@ public function handleDeactivateFeedback()
119111
return;
120112
}
121113

122-
if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['_ajax_nonce'])), $this->client->prefix . 'nonce')) {
114+
if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['_ajax_nonce'])), TelemetryConfig::getPrefix() . 'nonce')) {
123115
wp_send_json_error('Nonce verification failed');
124116
}
125117

126118
if (!current_user_can('activate_plugins')) {
127119
wp_send_json_error('Permission denied');
128120
}
129121

130-
$report = $this->client->report->getTrackingData();
122+
$report = [];
131123
$report['site_lang'] = get_bloginfo('language');
132124
$report['feedback_key'] = sanitize_text_field(wp_unslash($_POST['reason_key'])) ?: null;
133125
$report['feedback'] = sanitize_text_field(wp_unslash($_POST["reason_{$report['feedback_key']}"])) ?: null;
134126

135-
$this->client->sendReport('deactivate-reason', $report);
127+
Telemetry::sendReport('deactivate-reason', $report);
136128

137129
wp_send_json_success();
138130
}

0 commit comments

Comments
 (0)