/**
  * Init an object.
  *
  * @since 1.0
  *
  * @param \stdClass $data
  */
 protected function init(\stdClass $data)
 {
     $this->ID = $data->ID;
     $this->activation = itelic_get_activation($data->activation);
     $this->release = itelic_get_release($data->release_id);
     $this->update_date = make_date_time($data->update_date);
     $this->previous_version = $data->previous_version;
 }
/**
 * 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']);
}
 /**
  * Initialize this object.
  *
  * @param \stdClass $data
  */
 protected function init(\stdClass $data)
 {
     $this->id = $data->id;
     $this->key = itelic_get_key($data->lkey);
     $this->location = $data->location;
     $this->status = $data->status;
     $this->activation = make_date_time($data->activation);
     if (!empty($data->deactivation) && $data->deactivation != '0000-00-00 00:00:00') {
         $this->deactivation = make_date_time($data->deactivation);
     }
     $this->release = itelic_get_release($data->release_id);
 }
 /**
  * Get the chart for this report.
  *
  * @since 1.0
  *
  * @param string $date_type
  * @param int    $product
  *
  * @return Chart
  */
 public function get_chart($date_type = 'this_year', $product = 0)
 {
     if (!$product) {
         return null;
     }
     /**
      * @var \wpdb $wpdb
      */
     global $wpdb;
     $atn = Manager::get('itelic-activations')->get_table_name($wpdb);
     $ktn = Manager::get('itelic-keys')->get_table_name($wpdb);
     $raw = "SELECT COUNT(1) as c, `release_id` as d FROM {$atn} a JOIN {$ktn} k ON (k.lkey = a.lkey AND k.product = %d)\n\t\t\t\tWHERE a.status = %s GROUP BY `release_id` LIMIT 5";
     $results = $wpdb->get_results($wpdb->prepare($raw, $product, Activation::ACTIVE));
     $translated = self::translate_results($results);
     if (isset($translated[''])) {
         $unknown = $translated[''];
         $translated[__('Unknown', Plugin::SLUG)] = $unknown;
         unset($translated['']);
     }
     $colors = array(array('color' => '#E94F37', 'highlight' => '#FF6951'), array('color' => '#393E41', 'highlight' => '#53585B'), array('color' => '#3F88C5', 'highlight' => '#59A2DF'), array('color' => '#44BBA4', 'highlight' => '#5ED5BE'), array('color' => '#EDDDD4', 'highlight' => '#D4C4BB'));
     $chart = new Pie(600, 200, array('ibdShowLegend' => '#legend-' . $this->get_slug(), 'responsive' => true, 'tooltipTemplate' => '<%= value %> install<%if (value != 1){%>s<%}%>'));
     $i = 0;
     foreach ($translated as $label => $value) {
         if ($label != __('Unknown', Plugin::SLUG)) {
             $release = itelic_get_release($label);
             $label = $release->get_version();
             $label = "v{$label}";
         }
         $chart->add_data_set($value, $label, $colors[$i]);
         $i++;
     }
     return $chart;
 }
 /**
  * Activate the initial release when the product is published.
  *
  * @since 1.0
  *
  * @param string   $new_status
  * @param string   $old_status
  * @param \WP_Post $post
  */
 public function activate_initial_release($new_status, $old_status, $post)
 {
     $first_release = get_post_meta($post->ID, '_itelic_first_release', true);
     if (!$first_release) {
         return;
     }
     if ($new_status == 'publish' && $old_status != 'publish') {
         $release = itelic_get_release($first_release);
         if ($release && $release->get_status() !== Release::STATUS_ACTIVE) {
             $release->activate();
         }
     }
 }
 /**
  * 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 an activation record.
 *
 * @api
 *
 * @since 1.0
 *
 * @param array $args
 *
 * @return \ITELIC\Activation|WP_Error
 */
