From c14c9f2914fd4de04779aabd1ceb303e79187478 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Wed, 4 Mar 2020 23:36:55 +0530 Subject: [PATCH 1/3] Add code to process and insert user meta --- class-wxr-importer.php | 66 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/class-wxr-importer.php b/class-wxr-importer.php index 19afbec..71b3768 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1468,6 +1468,13 @@ protected function parse_author_node( $node ) { case 'wp:author_last_name': $data['last_name'] = $child->textContent; break; + + case 'wp:authormeta': + $meta_item = $this->parse_meta_node( $child ); + if ( ! empty( $meta_item ) ) { + $meta[] = $meta_item; + } + break; } } @@ -1568,7 +1575,9 @@ protected function process_author( $data, $meta ) { $user_id ) ); - // TODO: Implement meta handling once WXR includes it + // Import user meta. + $this->process_author_meta( $meta, $user_id, $userdata ); + /** * User processing completed. * @@ -1578,6 +1587,61 @@ protected function process_author( $data, $meta ) { do_action( 'wxr_importer.processed.user', $user_id, $userdata ); } + /** + * Process and import user meta items. + * + * @param array $meta List of meta data arrays + * @param int $user_id User to associate with + * @param array $userdata User data + * + * @return int|WP_Error Number of meta items imported on success, error otherwise. + */ + protected function process_author_meta( $meta, $user_id, $userdata ) { + + if ( empty( $meta ) ) { + return true; + } + + foreach ( $meta as $meta_item ) { + + /** + * Pre-process user meta data. + * + * @param array $meta_item Meta data. (Return empty to skip.) + * @param int $user_id User the meta is attached to. + */ + $meta_item = apply_filters( 'wxr_importer.pre_process.user_meta', $meta_item, $user_id ); + + if ( empty( $meta_item ) ) { + return false; + } + + /** + * Pre-process user meta import key. + * + * @param string $meta_item['key'] Meta key. + * @param int $user_id User the meta is attached to. + * @param array $userdata User data. + */ + $key = apply_filters( 'import_user_meta_key', $meta_item['key'], $user_id, $userdata ); + + $value = false; + + if ( $key ) { + + // Export gets meta straight from the DB so could have a serialized string. + if ( ! $value ) { + $value = maybe_unserialize( $meta_item['value'] ); + } + + add_user_meta( $user_id, $key, $value ); + do_action( 'import_user_meta', $user_id, $key, $value ); + } + } + + return true; + } + protected function parse_term_node( $node, $type = 'term' ) { $data = array(); $meta = array(); From 82f889f796443bd892bb608dd0e0723ddd6adb1f Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Thu, 5 Mar 2020 13:28:11 +0530 Subject: [PATCH 2/3] Use wp_slash() before saving meta data --- class-wxr-importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-wxr-importer.php b/class-wxr-importer.php index 71b3768..1929b9c 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1634,7 +1634,7 @@ protected function process_author_meta( $meta, $user_id, $userdata ) { $value = maybe_unserialize( $meta_item['value'] ); } - add_user_meta( $user_id, $key, $value ); + add_user_meta( $user_id, wp_slash( $key ), wp_slash( $value ) ); do_action( 'import_user_meta', $user_id, $key, $value ); } } From 626e6348910e556e83867cb025b542fa2bc2e1f7 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Fri, 24 Apr 2020 22:53:58 +0530 Subject: [PATCH 3/3] Use update_user_meta() function to overcome duplication issue --- class-wxr-importer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-wxr-importer.php b/class-wxr-importer.php index 1929b9c..bfe4b29 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1634,7 +1634,7 @@ protected function process_author_meta( $meta, $user_id, $userdata ) { $value = maybe_unserialize( $meta_item['value'] ); } - add_user_meta( $user_id, wp_slash( $key ), wp_slash( $value ) ); + update_user_meta( $user_id, wp_slash( $key ), wp_slash( $value ) ); do_action( 'import_user_meta', $user_id, $key, $value ); } }