Skip to content
Merged
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"wp-cli/wp-cli": "^2.12"
"wp-cli/wp-cli": "^2.13"
},
"require-dev": {
"wp-cli/cache-command": "^1 || ^2",
Expand Down Expand Up @@ -231,6 +231,7 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpunit": "run-php-unit-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpstan": "run-phpstan-tests",
Expand Down
24 changes: 2 additions & 22 deletions entity-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,7 @@
);
WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );
WP_CLI::add_command( 'term', 'Term_Command' );
WP_CLI::add_command(
'term meta',
'Term_Meta_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '4.4', '<' ) ) {
WP_CLI::error( 'Requires WordPress 4.4 or greater.' );
}
},
)
);
WP_CLI::add_command( 'term meta', 'Term_Meta_Command' );
WP_CLI::add_command( 'user', 'User_Command' );
WP_CLI::add_command(
'user application-password',
Expand All @@ -84,17 +74,7 @@
)
);
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
WP_CLI::add_command(
'user session',
'User_Session_Command',
array(
'before_invoke' => function () {
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
WP_CLI::error( 'Requires WordPress 4.0 or greater.' );
}
},
)
);
WP_CLI::add_command( 'user session', 'User_Session_Command' );
WP_CLI::add_command( 'user term', 'User_Term_Command' );

