Skip to content

Commit 883fd8d

Browse files
authored
Merge pull request #84 from wp-cli/fix/type-correctness
Improve tyoe handling and remove unused variables
2 parents a80849a + 1d730b6 commit 883fd8d

8 files changed

Lines changed: 57 additions & 64 deletions

src/Export_Command.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function __invoke( $_, $assoc_args ) {
168168

169169
add_action(
170170
'wp_export_new_file',
171-
function( $file_path ) {
171+
static function ( $file_path ) {
172172
WP_CLI::log( sprintf( 'Writing to file %s', $file_path ) );
173173
Utils\wp_clear_object_cache();
174174
}
@@ -214,7 +214,7 @@ private static function get_filename_template( $filename_format ) {
214214
}
215215

216216
public static function load_export_api() {
217-
require dirname( dirname( __FILE__ ) ) . '/functions.php';
217+
require dirname( __DIR__ ) . '/functions.php';
218218
}
219219

220220
private function validate_args( $args ) {
@@ -313,7 +313,6 @@ private function check_post_type__not_in( $post_type ) {
313313
$post_type = array_unique( array_filter( explode( ',', $post_type ) ) );
314314
$post_types = get_post_types();
315315

316-
$keep_post_types = [];
317316
foreach ( $post_type as $type ) {
318317
if ( ! in_array( $type, $post_types, true ) ) {
319318
WP_CLI::warning(
@@ -351,7 +350,7 @@ private function check_start_id( $start_id ) {
351350
return true;
352351
}
353352

354-
$start_id = intval( $start_id );
353+
$start_id = (int) $start_id;
355354

356355
// Post IDs must be greater than 0.
357356
if ( 0 >= $start_id ) {
@@ -371,7 +370,7 @@ private function check_author( $author ) {
371370
// phpcs:ignore WordPress.WP.DeprecatedFunctions.get_users_of_blogFound -- Fallback.
372371
$authors = function_exists( 'get_users' ) ? get_users() : get_users_of_blog();
373372
if ( empty( $authors ) || is_wp_error( $authors ) ) {
374-
WP_CLI::warning( sprintf( 'Could not find any authors in this blog.' ) );
373+
WP_CLI::warning( 'Could not find any authors in this blog.' );
375374
return false;
376375
}
377376
$hit = false;
@@ -398,7 +397,7 @@ private function check_author( $author ) {
398397

399398
private function check_max_num_posts( $num ) {
400399
if ( null !== $num && ( ! is_numeric( $num ) || $num <= 0 ) ) {
401-
WP_CLI::warning( sprintf( 'max_num_posts should be a positive integer.', $num ) );
400+
WP_CLI::warning( 'max_num_posts should be a positive integer.' );
402401
return false;
403402
}
404403

@@ -455,7 +454,7 @@ private function check_skip_comments( $skip ) {
455454

456455
private function check_max_file_size( $size ) {
457456
if ( ! is_numeric( $size ) ) {
458-
WP_CLI::warning( sprintf( 'max_file_size should be numeric.', $size ) );
457+
WP_CLI::warning( 'max_file_size should be numeric.' );
459458
return false;
460459
}
461460

src/WP_Export_File_Writer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public function export() {
1818
try {
1919
parent::export();
2020
} catch ( WP_Export_Exception $e ) {
21+
fclose( $this->f );
2122
throw $e;
2223
} catch ( WP_Export_Term_Exception $e ) {
24+
fclose( $this->f );
2325
throw $e;
2426
}
2527

@@ -29,7 +31,7 @@ public function export() {
2931
protected function write( $xml ) {
3032
$res = fwrite( $this->f, $xml );
3133
if ( false === $res ) {
32-
throw new WP_Export_Exception( __( 'WP Export: error writing to export file.' ) );
34+
throw new WP_Export_Exception( 'WP Export: error writing to export file.' );
3335
}
3436
}
3537
}

src/WP_Export_Oxymel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
class WP_Export_Oxymel extends Oxymel {
44
public function optional( $tag_name, $contents ) {
5-
if ( $contents ) {
5+
if ( ( property_exists( $this, $tag_name ) || method_exists( $this, $tag_name ) ) && $contents ) {
66
$this->$tag_name( $contents );
77
}
88
return $this;
99
}
1010

1111
public function optional_cdata( $tag_name, $contents ) {
12-
if ( $contents ) {
12+
if ( property_exists( $this, $tag_name ) && is_object( $this->$tag_name ) && $contents ) {
1313
$this->$tag_name->contains->cdata( $contents )->end;
1414
}
1515
return $this;
1616
}
1717

1818
public function cdata( $text ) {
19-
if ( ! seems_utf8( $text ) ) {
19+
if ( is_string( $text ) && ! seems_utf8( $text ) ) {
2020
$text = utf8_encode( $text );
2121
}
2222
return parent::cdata( $text );

src/WP_Export_Query.php

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class WP_Export_Query {
1111
const QUERY_CHUNK = 100;
1212

13-
private static $defaults = array(
13+
private static $defaults = [
1414
'post_ids' => null,
1515
'post_type' => null,
1616
'status' => null,
@@ -20,21 +20,20 @@ class WP_Export_Query {
2020
'start_id' => null,
2121
'max_num_posts' => null,
2222
'category' => null,
23-
);
23+
];
2424

2525
private $post_ids;
2626
private $filters;
27-
private $xml_gen;
2827

29-
private $wheres = array();
30-
private $joins = array();
28+
private $wheres = [];
29+
private $joins = [];
3130

3231
private $author;
3332
private $category;
3433

3534
public $missing_parents = false;
3635

37-
public function __construct( $filters = array() ) {
36+
public function __construct( $filters = [] ) {
3837
$this->filters = wp_parse_args( $filters, self::$defaults );
3938
$this->post_ids = $this->calculate_post_ids();
4039
}
@@ -48,15 +47,15 @@ public function charset() {
4847
}
4948

5049
public function site_metadata() {
51-
$metadata = array(
50+
$metadata = [
5251
'name' => $this->bloginfo_rss( 'name' ),
5352
'url' => $this->bloginfo_rss( 'url' ),
5453
'language' => $this->bloginfo_rss( 'language' ),
5554
'description' => $this->bloginfo_rss( 'description' ),
5655
'pubDate' => date( 'D, d M Y H:i:s +0000' ), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
5756
'site_url' => is_multisite() ? network_home_url() : $this->bloginfo_rss( 'url' ),
5857
'blog_url' => $this->bloginfo_rss( 'url' ),
59-
);
58+
];
6059
return $metadata;
6160
}
6261

@@ -67,9 +66,9 @@ public function wp_generator_tag() {
6766

6867
public function authors() {
6968
global $wpdb;
70-
$authors = array();
71-
$author_ids = $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft'" );
72-
foreach ( (array) $author_ids as $author_id ) {
69+
$authors = [];
70+
$author_ids = (array) $wpdb->get_col( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft'" );
71+
foreach ( $author_ids as $author_id ) {
7372
$authors[] = get_userdata( $author_id );
7473
}
7574
$authors = array_filter( $authors );
@@ -78,12 +77,12 @@ public function authors() {
7877

7978
public function categories() {
8079
if ( $this->category ) {
81-
return array( $this->category );
80+
return [ $this->category ];
8281
}
8382
if ( $this->filters['post_type'] ) {
84-
return array();
83+
return [];
8584
}
86-
$categories = (array) get_categories( array( 'get' => 'all' ) );
85+
$categories = (array) get_categories( [ 'get' => 'all' ] );
8786

8887
$this->check_for_orphaned_terms( $categories );
8988

@@ -94,9 +93,9 @@ public function categories() {
9493

9594
public function tags() {
9695
if ( $this->filters['post_type'] ) {
97-
return array();
96+
return [];
9897
}
99-
$tags = (array) get_tags( array( 'get' => 'all' ) );
98+
$tags = (array) get_tags( [ 'get' => 'all' ] );
10099

101100
$this->check_for_orphaned_terms( $tags );
102101

@@ -105,18 +104,18 @@ public function tags() {
105104

106105
public function custom_taxonomies_terms() {
107106
if ( $this->filters['post_type'] ) {
108-
return array();
107+
return [];
109108
}
110-
$custom_taxonomies = get_taxonomies( array( '_builtin' => false ) );
111-
$custom_terms = (array) get_terms( $custom_taxonomies, array( 'get' => 'all' ) );
109+
$custom_taxonomies = get_taxonomies( [ '_builtin' => false ] );
110+
$custom_terms = (array) get_terms( $custom_taxonomies, [ 'get' => 'all' ] );
112111
$this->check_for_orphaned_terms( $custom_terms );
113112
$custom_terms = self::topologically_sort_terms( $custom_terms );
114113
return $custom_terms;
115114
}
116115

117116
public function nav_menu_terms() {
118117
$nav_menus = wp_get_nav_menus();
119-
foreach ( $nav_menus as &$term ) {
118+
foreach ( $nav_menus as $term ) {
120119
$term->description = '';
121120
}
122121
return $nav_menus;
@@ -143,7 +142,7 @@ public function exportify_post( $post ) {
143142

144143
public function posts() {
145144
$posts_iterator = new WP_Post_IDs_Iterator( $this->post_ids, self::QUERY_CHUNK );
146-
return new WP_Map_Iterator( $posts_iterator, array( $this, 'exportify_post' ) );
145+
return new WP_Map_Iterator( $posts_iterator, [ $this, 'exportify_post' ] );
147146
}
148147

149148
private function calculate_post_ids() {
@@ -170,29 +169,28 @@ private function calculate_post_ids() {
170169
$join = implode( ' ', array_filter( $this->joins ) );
171170

172171
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Individual where clauses run through $wpdb->prepare().
173-
$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} AS p $join $where {$this->max_num_posts()}" );
172+
$post_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} AS p {$join} {$where} {$this->max_num_posts()}" );
174173
if ( $this->filters['post_type'] ) {
175174
$post_ids = array_merge( $post_ids, $this->include_attachment_ids( $post_ids ) );
176175
}
177176
return $post_ids;
178177
}
179178

180179
private function post_type_where() {
181-
global $wpdb;
182-
$post_types_filters = array( 'can_export' => true );
180+
$post_types_filters = [ 'can_export' => true ];
183181
if ( $this->filters['post_type'] ) {
184182
$post_types = $this->filters['post_type'];
185183

186184
// Flatten single post types
187185
if ( is_array( $post_types ) && 1 === count( $post_types ) ) {
188186
$post_types = array_shift( $post_types );
189187
}
190-
$post_types_filters = array_merge( $post_types_filters, array( 'name' => $post_types ) );
188+
$post_types_filters = array_merge( $post_types_filters, [ 'name' => $post_types ] );
191189
}
192190

193191
// Multiple post types
194192
if ( isset( $post_types_filters['name'] ) && is_array( $post_types_filters['name'] ) ) {
195-
$post_types = array();
193+
$post_types = [];
196194
foreach ( $post_types_filters['name'] as $post_type ) {
197195
if ( post_type_exists( $post_type ) ) {
198196
$post_types[] = $post_type;
@@ -294,7 +292,7 @@ private function max_num_posts() {
294292
private function include_attachment_ids( $post_ids ) {
295293
global $wpdb;
296294
if ( ! $post_ids ) {
297-
return array();
295+
return [];
298296
}
299297
$attachment_ids = [];
300298
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- Assigment is part of the break condition.
@@ -348,8 +346,8 @@ private static function topologically_sort_terms( $terms ) {
348346
}
349347

350348
private function check_for_orphaned_terms( $terms ) {
351-
$term_ids = array();
352-
$have_parent = array();
349+
$term_ids = [];
350+
$have_parent = [];
353351

354352
foreach ( $terms as $term ) {
355353
$term_ids[ $term->term_id ] = true;
@@ -369,16 +367,14 @@ private function check_for_orphaned_terms( $terms ) {
369367
private static function get_terms_for_post( $post ) {
370368
$taxonomies = get_object_taxonomies( $post->post_type );
371369
if ( empty( $taxonomies ) ) {
372-
return array();
370+
return [];
373371
}
374-
$terms = wp_get_object_terms( $post->ID, $taxonomies );
375-
$terms = $terms ? $terms : array();
376-
return $terms;
372+
return wp_get_object_terms( $post->ID, $taxonomies ) ?: [];
377373
}
378374

379375
private static function get_meta_for_post( $post ) {
380376
global $wpdb;
381-
$meta_for_export = array();
377+
$meta_for_export = [];
382378
$meta_from_db = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = %d", $post->ID ) );
383379
foreach ( $meta_from_db as $meta ) {
384380
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook.
@@ -397,14 +393,12 @@ private function get_comments_for_post( $post ) {
397393
global $wpdb;
398394

399395
if ( isset( $this->filters['skip_comments'] ) ) {
400-
return array();
396+
return [];
401397
}
402398

403399
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
404400
foreach ( $comments as $comment ) {
405-
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) );
406-
$meta = $meta ? $meta : array();
407-
$comment->meta = $meta;
401+
$comment->meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ) ?: [];
408402
}
409403
return $comments;
410404
}

src/WP_Export_Split_Files_Writer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function export() {
4848
protected function write( $xml ) {
4949
$res = fwrite( $this->f, $xml );
5050
if ( false === $res ) {
51-
throw new WP_Export_Exception( __( 'WP Export: error writing to export file.' ) );
51+
throw new WP_Export_Exception( 'WP Export: error writing to export file.' );
5252
}
5353
$this->current_file_size += strlen( $xml );
5454
}

0 commit comments

Comments
 (0)