Skip to content

Commit a877b0f

Browse files
committed
PHPCS: Update ruleset to exclude classes from PrefixAllGlobals.NonPrefixedClassFound sniff
Fix CS in Post command
1 parent d90e4e3 commit a877b0f

File tree

5 files changed

+117
-83
lines changed

5 files changed

+117
-83
lines changed

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</rule>
5959

6060
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
61+
<exclude-pattern>*/src/Post(_Meta|_Term|_Type)?_Command\.php$</exclude-pattern>
6162
<exclude-pattern>*/src/Site(_Meta|_Option)?_Command\.php$</exclude-pattern>
6263
<exclude-pattern>*/src/Term(_Meta)?_Command\.php$</exclude-pattern>
6364
<exclude-pattern>*/src/User(_Meta|_Session|_Term)?_Command\.php$</exclude-pattern>

src/Post_Command.php

Lines changed: 94 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
/**
46
* Manages posts, content, and meta.
57
*
@@ -19,19 +21,19 @@
1921
*
2022
* @package wp-cli
2123
*/
22-
class Post_Command extends \WP_CLI\CommandWithDBObject {
24+
class Post_Command extends WP_CLI\CommandWithDBObject {
2325

24-
protected $obj_type = 'post';
25-
protected $obj_fields = array(
26+
protected $obj_type = 'post';
27+
protected $obj_fields = [
2628
'ID',
2729
'post_title',
2830
'post_name',
2931
'post_date',
3032
'post_status',
31-
);
33+
];
3234

3335
public function __construct() {
34-
$this->fetcher = new \WP_CLI\Fetchers\Post;
36+
$this->fetcher = new \WP_CLI\Fetchers\Post();
3537
}
3638

3739
/**
@@ -160,25 +162,27 @@ public function create( $args, $assoc_args ) {
160162
$assoc_args['post_content'] = $this->read_from_file_or_stdin( $args[0] );
161163
}
162164

163-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'edit' ) ) {
164-
$input = \WP_CLI\Utils\get_flag_value( $assoc_args, 'post_content', '' );
165+
if ( Utils\get_flag_value( $assoc_args, 'edit' ) ) {
166+
$input = Utils\get_flag_value( $assoc_args, 'post_content', '' );
165167

166-
if ( $output = $this->_edit( $input, 'WP-CLI: New Post' ) )
168+
$output = $this->_edit( $input, 'WP-CLI: New Post' );
169+
if ( $output ) {
167170
$assoc_args['post_content'] = $output;
168-
else
171+
} else {
169172
$assoc_args['post_content'] = $input;
173+
}
170174
}
171175

172176
if ( isset( $assoc_args['post_category'] ) ) {
173177
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
174178
}
175179

176-
if ( isset( $assoc_args['meta_input'] ) && \WP_CLI\Utils\wp_version_compare( '4.4', '<' ) ) {
180+
if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
177181
WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
178182
}
179183

180-
$array_arguments = array( 'meta_input' );
181-
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
184+
$array_arguments = [ 'meta_input' ];
185+
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
182186

183187
if ( isset( $assoc_args['from-post'] ) ) {
184188
$post = $this->fetcher->get_check( $assoc_args['from-post'] );
@@ -202,9 +206,13 @@ public function create( $args, $assoc_args ) {
202206
}
203207

204208
$assoc_args = wp_slash( $assoc_args );
205-
parent::_create( $args, $assoc_args, function ( $params ) {
206-
return wp_insert_post( $params, true );
207-
} );
209+
parent::_create(
210+
$args,
211+
$assoc_args,
212+
function ( $params ) {
213+
return wp_insert_post( $params, true );
214+
}
215+
);
208216
}
209217

210218
/**
@@ -314,7 +322,7 @@ public function create( $args, $assoc_args ) {
314322
*/
315323
public function update( $args, $assoc_args ) {
316324

317-
foreach( $args as $key => $arg ) {
325+
foreach ( $args as $key => $arg ) {
318326
if ( is_numeric( $arg ) ) {
319327
continue;
320328
}
@@ -328,17 +336,21 @@ public function update( $args, $assoc_args ) {
328336
$assoc_args['post_category'] = $this->get_category_ids( $assoc_args['post_category'] );
329337
}
330338

331-
if ( isset( $assoc_args['meta_input'] ) && \WP_CLI\Utils\wp_version_compare( '4.4', '<' ) ) {
339+
if ( isset( $assoc_args['meta_input'] ) && Utils\wp_version_compare( '4.4', '<' ) ) {
332340
WP_CLI::warning( "The 'meta_input' field was only introduced in WordPress 4.4 so will have no effect." );
333341
}
334342

335-
$array_arguments = array( 'meta_input' );
336-
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments );
343+
$array_arguments = [ 'meta_input' ];
344+
$assoc_args = Utils\parse_shell_arrays( $assoc_args, $array_arguments );
337345

338346
$assoc_args = wp_slash( $assoc_args );
339-
parent::_update( $args, $assoc_args, function ( $params ) {
340-
return wp_update_post( $params, true );
341-
} );
347+
parent::_update(
348+
$args,
349+
$assoc_args,
350+
function ( $params ) {
351+
return wp_update_post( $params, true );
352+
}
353+
);
342354
}
343355

344356
/**
@@ -359,17 +371,20 @@ public function edit( $args, $_ ) {
359371

360372
$r = $this->_edit( $post->post_content, "WP-CLI post {$post->ID}" );
361373

362-
if ( $r === false )
374+
if ( false === $r ) {
363375
\WP_CLI::warning( 'No change made to post content.', 'Aborted' );
364-
else
365-
$this->update( $args, array( 'post_content' => $r, ) );
376+
} else {
377+
$this->update( $args, [ 'post_content' => $r ] );
378+
}
366379
}
367380

381+
// phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore -- Whitelisting to provide backward compatibility to classes possibly extending this class.
368382
protected function _edit( $content, $title ) {
383+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
369384
$content = apply_filters( 'the_editor_content', $content );
370-
$output = \WP_CLI\Utils\launch_editor_for_input( $content, $title );
371-
return ( is_string( $output ) ) ?
372-
apply_filters( 'content_save_pre', $output ) : $output;
385+
$output = Utils\launch_editor_for_input( $content, $title );
386+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
387+
return ( is_string( $output ) ) ? apply_filters( 'content_save_pre', $output ) : $output;
373388
}
374389

375390
/**
@@ -465,7 +480,7 @@ protected function delete_callback( $post_id, $assoc_args ) {
465480
$post_type = get_post_type( $post_id );
466481

467482
if ( ! $assoc_args['force']
468-
&& ( $post_type !== 'post' && $post_type !== 'page' ) ) {
483+
&& ( 'post' !== $post_type && 'page' !== $post_type ) ) {
469484
return [
470485
'error',
471486
"Posts of type '{$post_type}' do not support being sent to trash.\n"
@@ -584,30 +599,33 @@ protected function delete_callback( $post_id, $assoc_args ) {
584599
public function list_( $_, $assoc_args ) {
585600
$formatter = $this->get_formatter( $assoc_args );
586601

587-
$defaults = array(
602+
$defaults = [
588603
'posts_per_page' => -1,
589604
'post_status' => 'any',
590-
);
605+
];
591606
$query_args = array_merge( $defaults, $assoc_args );
592607
$query_args = self::process_csv_arguments_to_arrays( $query_args );
593608
if ( isset( $query_args['post_type'] ) && 'any' !== $query_args['post_type'] ) {
594609
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
595610
}
596611

597-
if ( 'ids' == $formatter->format ) {
612+
if ( 'ids' === $formatter->format ) {
598613
$query_args['fields'] = 'ids';
599-
$query = new WP_Query( $query_args );
614+
$query = new WP_Query( $query_args );
600615
echo implode( ' ', $query->posts );
601-
} else if ( 'count' === $formatter->format ) {
616+
} elseif ( 'count' === $formatter->format ) {
602617
$query_args['fields'] = 'ids';
603-
$query = new WP_Query( $query_args );
618+
$query = new WP_Query( $query_args );
604619
$formatter->display_items( $query->posts );
605620
} else {
606621
$query = new WP_Query( $query_args );
607-
$posts = array_map( function( $post ) {
608-
$post->url = get_permalink( $post->ID );
609-
return $post;
610-
}, $query->posts );
622+
$posts = array_map(
623+
function( $post ) {
624+
$post->url = get_permalink( $post->ID );
625+
return $post;
626+
},
627+
$query->posts
628+
);
611629
$formatter->display_items( $posts );
612630
}
613631
}
@@ -695,26 +713,34 @@ public function list_( $_, $assoc_args ) {
695713
public function generate( $args, $assoc_args ) {
696714
global $wpdb;
697715

698-
$defaults = array(
699-
'count' => 100,
700-
'max_depth' => 1,
701-
'post_type' => 'post',
702-
'post_status' => 'publish',
703-
'post_author' => false,
704-
'post_date' => false,
716+
$defaults = [
717+
'count' => 100,
718+
'max_depth' => 1,
719+
'post_type' => 'post',
720+
'post_status' => 'publish',
721+
'post_author' => false,
722+
'post_date' => false,
705723
'post_date_gmt' => false,
706-
'post_content' => '',
707-
'post_title' => '',
708-
);
709-
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );
724+
'post_content' => '',
725+
'post_title' => '',
726+
];
727+
728+
$final_post_data = array_merge( $defaults, $assoc_args );
729+
$post_date_gmt = $final_post_data['post_date_gmt'];
730+
$post_date = $final_post_data['post_date'];
731+
$post_type = $final_post_data['post_type'];
732+
$post_author = $final_post_data['post_author'];
733+
$count = $final_post_data['count'];
734+
$max_depth = $final_post_data['max_depth'];
735+
$post_status = $final_post_data['post_status'];
710736

711737
$call_time = current_time( 'mysql' );
712738

713-
if ( $post_date_gmt === false ) {
739+
if ( false === $post_date_gmt ) {
714740
$post_date_gmt = $post_date ? $post_date : $call_time;
715741
}
716742

717-
if ( $post_date === false ) {
743+
if ( false === $post_date ) {
718744
$post_date = $post_date_gmt ? $post_date_gmt : $call_time;
719745
}
720746

@@ -728,7 +754,7 @@ public function generate( $args, $assoc_args ) {
728754
$post_author = $user_fetcher->get_check( $post_author )->ID;
729755
}
730756

731-
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'post_content' ) ) {
757+
if ( Utils\get_flag_value( $assoc_args, 'post_content' ) ) {
732758
if ( ! \WP_CLI\Entity\Utils::has_stdin() ) {
733759
WP_CLI::error( 'The parameter `post_content` reads from STDIN.' );
734760
}
@@ -745,11 +771,11 @@ public function generate( $args, $assoc_args ) {
745771

746772
$limit = $count + $total;
747773

748-
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'progress' );
774+
$format = Utils\get_flag_value( $assoc_args, 'format', 'progress' );
749775

750776
$notify = false;
751777
if ( 'progress' === $format ) {
752-
$notify = \WP_CLI\Utils\make_progress_bar( 'Generating posts', $count );
778+
$notify = Utils\make_progress_bar( 'Generating posts', $count );
753779
}
754780

755781
$previous_post_id = 0;
@@ -773,7 +799,7 @@ public function generate( $args, $assoc_args ) {
773799
}
774800
}
775801

776-
$args = array(
802+
$args = [
777803
'post_type' => $post_type,
778804
'post_title' => ! empty( $post_title ) && $i === $total ? "$label" : "$label $i",
779805
'post_status' => $post_status,
@@ -783,7 +809,7 @@ public function generate( $args, $assoc_args ) {
783809
'post_date' => $post_date,
784810
'post_date_gmt' => $post_date_gmt,
785811
'post_content' => $post_content,
786-
);
812+
];
787813

788814
$post_id = wp_insert_post( $args, true );
789815
if ( is_wp_error( $post_id ) ) {
@@ -809,13 +835,13 @@ public function generate( $args, $assoc_args ) {
809835
}
810836

811837
private function maybe_make_child() {
812-
// 50% chance of making child post
813-
return ( mt_rand(1, 2) == 1 );
838+
// 50% chance of making child post.
839+
return ( wp_rand( 1, 2 ) === 1 );
814840
}
815841

816842
private function maybe_reset_depth() {
817-
// 10% chance of reseting to root depth
818-
return ( mt_rand(1, 10) == 7 );
843+
// 10% chance of reseting to root depth,
844+
return ( wp_rand( 1, 10 ) === 7 );
819845
}
820846

821847
/**
@@ -825,7 +851,7 @@ private function maybe_reset_depth() {
825851
* @return string
826852
*/
827853
private function read_from_file_or_stdin( $arg ) {
828-
if ( $arg !== '-' ) {
854+
if ( '-' !== $arg ) {
829855
$readfile = $arg;
830856
if ( ! file_exists( $readfile ) || ! is_file( $readfile ) ) {
831857
\WP_CLI::error( "Unable to read content from '$readfile'." );
@@ -845,7 +871,7 @@ private function read_from_file_or_stdin( $arg ) {
845871
private function get_category_ids( $arg ) {
846872

847873
$categories = explode( ',', $arg );
848-
$category_ids = array();
874+
$category_ids = [];
849875
foreach ( $categories as $post_category ) {
850876
if ( trim( $post_category ) ) {
851877
if ( is_numeric( $post_category ) && (int) $post_category ) {
@@ -872,11 +898,11 @@ private function get_category_ids( $arg ) {
872898
*/
873899
private function get_metadata( $post_id ) {
874900
$metadata = get_metadata( 'post', $post_id );
875-
$items = array();
901+
$items = [];
876902
foreach ( $metadata as $key => $values ) {
877903
foreach ( $values as $item_value ) {
878-
$item_value = maybe_unserialize( $item_value );
879-
$items[$key] = $item_value;
904+
$item_value = maybe_unserialize( $item_value );
905+
$items[ $key ] = $item_value;
880906
}
881907
}
882908

@@ -892,7 +918,7 @@ private function get_metadata( $post_id ) {
892918
*/
893919
private function get_category( $post_id ) {
894920
$category_data = get_the_category( $post_id );
895-
$category_arr = array();
921+
$category_arr = [];
896922
foreach ( $category_data as $cat ) {
897923
array_push( $category_arr, $cat->term_id );
898924
}
@@ -909,7 +935,7 @@ private function get_category( $post_id ) {
909935
*/
910936
private function get_tags( $post_id ) {
911937
$tag_data = get_the_tags( $post_id );
912-
$tag_arr = array();
938+
$tag_arr = [];
913939
if ( $tag_data ) {
914940
foreach ( $tag_data as $tag ) {
915941
array_push( $tag_arr, $tag->slug );

src/Post_Meta_Command.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Post_Meta_Command extends \WP_CLI\CommandWithMeta {
3030
* @param int
3131
*/
3232
protected function check_object_id( $object_id ) {
33-
$fetcher = new \WP_CLI\Fetchers\Post;
34-
$post = $fetcher->get_check( $object_id );
33+
$fetcher = new WP_CLI\Fetchers\Post();
34+
$post = $fetcher->get_check( $object_id );
3535
return $post->ID;
3636
}
3737

src/Post_Term_Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Post_Term_Command extends \WP_CLI\CommandWithTerms {
1313
protected $obj_type = 'post';
1414

1515
public function __construct() {
16-
$this->fetcher = new \WP_CLI\Fetchers\Post;
16+
$this->fetcher = new WP_CLI\Fetchers\Post();
1717
}
1818

1919
protected function get_object_type() {

0 commit comments

Comments
 (0)