/** * Initialize this object. * * @param \stdClass $data */ protected function init(\stdClass $data) { $this->id = $data->id; $this->key = itelic_get_key($data->lkey); $this->renewal_date = make_date_time($data->renewal_date); $this->key_expired_date = make_date_time($data->key_expired_date); if ($data->transaction_id) { $this->transaction = it_exchange_get_transaction($data->transaction_id); } else { $this->transaction = null; } $this->revenue = (double) $data->revenue; }
/** * Initialize this object. * * @param \stdClass $data */ protected function init(\stdClass $data) { $this->id = $data->id; $this->key = itelic_get_key($data->lkey); $this->location = $data->location; $this->status = $data->status; $this->activation = make_date_time($data->activation); if (!empty($data->deactivation) && $data->deactivation != '0000-00-00 00:00:00') { $this->deactivation = make_date_time($data->deactivation); } $this->release = itelic_get_release($data->release_id); }
function get_object_by_id($object_id) { return itelic_get_key($object_id); }
/** * Get the currently displayed key. * * @since 1.0 * * @return Key */ protected function get_current_key() { if (defined('DOING_AJAX') && DOING_AJAX) { $key = $_POST['key']; } elseif (isset($_GET['key'])) { $key = $_GET['key']; } else { return null; } return itelic_get_key($key); }
/** * Apply the renewal discount. * * @since 1.0 * * @param string|float $db_base_price * @param array $product * @param bool $format * * @return string|float */ public function apply_renewal_discount($db_base_price, $product, $format) { try { $product = itelic_get_product($product['product_id']); } catch (\Exception $e) { return $db_base_price; } if (!$product->get_feature('licensing')) { return $db_base_price; } $session = $this->get_cache_data(); if (!isset($session["p" . $product->ID]) || $session["p" . $product->ID] === null) { return $db_base_price; } $key = $session["p" . $product->ID]; $key = itelic_get_key($key); $discount = new Discount($key); if (!$discount->is_discount_valid()) { return $db_base_price; } return $discount->get_discount_price($format); }
/** * Serve the request to this endpoint. * * @param \ArrayAccess $get * @param \ArrayAccess $post * * @return Response */ public function serve(\ArrayAccess $get, \ArrayAccess $post) { /** * Fires before the download query args are validated. * * If add-ons are completely overriding how product updates * are delivered. They should use this action. * * @since 1.0 * * @param \ArrayAccess $get * @param \ArrayAccess $post */ do_action('itelic_pre_validate_download', $get, $post); if (!\ITELIC\validate_query_args($get)) { $this->logger->notice('Invalid download link used.', array('get' => $get, 'post' => $post)); status_header(403); _e("This download link is invalid or has expired.", Plugin::SLUG); if (!defined('DOING_TESTS') || !DOING_TESTS) { die; } else { return; } } $activation = itelic_get_activation($get['activation']); $key = $get['key']; if (!$activation || $activation->get_key()->get_key() != $key) { $key_object = itelic_get_key($key); if ($key_object && $key_object->get_customer()) { $user = $key_object->get_customer()->id; } elseif ($activation->get_key()->get_customer()) { $user = $activation->get_key()->get_customer()->id; } else { $user = 0; } $this->logger->notice('Invalid download link used. Key activation mismatch.', array('get' => $get, 'post' => $post, '_user' => $user)); status_header(403); _e("This download link is invalid or has expired.", Plugin::SLUG); if (!defined('DOING_TESTS') || !DOING_TESTS) { die; } else { return; } } $release = $activation->get_key()->get_product()->get_latest_release_for_activation($activation); $file = $release->get_download(); itelic_create_update(array('activation' => $activation, 'release' => $release)); $this->logger->info('Download for {product} delivered.', array('product' => $activation->get_key()->get_product()->post_title, '_user' => $activation->get_key()->get_customer()->id, 'get' => $get, 'post' => $post)); /** * Fires before a download is served. * * Download links are only generated from the Product endpoint * if both the license key and activation records are valid. * * If you are generating download links differently, you should * probably validate the activation status and key status again. * * @since 1.0 * * @param \WP_Post $file WordPress attachment object for the software update. * @param Key $key License key used for validation. * @param Activation $activation Activation the download is being delivered to. */ do_action('itelic_pre_serve_download', $file, $key, $activation); \ITELIC\serve_download(wp_get_attachment_url($file->ID)); }
/** * (PHP 5 >= 5.1.0)<br/> * Constructs the object * * @link http://php.net/manual/en/serializable.unserialize.php * * @param string $serialized <p> * The string representation of the object. * </p> * * @return void */ public function unserialize($serialized) { $data = unserialize($serialized); $this->product = itelic_get_product($data['product']); $this->feature_data = $this->product->get_feature('licensing-discount'); $this->key = itelic_get_key($data['key']); }
/** * Create a renewal transaction key. * * @api * * @param array $args { * * @type string $key The license key to be used. If empty, one will be * generated. * @type float $paid If manually generating a transaction, the amount paid. * @tpye string $date When the transaction occurred. GMT. * } * * @return IT_Exchange_Transaction */ function itelic_create_renewal_transaction($args) { $defaults = array('key' => '', 'paid' => '', 'date' => ''); $args = ITUtility::merge_defaults($args, $defaults); $key = itelic_get_key($args['key']); if (!$key) { return new WP_Error('invalid_key', __("Invalid key", \ITELIC\Plugin::SLUG)); } $product = $key->get_product(); if (!function_exists('it_exchange_manual_purchases_addon_transaction_uniqid')) { return new WP_Error('no_manual_purchases', __("Manual purchases add-on is not installed.", \ITELIC\Plugin::SLUG)); } // Grab default currency $settings = it_exchange_get_option('settings_general'); $currency = $settings['default-currency']; $description = array(); $product_id = $product->ID; $itemized_data = apply_filters('it_exchange_add_itemized_data_to_cart_product', array(), $product_id); if (!is_serialized($itemized_data)) { $itemized_data = maybe_serialize($itemized_data); } $i = $product_id . '-' . md5($itemized_data); $discounted = new \ITELIC\Renewal\Discount($key); $discounted = $discounted->get_discount_price(); $products[$i]['product_base_price'] = $discounted; $products[$i]['product_subtotal'] = $products[$i]['product_base_price']; //need to add count $products[$i]['product_name'] = get_the_title($product_id); $products[$i]['product_id'] = $product_id; $products[$i]['count'] = 1; $description[] = $products[$i]['product_name']; $description = apply_filters('it_exchange_get_cart_description', join(', ', $description), $description); // Package it up and send it to the transaction method add-on $total = empty($args['paid']) ? $discounted : it_exchange_convert_to_database_number($args['paid']); $object = new stdClass(); $object->total = number_format(it_exchange_convert_from_database_number($total), 2, '.', ''); $object->currency = $currency; $object->description = $description; $object->products = $products; remove_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase'); $uniquid = it_exchange_manual_purchases_addon_transaction_uniqid(); $txn_args = array(); if (isset($args['date'])) { $date = \ITELIC\make_date_time($args['date']); $txn_args['post_date'] = \ITELIC\convert_gmt_to_local($date)->format('Y-m-d H:i:s'); $txn_args['post_date_gmt'] = $date->format('Y-m-d H:i:s'); } $customer = $key->get_customer()->id; $tid = it_exchange_add_transaction('manual-purchases', $uniquid, 'Completed', $customer, $object, $txn_args); add_action('it_exchange_add_transaction_success', 'ITELIC\\renew_key_on_renewal_purchase'); return it_exchange_get_transaction($tid); }
/** * Setup the table. */ public function setup_table() { if (!Dispatch::is_current_view('list')) { return; } try { $msg = $this->process_bulk_actions(); if ($msg) { $this->message[View::NOTICE_SUCCESS] = $msg; } } catch (\Exception $e) { $this->message[View::NOTICE_ERROR] = $e->getMessage(); } $query = new Keys($this->generate_get_key_args()); $keys = $query->get_results(); $total = $query->get_total_items(); $products = itelic_get_products_with_licensing_enabled(); $this->table = new Table($this->prepare_data($keys), $total, $products); if (isset($_GET['s'])) { $key = itelic_get_key($_GET['s']); if ($key) { wp_redirect(itelic_get_admin_edit_key_link($key->get_key())); die; } } }
/** * Create an activation record. * * @api * * @since 1.0 * * @param array $args * * @return \ITELIC\Activation|WP_Error */ function itelic_create_activation($args) { $defaults = array('key' => '', 'location' => '', 'activation' => '', 'release' => '', 'status' => '', 'track' => 'stable'); $args = ITUtility::merge_defaults($args, $defaults); $key = is_string($args['key']) ? itelic_get_key($args['key']) : $args['key']; if (!$key) { return new WP_Error('invalid_key', __("Invalid Key", \ITELIC\Plugin::SLUG)); } $location = $args['location']; if (!empty($args['activation'])) { if (is_string($args['activation'])) { $activation = \ITELIC\make_date_time($args['activation']); } else { $activation = $args['activation']; } if (!$activation instanceof DateTime) { return new WP_Error('invalid_activation', __("Invalid activation date.", \ITELIC\Plugin::SLUG)); } } else { $activation = null; } if (!empty($args['release'])) { if (is_string($args['release'])) { $release = itelic_get_release($args['release']); } else { $release = $args['release']; } if (!$release instanceof \ITELIC\Release) { return new WP_Error('invalid_release', __("Invalid release.", \ITELIC\Plugin::SLUG)); } } else { $release = null; } $status = $args['status']; try { $activation = \ITELIC\Activation::create($key, $location, $activation, $release, $status); $activation->add_meta('track', $args['track']); } catch (Exception $e) { return new WP_Error('exception', $e->getMessage()); } return $activation; }
/** * AJAX handler for remote activating a location. * * @internal * * @since 1.0 */ function account_licenses_activate() { if (!isset($_POST['location']) || !isset($_POST['nonce']) || !isset($_POST['key'])) { wp_send_json_error(array('message' => __("Invalid request format.", Plugin::SLUG))); } $location = sanitize_text_field($_POST['location']); $key = $_POST['key']; $nonce = $_POST['nonce']; if (!wp_verify_nonce($nonce, "itelic-remote-activate-{$key}")) { wp_send_json_error(array('message' => __("Request expired. Please refresh and try again.", Plugin::SLUG))); } try { $key = itelic_get_key($key); } catch (\Exception $e) { wp_send_json_error(array('message' => __("Invalid license key.", Plugin::SLUG))); die; } if (!$key) { wp_send_json_error(array('message' => __("Invalid license key.", Plugin::SLUG))); } if (!current_user_can('edit_user', $key->get_customer()->wp_user->ID)) { wp_send_json_error(array('message' => __("You don't have permission to do this.", Plugin::SLUG))); } try { itelic_activate_license_key($key, $location); } catch (\Exception $e) { wp_send_json_error(array('message' => $e->getMessage())); } wp_send_json_success(); }
/** * Check authentication, keeping the mode in mind. * * @since 1.0 * * @param Authenticatable $endpoint * * @return bool */ protected function handle_auth(Authenticatable $endpoint) { if (!isset($_SERVER['PHP_AUTH_USER']) || trim($_SERVER['PHP_AUTH_USER']) == '') { return false; } $license_key = $_SERVER['PHP_AUTH_USER']; try { $key = itelic_get_key($license_key); } catch (\Exception $e) { return false; } if (!$key) { return false; } $this->current_user = $key->get_customer() ? $key->get_customer()->wp_user : null; if (!empty($_SERVER['PHP_AUTH_PW'])) { $activation = itelic_get_activation($_SERVER['PHP_AUTH_PW']); } else { $activation = null; } $endpoint->set_auth_license_key($key); $endpoint->set_auth_activation($activation); switch ($endpoint->get_auth_mode()) { case Authenticatable::MODE_ACTIVE: return $key->get_status() == Key::ACTIVE; case Authenticatable::MODE_EXISTS: return true; case Authenticatable::MODE_VALID_ACTIVATION: if (!$activation) { return false; } if ($key->get_status() != Key::ACTIVE) { return false; } if ($activation->get_status() != Activation::ACTIVE) { return false; } if ($activation->get_key()->get_key() != $key->get_key()) { return false; } return true; default: return false; } }
/** * Activate a release. * * ## OPTIONS * * <location> * : Where the software is being activated. Typically a website. * * <key> * : The key being activated. * * [--when=<when>] * : Wen the activation occurred. Accepts strtotime compatible * value. GMT. * * [--version=<version>] * : The version of the software installed. Default: latest. * * [--track=<track>] * : Accepted values: stable, pre-release. Default: stable * * [--porcelain] * : Output just the new activation ID. * * @param $args * @param $assoc_args */ public function activate($args, $assoc_args) { list($location, $key) = $args; $key = itelic_get_key($key); if (!$key) { WP_CLI::error("Invalid key."); } if ($key->get_status() != ITELIC\Key::ACTIVE) { WP_CLI::error(sprintf("Key has a status of '%s' not 'active'.", $key->get_status())); } if (isset($assoc_args['when'])) { $when = \ITELIC\make_date_time($assoc_args['when']); } else { $when = null; } if (isset($assoc_args['version'])) { $release = itelic_get_release_by_version($key->get_product()->ID, $assoc_args['version']); if (!$release) { WP_CLI::error(sprintf("Invalid release ID %d.", $assoc_args['release'])); } } else { $release = null; } if (isset($assoc_args['track'])) { if (in_array($assoc_args['track'], array('stable', 'pre-release'))) { $track = $assoc_args['track']; } else { WP_CLI::error("Invalid value '%s' for track."); } } else { $track = 'stable'; } parent::_create($args, $assoc_args, function () use($location, $key, $when, $release, $track) { $a = itelic_activate_license_key($key, $location, $when, $release, $track); if ($a) { return $a->get_pk(); } return new WP_Error(); }); }