Skip to content

Commit

Permalink
refactor: telemetry classes are now restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
shuvomohajan committed Mar 5, 2024
1 parent d60d90c commit fa85ea0
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 202 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"scripts": {
"test:unit": "./vendor/bin/pest --testdox --colors=always tests/ --exclude-group db",
"test": "./vendor/bin/pest",
"compat": "./vendor/bin/phpcs -p ./src --standard=PHPCompatibility --runtime-set testVersion 5.6-",
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"post-update-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility"
Expand Down
27 changes: 15 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ composer require bitapps/wp-telemetry
Initialize the telemetry client in your plugin.

```php
use BitApps\Telemetry\Telemetry;

function initialize_telemetry_client() {
$telemetryClient = new Client( $name, $slug, $prefix, $version );
TelemetryConfig::setSlug($title);
TelemetryConfig::setTitle($slug);
TelemetryConfig::setPrefix($prefix);
TelemetryConfig::setVersion($version);

// initialize tracking and reporting
$telemetryClient->report();
Telemetry::report()->init();

// initialize deactivation feedback survey
$telemetryClient->feedback();
Telemetry::feedback()->init();
}

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

```php
$telemetryClient->setServerUrl( 'https://example.com' );
TelemetryConfig::setServerBaseUrl( 'https://example.com' );
```

Set custom terms URL

```php
$telemetryClient->setTermsUrl( 'https://example.com/terms' );
TelemetryConfig::setTermsUrl( 'https://example.com/terms' );
```

Set custom privacy policy URL

```php
$telemetryClient->setPolicyUrl( 'https://example.com/privacy' );
TelemetryConfig::setPolicyUrl( 'https://example.com/privacy' );
```

### # Tracking Report Config

Add plugin information in tracking data

```php
$telemetryClient->report()
->addPluginData();
TelemetryConfig::report()
->addPluginData()
->init();
```

Add extra information in tracking data

```php
$telemetryClient->report()
TelemetryConfig::report()
->addExtraInfo([
'my_plugin_logs' => Log::get(),
]);
])
->init();
```

### # Deactivate Feedback Config
Expand Down
102 changes: 0 additions & 102 deletions src/Telemetry/Client.php

This file was deleted.

62 changes: 27 additions & 35 deletions src/Telemetry/Feedback/Feedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@

namespace BitApps\WPTelemetry\Telemetry\Feedback;

use BitApps\WPTelemetry\Telemetry\Client;
use BitApps\WPTelemetry\Telemetry\Telemetry;
use BitApps\WPTelemetry\Telemetry\TelemetryConfig;

class Feedback
{
private $client;

public function __construct(Client $client)
{
$this->client = $client;

$this->init();
}

public function init()
{
add_action('wp_ajax_' . $this->client->prefix . 'deactivate_feedback', [$this, 'handleDeactivateFeedback']);
add_action('wp_ajax_' . TelemetryConfig::getPrefix() . 'deactivate_feedback', [$this, 'handleDeactivateFeedback']);

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

$cssFilePath = $this->getAssetPath() . 'resources/css/deactivateModalStyle.css';
wp_register_style($this->client->prefix . 'deactivate_modal', $cssFilePath, [], $this->client->version);
wp_enqueue_style($this->client->prefix . 'deactivate_modal');
wp_register_style(TelemetryConfig::getPrefix() . 'deactivate_modal', $cssFilePath, [], TelemetryConfig::getVersion());
wp_enqueue_style(TelemetryConfig::getPrefix() . 'deactivate_modal');
}

public static function getAssetPath()
Expand All @@ -61,10 +53,10 @@ public static function getAssetPath()
*/
public function printDeactivateFeedbackDialog()
{
$this->client->view('deactivateModal', [
'slug' => $this->client->slug,
'prefix' => $this->client->prefix,
'title' => $this->client->title,
Telemetry::view('deactivateModal', [
'slug' => TelemetryConfig::getSlug(),
'prefix' => TelemetryConfig::getPrefix(),
'title' => TelemetryConfig::getTitle(),
'reasons' => $this->getDeactivateReasons(),
]);
}
Expand All @@ -73,37 +65,37 @@ public function getDeactivateReasons()
{
$reasons = [
'found_a_better_plugin' => [
'title' => esc_html__('Found a better plugin', $this->client->slug),
'placeholder' => esc_html__('Which plugin?', $this->client->slug),
'title' => esc_html__('Found a better plugin', TelemetryConfig::getSlug()),
'placeholder' => esc_html__('Which plugin?', TelemetryConfig::getSlug()),
],
'missing_specific_feature' => [
'title' => esc_html__('Missing a specific feature', $this->client->slug),
'placeholder' => esc_html__('Could you tell us more about that feature?', $this->client->slug),
'title' => esc_html__('Missing a specific feature', TelemetryConfig::getSlug()),
'placeholder' => esc_html__('Could you tell us more about that feature?', TelemetryConfig::getSlug()),
],
'not_working' => [
'title' => esc_html__('Not working', $this->client->slug),
'placeholder' => esc_html__('Could you tell us what is not working?', $this->client->slug),
'title' => esc_html__('Not working', TelemetryConfig::getSlug()),
'placeholder' => esc_html__('Could you tell us what is not working?', TelemetryConfig::getSlug()),
],
'not_working_as_expected' => [
'title' => esc_html__('Not working as expected', $this->client->slug),
'placeholder' => esc_html__('Could you tell us what do you expect?', $this->client->slug),
'title' => esc_html__('Not working as expected', TelemetryConfig::getSlug()),
'placeholder' => esc_html__('Could you tell us what do you expect?', TelemetryConfig::getSlug()),
],
'temporary_deactivation' => [
'title' => esc_html__('It\'s a temporary deactivation', $this->client->slug),
'title' => esc_html__('It\'s a temporary deactivation', TelemetryConfig::getSlug()),
'placeholder' => '',
],
$this->client->prefix . 'pro' => [
'title' => esc_html__('I have ' . $this->client->title . ' Pro', $this->client->slug),
TelemetryConfig::getPrefix() . 'pro' => [
'title' => esc_html__('I have ' . TelemetryConfig::getTitle() . ' Pro', TelemetryConfig::getSlug()),
'placeholder' => '',
'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),
'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()),
],
'other' => [
'title' => esc_html__('Other', $this->client->slug),
'placeholder' => esc_html__('Please share the reason', $this->client->slug),
'title' => esc_html__('Other', TelemetryConfig::getSlug()),
'placeholder' => esc_html__('Please share the reason', TelemetryConfig::getSlug()),
],
];

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

/**
Expand All @@ -119,20 +111,20 @@ public function handleDeactivateFeedback()
return;
}

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

if (!current_user_can('activate_plugins')) {
wp_send_json_error('Permission denied');
}

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

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

wp_send_json_success();
}
Expand Down
Loading

0 comments on commit fa85ea0

Please sign in to comment.