if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {
Expand Down
34 changes: 0 additions & 34 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -451,40 +451,6 @@ Feature: Manage WordPress posts
Test Post
"""

@less-than-wp-4.4
Scenario: Creating/updating posts with meta keys for WP < 4.4 has no effect so should give warning
When I try `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain`
Then the return code should be 0
And STDOUT should be a number
And save STDOUT as {POST_ID}
And STDERR should be:
"""
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
"""

When I run `wp post meta list {POST_ID} --format=count`
Then STDOUT should be:
"""
0
"""

When I try `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'`
Then the return code should be 0
And STDERR should be:
"""
Warning: The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect.
"""
And STDOUT should be:
"""
Success: Updated post {POST_ID}.
"""

When I run `wp post meta list {POST_ID} --format=count`
Then STDOUT should be:
"""
0
"""

Scenario: Publishing a post and setting a date fails if the edit_date flag is not passed.
Given a WP install

Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
level: 9
paths:
- src
- entity-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
48 changes: 27 additions & 21 deletions src/Comment_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function get( $args, $assoc_args ) {
}

if ( ! isset( $comment->url ) ) {
// @phpstan-ignore property.notFound
$comment->url = get_comment_link( $comment );
}

Expand Down Expand Up @@ -376,29 +377,27 @@ public function list_( $args, $assoc_args ) {
$assoc_args['count'] = true;
}

if ( ! empty( $assoc_args['comment__in'] )
&& ! empty( $assoc_args['orderby'] )
&& 'comment__in' === $assoc_args['orderby']
&& Utils\wp_version_compare( '4.4', '<' ) ) {
$comments = [];
foreach ( $assoc_args['comment__in'] as $comment_id ) {
$comment = get_comment( $comment_id );
if ( $comment ) {
$comments[] = $comment;
} else {
WP_CLI::warning( "Invalid comment {$comment_id}." );
}
}
} else {
$query = new WP_Comment_Query();
$comments = $query->query( $assoc_args );
}
$query = new WP_Comment_Query();
$comments = $query->query( $assoc_args );

if ( 'count' === $formatter->format ) {
/**
* @var int $comments
*/
echo $comments;
return;
} else {
/**
* @var array $comments
*/

if ( 'ids' === $formatter->format ) {
$comments = wp_list_pluck( $comments, 'comment_ID' );
/**
* @var \WP_Comment[] $comments
*/
$items = wp_list_pluck( $comments, 'comment_ID' );

$comments = $items;
} elseif ( is_array( $comments ) ) {
$comments = array_map(
function ( $comment ) {
Expand Down Expand Up @@ -439,7 +438,7 @@ public function delete( $args, $assoc_args ) {
$args,
$assoc_args,
function ( $comment_id, $assoc_args ) {
$force = Utils\get_flag_value( $assoc_args, 'force' );
$force = (bool) Utils\get_flag_value( $assoc_args, 'force' );

$status = wp_get_comment_status( $comment_id );
$result = wp_delete_comment( $comment_id, $force );
Expand All @@ -457,6 +456,9 @@ function ( $comment_id, $assoc_args ) {
private function call( $args, $status, $success, $failure ) {
$comment_id = absint( $args );

/**
* @var callable $func
*/
$func = "wp_{$status}_comment";

if ( ! $func( $comment_id ) ) {
Expand Down Expand Up @@ -642,16 +644,17 @@ public function unapprove( $args, $assoc_args ) {
* total_comments: 19
*/
public function count( $args, $assoc_args ) {
$post_id = Utils\get_flag_value( $args, 0, 0 );
$post_id = $args[0] ?? null;

$count = wp_count_comments( $post_id );

// Move total_comments to the end of the object
$total = $count->total_comments;
unset( $count->total_comments );
// @phpstan-ignore assign.propertyReadOnly
$count->total_comments = $total;

foreach ( $count as $status => $count ) {
foreach ( (array) $count as $status => $count ) {
WP_CLI::line( str_pad( "$status:", 17 ) . $count );
}
}
Expand All @@ -673,6 +676,9 @@ public function count( $args, $assoc_args ) {
public function recount( $args ) {
foreach ( $args as $id ) {
if ( wp_update_comment_count( $id ) ) {
/**
* @var \WP_Post $post
*/
$post = get_post( $id );
WP_CLI::log( "Updated post {$post->ID} comment count to {$post->comment_count}." );
} else {
Expand Down
10 changes: 7 additions & 3 deletions src/Comment_Meta_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_va
* specified.
*
* @return mixed Single metadata value, or array of values.
*
* @phpstan-return ($single is true ? string : $meta_key is "" ? array<array<string>> : array<string>)
*/
protected function get_metadata( $object_id, $meta_key = '', $single = false ) {
// @phpstan-ignore return.type
return get_comment_meta( $object_id, $meta_key, $single );
}

Expand All @@ -104,11 +107,12 @@ protected function delete_metadata( $object_id, $meta_key, $meta_value = '' ) {
/**
* Check that the comment ID exists
*
* @param int
* @param string|int $object_id
* @return int|never
*/
protected function check_object_id( $object_id ) {
$fetcher = new CommentFetcher();
$comment = $fetcher->get_check( $object_id );
return $comment->comment_ID;
$comment = $fetcher->get_check( (string) $object_id );
return (int) $comment->comment_ID;
}
}
3 changes: 2 additions & 1 deletion src/Menu_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function create( $args, $assoc_args ) {

} elseif ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {

WP_CLI::line( $menu_id );
WP_CLI::line( (string) $menu_id );
} else {
WP_CLI::success( "Created menu {$menu_id}." );
}
Expand Down Expand Up @@ -166,6 +166,7 @@ public function list_( $args, $assoc_args ) {
$menu_locations = get_nav_menu_locations();
foreach ( $menus as &$menu ) {

// @phpstan-ignore property.notFound
$menu->locations = [];
foreach ( $menu_locations as $location => $term_id ) {

Expand Down
22 changes: 16 additions & 6 deletions src/Menu_Item_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Menu_Item_Command extends WP_CLI_Command {
public function list_( $args, $assoc_args ) {

$items = wp_get_nav_menu_items( $args[0] );
if ( false === $items || is_wp_error( $items ) ) {
if ( false === $items ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it impossible here to end up with a WP_Error? Relying on the docblocks is not enough, and I'm seeing that multiple filters are involved... 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no code path in there that returns WP_Error, I already checked.

Filters can always be abused, but we can‘t check for every possible return value.

git blame was unfortunately not helpful here (mangled history)

WP_CLI::error( 'Invalid menu.' );
}

Expand Down Expand Up @@ -367,8 +367,10 @@ public function delete( $args, $assoc_args ) {

foreach ( $args as $arg ) {

$post = get_post( $arg );
$menu_term = get_the_terms( $arg, 'nav_menu' );
$post = get_post( $arg );
$menu_term = get_the_terms( $arg, 'nav_menu' );

// @phpstan-ignore cast.int
$parent_menu_id = (int) get_post_meta( $arg, '_menu_item_menu_item_parent', true );
$result = wp_delete_post( $arg, true );
if ( ! $result ) {
Expand Down Expand Up @@ -408,10 +410,10 @@ public function delete( $args, $assoc_args ) {
private function add_or_update_item( $method, $type, $args, $assoc_args ) {

$menu = $args[0];
$menu_item_db_id = Utils\get_flag_value( $args, 1, 0 );
$menu_item_db_id = $args[1] ?? 0;

$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu || is_wp_error( $menu ) ) {
if ( false === $menu ) {
Comment on lines -414 to +416
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here re. WP_Error. I'm assuming there was a reason why that particular check was in there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

It was originally introduced in 246a8bc, but without any test coverage or so.

Like in the other cases, this function never returns a WP_Error

WP_CLI::error( 'Invalid menu.' );
}

Expand All @@ -422,6 +424,14 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
if ( 'update' === $method ) {

$menu_item_obj = get_post( $menu_item_db_id );

if ( ! $menu_item_obj ) {
WP_CLI::error( 'Invalid menu.' );
}

/**
* @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, post_status: string, menu_order: int} $menu_item_obj
*/
$menu_item_obj = wp_setup_nav_menu_item( $menu_item_obj );

// Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140
Expand Down Expand Up @@ -502,7 +512,7 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
}

if ( 'add' === $method && ! empty( $assoc_args['porcelain'] ) ) {
WP_CLI::line( $result );
WP_CLI::line( (string) $result );
} elseif ( 'add' === $method ) {
WP_CLI::success( 'Menu item added.' );
} elseif ( 'update' === $method ) {
Expand Down
8 changes: 6 additions & 2 deletions src/Menu_Location_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ function ( $o ) {
* Success: Assigned location primary to menu primary-menu.
*
* @subcommand assign
*
* @param array{string, string} $args
*/
public function assign( $args, $assoc_args ) {

Expand Down Expand Up @@ -147,18 +149,20 @@ public function assign( $args, $assoc_args ) {
* Success: Removed location from menu.
*
* @subcommand remove
*
* @param array{string, string} $args
*/
public function remove( $args, $assoc_args ) {

list( $menu, $location ) = $args;

$menu = wp_get_nav_menu_object( $menu );
if ( ! $menu || is_wp_error( $menu ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here re. WP_Error.

if ( false === $menu ) {
WP_CLI::error( 'Invalid menu.' );
}

$locations = get_nav_menu_locations();
if ( Utils\get_flag_value( $locations, $location ) !== $menu->term_id ) {
if ( ( $locations[ $location ] ?? null ) !== $menu->term_id ) {
WP_CLI::error( "Menu isn't assigned to location." );
}

Expand Down
Loading