PluginProbe
WPPizza – A Restaurant Plugin / 3.20
WPPizza – A Restaurant Plugin v3.20
trunk 2.16.11.28 3.19 3.19.1 3.19.2 3.19.3 3.19.4 3.19.5 3.19.6 3.19.7 3.19.7.1 3.19.7.2 3.19.7.3 3.19.7.4 3.19.8 3.19.8.1 3.19.8.2 3.19.8.3 3.19.9 3.20 3.20.1
wppizza / classes / modules / mod.tools.licenses.init.php

mod.tools.licenses.init.php in WPPizza – A Restaurant Plugin 3.20, at classes/modules/mod.tools.licenses.init.php

1,169 lines 40.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPPIZZA_MODULE_TOOLS_LICENCES_INIT Class
4 *
5 * @package WPPIZZA
6 * @subpackage WPPIZZA_MODULE_TOOLS_LICENCES_INIT
7 * @copyright Copyright (c) 2015, Oliver Bach
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 3.0
10 *
11 */
12 if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/
13
14
15 /************************************************************************************************************************
16 *
17 *
18 *
19 *
20 *
21 *
22 ************************************************************************************************************************/
23 class WPPIZZA_MODULE_TOOLS_LICENCES_INIT{
24
25 /*
26 which admin subpage (identified there by this->class_key) are we adding this to
27 @param str
28 */
29 private $settings_page = 'tools';
30
31 /*
32 must be unique within this admin page
33 @param str - _GET['tab'] value
34 @param str - (sub) section of _GET['tab'] page (there's only this single section here , but might get added to in the future if required)
35 */
36 private $tab_key = 'licenses';
37 private $section_key = 'licenses_list';
38
39 /*
40 bool flag to see if there are any edd enabled plugins/gateways that need to be
41 verified and/or have licenses and license keys displayed etc
42 @param bool
43 */
44 private $has_registered_edd_plugins = false ;
45
46 /*
47 array (might be empty) of edd enabled plugins/gateways
48 @param array
49 */
50 private $edd_registered_plugins = false ;
51
52 /*
53 time - in seconds - when we should recheck since the last verification (10800 / 3 hours) set to 0 for testing
54 @param int
55 */
56 private $edd_verification_interval = 10800;//10800;
57
58 /*
59 array of options for edd enabled plugins
60 @param array
61 */
62 private $edd_plugin_updated = array();
63
64
65 function __construct() {
66
67 /**********************************************************
68 [add settings to admin]
69 ***********************************************************/
70 if(is_admin()){
71 /* check if any plugins/gateways have edd licenses registered to add labels and tabs */
72 add_action('init', array($this, 'has_edd_registered_plugins'), 5 );
73 add_action('admin_init', array($this, 'edd_init_updater'), 6 );
74
75
76 /*** alter submenu label link***/
77 add_filter('wppizza_filter_admin_label_link_label_'.$this->settings_page.'', array($this, 'admin_link_label'), 10);
78
79 /*** add to a specific tab ***/
80 add_filter('wppizza_filter_admin_tabs_'.$this->settings_page.'', array($this, 'admin_tabs'), 10);
81
82 /* add admin options settings page*/
83 add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 10, 5);
84
85 /* add admin options settings page fields */
86 add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 10, 5);
87
88 /**additional text header for this tab*/
89 add_action('wppizza_settings_sections_header_'.$this->settings_page.'', array( $this, 'sections_header'), 10, 2 );
90
91 /*execute some helper functions once to use their return multiple times */
92 add_action('current_screen', array( $this, 'wppizza_add_helpers') );
93
94 /** admin ajax **/
95 add_action('wppizza_ajax_admin_'.$this->settings_page.'', array( $this, 'admin_ajax'));
96
97 /** verify license status on load of tab**/
98 add_action('current_screen', array($this, 'edd_verify'));
99
100 /** echo license inputs**/
101 add_action('wppizza_edd_init', array($this, 'edd_echo'), 10, 2);
102
103 }
104
105 }
106
107 /*******************************************************************************************************************************************************
108 * [check if there are any plugins or gateways that have edd licenses defined]
109 * [must run early as otherwise we will not be able to filter the link labels etc ]
110 * @param void
111 * @return void
112 *******************************************************************************************************************************************************/
113 function has_edd_registered_plugins(){
114
115 /*
116 checking if there is any edd enabled plugin/extension
117 */
118 $registered_plugins = apply_filters('wppizza_edd_licenses', array());
119 if(!empty($registered_plugins)){
120 $this -> has_registered_edd_plugins = true;
121 return;
122 }
123 /*
124 checking if there is any edd enabled gateway
125 */
126 $registered_gateways = apply_filters('wppizza_register_gateways', array());
127 foreach($registered_gateways as $gateway_class){
128
129
130 if (is_callable(array($gateway_class, 'edd_init'))){
131 $this -> has_registered_edd_plugins = true;
132 return;
133 }
134
135 /*
136 also checking for VERY OLD edd enabled gateway
137 that used "gateway_edd_sl" method
138 */
139 if (is_callable(array($gateway_class, 'gateway_edd_sl'))){
140 $this -> has_registered_edd_plugins = true;
141 return;
142 }
143
144 }
145 }
146
147 /*******************************************************************************
148 *
149 *
150 * [add edd sl updater class]
151 * @param void
152 * @return void
153 *
154 *******************************************************************************/
155 function edd_init_updater(){
156
157 #uncomment to test
158 #delete_option('_site_transient_update_plugins');//test
159
160 /* skip on ajax and non admin (although only called on admin init anyway)*/
161 if( !is_admin() || wppizza_is_ajax()){
162 return;
163 }
164
165 /*
166 simply skip if we know there are none
167 and we are not passing things on distinctly
168 */
169 if(empty($this -> has_registered_edd_plugins)){
170 return;
171 }
172
173 /*
174 include updater class
175 */
176 if( !class_exists( 'WPPIZZA_EDD_SL_UPDATER' ) ) {
177 require_once(WPPIZZA_PATH .'classes/shared/wppizza.edd.plugin.updater.latest.php');
178 }
179
180 /*
181 get licenses etc
182 */
183 $defined_licenses = $this->get_edd_registered_plugins();
184 $edd_updater_data = array();
185
186 foreach($defined_licenses as $key => $values){
187
188 /*
189 check that there's a license key entered
190 to start off with -
191 gateways are set-up a bit differently though
192 */
193 if(!empty($values['is_gateway'])){
194
195 /*
196 gateways
197 */
198
199 if(!empty($values['options']['GatewayEDDLicense']['licence']) && !empty($values['edd_data'])){
200
201
202 $edd_updater_data[$key]['url'] = $values['edd_data']['url'] ;
203 $edd_updater_data[$key]['path'] = $values['edd_data']['path'] ;
204 $edd_updater_data[$key]['version'] = $values['edd_data']['version'] ;
205 $edd_updater_data[$key]['license'] = $values['options']['GatewayEDDLicense']['licence'] ;
206 $edd_updater_data[$key]['url'] = $values['edd_data']['url'];
207 $edd_updater_data[$key]['plugin'] = $values['edd_data']['path'];
208 $edd_updater_data[$key]['item_name'] = $values['edd_data']['name'] ;
209
210 /* optional */
211 $edd_updater_data[$key]['author'] = empty($values['edd_data']['author']) ? 'ollybach' : $values['edd_data']['author'] ;
212 $edd_updater_data[$key]['item_id'] = !empty($values['edd_data']['item_id']) ? $values['edd_data']['item_id'] : '';
213 }
214
215
216 }else{
217 if(!empty($values['options']['license']['key']) && !empty($values['edd_data'])){
218
219 $edd_updater_data[$key]['url'] = $values['edd_data']['url'] ;
220 $edd_updater_data[$key]['path'] = $values['edd_data']['path'] ;
221 $edd_updater_data[$key]['version'] = $values['edd_data']['version'] ;
222 $edd_updater_data[$key]['license'] = $values['options']['license']['key'] ;
223 $edd_updater_data[$key]['url'] = $values['edd_data']['url'];
224 $edd_updater_data[$key]['plugin'] = $values['edd_data']['path'];
225 $edd_updater_data[$key]['item_name'] = $values['edd_data']['name'] ;
226
227 /* optional */
228 $edd_updater_data[$key]['author'] = empty($values['edd_data']['author']) ? 'ollybach' : $values['edd_data']['author'] ;
229 $edd_updater_data[$key]['item_id'] = !empty($values['edd_data']['item_id']) ? $values['edd_data']['item_id'] : '';
230 }
231 }
232 }
233
234
235 /*
236 setup the updater
237 */
238 if(!empty($edd_updater_data)){
239 foreach($edd_updater_data as $data){
240
241 /*
242 checking by id or name ?
243 */
244 $check_type = !empty($data['item_id']) ? 'item_id' : 'item_name' ;
245 $check_value = !empty($data['item_id']) ? $data['item_id'] : $data['item_name'];
246
247 /*
248 run updater for each plugn
249 */
250 $edd_updater = new WPPIZZA_EDD_SL_UPDATER(
251
252 $data['url'],
253
254 $data['path'],
255
256 array(
257 'version' => $data['version'], // current version number
258 'license' => $data['license'], // license key (used get_option above to retrieve from DB)
259 'author' => $data['author'], // author of this plugin
260 'url' => home_url(), // always added
261 'plugin' => $data['path'], // added to eliminate php notice on admin plugins screen if missing
262 $check_type => $check_value, // use id instead of name if we have it
263 )
264
265 );
266
267 }}
268
269 return ;
270 }
271
272 /*******************************************************************************************************************************************************
273 * [get all plugins/gateways that have edd licenses registered including their current options set
274 * called just before verifying and displaying licenses inputs - get once only
275 * @param void
276 * @return void
277 *******************************************************************************************************************************************************/
278 function get_edd_registered_plugins(){
279
280 static $edd_registered_plugins = null;
281
282
283 /*
284 simply return empty array if we already know there are none
285 */
286 if(empty($this -> has_registered_edd_plugins)){
287 return array();
288 }
289
290 if($edd_registered_plugins === null){
291 /*
292 ini as empty array
293 */
294 $edd_registered_plugins = array();
295
296 /*
297 get plugins/extensions with defined edd licenses
298 registered by filter.
299 */
300 $registered_plugins = apply_filters('wppizza_edd_licenses', array());
301
302 foreach($registered_plugins as $slug => $registered_plugin){
303 $edd_registered_plugins[$slug] = $registered_plugin;
304 }
305
306 /*
307 get gateways registered by filter with defined edd_init method
308 */
309 $registered_gateways = apply_filters('wppizza_register_gateways', array());
310 foreach($registered_gateways as $gateway_class){
311
312 if( !class_exists( $gateway_class ) ) {
313 continue;
314 }
315
316
317 $init_class = new $gateway_class();
318 if (is_callable(array($init_class, 'edd_init'))){
319 $edd_registered_plugins[$gateway_class] = $init_class->edd_init();
320 $edd_registered_plugins[$gateway_class]['is_gateway'] = true;
321 }
322
323 /*
324 also checking for VERY OLD edd enabled gateway
325 that used "gateway_edd_sl" method only
326 (using elseif here as some might have both methods)
327 */
328 elseif (is_callable(array($init_class, 'gateway_edd_sl'))){
329
330 $gw_options = get_option(strtolower($gateway_class));
331
332 $edd_registered_plugins[$gateway_class]['slug'] = $gateway_class;
333 $edd_registered_plugins[$gateway_class]['edd_data'] = $init_class->gateway_edd_sl();
334 $edd_registered_plugins[$gateway_class]['edd_data']['option_name'] = strtolower($gateway_class);
335 $edd_registered_plugins[$gateway_class]['edd_data']['version'] = !empty($gw_options['_gateway_version']) ? $gw_options['_gateway_version'] : '' ;
336 $edd_registered_plugins[$gateway_class]['options']['GatewayEDDLicense'] = $gw_options['GatewayEDDLicense'];
337 $edd_registered_plugins[$gateway_class]['is_gateway'] = true;
338
339 }
340 }
341 }
342
343
344 return $edd_registered_plugins;
345 }
346 /*******************************************************************************************************************************************************
347 *
348 * [verify license when loading license tab]
349 *
350 ********************************************************************************************************************************************************/
351 function edd_verify($current_screen){
352
353 if( is_admin() &&
354 ( !defined('DOING_AJAX') || !DOING_AJAX ) &&
355 (
356 (
357 $current_screen->id==''.WPPIZZA_SLUG.'_page_tools' && !empty($_GET['tab']) && 'licenses' == $_GET['tab']
358 )
359 )
360 ){
361
362 /*
363 plugins with edd defined licenses
364 */
365 //$defined_licenses = apply_filters('wppizza_edd_licenses', array());
366 $defined_licenses = $this->get_edd_registered_plugins();
367
368
369 /*
370 loop through plugins/gateways with defined licenses
371 updating options as required
372 */
373 foreach($defined_licenses as $plugin_slug => $plugin_data){
374
375 /*
376 force lowercase
377 */
378 $plugin_slug = strtolower($plugin_slug);
379
380 /*
381 for historical reasons, gateways use GatewayEDDLicense as key
382 , "licence" (with a c) instead of key
383 , "timestamp" instead of verified
384 */
385 $is_gateway_license = !empty($plugin_data['is_gateway']) ? true : false;
386
387
388 /* standard extensions (non gateway) plugins */
389 if(!$is_gateway_license){
390 /*
391 currently set license key, if any
392 */
393 $license_data['key'] = isset($plugin_data['options']['license']['key']) ? $plugin_data['options']['license']['key'] : false ;
394 /*
395 currently set license status, if any
396 */
397 $license_data['status'] = isset($plugin_data['options']['license']['status']) ? $plugin_data['options']['license']['status'] : false ;
398 /*
399 currently set expiry
400 */
401 $license_data['expires'] = isset($plugin_data['options']['license']['expires']) ? $plugin_data['options']['license']['expires'] : false ;
402 /*
403 currently set time last time license was verified, else 0
404 */
405 $license_data['verified'] = !empty($plugin_data['options']['license']['verified']) ? $plugin_data['options']['license']['verified'] : 0 ;
406 /*
407 currently set error last time license was verified, else 0
408 */
409 $license_data['error'] = !empty($plugin_data['options']['license']['error']) ? $plugin_data['options']['license']['error'] : array();
410 }else{
411
412
413 /*
414 currently set license key, if any
415 */
416 $license_data['key'] = isset($plugin_data['options']['GatewayEDDLicense']['licence']) ? $plugin_data['options']['GatewayEDDLicense']['licence'] : false ;
417 /*
418 currently set license status, if any
419 */
420 $license_data['status'] = isset($plugin_data['options']['GatewayEDDLicense']['status']) ? $plugin_data['options']['GatewayEDDLicense']['status'] : false ;
421 /*
422 currently set license status, if any
423 */
424 $license_data['expires'] = isset($plugin_data['options']['GatewayEDDLicense']['expires']) ? $plugin_data['options']['GatewayEDDLicense']['expires'] : false ;
425 /*
426 currently set time last time license was verified, else 0
427 */
428 $license_data['verified'] = !empty($plugin_data['options']['GatewayEDDLicense']['timestamp']) ? $plugin_data['options']['GatewayEDDLicense']['timestamp'] : 0 ;
429 /*
430 currently set error last time license was verified, else 0
431 */
432 $license_data['error'] = !empty($plugin_data['options']['GatewayEDDLicense']['error']) ? $plugin_data['options']['GatewayEDDLicense']['error'] : array();
433
434 }
435
436
437 /*
438 edd data (version , name , url, path etc)
439 */
440 $edd_data = $plugin_data['edd_data'];
441
442 /*
443 flag to decide if we should update options
444 for this plugin. ini as false
445 */
446 $do_option_update = false;
447
448 /*
449 init updater
450 CURRENTLY NOT IMPLEMENTED
451 */
452 #$this->edd_init_updater($license_data['key'], $edd_data);
453
454
455 /*
456 check_license and save details in options
457 if already recently verified, get those details
458 if no license key entered yet, set default
459 */
460 $current_license =array();
461
462 /*
463 only do any of this
464 if a license key was actually set
465 */
466 if($license_data['key'] !== false){
467
468 /*
469 skip license check if license key was checked in the last couple of hours or so
470 and was valid at the time
471 */
472 if($license_data['verified'] <= (time()-$this->edd_verification_interval) ){//&& $license_data['status']=='valid'
473
474 /*
475 check license
476 */
477 $args = array();
478 $args['action'] = 'check_license';
479 $args['license'] = $license_data['key'];
480 $args['edd']['name'] = $edd_data['name'];
481 $args['edd']['url'] = $edd_data['url'];
482 $edd_license = $this->edd_action($args);
483
484 /*
485 still valid
486 */
487 if($edd_license['status']=='valid'){
488
489 /*
490 set flag to
491 *update* current license status
492 */
493 $do_option_update = true;
494 /*
495 set updated license options
496 */
497 $current_license['key'] = $license_data['key'];// as set
498 $current_license['status'] = $edd_license['status'];
499 $current_license['expires'] = $edd_license['license_data']->expires;
500 $current_license['verified'] = time();//update timestamp
501 $current_license['error'] = $license_data['error'];// as set
502 }
503
504 /*
505 invalid
506 */
507 if($edd_license['status']!='valid'){
508
509 /*
510 set flag to
511 *update* license status
512 */
513 $do_option_update = true;
514 /*
515 set updated license options
516 */
517 $current_license['key'] = $license_data['key'];// as set
518 $current_license['status'] = $edd_license['status'];
519 $current_license['expires'] = !empty($edd_license['license_data']->expires) ? $edd_license['license_data']->expires : 0;
520 $current_license['verified'] = time();
521 $current_license['error'] = !empty($edd_license['error']) ? $edd_license['error'] : false ;
522
523 }
524 }
525 }
526
527
528 /*
529 no license key set yet
530 set flag to update option,
531 inserting status and error etc
532 */
533 if($license_data['key'] === false){
534
535 /*
536 set flag to *add* current license status
537 to plugin options array
538 */
539 $do_option_update = true;
540
541 /*
542 set license options
543 */
544 //$current_license['activate'] = false; // i dont think we need this
545 //$current_license['deactivate'] = false; //i dont think we need this
546 $current_license['key'] = false;
547 $current_license['verified'] = time();
548 $current_license['status'] = '';
549 $current_license['expires'] = false;
550 $current_license['error'] = false;
551 }
552
553 /*******************************************************
554 # [update option]
555 # if re-verified outside 2 hour window
556 # or if not ever verified previously
557 *******************************************************/
558 if($do_option_update){
559 $update_options = $plugin_data['options'];
560 /* extension licenses */
561 if(!$is_gateway_license){
562 $update_options['license'] = $current_license;
563 }
564 /* gateway licenses, setting appropriate keys as necessary */
565 if($is_gateway_license){
566 $update_options['GatewayEDDLicense'] = array();
567 $update_options['GatewayEDDLicense']['licence'] = $current_license['key'];
568 $update_options['GatewayEDDLicense']['status'] = $current_license['status'];
569 $update_options['GatewayEDDLicense']['expires'] = $current_license['expires'];
570 $update_options['GatewayEDDLicense']['timestamp'] = $current_license['verified'];
571 $update_options['GatewayEDDLicense']['error'] = $current_license['error'];
572 }
573
574 /*
575 make sure we reflect this possibly changed status
576 in the page body (edd_echo) too
577 */
578 $this->edd_plugin_updated[$plugin_slug] = $update_options;
579
580 /*
581 update/adding license status, errors etc etc
582 */
583 update_option($plugin_slug, $update_options);
584 }
585 }
586 }
587
588 /** plugins page - same as above, but let's keep it separate for now. might come in useful */
589 //if( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) && ( $current_screen->id=='plugins' || $current_screen->id=='plugins-network') ){
590 /**check for updates**/
591 //$this->wppizza_gateways_edd($this->wppizza_registered_gateways);
592 //}
593 }
594
595 /*******************************************************************************************************************************************************
596 *
597 * [echo license inputs]
598 *
599 ********************************************************************************************************************************************************/
600 function edd_echo($plugin_slug, $plugin_options){
601 /* cast to lowercase to match option names that are always stored lowercase*/
602 $lowercase_plugin_slug = strtolower($plugin_slug);
603
604 /*
605 when we have just checked the license on page load
606 the status might have changed so we immediately want to reflect this in the ouput here
607 as otherwise this would only be shown when reloading the page
608 */
609 if(!empty($this->edd_plugin_updated[$lowercase_plugin_slug])){
610 $plugin_options['options'] = $this->edd_plugin_updated[$lowercase_plugin_slug];
611 }
612
613 /*
614 for historical reasons, gateways use GatewayEDDLicense as key
615 , "licence" (with a c) instead of key
616 , "timestamp" instead of verified
617 */
618 $is_gateway_license = !empty($plugin_options['is_gateway']) ? true : false;
619
620
621 /*
622 get current license key etc , if any
623 */
624 /* non-gateways */
625 if(!$is_gateway_license){
626 /* license key*/
627 $license = !empty($plugin_options['options']['license']['key']) ? $plugin_options['options']['license']['key'] : '';
628 /* status*/
629 $status = !empty($plugin_options['options']['license']['status']) ? $plugin_options['options']['license']['status'] : '' ;
630 /* expires*/
631 $expires = !empty($plugin_options['options']['license']['expires']) ? $plugin_options['options']['license']['expires'] : '' ;
632 /* license last verified */
633 $verified = !empty($plugin_options['options']['license']['verified']) ? $plugin_options['options']['license']['verified'] : 0 ;
634 /* error messages */
635 $error = !empty($plugin_options['options']['license']['error']) ? $plugin_options['options']['license']['error'] : '' ;
636 }
637
638 /* gateways */
639 if($is_gateway_license){
640 /* license key*/
641 $license = !empty($plugin_options['options']['GatewayEDDLicense']['licence']) ? $plugin_options['options']['GatewayEDDLicense']['licence'] : '';
642 /* status*/
643 $status = !empty($plugin_options['options']['GatewayEDDLicense']['status']) ? $plugin_options['options']['GatewayEDDLicense']['status'] : '' ;
644 /* status*/
645 $expires = !empty($plugin_options['options']['GatewayEDDLicense']['expires']) ? $plugin_options['options']['GatewayEDDLicense']['expires'] : '' ;
646 /* license last verified */
647 $verified = !empty($plugin_options['options']['GatewayEDDLicense']['timestamp']) ? $plugin_options['options']['GatewayEDDLicense']['timestamp'] : 0 ;
648 /* error messages */
649 $error = !empty($plugin_options['options']['GatewayEDDLicense']['error']) ? $plugin_options['options']['GatewayEDDLicense']['error'] : '' ;
650 }
651
652 /*******************************
653 markup license key inputs
654 *******************************/
655 $markup='';
656
657 /*
658 wrapper. must be "form" element
659 for js serialization
660 */
661 $markup.="<div id='".WPPIZZA_SLUG."_license_".$plugin_slug."' class='".WPPIZZA_SLUG."_license'>";
662
663 /*
664 license key input
665 */
666 $markup.="<input id='".WPPIZZA_SLUG."_license_key_".$plugin_slug."' class='".WPPIZZA_SLUG."_license_key' name='".WPPIZZA_SLUG."_license_key' type='text' placeholder='".__('Enter your Licence Key', 'wppizza-admin')."' size='35' class='regular-text' value='".$license."' />";
667 $markup.=' '.__('Licence Key', 'wppizza-admin').'<br />';
668
669 /**
670 print activate or de-activate button
671 **/
672 $markup.="<div id='".WPPIZZA_SLUG."_license_info_".$plugin_slug."' class='".WPPIZZA_SLUG."_license_info' style='padding:5px;line-height:120%; margin:3px 0'>";
673
674 /*
675 set action and label
676 */
677 $action_class = ( $status =='' || $status != 'valid' ) ? ''.WPPIZZA_SLUG.'_license_activate' : ''.WPPIZZA_SLUG.'_license_deactivate';
678 $action_label = ( $status =='' || $status != 'valid' ) ? __('Activate Licence', 'wppizza-admin') : __('De-Activate Licence', 'wppizza-admin');
679 $show_remove = ( $status =='' || $status != 'valid' ) ? false : true;
680
681 /*
682 action buttons
683 */
684 $markup.="<label class='".WPPIZZA_SLUG."_license_action'>";
685 $markup.="<input class='button-secondary ".$action_class."' type='button' value='".$action_label."' />";
686 $markup.="<input name='".WPPIZZA_SLUG."_license_edd' type='hidden' value='".( empty($plugin_options['edd_data']['method']) ? '' : maybe_serialize($plugin_options['edd_data']['method']) )."' />";
687 $markup.="<input name='".WPPIZZA_SLUG."_license_current' type='hidden' value='".$license."' />";
688 $markup.="</label>";
689
690 /**
691 print status info
692 **/
693 $markup.="<span class='".WPPIZZA_SLUG."_license_status'>";
694 $markup.= $this->edd_status_markup($status, $error, $expires, $show_remove);
695 $markup.="</span>";
696
697 $markup.="</div>";
698
699
700 /*
701 add nonce
702 */
703 $markup .= ''.wp_nonce_field( '' . WPPIZZA_SLUG . '_license_nonce','' . WPPIZZA_SLUG . '_license_nonce',true,false).'';
704
705
706 $markup.='</div>';
707
708
709 /* print markup */
710 print $markup;
711
712 return;
713 }
714
715 /*******************************************************************************************************************************************************
716 *
717 * [license status output]
718 *
719 ********************************************************************************************************************************************************/
720 function edd_status_markup($status, $error, $expires, $show_remove){
721 $markup = '';
722
723 $expiry_time = !empty($expires) ? (strtotime($expires)+1) : false;//+1 to make it 0:00 next day instead of 23:59:59
724
725 if( $status =='' ) {/* not yet known or key deleted*/
726 $markup.='<span> '. __('Licence in-active', 'wppizza-admin').'';
727 }
728 if( $status !='' && empty($error['message']) && $status == 'valid' ) {/* valid and activated */
729 $markup.='<span style="color:green;"> '. __('Licence active', 'wppizza-admin').'</span>';
730 }
731 if( $status !='' && $status !='valid' && $status !='unknown') {/* inactive */
732 $markup.='<span style="color:red;"> '. __('Licence Status', 'wppizza-admin').': '.$status.'</span>';
733 }
734 if( $status !='' && $status !='valid' && $status =='unknown') {/* inactive */
735 $markup.='<span style="color:red;"> '. __('Licence Status unknown', 'wppizza-admin').'</span>';
736 }
737
738 if( !empty($error['message'])) {/* print error */
739 $evalue = !empty($error['value']) ? ' ['.$error['value'].']' : '' ;
740 $markup.='<span style="color:red;"> '. __('Error', 'wppizza-admin').': '.$error['message'].''.$evalue.'</span><br/>';
741 }
742
743 /* remove licence completely checkbox */
744 if(!empty($show_remove)){
745 $markup.="<br/><label style='font-size:80%'><input type='checkbox' name='".WPPIZZA_SLUG."_license_remove' value='1' /> ".__('Empty licence key field on de-activation', 'wppizza-admin')."</label>" ;
746 }
747
748 if(!empty($expiry_time)){
749 if($status == 'valid' && $expiry_time > time()){
750 /* Translators: 1: License expiry date (future) */
751 $markup.='<br/><span style="font-size:90%">'.sprintf(__('This licence key expires on %s', 'wppizza-admin'),date('l d F, Y', $expiry_time)).'</span>';
752 }
753 if(in_array($status, array('expired', 'invalid')) && $expiry_time <= time()){
754 /* Translators: 1: License expiry date (past) */
755 $markup.='<br/><span style="font-size:90%;color:red;">'.sprintf(__('This licence key expired on %s', 'wppizza-admin'),date('l d F, Y', $expiry_time)).'</span>';
756 }
757 }
758
759 return $markup ;
760 }
761 /*******************************************************************************************************************************************************
762 *
763 * [activate/deactivate etc]
764 *
765 ********************************************************************************************************************************************************/
766 function edd_action($edd_data){
767
768 /*
769 Call the edd API.
770 */
771 $api_params = array(
772 'edd_action'=> $edd_data['action'],
773 'license' => $edd_data['license'],
774 );
775 /*
776 add api parameters depending of whether we have an item id
777 */
778 if(!empty($edd_data['edd']['item_id'])){
779 $api_params['item_id'] = $edd_data['edd']['item_id'];
780 }else{
781 $api_params['item_name'] = urlencode( $edd_data['edd']['name'] );
782 }
783
784 $response = wp_remote_get( add_query_arg( $api_params, $edd_data['edd']['url'] ), array( 'timeout' => 15, 'sslverify' => false ) );
785
786
787
788 /*
789 ini array to add in this filter
790 */
791 $result = array();
792
793 // make sure the response came back okay
794 if ( is_wp_error( $response ) ){
795 /* error */
796 $result['error']=true;
797 }else{
798 // decode the license data
799 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
800
801 /* add results to return */
802 $result['success']=true;
803 $result['status'] = wppizza_validate_string($license_data->license);
804 $result['license_data'] = $license_data;
805 }
806
807
808 return $result;
809 }
810
811
812 /*******************************************************************************************************************************************************
813 *
814 * [admin ajax]
815 *
816 ********************************************************************************************************************************************************/
817 function admin_ajax($wppizza_options){
818
819 //need right caps for access
820 if( !current_user_can('wppizza_cap_'.$this->settings_page.'')){
821 $access_denied['access_denied']['status'] = ' <span class="wppizza-highlight">'.__('Access Denied', 'wppizza-admin').' ['.$this->tab_key.']</span>';
822 /*********
823 return to ajax request
824 *********/
825 print"".json_encode($access_denied)."";
826 exit();
827 }
828
829 /*****************************************************
830 [(de)activating license]
831 *****************************************************/
832 if($_POST['vars']['field']=='license_action'){
833
834 /*
835 get posted data and parse
836 */
837 $posted_data = array();
838 parse_str($_POST['vars']['data'], $posted_data);
839
840
841 /*
842 ini return object
843 */
844 $edd_data = array();
845
846 /*
847 get entered license key
848 */
849 $license_key = !empty($posted_data['' . WPPIZZA_SLUG . '_license_key']) ? sanitize_text_field($posted_data['' . WPPIZZA_SLUG . '_license_key']) : false;
850
851
852 /*
853 get current license key
854 (as it might differ from the one entered/changed now)
855
856 */
857 $license_key_current = !empty($posted_data['' . WPPIZZA_SLUG . '_license_current']) ? sanitize_text_field($posted_data['' . WPPIZZA_SLUG . '_license_current']) : false;
858
859 /*
860 [verify nonce]
861 */
862 if (!wp_verify_nonce( sanitize_text_field($posted_data['' . WPPIZZA_SLUG . '_license_nonce']) , '' . WPPIZZA_SLUG . '_license_nonce' ) ) {
863 /* invalid nonce */
864 $edd_data['error'] = __('Invalid Nonce', 'wppizza-admin');
865 print"".json_encode($edd_data)."";
866 exit();
867 }
868
869 /*
870 [verify that there is actually a license entered]
871 */
872 if (empty($license_key)){
873 /* no key entered */
874 $edd_data['error'] = __('No Licence Key entered', 'wppizza-admin');
875 print"".json_encode($edd_data)."";
876 exit();
877 }
878
879 /*
880 get all wppizza edd registered plugins
881 */
882 $edd_registered_plugins = $this->get_edd_registered_plugins();
883
884 /*
885 get the edd data and options
886 for this plugin
887 */
888 $get_class_method = explode('::',$posted_data['' . WPPIZZA_SLUG . '_license_edd']);
889 $class = $get_class_method[0];
890 $method = $get_class_method[1];
891 /* ini plugin class and get edd values */
892 $plugin = new $class();
893 $edd_init = $plugin->$method(array());
894
895
896 /* add flag to identify we are updating a gateway license */
897 $edd_init['is_gateway'] = !empty($edd_registered_plugins[$class]['is_gateway']) ? true : false ;
898
899
900 /*
901 option name , by key if non gateway
902 and traversing into this array to get the edd data
903 */
904 if(!$edd_init['is_gateway']){
905 $option_name = $edd_init[key($edd_init)]['edd_data']['option_name'];
906 $edd_init['edd_data'] = $edd_init[key($edd_init)]['edd_data'];
907 $edd_init['options'] = $edd_init[key($edd_init)]['options'];
908 }else{
909 $option_name = $edd_init['edd_data']['option_name'];
910 }
911
912
913 /*
914 [data]
915 */
916 $edd_data['license'] = $license_key;
917 $edd_data['action'] = (sanitize_text_field($_POST['vars']['action']) == 'activate') ? 'activate_license' : 'deactivate_license';
918
919
920 /*
921 if we are activating or de-activating a license
922 clear the WP transient that might otherwise still have
923 it stored as an update being available when de-activating
924 or not show the "view details" on the plugin screen yet if activating
925
926 */
927 if(in_array($edd_data['action'], array( 'deactivate_license', 'activate_license'))){
928 delete_option('_site_transient_update_plugins');
929 }
930
931
932 /*
933 [de_activate *OLD* license if any and different from entered]
934 */
935 if (!empty($license_key_current) && $license_key != $license_key_current){
936 $api_args = array();
937 $api_args['action'] = 'deactivate_license';
938 $api_args['license'] = $license_key_current;
939 $api_args['edd']['name'] = $edd_init['edd_data']['name'];
940 $api_args['edd']['url'] = $edd_init['edd_data']['url'];
941 $edd_data['api_results_deactivate_old'] = $this->edd_action($api_args);
942 }
943
944 /*
945 activate/deactivate new/entered license
946 */
947 $api_args = array();
948 $api_args['action'] = $edd_data['action'];
949 $api_args['license'] = $license_key;
950 //$api_args['edd']['name'] = $edd_init['edd_data']['name'];
951 //use id if defined
952 if(!empty($edd_init['edd_data']['item_id'])){
953 $api_args['edd']['item_id'] = $edd_init['edd_data']['item_id'];
954 }else{
955 $api_args['edd']['name'] = $edd_init['edd_data']['name'];
956 }
957 $api_args['edd']['url'] = $edd_init['edd_data']['url'];
958 $edd_data['api_results'] = $this->edd_action($api_args);
959
960 /*
961 update options if success
962 does not mean that the license is valid
963 only that the api call succeeded
964 */
965 if(!empty($edd_data['api_results']['success'])){
966 /*
967 get current options
968 */
969 $update_options = $edd_init['options'];
970
971 /*
972 any errors ?
973 */
974 $api_call_errors = !empty($edd_data['api_results']['license_data']->error) ? $edd_data['api_results']['license_data']->error : '';
975
976
977 /*
978 are we removing the license key entirely when de-activating ?
979 - provided we have no errors
980 */
981 $remove_license = false;
982 if($edd_data['action'] == 'deactivate_license' && !empty($posted_data['' . WPPIZZA_SLUG . '_license_remove']) && empty($api_call_errors)){
983 $remove_license = true;
984 }
985 /*
986 do we need to show the remove licence value checkbox (after activation)
987 */
988 $show_remove = ($edd_data['action'] == 'activate_license') ? true : false;
989
990
991 /*
992 is gateway
993 */
994 if(!empty($edd_init['is_gateway'])){
995 /* overwrite previous license options */
996 $update_options['GatewayEDDLicense'] = array();
997 $update_options['GatewayEDDLicense']['licence'] = ($remove_license) ? '' : $license_key ;
998 $update_options['GatewayEDDLicense']['status'] = ($remove_license) ? '' : $edd_data['api_results']['status'];
999 $update_options['GatewayEDDLicense']['expires'] = ($remove_license) ? 0 : $edd_data['api_results']['license_data']->expires;
1000 $update_options['GatewayEDDLicense']['timestamp'] = time();
1001 $update_options['GatewayEDDLicense']['error'] = $api_call_errors ;
1002
1003 /* update/adding license status, errors etc etc */
1004 update_option($option_name, $update_options);
1005 }else{
1006 /* overwrite previous license options */
1007 $update_options['license'] = array();
1008 $update_options['license']['key'] = ($remove_license) ? '' : $license_key ;
1009 $update_options['license']['status'] = ($remove_license) ? '' : $edd_data['api_results']['status'];
1010 $update_options['license']['expires'] = ($remove_license) ? 0 : $edd_data['api_results']['license_data']->expires;
1011 $update_options['license']['verified'] = time();
1012 $update_options['license']['error'] = $api_call_errors ;
1013
1014 /* update/adding license status, errors etc etc */
1015 update_option($option_name, $update_options);
1016 }
1017 }
1018
1019 /*
1020 [html elements - add/remove/change classes, labels, status]
1021 */
1022 $edd_data['html'] = array();
1023 $edd_data['html']['class_add'] = ($edd_data['action'] == 'activate_license') ? '' . WPPIZZA_SLUG . '_license_deactivate' : '' . WPPIZZA_SLUG . '_license_activate';
1024 $edd_data['html']['class_remove'] = ($edd_data['action'] == 'deactivate_license') ? '' . WPPIZZA_SLUG . '_license_deactivate' : '' . WPPIZZA_SLUG . '_license_activate';
1025 $edd_data['html']['label'] = ($edd_data['action'] == 'deactivate_license') ? __('Activate License', 'wppizza-admin') : __('De-Activate License', 'wppizza-admin');
1026 $edd_data['html']['status'] = $this->edd_status_markup($edd_data['api_results']['status'], $api_call_errors, $edd_data['api_results']['license_data']->expires, $show_remove);
1027 $edd_data['html']['update_success'] = (!empty($edd_data['api_results']['success']) && ($edd_data['api_results']['license_data']->success) ) ? true : false;//check if api call itself and license update succeeded , else do not change classes or lables
1028 $edd_data['html']['no_license_value'] = ($remove_license) ? true : false;
1029
1030
1031 /*********
1032 return to ajax request
1033 *********/
1034 print"".json_encode($edd_data)."";
1035 exit();
1036 }
1037 }
1038
1039 /*********************************************************
1040 *
1041 * [enqueue js if necessary]
1042 *
1043 *********************************************************/
1044 public function wppizza_add_helpers($current_screen){
1045 if( !empty($this -> has_registered_edd_plugins) && $current_screen->id == ''.WPPIZZA_POST_TYPE.'_page_'.$this->settings_page.'' && $current_screen->post_type == WPPIZZA_POST_TYPE && !empty($_GET['tab']) && 'licenses' == $_GET['tab']){
1046 /***enqueue scripts and styles***/
1047 add_action('admin_enqueue_scripts', array( $this, 'wppizza_enqueue_admin_scripts_and_styles'));
1048 }
1049 }
1050 /*********************************************************
1051 *
1052 * [add js]
1053 *
1054 *********************************************************/
1055 public function wppizza_enqueue_admin_scripts_and_styles($hook) {
1056 wp_register_script(WPPIZZA_SLUG.'_'.$this->settings_page.'_'.$this->tab_key, plugins_url( 'js/scripts.admin.'.$this->settings_page.'.'.$this->tab_key.'.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true);
1057 wp_enqueue_script(WPPIZZA_SLUG.'_'.$this->settings_page.'_'.$this->tab_key);
1058 }
1059
1060 /*------------------------------------------------------------------------------
1061 #
1062 #
1063 # [settings page]
1064 #
1065 #
1066 ------------------------------------------------------------------------------*/
1067 /*********************************************************
1068 [alter sidebar link label]
1069 *********************************************************/
1070 function admin_link_label($label){
1071 if(!empty($this -> has_registered_edd_plugins)){
1072 $label .=' / '.__('Licences','wppizza-admin').'';
1073 }
1074 return $label;
1075 }
1076 /*********************************************************
1077 [add section to a particular tab]
1078 *********************************************************/
1079 function admin_tabs($tabs){
1080 /**
1081 edd license tabs - since 3.3.6
1082 **/
1083 if(!empty($this -> has_registered_edd_plugins)){
1084 /* main tab */
1085 $tabs['tab'][$this->tab_key]=array('lbl'=>__('Licences','wppizza-admin'), 'slug'=>'licenses', 'sections'=>array(), 'save_options' => false);
1086 /* sections in tab */
1087 $tabs['tab'][$this->tab_key]['sections'][] = $this->section_key;
1088
1089 }
1090
1091 return $tabs;
1092 }
1093
1094
1095 /*------------------------------------------------------------------------------
1096 # [section header to output some more info]
1097 # @since 3.0
1098 # @return array()
1099 ------------------------------------------------------------------------------*/
1100 function sections_header($settings, $sections){
1101 if($settings['id']==$this->section_key){
1102 echo''.__('Entering and activating the licence is optional, but if you choose not to do so, you will not be informed of any future bugfixes and/or updates.', 'wppizza-admin').'<br />';
1103 echo''.__('Changing an existing key will automatically de-activate the old key for this site when activating your new key.', 'wppizza-admin').'<br />';
1104 echo'<b>'.__('To ensure backwards compatibility licence key settings might additionally also still be available for plugins/gateways in their dedicated licence settings page and can interchangeably be set there.', 'wppizza-admin').'</b><br />';
1105 echo'<span class="wppizza-highlight">'.__('Note: you will only receive update notifications if these have not been disabled globally (perhaps by using another plugin that manages these kind of notifications)', 'wppizza-admin').'</span>';
1106 }
1107 return;
1108 }
1109
1110 /*------------------------------------------------------------------------------
1111 # [settings section - setting page]
1112 # @since 3.0
1113 # @return array()
1114 ------------------------------------------------------------------------------*/
1115 function admin_options_settings($settings, $sections, $fields, $inputs, $help){
1116
1117 /*section*/
1118 if($sections){
1119 $settings['sections'][$this->section_key] = __('Licences', 'wppizza-admin');
1120 }
1121
1122 /*help*/
1123 if($help){
1124 }
1125
1126 /*fields*/
1127 if($fields){
1128 /*
1129 licences
1130 */
1131 $defined_licenses = $this->get_edd_registered_plugins();
1132 foreach($defined_licenses as $pluginKey => $licenseData){
1133 $field = $pluginKey.'_license';
1134 $settings['fields'][$this->section_key][$field] = array($licenseData['edd_data']['name'] , array(
1135 'value_key'=>$field,
1136 'option_key'=>$this->settings_page,
1137 'label'=>'',
1138 'description'=>array()
1139 ));
1140 }
1141 }
1142
1143 return $settings;
1144 }
1145 /*------------------------------------------------------------------------------
1146 # [output option fields - setting page]
1147 # @since 3.0
1148 # @return array()
1149 ------------------------------------------------------------------------------*/
1150 function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){
1151
1152 /*
1153 print row for each license
1154 */
1155 $defined_licenses = $this->get_edd_registered_plugins();
1156 foreach($defined_licenses as $pluginKey => $licenseData){
1157 if($field == $pluginKey.'_license'){
1158 do_action('wppizza_edd_init', $pluginKey, $licenseData);
1159 }
1160 }
1161 }
1162 }
1163 /***************************************************************
1164 *
1165 * [ini]
1166 *
1167 ***************************************************************/
1168 $WPPIZZA_MODULE_TOOLS_LICENCES_INIT = new WPPIZZA_MODULE_TOOLS_LICENCES_INIT();
1169 ?>