PluginProbe
Edwiser Bridge – WordPress Moodle Integration / trunk
Edwiser Bridge – WordPress Moodle Integration vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 All 74 releases
edwiser-bridge / includes / analytics / class-modular-analytics.php

class-modular-analytics.php in Edwiser Bridge – WordPress Moodle Integration trunk, at includes/analytics/class-modular-analytics.php

250 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace app\wisdmlabs\edwiserBridge;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 class Modular_Analytics_System
10 {
11 private static $instance;
12 private $server_url = 'https://edwiser.org/wp-json/analytics/v1/collect'; // Replace with your server URL
13 private $deactivation_url = 'https://edwiser.org/wp-json/analytics/v1/deactivate'; // URL for deactivation feedback
14 private $plugin_file = 'edwiser-bridge/edwiser-bridge.php'; //replace with your plugin file
15 private $plugin_name = 'Edwiser Bridge - WordPress Moodle LMS Integration'; //replace with your plugin name
16 private $plugin_version = '4.3.1'; //replace with your plugin version
17
18 public static function get_instance()
19 {
20 if (!isset(self::$instance)) {
21 self::$instance = new self();
22 }
23 return self::$instance;
24 }
25
26 private function __construct()
27 {
28 register_activation_hook($this->plugin_file, [$this, 'on_activation']);
29 register_deactivation_hook($this->plugin_file, [$this, 'on_deactivation']); // Add deactivation hook
30 add_action('admin_init', [$this, 'check_consent']);
31 add_action('admin_enqueue_scripts', [$this, 'enqueue_modal_scripts']); // Enqueue JS and CSS
32 add_action('admin_footer', [$this, 'set_deactivation_url']); // Set deactivation URL
33 }
34
35 public function on_activation()
36 {
37
38 $consent = get_option('modular_analytics_consent');
39
40 if ($consent === 'yes') {
41 $this->send_data();
42 } elseif ($consent === 'no') {
43 // User has opted out, do nothing
44 } else {
45 // Default to pending consent
46 add_option('modular_analytics_consent', 'pending');
47 }
48 }
49
50 public function on_deactivation()
51 {
52 add_option('modular_analytics_deactivation_feedback', 'pending');
53 }
54
55 function set_deactivation_url($plugin_file = 'edwiser-bridge/edwiser-bridge.php')
56 { //replace with your plugin file
57
58 // Check if the plugin file exists.
59 $plugins = get_plugins(); // Get all installed plugins.
60
61 if (! isset($plugins[$plugin_file])) {
62 return false; // Plugin not found.
63 }
64
65 // Get the plugin's directory.
66 $plugin_dir = dirname($plugin_file);
67
68 // Build the deactivation link. We use admin_url() for proper URL generation.
69 $deactivate_link = add_query_arg(
70 array(
71 'action' => 'deactivate',
72 'plugin' => $plugin_file,
73 'plugin_status' => 'all', // Important for multisite.
74 'paged' => 1, // Important for multisite.
75 '_wpnonce' => wp_create_nonce('deactivate-plugin_' . $plugin_file),
76 ),
77 admin_url('plugins.php')
78 );
79
80 return $deactivate_link;
81 }
82
83 public function enqueue_modal_scripts()
84 {
85
86 wp_enqueue_script('modular-analytics-modal', plugin_dir_url(__FILE__) . 'js/modal.js', ['jquery'], '1.0', true); // Path to your JS file
87
88 // Localize script to pass data to JS
89 wp_localize_script('modular-analytics-modal', 'modular_analytics_params', [
90 'ajax_url' => admin_url('admin-ajax.php'),
91 'nonce' => wp_create_nonce('modular_analytics_deactivation'),
92 'deactivation_url' => $this->set_deactivation_url(), // Pass the deactivation URL
93 'website' => get_site_url(),
94 'email' => wp_get_current_user()->user_email,
95 'ip' => $this->get_user_ip(),
96 ]);
97 }
98
99 public function check_consent()
100 {
101 $consent = get_option('modular_analytics_consent', 'pending');
102
103 if ($consent === 'pending' && current_user_can('manage_options')) {
104 add_action('admin_notices', [$this, 'consent_notice']);
105 }
106 }
107
108 public function consent_notice()
109 {
110 ?>
111 <div class="notice eb_admin_remui_demo_notice">
112 <div class="eb_remui_demo_notice_content">
113 <p style="font-size: 16px; color: #133F3F;">
114 <strong style="display: block; font-size: 18px; font-weight: 700; color: #F75D25; margin-bottom: 4px"><?php esc_html_e('Improve Edwiser Bridge: Your Data, Our Progress', 'edwiser-bridge'); ?></strong>
115 <?php esc_html_e('To help us continually improve Edwiser Bridge, please consider opting in. This allows us to gather your email address, along with basic data about your WordPress environment and how you use our plugin. Rest assured, this information is used only for plugin enhancement and is handled with the utmost care as outlined in our', 'edwiser-bridge'); ?>
116 <strong>
117 <a style="color: #F75D25;" href="https://edwiser.org/privacy-policy/" target="_blank"><?php esc_html_e('Privacy Policy', 'edwiser-bridge'); ?></a>
118 </strong>.
119 <span style="display: block; margin-top: 4px">
120 <?php esc_html_e('Your participation makes a difference!', 'edwiser-bridge'); ?></span>
121 </p>
122 <p>
123 <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin-post.php?action=modular_analytics_consent&consent=yes'), 'modular_analytics_consent')); ?>" class="button-primary" style="background-color: #F75D25; color: #fff; border: none; border-radius: 5px; cursor: pointer; font-size: 14px; font-weight: 400; margin-right: 10px;">
124 <?php esc_html_e('Allow & Continue', 'edwiser-bridge'); ?>
125 </a>
126 <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin-post.php?action=modular_analytics_consent&consent=no'), 'modular_analytics_consent')); ?>" class="button-secondary" style="background-color: #fff; color: #F75D25; border: 1px solid #F75D25; border-radius: 5px; cursor: pointer; font-size: 14px;">
127 <?php esc_html_e('Skip', 'edwiser-bridge'); ?>
128 </a>
129 </p>
130 </div>
131 </div>
132
133 <?php
134 }
135
136 public function handle_consent()
137 {
138 if (!current_user_can('manage_options') || !isset($_GET['consent'])) {
139 wp_die(esc_html__('Unauthorized action.', 'edwiser-bridge'));
140 }
141
142 if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'modular_analytics_consent' ) ) {
143 wp_die( esc_html__( 'Security check failed.', 'edwiser-bridge' ) );
144 }
145
146 $consent = sanitize_text_field($_GET['consent']);
147 if ( ! in_array( $consent, array( 'yes', 'no' ), true ) ) {
148 $consent = 'no';
149 }
150 update_option('modular_analytics_consent', $consent);
151
152 if ($consent === 'yes') {
153 $this->send_data();
154 }
155
156 wp_safe_redirect(admin_url('edit.php?post_type=eb_course&page=eb-settings'));
157 exit;
158 }
159
160 private function send_data()
161 {
162 $user = wp_get_current_user();
163 $website = get_site_url();
164 $ip = $this->get_user_ip();
165 $timestamp = current_time('mysql');
166
167 $data = [
168 'email' => sanitize_email($user->user_email),
169 'first_name' => sanitize_text_field($user->first_name),
170 'last_name' => sanitize_text_field($user->last_name),
171 'website' => esc_url_raw($website),
172 'site_name' => sanitize_text_field(get_bloginfo('name')),
173 'wp_version' => sanitize_text_field(get_bloginfo('version')),
174 'php_version' => sanitize_text_field(phpversion()),
175 'plugin_name' => $this->plugin_name,
176 'plugin_version' => $this->plugin_version,
177 'ip' => sanitize_text_field($ip),
178 'timestamp' => sanitize_text_field($timestamp),
179 ];
180
181 wp_remote_post($this->server_url, [
182 'method' => 'POST',
183 'body' => wp_json_encode($data),
184 'headers' => ['Content-Type' => 'application/json'],
185 'timeout' => 10,
186 ]);
187 }
188
189 private function get_user_ip()
190 {
191 if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
192 return $_SERVER['HTTP_CLIENT_IP'];
193 } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
194 return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
195 } else {
196 return $_SERVER['REMOTE_ADDR'];
197 }
198 }
199
200 public function handle_deactivation_feedback_ajax()
201 {
202 check_ajax_referer('modular_analytics_deactivation', 'nonce');
203
204 if (!isset($_POST['reason'])) {
205 wp_send_json_error(['message' => 'Reason is required']);
206 }
207
208 $reason = sanitize_text_field($_POST['reason']);
209
210 $response = $this->send_deactivation_feedback($reason);
211 if (is_wp_error($response)) {
212 wp_send_json_error(['message' => $response->get_error_message()]);
213 } else {
214 update_option('modular_analytics_deactivation_feedback', 'sent');
215 wp_send_json_success();
216 }
217 }
218
219 private function send_deactivation_feedback($reason)
220 {
221 $data = [
222 'email' => sanitize_email(wp_get_current_user()->user_email),
223 'website' => esc_url_raw(get_site_url()),
224 'plugin_name' => $this->plugin_name,
225 'ip' => sanitize_text_field($this->get_user_ip()),
226 'timestamp' => sanitize_text_field(current_time('mysql')),
227 'reason' => $reason,
228 ];
229
230 $response = wp_remote_post($this->deactivation_url, [
231 'method' => 'POST',
232 'body' => wp_json_encode($data),
233 'headers' => ['Content-Type' => 'application/json'],
234 'timeout' => 10,
235 ]);
236
237 return $response;
238 }
239
240 public function handle_dismiss_feedback()
241 {
242 check_ajax_referer('modular_analytics_dismiss', 'nonce');
243 update_option('modular_analytics_deactivation_feedback', 'dismissed');
244 wp_die();
245 }
246 }
247
248 // Register the consent handler
249 add_action('admin_post_modular_analytics_consent', [Modular_Analytics_System::get_instance(), 'handle_consent']);
250