/**
 * Create a renewal record.
 *
 * @api
 *
 * @since 1.0
 *
 * @param array $args
 *
 * @return \ITELIC\Renewal|WP_Error
 */
function itelic_create_renewal($args)
{
    $defaults = array('key' => '', 'transaction' => '', 'expired' => '', 'renewal' => '');
    $args = ITUtility::merge_defaults($args, $defaults);
    if (is_string($args['key'])) {
        $key = itelic_get_key($args['key']);
    } else {
        $key = $args['key'];
    }
    if (!$key) {
        return new WP_Error('invalid_key', __("Invalid Key", \ITELIC\Plugin::SLUG));
    }
    if (!empty($args['transaction'])) {
        $transaction = it_exchange_get_transaction($args['transaction']);
        if (!$transaction) {
            return new WP_Error('invalid_transaction', __("Invalid transaction.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $transaction = null;
    }
    $expired = is_string($args['expired']) ? \ITELIC\make_date_time($args['expired']) : $args['expired'];
    if (!$expired instanceof DateTime) {
        return new WP_Error('invalid_expiration', __("Invalid expiration date.", \ITELIC\Plugin::SLUG));
    }
    if (!empty($args['renewal'])) {
        $renewal = is_string($args['renewal']) ? \ITELIC\make_date_time($args['renewal']) : $args['renewal'];
        if (!$renewal instanceof DateTime) {
            return new WP_Error("invalid_renewal", __("Invalid renewal date.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $renewal = null;
    }
    return \ITELIC\Renewal::create($key, $transaction, $expired, $renewal);
}
示例#2
0
 /**
  * Renew this license.
  *
  * @since 1.0
  *
  * @param \IT_Exchange_Transaction $transaction
  *
  * @return Renewal
  */
 public function renew(\IT_Exchange_Transaction $transaction = null)
 {
     if (!$this->is_renewable()) {
         throw new \UnexpectedValueException(__("You can't renew a license key that doesn't expire.", Plugin::SLUG));
     }
     if ($transaction === null) {
         $date = null;
     } else {
         $date = make_date_time($transaction->post_date_gmt);
     }
     $record = Renewal::create($this, $transaction, $this->get_expires(), $date);
     $this->extend();
     $now = make_date_time();
     if ($this->get_expires() > $now) {
         $this->set_status(self::ACTIVE);
     }
     /**
      * Fires when a license key is renewed.
      *
      * @since 1.0
      *
      * @param Key $this
      */
     do_action('itelic_renew_key', $this);
     return $record;
 }