| 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 |
} |
| 394 |
|