|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BitApps\WPTelemetry\Telemetry\Feedback; |
| 4 | + |
| 5 | +use BitApps\WPTelemetry\Telemetry\Client; |
| 6 | + |
| 7 | +class Feedback |
| 8 | +{ |
| 9 | + private $client; |
| 10 | + |
| 11 | + public function __construct(Client $client) |
| 12 | + { |
| 13 | + $this->client = $client; |
| 14 | + |
| 15 | + $this->init(); |
| 16 | + } |
| 17 | + |
| 18 | + public function init() |
| 19 | + { |
| 20 | + add_action('wp_ajax_' . $this->client->prefix . 'deactivate_feedback', [$this, 'handleDeactivateFeedback']); |
| 21 | + |
| 22 | + add_action('current_screen', [$this, 'loadAllScripts']); |
| 23 | + } |
| 24 | + |
| 25 | + public function loadAllScripts() |
| 26 | + { |
| 27 | + if (!$this->is_plugins_screen()) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + add_action('admin_enqueue_scripts', [$this, 'enqueueFeedbackDialogScripts']); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Enqueue feedback dialog scripts. |
| 36 | + * |
| 37 | + * Registers the feedback dialog scripts and enqueues them. |
| 38 | + * |
| 39 | + * @since 0.0.1 |
| 40 | + */ |
| 41 | + public function enqueueFeedbackDialogScripts() |
| 42 | + { |
| 43 | + add_action('admin_footer', [$this, 'printDeactivateFeedbackDialog']); |
| 44 | + |
| 45 | + $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'); |
| 48 | + } |
| 49 | + |
| 50 | + public static function getAssetPath() |
| 51 | + { |
| 52 | + return plugin_dir_url(\dirname(__DIR__)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Print deactivate feedback dialog. |
| 57 | + * |
| 58 | + * Display a dialog box to ask the user why he deactivated this plugin. |
| 59 | + * |
| 60 | + * @since 0.0.1 |
| 61 | + */ |
| 62 | + public function printDeactivateFeedbackDialog() |
| 63 | + { |
| 64 | + $this->client->view('deactivateModal', [ |
| 65 | + 'slug' => $this->client->slug, |
| 66 | + 'prefix' => $this->client->prefix, |
| 67 | + 'title' => $this->client->title, |
| 68 | + 'logo' => $this->client->logo, |
| 69 | + 'reasons' => $this->getDeactivateReasons(), |
| 70 | + ]); |
| 71 | + } |
| 72 | + |
| 73 | + public function getDeactivateReasons() |
| 74 | + { |
| 75 | + $reasons = [ |
| 76 | + 'found_a_better_plugin' => [ |
| 77 | + 'title' => esc_html__('Found a better plugin', $this->client->slug), |
| 78 | + 'placeholder' => esc_html__('Which plugin?', $this->client->slug), |
| 79 | + ], |
| 80 | + 'missing_specific_feature' => [ |
| 81 | + 'title' => esc_html__('Missing a specific featureMissing a specific featureMissing a specific feature featureMissing a specific feature', $this->client->slug), |
| 82 | + 'placeholder' => esc_html__('Could you tell us more about that feature?', $this->client->slug), |
| 83 | + ], |
| 84 | + 'not_working' => [ |
| 85 | + 'title' => esc_html__('Not working', $this->client->slug), |
| 86 | + 'placeholder' => esc_html__('Could you tell us what is not working?', $this->client->slug), |
| 87 | + ], |
| 88 | + 'not_working_as_expected' => [ |
| 89 | + 'title' => esc_html__('Not working as expected', $this->client->slug), |
| 90 | + 'placeholder' => esc_html__('Could you tell us what do you expect?', $this->client->slug), |
| 91 | + ], |
| 92 | + 'temporary_deactivation' => [ |
| 93 | + 'title' => esc_html__('It\'s a temporary deactivation', $this->client->slug), |
| 94 | + 'placeholder' => '', |
| 95 | + ], |
| 96 | + $this->client->prefix . 'pro' => [ |
| 97 | + 'title' => esc_html__('I have ' . $this->client->title . ' Pro', $this->client->slug), |
| 98 | + 'placeholder' => '', |
| 99 | + '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), |
| 100 | + ], |
| 101 | + 'other' => [ |
| 102 | + 'title' => esc_html__('Other', $this->client->slug), |
| 103 | + 'placeholder' => esc_html__('Please share the reason', $this->client->slug), |
| 104 | + ], |
| 105 | + ]; |
| 106 | + |
| 107 | + return apply_filters($this->client->prefix . 'deactivate_reasons', $reasons, $this->client); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Ajax plugin deactivate feedback. |
| 112 | + * |
| 113 | + * Send the user feedback when plugin is deactivated. |
| 114 | + * |
| 115 | + * @since 0.0.1 |
| 116 | + */ |
| 117 | + public function handleDeactivateFeedback() |
| 118 | + { |
| 119 | + if (!isset($_POST['_ajax_nonce'])) { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (!wp_verify_nonce(sanitize_key(wp_unslash($_POST['_ajax_nonce'])), $this->client->prefix . 'nonce')) { |
| 124 | + wp_send_json_error('Nonce verification failed'); |
| 125 | + } |
| 126 | + |
| 127 | + if (!current_user_can('activate_plugins')) { |
| 128 | + wp_send_json_error('Permission denied'); |
| 129 | + } |
| 130 | + |
| 131 | + $report = $this->client->report->getTrackingData(); |
| 132 | + $report['site_lang'] = get_bloginfo('language'); |
| 133 | + $report['feedback_key'] = sanitize_text_field(wp_unslash($_POST['reason_key'])) ?: null; |
| 134 | + $report['feedback'] = sanitize_text_field(wp_unslash($_POST["reason_{$report['feedback_key']}"])) ?: null; |
| 135 | + |
| 136 | + $this->client->sendReport('deactivate-reason', $report); |
| 137 | + |
| 138 | + wp_send_json_success(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @since 0.0.1 |
| 143 | + */ |
| 144 | + private function is_plugins_screen() |
| 145 | + { |
| 146 | + return \in_array(get_current_screen()->id, ['plugins', 'plugins-network']); |
| 147 | + } |
| 148 | +} |
0 commit comments