Skip to content

Commit 21b9e01

Browse files
authored
Merge pull request #133 from wp-cli/4616-parse-shell-arrays
Implement `--meta_input` with `post create` & `post update`
2 parents 27e4798 + f61da51 commit 21b9e01

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"require": {},
2727
"require-dev": {
2828
"behat/behat": "~2.5",
29-
"wp-cli/wp-cli": "*",
29+
"wp-cli/wp-cli": "^1.5",
3030
"phpunit/phpunit": "^4.8"
3131
},
3232
"extra": {

features/post.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,23 @@ Feature: Manage WordPress posts
232232
"""
233233
var isEmailValid = /^\S+@\S+.\S+$/.test(email);
234234
"""
235+
236+
@require-wp-4.4
237+
Scenario: Creating/updating posts with meta keys
238+
When I run `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
239+
Then STDOUT should be a number
240+
And save STDOUT as {POST_ID}
241+
242+
When I run `wp post meta list {POST_ID} --format=table`
243+
Then STDOUT should be a table containing rows:
244+
| post_id | meta_key | meta_value |
245+
| {POST_ID} | key1 | value1 |
246+
| {POST_ID} | key2 | value2 |
247+
248+
When I run `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'`
249+
And I run `wp post meta list {POST_ID} --format=table`
250+
Then STDOUT should be a table containing rows:
251+
| post_id | meta_key | meta_value |
252+
| {POST_ID} | key1 | value1 |
253+
| {POST_ID} | key2 | value2b |
254+
| {POST_ID} | key3 | value3 |

src/Post_Command.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function __construct() {
112112
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
113113
*
114114
* [--meta_input=<meta_input>]
115-
* : Array of post meta values keyed by their post meta key. Default empty.
115+
* : Array in JSON format of post meta values keyed by their post meta key. Default empty.
116116
*
117117
* [<file>]
118118
* : Read post content from <file>. If this value is present, the
@@ -143,6 +143,10 @@ public function __construct() {
143143
* # Create post with content from given file
144144
* $ wp post create ./post-content.txt --post_category=201,345 --post_title='Post from file'
145145
* Success: Created post 1922.
146+
*
147+
* # Create a post with multiple meta values.
148+
* $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
149+
* Success: Created post 1923.
146150
*/
147151
public function create( $args, $assoc_args ) {
148152
if ( ! empty( $args[0] ) ) {
@@ -162,6 +166,9 @@ public function create( $args, $assoc_args ) {
162166
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
163167
}
164168

169+
$array_arguments = array( 'meta_input' );
170+
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
171+
165172
$assoc_args = wp_slash( $assoc_args );
166173
parent::_create( $args, $assoc_args, function ( $params ) {
167174
return wp_insert_post( $params, true );
@@ -249,7 +256,7 @@ public function create( $args, $assoc_args ) {
249256
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
250257
*
251258
* [--meta_input=<meta_input>]
252-
* : Array of post meta values keyed by their post meta key. Default empty.
259+
* : Array in JSON format of post meta values keyed by their post meta key. Default empty.
253260
*
254261
* [<file>]
255262
* : Read post content from <file>. If this value is present, the
@@ -268,6 +275,10 @@ public function create( $args, $assoc_args ) {
268275
*
269276
* $ wp post update 123 --post_name=something --post_status=draft
270277
* Success: Updated post 123.
278+
*
279+
* # Update a post with multiple meta values.
280+
* $ wp post update 123 --meta_input='{"key1":"value1","key2":"value2"}
281+
* Success: Updated post 123.
271282
*/
272283
public function update( $args, $assoc_args ) {
273284

@@ -285,6 +296,9 @@ public function update( $args, $assoc_args ) {
285296
$assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] );
286297
}
287298

299+
$array_arguments = array( 'meta_input' );
300+
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
301+
288302
$assoc_args = wp_slash( $assoc_args );
289303
parent::_update( $args, $assoc_args, function ( $params ) {
290304
return wp_update_post( $params, true );

0 commit comments

Comments
 (0)