function create_object($args)
 {
     $activation = itelic_create_release($args);
     if (is_wp_error($activation)) {
         return $activation;
     }
     return $activation->get_pk();
 }
 /**
  * Create a product.
  *
  * @param string $title
  * @param float  $price
  * @param array  $params
  *
  * @return WP_Error|int
  */
 protected function create_product($title, $price, $params)
 {
     $file = get_post($params['file']);
     if (get_post_type($file) != 'attachment') {
         return new WP_Error('invalid_file', "Invalid file. Post type is not attachment.");
     }
     $limit = (int) $params['limit'];
     if (empty($limit)) {
         $limit = '';
     }
     $key_type = \WP_CLI\Utils\get_flag_value($params, 'key-type', 'random');
     $online_software = \WP_CLI\Utils\get_flag_value($params, 'online-software', 'true');
     $version = \WP_CLI\Utils\get_flag_value($params, 'version', '1.0');
     $description = \WP_CLI\Utils\get_flag_value($params, 'description', '');
     try {
         $product = it_exchange_add_product(array('type' => 'digital-downloads-product-type', 'title' => $title, 'base-price' => $price, 'description' => $description, 'show_in_store' => true));
     } catch (Exception $e) {
         WP_CLI::error($e->getMessage());
     }
     if (!$product) {
         return new WP_Error('product_error', 'Product not created.');
     }
     $product = itelic_get_product($product);
     $faker = \Faker\Factory::create();
     $new_date = $faker->dateTimeBetween('-2 years', '-1 months')->format('Y-m-d H:i:s');
     wp_update_post(array('ID' => $product->ID, 'post_date' => $new_date, 'post_date_gmt' => get_gmt_from_date($new_date)));
     // refresh object to get new dates
     $product = itelic_get_product($product->ID);
     $download_data = array('product_id' => $product->ID, 'source' => wp_get_attachment_url($file->ID), 'name' => $file->post_title);
     $product->update_feature('downloads', $download_data);
     $downloads = $product->get_feature('downloads');
     $download_id = key($downloads);
     $feature_data = array('enabled' => true, 'online-software' => $online_software == 'true' ? true : false, 'limit' => $limit, 'key-type' => $key_type, 'update-file' => $download_id, 'version' => $version);
     $product->update_feature('licensing', $feature_data);
     if (isset($params['interval'])) {
         $product->update_feature('recurring-payments', 'on');
         $product->update_feature('recurring-payments', $params['interval'], array('setting' => 'interval'));
         $product->update_feature('recurring-payments', $params['interval-count'], array('setting' => 'interval-count'));
     }
     $type = \ITELIC\Release::TYPE_MAJOR;
     $status = \ITELIC\Release::STATUS_PAUSED;
     $changelog = '<ul><li>' . __("Initial release.", \ITELIC\Plugin::SLUG) . '</li></ul>';
     try {
         $args = array('product' => $product, 'file' => $file, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => $changelog);
         $release = itelic_create_release($args);
         if (is_wp_error($release)) {
             return $release;
         }
         $when = \ITELIC\make_date_time($product->post_date_gmt);
         $release->activate($when);
     } catch (Exception $e) {
         return new WP_Error('release_exception', $e->getMessage());
     }
     update_post_meta($product->ID, '_itelic_first_release', $release->get_pk());
     return $product->ID;
 }
 /**
  * This saves the value.
  *
  * @since 1.0
  */
 public function save_feature_on_product_save()
 {
     // Abort if we don't have a product ID
     $product_id = empty($_POST['ID']) ? false : $_POST['ID'];
     if (!$product_id) {
         return;
     }
     $prev = it_exchange_get_product_feature($product_id, $this->slug);
     $data = $_POST['itelic'];
     $data['enabled'] = isset($data['enabled']) ? it_exchange_str_true($data['enabled']) : false;
     $data['enabled_variant_activations'] = isset($data['enabled_variant_activations']) ? it_exchange_str_true($data['enabled_variant_activations']) : false;
     $data['online-software'] = isset($data['online-software']) ? it_exchange_str_true($data['online-software']) : false;
     if (!empty($prev['version'])) {
         unset($data['version']);
     }
     $first_release = get_post_meta($product_id, '_itelic_first_release', true);
     if (!$first_release && isset($data['update-file']) && $data['update-file']) {
         $download = $data['update-file'];
         $download_meta = get_post_meta($download, '_it-exchange-download-info', true);
         if (empty($download_meta)) {
             return;
         }
         $url = $download_meta['source'];
         /**
          * @var \wpdb $wpdb
          */
         global $wpdb;
         $ID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid = %s", $url));
         $file = get_post($ID);
         if ($file->post_type == 'attachment') {
             $product = itelic_get_product($product_id);
             if (isset($data['version'])) {
                 $version = $data['version'];
             } elseif (isset($prev['version'])) {
                 $version = $prev['version'];
             } else {
                 $version = '';
             }
             $type = Release::TYPE_MAJOR;
             $status = get_post_status($product_id) == 'publish' ? Release::STATUS_ACTIVE : Release::STATUS_DRAFT;
             $changelog = '<ul><li>' . __("Initial release.", Plugin::SLUG) . '</li></ul>';
             if ($version) {
                 try {
                     $args = array('product' => $product, 'file' => $file, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => $changelog);
                     $release = itelic_create_release($args);
                     if ($release && !is_wp_error($release)) {
                         update_post_meta($product_id, '_itelic_first_release', $release->get_pk());
                     }
                 } catch (\Exception $e) {
                 }
             }
         }
     }
     it_exchange_update_product_feature($product_id, $this->slug, $data);
 }
 /**
  * Save post data to a new release.
  *
  * @since 1.0
  */
 public function save_new_release()
 {
     if (!isset($_POST['itelic-action']) || $_POST['itelic-action'] != 'add-new-release') {
         return;
     }
     if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'itelic-add-new-release')) {
         $this->errors[] = __("Request expired. Please try again.", Plugin::SLUG);
         return;
     }
     if (!isset($_POST['type-select']) || !array_key_exists($_POST['type-select'], Release::get_types())) {
         $this->errors[] = __("Invalid release type selected.", Plugin::SLUG);
         return;
     }
     $type = $_POST['type-select'];
     if (empty($_POST['product'])) {
         $this->errors[] = __("You must select a product.", Plugin::SLUG);
         return;
     }
     try {
         $product = itelic_get_product($_POST['product']);
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
         return;
     }
     if (!$product->has_feature('licensing')) {
         $this->errors[] = __("Product selected does not support licensing.", Plugin::SLUG);
         return;
     }
     if (empty($_POST['version'])) {
         $this->errors[] = __("Invalid version number entered.", Plugin::SLUG);
         return;
     }
     $version = sanitize_text_field($_POST['version']);
     if (empty($_POST['upload-file'])) {
         $this->errors[] = __("No software file selected.", Plugin::SLUG);
         return;
     }
     $attachment = get_post($_POST['upload-file']);
     $changelog = empty($_POST['whats-changed']) ? '' : stripslashes($_POST['whats-changed']);
     $security_message = empty($_POST['security-message']) ? '' : stripslashes($_POST['security-message']);
     if (!current_user_can('unfiltered_html')) {
         $changelog = wp_kses($changelog, wp_kses_allowed_html());
         $security_message = wp_kses($security_message, wp_kses_allowed_html());
     }
     $action = isset($_POST['release']) && $_POST['release'] ? 'release' : 'draft';
     $status = 'release' == $action ? Release::STATUS_ACTIVE : Release::STATUS_DRAFT;
     try {
         $args = array('product' => $product, 'file' => $attachment, 'version' => $version, 'type' => $type, 'status' => $status, 'changelog' => $changelog, 'security-message' => $security_message);
         /**
          * Filters the add new release args.
          *
          * @since 1.0
          *
          * @param array $args
          */
         $args = apply_filters('itelic_add_new_release_args', $args);
         $release = itelic_create_release($args);
         if (is_wp_error($release)) {
             $this->errors[] = $release->get_error_message();
             return;
         }
         if ($release) {
             /**
              * Fires when a new release is saved.
              *
              * @since 1.0
              *
              * @param Release $release
              */
             do_action('itelic_add_new_release_save', $release);
             $url = add_query_arg('new', true, itelic_get_admin_edit_release_link($release->get_pk()));
             wp_redirect($url);
             die;
         }
     } catch (\Exception $e) {
         $this->errors[] = $e->getMessage();
     }
 }
 /**
  * Generate a minor release.
  *
  * @param \ITELIC\Release $latest
  *
  * @return \ITELIC\Release|null
  */
 protected function generate_minor_release(\ITELIC\Release $latest)
 {
     $product = $latest->get_product();
     $type = \ITELIC\Release::TYPE_MINOR;
     $version = \WP_CLI\Utils\increment_version($latest->get_version(), 'minor');
     $download = $latest->get_download();
     $name = $product->post_title . "-{$version}.zip";
     $file = itelic_rename_file($download, $name);
     $fixes = rand(3, 15);
     $tweaks = rand(1, 10);
     $adds = rand(0, 2);
     $changelog = $this->generate_changelog($adds, $tweaks, $fixes);
     return itelic_create_release(array('product' => $product, 'file' => $file, 'version' => $version, 'type' => $type, 'changelog' => $changelog));
 }