| 1 |
<?php |
| 2 |
/** |
| 3 |
* The deactivate feedback model class for ThemeIsle SDK |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Feedback |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
if ( ! class_exists( 'ThemeIsle_SDK_Feedback_Deactivate' ) ) : |
| 16 |
/** |
| 17 |
* Deactivate feedback model for ThemeIsle SDK. |
| 18 |
*/ |
| 19 |
class ThemeIsle_SDK_Feedback_Deactivate extends ThemeIsle_SDK_Feedback { |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array $options_plugin The main options list for plugins. |
| 23 |
*/ |
| 24 |
private $options_plugin = array( |
| 25 |
'I only needed the plugin for a short period' => array( |
| 26 |
'id' => 1, |
| 27 |
), |
| 28 |
'The plugin broke my site' => array( |
| 29 |
'id' => 2, |
| 30 |
), |
| 31 |
'I found a better plugin' => array( |
| 32 |
'id' => 3, |
| 33 |
'type' => 'text', |
| 34 |
'placeholder' => 'What\'s the plugin\'s name?', |
| 35 |
), |
| 36 |
'The plugin suddenly stopped working' => array( |
| 37 |
'id' => 4, |
| 38 |
), |
| 39 |
'I no longer need the plugin' => array( |
| 40 |
'id' => 5, |
| 41 |
'type' => 'textarea', |
| 42 |
'placeholder' => 'If you could improve one thing about our product, what would it be?', |
| 43 |
), |
| 44 |
'It\'s a temporary deactivation. I\'m just debugging an issue.' => array( |
| 45 |
'id' => 6, |
| 46 |
), |
| 47 |
); |
| 48 |
|
| 49 |
/** |
| 50 |
* @var array $options_theme The main options list for themes. |
| 51 |
*/ |
| 52 |
private $options_theme = array( |
| 53 |
'I don\'t know how to make it look like demo' => array( |
| 54 |
'id' => 7, |
| 55 |
), |
| 56 |
'It lacks options' => array( |
| 57 |
'id' => 8, |
| 58 |
), |
| 59 |
'Is not working with a plugin that I need' => array( |
| 60 |
'id' => 9, |
| 61 |
'type' => 'text', |
| 62 |
'placeholder' => 'What is the name of the plugin', |
| 63 |
), |
| 64 |
'I want to try a new design, I don\'t like {theme} style' => array( |
| 65 |
'id' => 10, |
| 66 |
), |
| 67 |
); |
| 68 |
|
| 69 |
/** |
| 70 |
* @var array $other The other option |
| 71 |
*/ |
| 72 |
private $other = array( |
| 73 |
'Other' => array( |
| 74 |
'id' => 999, |
| 75 |
'type' => 'textarea', |
| 76 |
'placeholder' => 'cmon cmon tell us', |
| 77 |
), |
| 78 |
); |
| 79 |
|
| 80 |
/** |
| 81 |
* @var string $heading_plugin The heading of the modal |
| 82 |
*/ |
| 83 |
private $heading_plugin = 'If you have a moment, please let us know why you are deactivating:'; |
| 84 |
|
| 85 |
/** |
| 86 |
* @var string $heading_theme The heading of the modal |
| 87 |
*/ |
| 88 |
private $heading_theme = 'Looking to change {theme}, what doesn\'t work for you?'; |
| 89 |
|
| 90 |
/** |
| 91 |
* @var string $button_submit_before The text of the deactivate button before an option is chosen |
| 92 |
*/ |
| 93 |
private $button_submit_before = 'Skip & Deactivate'; |
| 94 |
|
| 95 |
/** |
| 96 |
* @var string $button_submit The text of the deactivate button |
| 97 |
*/ |
| 98 |
private $button_submit = 'Submit & Deactivate'; |
| 99 |
|
| 100 |
/** |
| 101 |
* @var string $button_cancel The text of the cancel button |
| 102 |
*/ |
| 103 |
private $button_cancel = 'Cancel'; |
| 104 |
|
| 105 |
/** |
| 106 |
* @var int how many seconds before the deactivation window is triggered for themes |
| 107 |
*/ |
| 108 |
const AUTO_TRIGGER_DEACTIVATE_WINDOW_SECONDS = 3; |
| 109 |
|
| 110 |
/** |
| 111 |
* @var int how many days before the deactivation window pops up again for the theme |
| 112 |
*/ |
| 113 |
const PAUSE_DEACTIVATE_WINDOW_DAYS = 100; |
| 114 |
|
| 115 |
/** |
| 116 |
* ThemeIsle_SDK_Feedback_Deactivate constructor. |
| 117 |
* |
| 118 |
* @param ThemeIsle_SDK_Product $product_object The product object. |
| 119 |
*/ |
| 120 |
public function __construct( $product_object ) { |
| 121 |
parent::__construct( $product_object ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Registers the hooks |
| 126 |
*/ |
| 127 |
public function setup_hooks_child() { |
| 128 |
global $pagenow; |
| 129 |
|
| 130 |
if ( ( $this->product->get_type() === 'plugin' && $pagenow === 'plugins.php' ) || ( $this->product->get_type() === 'theme' && $pagenow === 'theme-install.php' ) ) { |
| 131 |
add_action( 'admin_head', array( $this, 'load_resources' ) ); |
| 132 |
} |
| 133 |
add_action( 'wp_ajax_' . $this->product->get_key() . __CLASS__, array( $this, 'post_deactivate' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Loads the additional resources |
| 138 |
*/ |
| 139 |
function load_resources() { |
| 140 |
add_thickbox(); |
| 141 |
|
| 142 |
$id = $this->product->get_key() . '_deactivate'; |
| 143 |
|
| 144 |
$this->add_css( $this->product->get_type(), $this->product->get_key() ); |
| 145 |
$this->add_js( $this->product->get_type(), $this->product->get_key(), '#TB_inline?' . apply_filters( $this->product->get_key() . '_feedback_deactivate_attributes', 'width=600&height=550' ) . '&inlineId=' . $id ); |
| 146 |
|
| 147 |
echo '<div id="' . $id . '" style="display:none;" class="themeisle-deactivate-box">' . $this->get_html( $this->product->get_type(), $this->product->get_key() ) . '</div>'; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Loads the css |
| 152 |
* |
| 153 |
* @param string $type The type of product. |
| 154 |
* @param string $key The product key. |
| 155 |
*/ |
| 156 |
function add_css( $type, $key ) { |
| 157 |
$suffix = 'theme' === $type ? 'theme-install-php' : 'plugins-php'; |
| 158 |
?> |
| 159 |
<style type="text/css" id="<?php echo $key; ?>ti-deactivate-css"> |
| 160 |
input[name="ti-deactivate-option"] ~ div { |
| 161 |
display: none; |
| 162 |
} |
| 163 |
|
| 164 |
input[name="ti-deactivate-option"]:checked ~ div { |
| 165 |
display: block; |
| 166 |
} |
| 167 |
|
| 168 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container #TB_window.thickbox-loading:before { |
| 169 |
background: none !important; |
| 170 |
} |
| 171 |
|
| 172 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container #TB_title { |
| 173 |
font-size: 21px; |
| 174 |
padding: 20px 0; |
| 175 |
background-color: #f3f3f3; |
| 176 |
} |
| 177 |
|
| 178 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container div.actions { |
| 179 |
padding: 20px 0; |
| 180 |
background-color: #f3f3f3; |
| 181 |
border-top: 1px solid #dddddd; |
| 182 |
} |
| 183 |
|
| 184 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container input.button.button-primary { |
| 185 |
margin-right: 20px; |
| 186 |
} |
| 187 |
|
| 188 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container input.button { |
| 189 |
margin-right: 20px; |
| 190 |
} |
| 191 |
|
| 192 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container #TB_ajaxWindowTitle { |
| 193 |
text-align: left; |
| 194 |
margin-left: 15px; |
| 195 |
} |
| 196 |
|
| 197 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container div.revive_network-container { |
| 198 |
background-color: #ffffff; |
| 199 |
} |
| 200 |
|
| 201 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container ul.ti-list li { |
| 202 |
font-size: 14px; |
| 203 |
} |
| 204 |
|
| 205 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container ul.ti-list li label { |
| 206 |
margin-left: 10px; |
| 207 |
line-height: 32px; |
| 208 |
font-size: 16px; |
| 209 |
} |
| 210 |
|
| 211 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container #TB_ajaxContent { |
| 212 |
padding: 10px 20px; |
| 213 |
} |
| 214 |
|
| 215 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container li div textarea { |
| 216 |
padding: 10px 15px; |
| 217 |
width: 100%; |
| 218 |
} |
| 219 |
|
| 220 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container ul.ti-list li div { |
| 221 |
margin: 10px 30px; |
| 222 |
} |
| 223 |
|
| 224 |
.<?php echo $key; ?>-container #TB_title #TB_ajaxWindowTitle { |
| 225 |
display: block; |
| 226 |
} |
| 227 |
|
| 228 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container .actions { |
| 229 |
|
| 230 |
width: 100%; |
| 231 |
display: block; |
| 232 |
position: absolute; |
| 233 |
left: 0px; |
| 234 |
bottom: 0px; |
| 235 |
text-align: right; |
| 236 |
} |
| 237 |
|
| 238 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container #TB_title { |
| 239 |
|
| 240 |
height: 33px; |
| 241 |
width: 100%; |
| 242 |
text-align: center; |
| 243 |
} |
| 244 |
|
| 245 |
.theme-install-php .<?php echo $key; ?>-container #TB_closeWindowButton .tb-close-icon:before { |
| 246 |
font-size: 32px; |
| 247 |
} |
| 248 |
|
| 249 |
.<?php echo $key; ?>-container #TB_closeWindowButton .tb-close-icon { |
| 250 |
|
| 251 |
color: #eee; |
| 252 |
} |
| 253 |
|
| 254 |
.<?php echo $key; ?>-container #TB_closeWindowButton { |
| 255 |
left: auto; |
| 256 |
right: -30px; |
| 257 |
color: #eee; |
| 258 |
} |
| 259 |
|
| 260 |
body.<?php echo $suffix; ?> .<?php echo $key; ?>-container { |
| 261 |
|
| 262 |
margin: auto !important; |
| 263 |
height: 550px !important; |
| 264 |
top: 0 !important; |
| 265 |
left: 0 !important; |
| 266 |
bottom: 0 !important; |
| 267 |
right: 0 !important; |
| 268 |
} |
| 269 |
</style> |
| 270 |
<?php |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Loads the js |
| 275 |
* |
| 276 |
* @param string $type The type of product. |
| 277 |
* @param string $key The product key. |
| 278 |
* @param string $src The url that will hijack the deactivate button url. |
| 279 |
*/ |
| 280 |
function add_js( $type, $key, $src ) { |
| 281 |
$heading = 'plugin' === $type ? $this->heading_plugin : str_replace( '{theme}', $this->product->get_name(), $this->heading_theme ); |
| 282 |
$heading = apply_filters( $this->product->get_key() . '_feedback_deactivate_heading', $heading ); |
| 283 |
?> |
| 284 |
<script type="text/javascript" id="ti-deactivate-js"> |
| 285 |
(function ($) { |
| 286 |
$(document).ready(function () { |
| 287 |
var auto_trigger = false; |
| 288 |
var target_element = 'tr[data-plugin^="<?php echo $this->product->get_slug(); ?>/"] span.deactivate a'; |
| 289 |
<?php |
| 290 |
if ( 'theme' === $type ) { |
| 291 |
?> |
| 292 |
auto_trigger = true; |
| 293 |
if ($('a.ti-auto-anchor').length == 0) { |
| 294 |
$('body').append($('<a class="ti-auto-anchor" href=""></a>')); |
| 295 |
} |
| 296 |
target_element = 'a.ti-auto-anchor'; |
| 297 |
<?php |
| 298 |
} |
| 299 |
?> |
| 300 |
|
| 301 |
if (auto_trigger) { |
| 302 |
setTimeout(function () { |
| 303 |
$('a.ti-auto-anchor').trigger('click'); |
| 304 |
}, <?php echo self::AUTO_TRIGGER_DEACTIVATE_WINDOW_SECONDS * 1000; ?> ); |
| 305 |
} |
| 306 |
$( document ).on( 'thickbox:removed', function() { |
| 307 |
$.ajax({ |
| 308 |
url: ajaxurl, |
| 309 |
method: 'post', |
| 310 |
data: { |
| 311 |
'action' : '<?php echo $key . __CLASS__; ?>', |
| 312 |
'nonce' : '<?php echo wp_create_nonce( (string) __CLASS__ ); ?>', |
| 313 |
'type' : '<?php echo $type; ?>', |
| 314 |
'key' : '<?php echo $key; ?>' |
| 315 |
}, |
| 316 |
}); |
| 317 |
}); |
| 318 |
var href = $(target_element).attr('href'); |
| 319 |
$('#<?php echo $key; ?>ti-deactivate-no').on('click', function (e) { |
| 320 |
e.preventDefault(); |
| 321 |
e.stopPropagation(); |
| 322 |
|
| 323 |
$('body').unbind('thickbox:removed'); |
| 324 |
tb_remove(); |
| 325 |
}); |
| 326 |
|
| 327 |
$('#<?php echo $key; ?> ul.ti-list label, #<?php echo $key; ?> ul.ti-list input[name="ti-deactivate-option"]').on('click', function (e) { |
| 328 |
$('#<?php echo $key; ?>ti-deactivate-yes').val($('#<?php echo $key; ?>ti-deactivate-yes').attr('data-after-text')); |
| 329 |
|
| 330 |
var radio = $(this).prop('tagName') === 'LABEL' ? $(this).parent() : $(this); |
| 331 |
if (radio.parent().find('textarea').length > 0 && radio.parent().find('textarea').val().length === 0) { |
| 332 |
$('#<?php echo $key; ?>ti-deactivate-yes').attr('disabled', 'disabled'); |
| 333 |
radio.parent().find('textarea').on('keyup', function (ee) { |
| 334 |
if ($(this).val().length === 0) { |
| 335 |
$('#<?php echo $key; ?>ti-deactivate-yes').attr('disabled', 'disabled'); |
| 336 |
} else { |
| 337 |
$('#<?php echo $key; ?>ti-deactivate-yes').removeAttr('disabled'); |
| 338 |
} |
| 339 |
}); |
| 340 |
} else { |
| 341 |
$('#<?php echo $key; ?>ti-deactivate-yes').removeAttr('disabled'); |
| 342 |
} |
| 343 |
}); |
| 344 |
|
| 345 |
$('#<?php echo $key; ?>ti-deactivate-yes').attr('data-ti-action', href).on('click', function (e) { |
| 346 |
e.preventDefault(); |
| 347 |
e.stopPropagation(); |
| 348 |
$.ajax({ |
| 349 |
url: ajaxurl, |
| 350 |
method: 'post', |
| 351 |
data: { |
| 352 |
'action' : '<?php echo $key . __CLASS__; ?>', |
| 353 |
'nonce' : '<?php echo wp_create_nonce( (string) __CLASS__ ); ?>', |
| 354 |
'id' : $('#<?php echo $key; ?> input[name="ti-deactivate-option"]:checked').parent().attr('ti-option-id'), |
| 355 |
'msg' : $('#<?php echo $key; ?> input[name="ti-deactivate-option"]:checked').parent().find('textarea').val(), |
| 356 |
'type' : '<?php echo $type; ?>', |
| 357 |
'key' : '<?php echo $key; ?>' |
| 358 |
}, |
| 359 |
}); |
| 360 |
var redirect = $(this).attr('data-ti-action'); |
| 361 |
if (redirect != '') { |
| 362 |
location.href = redirect; |
| 363 |
} else { |
| 364 |
$('body').unbind('thickbox:removed'); |
| 365 |
tb_remove(); |
| 366 |
} |
| 367 |
}); |
| 368 |
|
| 369 |
$(target_element).attr('name', '<?php echo esc_html( $heading ); ?>').attr('href', '<?php echo $src; ?>').addClass('thickbox'); |
| 370 |
var thicbox_timer; |
| 371 |
$(target_element).on('click', function () { |
| 372 |
tiBindThickbox(); |
| 373 |
}); |
| 374 |
|
| 375 |
function tiBindThickbox() { |
| 376 |
var thicbox_timer = setTimeout(function () { |
| 377 |
if ($("#<?php echo esc_html( $key ); ?>").is(":visible")) { |
| 378 |
$("body").trigger('thickbox:iframe:loaded'); |
| 379 |
$("#TB_window").addClass("<?php echo $key; ?>-container"); |
| 380 |
clearTimeout(thicbox_timer); |
| 381 |
$('body').unbind('thickbox:removed'); |
| 382 |
} else { |
| 383 |
tiBindThickbox(); |
| 384 |
} |
| 385 |
}, 100); |
| 386 |
} |
| 387 |
}); |
| 388 |
})(jQuery); |
| 389 |
</script> |
| 390 |
<?php |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Generates the HTML |
| 395 |
* |
| 396 |
* @param string $type The type of product. |
| 397 |
* @param string $key The product key. |
| 398 |
*/ |
| 399 |
function get_html( $type, $key ) { |
| 400 |
$options = 'plugin' === $type ? $this->options_plugin : $this->options_theme; |
| 401 |
$button_submit_before = 'plugin' === $type ? $this->button_submit_before : 'Submit'; |
| 402 |
$button_submit = 'plugin' === $type ? $this->button_submit : 'Submit'; |
| 403 |
$options = $this->randomize_options( apply_filters( $this->product->get_key() . '_feedback_deactivate_options', $options ) ); |
| 404 |
$button_submit_before = apply_filters( $this->product->get_key() . '_feedback_deactivate_button_submit_before', $button_submit_before ); |
| 405 |
$button_submit = apply_filters( $this->product->get_key() . '_feedback_deactivate_button_submit', $button_submit ); |
| 406 |
$button_cancel = apply_filters( $this->product->get_key() . '_feedback_deactivate_button_cancel', $this->button_cancel ); |
| 407 |
|
| 408 |
$options += $this->other; |
| 409 |
|
| 410 |
$list = ''; |
| 411 |
foreach ( $options as $title => $attributes ) { |
| 412 |
$id = $attributes['id']; |
| 413 |
$list .= '<li ti-option-id="' . $id . '"><input type="radio" name="ti-deactivate-option" id="' . $key . $id . '"><label for="' . $key . $id . '">' . str_replace( '{theme}', $this->product->get_name(), $title ) . '</label>'; |
| 414 |
if ( array_key_exists( 'type', $attributes ) ) { |
| 415 |
$list .= '<div>'; |
| 416 |
$placeholder = array_key_exists( 'placeholder', $attributes ) ? __( $attributes['placeholder'] ) : ''; |
| 417 |
switch ( $attributes['type'] ) { |
| 418 |
case 'text': |
| 419 |
$list .= '<textarea style="width: 100%" rows="1" name="comments" placeholder="' . $placeholder . '"></textarea>'; |
| 420 |
break; |
| 421 |
case 'textarea': |
| 422 |
$list .= '<textarea style="width: 100%" rows="2" name="comments" placeholder="' . $placeholder . '"></textarea>'; |
| 423 |
break; |
| 424 |
} |
| 425 |
$list .= '</div>'; |
| 426 |
} |
| 427 |
$list .= '</li>'; |
| 428 |
} |
| 429 |
|
| 430 |
return '<div id="' . $this->product->get_key() . '">' |
| 431 |
. '<ul class="ti-list">' . $list . '</ul>' |
| 432 |
. '<div class="actions">' |
| 433 |
. get_submit_button( |
| 434 |
__( $button_submit_before ), 'secondary', $this->product->get_key() . 'ti-deactivate-yes', false, array( |
| 435 |
'data-after-text' => $button_submit, |
| 436 |
) |
| 437 |
) |
| 438 |
. get_submit_button( __( $button_cancel ), 'primary', $this->product->get_key() . 'ti-deactivate-no', false ) |
| 439 |
. '</div></div>'; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Called when the deactivate button is clicked |
| 444 |
*/ |
| 445 |
function post_deactivate() { |
| 446 |
check_ajax_referer( (string) __CLASS__, 'nonce' ); |
| 447 |
|
| 448 |
if ( ! empty( $_POST['id'] ) ) { |
| 449 |
$this->call_api( |
| 450 |
array( |
| 451 |
'type' => 'deactivate', |
| 452 |
'id' => $_POST['id'], |
| 453 |
'comment' => isset( $_POST['msg'] ) ? $_POST['msg'] : '', |
| 454 |
) |
| 455 |
); |
| 456 |
} |
| 457 |
|
| 458 |
$this->post_deactivate_or_cancel(); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Called when the deactivate/cancel button is clicked |
| 463 |
*/ |
| 464 |
private function post_deactivate_or_cancel() { |
| 465 |
if ( isset( $_POST['type'] ) && isset( $_POST['key'] ) && 'theme' === $_POST['type'] ) { |
| 466 |
set_transient( 'ti_sdk_pause_' . $_POST['key'], true, PAUSE_DEACTIVATE_WINDOW_DAYS * DAY_IN_SECONDS ); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
endif; |
| 471 |
|