22
33class Import_Command extends WP_CLI_Command {
44
5+ private $ blog_users = array ();
6+
57 public $ processed_posts = array ();
68
79 /**
@@ -113,9 +115,6 @@ private function import_wxr( $file, $args ) {
113115 $ wp_import = new WP_Import ();
114116 $ wp_import ->processed_posts = $ this ->processed_posts ;
115117 $ import_data = $ wp_import ->parse ( $ file );
116- if ( is_wp_error ( $ import_data ) ) {
117- return $ import_data ;
118- }
119118
120119 // Prepare the data to be used in process_author_mapping();
121120 $ wp_import ->get_authors_from_import ( $ import_data );
@@ -147,6 +146,10 @@ private function import_wxr( $file, $args ) {
147146 $ author_data [] = $ author ;
148147 }
149148
149+ /**
150+ * @var array<\WP_User> $author_data
151+ */
152+
150153 // Build the author mapping
151154 $ author_mapping = $ this ->process_author_mapping ( $ args ['authors ' ], $ author_data );
152155 if ( is_wp_error ( $ author_mapping ) ) {
@@ -256,6 +259,7 @@ function ( $post_id ) {
256259 }
257260
258261 if ( 0 === ( $ wpcli_import_counts ['current_post ' ] % 500 ) ) {
262+ // @phpstan-ignore function.deprecated
259263 WP_CLI \Utils \wp_clear_object_cache ();
260264 WP_CLI ::log ( '-- Cleared object cache. ' );
261265 }
@@ -323,9 +327,9 @@ private function is_importer_available() {
323327 /**
324328 * Processes how the authors should be mapped
325329 *
326- * @param string $authors_arg The `--author` argument originally passed to command
327- * @param array $author_data An array of WP_User-esque author objects
328- * @return array|WP_Error $author_mapping Author mapping array if successful, WP_Error if something bad happened
330+ * @param string $authors_arg The `--author` argument originally passed to command
331+ * @param array<\WP_User> $author_data An array of WP_User-esque author objects
332+ * @return array<\WP_User> |WP_Error Author mapping array if successful, WP_Error if something bad happened
329333 */
330334 private function process_author_mapping ( $ authors_arg , $ author_data ) {
331335
@@ -360,6 +364,9 @@ private function read_author_mapping_file( $file ) {
360364 $ author_mapping = array ();
361365
362366 foreach ( new \WP_CLI \Iterators \CSV ( $ file ) as $ i => $ author ) {
367+ /**
368+ * @var array<string, \WP_User> $author
369+ */
363370 if ( ! array_key_exists ( 'old_user_login ' , $ author ) || ! array_key_exists ( 'new_user_login ' , $ author ) ) {
364371 return new WP_Error ( 'invalid-author-mapping ' , "Author mapping file isn't properly formatted. " );
365372 }
@@ -386,7 +393,15 @@ private function create_author_mapping_file( $file, $author_data ) {
386393 );
387394 }
388395 $ file_resource = fopen ( $ file , 'w ' );
389- \WP_CLI \utils \write_csv ( $ file_resource , $ author_mapping , array ( 'old_user_login ' , 'new_user_login ' ) );
396+
397+ if ( ! $ file_resource ) {
398+ return new WP_Error ( 'author-mapping-error ' , "Couldn't create author mapping file. " );
399+ }
400+
401+ // TODO: Fix $rows type upstream in write_csv()
402+ // @phpstan-ignore argument.type
403+ \WP_CLI \Utils \write_csv ( $ file_resource , $ author_mapping , array ( 'old_user_login ' , 'new_user_login ' ) );
404+
390405 return new WP_Error ( 'author-mapping-error ' , sprintf ( 'Please update author mapping file before continuing: %s ' , $ file ) );
391406 } else {
392407 return new WP_Error ( 'author-mapping-error ' , "Couldn't create author mapping file. " );
@@ -395,6 +410,8 @@ private function create_author_mapping_file( $file, $author_data ) {
395410
396411 /**
397412 * Creates users if they don't exist, and build an author mapping file.
413+ *
414+ * @param array<\WP_User> $author_data
398415 */
399416 private function create_authors_for_mapping ( $ author_data ) {
400417
@@ -433,6 +450,9 @@ private function create_authors_for_mapping( $author_data ) {
433450 return $ user_id ;
434451 }
435452
453+ /**
454+ * @var \WP_User $user
455+ */
436456 $ user = get_user_by ( 'id ' , $ user_id );
437457 $ author_mapping [] = array (
438458 'old_user_login ' => $ author ->user_login ,
@@ -444,6 +464,8 @@ private function create_authors_for_mapping( $author_data ) {
444464
445465 /**
446466 * Suggests a blog user based on the levenshtein distance.
467+ *
468+ * @return string|\WP_User
447469 */
448470 private function suggest_user ( $ author_user_login , $ author_user_email = '' ) {
449471
0 commit comments