| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if (!class_exists('Ultimate_Post_Kit_Reviews_Collector')) { |
| 8 |
class Ultimate_Post_Kit_Reviews_Collector { |
| 9 |
|
| 10 |
public $version = '1.0.0'; |
| 11 |
|
| 12 |
public $rc_name; |
| 13 |
public $rc_allow_name; |
| 14 |
public $rc_date_name; |
| 15 |
public $rc_count_name; |
| 16 |
public $nonce; |
| 17 |
public $params; |
| 18 |
public $review_url; |
| 19 |
|
| 20 |
private static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get Instance |
| 24 |
* |
| 25 |
* @since 0.0.0 |
| 26 |
*/ |
| 27 |
public static function get_instance($params) { |
| 28 |
if (!isset(self::$instance)) { |
| 29 |
self::$instance = new self($params); |
| 30 |
} |
| 31 |
|
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Insights SDK Version |
| 37 |
* param array $params |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function __construct($params) { |
| 41 |
$this->params = $params; |
| 42 |
$this->review_url = isset($params['review_url']) ? $params['review_url'] : false; |
| 43 |
|
| 44 |
// add_action( 'admin_enqueue_scripts', array( $this, 'rc_enqueue_scripts' ) ); |
| 45 |
add_action('wp_ajax_ultimate_post_kit_reviews_insights', array($this, 'rc_sdk_insights')); |
| 46 |
add_action('wp_ajax_ultimate_post_kit_reviews_dismiss_notice', array($this, 'rc_sdk_dismiss_notice')); |
| 47 |
|
| 48 |
$security_key = md5($params['plugin_name']); |
| 49 |
$this->rc_name = 'rc_' . str_replace('-', '_', sanitize_title($params['plugin_name']) . '_' . $security_key); |
| 50 |
$this->rc_allow_name = 'rc_allow_' . $this->rc_name; |
| 51 |
$this->rc_date_name = 'rc_date_' . $this->rc_name; |
| 52 |
$rc_count_name = 'rc_attempt_count_' . $this->rc_name; |
| 53 |
$rc_status_db = get_option($this->rc_allow_name, false); |
| 54 |
|
| 55 |
$this->nonce = wp_create_nonce($this->rc_allow_name); |
| 56 |
|
| 57 |
/** |
| 58 |
* Show Notice after 3 days |
| 59 |
* Now 5 minutes |
| 60 |
*/ |
| 61 |
$installed = get_option($this->rc_date_name . '_installed', false); |
| 62 |
|
| 63 |
if (!$installed) { |
| 64 |
update_option($this->rc_date_name . '_installed', time()); |
| 65 |
} |
| 66 |
|
| 67 |
$installed = get_option($this->rc_date_name . '_installed', false); |
| 68 |
|
| 69 |
// if ( $installed && ( time() - $installed ) < 1 * MINUTE_IN_SECONDS ) { |
| 70 |
if ($installed && (time() - $installed) < 3 * DAY_IN_SECONDS) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Show Notice |
| 76 |
*/ |
| 77 |
if (!$rc_status_db) { |
| 78 |
$this->display_notice(); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* If Disallow |
| 84 |
*/ |
| 85 |
if ('disallow' == $rc_status_db) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Skip & Date Not Expired |
| 91 |
* Show Notice |
| 92 |
*/ |
| 93 |
if ('skip' == $rc_status_db && true == $this->check_date()) { |
| 94 |
$this->display_notice(); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Allowed & Date not Expired |
| 100 |
* No need send data to server |
| 101 |
* Else Send Data to Server |
| 102 |
*/ |
| 103 |
if (!$this->check_date()) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Count attempt every time |
| 109 |
*/ |
| 110 |
$rc_attempt = get_option($rc_count_name, 0); |
| 111 |
|
| 112 |
if (!$rc_attempt) { |
| 113 |
update_option($rc_count_name, 1); |
| 114 |
} |
| 115 |
update_option($rc_count_name, $rc_attempt + 1); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Notice Modal |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function display_notice() { |
| 124 |
add_action('admin_enqueue_scripts', array($this, 'rc_enqueue_scripts')); |
| 125 |
|
| 126 |
if (!get_transient('dismissed_notice_' . $this->rc_name)) { |
| 127 |
add_action('admin_notices', array($this, 'display_global_notice')); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* If date is expired immidiate action |
| 133 |
* |
| 134 |
* @return boolean |
| 135 |
*/ |
| 136 |
public function check_date() { |
| 137 |
$current_date = strtotime(gmdate('Y-m-d')); |
| 138 |
$rc_status_date = strtotime(get_option($this->rc_date_name, false)); |
| 139 |
|
| 140 |
if (!$rc_status_date) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
if ($rc_status_date && $current_date >= $rc_status_date) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Reset Options Settings |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
public function reset_settings() { |
| 155 |
delete_option($this->rc_allow_name); |
| 156 |
delete_option($this->rc_date_name); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Ajax callback |
| 161 |
*/ |
| 162 |
public function rc_sdk_insights() { |
| 163 |
$sanitized_status = isset($_POST['button_val']) ? sanitize_text_field(wp_unslash($_POST['button_val'])) : ''; |
| 164 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 165 |
$allow_name = isset($_POST['allow_name']) ? sanitize_text_field(wp_unslash($_POST['allow_name'])) : ''; |
| 166 |
$date_name = isset($_POST['date_name']) ? sanitize_text_field(wp_unslash($_POST['date_name'])) : ''; |
| 167 |
|
| 168 |
// Confine the writes to this SDK's own option namespace so a request |
| 169 |
// cannot use these to overwrite an arbitrary WordPress option. |
| 170 |
if (0 !== strpos($allow_name, 'rc_allow_')) { |
| 171 |
$allow_name = ''; |
| 172 |
} |
| 173 |
if (0 !== strpos($date_name, 'rc_date_')) { |
| 174 |
$date_name = ''; |
| 175 |
} |
| 176 |
|
| 177 |
if (!wp_verify_nonce($nonce, 'rc_sdk')) { |
| 178 |
wp_send_json(array( |
| 179 |
'status' => 'error', |
| 180 |
'title' => 'Error', |
| 181 |
'message' => 'Nonce verification failed', |
| 182 |
)); |
| 183 |
wp_die(); |
| 184 |
} |
| 185 |
|
| 186 |
if (!current_user_can('manage_options')) { |
| 187 |
wp_send_json(array( |
| 188 |
'status' => 'error', |
| 189 |
'title' => 'Error', |
| 190 |
'message' => 'Denied, you don\'t have right permission', |
| 191 |
)); |
| 192 |
wp_die(); |
| 193 |
} |
| 194 |
|
| 195 |
if ('disallow' == $sanitized_status && $allow_name) { |
| 196 |
update_option($allow_name, 'disallow'); |
| 197 |
} |
| 198 |
|
| 199 |
if ($sanitized_status == 'skip' && $allow_name) { |
| 200 |
update_option($allow_name, 'skip'); |
| 201 |
/** |
| 202 |
* Next schedule date for attempt |
| 203 |
*/ |
| 204 |
if ($date_name) { |
| 205 |
update_option($date_name, gmdate('Y-m-d', strtotime("+1 month"))); |
| 206 |
} |
| 207 |
} elseif ($sanitized_status == 'yes' && $allow_name) { |
| 208 |
update_option($allow_name, 'yes'); |
| 209 |
} |
| 210 |
|
| 211 |
wp_send_json(array( |
| 212 |
'status' => 'success', |
| 213 |
'title' => 'Success', |
| 214 |
'message' => 'Success.', |
| 215 |
'action' => $sanitized_status, |
| 216 |
)); |
| 217 |
wp_die(); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Enqueue scripts and styles. |
| 222 |
* |
| 223 |
* @since 1.0.0 |
| 224 |
*/ |
| 225 |
public function rc_enqueue_scripts() { |
| 226 |
wp_enqueue_style('rc-sdk', plugins_url('assets/rc.css', __FILE__), array(), '1.0.0'); |
| 227 |
wp_enqueue_script('rc-sdk', plugins_url('assets/rc.js', __FILE__), array('jquery'), '1.0.0', true); |
| 228 |
|
| 229 |
// Add inline style to hide all but the first notice on page load |
| 230 |
$inline_css = '.rc-global-notice { display: none; }'; |
| 231 |
wp_add_inline_style( 'rc-sdk', $inline_css ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Display Global Notice |
| 236 |
* |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function display_global_notice() { |
| 240 |
$plugin_title = isset($this->params['plugin_title']) ? $this->params['plugin_title'] : ''; |
| 241 |
$plugin_msg = isset($this->params['plugin_msg']) ? $this->params['plugin_msg'] : ''; |
| 242 |
$plugin_icon = isset($this->params['plugin_icon']) ? $this->params['plugin_icon'] : ''; |
| 243 |
|
| 244 |
?> |
| 245 |
<div class="rc-global-notice notice notice-success is-dismissible <?php echo esc_attr(substr($this->rc_name, 0, -33)); ?>"> |
| 246 |
<div class="rc-global-header"> |
| 247 |
<?php if (!empty($plugin_icon)) : ?> |
| 248 |
<div class="bdt-notice-rc-logo"> |
| 249 |
<img src="<?php echo esc_url($plugin_icon); ?>" alt="icon"> |
| 250 |
</div> |
| 251 |
<?php endif; ?> |
| 252 |
|
| 253 |
<div class="bdt-notice-rc-content"> |
| 254 |
<h3> |
| 255 |
<?php printf(wp_kses_post($plugin_title)); ?> |
| 256 |
</h3> |
| 257 |
<?php printf(wp_kses_post($plugin_msg)); ?> |
| 258 |
<input type="hidden" name="rc_name" value="<?php echo esc_html($this->rc_name); ?>"> |
| 259 |
<input type="hidden" name="nonce" value="<?php echo esc_html(wp_create_nonce('rc_sdk')); ?>"> |
| 260 |
<div class="bdt-notice-rc-buttons"> |
| 261 |
<button data-rc_name="<?php echo esc_html($this->rc_name); ?>" data-date_name="<?php echo esc_html($this->rc_date_name); ?>" data-allow_name="<?php echo esc_html($this->rc_allow_name); ?>" data-nonce="<?php echo esc_html(wp_create_nonce('rc_sdk')); ?>" data-review_url="<?php echo esc_html($this->review_url); ?>" name="rc_allow_status" value="yes" class="rc-button-allow"> |
| 262 |
<span class="dashicons dashicons-star-filled" style="margin-top: 3px;"></span> Give us your Review |
| 263 |
</button> |
| 264 |
<button data-rc_name="<?php echo esc_html($this->rc_name); ?>" data-date_name="<?php echo esc_html($this->rc_date_name); ?>" data-allow_name="<?php echo esc_html($this->rc_allow_name); ?>" data-nonce="<?php echo esc_html(wp_create_nonce('rc_sdk')); ?>" data-review_url="<?php echo esc_html($this->review_url); ?>" name="rc_allow_status" value="skip" class="rc-button-skip"> |
| 265 |
I'll skip for now |
| 266 |
</button> |
| 267 |
<button data-rc_name="<?php echo esc_html($this->rc_name); ?>" data-date_name="<?php echo esc_html($this->rc_date_name); ?>" data-allow_name="<?php echo esc_html($this->rc_allow_name); ?>" data-nonce="<?php echo esc_html(wp_create_nonce('rc_sdk')); ?>" data-review_url="<?php echo esc_html($this->review_url); ?>" name="rc_allow_status" value="disallow" class="rc-button-disallow rc-button-danger"> |
| 268 |
Hide and Don't show again |
| 269 |
</button> |
| 270 |
</div> |
| 271 |
</div> |
| 272 |
</div> |
| 273 |
</div> |
| 274 |
<?php |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Dismiss Notice |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function rc_sdk_dismiss_notice() { |
| 283 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : ''; |
| 284 |
$rc_name = isset($_POST['rc_name']) ? sanitize_text_field(wp_unslash($_POST['rc_name'])) : ''; |
| 285 |
|
| 286 |
if (!wp_verify_nonce($nonce, 'rc_sdk')) { |
| 287 |
wp_send_json(array( |
| 288 |
'status' => 'error', |
| 289 |
'title' => 'Error', |
| 290 |
'message' => 'Nonce verification failed', |
| 291 |
)); |
| 292 |
wp_die(); |
| 293 |
} |
| 294 |
|
| 295 |
if (!current_user_can('manage_options')) { |
| 296 |
wp_send_json(array( |
| 297 |
'status' => 'error', |
| 298 |
'title' => 'Error', |
| 299 |
'message' => 'Denied, you don\'t have right permission', |
| 300 |
)); |
| 301 |
wp_die(); |
| 302 |
} |
| 303 |
|
| 304 |
set_transient('dismissed_notice_' . $rc_name, true, 30 * DAY_IN_SECONDS); |
| 305 |
|
| 306 |
wp_send_json(array( |
| 307 |
'status' => 'success', |
| 308 |
'title' => 'Success', |
| 309 |
'message' => 'Success.', |
| 310 |
)); |
| 311 |
wp_die(); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Main Insights Function |
| 318 |
*/ |
| 319 |
function ultimate_post_kit_reviews_automate($params) { |
| 320 |
new Ultimate_Post_Kit_Reviews_Collector($params); |
| 321 |
} |
| 322 |
|