From 51ec16b544ec4bccb73d6852382fcf20b5f742de Mon Sep 17 00:00:00 2001 From: Stephen Harris Date: Wed, 8 Mar 2017 22:36:51 +0000 Subject: [PATCH 1/2] Add support for term meta data. See https://core.trac.wordpress.org/ticket/34062 --- class-wxr-importer.php | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/class-wxr-importer.php b/class-wxr-importer.php index 51c6a80..9b66d32 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1624,6 +1624,11 @@ protected function parse_term_node( $node, $type = 'term' ) { $key = array_search( $child->tagName, $tag_name ); if ( $key ) { $data[ $key ] = $child->textContent; + } elseif ( $child->tagName === 'wp:termmeta' ) { + $meta_item = $this->parse_meta_node( $child ); + if ( ! empty( $meta_item ) ) { + $meta[] = $meta_item; + } } } @@ -1742,6 +1747,8 @@ protected function process_term( $data, $meta ) { $term_id ) ); + $this->process_term_meta( $meta, $term_id, $term ); + do_action( 'wp_import_insert_term', $term_id, $data ); /** @@ -1753,6 +1760,47 @@ protected function process_term( $data, $meta ) { do_action( 'wxr_importer.processed.term', $term_id, $data ); } + /** + * Process and import term meta items. + * + * @param array $meta List of meta data arrays + * @param int $term_id Term ID to associate with + * @param array $term Term data + * @return int|bool Number of meta items imported on success, false otherwise. + */ + protected function process_term_meta( $meta, $term_id, $term ) { + if ( empty( $meta ) ) { + return true; + } + + foreach ( $meta as $meta_item ) { + /** + * Pre-process term meta data. + * + * @param array $meta_item Meta data. (Return empty to skip.) + * @param int $term_id Term the meta is attached to. + */ + $meta_item = apply_filters( 'wxr_importer.pre_process.term_meta', $meta_item, $term_id ); + if ( empty( $meta_item ) ) { + return false; + } + + $key = apply_filters( 'import_term_meta_key', $meta_item['key'], $term_id, $term ); + + if ( ! $key ) { + continue; + } + + // export gets meta straight from the DB so could have a serialized string + $value = maybe_unserialize( $meta_item['value'] ); + + add_term_meta( $term_id, $key, $value ); + do_action( 'import_term_meta', $term_id, $key, $value ); + } + + return true; + } + /** * Attempt to download a remote file attachment * From 4855a533c1c174bd845061d528d0a2245de2a6fc Mon Sep 17 00:00:00 2001 From: Stephen Harris Date: Sun, 12 Mar 2017 14:19:35 +0000 Subject: [PATCH 2/2] Returning a falsey value on wxr_importer.pre_process.term_meta filter should skip the current term meta, not all subsequent term meta. --- 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 9b66d32..353a317 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1782,7 +1782,7 @@ protected function process_term_meta( $meta, $term_id, $term ) { */ $meta_item = apply_filters( 'wxr_importer.pre_process.term_meta', $meta_item, $term_id ); if ( empty( $meta_item ) ) { - return false; + continue; } $key = apply_filters( 'import_term_meta_key', $meta_item['key'], $term_id, $term );