/** * Create an activation. * * @param Key $key * @param string $location * @param \DateTime $activation * @param Release $release * @param string $status * * @return Activation * * @throws \LogicException|DB_Exception */ public static function create(Key $key, $location, \DateTime $activation = null, Release $release = null, $status = '') { if (empty($key) || empty($location)) { throw new \InvalidArgumentException(__("The license key and install location are required.", Plugin::SLUG)); } if (strlen($location) > 191) { throw new \LengthException("The location field has a max length of 191 characters."); } if ($key->get_max() && $key->get_active_count() >= $key->get_max()) { throw new \OverflowException(__("This license key has reached it's maximum number of activations.", Plugin::SLUG)); } if ($activation === null) { $activation = make_date_time()->format('Y-m-d H:i:s'); } else { $activation = $activation->format('Y-m-d H:i:s'); } if (empty($status)) { $status = self::ACTIVE; } if ($key->is_online_product()) { $location = itelic_normalize_url($location); } $data = array('lkey' => $key->get_key(), 'location' => $location, 'activation' => $activation, 'deactivation' => null, 'status' => $status); if ($release) { $data['release_id'] = $release->get_pk(); } $db = Manager::make_simple_query_object('itelic-activations'); $existing_activation = itelic_get_activation_by_location($location, $key); if ($existing_activation) { throw new \InvalidArgumentException(__("An activation with this same location already exists.", Plugin::SLUG)); } $id = $db->insert($data); if (!$id) { return null; } $activation = self::get($id); Cache::add($activation); if (!$release) { $latest = $key->get_product()->get_latest_release_for_activation($activation); if ($latest) { $activation->set_release($latest); } } /** * Fires when an activation record is created. * * @since 1.0 * * @param Activation $activation */ do_action('itelic_create_activation', $activation); return $activation; }
/** * Create an Upgrade record. * * @since 1.0 * * @param Activation $activation * @param Release $release * @param \DateTime $update_date * @param string $previous_version * * @return Update|null * @throws DB_Exception */ public static function create(Activation $activation, Release $release, \DateTime $update_date = null, $previous_version = '') { if ($update_date === null) { $update_date = make_date_time(); } if (empty($previous_version) && $activation->get_release()) { $previous_version = $activation->get_release()->get_version(); } $data = array('activation' => $activation->get_id(), 'release_id' => $release->get_ID(), 'update_date' => $update_date->format("Y-m-d H:i:s"), 'previous_version' => $previous_version); $db = Manager::make_simple_query_object('itelic-updates'); $ID = $db->insert($data); $update = self::get($ID); if ($update) { $activation->set_release($release); /** * Fires when an update record is created. * * @since 1.0 * * @param Update $update */ do_action('itelic_create_update', $update); Cache::add($update); } return $update; }
/** * Create a renewal record. * * @since 1.0 * * @param Key $key * @param \IT_Exchange_Transaction $transaction * @param \DateTime $expired * @param \DateTime $renewal * * @return Renewal */ public static function create(Key $key, \IT_Exchange_Transaction $transaction = null, \DateTime $expired, \DateTime $renewal = null) { if (empty($renewal)) { $renewal = make_date_time(); } $revenue = '0.00'; if ($transaction) { $tid = $transaction->ID; foreach ($transaction->get_products() as $product) { if ($product['product_id'] == $key->get_product()->ID) { $revenue = $product['product_subtotal']; break; } } } else { $tid = 0; } $data = array('lkey' => $key->get_key(), 'renewal_date' => $renewal->format("Y-m-d H:i:s"), 'key_expired_date' => $expired->format("Y-m-d H:i:s"), 'transaction_id' => $tid, 'revenue' => $revenue); $db = Manager::make_simple_query_object('itelic-renewals'); $id = $db->insert($data); $renewal = self::get($id); if ($renewal) { /** * Fires when a renewal record is created. * * @since 1.0 * * @param Renewal $renewal */ do_action('itelic_create_renewal', $renewal); Cache::add($renewal); } return $renewal; }
/** * Create a new release record. * * If status is set to active, the start date will automatically be set to * now. * * @since 1.0 * * @param Product $product * @param \WP_Post $file Attachment of the download * @param string $version * @param string $type * @param string $status * @param string $changelog * * @return Release|null * @throws DB_Exception */ public static function create(Product $product, \WP_Post $file, $version, $type, $status = '', $changelog = '') { if (empty($status)) { $status = self::STATUS_DRAFT; } if (!array_key_exists($status, self::get_statuses())) { throw new \InvalidArgumentException("Invalid status."); } if (!array_key_exists($type, self::get_types())) { throw new \InvalidArgumentException("Invalid type."); } if (get_post_type($file) != 'attachment') { throw new \InvalidArgumentException("Invalid update file."); } if (!$product->has_feature('licensing')) { throw new \InvalidArgumentException("Product given does not have the licensing feature enabled."); } $current_version = $product->get_feature('licensing', array('field' => 'version')); $first_release = itelic_get_release(get_post_meta($product->ID, '_itelic_first_release', true)); if ($first_release && version_compare($version, $current_version, '<=')) { throw new \InvalidArgumentException("New release version must be greater than the current product's version."); } $data = array('product' => $product->ID, 'download' => $file->ID, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => wp_kses_post($changelog)); if ($status == self::STATUS_ACTIVE) { $data['start_date'] = make_date_time()->format('Y-m-d H:i:s'); } $db = Manager::make_simple_query_object('itelic-releases'); $ID = $db->insert($data); $release = self::get($ID); if ($release) { /** * Fires when a release is created. * * @since 1.0 * * @param Release $release */ do_action('itelic_create_release', $release); if ($status == self::STATUS_ACTIVE) { if ($type != self::TYPE_PRERELEASE) { self::do_activation($product, $file, $version); } /** * Fires when a release is activated. * * @since 1.0 * * @param Release $release */ do_action('itelic_activate_release', $release); } if (in_array($status, array(self::STATUS_ACTIVE, self::STATUS_ARCHIVED))) { wp_cache_delete($product->ID, 'itelic-changelog'); } Cache::add($release); } return $release; }
/** * Create a license key record. * * @since 1.0 * * @param string $key * @param \IT_Exchange_Transaction $transaction * @param \IT_Exchange_Product $product * @param \IT_Exchange_Customer $customer * @param int $max * @param \DateTime $expires * @param string $status * * @return Key */ public static function create($key, \IT_Exchange_Transaction $transaction, \IT_Exchange_Product $product, \IT_Exchange_Customer $customer, $max, \DateTime $expires = null, $status = '') { if (empty($key)) { throw new \LengthException("\$key must not be empty."); } if (strlen($key) > 128) { throw new \LengthException("The maximum key length is 128 characters."); } if (empty($status)) { $status = self::ACTIVE; } $now = make_date_time(); if ($expires && $expires < $now) { $status = self::EXPIRED; } $data = array('lkey' => $key, 'transaction_id' => $transaction->ID, 'product' => $product->ID, 'customer' => $customer->id, 'status' => $status, 'max' => (int) $max, 'expires' => isset($expires) ? $expires->format("Y-m-d H:i:s") : null); $db = Manager::make_simple_query_object('itelic-keys'); $db->insert($data); $key = self::get($key); if ($key) { /** * Fires when a license key is created. * * @since 1.0 * * @param Key $key */ do_action('itelic_create_key', $key); Cache::add($key); } return $key; }