PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / update / class-edd-sl-plugin-updater.php

class-edd-sl-plugin-updater.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at update/class-edd-sl-plugin-updater.php

854 lines 31.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) ) exit;
5
6 /**
7 * Allows plugins to use their own update API.
8 *
9 * @author Easy Digital Downloads
10 * @version 1.6.13
11 */
12 if( !class_exists('WPPB_EDD_SL_Plugin_Updater') ) {
13 class WPPB_EDD_SL_Plugin_Updater {
14
15 private $api_url = '';
16 private $api_data = array();
17 private $name = '';
18 private $slug = '';
19 private $version = '';
20 private $wp_override = false;
21 private $cache_key = '';
22
23 /**
24 * Class constructor.
25 *
26 * @uses plugin_basename()
27 * @uses hook()
28 *
29 * @param string $_api_url The URL pointing to the custom API endpoint.
30 * @param string $_plugin_file Path to the plugin file.
31 * @param array $_api_data Optional data to send with API calls.
32 */
33 public function __construct($_api_url, $_plugin_file, $_api_data = null)
34 {
35
36 global $edd_plugin_data;
37
38 $this->api_url = trailingslashit($_api_url);
39 $this->api_data = $_api_data;
40 $this->name = plugin_basename($_plugin_file);
41 $this->slug = basename($_plugin_file, '.php');
42
43 /**
44 * Necessary in order for the View Details button to work properly when multiple products using
45 * this class aare active
46 *
47 * The original takes the base file name as the slug, but our file names are just `index.php` so we
48 * use the folder name instead
49 */
50 if ( $this->slug === 'index') {
51 $this->slug = dirname( plugin_basename( $_plugin_file ) );
52 }
53 // end modification
54
55 $this->version = $_api_data['version'];
56 $this->wp_override = isset($_api_data['wp_override']) ? (bool)$_api_data['wp_override'] : false;
57 $this->beta = !empty($this->api_data['beta']) ? true : false;
58 $this->cache_key = md5(serialize($this->slug . $this->api_data['license'] . $this->beta));
59
60 $edd_plugin_data[$this->slug] = $this->api_data;
61
62 // Set up hooks.
63 $this->init();
64
65 }
66
67 /**
68 * Set up WordPress filters to hook into WP's update process.
69 *
70 * @uses add_filter()
71 *
72 * @return void
73 */
74 public function init()
75 {
76
77 add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
78 add_filter('plugins_api', array($this, 'plugins_api_filter'), 10, 3);
79 remove_action('after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10);
80 add_action('after_plugin_row_' . $this->name, array($this, 'show_update_notification'), 10, 2);
81 add_action('admin_init', array($this, 'show_changelog'));
82
83 }
84
85 /**
86 * Check for Updates at the defined API endpoint and modify the update array.
87 *
88 * This function dives into the update API just when WordPress creates its update array,
89 * then adds a custom API call and injects the custom plugin data retrieved from the API.
90 * It is reassembled from parts of the native WordPress plugin update code.
91 * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
92 *
93 * @uses api_request()
94 *
95 * @param array $_transient_data Update array build by WordPress.
96 * @return array Modified update array with custom plugin data.
97 */
98 public function check_update($_transient_data)
99 {
100
101 global $pagenow;
102
103 if (!is_object($_transient_data)) {
104 $_transient_data = new stdClass;
105 }
106
107 if ('plugins.php' == $pagenow && is_multisite()) {
108 return $_transient_data;
109 }
110
111 if (!empty($_transient_data->response) && !empty($_transient_data->response[$this->name]) && false === $this->wp_override) {
112 return $_transient_data;
113 }
114
115 $version_info = $this->get_cached_version_info();
116
117 if (false === $version_info) {
118 $version_info = $this->api_request('plugin_latest_version', array('slug' => $this->slug, 'beta' => $this->beta));
119
120 $this->set_version_info_cache($version_info);
121
122 }
123
124 if (false !== $version_info && is_object($version_info) && isset($version_info->new_version)) {
125
126 if (version_compare($this->version, $version_info->new_version, '<')) {
127
128 $_transient_data->response[$this->name] = $version_info;
129
130 }
131
132 $_transient_data->last_checked = current_time('timestamp');
133 $_transient_data->checked[$this->name] = $this->version;
134
135 }
136
137 return $_transient_data;
138 }
139
140 /**
141 * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
142 *
143 * @param string $file
144 * @param array $plugin
145 */
146 public function show_update_notification($file, $plugin)
147 {
148
149 if (is_network_admin()) {
150 return;
151 }
152
153 if (!current_user_can('update_plugins')) {
154 return;
155 }
156
157 if (!is_multisite()) {
158 return;
159 }
160
161 if ($this->name != $file) {
162 return;
163 }
164
165 // Remove our filter on the site transient
166 remove_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'), 10);
167
168 $update_cache = get_site_transient('update_plugins');
169
170 $update_cache = is_object($update_cache) ? $update_cache : new stdClass();
171
172 if (empty($update_cache->response) || empty($update_cache->response[$this->name])) {
173
174 $version_info = $this->get_cached_version_info();
175
176 if (false === $version_info) {
177 $version_info = $this->api_request('plugin_latest_version', array('slug' => $this->slug, 'beta' => $this->beta));
178
179 $this->set_version_info_cache($version_info);
180 }
181
182 if (!is_object($version_info)) {
183 return;
184 }
185
186 if (version_compare($this->version, $version_info->new_version, '<')) {
187
188 $update_cache->response[$this->name] = $version_info;
189
190 }
191
192 $update_cache->last_checked = current_time('timestamp');
193 $update_cache->checked[$this->name] = $this->version;
194
195 set_site_transient('update_plugins', $update_cache);
196
197 } else {
198
199 $version_info = $update_cache->response[$this->name];
200
201 }
202
203 // Restore our filter
204 add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
205
206 if (!empty($update_cache->response[$this->name]) && version_compare($this->version, $version_info->new_version, '<')) {
207
208 // build a plugin list row, with update notification
209 $wp_list_table = _get_list_table('WP_Plugins_List_Table');
210 # <tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange">
211 echo '<tr class="plugin-update-tr" id="' . esc_attr( $this->slug ) . '-update" data-slug="' . esc_attr( $this->slug ) . '" data-plugin="' . esc_attr( $this->slug ) . '/' . esc_attr( $file ) . '">';
212 echo '<td colspan="3" class="plugin-update colspanchange">';
213 echo '<div class="update-message notice inline notice-warning notice-alt">';
214
215 $changelog_link = self_admin_url('index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911');
216
217 if (empty($version_info->download_link)) {
218 printf(
219 __('There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'profile-builder'), //phpcs:ignore
220 esc_html($version_info->name),
221 '<a target="_blank" class="thickbox" href="' . esc_url($changelog_link) . '">',
222 esc_html($version_info->new_version),
223 '</a>'
224 );
225 } else {
226 printf(
227 __('There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'profile-builder'), //phpcs:ignore
228 esc_html($version_info->name),
229 '<a target="_blank" class="thickbox" href="' . esc_url($changelog_link) . '">',
230 esc_html($version_info->new_version),
231 '</a>',
232 '<a href="' . esc_url(wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=') . $this->name, 'upgrade-plugin_' . $this->name)) . '">',
233 '</a>'
234 );
235 }
236
237 do_action("in_plugin_update_message-{$file}", $plugin, $version_info);
238
239 echo '</div></td></tr>';
240 }
241 }
242
243 /**
244 * Updates information on the "View version x.x details" page with custom data.
245 *
246 * @uses api_request()
247 *
248 * @param mixed $_data
249 * @param string $_action
250 * @param object $_args
251 * @return object $_data
252 */
253 public function plugins_api_filter($_data, $_action = '', $_args = null)
254 {
255
256 if ($_action != 'plugin_information') {
257
258 return $_data;
259
260 }
261
262 if (!isset($_args->slug) || ($_args->slug != $this->slug)) {
263
264 return $_data;
265
266 }
267
268 $to_send = array(
269 'slug' => $this->slug,
270 'is_ssl' => is_ssl(),
271 'fields' => array(
272 'banners' => array(),
273 'reviews' => false
274 )
275 );
276
277 $cache_key = 'edd_api_request_' . md5(serialize($this->slug . $this->api_data['license'] . $this->beta));
278
279 // Get the transient where we store the api request for this plugin for 24 hours
280 $edd_api_request_transient = $this->get_cached_version_info($cache_key);
281
282 //If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
283 if (empty($edd_api_request_transient)) {
284
285 $api_response = $this->api_request('plugin_information', $to_send);
286
287 // Expires in 3 hours
288 $this->set_version_info_cache($api_response, $cache_key);
289
290 if (false !== $api_response) {
291 $_data = $api_response;
292 }
293
294 } else {
295 $_data = $edd_api_request_transient;
296 }
297
298 // Convert sections into an associative array, since we're getting an object, but Core expects an array.
299 if (isset($_data->sections) && !is_array($_data->sections)) {
300 $new_sections = array();
301 foreach ($_data->sections as $key => $value) {
302 $new_sections[$key] = $value;
303 }
304
305 $_data->sections = $new_sections;
306 }
307
308 // Convert banners into an associative array, since we're getting an object, but Core expects an array.
309 if (isset($_data->banners) && !is_array($_data->banners)) {
310 $new_banners = array();
311 foreach ($_data->banners as $key => $value) {
312 $new_banners[$key] = $value;
313 }
314
315 $_data->banners = $new_banners;
316 }
317
318 return $_data;
319 }
320
321 /**
322 * Disable SSL verification in order to prevent download update failures
323 *
324 * @param array $args
325 * @param string $url
326 * @return object $array
327 */
328 public function http_request_args($args, $url)
329 {
330
331 $verify_ssl = $this->verify_ssl();
332 if (strpos($url, 'https://') !== false && strpos($url, 'edd_action=package_download')) {
333 $args['sslverify'] = $verify_ssl;
334 }
335 return $args;
336
337 }
338
339 /**
340 * Calls the API and, if successfull, returns the object delivered by the API.
341 *
342 * @uses get_bloginfo()
343 * @uses wp_remote_post()
344 * @uses is_wp_error()
345 *
346 * @param string $_action The requested action.
347 * @param array $_data Parameters for the API action.
348 * @return false|object
349 */
350 private function api_request($_action, $_data)
351 {
352
353 global $wp_version;
354
355 $data = array_merge($this->api_data, $_data);
356
357 if ($data['slug'] != $this->slug) {
358 return;
359 }
360
361 if ($this->api_url == trailingslashit(home_url())) {
362 return false; // Don't allow a plugin to ping itself
363 }
364
365 $api_params = array(
366 'edd_action' => 'get_version',
367 'license' => !empty($data['license']) ? $data['license'] : '',
368 'item_name' => isset($data['item_name']) ? $data['item_name'] : false,
369 'item_id' => isset($data['item_id']) ? $data['item_id'] : false,
370 'version' => isset($data['version']) ? $data['version'] : false,
371 'slug' => $data['slug'],
372 'author' => $data['author'],
373 'url' => home_url(),
374 'beta' => !empty($data['beta']),
375 );
376
377 $verify_ssl = $this->verify_ssl();
378 $request = wp_remote_post($this->api_url, array('timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params));
379
380 if (!is_wp_error($request)) {
381 $request = json_decode(wp_remote_retrieve_body($request));
382 }
383
384 if ($request && isset($request->sections)) {
385 $request->sections = maybe_unserialize($request->sections);
386 } else {
387 $request = false;
388 }
389
390 if ($request && isset($request->banners)) {
391 $request->banners = maybe_unserialize($request->banners);
392 }
393
394 if (!empty($request->sections)) {
395 foreach ($request->sections as $key => $section) {
396 $request->$key = (array)$section;
397 }
398 }
399
400 return $request;
401 }
402
403 public function show_changelog()
404 {
405
406 global $edd_plugin_data;
407
408 if (empty($_REQUEST['edd_sl_action']) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action']) {
409 return;
410 }
411
412 if (empty($_REQUEST['plugin'])) {
413 return;
414 }
415
416 if (empty($_REQUEST['slug'])) {
417 return;
418 }
419
420 if (!current_user_can('update_plugins')) {
421 wp_die( esc_html__('You do not have permission to install plugin updates', 'profile-builder'), esc_html__('Error', 'profile-builder'), array('response' => 403));
422 }
423
424 $data = $edd_plugin_data[sanitize_text_field( $_REQUEST['slug'] )];
425 $beta = !empty($data['beta']) ? true : false;
426 $cache_key = md5('edd_plugin_' . sanitize_key($_REQUEST['plugin']) . '_' . $beta . '_version_info');
427 $version_info = $this->get_cached_version_info($cache_key);
428
429 if (false === $version_info) {
430
431 $api_params = array(
432 'edd_action' => 'get_version',
433 'item_name' => isset($data['item_name']) ? $data['item_name'] : false,
434 'item_id' => isset($data['item_id']) ? $data['item_id'] : false,
435 'slug' => sanitize_text_field( $_REQUEST['slug'] ),
436 'author' => $data['author'],
437 'url' => home_url(),
438 'beta' => !empty($data['beta'])
439 );
440
441 $verify_ssl = $this->verify_ssl();
442 $request = wp_remote_post($this->api_url, array('timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params));
443
444 if (!is_wp_error($request)) {
445 $version_info = json_decode(wp_remote_retrieve_body($request));
446 }
447
448
449 if (!empty($version_info) && isset($version_info->sections)) {
450 $version_info->sections = maybe_unserialize($version_info->sections);
451 } else {
452 $version_info = false;
453 }
454
455 if (!empty($version_info)) {
456 foreach ($version_info->sections as $key => $section) {
457 $version_info->$key = (array)$section;
458 }
459 }
460
461 $this->set_version_info_cache($version_info, $cache_key);
462
463 }
464
465 if (!empty($version_info) && isset($version_info->sections['changelog'])) {
466 echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $version_info->sections['changelog'] ) . '</div>';
467 }
468
469 exit;
470 }
471
472 public function get_cached_version_info($cache_key = '')
473 {
474
475 if (empty($cache_key)) {
476 $cache_key = $this->cache_key;
477 }
478
479 $cache = get_option($cache_key);
480
481 if (empty($cache['timeout']) || current_time('timestamp') > $cache['timeout']) {
482 return false; // Cache is expired
483 }
484
485 return json_decode($cache['value']);
486
487 }
488
489 public function set_version_info_cache($value = '', $cache_key = '')
490 {
491
492 if (empty($cache_key)) {
493 $cache_key = $this->cache_key;
494 }
495
496 $data = array(
497 'timeout' => strtotime('+3 hours', current_time('timestamp')),
498 'value' => json_encode($value)
499 );
500
501 update_option($cache_key, $data);
502
503 }
504
505 /**
506 * Returns if the SSL of the store should be verified.
507 *
508 * @since 1.6.13
509 * @return bool
510 */
511 private function verify_ssl()
512 {
513 return (bool)apply_filters('edd_sl_api_request_verify_ssl', true, $this);
514 }
515
516 }
517 }
518
519 class WPPB_Plugin_Updater {
520
521 private $store_url = "https://www.cozmoslabs.com";
522
523 public function __construct(){
524
525 if( defined( 'WPPB_PAID_PLUGIN_DIR' ) ){
526 add_action('admin_init', array( $this, 'activate_license' ) );
527 add_action('admin_init', array( $this, 'deactivate_license' ) );
528 add_action('admin_notices', array( $this, 'admin_activation_notices' ) );
529
530 add_filter('pre_set_site_transient_update_plugins', array( $this, 'check_license' ) );
531
532 // Activate current site if license is correct
533 add_action('admin_init', array( $this, 'initial_site_activation' ) );
534 }
535
536 }
537
538 protected function get_option( $license_key_option ){
539 return get_option( $license_key_option );
540 }
541
542 protected function delete_option( $license_key_option ){
543 delete_option( $license_key_option );
544 }
545
546 protected function update_option( $license_key_option, $value ){
547 update_option( $license_key_option, $value );
548 }
549
550 protected function license_page_url( ){
551
552 if( !is_multisite() )
553 return admin_url( 'admin.php?page=profile-builder-general-settings' );
554 else
555 return network_admin_url( 'admin.php?page=profile-builder-register' );
556
557 }
558
559 public function edd_sanitize_license( $new ) {
560 $new = sanitize_text_field($new);
561 $old = wppb_get_serial_number();
562 if( $old && $old != $new ) {
563 $this->delete_option( 'wppb_license_status' ); // new license has been entered, so must reactivate
564 }
565 return $new;
566 }
567
568 /**
569 * This function is run when wordpress checks for updates ( twice a day I believe )
570 * @param $transient_data
571 * @return mixed
572 */
573 public function check_license( $transient_data ){
574
575 if( empty( $transient_data->response ) )
576 return $transient_data;
577
578 if ( false === ( $wppb_check_license = get_transient( 'wppb_checked_licence' ) ) ) {
579
580 $license = trim( wppb_get_serial_number() );
581 $license_details = array();
582
583 // data to send in our API request
584 $api_params = array(
585 'edd_action' => 'activate_license', //as the license is already activated this does not do anything. We could use check_license action but it gives different results so we can't use it consistently with the result we get from the moment we activate it
586 'license' => $license,
587 'item_name' => urlencode( $this->get_edd_product_name() ), // the name of our product in EDD
588 'url' => home_url()
589 );
590
591 // Call the custom API.
592 $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
593
594 // make sure the response came back okay
595 if ( !is_wp_error($response) ) {
596
597 $license_data = json_decode(wp_remote_retrieve_body($response));
598
599 if ( false === $license_data->success )
600 $license_details = $license_data;
601 else
602 $license_details = $license_data;
603
604 }
605
606 $this->update_option('wppb_license_details', $license_details);
607
608 if( !$license ){
609 //we need to throw a notice if we have a pro addon active and no license entered
610 $license_details = (object) array( 'error' => 'missing' );
611 $this->update_option('wppb_license_details', $license_details);
612 }
613
614
615 set_transient( 'wppb_checked_licence', 'yes', DAY_IN_SECONDS );
616
617 }
618
619 return $transient_data;
620 }
621
622 public function admin_activation_notices() {
623 if ( isset( $_GET['wppb_sl_activation'] ) && ! empty( $_GET['message'] ) ) {
624
625 switch( $_GET['wppb_sl_activation'] ) {
626 case 'false':
627 $class ="error";
628 break;
629 case 'true':
630 default:
631 $class ="updated";
632 break;
633 }
634
635 ?>
636 <div class="<?php echo esc_attr( $class ); ?>">
637 <p><?php echo wp_kses_post( urldecode( $_GET['message'] ) );//phpcs:ignore ?></p>
638 </div>
639 <?php
640 }
641 }
642
643 public function activate_license() {
644
645 // listen for our activate button to be clicked
646 if( isset( $_POST['wppb_edd_license_activate'] ) ) {
647 // run a quick security check
648 if( ! check_admin_referer( 'wppb_license_nonce', 'wppb_license_nonce' ) )
649 return; // get out if we didn't click the Activate button
650
651
652 if ( isset( $_POST['wppb_license_key'] ) && preg_match('/^[*]+$/', $_POST['wppb_license_key']) && strlen( $_POST['wppb_license_key'] ) > 5 ) { //phpcs:ignore
653 // pressed submit without altering the existing license key (containing only * as outputted by default)
654 // useful for Deactivating/Activating valid license back
655 $license = wppb_get_serial_number();
656 } else {
657 // save the license
658 $license = $this->edd_sanitize_license( trim( $_POST['wppb_license_key'] ) );//phpcs:ignore
659 $this->update_option( 'wppb_license_key', $license );
660 }
661
662 $message = array();
663 $license_details = array();
664
665 // data to send in our API request
666 $api_params = array(
667 'edd_action' => 'activate_license',
668 'license' => $license,
669 'item_name' => urlencode( $this->get_edd_product_name() ), // the name of our product in EDD
670 'url' => home_url()
671 );
672
673 // Call the custom API.
674 $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
675
676 // make sure the response came back okay
677 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
678
679 $response_error_message = $response->get_error_message();
680 $message[] = ( is_wp_error( $response ) && ! empty( $response_error_message ) ) ? $response->get_error_message() : __( 'An error occurred, please try again.', 'profile-builder' );
681
682 } else {
683
684 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
685
686 if ( false === $license_data->success ) {
687
688 switch( $license_data->error ) {
689 case 'expired' :
690 $message[] = sprintf(
691 __( 'Your license key expired on %s.', 'profile-builder' ),
692 date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
693 );
694 break;
695 case 'revoked' :
696 $message[] = __( 'Your license key has been disabled.', 'profile-builder' );
697 break;
698 case 'missing' :
699 $message[] = __( 'Invalid license.', 'profile-builder' );
700 break;
701 case 'invalid' :
702 case 'site_inactive' :
703 $message[] = __( 'Your license is not active for this URL.', 'profile-builder' );
704 break;
705 case 'item_name_mismatch' :
706 $message[] = sprintf( __( 'This appears to be an invalid license key for %s.', 'profile-builder' ), $this->get_edd_product_name() );
707 break;
708 case 'no_activations_left':
709 $message[] = __( 'Your license key has reached its activation limit.', 'profile-builder' );
710 break;
711 default :
712 $message[] = __( 'An error occurred, please try again.', 'profile-builder' );
713 break;
714 }
715
716 $license_details = $license_data;
717
718 } else {
719 $license_details = $license_data;
720 }
721
722 }
723
724 //store the license reponse for each addon in the database
725 $this->update_option( 'wppb_license_details', $license_details );
726
727
728 // Check if anything passed on a message constituting a failure
729 if ( ! empty( $message ) ) {
730 $message = implode( "<br/>", array_unique($message) );//if we got the same message for multiple addons show just one, and add a br in case we show multiple messages
731 $redirect = add_query_arg( array( 'wppb_sl_activation' => 'false', 'message' => urlencode( $message ) ), $this->license_page_url() );
732
733 $this->update_option( 'wppb_license_status', isset( $license_data->error ) ? $license_data->error : $license_data->license );
734
735 wp_redirect( $redirect );
736 exit();
737 }
738
739 // $license_data->license will be either "valid" or "invalid"
740 $this->update_option( 'wppb_license_status', isset( $license_data->error ) ? $license_data->error : $license_data->license );
741
742 wp_redirect( add_query_arg( array( 'wppb_sl_activation' => 'true', 'message' => urlencode( __( 'You have successfully activated your license.', 'profile-builder' ) ) ), $this->license_page_url() ) );
743 exit();
744 }
745 }
746
747
748 public function initial_site_activation(){
749
750 if( is_multisite() )
751 $edd_sl_initial_activation = get_network_option( null, 'wppb_edd_sl_initial_activation', false );
752 else
753 $edd_sl_initial_activation = get_option( 'wppb_edd_sl_initial_activation', false );
754
755 if( $edd_sl_initial_activation != false )
756 return;
757
758 $license = wppb_get_serial_number();
759
760 if( empty( $license ) )
761 return;
762
763 // data to send in our API request
764 $api_params = array(
765 'edd_action' => 'activate_license',
766 'license' => $license,
767 'item_name' => urlencode( $this->get_edd_product_name() ), // the name of our product in EDD
768 'url' => home_url()
769 );
770
771 // Call the custom API.
772 $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
773
774 if ( !is_wp_error($response) ) {
775
776 $license_data = json_decode(wp_remote_retrieve_body($response));
777
778 if ( false === $license_data->success )
779 $license_details = $license_data;
780 else
781 $license_details = $license_data;
782
783 }
784
785 $this->update_option('wppb_license_details', $license_details);
786
787 if( is_multisite() )
788 update_network_option( null, 'wppb_edd_sl_initial_activation', 'yes' );
789 else
790 update_option( 'wppb_edd_sl_initial_activation', 'yes', false );
791
792 }
793
794 function deactivate_license() {
795
796 // listen for our activate button to be clicked
797 if( isset( $_POST['wppb_edd_license_deactivate'] ) ) {
798
799 // run a quick security check
800 if( ! check_admin_referer( 'wppb_license_nonce', 'wppb_license_nonce' ) )
801 return; // get out if we didn't click the Activate button
802
803 // retrieve the license from the database
804 $license = trim( wppb_get_serial_number() );
805
806 // data to send in our API request
807 $api_params = array(
808 'edd_action' => 'deactivate_license',
809 'license' => $license,
810 'item_name' => urlencode( $this->get_edd_product_name() ), // the name of our product in EDD
811 'url' => home_url()
812 );
813
814 // Call the custom API.
815 $response = wp_remote_post( $this->store_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
816
817 // make sure the response came back okay
818 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
819
820 if ( is_wp_error( $response ) )
821 $message = $response->get_error_message();
822 else
823 $message = __( 'An error occurred, please try again.', 'profile-builder' );
824
825 wp_redirect( add_query_arg( array( 'wppb_sl_activation' => 'false', 'message' => urlencode( $message ) ), $this->license_page_url() ) );
826 exit();
827 }
828
829 // decode the license data
830 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
831
832 // $license_data->license will be either "deactivated" or "failed"
833 // regardless, we delete the record in the client website. Otherwise, if he tries to add a new license, he can't.
834 if( $license_data->license == 'deactivated' || $license_data->license == 'failed'){
835 delete_option( 'wppb_license_status' );
836 delete_option( 'wppb_license_details' );
837 }
838
839 wp_redirect( $this->license_page_url() );
840 exit();
841
842 }
843
844 }
845
846 function get_edd_product_name(){
847
848 return PROFILE_BUILDER;
849
850 }
851
852 }
853
854 new WPPB_Plugin_Updater();