Skip to content

Commit

Permalink
initialize 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
shuvomohajan committed Mar 3, 2024
0 parents commit 8da6782
Show file tree
Hide file tree
Showing 12 changed files with 1,555 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tests export-ignore
/phpunit.xml export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.lock
.phpunit.result.cache
/phpunit.xml
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "bitapps/wp-telemetry",
"description": "Collect diagnostic data and deactivation reason for a WordPress plugin",
"type": "library",
"homepage": "https://bitapps.pro",
"license": "GPL-2.0-or-later",
"version": "0.0.1",
"keywords": [
"diagnostic",
"data",
"collect",
"deactivation",
"reason"
],
"support": {
"issues": "https://github.com/Bit-Apps-Pro/wp-telemetry/issues",
"source": "https://github.com/Bit-Apps-Pro/wp-telemetry"
},
"authors": [
{
"email": "[email protected]",
"name": "BitApps"
}
],
"autoload": {
"psr-4": {
"BitApps\\WPTelemetry\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"BitApps\\WPTelemetry\\Tests\\": [
"tests/"
]
}
},
"scripts": {
"test:unit": "./vendor/bin/pest --testdox --colors=always tests/ --exclude-group db",
"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"
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"pestphp/pest": "3.x-dev",
"squizlabs/php_codesniffer": "*",
"phpcompatibility/php-compatibility": "*"
},
"minimum-stability": "dev",
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
Empty file added readme.md
Empty file.
109 changes: 109 additions & 0 deletions src/Telemetry/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace BitApps\WPTelemetry\Telemetry;

use BitApps\WPTelemetry\Telemetry\Feedback\Feedback;
use BitApps\WPTelemetry\Telemetry\Report\Report;

class Client
{
public $report;

public $feedback;

public $title;

public $slug;

public $prefix;

public $version;

public $logo;

public $telemetryVersion = '0.1.0';

public $termsUrl = 'https://bitapps.pro/terms';

public $policyUrl = 'https://bitapps.pro/privacy-policy';

public $apiBaseUrl = 'https://wp-api.bitapps.pro/public/';

public function __construct($title, $slug, $prefix, $version)
{
$this->title = $title;

$this->slug = $slug;

$this->prefix = $prefix;

$this->version = $version;
}

public function report()
{
if (!$this->report) {
$this->report = new Report($this);
}

return $this->report;
}

public function feedback()
{
if (!$this->feedback) {
$this->feedback = new Feedback($this);
}

return $this->feedback;
}

public function setLogo($logo)
{
$this->logo = $logo;
}

public function setTermsUrl($url)
{
$this->termsUrl = $url;
}

public function setPolicyUrl($url)
{
$this->policyUrl = $url;
}

public function setServerUrl($url)
{
$this->apiBaseUrl = trailingslashit($url);
}

public function view($fileName, $args)
{
load_template(\dirname(\dirname(__DIR__)) . '/src/views/' . $fileName . '.php', false, $args);
}

public function sendReport($route, $data, $blocking = false)
{
$apiUrl = $this->apiBaseUrl . $route;

$headers = [
'host-user' => 'BitApps/' . md5(esc_url(home_url())),
'Content-Type' => 'application/json',
];

return wp_remote_post(
$apiUrl,
[
'method' => 'POST',
'timeout' => 30,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => $blocking,
'headers' => $headers,
'body' => wp_json_encode(array_merge($data, ['wp_telemetry' => $this->telemetryVersion])),
'cookies' => [],
]
);
}
}
148 changes: 148 additions & 0 deletions src/Telemetry/Feedback/Feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace BitApps\WPTelemetry\Telemetry\Feedback;

use BitApps\WPTelemetry\Telemetry\Client;

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('current_screen', [$this, 'loadAllScripts']);
}

public function loadAllScripts()
{
if (!$this->is_plugins_screen()) {
return;
}

add_action('admin_enqueue_scripts', [$this, 'enqueueFeedbackDialogScripts']);
}

/**
* Enqueue feedback dialog scripts.
*
* Registers the feedback dialog scripts and enqueues them.
*
* @since 0.0.1
*/
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');
}

public static function getAssetPath()
{
return plugin_dir_url(\dirname(__DIR__));
}

/**
* Print deactivate feedback dialog.
*
* Display a dialog box to ask the user why he deactivated this plugin.
*
* @since 0.0.1
*/
public function printDeactivateFeedbackDialog()
{
$this->client->view('deactivateModal', [
'slug' => $this->client->slug,
'prefix' => $this->client->prefix,
'title' => $this->client->title,
'logo' => $this->client->logo,
'reasons' => $this->getDeactivateReasons(),
]);
}

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),
],
'missing_specific_feature' => [
'title' => esc_html__('Missing a specific featureMissing a specific featureMissing a specific feature featureMissing a specific feature', $this->client->slug),
'placeholder' => esc_html__('Could you tell us more about that feature?', $this->client->slug),
],
'not_working' => [
'title' => esc_html__('Not working', $this->client->slug),
'placeholder' => esc_html__('Could you tell us what is not working?', $this->client->slug),
],
'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),
],
'temporary_deactivation' => [
'title' => esc_html__('It\'s a temporary deactivation', $this->client->slug),
'placeholder' => '',
],
$this->client->prefix . 'pro' => [
'title' => esc_html__('I have ' . $this->client->title . ' Pro', $this->client->slug),
'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),
],
'other' => [
'title' => esc_html__('Other', $this->client->slug),
'placeholder' => esc_html__('Please share the reason', $this->client->slug),
],
];

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

/**
* Ajax plugin deactivate feedback.
*
* Send the user feedback when plugin is deactivated.
*
* @since 0.0.1
*/
public function handleDeactivateFeedback()
{
if (!isset($_POST['_ajax_nonce'])) {
return;
}

if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['_ajax_nonce'])), $this->client->prefix . '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['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);

wp_send_json_success();
}

/**
* @since 0.0.1
*/
private function is_plugins_screen()
{
return \in_array(get_current_screen()->id, ['plugins', 'plugins-network']);
}
}
Loading

0 comments on commit 8da6782

Please sign in to comment.