Skip to content

Commit 2771a6b

Browse files
caribeswissspidy
andauthored
wp post create: Add JSON input support for tax_input (#532)
Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent bee03c5 commit 2771a6b

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

features/post.feature

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,36 @@ Feature: Manage WordPress posts
422422
post-2
423423
"""
424424

425+
Scenario: Creating/updating posts with taxonomies
426+
When I run `wp term create category "First Category" --porcelain`
427+
And save STDOUT as {CAT_1}
428+
And I run `wp term create category "Second Category" --porcelain`
429+
And save STDOUT as {CAT_2}
430+
And I run `wp term create post_tag "Term One" --porcelain`
431+
And I run `wp term create post_tag "Term Two" --porcelain`
432+
And I run `wp post create --post_title='Test Post' --post_content='Test post content' --tax_input='{"category":[{CAT_1},{CAT_2}],"post_tag":["term-one", "term-two"]}' --porcelain`
433+
Then STDOUT should be a number
434+
And save STDOUT as {POST_ID}
435+
436+
When I run `wp post term list {POST_ID} category post_tag --format=table --fields=name,taxonomy`
437+
Then STDOUT should be a table containing rows:
438+
| name | taxonomy |
439+
| First Category | category |
440+
| Second Category | category |
441+
| Term One | post_tag |
442+
| Term Two | post_tag |
443+
When I run `wp post update {POST_ID} --tax_input='{"category":[{CAT_1}],"post_tag":["term-one"]}'`
444+
Then STDOUT should contain:
445+
"""
446+
Success: Updated post {POST_ID}.
447+
"""
448+
449+
When I run `wp post term list {POST_ID} category post_tag --format=table --fields=name,taxonomy`
450+
Then STDOUT should be a table containing rows:
451+
| name | taxonomy |
452+
| First Category | category |
453+
| Term One | post_tag |
454+
425455
Scenario: Update categories on a post
426456
When I run `wp term create category "Test Category" --porcelain`
427457
Then save STDOUT as {TERM_ID}

src/Post_Command.php

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public function __construct() {
122122
* [--tax_input=<tax_input>]
123123
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
124124
*
125+
* Note: In WordPress core, this normally requires a user context to satisfy capability checks. WP-CLI bypasses this for convenience. See https://core.trac.wordpress.org/ticket/19373
126+
*
125127
* [--meta_input=<meta_input>]
126128
* : Array in JSON format of post meta values keyed by their post meta key. Default empty.
127129
*
@@ -183,7 +185,7 @@ public function create( $args, $assoc_args ) {
183185
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
184186
}
185187

186-
$array_arguments = [ 'meta_input' ];
188+
$array_arguments = [ 'meta_input', 'tax_input' ];
187189
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
188190

189191
if ( isset( $assoc_args['from-post'] ) ) {
@@ -212,7 +214,41 @@ public function create( $args, $assoc_args ) {
212214
$args,
213215
$assoc_args,
214216
function ( $params ) {
215-
return wp_insert_post( $params, true );
217+
$filter_callback = null;
218+
219+
if ( 0 === get_current_user_id() && ! empty( $params['tax_input'] ) ) {
220+
$allowed_caps = [];
221+
/**
222+
* @var string $taxonomy
223+
*/
224+
foreach ( array_keys( $params['tax_input'] ) as $taxonomy ) {
225+
$tax_obj = get_taxonomy( $taxonomy );
226+
if ( $tax_obj ) {
227+
$primitive_caps = map_meta_cap( $tax_obj->cap->assign_terms, 0 );
228+
$allowed_caps = array_merge( $allowed_caps, $primitive_caps );
229+
}
230+
}
231+
232+
if ( ! empty( $allowed_caps ) ) {
233+
$filter_callback = function ( $allcaps, $caps ) use ( $allowed_caps ) {
234+
foreach ( $caps as $cap ) {
235+
if ( in_array( $cap, $allowed_caps, true ) ) {
236+
$allcaps[ $cap ] = true;
237+
}
238+
}
239+
return $allcaps;
240+
};
241+
add_filter( 'user_has_cap', $filter_callback, 10, 2 );
242+
}
243+
}
244+
245+
$result = wp_insert_post( $params, true );
246+
247+
if ( $filter_callback ) {
248+
remove_filter( 'user_has_cap', $filter_callback );
249+
}
250+
251+
return $result;
216252
}
217253
);
218254
}
@@ -297,6 +333,8 @@ function ( $params ) {
297333
* [--tax_input=<tax_input>]
298334
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
299335
*
336+
* Note: In WordPress core, this normally requires a user context to satisfy capability checks. WP-CLI bypasses this for convenience. See https://core.trac.wordpress.org/ticket/19373
337+
*
300338
* [--meta_input=<meta_input>]
301339
* : Array in JSON format of post meta values keyed by their post meta key. Default empty.
302340
*
@@ -348,15 +386,49 @@ public function update( $args, $assoc_args ) {
348386
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
349387
}
350388

351-
$array_arguments = [ 'meta_input' ];
389+
$array_arguments = [ 'meta_input', 'tax_input' ];
352390
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
353391

354392
$assoc_args = wp_slash( $assoc_args );
355393
parent::_update(
356394
$args,
357395
$assoc_args,
358396
function ( $params ) {
359-
return wp_update_post( $params, true );
397+
$filter_callback = null;
398+
399+
if ( 0 === get_current_user_id() && ! empty( $params['tax_input'] ) ) {
400+
$allowed_caps = [];
401+
/**
402+
* @var string $taxonomy
403+
*/
404+
foreach ( array_keys( $params['tax_input'] ) as $taxonomy ) {
405+
$tax_obj = get_taxonomy( $taxonomy );
406+
if ( $tax_obj ) {
407+
$primitive_caps = map_meta_cap( $tax_obj->cap->assign_terms, 0 );
408+
$allowed_caps = array_merge( $allowed_caps, $primitive_caps );
409+
}
410+
}
411+
412+
if ( ! empty( $allowed_caps ) ) {
413+
$filter_callback = function ( $allcaps, $caps ) use ( $allowed_caps ) {
414+
foreach ( $caps as $cap ) {
415+
if ( in_array( $cap, $allowed_caps, true ) ) {
416+
$allcaps[ $cap ] = true;
417+
}
418+
}
419+
return $allcaps;
420+
};
421+
add_filter( 'user_has_cap', $filter_callback, 10, 2 );
422+
}
423+
}
424+
425+
$result = wp_update_post( $params, true );
426+
427+
if ( $filter_callback ) {
428+
remove_filter( 'user_has_cap', $filter_callback );
429+
}
430+
431+
return $result;
360432
}
361433
);
362434
}

0 commit comments

Comments
 (0)