/** * Shortcode view * * @return string */ private function view() { ob_start(); // load template file via WooCommerce template function wc_get_template('lost-license-form.php', array(), 'license-wp', license_wp()->service('file')->plugin_path() . '/templates/'); return ob_get_clean(); }
/** * Persist license data in WordPress database * * @param License $license * * @return License */ public function persist($license) { global $wpdb; // dem defaults $defaults = array('order_id' => '', 'activation_email' => '', 'user_id' => '', 'license_key' => '', 'product_id' => '', 'activation_limit' => '', 'date_expires' => '', 'date_created' => ''); // get date expiration $date_expires = $license->get_date_expires(); // set correct DateTime for non expiring licenses if (!$date_expires) { $date_expires = new \DateTime('0000-00-00'); $date_expires->setTime(0, 0, 0); } // setup array with data $data = wp_parse_args(array('license_key' => $license->get_key(), 'order_id' => $license->get_order_id(), 'user_id' => $license->get_user_id(), 'activation_email' => $license->get_activation_email(), 'product_id' => $license->get_product_id(), 'activation_limit' => $license->get_activation_limit(), 'date_created' => $license->get_date_created()->format('Y-m-d'), 'date_expires' => $date_expires->format('Y-m-d')), $defaults); // check if new license or existing if ('' == $license->get_key()) { // insert // generate new license $license->set_key(license_wp()->service('license_manager')->generate_license_key()); // set key in data $data['license_key'] = $license->get_key(); // insert into WordPress database $wpdb->insert($wpdb->lwp_licenses, $data); } else { // update // unset license from data unset($data['license_key']); // update database $wpdb->update($wpdb->lwp_licenses, $data, array('license_key' => $license->get_key())); } return $license; }
/** * Deactivate an activation * * @param Activation $activation * * @return bool */ public function deactivate($activation) { // set active to 0 $activation->set_activation_active(0); // persists activation $activation = license_wp()->service('activation_repository')->persist($activation); // return true of active is set to 0 return $activation->get_activation_active() == 0; }
/** * Enqueue backend(admin) assets */ public static function enqueue_backend() { global $pagenow, $post; // only load WP media assets on correct page if ($pagenow == 'post.php' && isset($post) && ApiProduct\PostType::KEY === $post->post_type || $pagenow == 'post-new.php' && isset($_GET['post_type']) && ApiProduct\PostType::KEY == $_GET['post_type']) { wp_enqueue_media(); } // admin CSS wp_enqueue_style('license_wp_style', license_wp()->service('file')->plugin_url('/assets/css/admin.css'), array(), license_wp()->get_version()); }
/** * @return string */ public function get_content() { // load template file into content if still empty if (empty($this->content)) { ob_start(); wc_get_template($this->template, $this->args, 'license-wp', license_wp()->service('file')->plugin_path() . '/templates/'); $this->content = ob_get_clean(); } return $this->content; }
/** * Trigger download * * @param int $product_id * @param string $license_key * @param string $activation_email */ private function trigger($product_id, $license_key, $activation_email) { // clean vars $product_id = absint($product_id); $license_key = sanitize_text_field($license_key); $activation_email = sanitize_text_field($activation_email); // get license /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make($license_key); // check if license exists if ('' == $license->get_key()) { wp_die(__('Invalid license key.', 'license-wp')); } // check if license expired if ($license->is_expired()) { wp_die(sprintf(__('License has expires. You can renew it here: %s', 'license-wp'), $license->get_renewal_url())); } // check if this license is owned by logged in user if (is_user_logged_in() && $license->get_user_id() != get_current_user_id()) { wp_die(__('This license does not appear to be yours.', 'license-wp')); } // check if activation email is correct if (!is_email($activation_email) || $activation_email != $license->get_activation_email()) { wp_die(__('Invalid activation email address.', 'license-wp')); } // get api products linked to license $api_products = $license->get_api_products(); // store api product ids in array $api_products_ids = array(); if (count($api_products) > 0) { foreach ($api_products as $api_product) { $api_products_ids[] = $api_product->get_id(); } } // check if license grants access to request api product if (!in_array($product_id, $api_products_ids)) { wp_die(__('This license does not allow access to the requested product.', 'license-wp')); } // get actual API product /** @var \Never5\LicenseWP\ApiProduct\ApiProduct $api_product */ $api_product = license_wp()->service('api_product_factory')->make($product_id); // check if there's a package defined if ($api_product->get_package() == '') { wp_die(__('Download package is missing.', 'license-wp')); } // log request before we start download license_wp()->service('log')->insert($product_id, $license_key, $activation_email); // download file $this->download($api_product->get_package()); }
/** * Add license keys to WooCommerce emails * * @param $order */ public function add_keys($order) { global $wpdb; // check and get order if (!is_object($order)) { $order = new \WC_Order($order); } // fetch license keys $licenses = license_wp()->service('license_manager')->get_licenses_by_order($order->id); // check if we've found license keys if (is_array($licenses) && count($licenses) > 0) { // load our template file wc_get_template('email-keys.php', array('licenses' => $licenses), 'license-wp', license_wp()->service('file')->plugin_path() . '/templates/'); } }
/** * Catch the deactivate activation request from My Account page */ public function catch_deactivate_activation() { if (is_user_logged_in() && isset($_GET['deactivate_license']) && isset($_GET['license_key']) && isset($_GET['activation_email'])) { // clean vars $activation_id = absint($_GET['deactivate_license']); $license_key = sanitize_text_field($_GET['license_key']); $activation_email = sanitize_text_field($_GET['activation_email']); // get license /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make($license_key); // check if license exists if ('' == $license->get_key()) { wp_die(__('Invalid or expired license key.', 'license-wp')); } // check if license expired if ($license->is_expired()) { wp_die(sprintf(__('License has expires. You can renew it here: %s', 'license-wp'), $license->get_renewal_url())); } // check if this license is owned by logged in user if (is_user_logged_in() && $license->get_user_id() != get_current_user_id()) { wp_die(__('This license does not appear to be yours.', 'license-wp')); } // check if activation email is correct if (!is_email($activation_email) || $activation_email != $license->get_activation_email()) { wp_die(__('Invalid activation email address.', 'license-wp')); } // get activation /** @var \Never5\LicenseWP\Activation\Activation $activation */ $activation = license_wp()->service('activation_factory')->make($activation_id); // check if lincense key in activation equals given license key if ($activation->get_license_key() != $license_key) { wp_die(__('This appears to be not your activation.', 'license-wp')); } // deactivate activation if (license_wp()->service('activation_manager')->deactivate($activation)) { wc_add_notice(__('License successfully deactivated.', 'license-wp'), 'success'); } } }
/** * Add license renewal to cart * * @param string $license_key * @param string $activation_email */ private function add_renewal_to_cart($license_key, $activation_email) { // sanitize license key & activation email $license_key = sanitize_text_field($license_key); $activation_email = sanitize_text_field($activation_email); // get license /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make($license_key); // check if license exists if ('' == $license->get_key()) { wc_add_notice(__('Invalid license key.', 'license-wp')); } // check if this license is owned by logged in user if (is_user_logged_in() && $license->get_user_id() != get_current_user_id()) { wc_add_notice(__('This license does not appear to be yours.', 'license-wp')); } // check if activation email is correct if (!is_email($activation_email) || $activation_email != $license->get_activation_email()) { wc_add_notice(__('Invalid activation email address.', 'license-wp')); } // get WooCommerce product $product = wc_get_product($license->get_product_id()); // check if product is purchasable if (!$product->is_purchasable()) { wc_add_notice(__('This product can no longer be purchased', 'license-wp'), 'error'); return; } // Add to cart WC()->cart->empty_cart(); WC()->cart->add_to_cart($license->get_product_id(), 1, '', '', array('renewing_key' => $license->get_key())); // Message wc_add_notice(sprintf(__('The product has been added to your cart with a %d%% discount.', 'license-wp'), 30), 'success'); // @todo this should become an option // Redirect to checkout wp_redirect(get_permalink(wc_get_page_id('checkout'))); // bye exit; }
/** * Save the new license */ public function save() { // vars $license_key = wc_clean($_GET['edit']); $activation_email = wc_clean($_POST['activation_email']); $product_id = absint($_POST['product_id']); $user_id = absint($_POST['user_id']); $order_id = absint($_POST['order_id']); $activation_limit = absint($_POST['activation_limit']); $date_expires = wc_clean($_POST['date_expires']); try { // check nonce if (empty($_POST['license_wp_licensing_nonce']) || !wp_verify_nonce($_POST['license_wp_licensing_nonce'], 'edit_license')) { throw new \Exception(__('Nonce check failed', 'license-wp')); } // check product ID if (empty($product_id)) { throw new \Exception(__('You must choose a product for this license', 'license-wp')); } // check activation email if (empty($activation_email) && empty($user_id)) { throw new \Exception(__('Either an activation email or user ID is required', 'license-wp')); } // get WooCommerce product $product = \wc_get_product($product_id); // product must be an API license product if ('yes' !== get_post_meta($product->id, '_is_api_product_license', true)) { throw new \Exception(__('Invalid product', 'license-wp')); } // set activation email to user email if no activation email is set if (!$activation_email && $user_id) { $user = get_user_by('id', $user_id); $activation_email = $user->user_email; } // exit if we still have no valid email address at this point if (empty($activation_email) || !is_email($activation_email)) { throw new \Exception(__('A valid activation email is required', 'license-wp')); } // create license object /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make($license_key); // set license data, key is generated when persisting license $license->set_activation_email($activation_email); $license->set_user_id($user_id); $license->set_product_id($product_id); $license->set_activation_limit($activation_limit); $license->set_order_id($order_id); if ($date_expires) { $exp_date = new \DateTime($date_expires); $license->set_date_expires($exp_date); } else { $license->set_date_expires(false); } // save license $license = license_wp()->service('license_repository')->persist($license); } catch (\Exception $e) { echo sprintf('<div class="error"><p>%s</p></div>', $e->getMessage()); } }
/** * Send all expiration emails */ public function send_expiration_emails() { // load emails $emails = apply_filters('license_wp_renewal_emails', array()); // check if we have at least 1 email if (is_array($emails) && count($emails) > 0) { // loop through emails foreach ($emails as $email_data) { // create \DateTime of today $date = new \DateTime(); $date->setTime(0, 0, 0); // try to modify object with $email_data['date_modify'] if (false !== $date->modify($email_data['date_modify'])) { // get licenses that expire on modified date object $licenses = $this->get_licenses_that_expire_on($date); // check if there are licenses if (count($licenses) > 0) { /** @var License $license */ foreach ($licenses as $license) { // prep body, replace vars for correct content $body = $this->replace_expiration_vars($email_data['body'], $license); // setup email object $email = new Email\ExpiringLicense($email_data['subject'], $body, $license); // send email license_wp()->service('email_manager')->send($email, $license->get_activation_email()); } } } } } }
/** * Handle API request */ public function handle() { global $wpdb; // hide DB errors $wpdb->hide_errors(); // send no-cache header nocache_headers(); // set request $request = array_map('sanitize_text_field', apply_filters('license_wp_api_update_request', $_GET)); // check for required things try { // check for request var if (!isset($request['request']) || empty($request['request'])) { throw new UpdateException(__('Update error: No API Request set.', 'license-wp'), 'invalid_request'); } // check for license var if (!isset($request['license_key']) || empty($request['license_key'])) { throw new UpdateException(__('Update error: No license key set.', 'license-wp'), 'no_key'); } // check for api product ID var if (!isset($request['api_product_id']) || empty($request['api_product_id'])) { throw new UpdateException(__('Update error: No API Product ID set.', 'license-wp'), 'no_api_product_id'); } // get license /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make($request['license_key']); // check if license exists if ('' == $license->get_key()) { throw new UpdateException(__('Update error: The provided license is invalid.', 'license-wp'), 'invalid_key'); } // check if license expired if ($license->is_expired()) { throw new UpdateException(__('Update error: The provided license has expired.', 'license-wp'), 'expired_key'); } // get api product by given api product id (slug) $api_product = $license->get_api_product_by_slug($request['api_product_id']); // check if license grants access to request api product if (null === $api_product) { throw new UpdateException(__('Update error: This license does not allow access to the requested product.', 'license-wp'), 'no_api_product_access'); } // get activations $activations = $license->get_activations($api_product); // store if activation is found $is_activated = false; // check if instance is activated if (count($activations) > 0) { /** @var \Never5\LicenseWP\Activation\Activation $activation */ foreach ($activations as $activation) { if ($activation->get_instance() === $activation->format_instance($request['instance'])) { $is_activated = true; break; } } } // throw exception if given instance is not activated if (false === $is_activated) { throw new UpdateException(sprintf(__('Update error: This license is not activated on this website. Manage your activations on your My Account page: %s', 'license-wp'), get_permalink(get_option('woocommerce_myaccount_page_id'))), 'no_activation'); } // do given request switch ($request['request']) { case 'pluginupdatecheck': $this->plugin_update_check($license, $api_product, $request); break; case 'plugininformation': $this->plugin_information($license, $api_product, $request); break; } } catch (UpdateException $e) { $response = new \stdClass(); switch ($request['request']) { case 'pluginupdatecheck': $response->slug = ''; $response->plugin = ''; $response->new_version = ''; $response->url = ''; $response->package = ''; break; case 'plugininformation': $response->name = ''; $response->slug = ''; $response->plugin = ''; $response->version = ''; $response->last_updated = ''; $response->download_link = ''; $response->author = ''; $response->requires = ''; $response->tested = ''; $response->homepage = ''; $response->sections = ''; break; } $response->errors = $e->__toArray(); die(serialize($response)); } }
function __load_license_wp() { // fetch instance license_wp(); }
/** * Deactivates an instance of a license * * @param \Never5\LicenseWP\License\License $license * @param \Never5\LicenseWP\ApiProduct\ApiProduct $api_product * @param array $request * * @throws ApiException */ private function deactivate($license, $api_product, $request) { // get activations $activations = $license->get_activations($api_product); // check & loop if (count($activations) > 0) { /** @var \Never5\LicenseWP\Activation\Activation $activation */ foreach ($activations as $activation) { // check if given instance equals activation instance if ($activation->format_instance($request['instance']) === $activation->get_instance()) { // set activation to not active $activation->set_activation_active(0); // set activation date to now $activation->set_activation_date(new \DateTime()); // persist activation $activation = license_wp()->service('activation_repository')->persist($activation); // check if deactivation was successful if ($activation->is_active()) { throw new ApiException(__('Deactivation error: Could not deactivate license key. Please contact support.', 'license-wp'), 108); } // response $response = apply_filters('license_wp_api_activation_response', array('success' => true)); // send JSON the WP way wp_send_json($response); exit; } } } throw new ApiException(__('Deactivation error: instance not found.', 'license-wp'), 109); }
/** * On delete post * * @param int $order_id */ public function order_delete($order_id) { // check if allowed if (!current_user_can('delete_posts')) { return; } // check id if ($order_id > 0) { // check post type $post_type = get_post_type($order_id); // only continue on WC shop order if ('shop_order' === $post_type) { license_wp()->service('license_manager')->remove_license_data_by_order($order_id); } } }
/** * Variable product license data * * @param $loop * @param $variation_data * @param $variation */ public function variable_license_data($loop, $variation_data, $variation) { global $post, $thepostid; include license_wp()->service('file')->plugin_path() . '/assets/views/html-variation-license-data.php'; }
/** * Method to enqueue page specific styles & scripts */ public function page_enqueue($hook) { if ('post-new.php' === $hook || 'post.php' === $hook) { wp_enqueue_script('lwp_add_api_product', license_wp()->service('file')->plugin_url('/assets/js/add-api-product' . (!SCRIPT_DEBUG ? '.min' : '') . '.js'), array('jquery', 'jquery-ui-datepicker'), license_wp()->get_version()); } }
/** * Get activations of license * Uses Activations\Manager:get_activations * * @param \Never5\LicenseWP\ApiProduct\ApiProduct $api_product * * @return array */ public function get_activations($api_product = null) { return license_wp()->service('activation_manager')->get_activations($this, $api_product); }
/** * Save the new license */ public function save() { // vars $activation_email = wc_clean($_POST['activation_email']); $product_id = absint($_POST['product_id']); $user_id = absint($_POST['user_id']); try { // check nonce if (empty($_POST['license_wp_licensing_nonce']) || !wp_verify_nonce($_POST['license_wp_licensing_nonce'], 'add_license')) { throw new \Exception(__('Nonce check failed', 'license-wp')); } // check product ID if (empty($product_id)) { throw new \Exception(__('You must choose a product for this license', 'license-wp')); } // check activation email if (empty($activation_email) && empty($user_id)) { throw new \Exception(__('Either an activation email or user ID is required', 'license-wp')); } // get WooCommerce product $product = \wc_get_product($product_id); // product must be an API license product if ('yes' !== get_post_meta($product->id, '_is_api_product_license', true)) { throw new \Exception(__('Invalid product', 'license-wp')); } // set activation email to user email if no activation email is set if (!$activation_email && $user_id) { $user = get_user_by('id', $user_id); $activation_email = $user->user_email; } // exit if we still have no valid email address at this point if (empty($activation_email) || !is_email($activation_email)) { throw new \Exception(__('A valid activation email is required', 'license-wp')); } // get activation limit if (!$product->variation_id || !($activation_limit = get_post_meta($product->variation_id, '_license_activation_limit', true))) { $activation_limit = get_post_meta($product->id, '_license_activation_limit', true); } // get expiry days if (!$product->variation_id || !($license_expiry_days = get_post_meta($product->variation_id, '_license_expiry_days', true))) { $license_expiry_days = get_post_meta($product->id, '_license_expiry_days', true); } // create license object /** @var \Never5\LicenseWP\License\License $license */ $license = license_wp()->service('license_factory')->make(); // set license data, key is generated when persisting license $license->set_activation_email($activation_email); $license->set_user_id($user_id); $license->set_product_id($product_id); $license->set_activation_limit($activation_limit); // set date created $date_created = new \DateTime(); $license->set_date_created($date_created->setTime(0, 0, 0)); // set correct expiry days if (!empty($license_expiry_days)) { $exp_date = new \DateTime(); $license->set_date_expires($exp_date->setTime(0, 0, 0)->modify("+{$license_expiry_days} days")); } // store license $license = license_wp()->service('license_repository')->persist($license); // check if license was stored if ('' != $license->get_key()) { // try to get a first name if (!empty($user) && !empty($user->first_name)) { $user_first_name = $user->first_name; } else { $user_first_name = false; } // send email to activation email license_wp()->service('email_manager')->send(new Email\NewLicense($license, $user_first_name), $activation_email); $admin_message = sprintf(__('License key has been emailed to %s.', 'license-wp'), $activation_email); echo sprintf('<div class="updated"><p>%s</p></div>', $admin_message); } else { throw new \Exception(__('Could not create license!', 'license-wp')); } } catch (\Exception $e) { echo sprintf('<div class="error"><p>%s</p></div>', $e->getMessage()); } }