function itelic_create_activation($args)
{
    $defaults = array('key' => '', 'location' => '', 'activation' => '', 'release' => '', 'status' => '', 'track' => 'stable');
    $args = ITUtility::merge_defaults($args, $defaults);
    $key = is_string($args['key']) ? itelic_get_key($args['key']) : $args['key'];
    if (!$key) {
        return new WP_Error('invalid_key', __("Invalid Key", \ITELIC\Plugin::SLUG));
    }
    $location = $args['location'];
    if (!empty($args['activation'])) {
        if (is_string($args['activation'])) {
            $activation = \ITELIC\make_date_time($args['activation']);
        } else {
            $activation = $args['activation'];
        }
        if (!$activation instanceof DateTime) {
            return new WP_Error('invalid_activation', __("Invalid activation date.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $activation = null;
    }
    if (!empty($args['release'])) {
        if (is_string($args['release'])) {
            $release = itelic_get_release($args['release']);
        } else {
            $release = $args['release'];
        }
        if (!$release instanceof \ITELIC\Release) {
            return new WP_Error('invalid_release', __("Invalid release.", \ITELIC\Plugin::SLUG));
        }
    } else {
        $release = null;
    }
    $status = $args['status'];
    try {
        $activation = \ITELIC\Activation::create($key, $location, $activation, $release, $status);
        $activation->add_meta('track', $args['track']);
    } catch (Exception $e) {
        return new WP_Error('exception', $e->getMessage());
    }
    return $activation;
}
 function get_object_by_id($object_id)
 {
     return itelic_get_release($object_id);
 }
 /**
  * Generate releases.
  *
  * @param $args
  * @param $assoc_args
  */
 public function generate($args, $assoc_args)
 {
     $products = itelic_get_products_with_licensing_enabled();
     $notify = \WP_CLI\Utils\make_progress_bar('Generating Releases', count($products));
     $now = new DateTime();
     foreach ($products as $product) {
         $major = get_post_meta($product->ID, '_itelic_first_release', true);
         $major = itelic_get_release($major);
         $minor_releases = rand(3, 9);
         $last = $major;
         while ($last->get_start_date() < $now) {
             for ($i = 0; $i < $minor_releases; $i++) {
                 // this exponentially spaces out minor releases
                 $days = rand($i ^ 2, $i + 1 ^ 2);
                 $start = $last->get_start_date()->add(new DateInterval("P{$days}D"));
                 // we don't want to create any releases in the future
                 if ($start > $now) {
                     break 2;
                 }
                 $last = $this->generate_minor_release($last);
                 $last->activate($start);
             }
             // between 3 and 7 weeks after a minor release to a major release
             $days_before_major = rand(3 * 7, 7 * 7);
             $start = $last->get_start_date()->add(new DateInterval("P{$days_before_major}D"));
             if ($start > $now) {
                 break;
             }
             $last = $this->generate_major_release($last);
             $last->activate($start);
         }
         $notify->tick();
     }
     $notify->finish();
 }
 /**
  * Enqueue scripts and styles.
  *
  * @since 1.0
  */
 private function enqueue()
 {
     $release = itelic_get_release($_GET['ID']);
     wp_enqueue_style('itelic-admin-releases-edit');
     wp_enqueue_script('itelic-admin-releases-edit');
     wp_localize_script('itelic-admin-releases-edit', 'ITELIC', array('prevVersion' => __("Previous version: %s", Plugin::SLUG), 'uploadTitle' => __("Choose Software File", Plugin::SLUG), 'uploadButton' => __("Replace File", Plugin::SLUG), 'uploadLabel' => __("Upload File", Plugin::SLUG), 'lessUpgrade' => __("Less", Plugin::SLUG), 'moreUpgrade' => __("More", Plugin::SLUG), 'saving' => __("Saving", Plugin::SLUG), 'ibdLoadOn' => 'loadCharts', 'statuses' => Release::get_statuses(), 'types' => Release::get_types(true), 'release' => $_GET['ID'], 'update_nonce' => wp_create_nonce('itelic-update-release-' . $_GET['ID']), 'ok' => __("Ok", Plugin::SLUG), 'cancel' => __("Cancel", Plugin::SLUG), 'currentVersion' => $release ? $release->get_product()->get_feature('licensing', array('field' => 'version')) : ''));
     wp_enqueue_media();
 }