/**
  * 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;
 }
 /**
  * Get a count of all of the sites that have been updated.
  *
  * @since 1.0
  *
  * @param bool $break_cache
  *
  * @return int
  */
 public function get_total_updated($break_cache = false)
 {
     if ($this->get_status() == self::STATUS_ARCHIVED) {
         return $this->get_meta('updated', true);
     }
     $count = wp_cache_get($this->get_ID(), 'itelic-release-upgrade-count');
     if ($count === false || $break_cache) {
         $simple_query = Manager::make_simple_query_object('itelic-updates');
         $count = $simple_query->count(array('release_id' => $this->get_ID()));
         wp_cache_set($this->get_ID(), $count, 'itelic-release-upgrade-count', HOUR_IN_SECONDS);
     }
     return $count;
 }
 /**
  * @return int
  */
 public function get_active_count()
 {
     $count = wp_cache_get($this->get_key(), 'itelic-key-active-count');
     if ($count === false) {
         $db = Manager::make_simple_query_object('itelic-activations');
         $count = $db->count(array('lkey' => $this->get_key(), 'status' => Activation::ACTIVE));
         wp_cache_set($this->get_key(), $count, 'itelic-key-active-count');
     }
     return $count;
 }