WSOE_License_Verify_Notify

Verifies user's license and if it is expired, notify them using admin notice.

Defined (1)

The class is defined in the following location(s).

/classes/class-license-verify-notify.php  
  1. class WSOE_License_Verify_Notify { 
  2.  
  3. /** 
  4. * @var string Download slug. 
  5. */ 
  6. public $slug; 
  7.  
  8. /** 
  9. * @var string Endpoint to request to. 
  10. */ 
  11. public $endpoint; 
  12.  
  13. /** 
  14. * Option name to set/get for corresponding extension. 
  15. */ 
  16. public $option_to_set; 
  17.  
  18. /** 
  19. * File ID 
  20. */ 
  21. public $file_id; 
  22.  
  23. /** 
  24. * Hook name for license check 
  25. */ 
  26. public $hook_name; 
  27.  
  28. /** 
  29. * To track some local/conditional checks. 
  30. */ 
  31. static $tracker = array(); 
  32.  
  33. /** 
  34. * Constructor - Adds necessary hooks. 
  35. * Also assigns enpoints and slug for download. 
  36. */ 
  37. function __construct( $options ) { 
  38.  
  39. $this->endpoint = $options['endpoint']; 
  40. $this->slug = $options['slug']; 
  41. $this->option_to_set = $options['option_name']; 
  42. $this->hook_name = (string)$options['hook']; 
  43. $this->file_id = (int)$options['file_id']; 
  44.  
  45. add_action( $this->hook_name, array($this, 'check_license') ); 
  46. add_action( 'woocommerce_settings_saved', array($this, 'schedule_license_check_event'), 9 ); 
  47. add_action( 'admin_notices', array($this, 'license_notice') ); 
  48. add_action( 'admin_footer', array($this, 'dismiss_notice_event'), 99 ); 
  49. add_action( 'wp_ajax_wsoe_addon_expired_dismiss', array($this, 'wsoe_addon_expired_dismiss') ); 
  50. add_action( 'wp_ajax_wsoe_scheduler_expired_dismiss', array($this, 'wsoe_scheduler_expired_dismiss') ); 
  51.  
  52. function check_license( $email ) { 
  53.  
  54. if( !empty($email) ) { 
  55.  
  56. // Get the remote info 
  57. $ping_url = trailingslashit($this->endpoint) . "?wsoe_license_check=true"; 
  58. $ping_url .= "&email=" . urlencode( $email ); 
  59. $ping_url .= "&slug=" . urlencode( $this->slug ); 
  60.  
  61. $request = wp_remote_post( $ping_url ); 
  62.  
  63. if( (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) && is_serialized($request['body']) ) { 
  64.  
  65. $info = @unserialize($request['body']); 
  66.  
  67. if( $info->exists && $info->expired ) { 
  68. update_option( $this->option_to_set, $info, false ); 
  69.  
  70. /** 
  71. * Show license related notice. 
  72. */ 
  73. function license_notice() { 
  74.  
  75. $is_expired = get_option( $this->option_to_set, false ); 
  76.  
  77. if( $is_expired ) { 
  78.  
  79. ob_start(); 
  80.  
  81. if( $this->file_id == 1 ) { 
  82. include trailingslashit(WSOE_BASE) .'views/html-notice-addon-license.php'; 
  83. }else{ 
  84. include trailingslashit(WSOE_BASE) .'views/html-notice-scheduler-license.php'; 
  85.  
  86. echo ob_get_clean(); 
  87.  
  88. /** 
  89. * If updater email is entered, schedule event. 
  90. */ 
  91. function schedule_license_check_event() { 
  92.  
  93. $prev_email = $this->get_updater_email( true, false ); 
  94. $new_email = $this->get_updater_email( false, true ); 
  95. $cron_event = wp_next_scheduled( $this->hook_name, array('email'=>$prev_email)); 
  96.  
  97. $create_cron = ( ( empty($cron_event) || strcasecmp($prev_email, $new_email) != 0 ) && !empty($new_email) ) ? true : false; 
  98.  
  99. if( $create_cron ) { 
  100.  
  101. //Unschedule the event first, then only create new one. 
  102. wp_clear_scheduled_hook( $this->hook_name, array('email'=>$prev_email) ); 
  103.  
  104. //Schedule next event after 1 minute the settings are saved. 
  105. wp_schedule_event( strtotime("now"), 'daily', $this->hook_name, array('email'=>$new_email) ); 
  106.  
  107. /** 
  108. * Get updater email for addon/scheduler. 
  109. *  
  110. * @param bool $prev Fetches old updater email. 
  111. * @param bool $new Fetches newly updater email. 
  112. */ 
  113. private function get_updater_email( $prev = true, $new = false ) { 
  114.  
  115. //If old email is to be retrieved. 
  116. if( $prev ) { 
  117.  
  118. $email = ''; 
  119.  
  120. if( $this->file_id == 1 ) { 
  121. $email = get_option( 'edd-updater-email', '' ); 
  122. }else{ 
  123. $scheduler_settings = get_option( 'wsoe_scheduler_options', '' ); 
  124. $email = ( is_array($scheduler_settings) && array_key_exists('export_update_email', $scheduler_settings) ) ? $scheduler_settings['export_update_email'] : ''; 
  125.  
  126. return $email; 
  127.  
  128. // If newly updated email is to be retrieved. 
  129. if( $new ) { 
  130. return ($this->file_id == 1) ? ( empty($_POST['edd_updater_email']) ? '' : $_POST['edd_updater_email'] ) : ( empty($_POST['wsoe_scheduler_update_email']) ? '' : $_POST['wsoe_scheduler_update_email'] ); 
  131. }  
  132.  
  133. /** 
  134. * Dismisses the add-on related license notice. 
  135. */ 
  136. function wsoe_addon_expired_dismiss() { 
  137. delete_option( 'wsoe-addon-expired' ); 
  138.  
  139. /** 
  140. * Dismisses the scheduler related license notice. 
  141. */ 
  142. function wsoe_scheduler_expired_dismiss() { 
  143. delete_option( 'wsoe-scheduler-expired' ); 
  144.  
  145. function dismiss_notice_event() { 
  146.  
  147. if( empty( self::$tracker['ran_dismiss_notice_event'] ) ) {?> 
  148. <script type="text/javascript"> 
  149. /** 
  150. * Dismiss license related notice. 
  151. */ 
  152. jQuery('body').on( 'click', '.wsoe-addon-expired .notice-dismiss, .wsoe-scheduler-expired .notice-dismiss', function() { 
  153.  
  154. var action = jQuery(this).parent().hasClass('wsoe-addon-expired') ? 'wsoe_addon_expired_dismiss' : 'wsoe_scheduler_expired_dismiss'; 
  155.  
  156. console.log(action); 
  157. jQuery.ajax({ 
  158. url: ajaxurl,  
  159. data: { 
  160. action: action 
  161. },  
  162. success:function(response) { 
  163. //console.log(response); 
  164. }) 
  165.  
  166. }); 
  167. </script><?php 
  168. self::$tracker['ran_dismiss_notice_event'] = true;