2626 *
2727 * @package wp-cli
2828 */
29- class Comment_Command extends \ WP_CLI \CommandWithDBObject {
29+ class Comment_Command extends WP_CLI \CommandWithDBObject {
3030
31- protected $ obj_type = 'comment ' ;
31+ protected $ obj_type = 'comment ' ;
3232 protected $ obj_id_key = 'comment_ID ' ;
33- protected $ obj_fields = array (
33+ protected $ obj_fields = [
3434 'comment_ID ' ,
3535 'comment_post_ID ' ,
3636 'comment_date ' ,
3737 'comment_approved ' ,
3838 'comment_author ' ,
3939 'comment_author_email ' ,
40- ) ;
40+ ] ;
4141
4242 public function __construct () {
43- $ this ->fetcher = new \WP_CLI \Fetchers \Comment ;
43+ $ this ->fetcher = new \WP_CLI \Fetchers \Comment () ;
4444 }
4545
4646 /**
@@ -62,28 +62,32 @@ public function __construct() {
6262 */
6363 public function create ( $ args , $ assoc_args ) {
6464 $ assoc_args = wp_slash ( $ assoc_args );
65- parent ::_create ( $ args , $ assoc_args , function ( $ params ) {
66- if ( isset ( $ params ['comment_post_ID ' ] ) ) {
67- $ post_id = $ params ['comment_post_ID ' ];
68- $ post = get_post ( $ post_id );
69- if ( !$ post ) {
70- return new WP_Error ( 'no_post ' , "Can't find post $ post_id. " );
65+ parent ::_create (
66+ $ args ,
67+ $ assoc_args ,
68+ function ( $ params ) {
69+ if ( isset ( $ params ['comment_post_ID ' ] ) ) {
70+ $ post_id = $ params ['comment_post_ID ' ];
71+ $ post = get_post ( $ post_id );
72+ if ( ! $ post ) {
73+ return new WP_Error ( 'no_post ' , "Can't find post $ post_id. " );
74+ }
75+ } else {
76+ // Make sure it's set for older WP versions else get undefined PHP notice.
77+ $ params ['comment_post_ID ' ] = 0 ;
7178 }
72- } else {
73- // Make sure it's set for older WP versions else get undefined PHP notice.
74- $ params ['comment_post_ID ' ] = 0 ;
75- }
7679
77- // We use wp_insert_comment() instead of wp_new_comment() to stay at a low level and
78- // avoid wp_die() formatted messages or notifications
79- $ comment_id = wp_insert_comment ( $ params );
80+ // We use wp_insert_comment() instead of wp_new_comment() to stay at a low level and
81+ // avoid wp_die() formatted messages or notifications
82+ $ comment_id = wp_insert_comment ( $ params );
8083
81- if ( !$ comment_id ) {
82- return new WP_Error ( 'db_error ' , 'Could not create comment. ' );
83- }
84+ if ( ! $ comment_id ) {
85+ return new WP_Error ( 'db_error ' , 'Could not create comment. ' );
86+ }
8487
85- return $ comment_id ;
86- } );
88+ return $ comment_id ;
89+ }
90+ );
8791 }
8892
8993 /**
@@ -105,13 +109,17 @@ public function create( $args, $assoc_args ) {
105109 */
106110 public function update ( $ args , $ assoc_args ) {
107111 $ assoc_args = wp_slash ( $ assoc_args );
108- parent ::_update ( $ args , $ assoc_args , function ( $ params ) {
109- if ( !wp_update_comment ( $ params ) ) {
110- return new WP_Error ( 'Could not update comment. ' );
111- }
112+ parent ::_update (
113+ $ args ,
114+ $ assoc_args ,
115+ function ( $ params ) {
116+ if ( ! wp_update_comment ( $ params ) ) {
117+ return new WP_Error ( 'Could not update comment. ' );
118+ }
112119
113- return true ;
114- } );
120+ return true ;
121+ }
122+ );
115123 }
116124
117125 /**
@@ -153,31 +161,33 @@ public function update( $args, $assoc_args ) {
153161 */
154162 public function generate ( $ args , $ assoc_args ) {
155163
156- $ defaults = array (
157- 'count ' => 100 ,
158- 'post_id ' => 0 ,
159- ) ;
164+ $ defaults = [
165+ 'count ' => 100 ,
166+ 'post_id ' => 0 ,
167+ ] ;
160168 $ assoc_args = array_merge ( $ defaults , $ assoc_args );
161169
162- $ format = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'format ' , 'progress ' );
170+ $ format = Utils \get_flag_value ( $ assoc_args , 'format ' , 'progress ' );
163171
164172 $ notify = false ;
165173 if ( 'progress ' === $ format ) {
166- $ notify = \ WP_CLI \ Utils \make_progress_bar ( 'Generating comments ' , $ assoc_args ['count ' ] );
174+ $ notify = Utils \make_progress_bar ( 'Generating comments ' , $ assoc_args ['count ' ] );
167175 }
168176
169177 $ comment_count = wp_count_comments ();
170- $ total = (int ) $ comment_count ->total_comments ;
171- $ limit = $ total + $ assoc_args ['count ' ];
178+ $ total = (int ) $ comment_count ->total_comments ;
179+ $ limit = $ total + $ assoc_args ['count ' ];
172180
173181 for ( $ i = $ total ; $ i < $ limit ; $ i ++ ) {
174- $ comment_id = wp_insert_comment ( array (
175- 'comment_content ' => "Comment {$ i }" ,
176- 'comment_post_ID ' => $ assoc_args ['post_id ' ],
177- ) );
182+ $ comment_id = wp_insert_comment (
183+ [
184+ 'comment_content ' => "Comment {$ i }" ,
185+ 'comment_post_ID ' => $ assoc_args ['post_id ' ],
186+ ]
187+ );
178188 if ( 'progress ' === $ format ) {
179189 $ notify ->tick ();
180- } else if ( 'ids ' === $ format ) {
190+ } elseif ( 'ids ' === $ format ) {
181191 if ( 'ids ' === $ format ) {
182192 echo $ comment_id ;
183193 if ( $ i < $ limit - 1 ) {
@@ -225,14 +235,14 @@ public function generate( $args, $assoc_args ) {
225235 * Thanks for all the comments, everyone!
226236 */
227237 public function get ( $ args , $ assoc_args ) {
228- $ comment_id = (int )$ args [0 ];
229- $ comment = get_comment ( $ comment_id );
238+ $ comment_id = (int ) $ args [0 ];
239+ $ comment = get_comment ( $ comment_id );
230240 if ( empty ( $ comment ) ) {
231- WP_CLI ::error ( " Invalid comment ID. " );
241+ WP_CLI ::error ( ' Invalid comment ID. ' );
232242 }
233243
234244 if ( empty ( $ assoc_args ['fields ' ] ) ) {
235- $ comment_array = get_object_vars ( $ comment );
245+ $ comment_array = get_object_vars ( $ comment );
236246 $ assoc_args ['fields ' ] = array_keys ( $ comment_array );
237247 }
238248
@@ -325,7 +335,7 @@ public function get( $args, $assoc_args ) {
325335 public function list_ ( $ _ , $ assoc_args ) {
326336 $ formatter = $ this ->get_formatter ( $ assoc_args );
327337
328- if ( 'ids ' == $ formatter ->format ) {
338+ if ( 'ids ' === $ formatter ->format ) {
329339 $ assoc_args ['fields ' ] = 'comment_ID ' ;
330340 }
331341
@@ -339,30 +349,33 @@ public function list_( $_, $assoc_args ) {
339349 && ! empty ( $ assoc_args ['orderby ' ] )
340350 && 'comment__in ' === $ assoc_args ['orderby ' ]
341351 && Utils \wp_version_compare ( '4.4 ' , '< ' ) ) {
342- $ comments = array () ;
343- foreach ( $ assoc_args ['comment__in ' ] as $ comment_id ) {
352+ $ comments = [] ;
353+ foreach ( $ assoc_args ['comment__in ' ] as $ comment_id ) {
344354 $ comment = get_comment ( $ comment_id );
345355 if ( $ comment ) {
346356 $ comments [] = $ comment ;
347357 } else {
348- WP_CLI ::warning ( sprintf ( " Invalid comment %s. " , $ comment_id ) );
358+ WP_CLI ::warning ( sprintf ( ' Invalid comment %s. ' , $ comment_id ) );
349359 }
350360 }
351361 } else {
352- $ query = new WP_Comment_Query ();
362+ $ query = new WP_Comment_Query ();
353363 $ comments = $ query ->query ( $ assoc_args );
354364 }
355365
356366 if ( 'count ' === $ formatter ->format ) {
357367 echo $ comments ;
358368 } else {
359- if ( 'ids ' == $ formatter ->format ) {
369+ if ( 'ids ' === $ formatter ->format ) {
360370 $ comments = wp_list_pluck ( $ comments , 'comment_ID ' );
361371 } elseif ( is_array ( $ comments ) ) {
362- $ comments = array_map ( function ( $ comment ){
363- $ comment ->url = get_comment_link ( $ comment ->comment_ID );
364- return $ comment ;
365- }, $ comments );
372+ $ comments = array_map (
373+ function ( $ comment ) {
374+ $ comment ->url = get_comment_link ( $ comment ->comment_ID );
375+ return $ comment ;
376+ },
377+ $ comments
378+ );
366379 }
367380 $ formatter ->display_items ( $ comments );
368381 }
@@ -391,22 +404,26 @@ public function list_( $_, $assoc_args ) {
391404 * Success: Deleted comment 2341.
392405 */
393406 public function delete ( $ args , $ assoc_args ) {
394- parent ::_delete ( $ args , $ assoc_args , function ( $ comment_id , $ assoc_args ) {
395- $ force = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'force ' );
396-
397- $ status = wp_get_comment_status ( $ comment_id );
398- $ r = wp_delete_comment ( $ comment_id , $ force );
399-
400- if ( $ r ) {
401- if ( $ force || 'trash ' === $ status ) {
402- return array ( 'success ' , "Deleted comment $ comment_id. " );
407+ parent ::_delete (
408+ $ args ,
409+ $ assoc_args ,
410+ function ( $ comment_id , $ assoc_args ) {
411+ $ force = Utils \get_flag_value ( $ assoc_args , 'force ' );
412+
413+ $ status = wp_get_comment_status ( $ comment_id );
414+ $ r = wp_delete_comment ( $ comment_id , $ force );
415+
416+ if ( $ r ) {
417+ if ( $ force || 'trash ' === $ status ) {
418+ return [ 'success ' , "Deleted comment $ comment_id. " ];
419+ } else {
420+ return [ 'success ' , "Trashed comment $ comment_id. " ];
421+ }
403422 } else {
404- return array ( ' success ' , "Trashed comment $ comment_id. " ) ;
423+ return [ ' error ' , "Failed deleting comment $ comment_id. " ] ;
405424 }
406- } else {
407- return array ( 'error ' , "Failed deleting comment $ comment_id. " );
408425 }
409- } );
426+ );
410427 }
411428
412429 private function call ( $ args , $ status , $ success , $ failure ) {
@@ -458,7 +475,7 @@ private function check_server_name() {
458475 * Success: Trashed comment 1337.
459476 */
460477 public function trash ( $ args , $ assoc_args ) {
461- foreach ( $ args as $ id ) {
478+ foreach ( $ args as $ id ) {
462479 $ this ->call ( $ id , __FUNCTION__ , 'Trashed ' , 'Failed trashing ' );
463480 }
464481 }
@@ -479,7 +496,7 @@ public function trash( $args, $assoc_args ) {
479496 */
480497 public function untrash ( $ args , $ assoc_args ) {
481498 $ this ->check_server_name ();
482- foreach ( $ args as $ id ) {
499+ foreach ( $ args as $ id ) {
483500 $ this ->call ( $ id , __FUNCTION__ , 'Untrashed ' , 'Failed untrashing ' );
484501 }
485502 }
@@ -499,7 +516,7 @@ public function untrash( $args, $assoc_args ) {
499516 * Success: Marked as spam comment 1337.
500517 */
501518 public function spam ( $ args , $ assoc_args ) {
502- foreach ( $ args as $ id ) {
519+ foreach ( $ args as $ id ) {
503520 $ this ->call ( $ id , __FUNCTION__ , 'Marked as spam ' , 'Failed marking as spam ' );
504521 }
505522 }
@@ -520,7 +537,7 @@ public function spam( $args, $assoc_args ) {
520537 */
521538 public function unspam ( $ args , $ assoc_args ) {
522539 $ this ->check_server_name ();
523- foreach ( $ args as $ id ) {
540+ foreach ( $ args as $ id ) {
524541 $ this ->call ( $ id , __FUNCTION__ , 'Unspammed ' , 'Failed unspamming ' );
525542 }
526543 }
@@ -541,8 +558,8 @@ public function unspam( $args, $assoc_args ) {
541558 */
542559 public function approve ( $ args , $ assoc_args ) {
543560 $ this ->check_server_name ();
544- foreach ( $ args as $ id ) {
545- $ this ->set_status ( $ id , 'approve ' , " Approved " );
561+ foreach ( $ args as $ id ) {
562+ $ this ->set_status ( $ id , 'approve ' , ' Approved ' );
546563 }
547564 }
548565
@@ -562,8 +579,8 @@ public function approve( $args, $assoc_args ) {
562579 */
563580 public function unapprove ( $ args , $ assoc_args ) {
564581 $ this ->check_server_name ();
565- foreach ( $ args as $ id ) {
566- $ this ->set_status ( $ id , 'hold ' , " Unapproved " );
582+ foreach ( $ args as $ id ) {
583+ $ this ->set_status ( $ id , 'hold ' , ' Unapproved ' );
567584 }
568585 }
569586
@@ -598,7 +615,7 @@ public function unapprove( $args, $assoc_args ) {
598615 * total_comments: 19
599616 */
600617 public function count ( $ args , $ assoc_args ) {
601- $ post_id = \ WP_CLI \ Utils \get_flag_value ( $ args , 0 , 0 );
618+ $ post_id = Utils \get_flag_value ( $ args , 0 , 0 );
602619
603620 $ count = wp_count_comments ( $ post_id );
604621
@@ -627,11 +644,11 @@ public function count( $args, $assoc_args ) {
627644 * Updated post 123 comment count to 67.
628645 */
629646 public function recount ( $ args ) {
630- foreach ( $ args as $ id ) {
647+ foreach ( $ args as $ id ) {
631648 wp_update_comment_count ( $ id );
632649 $ post = get_post ( $ id );
633650 if ( $ post ) {
634- WP_CLI ::log ( sprintf ( " Updated post %d comment count to %d. " , $ post ->ID , $ post ->comment_count ) );
651+ WP_CLI ::log ( sprintf ( ' Updated post %d comment count to %d. ' , $ post ->ID , $ post ->comment_count ) );
635652 } else {
636653 WP_CLI ::warning ( sprintf ( "Post %d doesn't exist. " , $ post ->ID ) );
637654 }
0 commit comments