/** * Create a post. * * ## OPTIONS * * [<file>] * : Read post content from <file>. If this value is present, the * `--post_content` argument will be ignored. * * Passing `-` as the filename will cause post content to * be read from STDIN. * * [--<field>=<value>] * : Associative args for the new post. See wp_insert_post(). * * [--edit] * : Immediately open system's editor to write or edit post content. * * If content is read from a file, from STDIN, or from the `--post_content` * argument, that text will be loaded into the editor. * * [--porcelain] * : Output just the new post id. * * ## EXAMPLES * * wp post create --post_type=page --post_title='A future post' --post_status=future --post_date='2020-12-01 07:00:00' * * wp post create ./post-content.txt --post_category=201,345 --post_title='Post from file' */ public function create($args, $assoc_args) { if (!empty($args[0])) { if ($args[0] !== '-') { $readfile = $args[0]; if (!file_exists($readfile) || !is_file($readfile)) { \WP_CLI::error("Unable to read content from {$readfile}."); } } else { $readfile = 'php://stdin'; } $assoc_args['post_content'] = file_get_contents($readfile); } if (isset($assoc_args['edit'])) { $input = isset($assoc_args['post_content']) ? $assoc_args['post_content'] : ''; if ($output = $this->_edit($input, 'WP-CLI: New Post')) { $assoc_args['post_content'] = $output; } else { $assoc_args['post_content'] = $input; } } if (isset($assoc_args['post_category'])) { $assoc_args['post_category'] = explode(',', $assoc_args['post_category']); } parent::_create($args, $assoc_args, function ($params) { return wp_insert_post($params, true); }); }
/** * Insert a comment. * * ## OPTIONS * * --<field>=<value> * : Associative args for the new comment. See wp_insert_comment(). * * [--porcelain] * : Output just the new comment id. * * ## EXAMPLES * * wp comment create --comment_post_ID=15 --comment_content="hello blog" --comment_author="wp-cli" */ public function create($args, $assoc_args) { parent::_create($args, $assoc_args, function ($params) { $post_id = $params['comment_post_ID']; $post = get_post($post_id); if (!$post) { return new WP_Error('no_post', "Can't find post {$post_id}."); } // We use wp_insert_comment() instead of wp_new_comment() to stay at a low level and // avoid wp_die() formatted messages or notifications $comment_id = wp_insert_comment($params); if (!$comment_id) { return new WP_Error('db_error', 'Could not create comment.'); } return $comment_id; }); }
/** * Create a post. * * ## OPTIONS * * [<file>] * : Read post content from <file>. If this value is present, the * `--post_content` argument will be ignored. * * Passing `-` as the filename will cause post content to * be read from STDIN. * * [--<field>=<value>] * : Associative args for the new post. See wp_insert_post(). * * [--edit] * : Immediately open system's editor to write or edit post content. * * If content is read from a file, from STDIN, or from the `--post_content` * argument, that text will be loaded into the editor. * * [--porcelain] * : Output just the new post id. * * ## EXAMPLES * * wp post create --post_type=page --post_title='A future post' --post_status=future --post_date='2020-12-01 07:00:00' * * wp post create ./post-content.txt --post_category=201,345 --post_title='Post from file' */ public function create($args, $assoc_args) { if (!empty($args[0])) { $assoc_args['post_content'] = $this->read_from_file_or_stdin($args[0]); } if (\WP_CLI\Utils\get_flag_value($assoc_args, 'edit')) { $input = \WP_CLI\Utils\get_flag_value($assoc_args, 'post_content', ''); if ($output = $this->_edit($input, 'WP-CLI: New Post')) { $assoc_args['post_content'] = $output; } else { $assoc_args['post_content'] = $input; } } if (isset($assoc_args['post_category'])) { $assoc_args['post_category'] = explode(',', $assoc_args['post_category']); } parent::_create($args, $assoc_args, function ($params) { return wp_insert_post($params, true); }); }
/** * Create a product with licensing enabled. * * ## Options * * <title> * : Product Title * * <price> * : Product price. Formatted as a float. * * --file=<file> * : Attachment ID used for download * * --limit=<limit> * : Activation limit. Variants are not supported. Pass 0 for unlimited. * * [--key-type=<key-type>] * : Key type. Default: random. * * [--online-software=<online-software>] * : Enable online software tools like remote deactivation. Default: true * * [--version=<version>] * : Initial version. Default: 1.0 * * [--description=<description>] * : Short product description. 3-5 sentences max. * * @param $args * @param $assoc_args */ public function create($args, $assoc_args) { list($title, $price) = $args; $fn = array($this, 'create_product'); parent::_create($args, $assoc_args, function ($params) use($title, $price, $fn) { return call_user_func($fn, $title, $price, $params); }); }
/** * Create a license key. * * Requires manual purchases add-on. * * ## Options * * <product> * : Product ID. * * <customer> * : Customer ID * * [<key>] * : Optionally, specify the license key to be used. * * [--limit=<limit>] * : Activation limit. Defaults to lowest value available. * Set to '-' for unlimited. * * [--amount-paid=<amount-paid>] * : The amount the customer paid for this key. Defaults to the product * base price. * * [--expires=<expires>] * : License key expiry date. * Default: forever. Accepts strtotime compatible value. GMT. * * [--status=<status>] * : Key status. Accepts: active, expired, disabled. Default: active * * @param $args * @param $assoc_args */ public function create($args, $assoc_args) { list($ID, $customer, $key) = array_pad($args, 3, ''); $product = itelic_get_product($ID); if (!$product || !$product->has_feature('licensing')) { WP_CLI::error("Invalid product."); } $customer = it_exchange_get_customer($customer); if (!$customer) { WP_CLI::error("Invalid customer."); } $create_args = array('product' => $product->ID, 'customer' => $customer->id, 'key' => $key, 'status' => \WP_CLI\Utils\get_flag_value($assoc_args, 'status', \ITELIC\Key::ACTIVE)); if (isset($assoc_args['limit'])) { $create_args['limit'] = $assoc_args['limit']; } if (isset($assoc_args['expires'])) { $create_args['expires'] = $assoc_args['expires']; } if (isset($assoc_args['amount-paid'])) { $create_args['paid'] = $assoc_args['amount-paid']; } else { $create_args['paid'] = $product->get_feature('base-price'); } parent::_create($args, $assoc_args, function () use($create_args) { try { $key = itelic_create_key($create_args); if (!$key) { WP_CLI::error("Unknown error occurred."); } return $key->get_pk(); } catch (Exception $e) { WP_CLI::error($e->getMessage()); } WP_CLI::error("Unknown error occurred."); }); }
/** * Create a release. * * [<file>] * : Read changelog from <file> Passing `-` as the filename will cause * changelog to be read from STDIN * * --product=<product> * : ID of the product being released. * * --version=<version> * : Version number of the release being created. * * --file=<file> * : ID of the attachment being used for the software download. * * --type=<type> * : Type of release. Accepted values: major, minor, security, pre-release * * [--status=<status>] * : Status of the release. Accepted values: draft, active. Default: draft * * [--changelog=<changelog>] * : What changed in this release. Version number and date omitted. HTML * allowed. * * [--edit] * : Immediately open system's editor to write or edit changelog. * * [--porcelain] * : Output just the new release ID. * * @param $args * @param $assoc_args */ public function create($args, $assoc_args) { if (!empty($args[0])) { $assoc_args['changelog'] = $this->read_from_file_or_stdin($args[0]); } if (\WP_CLI\Utils\get_flag_value($assoc_args, 'edit')) { $input = \WP_CLI\Utils\get_flag_value($assoc_args, 'post_content', ''); $output = \WP_CLI\Utils\launch_editor_for_input($input, 'WP-CLI: New Release Changelog'); if ($output) { $assoc_args['changelog'] = $output; } else { $assoc_args['changelog'] = $input; } } $assoc_args = wp_parse_args($assoc_args, array('status' => \ITELIC\Release::STATUS_DRAFT, 'changelog' => '')); parent::_create($args, $assoc_args, function ($params) { $product = itelic_get_product($params['product']); if (!$product) { WP_CLI::error('Invalid product ID.'); } $version = $params['version']; $file = get_post($params['file']); if (!$file) { WP_CLI::error('Invalid post ID for download file.'); } $type = $params['type']; $status = $params['status']; if (!in_array($status, array(\ITELIC\Release::STATUS_ACTIVE, \ITELIC\Release::STATUS_DRAFT))) { WP_CLI::error("Invalid status."); } $changelog = $params['changelog']; 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; } return $release->get_pk(); } catch (Exception $e) { WP_CLI::error($e->getMessage()); } }); }
/** * Activate a release. * * ## OPTIONS * * <location> * : Where the software is being activated. Typically a website. * * <key> * : The key being activated. * * [--when=<when>] * : Wen the activation occurred. Accepts strtotime compatible * value. GMT. * * [--version=<version>] * : The version of the software installed. Default: latest. * * [--track=<track>] * : Accepted values: stable, pre-release. Default: stable * * [--porcelain] * : Output just the new activation ID. * * @param $args * @param $assoc_args */ public function activate($args, $assoc_args) { list($location, $key) = $args; $key = itelic_get_key($key); if (!$key) { WP_CLI::error("Invalid key."); } if ($key->get_status() != ITELIC\Key::ACTIVE) { WP_CLI::error(sprintf("Key has a status of '%s' not 'active'.", $key->get_status())); } if (isset($assoc_args['when'])) { $when = \ITELIC\make_date_time($assoc_args['when']); } else { $when = null; } if (isset($assoc_args['version'])) { $release = itelic_get_release_by_version($key->get_product()->ID, $assoc_args['version']); if (!$release) { WP_CLI::error(sprintf("Invalid release ID %d.", $assoc_args['release'])); } } else { $release = null; } if (isset($assoc_args['track'])) { if (in_array($assoc_args['track'], array('stable', 'pre-release'))) { $track = $assoc_args['track']; } else { WP_CLI::error("Invalid value '%s' for track."); } } else { $track = 'stable'; } parent::_create($args, $assoc_args, function () use($location, $key, $when, $release, $track) { $a = itelic_activate_license_key($key, $location, $when, $release, $track); if ($a) { return $a->get_pk(); } return new WP_Error(); }); }