Ticket #15091: wordpress-importer.diff

File wordpress-importer.diff, 2.2 KB (added by wpdavis, 3 years ago)

Attempts a patch to make sure the WordPress importer brings in all terms with each post

Line 
1Index: wordpress-importer.php
2===================================================================
3--- wordpress-importer.php      (revision 298755)
4+++ wordpress-importer.php      (working copy)
5@@ -463,15 +463,8 @@
6                $post_content = str_replace('<br>', '<br />', $post_content);
7                $post_content = str_replace('<hr>', '<hr />', $post_content);
8 
9-               preg_match_all('|<category domain="tag">(.*?)</category>|is', $post, $tags);
10-               $tags = $tags[1];
11+               preg_match_all('|<category\s*(?:\bdomain="(.*?)"\s*)?(?:\bnicename="(.*?)")?>(?:<!\[CDATA\[)?(.*?)(?:\]\]>)?</category>|', $post, $terms, PREG_SET_ORDER);
12 
13-               $tag_index = 0;
14-               foreach ($tags as $tag) {
15-                       $tags[$tag_index] = $wpdb->escape( html_entity_decode( str_replace(array( '<![CDATA[', ']]>' ), '', $tag ) ) );
16-                       $tag_index++;
17-               }
18-
19                preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
20                $categories = $categories[1];
21 
22@@ -556,26 +549,30 @@
23                        }
24 
25                        // Add terms.
26-                       if (count($tags) > 0) {
27-                               $post_tags = array();
28-                               foreach ($tags as $tag) {
29-                                       if ( '' == $tag )
30+                       if (count($terms) > 0) {
31+                               $post_terms = array();
32+                               foreach ($terms as $term) {
33+                                       if ( '' == $term[1] || 'category' == $term[1])
34                                                continue;
35-                                       $slug = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
36-                                       $tag_obj = get_term_by('slug', $slug, 'post_tag');
37-                                       $tag_id = 0;
38-                                       if ( ! empty($tag_obj) )
39-                                               $tag_id = $tag_obj->term_id;
40-                                       if ( $tag_id == 0 ) {
41-                                               $tag = $wpdb->escape($tag);
42-                                               $tag_id = wp_insert_term($tag, 'post_tag');
43-                                               if ( is_wp_error($tag_id) )
44+                                       $slug = $term[2];
45+                                       echo $slug;
46+                                       $domain = $term[1];
47+                                       if ($domain == 'tag') {
48+                                               $domain = 'post_tag';
49+                                       }
50+                                       $term = $wpdb->escape($term[3]);
51+                                       $term_id = term_exists( $term, $domain );
52+                                       if ( $term_id || $term_id != 0) {
53+                                               echo $term_id;
54+                                       } else {
55+                                               $term_id = wp_insert_term($term, $domain);
56+                                               echo $term_id;
57+                                               if ( is_wp_error($term_id) )
58                                                        continue;
59-                                               $tag_id = $tag_id['term_id'];
60+                                               $term_id = $term_id['term_id'];
61                                        }
62-                                       $post_tags[] = intval($tag_id);
63+                                       wp_set_post_terms($post_id, $term, $domain,true);
64                                }
65-                               wp_set_post_tags($post_id, $post_tags);
66                        }
67                }
68