Skip to content

Commit 99de66c

Browse files
committed
Almost level 7
1 parent db4f48e commit 99de66c

11 files changed

+90
-89
lines changed

entity-command.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,7 @@
6060
);
6161
WP_CLI::add_command( 'taxonomy', 'Taxonomy_Command' );
6262
WP_CLI::add_command( 'term', 'Term_Command' );
63-
WP_CLI::add_command(
64-
'term meta',
65-
'Term_Meta_Command',
66-
array(
67-
'before_invoke' => function () {
68-
if ( Utils\wp_version_compare( '4.4', '<' ) ) {
69-
WP_CLI::error( 'Requires WordPress 4.4 or greater.' );
70-
}
71-
},
72-
)
73-
);
63+
WP_CLI::add_command( 'term meta', 'Term_Meta_Command' );
7464
WP_CLI::add_command( 'user', 'User_Command' );
7565
WP_CLI::add_command(
7666
'user application-password',
@@ -84,17 +74,7 @@
8474
)
8575
);
8676
WP_CLI::add_command( 'user meta', 'User_Meta_Command' );
87-
WP_CLI::add_command(
88-
'user session',
89-
'User_Session_Command',
90-
array(
91-
'before_invoke' => function () {
92-
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
93-
WP_CLI::error( 'Requires WordPress 4.0 or greater.' );
94-
}
95-
},
96-
)
97-
);
77+
WP_CLI::add_command( 'user session', 'User_Session_Command' );
9878
WP_CLI::add_command( 'user term', 'User_Term_Command' );
9979

