/**
  * Deactivate a licence from the my account page
  */
 public function deactivate_licence()
 {
     global $wpdb;
     if (is_user_logged_in() && !empty($_GET['deactivate_licence'])) {
         $activation_id = sanitize_text_field($_GET['deactivate_licence']);
         $licence_key = sanitize_text_field($_GET['licence_key']);
         $activation_email = sanitize_text_field($_GET['activation_email']);
         $licence = wppl_get_licence_from_key($licence_key);
         // Validation
         if (!$licence) {
             wp_die(__('Invalid or expired licence key.', 'wp-plugin-licencing'));
         }
         if ($licence->user_id && $licence->user_id != get_current_user_id()) {
             wp_die(__('This licence does not appear to be yours.', 'wp-plugin-licencing'));
         }
         if (!is_email($activation_email) || $activation_email != $licence->activation_email) {
             wp_die(__('Invalid activation email address.', 'wp-plugin-licencing'));
         }
         if ($wpdb->update("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 0), array('activation_id' => $activation_id, 'licence_key' => $licence_key))) {
             wc_add_notice(__('Licence successfully deactivated.'), 'success');
         } else {
             wc_add_notice(__('The licence could not be deactivated.'), 'error');
         }
     }
 }
 /**
  * Check if we need to download a file and check validity
  */
 public function download_api_product()
 {
     global $wpdb;
     if (isset($_GET['download_api_product']) && isset($_GET['licence_key'])) {
         $download_api_product = absint($_GET['download_api_product']);
         $licence_key = sanitize_text_field($_GET['licence_key']);
         $activation_email = sanitize_text_field($_GET['activation_email']);
         $licence = wppl_get_licence_from_key($licence_key);
         // Validation
         if (!$licence) {
             wp_die(__('Invalid or expired licence key.', 'wp-plugin-licencing'));
         }
         if (is_user_logged_in() && $licence->user_id && $licence->user_id != get_current_user_id()) {
             wp_die(__('This licence does not appear to be yours.', 'wp-plugin-licencing'));
         }
         if (!is_email($activation_email) || $activation_email != $licence->activation_email) {
             wp_die(__('Invalid activation email address.', 'wp-plugin-licencing'));
         }
         if (!in_array($download_api_product, wppl_get_licence_api_product_permissions($licence->product_id))) {
             wp_die(__('This licence does not allow access to the requested product.', 'wp-plugin-licencing'));
         }
         // Get the download URL
         $file_path = wppl_get_package_file_path($download_api_product);
         // Log this download
         $wpdb->insert($wpdb->prefix . 'wp_plugin_licencing_download_log', array('licence_key' => $licence_key, 'activation_email' => $activation_email, 'api_product_id' => $download_api_product, 'date_downloaded' => current_time('mysql'), 'user_ip_address' => sanitize_text_field(isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'])));
         // Download it!
         $this->download($file_path);
     }
 }
 /**
  * Plugin update check
  */
 public function plugin_update_check()
 {
     $this->check_access();
     $licence = wppl_get_licence_from_key($this->request['licence_key']);
     $api_product_post_id = wppl_get_api_product_post_id($this->request['api_product_id']);
     $data = new stdClass();
     $data->plugin = $this->request['plugin_name'];
     $data->slug = $this->request['api_product_id'];
     $data->new_version = get_post_meta($api_product_post_id, '_version', true);
     $data->url = get_post_meta($api_product_post_id, '_plugin_uri', true);
     $data->package = wppl_get_package_download_url($api_product_post_id, $this->request['licence_key'], $this->request['email']);
     $this->send_response($data);
 }
 /**
  * Activate a licence key
  */
 public function activate()
 {
     global $wpdb;
     $this->check_required(array('email', 'licence_key', 'api_product_id', 'instance'));
     $licence = wppl_get_licence_from_key($this->request['licence_key']);
     $api_product_post_id = wppl_get_api_product_post_id($this->request['api_product_id']);
     if (!$licence) {
         $this->trigger_error('101', __('Activation error: The provided licence is invalid or has expired.', 'wp-plugin-licencing'));
     }
     if (!$api_product_post_id) {
         $this->trigger_error('102', __('Activation error: Invalid API Product ID.', 'wp-plugin-licencing'));
     }
     if (!is_email($this->request['email']) || strtolower($this->request['email']) != strtolower($licence->activation_email)) {
         $this->trigger_error('103', sprintf(__('Activation error: The email provided (%s) is invalid.', 'wp-plugin-licencing'), $this->request['email']));
     }
     if (!in_array($api_product_post_id, wppl_get_licence_api_product_permissions($licence->product_id))) {
         $this->trigger_error('104', __('Activation error: Licence is not for this product.', 'wp-plugin-licencing'));
     }
     $active_instances = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id'], 1);
     // Check if activation limit is reached
     if ($licence->activation_limit && sizeof($active_instances) >= $licence->activation_limit) {
         // lets allow reactvation for guests, but registered users need to de-activate first
         if (!$licence->user_id) {
             foreach ($active_instances as $activation) {
                 if ($activation->instance == $this->request['instance']) {
                     // Reactivate the key
                     $activation_result = $wpdb->update("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql')), array('instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key']));
                     if (!$activation_result) {
                         $this->trigger_error('106', __('Activation error: Could not reactivate licence key.', 'wp-plugin-licencing'));
                     } else {
                         $response = array('activated' => true);
                         $activations_remaining = absint($licence->activation_limit - sizeof($active_instances));
                         $response['remaining'] = sprintf(__('%s out of %s activations remaining', 'wp-plugin-licencing'), $activations_remaining, $licence->activation_limit);
                         $this->send_response($response);
                     }
                 }
             }
         }
         $this->trigger_error('105', __('Activation error: Licence key activation limit reached. Deactivate an install first.', 'wp-plugin-licencing'));
     }
     $instance_exists = false;
     $instances = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id']);
     // Check for reactivation
     if ($instances) {
         foreach ($instances as $activation) {
             if ($activation->instance == $this->request['instance']) {
                 $instance_exists = true;
             }
         }
     }
     if ($instance_exists) {
         $activation_result = $wpdb->update("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql')), array('instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key']));
     } else {
         $activation_result = $wpdb->insert("{$wpdb->prefix}wp_plugin_licencing_activations", array('activation_active' => 1, 'activation_date' => current_time('mysql'), 'instance' => $this->request['instance'], 'api_product_id' => $this->request['api_product_id'], 'licence_key' => $this->request['licence_key']));
     }
     if (!$activation_result) {
         $this->trigger_error('107', __('Activation error: Could not activate licence key.', 'wp-plugin-licencing'));
     }
     $activations = wppl_get_licence_activations($this->request['licence_key'], $this->request['api_product_id']);
     $response = array('activated' => true);
     if ($licence->activation_limit) {
         $activations_remaining = absint($licence->activation_limit - sizeof($activations));
         $response['remaining'] = sprintf(__('%s out of %s activations remaining', 'wp-plugin-licencing'), $activations_remaining, $licence->activation_limit);
     }
     $this->send_response($response);
 }
 /**
  * Save the new key
  */
 public function save()
 {
     $licence_key = wc_clean($_POST['licence_key']);
     $activation_email = wc_clean($_POST['activation_email']);
     $product_id = absint($_POST['product_id']);
     $user_id = absint($_POST['user_id']);
     try {
         // Validate
         if (empty($licence_key)) {
             throw new Exception(__('Licence key is a required field', 'wp-plugin-licencing'));
         }
         if (empty($product_id)) {
             throw new Exception(__('You must choose a product for this licence', 'wp-plugin-licencing'));
         }
         if (empty($activation_email) && empty($user_id)) {
             throw new Exception(__('Either an activation email or user ID is required', 'wp-plugin-licencing'));
         }
         $product = get_product($product_id);
         if ('yes' !== get_post_meta($product->id, '_is_api_product_licence', true)) {
             throw new Exception(__('Invalid product', 'wp-plugin-licencing'));
         }
         if (!$activation_email && $user_id) {
             $user = get_user_by('id', $user_id);
             $activation_email = $user->user_email;
         }
         if (empty($activation_email) || !is_email($activation_email)) {
             throw new Exception(__('A valid activation email is required', 'wp-plugin-licencing'));
         }
         if (!$product->variation_id || !($activation_limit = get_post_meta($product->variation_id, '_licence_activation_limit', true))) {
             $activation_limit = get_post_meta($product->id, '_licence_activation_limit', true);
         }
         if (!$product->variation_id || !($licence_expiry_days = get_post_meta($product->variation_id, '_licence_expiry_days', true))) {
             $licence_expiry_days = get_post_meta($product->id, '_licence_expiry_days', true);
         }
         $data = array('order_id' => 0, 'licence_key' => $licence_key, 'activation_email' => $activation_email, 'user_id' => $user_id, 'product_id' => $product->variation_id ? $product->variation_id : $product->id, 'activation_limit' => $activation_limit, 'date_expires' => !empty($licence_expiry_days) ? date("Y-m-d H:i:s", strtotime("+{$licence_expiry_days} days", current_time('timestamp'))) : '');
         if (WP_Plugin_Licencing_Orders::save_licence_key($data)) {
             ob_start();
             // Try to get a user name
             if (!empty($user) && !empty($user->first_name)) {
                 $user_first_name = $user->first_name;
             } else {
                 $user_first_name = false;
             }
             wc_get_template('new-licence-email.php', array('key' => wppl_get_licence_from_key($licence_key), 'user_first_name' => $user_first_name), 'wp-plugin-licencing', WP_PLUGIN_LICENCING_PLUGIN_DIR . '/templates/');
             // Get contents
             $message = ob_get_clean();
             wp_mail($activation_email, __('Your licence keys for "WP Job Manager"', 'wp-plugin-licencing'), $message);
             $admin_message = sprintf(__('Licence has been emailed to %s.', 'wp-plugin-licencing'), $activation_email);
             echo sprintf('<div class="updated"><p>%s</p></div>', $admin_message);
         } else {
             throw new Exception(__('Could not create the licence', 'wp-plugin-licencing'));
         }
     } catch (Exception $e) {
         echo sprintf('<div class="error"><p>%s</p></div>', $e->getMessage());
     }
 }