Example #1
0
 /**
  * Check if a key is renewable.
  *
  * @since 1.0
  *
  * @return bool
  */
 public function is_renewable()
 {
     if ($this->get_expires() === null) {
         return false;
     }
     return $this->product->has_feature('recurring-payments');
 }
 /**
  * Constructor.
  *
  * @since 1.0
  *
  * @param Key     $key
  * @param Product $upgrade_product
  * @param string  $variant_hash
  */
 public function __construct(Key $key, Product $upgrade_product, $variant_hash = '')
 {
     $this->key = $key;
     if (!$upgrade_product->has_feature('licensing')) {
         throw new \InvalidArgumentException("Upgrade product must have licensing enabled.");
     }
     $this->upgrade_product = $upgrade_product;
     $this->variant_hash = $variant_hash;
 }
 /**
  * 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;
 }
/**
 * Generate a key for a certain transaction product.
 *
 * @internal
 *
 * @since 1.0
 *
 * @param \IT_Exchange_Transaction $transaction
 * @param Product                  $product
 * @param Factory                  $factory
 * @param string                   $status Optionally override the new key's status. Default active.
 * @param string                   $key    Optionally specify the license key to be used. If empty, uses Factory.
 *
 * @return Key
 */
function generate_key_for_transaction_product(\IT_Exchange_Transaction $transaction, Product $product, Factory $factory, $status = '', $key = '')
{
    $customer = it_exchange_get_transaction_customer($transaction);
    if (!$customer instanceof \IT_Exchange_Customer) {
        $customer = new \IT_Exchange_Customer($customer);
    }
    if (!$key) {
        $key = $factory->make();
    }
    foreach ($transaction->get_products() as $tran_product) {
        if ($tran_product['product_id'] == $product->ID) {
            if (empty($tran_product['itemized_data'])) {
                continue;
            }
            if (is_string($tran_product['itemized_data'])) {
                $itemized = maybe_unserialize($tran_product['itemized_data']);
            } else {
                $itemized = $tran_product['itemized_data'];
            }
            if (isset($itemized['it_variant_combo_hash'])) {
                $hash = $itemized['it_variant_combo_hash'];
                $max = $product->get_feature('licensing', array('field' => 'limit', 'for_hash' => $hash));
            }
        }
    }
    if (!isset($max)) {
        $max = $product->get_feature('licensing', array('field' => 'limit'));
    }
    if (!$product->has_feature('recurring-payments')) {
        $expires = null;
    } else {
        $type = $product->get_feature('recurring-payments', array('setting' => 'interval'));
        $count = $product->get_feature('recurring-payments', array('setting' => 'interval-count'));
        $interval = convert_rp_to_date_interval($type, $count);
        $expires = make_date_time($transaction->post_date_gmt);
        $expires->add($interval);
    }
    return Key::create($key, $transaction, $product, $customer, $max, $expires, $status);
}