10080
if ( class_exists( 'WP_CLI\Dispatcher\CommandNamespace' ) ) {

src/Comment_Command.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,29 +379,22 @@ public function list_( $args, $assoc_args ) {
379379
$assoc_args['count'] = true;
380380
}
381381

382-
if ( ! empty( $assoc_args['comment__in'] )
383-
&& ! empty( $assoc_args['orderby'] )
384-
&& 'comment__in' === $assoc_args['orderby']
385-
&& Utils\wp_version_compare( '4.4', '<' ) ) {
386-
$comments = [];
387-
foreach ( $assoc_args['comment__in'] as $comment_id ) {
388-
$comment = get_comment( $comment_id );
389-
if ( $comment ) {
390-
$comments[] = $comment;
391-
} else {
392-
WP_CLI::warning( "Invalid comment {$comment_id}." );
393-
}
394-
}
395-
} else {
396-
$query = new WP_Comment_Query();
397-
$comments = $query->query( $assoc_args );
398-
}
382+
$query = new WP_Comment_Query();
383+
$comments = $query->query( $assoc_args );
399384

400385
if ( 'count' === $formatter->format ) {
386+
/**
387+
* @var int $comments
388+
*/
401389
echo $comments;
402390
} else {
403391
if ( 'ids' === $formatter->format ) {
404-
$comments = wp_list_pluck( $comments, 'comment_ID' );
392+
/**
393+
* @var \WP_Comment[] $comments
394+
*/
395+
$items = wp_list_pluck( $comments, 'comment_ID' );
396+
397+
$comments = $items;
405398
} elseif ( is_array( $comments ) ) {
406399
$comments = array_map(
407400
function ( $comment ) {
@@ -460,6 +453,9 @@ function ( $comment_id, $assoc_args ) {
460453
private function call( $args, $status, $success, $failure ) {
461454
$comment_id = absint( $args );
462455

456+
/**
457+
* @var callable $func
458+
*/
463459
$func = "wp_{$status}_comment";
464460

465461
if ( ! $func( $comment_id ) ) {

src/Menu_Item_Command.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ private function add_or_update_item( $method, $type, $args, $assoc_args ) {
422422
if ( 'update' === $method ) {
423423

424424
$menu_item_obj = get_post( $menu_item_db_id );
425+
426+
/**
427+
* @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
428+
*/
425429
$menu_item_obj = wp_setup_nav_menu_item( $menu_item_obj );
426430

427431
// Correct the menu position if this was the first item. See https://core.trac.wordpress.org/ticket/28140

src/Option_Command.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -746,17 +746,11 @@ function ( $key ) {
746746
}
747747

748748
private static function esc_like( $old ) {
749+
/**
750+
* @var \wpdb $wpdb
751+
*/
749752
global $wpdb;
750753

751-
// Remove notices in 4.0 and support backwards compatibility
752-
if ( method_exists( $wpdb, 'esc_like' ) ) {
753-
// 4.0
754-
$old = $wpdb->esc_like( $old );
755-
} else {
756-
// phpcs:ignore WordPress.WP.DeprecatedFunctions.like_escapeFound -- called in WordPress 3.9 or less.
757-
$old = like_escape( esc_sql( $old ) );
758-
}
759-
760-
return $old;
754+
return $wpdb->esc_like( $old );
761755
}
762756
}

src/Post_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ private function read_from_file_or_stdin( $arg ) {
933933
} else {
934934
$readfile = 'php://stdin';
935935
}
936-
return file_get_contents( $readfile );
936+
return (string) file_get_contents( $readfile );
937937
}
938938

939939
/**
@@ -1010,7 +1010,7 @@ private function get_category( $post_id ) {
10101010
private function get_tags( $post_id ) {
10111011
$tag_data = get_the_tags( $post_id );
10121012
$tag_arr = [];
1013-
if ( $tag_data ) {
1013+
if ( $tag_data && ! is_wp_error( $tag_data ) ) {
10141014
foreach ( $tag_data as $tag ) {
10151015
array_push( $tag_arr, $tag->slug );
10161016
}

src/Site_Command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* Success: The site at 'http://www.example.com/example' was deleted.
3030
*
3131
* @package wp-cli
32+
*
33+
* @phpstan-type UserSite object{userblog_id: int, blogname: string, domain: string, path: string, site_id: int, siteurl: string, archived: int, spam: int, deleted: int}
3234
*/
3335
class Site_Command extends CommandWithDBObject {
3436

@@ -809,6 +811,9 @@ public function list_( $args, $assoc_args ) {
809811
$user = ( new UserFetcher() )->get_check( $assoc_args['site_user'] );
810812

811813
if ( $user ) {
814+
/**
815+
* @phpstan-var UserSite[] $blogs
816+
*/
812817
$blogs = get_blogs_of_user( $user->ID );
813818

814819
foreach ( $blogs as $blog ) {

src/Site_Option_Command.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,17 +406,11 @@ function ( $key ) {
406406
}
407407

408408
private static function esc_like( $old ) {
409+
/**
410+
* @var \wpdb $wpdb
411+
*/
409412
global $wpdb;
410413

411-
// Remove notices in 4.0 and support backwards compatibility
412-
if ( method_exists( $wpdb, 'esc_like' ) ) {
413-
// 4.0
414-
$old = $wpdb->esc_like( $old );
415-
} else {
416-
// phpcs:ignore WordPress.WP.DeprecatedFunctions.like_escapeFound -- called in WordPress 3.9 or less.
417-
$old = like_escape( esc_sql( $old ) );
418-
}
419-
420-
return $old;
414+
return $wpdb->esc_like( $old );
421415
}
422416
}

src/Term_Command.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ public function create( $args, $assoc_args ) {
223223
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
224224
unset( $assoc_args['porcelain'] );
225225

226-
// Compatibility for < WP 4.0
227-
if ( $assoc_args['parent'] > 0 && ! term_exists( (int) $assoc_args['parent'] ) ) {
228-
WP_CLI::error( 'Parent term does not exist.' );
229-
}
230-
231226
$assoc_args = wp_slash( $assoc_args );
232227
$term = wp_slash( $term );
233228
$result = wp_insert_term( $term, $taxonomy, $assoc_args );
@@ -537,10 +532,13 @@ public function generate( $args, $assoc_args ) {
537532
WP_CLI::error( "'{$taxonomy}' is not a registered taxonomy." );
538533
}
539534

540-
$label = get_taxonomy( $taxonomy )->labels->singular_name;
541-
$slug = sanitize_title_with_dashes( $label );
535+
/**
536+
* @var \WP_Taxonomy $tax
537+
*/
538+
$tax = get_taxonomy( $taxonomy );
542539

543-
$hierarchical = get_taxonomy( $taxonomy )->hierarchical;
540+
$label = $tax->labels->singular_name;
541+
$slug = sanitize_title_with_dashes( $label );
544542

545543
$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );
546544

@@ -560,7 +558,7 @@ public function generate( $args, $assoc_args ) {
560558

561559
for ( $index = $max_id + 1; $index <= $max_id + $count; $index++ ) {
562560

563-
if ( $hierarchical ) {
561+
if ( $tax->hierarchical ) {
564562

565563
if ( $previous_term_id && $this->maybe_make_child() && $current_depth < $max_depth ) {
566564

@@ -645,6 +643,10 @@ public function recount( $args ) {
645643
WP_CLI::warning( "Taxonomy {$taxonomy} does not exist." );
646644
} else {
647645

646+
/**
647+
* @var \WP_Term[] $terms
648+
*/
649+
648650
// phpcs:ignore WordPress.WP.DeprecatedParameters.Get_termsParam2Found -- Required for backward compatibility.
649651
$terms = get_terms( $taxonomy, [ 'hide_empty' => false ] );
650652
$term_taxonomy_ids = wp_list_pluck( $terms, 'term_taxonomy_id' );
@@ -699,7 +701,11 @@ public function migrate( $args, $assoc_args ) {
699701
WP_CLI::error( "Taxonomy term '{$term_reference}' for taxonomy '{$original_taxonomy}' doesn't exist." );
700702
}
701703

702-
$original_taxonomy = get_taxonomy( $original_taxonomy );
704+
$tax = get_taxonomy( $original_taxonomy );
705+
706+
if ( ! $tax ) {
707+
WP_CLI::error( "Taxonomy '{$original_taxonomy}' doesn't exist." );
708+
}
703709

704710
$id = wp_insert_term(
705711
$term->name,
@@ -715,11 +721,14 @@ public function migrate( $args, $assoc_args ) {
715721
WP_CLI::error( $id->get_error_message() );
716722
}
717723

718-
$post_ids = get_objects_in_term( $term->term_id, $original_taxonomy->name );
724+
/**
725+
* @var string[] $post_ids
726+
*/
727+
$post_ids = get_objects_in_term( $term->term_id, $tax->name );
719728

720729
foreach ( $post_ids as $post_id ) {
721730
$type = get_post_type( (int) $post_id );
722-
if ( in_array( $type, $original_taxonomy->object_type, true ) ) {
731+
if ( in_array( $type, $tax->object_type, true ) ) {
723732
$term_taxonomy_id = wp_set_object_terms( (int) $post_id, $id['term_id'], $destination_taxonomy, true );
724733

725734
if ( is_wp_error( $term_taxonomy_id ) ) {
@@ -736,7 +745,7 @@ public function migrate( $args, $assoc_args ) {
736745

737746
WP_CLI::log( "Term '{$term->slug}' migrated." );
738747

739-
$del = wp_delete_term( $term->term_id, $original_taxonomy->name );
748+
$del = wp_delete_term( $term->term_id, $tax->name );
740749

741750
if ( is_wp_error( $del ) ) {
742751
WP_CLI::error( "Failed to delete the term '{$term->slug}'. Reason: " . $del->get_error_message() );
@@ -745,7 +754,7 @@ public function migrate( $args, $assoc_args ) {
745754
WP_CLI::log( "Old instance of term '{$term->slug}' removed from its original taxonomy." );
746755
$post_count = count( $post_ids );
747756
$post_plural = Utils\pluralize( 'post', $post_count );
748-
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$original_taxonomy->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
757+
WP_CLI::success( "Migrated the term '{$term->slug}' from taxonomy '{$tax->name}' to taxonomy '{$destination_taxonomy}' for {$post_count} {$post_plural}." );
749758
}
750759

751760
private function maybe_make_child() {

src/Term_Meta_Command.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Term_Meta_Command extends CommandWithMeta {
3333
*/
3434
protected function check_object_id( $object_id ) {
3535
$term = get_term( $object_id );
36-
if ( ! $term ) {
36+
if ( ! $term || is_wp_error( $term ) ) {
3737
WP_CLI::error( "Could not find the term with ID {$object_id}." );
3838
}
3939
return $term->term_id;
@@ -52,7 +52,7 @@ protected function check_object_id( $object_id ) {
5252
* value for the specified metadata key, no change
5353
* will be made.
5454
*
55-
* @return int|false The meta ID on success, false on failure.
55+
* @return int|false|WP_Error The meta ID on success, false on failure, WP_Error when term_id is ambiguous between taxonomies.
5656
*/
5757
protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = false ) {
5858
return add_term_meta( $object_id, $meta_key, $meta_value, $unique );
@@ -69,8 +69,8 @@ protected function add_metadata( $object_id, $meta_key, $meta_value, $unique = f
6969
* metadata entries with the specified value.
7070
* Otherwise, update all entries.
7171
*
72-
* @return int|bool Meta ID if the key didn't exist, true on successful
73-
* update, false on failure.
72+
* @return int|bool|WP_Error Meta ID if the key didn't exist, true on successful
73+
* update, false on failure, WP_Error when term_id is ambiguous between taxonomies.
7474
*/
7575
protected function update_metadata( $object_id, $meta_key, $meta_value, $prev_value = '' ) {
7676
return update_term_meta( $object_id, $meta_key, $meta_value, $prev_value );

src/User_Command.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* Success: Removed user 123 from http://example.com.
3232
*
3333
* @package wp-cli
34+
*
35+
* @phpstan-import-type UserSite from Site_Command
3436
*/
3537
class User_Command extends CommandWithDBObject {
3638

@@ -645,15 +647,18 @@ public function generate( $args, $assoc_args ) {
645647
]
646648
);
647649

648-
if ( false === $role ) {
650+
if ( false === $role && ! is_wp_error( $user_id ) ) {
649651
delete_user_option( $user_id, 'capabilities' );
650652
delete_user_option( $user_id, 'user_level' );
651653
}
652654

653655
if ( 'progress' === $format ) {
654656
$notify->tick();
655657
} elseif ( 'ids' === $format ) {
656-
echo $user_id;
658+
if ( ! is_wp_error( $user_id ) ) {
659+
echo $user_id;
660+
}
661+
657662
if ( $index < $limit - 1 ) {
658663
echo ' ';
659664
}
@@ -1164,6 +1169,11 @@ public function import_csv( $args, $assoc_args ) {
11641169
WP_CLI::log( "{$existing_user->user_login} added as {$new_user['role']}." );
11651170
}
11661171

1172+
if ( is_wp_error( $user_id ) ) {
1173+
WP_CLI::warning( $user_id );
1174+
continue;
1175+
}
1176+
11671177
// Create the user
11681178
} else {
11691179
unset( $new_user['ID'] ); // Unset else it will just return the ID
@@ -1189,22 +1199,24 @@ public function import_csv( $args, $assoc_args ) {
11891199
$user_id = wp_insert_user( $new_user );
11901200
}
11911201

1202+
if ( is_wp_error( $user_id ) ) {
1203+
WP_CLI::warning( $user_id );
1204+
continue;
1205+
}
1206+
11921207
if ( Utils\get_flag_value( $assoc_args, 'send-email' ) ) {
11931208
self::wp_new_user_notification( $user_id, $new_user['user_pass'] );
11941209
}
11951210
}
11961211

1197-
if ( is_wp_error( $user_id ) ) {
1198-
WP_CLI::warning( $user_id );
1199-
continue;
1200-
1201-
}
1202-
12031212
if ( false === $new_user['role'] ) {
12041213
delete_user_option( $user_id, 'capabilities' );
12051214
delete_user_option( $user_id, 'user_level' );
12061215
}
12071216

1217+
/**
1218+
* @var \WP_User $user
1219+
*/
12081220
$user = get_user_by( 'id', $user_id );
12091221
foreach ( $secondary_roles as $secondary_role ) {
12101222
$user->add_role( $secondary_role );
@@ -1332,7 +1344,7 @@ private static function validate_role( $role, $warn_user_only = false ) {
13321344
* @param mixed $password
13331345
*/
13341346
public static function wp_new_user_notification( $user_id, $password ) {
1335-
wp_new_user_notification( $user_id, null, 'both' );
1347+
wp_new_user_notification( (int) $user_id, null, 'both' );
13361348
}
13371349

13381350
/**
@@ -1414,6 +1426,10 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
14141426
}
14151427

14161428
// Make that user's blog as spam too.
1429+
1430+
/**
1431+
* @phpstan-var UserSite[] $blogs
1432+
*/
14171433
$blogs = (array) get_blogs_of_user( $user_id, true );
14181434
foreach ( $blogs as $details ) {
14191435
// Only mark site as spam if not main site.

0 commit comments

Comments
 (0)