/**
  * Serve the request to this endpoint.
  *
  * @param \ArrayAccess $get
  * @param \ArrayAccess $post
  *
  * @return Response
  *
  * @throws Exception|\Exception
  */
 public function serve(\ArrayAccess $get, \ArrayAccess $post)
 {
     $now = \ITELIC\make_date_time();
     $expires = $now->add(new \DateInterval("P1D"));
     $release = $this->activation->get_key()->get_product()->get_latest_release_for_activation($this->activation);
     // this really is a safeguard.
     if (!$release) {
         throw new \UnexpectedValueException(__("No releases available for this product.", Plugin::SLUG));
     }
     if ($release->get_type() == Release::TYPE_SECURITY) {
         $notice = $release->get_meta('security-message', true);
     } else {
         if ($release->get_type() == Release::TYPE_MAJOR) {
             $notice = __("Warning! This is a major upgrade. Make sure you backup your website before updating.", Plugin::SLUG);
         } else {
             $notice = '';
         }
     }
     /**
      * Filters the upgrade notice sent back from the API.
      *
      * @since 1.0
      *
      * @param string  $notice
      * @param Release $release
      */
     $notice = apply_filters('itelic_get_release_upgrade_notice', $notice, $release);
     // if the installed version of the software is passed to the API,
     // and the installed version is greater than the version on record, create an update record
     // this accounts for manually updating the theme or plugin
     if (isset($get['installed_version'])) {
         $installed = itelic_get_release_by_version($this->key->get_product()->ID, $get['installed_version']);
         if ($installed && version_compare($installed->get_version(), $this->activation->get_release()->get_version(), '>')) {
             Update::create($this->activation, $installed);
         }
     }
     $info = array('version' => $release->get_version(), 'package' => \ITELIC\generate_download_link($this->activation), 'expires' => $expires->format(\DateTime::ISO8601), 'upgrade_notice' => $notice, 'type' => $release->get_type());
     /**
      * Filter the version info returned by the API.
      *
      * @since 1.0
      *
      * @param array   $info
      * @param Key     $key
      * @param Product $product
      */
     $info = apply_filters('itelic_api_version_info', $info, $this->key, $this->key->get_product());
     return new Response(array('success' => true, 'body' => array('list' => array($this->key->get_product()->ID => $info))));
 }
 /**
  * Generate update records.
  *
  * ## Options
  *
  * <product>
  * : Product ID to generate update records for.
  *
  * @param $args
  * @param $assoc_args
  */
 public function generate($args, $assoc_args)
 {
     list($product) = $args;
     $product = itelic_get_product($product);
     if (!$product) {
         WP_CLI::error("Invalid product ID");
     }
     $releases = itelic_get_releases(array('product' => $product->ID, 'order' => array('start_date' => 'ASC')));
     $notify = \WP_CLI\Utils\make_progress_bar(sprintf("Generating Updates: %d", $product->ID), count($releases));
     foreach ($releases as $release) {
         switch ($release->get_type()) {
             case \ITELIC\Release::TYPE_MAJOR:
                 $percent_updated = 75;
                 break;
             case \ITELIC\Release::TYPE_MINOR:
                 $percent_updated = 90;
                 break;
             case \ITELIC\Release::TYPE_SECURITY:
                 $percent_updated = 95;
                 break;
             case \ITELIC\Release::TYPE_PRERELEASE:
                 $percent_updated = 95;
                 break;
             default:
                 throw new InvalidArgumentException("Invalid release type.");
         }
         $total_activations = new \ITELIC\Query\Activations(array('activation' => array('before' => $release->get_start_date()->format('Y-m-d H:i:s')), 'product' => $product->ID, 'return_value' => 'count'));
         $total_activations = $total_activations->get_results();
         $activations = itelic_get_activations(array('activation' => array('before' => $release->get_start_date()->format('Y-m-d H:i:s')), 'product' => $product->ID, 'order' => 'rand', 'items_per_page' => $total_activations * ($percent_updated / 100)));
         foreach ($activations as $activation) {
             if ($activation->get_deactivation() && $activation->get_deactivation() > $release->get_start_date()) {
                 continue;
             }
             if ($release->get_type() == ITELIC\Release::TYPE_MAJOR) {
                 $days = rand(0, 10);
             } else {
                 $days = rand(0, 4);
             }
             $upgade_date = $release->get_start_date()->add(new DateInterval("P{$days}D"));
             \ITELIC\Update::create($activation, $release, $upgade_date);
         }
         if ($release->get_status() == \ITELIC\Release::STATUS_ARCHIVED) {
             $release->set_status(\ITELIC\Release::STATUS_ACTIVE);
             $release->archive();
         }
         $notify->tick();
     }
     $notify->finish();
 }
/**
 * Create an update record.
 *
 * @api
 *
 * @since 1.0
 *
 * @param array $args
 *
 * @return \ITELIC\Update|WP_Error
 */
function itelic_create_update($args)
{
    $defaults = array('activation' => '', 'release' => '', 'update_date' => '', 'previous_version' => '');
    $args = ITUtility::merge_defaults($args, $defaults);
    $activation = is_numeric($args['activation']) ? itelic_get_activation($args['activation']) : $args['activation'];
    if (!$activation) {
        return new WP_Error('invalid_activation', __("Invalid activation record.", \ITELIC\Plugin::SLUG));
    }
    $release = is_numeric($args['release']) ? itelic_get_release($args['release']) : $args['release'];
    if (!$release) {
        return new WP_Error('invalid_release', __("Invalid release object.", \ITELIC\Plugin::SLUG));
    }
    if (!empty($args['update_date'])) {
        $update_date = is_string($args['update_date']) ? \ITELIC\make_date_time($args['update_date']) : $args['update_date'];
        if (!$update_date instanceof DateTime) {
            return new WP_Error("invalid_update_date", __("Invalid update date.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $update_date = null;
    }
    return \ITELIC\Update::create($activation, $release, $update_date, $args['previous_version']);
}