| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! class_exists( 'WeForms_Upsell' ) ) : |
| 4 |
|
| 5 |
/** |
| 6 |
* weForms Upsell Class |
| 7 |
*/ |
| 8 |
class WeForms_Upsell { |
| 9 |
|
| 10 |
/** |
| 11 |
* Affiliate code for referal tracking |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $affiliate; |
| 16 |
|
| 17 |
/** |
| 18 |
* Learn more link |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $learn_more = 'https://wedevs.com/weforms-details/'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Instantiate the class |
| 26 |
* |
| 27 |
* @param string $affiliate |
| 28 |
*/ |
| 29 |
function __construct( $affiliate = '' ) { |
| 30 |
$this->affiliate = $affiliate; |
| 31 |
|
| 32 |
add_action( 'init', array( $this, 'init_hooks' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Initialize the hooks |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function init_hooks() { |
| 41 |
|
| 42 |
if ( class_exists( 'weForms' ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
add_action( 'admin_notices', array( $this, 'activation_notice' ) ); |
| 51 |
|
| 52 |
add_action( 'wp_ajax_weforms_upsell_installer', array( $this, 'install_weforms' ) ); |
| 53 |
add_action( 'wp_ajax_weforms_upsell_dismiss', array( $this, 'dismiss_weforms_notice' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Show the plugin installation notice |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function activation_notice() { |
| 62 |
|
| 63 |
if ( $this->is_dismissed() ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
?> |
| 67 |
<div class="updated" id="weforms-upsell-prompt"> |
| 68 |
<div class="weforms-upsell-logo"> |
| 69 |
<img src="https://wedevs-com-wedevs.netdna-ssl.com/wp-content/uploads/2017/08/weforms-upsell.png" width="272" height="71" alt="weForms Logo"> |
| 70 |
</div> |
| 71 |
<div class="weforms-upsell-text"> |
| 72 |
<h2>weForms is here!</h2> |
| 73 |
|
| 74 |
<p>weForms is the next generation contact form plugin for WordPress.</p> |
| 75 |
</div> |
| 76 |
<div class="weforms-upsell-cta"> |
| 77 |
<button id="weforms-upsell-prompt-btn" class="button"><?php _e( 'Install Now', 'weforms' ); ?></button> |
| 78 |
<a href="#" class="learn-more" data-tube="NJvjy9WFyAM">Learn More</a> |
| 79 |
</div> |
| 80 |
<button type="button" class="notice-dismiss" style="padding: 3px;" title="<?php _e( 'Dismiss this notice.' ); ?>"> |
| 81 |
<span class="screen-reader-text"><?php _e( 'Dismiss this notice.' ); ?></span> |
| 82 |
</button> |
| 83 |
</div> |
| 84 |
|
| 85 |
<div class="weforms-upsell-modal" id="weforms-upsell-modal"> |
| 86 |
<a class="close"> |
| 87 |
× |
| 88 |
<span class="screen-reader-text">Close modal window</span> |
| 89 |
</a> |
| 90 |
<div class="video-wrap"> |
| 91 |
<iframe id="weforms-upsell-modal-iframe" width="1280" height="720" src="" frameborder="0" allowfullscreen></iframe> |
| 92 |
</div> |
| 93 |
|
| 94 |
<div class="learn-more"> |
| 95 |
<a href="<?php echo $this->learn_more_link(); ?>" target="_blank" class="button button-primary">Learn more about weForms</a> |
| 96 |
</div> |
| 97 |
</div> |
| 98 |
<div class="weforms-upsell-modal-backdrop" id="weforms-upsell-modal-backdrop"></div> |
| 99 |
|
| 100 |
<style type="text/css"> |
| 101 |
div#weforms-upsell-prompt * { |
| 102 |
box-sizing: border-box; |
| 103 |
} |
| 104 |
|
| 105 |
div#weforms-upsell-prompt { |
| 106 |
display: flex; |
| 107 |
flex-direction: row; |
| 108 |
flex-wrap: nowrap; |
| 109 |
justify-content: flex-start; |
| 110 |
align-content: flex-start; |
| 111 |
align-items: flex-start; |
| 112 |
position: relative; |
| 113 |
border: none; |
| 114 |
margin: 5px 0 15px; |
| 115 |
padding: 0 0 0 10px; |
| 116 |
} |
| 117 |
|
| 118 |
.weforms-upsell-logo { |
| 119 |
margin: 0; |
| 120 |
height: 71px; |
| 121 |
order: 0; |
| 122 |
flex: 0 1 272px; |
| 123 |
align-self: auto; |
| 124 |
padding-left: 15px; |
| 125 |
} |
| 126 |
|
| 127 |
.weforms-upsell-text { |
| 128 |
background: #46b450; |
| 129 |
color: #fff; |
| 130 |
padding: 0; |
| 131 |
height: 71px; |
| 132 |
margin-left: -35px; |
| 133 |
order: 0; |
| 134 |
flex: 1 1 auto; |
| 135 |
align-self: auto; |
| 136 |
} |
| 137 |
|
| 138 |
.weforms-upsell-text h2 { |
| 139 |
color: #fff; |
| 140 |
margin: 10px 0; |
| 141 |
} |
| 142 |
|
| 143 |
.weforms-upsell-cta { |
| 144 |
text-align: center; |
| 145 |
order: 0; |
| 146 |
flex: 0 1 220px; |
| 147 |
align-self: auto; |
| 148 |
padding-top: 20px; |
| 149 |
vertical-align: middle; |
| 150 |
height: 71px; |
| 151 |
line-height: 28px; |
| 152 |
} |
| 153 |
|
| 154 |
.weforms-upsell-modal { |
| 155 |
background: #fff; |
| 156 |
position: fixed; |
| 157 |
top: 5%; |
| 158 |
bottom: 5%; |
| 159 |
right: 10%; |
| 160 |
left: 10%; |
| 161 |
display: none; |
| 162 |
box-shadow: 0 1px 20px 5px rgba(0, 0, 0, 0.1); |
| 163 |
z-index: 160000; |
| 164 |
} |
| 165 |
|
| 166 |
.weforms-upsell-modal .video-wrap { |
| 167 |
position: relative; |
| 168 |
padding-bottom: 56.25%; /* 16:9 */ |
| 169 |
padding-top: 25px; |
| 170 |
height: 0; |
| 171 |
} |
| 172 |
|
| 173 |
.weforms-upsell-modal .video-wrap iframe { |
| 174 |
position: absolute; |
| 175 |
top: 0; |
| 176 |
left: 0; |
| 177 |
width: 100%; |
| 178 |
height: 100%; |
| 179 |
} |
| 180 |
|
| 181 |
.weforms-upsell-modal .learn-more { |
| 182 |
position: absolute; |
| 183 |
bottom: 0; |
| 184 |
right: 10px; |
| 185 |
background: #fff; |
| 186 |
padding: 10px; |
| 187 |
border-radius: 3px; |
| 188 |
} |
| 189 |
|
| 190 |
.weforms-upsell-modal a.close { |
| 191 |
position: absolute; |
| 192 |
top: 20px; |
| 193 |
right: -60px; |
| 194 |
font: 300 1.71429em "dashicons" !important; |
| 195 |
content: '\f335'; |
| 196 |
display: inline-block; |
| 197 |
padding: 10px 20px 0 20px; |
| 198 |
z-index: 5; |
| 199 |
text-decoration: none; |
| 200 |
height: 40px; |
| 201 |
cursor: pointer; |
| 202 |
background: #000; |
| 203 |
color: #fff; |
| 204 |
border-radius: 50%; |
| 205 |
} |
| 206 |
|
| 207 |
.weforms-upsell-modal-backdrop { |
| 208 |
position: fixed; |
| 209 |
z-index: 159999; |
| 210 |
top: 0; |
| 211 |
left: 0; |
| 212 |
right: 0; |
| 213 |
bottom: 0; |
| 214 |
min-height: 360px; |
| 215 |
background: #000; |
| 216 |
opacity: .7; |
| 217 |
display: none; |
| 218 |
} |
| 219 |
|
| 220 |
.weforms-upsell-modal.show, |
| 221 |
.weforms-upsell-modal-backdrop.show { |
| 222 |
display: block; |
| 223 |
} |
| 224 |
</style> |
| 225 |
|
| 226 |
<script type="text/javascript"> |
| 227 |
(function ($) { |
| 228 |
var wrapper = $('#weforms-upsell-prompt'), |
| 229 |
modal = $('#weforms-upsell-modal'), |
| 230 |
modalBackdrop = $('#weforms-upsell-modal-backdrop'), |
| 231 |
iframe = $('#weforms-upsell-modal-iframe'); |
| 232 |
|
| 233 |
wrapper.on('click', '#weforms-upsell-prompt-btn', function (e) { |
| 234 |
var self = $(this); |
| 235 |
|
| 236 |
e.preventDefault(); |
| 237 |
self.addClass('install-now updating-message'); |
| 238 |
self.text('<?php echo esc_js( 'Installing...' ); ?>'); |
| 239 |
|
| 240 |
wp.ajax.send( 'weforms_upsell_installer', { |
| 241 |
data: { |
| 242 |
_wpnonce: '<?php echo wp_create_nonce('weforms_upsell_installer'); ?>' |
| 243 |
}, |
| 244 |
|
| 245 |
success: function(response) { |
| 246 |
self.text('<?php echo esc_js( 'Installed' ); ?>'); |
| 247 |
window.location.href = '<?php echo admin_url( 'admin.php?page=weforms' ); ?>'; |
| 248 |
}, |
| 249 |
|
| 250 |
error: function(error) { |
| 251 |
self.removeClass('install-now updating-message'); |
| 252 |
alert( error ); |
| 253 |
}, |
| 254 |
|
| 255 |
complete: function() { |
| 256 |
self.attr('disabled', 'disabled'); |
| 257 |
self.removeClass('install-now updating-message'); |
| 258 |
} |
| 259 |
}); |
| 260 |
}); |
| 261 |
|
| 262 |
wrapper.on('click', '.notice-dismiss', function (e) { |
| 263 |
e.preventDefault(); |
| 264 |
|
| 265 |
var self = $(this); |
| 266 |
|
| 267 |
wp.ajax.send( 'weforms_upsell_dismiss' ); |
| 268 |
|
| 269 |
self.closest('.updated').slideUp('fast', function() { |
| 270 |
self.remove(); |
| 271 |
}); |
| 272 |
}); |
| 273 |
|
| 274 |
wrapper.on('click', 'a.learn-more', function(e) { |
| 275 |
e.preventDefault(); |
| 276 |
|
| 277 |
modal.addClass('show'); |
| 278 |
modalBackdrop.addClass('show'); |
| 279 |
|
| 280 |
iframe.attr( 'src', 'https://www.youtube.com/embed/sqP-nvyqUdQ?rel=0&controls=0&showinfo=0&autoplay=1' ); |
| 281 |
}); |
| 282 |
|
| 283 |
$('body').on('click', '.weforms-upsell-modal a.close', function(e) { |
| 284 |
e.preventDefault(); |
| 285 |
|
| 286 |
console.log('close modal'); |
| 287 |
|
| 288 |
modal.removeClass('show'); |
| 289 |
modalBackdrop.removeClass('show'); |
| 290 |
|
| 291 |
iframe.attr( 'src', '' ); |
| 292 |
}); |
| 293 |
})(jQuery); |
| 294 |
</script> |
| 295 |
<?php |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Check if the notice is dimissed |
| 300 |
* |
| 301 |
* @return boolean |
| 302 |
*/ |
| 303 |
public function is_dismissed() { |
| 304 |
return 'yes' == get_option( 'weforms_upsell_dismiss', 'no' ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Dismiss the weForms notice |
| 309 |
* |
| 310 |
* @return void |
| 311 |
*/ |
| 312 |
public function dismiss_notice() { |
| 313 |
update_option( 'weforms_upsell_dismiss', 'yes' ); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Learn more link, append affiliate link if present |
| 318 |
* |
| 319 |
* @return string |
| 320 |
*/ |
| 321 |
public function learn_more_link() { |
| 322 |
$link = $this->learn_more; |
| 323 |
|
| 324 |
if ( ! empty( $this->affiliate ) ) { |
| 325 |
$link = add_query_arg( array( 'ref' => $this->affiliate ), $link ); |
| 326 |
} |
| 327 |
|
| 328 |
return $link; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Fail if plugin installtion/activation fails |
| 333 |
* |
| 334 |
* @param Object $thing |
| 335 |
* |
| 336 |
* @return void |
| 337 |
*/ |
| 338 |
public function fail_on_error( $thing ) { |
| 339 |
if ( is_wp_error( $thing ) ) { |
| 340 |
wp_send_json_error( $thing->get_error_message() ); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Install weForms |
| 346 |
* |
| 347 |
* @return void |
| 348 |
*/ |
| 349 |
public function install_weforms() { |
| 350 |
check_ajax_referer( 'weforms_upsell_installer' ); |
| 351 |
|
| 352 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 353 |
wp_send_json_error( __( 'You don\'t have permission to install the plugins' ) ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( ! class_exists( 'WP_User_Frontend' ) ) { |
| 357 |
$wpuf_status = $this->install_plugin( 'wp-user-frontend', 'wpuf.php' ); |
| 358 |
|
| 359 |
$this->fail_on_error( $wpuf_status ); |
| 360 |
} |
| 361 |
|
| 362 |
$weforms_status = $this->install_plugin( 'weforms', 'weforms.php' ); |
| 363 |
$this->fail_on_error( $weforms_status ); |
| 364 |
|
| 365 |
$this->dismiss_notice(); |
| 366 |
|
| 367 |
if ( ! empty( $this->affiliate ) ) { |
| 368 |
update_option( '_weforms_aff_ref', $this->affiliate ); |
| 369 |
} |
| 370 |
|
| 371 |
wp_send_json_success(); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Install and activate a plugin |
| 376 |
* |
| 377 |
* @param string $slug |
| 378 |
* @param string $file |
| 379 |
* |
| 380 |
* @return WP_Error|null |
| 381 |
*/ |
| 382 |
public function install_plugin( $slug, $file ) { |
| 383 |
include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 384 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 385 |
|
| 386 |
$plugin_basename = $slug . '/' . $file; |
| 387 |
|
| 388 |
// if exists and not activated |
| 389 |
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_basename ) ) { |
| 390 |
return activate_plugin( $plugin_basename ); |
| 391 |
} |
| 392 |
|
| 393 |
// seems like the plugin doesn't exists. Download and activate it |
| 394 |
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); |
| 395 |
|
| 396 |
$api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); |
| 397 |
$result = $upgrader->install( $api->download_link ); |
| 398 |
|
| 399 |
if ( is_wp_error( $result ) ) { |
| 400 |
return $result; |
| 401 |
} |
| 402 |
|
| 403 |
return activate_plugin( $plugin_basename ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Dismiss the notice via Ajax |
| 408 |
* |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
public function dismiss_weforms_notice() { |
| 412 |
$this->dismiss_notice(); |
| 413 |
|
| 414 |
wp_send_json_success(); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
endif; |
| 419 |
|