PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.9.9
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.9.9
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
← All changes | admin/dci/insights.php +733 -393 2.3.0 → 2.9.9 View file →
@@ -1,393 +1,733 @@
1 -<?php
2 -/**
3 - * Insights SDK File
4 - * SDK Version 1.0.0
5 - */
6 -
7 -if ( ! defined( 'ABSPATH' ) ) {
8 - exit;
9 -}
10 -
11 -
12 -if ( ! class_exists( 'Insights_SDK' ) ) {
13 -
14 - /**
15 - * Insights SDK Class
16 - */
17 - class Insights_SDK {
18 -
19 - public $version = '1.0.0';
20 -
21 - public $dci_name;
22 - public $dci_allow_name;
23 - public $dci_date_name;
24 - public $dci_count_name;
25 - public $nonce;
26 - public $params;
27 -
28 - /**
29 - * Insights SDK Version
30 - * param array $params
31 - * @return void
32 - */
33 - public function __construct( $params ) {
34 - $this->params = $params;
35 -
36 - add_action( 'admin_enqueue_scripts', array( $this, 'dci_enqueue_scripts' ) );
37 - add_action( 'wp_ajax_dci_sdk_insights', array( $this, 'dci_sdk_insights' ) );
38 - add_action( 'wp_ajax_dci_sdk_dismiss_notice', array( $this, 'dci_sdk_dismiss_notice' ) );
39 -
40 - $security_key = md5( $params['public_key'] . $params['menu_slug'] );
41 - $this->dci_name = str_replace( '-', '_', sanitize_title( $params['plugin_title'] ) . $security_key );
42 - $this->dci_allow_name = 'dci_allow_status_' . $security_key;
43 - $this->dci_date_name = 'dci_status_date_' . $security_key;
44 - $dci_count_name = 'dci_attempt_count_' . $security_key;
45 - $dci_status_db = get_option( $this->dci_allow_name, false );
46 -
47 - // if ( $params['current_page'] !== $params['menu_slug'] ) {
48 - // return;
49 - // }
50 -
51 - $this->nonce = wp_create_nonce( $this->dci_allow_name );
52 -
53 - /**
54 - * Modal Trigger if not init
55 - * Show Notice Modal
56 - */
57 - if ( ! $dci_status_db ) {
58 - // echo '111111111111';
59 - $this->notice_modal( $params );
60 - return;
61 - }
62 -
63 - /**
64 - * Skip & Date Not Expired
65 - * Show Notice Modal
66 - */
67 - if ( 'skip' == $dci_status_db && true == $this->check_date() ) {
68 - // echo '222222222222';
69 - $this->notice_modal( $params );
70 - return;
71 - }
72 -
73 - // echo 'yyyyyyyyyyyyyyy-';
74 -
75 - // var_dump( $this->check_date() );
76 -
77 - /**
78 - * Allowed & Date not Expired
79 - * No need send data to server
80 - * Else Send Data to Server
81 - */
82 - if ( ! $this->check_date() ) {
83 - return;
84 - }
85 - // echo 'zzzzzzzzzzzzzzz-';
86 -
87 - /**
88 - * Count attempt every time
89 - */
90 - $dci_attempt = get_option( $dci_count_name, 0 );
91 -
92 - if ( ! $dci_attempt ) {
93 - update_option( $dci_count_name, 1 );
94 - }
95 - update_option( $dci_count_name, $dci_attempt + 1 );
96 -
97 - /**
98 - * Next schedule date for attempt
99 - */
100 - update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
101 -
102 - /**
103 - * Prepare data
104 - */
105 - $this->data_prepare( $params );
106 -
107 - }
108 -
109 - /**
110 - * Notice Modal
111 - *
112 - * @return void
113 - */
114 - public function notice_modal( $params ) {
115 - if ( $params['current_page'] !== $params['menu_slug'] ) {
116 - if ( ! get_transient( 'dismissed_notice_' . $this->dci_name ) ) {
117 - add_action( 'admin_notices', array( $this, 'display_global_notice' ) );
118 - }
119 - return;
120 - }
121 - require_once dirname( __FILE__ ) . '/notice.php';
122 - }
123 -
124 - /**
125 - * If date is expired immidiate action
126 - *
127 - * @return boolean
128 - */
129 - public function check_date() {
130 - $current_date = strtotime( gmdate( 'Y-m-d' ) );
131 - $dci_status_date = strtotime( get_option( $this->dci_date_name, false ) );
132 -
133 - if ( ! $dci_status_date ) {
134 - return true;
135 - }
136 -
137 - if ( $dci_status_date && $current_date >= $dci_status_date ) {
138 - return true;
139 - }
140 - return false;
141 - }
142 -
143 - /**
144 - * Modal Trigger
145 - *
146 - * @return boolean
147 - */
148 - public function modal_trigger() {
149 -
150 - if ( ! wp_verify_nonce( $this->dci_allow_name, $this->nonce ) ) {
151 - echo 'Nonce Verification Failed';
152 - return false;
153 - }
154 -
155 - $sanitized_status = sanitize_text_field( $_GET['dci_allow_status'] );
156 -
157 - if ( $sanitized_status == 'skip' ) {
158 - update_option( $this->dci_allow_name, 'skip' );
159 - /**
160 - * Next schedule date for attempt
161 - */
162 - update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
163 - return false;
164 - } elseif ( $sanitized_status == 'yes' ) {
165 - update_option( $this->dci_allow_name, 'yes' );
166 - return true;
167 - }
168 -
169 - return false;
170 -
171 - }
172 -
173 - /**
174 - * Reset Options Settings
175 - * @return void
176 - */
177 - public function reset_settings() {
178 - delete_option( $this->dci_allow_name );
179 - delete_option( $this->dci_date_name );
180 - }
181 -
182 - /**
183 - * Data prepare for send server
184 - *
185 - * @param array $server_url
186 - * @return void
187 - */
188 - public function data_prepare( $params ) {
189 - $server_url = isset( $params['api_endpoint'] ) ? $params['api_endpoint'] : false;
190 - $public_key = isset( $params['public_key'] ) ? $params['public_key'] : false;
191 - $custom_data = isset( $params['custom_data'] ) ? $params['custom_data'] : false;
192 - $product_id = isset( $params['product_id'] ) ? $params['product_id'] : false;
193 -
194 - if ( ! $server_url || ! $public_key ) {
195 - return;
196 - }
197 -
198 - $data = array();
199 - $data['public_key'] = $public_key;
200 - $data['product_id'] = $product_id;
201 - $data['custom_data'] = $custom_data;
202 -
203 - $non_sensitive_data = $this->dci_non_sensitve_data();
204 - $data = array_merge( $data, $non_sensitive_data );
205 -
206 - $this->dci_send_data_to_server( $server_url, $data );
207 - }
208 -
209 - /**
210 - * Non sensitive data
211 - *
212 - * @return array
213 - */
214 - public function dci_non_sensitve_data() {
215 - $current_user = wp_get_current_user();
216 -
217 - $first_name = $current_user->first_name;
218 - $last_name = $current_user->last_name;
219 -
220 - if ( empty( $first_name ) && empty( $last_name ) ) {
221 - $first_name = null;
222 - $last_name = $current_user->display_name;
223 - }
224 -
225 - $data = array(
226 - 'first_name' => $first_name,
227 - 'last_name' => $last_name,
228 - 'email' => $current_user->user_email,
229 - 'user_role' => $current_user->roles[0],
230 - 'website_url' => $current_user->user_url,
231 - 'website_data' => array(
232 - 'website_name' => get_bloginfo( 'name' ),
233 - 'wp_version' => get_bloginfo( 'version' ),
234 - 'php_version' => phpversion(),
235 - 'locale' => get_locale(),
236 - 'sdk_version' => $this->version,
237 - ),
238 - );
239 -
240 - return $data;
241 - }
242 -
243 - /**
244 - * Send data to server
245 - *
246 - * @param [string] $server_url
247 - * @param [array] $data
248 - * @return void
249 - */
250 - public function dci_send_data_to_server( $server_url, $data = null ) {
251 -
252 - $args = array(
253 - 'method' => 'POST',
254 - 'timeout' => 60,
255 - 'headers' => array(
256 - 'Content-Type' => 'application/json',
257 - 'X-API-KEY' => $data['public_key'],
258 - ),
259 - 'body' => json_encode( $data ),
260 - );
261 -
262 - $response = wp_remote_request( $server_url, $args );
263 -
264 - if ( is_wp_error( $response ) ) {
265 - // echo 'Error: ' . $response->get_error_message();
266 - $this->reset_settings();
267 - } else {
268 - $response_data = wp_remote_retrieve_body( $response );
269 - $response_data = json_decode( $response_data, true );
270 - // print_r( $response_data );
271 - if ( isset( $response_data['data']['status'] ) && 401 == $response_data['data']['status'] ) {
272 - update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+3 days" ) ) );
273 - }
274 - }
275 - }
276 -
277 - /**
278 - * Ajax callback
279 - */
280 - public function dci_sdk_insights() {
281 - $sanitized_status = isset( $_POST['button_val'] ) ? sanitize_text_field( $_POST['button_val'] ) : '';
282 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
283 -
284 - if ( ! wp_verify_nonce( $nonce, 'dci_sdk' ) ) {
285 - wp_send_json( array(
286 - 'status' => 'error',
287 - 'title' => 'Error',
288 - 'message' => 'Nonce verification failed',
289 - ) );
290 - wp_die();
291 - }
292 -
293 - if ( $sanitized_status == 'skip' ) {
294 - update_option( $this->dci_allow_name, 'skip' );
295 - /**
296 - * Next schedule date for attempt
297 - */
298 - update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
299 - } elseif ( $sanitized_status == 'yes' ) {
300 - update_option( $this->dci_allow_name, 'yes' );
301 - }
302 -
303 - wp_send_json( array(
304 - 'status' => 'success',
305 - 'title' => 'Success',
306 - 'message' => 'Success.',
307 - ) );
308 - wp_die();
309 - }
310 -
311 - /**
312 - * Enqueue scripts and styles.
313 - *
314 - * @since 1.0.0
315 - */
316 - public function dci_enqueue_scripts() {
317 - wp_enqueue_style( 'dci-sdk', plugins_url( 'assets/css/dci.css', __FILE__ ), array(), '1.0.0' );
318 - wp_enqueue_script( 'dci-sdk', plugins_url( 'assets/js/dci.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
319 - wp_localize_script( 'dci-sdk', 'DCI_SDK',
320 - array(
321 - 'ajax_url' => admin_url( 'admin-ajax.php' ),
322 - 'nonce' => wp_create_nonce( 'dci_sdk' )
323 - )
324 - );
325 - }
326 -
327 - /**
328 - * Display Global Notice
329 - *
330 - * @return void
331 - */
332 - public function display_global_notice() {
333 - $admin_url = add_query_arg( array(
334 - 'page' => 'dci-app',
335 - ), admin_url( 'admin.php' ) );
336 -
337 - ?>
338 - <div class="dci-global-notice notice notice-success is-dismissible">
339 - <h3>
340 - <?php echo esc_html( $this->params['plugin_title'] ); ?>
341 - </h3>
342 - <Your>This is a notice about non-sensitive data collection and product updates. We kindly request your consent to collect
343 - and use non-sensitive data for the purpose of keeping your product up to date. </p>
344 - <p>If you have any concerns or questions about our data collection
345 - practices, please <a target="_blank" rel="nofollow noreferrer" href="https://gutenbergkits.com/contact">contact us</a>. What we <a target="_blank" rel="nofollow noreferrer" href="https://gutenbergkits.com/privacy-policy">collect</a>?
346 - </p>
347 - <p>
348 - <button name="dci_allow_status" value="yes" class="button button-primary dci-button-allow">Allow</button>
349 - <button name="dci_allow_status" value="skip" class="button dci-button-skip button-secondary">Skip</button>
350 - </p>
351 - </div>
352 - <?php
353 - }
354 -
355 - /**
356 - * Dismiss Notice
357 - *
358 - * @return void
359 - */
360 - public function dci_sdk_dismiss_notice() {
361 - $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
362 -
363 - if ( ! wp_verify_nonce( $nonce, 'dci_sdk' ) ) {
364 - wp_send_json( array(
365 - 'status' => 'error',
366 - 'title' => 'Error',
367 - 'message' => 'Nonce verification failed',
368 - ) );
369 - wp_die();
370 - }
371 -
372 - set_transient( 'dismissed_notice_' . $this->dci_name, true, 30 * DAY_IN_SECONDS );
373 -
374 - wp_send_json( array(
375 - 'status' => 'success',
376 - 'title' => 'Success',
377 - 'message' => 'Success.',
378 - ) );
379 - wp_die();
380 - }
381 - }
382 -}
383 -
384 -/**
385 - * Main Insights Function
386 - */
387 -if ( ! function_exists( 'dci_sdk_insights' ) ) {
388 - function dci_sdk_insights( $params ) {
389 - if ( class_exists( 'Insights_SDK' ) ) {
390 - $Insights_SDK = new Insights_SDK( $params );
391 - }
392 - }
393 -}
1 +<?php
2 +
3 +/**
4 + * Insights SDK File
5 + * SDK Version 1.0.0
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 + exit;
10 +}
11 +
12 +if ( ! class_exists( 'Insights_SDK' ) ) {
13 +
14 + /**
15 + * Insights SDK Class
16 + */
17 + class Insights_SDK {
18 +
19 + public $version = '1.3.0';
20 +
21 + public $dci_name;
22 + public $dci_allow_name;
23 + public $dci_date_name;
24 + public $dci_count_name;
25 + public $nonce;
26 + public $params;
27 + public $text_domain;
28 +
29 + /**
30 + * Insights SDK Version
31 + * param array $params
32 + * @return void
33 + */
34 + public function __construct( $params ) {
35 + $this->params = $params;
36 + $this->text_domain = $params['text_domain'];
37 +
38 + add_action( 'wp_ajax_dci_sdk_insights', array( $this, 'dci_sdk_insights' ) );
39 + add_action( 'wp_ajax_dci_sdk_dismiss_notice', array( $this, 'dci_sdk_dismiss_notice' ) );
40 + add_action( 'wp_ajax_dci_sdk_insights_deactivate_feedback', array( $this, 'insights_deactivate_feedback' ) );
41 +
42 + $security_key = md5( $params['plugin_name'] );
43 + $this->dci_name = 'dci_' . str_replace( '-', '_', sanitize_title( $params['plugin_name'] ) . '_' . $security_key );
44 + $this->dci_allow_name = 'dci_allow_status_' . $this->dci_name;
45 + $this->dci_date_name = 'dci_status_date_' . $this->dci_name;
46 + $dci_count_name = 'dci_attempt_count_' . $this->dci_name;
47 + $dci_status_db = get_option( $this->dci_allow_name, false );
48 +
49 + $this->nonce = wp_create_nonce( $this->dci_allow_name );
50 +
51 + /**
52 + * Deactivate Feedback
53 + * Visible only in plugins.php
54 + */
55 + $this->deactivation_feedback( $params );
56 +
57 + /**
58 + * Modal Trigger if not init
59 + * Show Notice Modal
60 + */
61 + if ( ! $dci_status_db ) {
62 + $this->notice_modal( $params );
63 + return;
64 + }
65 +
66 + /**
67 + * If Disallow
68 + */
69 + if ( 'disallow' == $dci_status_db ) {
70 + return;
71 + }
72 +
73 + /**
74 + * Skip & Date Not Expired
75 + * Show Notice Modal
76 + */
77 + if ( 'skip' == $dci_status_db && true == $this->check_date() ) {
78 + $this->notice_modal( $params );
79 + return;
80 + }
81 +
82 + /**
83 + * Allowed & Date not Expired
84 + * No need send data to server
85 + * Else Send Data to Server
86 + */
87 + if ( ! $this->check_date() ) {
88 + return;
89 + }
90 +
91 + /**
92 + * Count attempt every time
93 + */
94 + $dci_attempt = get_option( $dci_count_name, 0 );
95 +
96 + if ( ! $dci_attempt ) {
97 + update_option( $dci_count_name, 1 );
98 + }
99 + update_option( $dci_count_name, $dci_attempt + 1 );
100 +
101 + /**
102 + * Next schedule date for attempt
103 + */
104 + update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
105 +
106 + /**
107 + * Prepare data
108 + */
109 + $this->data_prepare( $params );
110 + }
111 +
112 + /**
113 + * Insights Deactivate Feedback
114 + */
115 + public function insights_deactivate_feedback() {
116 + $api_endpoint = isset( $_POST['api_endpoint'] ) ? sanitize_text_field( $_POST['api_endpoint'] ) : '';
117 + $public_key = isset( $_POST['public_key'] ) ? sanitize_text_field( $_POST['public_key'] ) : '';
118 + $product_id = isset( $_POST['product_id'] ) ? sanitize_text_field( $_POST['product_id'] ) : '';
119 + $feedback = isset( $_POST['feedback'] ) ? sanitize_text_field( $_POST['feedback'] ) : '';
120 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
121 +
122 + if ( ! wp_verify_nonce( $nonce, 'dci_sdk' ) ) {
123 + wp_send_json( array(
124 + 'status' => 'error',
125 + 'title' => 'Error',
126 + 'message' => 'Nonce verification failed',
127 + ) );
128 + wp_die();
129 + }
130 +
131 + if ( ! current_user_can( 'manage_options' ) ) {
132 + wp_send_json( array(
133 + 'status' => 'error',
134 + 'title' => 'Error',
135 + 'message' => 'Denied, you don\'t have right permission',
136 + ) );
137 + wp_die();
138 + }
139 +
140 + $feedback = json_decode( stripslashes( $feedback ), true );
141 +
142 + $data = array(
143 + 'api_endpoint' => $api_endpoint,
144 + 'public_key' => $public_key,
145 + 'product_id' => $product_id,
146 + 'feedback' => $feedback,
147 + );
148 +
149 + $this->data_prepare( $data );
150 +
151 + wp_send_json( array(
152 + 'status' => 'success',
153 + 'title' => 'Success',
154 + 'message' => 'Success.',
155 + ) );
156 + wp_die();
157 + }
158 +
159 + /**
160 + * Notice Modal
161 + *
162 + * @return void
163 + */
164 + public function notice_modal( $params ) {
165 +
166 + $dci_data = array();
167 + $dci_data['name'] = $this->dci_name;
168 + $dci_data['date_name'] = $this->dci_date_name;
169 + $dci_data['allow_name'] = $this->dci_allow_name;
170 + $dci_data['nonce'] = wp_create_nonce( 'dci_sdk' );
171 + $dci_data['slug'] = $params['slug'];
172 + $dci_data['text_domain'] = $this->text_domain;
173 +
174 + add_action( 'admin_enqueue_scripts', array( $this, 'dci_enqueue_scripts' ) );
175 +
176 + /**
177 + * If not current page
178 + * Show Notice Modal & Return
179 + */
180 + if ( $params['current_page'] !== $params['menu_slug'] ) {
181 + if ( ! get_transient( 'dismissed_notice_' . $this->dci_name ) ) {
182 + add_action( 'admin_notices', array( $this, 'display_global_notice' ) );
183 + }
184 + return;
185 + }
186 +
187 + add_action( 'admin_notices', array( $this, 'display_global_notice' ) );
188 +
189 +
190 + if ( isset( $params['popup_notice'] ) && true === $params['popup_notice'] ) {
191 +
192 + include_once dirname( __FILE__ ) . '/notice.php';
193 +
194 + add_action(
195 + 'in_admin_header',
196 + function () use ($dci_data) {
197 + if ( function_exists( 'dci_popup_notice' ) ) {
198 + dci_popup_notice( $dci_data );
199 + }
200 + },
201 + 99999
202 + );
203 + }
204 + }
205 +
206 + /**
207 + * Deactivate Feedback
208 + *
209 + * @return void
210 + */
211 + public function deactivation_feedback( $params ) {
212 + $dci_data = array();
213 + $dci_data['nonce'] = wp_create_nonce( 'dci_sdk' );
214 + $dci_data['slug'] = $params['slug'];
215 + $dci_data['text_domain'] = $this->text_domain;
216 + $dci_data['api_endpoint'] = $params['api_endpoint'];
217 + $dci_data['public_key'] = $params['public_key'];
218 + $dci_data['product_id'] = $params['product_id'];
219 +
220 + add_action( 'admin_enqueue_scripts', array( $this, 'dci_enqueue_scripts' ) );
221 +
222 + if ( isset( $params['deactivate_feedback'] ) && true === $params['deactivate_feedback'] ) {
223 +
224 + include_once dirname( __FILE__ ) . '/deactivate-feedback.php';
225 +
226 + $current_screen = get_admin_page_parent();
227 +
228 + if ( isset( $current_screen ) && ! empty( $current_screen ) && 'plugins.php' === $current_screen ) {
229 + add_action(
230 + 'in_admin_header',
231 + function () use ($dci_data) {
232 + if ( function_exists( 'dci_deactivate_feedback' ) ) {
233 + dci_deactivate_feedback( $dci_data );
234 + }
235 + },
236 + 99999
237 + );
238 + }
239 + }
240 + }
241 +
242 + /**
243 + * If date is expired immidiate action
244 + *
245 + * @return boolean
246 + */
247 + public function check_date() {
248 + $current_date = strtotime( gmdate( 'Y-m-d' ) );
249 + $dci_status_date = strtotime( get_option( $this->dci_date_name, false ) );
250 +
251 + if ( ! $dci_status_date ) {
252 + return true;
253 + }
254 +
255 + if ( $dci_status_date && $current_date >= $dci_status_date ) {
256 + return true;
257 + }
258 + return false;
259 + }
260 +
261 + /**
262 + * Modal Trigger
263 + *
264 + * Not used
265 + * @return boolean
266 + */
267 + public function modal_trigger() {
268 +
269 + if ( ! wp_verify_nonce( $this->dci_allow_name, $this->nonce ) ) {
270 + echo 'Nonce Verification Failed';
271 + return false;
272 + }
273 +
274 + $sanitized_status = sanitize_text_field( $_GET['dci_allow_status'] );
275 +
276 + if ( $sanitized_status == 'skip' ) {
277 + update_option( $this->dci_allow_name, 'skip' );
278 + /**
279 + * Next schedule date for attempt
280 + */
281 + update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
282 + return false;
283 + } elseif ( $sanitized_status == 'yes' ) {
284 + update_option( $this->dci_allow_name, 'yes' );
285 + return true;
286 + }
287 +
288 + return false;
289 + }
290 +
291 + /**
292 + * Reset Options Settings
293 + * @return void
294 + */
295 + public function reset_settings() {
296 + delete_option( $this->dci_allow_name );
297 + delete_option( $this->dci_date_name );
298 + }
299 +
300 + /**
301 + * Data prepare for send server
302 + *
303 + * @param array $server_url
304 + * @return void
305 + */
306 + public function data_prepare( $params ) {
307 + $server_url = isset( $params['api_endpoint'] ) ? $params['api_endpoint'] : false;
308 + $public_key = isset( $params['public_key'] ) ? $params['public_key'] : false;
309 + $custom_data = isset( $params['custom_data'] ) ? $params['custom_data'] : false;
310 + $product_id = isset( $params['product_id'] ) ? $params['product_id'] : false;
311 +
312 + if ( ! $server_url || ! $public_key ) {
313 + return;
314 + }
315 +
316 + /**
317 + * ==================================
318 + *
319 + * Start Own Custom Important Data
320 + *
321 + * ==================================
322 + */
323 +
324 + // $custom_data = array(
325 + // 'active_modules' => array(),
326 + // 'third_party' => array(),
327 + // 'extend' => array(),
328 + // 'other_settings' => array(),
329 + // );
330 +
331 + // $custom_data = wp_json_encode($custom_data, true);
332 +
333 + /**
334 + * ==================================
335 + *
336 + * End Own Custom Important Data
337 + *
338 + * ==================================
339 + */
340 +
341 + $data = array();
342 + $data['public_key'] = $public_key;
343 + $data['product_id'] = $product_id;
344 + $data['custom_data'] = $custom_data;
345 +
346 + if ( isset( $params['feedback'] ) && ! empty( $params['feedback'] ) ) {
347 + $data['feedback'] = $params['feedback'];
348 + }
349 +
350 + $non_sensitive_data = $this->dci_non_sensitve_data();
351 + $data = array_merge( $data, $non_sensitive_data );
352 +
353 + $this->dci_send_data_to_server( $server_url, $data );
354 + }
355 +
356 + /**
357 + * Get the list of active and inactive plugins
358 + *
359 + * @return array
360 + */
361 + private function get_all_plugins() {
362 + // Ensure get_plugins function is loaded
363 + if ( ! function_exists( 'get_plugins' ) ) {
364 + include ABSPATH . '/wp-admin/includes/plugin.php';
365 + }
366 +
367 + $plugins = get_plugins();
368 + $active_plugins_keys = get_option( 'active_plugins', [] );
369 + $active_plugins = [];
370 +
371 + foreach ( $plugins as $k => $v ) {
372 + // Take care of formatting the data how we want it.
373 + $formatted = [];
374 + $formatted['name'] = wp_strip_all_tags( $v['Name'] );
375 +
376 + if ( isset( $v['Version'] ) ) {
377 + $formatted['version'] = wp_strip_all_tags( $v['Version'] );
378 + }
379 +
380 + if ( isset( $v['Author'] ) ) {
381 + $formatted['author'] = wp_strip_all_tags( $v['Author'] );
382 + }
383 +
384 + if ( isset( $v['Network'] ) ) {
385 + $formatted['network'] = wp_strip_all_tags( $v['Network'] );
386 + }
387 +
388 + if ( isset( $v['PluginURI'] ) ) {
389 + $formatted['plugin_uri'] = wp_strip_all_tags( $v['PluginURI'] );
390 + }
391 +
392 + if ( in_array( $k, $active_plugins_keys, true ) ) {
393 + // Remove active plugins from list so we can show active and inactive separately
394 + unset( $plugins[ $k ] );
395 + $active_plugins[ $k ] = $formatted;
396 + } else {
397 + $plugins[ $k ] = $formatted;
398 + }
399 + }
400 +
401 + $plugins_data = [];
402 + foreach ( $active_plugins as $slug => $_plugin ) {
403 + $slug = strstr( $slug, '/', true );
404 +
405 + if ( ! $slug ) {
406 + continue;
407 + }
408 +
409 + $plugins_data[ $slug ] = [
410 + 'name' => isset( $_plugin['name'] ) ? $_plugin['name'] : '',
411 + 'version' => isset( $_plugin['version'] ) ? $_plugin['version'] : '',
412 + ];
413 + }
414 +
415 + return [
416 + 'active_plugins' => $active_plugins,
417 + 'inactive_plugins' => $plugins,
418 + 'plugins_data' => $plugins_data,
419 + ];
420 + }
421 +
422 + /**
423 + * Non sensitive data
424 + *
425 + * @return array
426 + */
427 + public function dci_non_sensitve_data() {
428 + $current_user = wp_get_current_user();
429 + $all_plugins = $this->get_all_plugins();
430 +
431 + $users = get_users(
432 + [
433 + 'role' => 'administrator',
434 + 'orderby' => 'ID',
435 + 'order' => 'ASC',
436 + 'number' => 1,
437 + 'paged' => 1,
438 + ]
439 + );
440 +
441 + $admin_user = ( is_array( $users ) && ! empty( $users ) ) ? $users[0] : false;
442 + $first_name = $current_user->first_name;
443 + $last_name = $current_user->last_name;
444 +
445 + if ( empty( $first_name ) && empty( $last_name ) ) {
446 + $first_name = $current_user->display_name;
447 + $last_name = null;
448 + }
449 +
450 + if ( $admin_user ) {
451 + $first_name = $admin_user->first_name ? $admin_user->first_name : $admin_user->display_name;
452 + $last_name = $admin_user->last_name;
453 + }
454 +
455 + $theme = wp_get_theme( get_stylesheet() );
456 +
457 + $data = array(
458 + 'first_name' => $first_name,
459 + 'last_name' => $last_name,
460 + 'email' => get_option( 'admin_email' ),
461 + 'user_role' => $current_user->roles[0],
462 + 'website_url' => $current_user->user_url,
463 + 'website_data' => array(
464 + 'sdk_version' => $this->version,
465 + 'website_name' => get_bloginfo( 'name' ),
466 + 'wp_version' => get_bloginfo( 'version' ),
467 + 'php_version' => phpversion(),
468 + 'locale' => get_locale(),
469 + 'wp_multisite' => is_multisite() ? 'Yes' : 'No',
470 + 'wp_memory_limit' => defined( WP_MEMORY_LIMIT ) ? WP_MEMORY_LIMIT : false,
471 + 'memory_limit' => ini_get( 'memory_limit' ),
472 +
473 + 'inactive_plugins' => $all_plugins['inactive_plugins'],
474 + 'active_plugins' => $all_plugins['active_plugins'],
475 + 'active_plugins_count' => count( $all_plugins['active_plugins'] ),
476 + 'inactive_plugins_count' => count( $all_plugins['inactive_plugins'] ),
477 +
478 + 'theme_name' => $theme->get( 'Name' ),
479 + 'theme_version' => $theme->get( 'Version' ),
480 + 'theme_uri' => $theme->get( 'ThemeURI' ),
481 + 'theme_author' => $theme->get( 'Author' ),
482 + ),
483 + );
484 +
485 + return $data;
486 + }
487 +
488 + /**
489 + * Send data to server
490 + *
491 + * @param [string] $server_url
492 + * @param [array] $data
493 + * @return void
494 + */
495 + public function dci_send_data_to_server( $server_url, $data = null ) {
496 +
497 + $args = array(
498 + 'method' => 'POST',
499 + 'timeout' => 60,
500 + 'headers' => array(
501 + 'Content-Type' => 'application/json',
502 + 'X-API-KEY' => $data['public_key'],
503 + ),
504 + 'body' => wp_json_encode( $data ),
505 + );
506 +
507 + // error_log( print_r( $args, true ) );
508 +
509 + $response = wp_remote_request( $server_url, $args );
510 +
511 + if ( is_wp_error( $response ) ) {
512 + // echo 'Error: ' . $response->get_error_message();
513 + $this->reset_settings();
514 + } else {
515 + $response_data = wp_remote_retrieve_body( $response );
516 + $response_data = json_decode( $response_data, true );
517 + // print_r( $response_data );
518 + if ( isset( $response_data['data']['status'] ) && 401 == $response_data['data']['status'] ) {
519 + update_option( $this->dci_date_name, gmdate( 'Y-m-d', strtotime( "+3 days" ) ) );
520 + }
521 + }
522 + }
523 +
524 + /**
525 + * Ajax callback
526 + */
527 + public function dci_sdk_insights() {
528 + $sanitized_status = isset( $_POST['button_val'] ) ? sanitize_text_field( $_POST['button_val'] ) : '';
529 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
530 + $allow_name = isset( $_POST['allow_name'] ) ? sanitize_text_field( $_POST['allow_name'] ) : '';
531 + $date_name = isset( $_POST['date_name'] ) ? sanitize_text_field( $_POST['date_name'] ) : '';
532 +
533 + if ( ! wp_verify_nonce( $nonce, 'dci_sdk' ) ) {
534 + wp_send_json( array(
535 + 'status' => 'error',
536 + 'title' => 'Error',
537 + 'message' => 'Nonce verification failed',
538 + ) );
539 + wp_die();
540 + }
541 +
542 + if ( ! current_user_can( 'manage_options' ) ) {
543 + wp_send_json( array(
544 + 'status' => 'error',
545 + 'title' => 'Error',
546 + 'message' => 'Denied, you don\'t have right permission',
547 + ) );
548 + wp_die();
549 + }
550 +
551 + if ( 'disallow' == $sanitized_status ) {
552 + update_option( $allow_name, 'disallow' );
553 + }
554 +
555 + if ( $sanitized_status == 'skip' ) {
556 + update_option( $allow_name, 'skip' );
557 + /**
558 + * Next schedule date for attempt
559 + */
560 + update_option( $date_name, gmdate( 'Y-m-d', strtotime( "+1 month" ) ) );
561 + } elseif ( $sanitized_status == 'yes' ) {
562 + update_option( $allow_name, 'yes' );
563 + }
564 +
565 + wp_send_json( array(
566 + 'status' => 'success',
567 + 'title' => 'Success',
568 + 'message' => 'Success.',
569 + ) );
570 + wp_die();
571 + }
572 +
573 + /**
574 + * Enqueue scripts and styles.
575 + *
576 + * @since 1.0.0
577 + */
578 + public function dci_enqueue_scripts() {
579 + wp_enqueue_style( 'dci-sdk', plugins_url( 'assets/css/dci.css', __FILE__ ), array(), $this->version, 'all' );
580 + wp_enqueue_script( 'dci-sdk', plugins_url( 'assets/js/dci.js', __FILE__ ), array( 'jquery' ), $this->version, true );
581 + }
582 +
583 + /**
584 + * Display Global Notice
585 + *
586 + * @return void
587 + */
588 + public function display_global_notice() {
589 + $menu_slug = isset( $this->params['menu_slug'] ) ? $this->params['menu_slug'] : 'javascript:void(0);';
590 +
591 + $admin_url = add_query_arg( array(
592 + 'page' => $menu_slug,
593 + ), admin_url( 'admin.php' ) );
594 +
595 + $plugin_title = isset( $this->params['plugin_title'] ) ? $this->params['plugin_title'] : '';
596 + $plugin_msg = isset( $this->params['plugin_msg'] ) ? $this->params['plugin_msg'] : '';
597 + $plugin_icon = isset( $this->params['plugin_icon'] ) ? $this->params['plugin_icon'] : '';
598 +
599 + ?>
600 + <div
601 + class="dci-global-notice dci-notice-data notice notice-success is-dismissible <?php echo esc_attr( substr( $this->dci_name, 0, -33 ) ); ?>">
602 + <div class="dci-global-header bdt-dci-notice-global-header">
603 + <?php if ( ! empty( $plugin_icon ) ) : ?>
604 + <div class="bdt-dci-notice-logo">
605 + <img src="<?php echo esc_url( $plugin_icon ); ?>" alt="icon">
606 + </div>
607 + <?php endif; ?>
608 + <div class="bdt-dci-notice-content">
609 + <h3>
610 + <?php printf( wp_kses_post( $plugin_title ) ); ?>
611 + </h3>
612 + <?php printf( wp_kses_post( $plugin_msg ) ); ?>
613 + <p>
614 + <a href="<?php echo esc_url( $admin_url ); ?>">Learn More</a>?
615 + </p>
616 + <input type="hidden" name="dci_name" value="<?php echo esc_html( $this->dci_name ); ?>">
617 + <input type="hidden" name="dci_date_name" value="<?php echo esc_html( $this->dci_date_name ); ?>">
618 + <input type="hidden" name="dci_allow_name" value="<?php echo esc_html( $this->dci_allow_name ); ?>">
619 + <input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'dci_sdk' ) ); ?>">
620 +
621 + <div class="bdt-dci-notice-button-wrap">
622 + <button name="dci_allow_status" value="yes" class="dci-button-allow">
623 + Yes, I'd Love To Contribute
624 + </button>
625 + <button name="dci_allow_status" value="skip" class="dci-button-skip">
626 + Skip For Now
627 + </button>
628 + <button name="dci_allow_status" value="disallow" class="dci-button-disallow dci-button-danger">
629 + No Thanks
630 + </button>
631 + </div>
632 + </div>
633 + </div>
634 +
635 + </div>
636 +
637 + <?php
638 + }
639 + public function __display_global_notice() {
640 + $menu_slug = isset( $this->params['menu_slug'] ) ? $this->params['menu_slug'] : 'javascript:void(0);';
641 +
642 + $admin_url = add_query_arg( array(
643 + 'page' => $menu_slug,
644 + ), admin_url( 'admin.php' ) );
645 +
646 + $plugin_title = isset( $this->params['plugin_title'] ) ? $this->params['plugin_title'] : '';
647 + $plugin_msg = isset( $this->params['plugin_msg'] ) ? $this->params['plugin_msg'] : '';
648 + $plugin_icon = isset( $this->params['plugin_icon'] ) ? $this->params['plugin_icon'] : '';
649 +
650 + ?>
651 + <div class="dci-global-notice dci-notice-data notice notice-success is-dismissible">
652 + <div class="dci-global-header">
653 + <?php if ( ! empty( $plugin_icon ) ) : ?>
654 + <div>
655 + <img src="<?php echo esc_url( $plugin_icon ); ?>" alt="icon">
656 + </div>
657 + <?php endif; ?>
658 + <h3>
659 + <?php printf( $plugin_title ); ?>
660 + </h3>
661 + </div>
662 + <?php printf( $plugin_msg ); ?>
663 + <p>
664 + What we <a href="<?php echo esc_url( $admin_url ); ?>">collect</a>?
665 + </p>
666 + <input type="hidden" name="dci_name" value="<?php echo esc_html( $this->dci_name ); ?>">
667 + <input type="hidden" name="dci_date_name" value="<?php echo esc_html( $this->dci_date_name ); ?>">
668 + <input type="hidden" name="dci_allow_name" value="<?php echo esc_html( $this->dci_allow_name ); ?>">
669 + <input type="hidden" name="nonce" value="<?php echo esc_html( wp_create_nonce( 'dci_sdk' ) ); ?>">
670 + <p>
671 + <button name="dci_allow_status" value="yes" class="button button-primary dci-button-allow">
672 + Allow
673 + </button>
674 + <button name="dci_allow_status" value="skip" class="button dci-button-skip button-secondary">
675 + I'll Skip For Now
676 + </button>
677 + <button name="dci_allow_status" value="disallow" class="button dci-button-disallow dci-button-danger">
678 + Don't Allow
679 + </button>
680 + </p>
681 + </div>
682 + <?php
683 + }
684 +
685 + /**
686 + * Dismiss Notice
687 + *
688 + * @return void
689 + */
690 + public function dci_sdk_dismiss_notice() {
691 + $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
692 + $dci_name = isset( $_POST['dci_name'] ) ? sanitize_text_field( $_POST['dci_name'] ) : '';
693 +
694 + if ( ! wp_verify_nonce( $nonce, 'dci_sdk' ) ) {
695 + wp_send_json( array(
696 + 'status' => 'error',
697 + 'title' => 'Error',
698 + 'message' => 'Nonce verification failed',
699 + ) );
700 + wp_die();
701 + }
702 +
703 + if ( ! current_user_can( 'manage_options' ) ) {
704 + wp_send_json( array(
705 + 'status' => 'error',
706 + 'title' => 'Error',
707 + 'message' => 'Denied, you don\'t have right permission',
708 + ) );
709 + wp_die();
710 + }
711 +
712 + set_transient( 'dismissed_notice_' . $dci_name, true, 30 * DAY_IN_SECONDS );
713 +
714 + wp_send_json( array(
715 + 'status' => 'success',
716 + 'title' => 'Success',
717 + 'message' => 'Success.',
718 + ) );
719 + wp_die();
720 + }
721 + }
722 +}
723 +
724 +/**
725 + * Main Insights Function
726 + */
727 +if ( ! function_exists( 'dci_sdk_insights' ) ) {
728 + function dci_sdk_insights( $params ) {
729 + if ( class_exists( 'Insights_SDK' ) ) {
730 + new Insights_SDK( $params );
731 + }
732 + }
733 +}