function create_object($args)
 {
     $key = itelic_create_key($args);
     if (is_wp_error($key)) {
         return $key;
     }
     return $key->get_key();
 }
 /**
  * Create the key.
  */
 public function create_key()
 {
     if (!isset($_POST['itelic-add-new-key']) || !isset($_POST['_wpnonce'])) {
         return;
     }
     if (!wp_verify_nonce($_POST['_wpnonce'], 'itelic-add-new-key')) {
         $this->message[View::NOTICE_ERROR] = __("Request expired. Please try again.", Plugin::SLUG);
         return;
     }
     if (!current_user_can('manage_options')) {
         $this->message[View::NOTICE_ERROR] = __("You don't have permission to do this.", Plugin::SLUG);
         return;
     }
     if (empty($_POST['product'])) {
         $this->message[View::NOTICE_ERROR] = __("You must select a product.", Plugin::SLUG);
         return;
     }
     $product = absint($_POST['product']);
     if (empty($_POST['username'])) {
         $customer = absint($_POST['customer']);
     } else {
         $customer = wp_insert_user(array('user_login' => $_POST['username'], 'user_email' => $_POST['email'], 'first_name' => $_POST['first'], 'last_name' => $_POST['last'], 'user_pass' => wp_generate_password(24, true, true)));
         if (is_wp_error($customer)) {
             $this->message[View::NOTICE_ERROR] = $customer->get_error_message();
             return;
         }
     }
     $activations = intval($_POST['activations']);
     $expiration = \ITELIC\make_local_time($_POST['expiration']);
     $key = $_POST['license'];
     $paid = $_POST['paid'];
     $args = array('key' => $key, 'product' => $product, 'customer' => $customer, 'paid' => $paid, 'limit' => $activations, 'expires' => \ITELIC\convert_local_to_gmt($expiration));
     /**
      * Filters the args used to create a new license key.
      *
      * @since 1.0
      *
      * @param array $args
      */
     $args = apply_filters('itelic_add_new_license_args', $args);
     $key = itelic_create_key($args);
     /**
      * Fires when a new license key is created from the add new form.
      *
      * @since 1.0
      *
      * @param Key $key
      */
     do_action('itelic_add_new_license_save', $key);
     if (is_wp_error($key)) {
         $this->message[View::NOTICE_ERROR] = $key->get_error_message();
     } else {
         /**
          * Determine whether or not to send WP's new user notification
          * when a key is manually created from the add new license key page.
          *
          * @since 1.0
          *
          * @param bool $send
          * @param Key  $key
          */
         $send_notification = apply_filters('itelic_add_new_license_send_new_user_notification', true, $key);
         if ($send_notification) {
             wp_new_user_notification($customer, null, 'both');
         }
         wp_redirect(itelic_get_admin_edit_key_link($key->get_key()));
         exit;
     }
 }
 /**
  * Generate license keys.
  *
  * ## Options
  *
  * [--count=<count>]
  * : Number of keys to generate. Default 500. Max 750.
  *
  * [--product=<product>]
  * : Only generate keys for a certain product.
  *
  * [--activations]
  * : Generate activations for license keys.
  *
  * @param $args
  * @param $assoc_args
  */
 public function generate($args, $assoc_args)
 {
     if ($product = \WP_CLI\Utils\get_flag_value($assoc_args, 'product')) {
         $product = itelic_get_product($product);
         if (!$product) {
             WP_CLI::error("Invalid product.");
         }
         $products = array($product->ID);
     } else {
         $products = wp_list_pluck(itelic_get_products_with_licensing_enabled(), 'ID');
     }
     $count = \WP_CLI\Utils\get_flag_value($assoc_args, 'count', 500);
     $notify = \WP_CLI\Utils\make_progress_bar("Generating keys", $count);
     for ($i = 0; $i < $count; $i++) {
         $product = $this->get_product($products);
         $customer = $this->get_random_customer();
         $min_date = max(strtotime($product->post_date), strtotime($customer->wp_user->user_registered));
         $date = $this->faker->dateTimeBetween("@{$min_date}");
         $key_args = array('product' => $product->ID, 'customer' => $customer->id, 'date' => $date->format('Y-m-d H:i:s'), 'status' => $this->get_status(), 'paid' => $product->get_feature('base-price'));
         $key = itelic_create_key($key_args);
         if (is_wp_error($key)) {
             WP_CLI::error($key);
         }
         if (\WP_CLI\Utils\get_flag_value($assoc_args, 'activations')) {
             $this->create_activations_for_key($key);
         }
         if ($key->get_status() == \ITELIC\Key::EXPIRED) {
             $key->expire($key->get_expires());
         }
         $notify->tick();
     }
     $notify->finish();
 }