PluginProbe
Polylang / 1.6
Polylang v1.6
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / admin / wp-import.php

wp-import.php in Polylang 1.6, at admin/wp-import.php

168 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * a class to import languages and translations information form a WXR file
5 *
6 * @since 1.2
7 */
8 class PLL_WP_Import extends WP_Import {
9
10 /*
11 * overrides WP_Import::process_terms to remap terms translations
12 *
13 * @since 1.2
14 */
15 function process_terms() {
16 // store this for future usage as parent function unsets $this->terms
17 foreach ($this->terms as $term) {
18 if ('post_translations' == $term['term_taxonomy'])
19 $this->post_translations[] = $term;
20 if ('term_translations' == $term['term_taxonomy'])
21 $term_translations[] = $term;
22 }
23
24 parent::process_terms();
25
26 global $polylang;
27 $polylang->model->clean_languages_cache(); // to update the languages list if needed
28
29 if (($options = get_option('polylang')) && empty($options['default_lang']) && ($languages = $polylang->model->get_languages_list())) {
30 // assign the default language if importer created the first language
31 $default_lang = reset($languages);
32 $options['default_lang'] = $default_lang->slug;
33 update_option('polylang', $options);
34 }
35
36 $this->remap_terms_relations($term_translations);
37 $this->remap_translations($term_translations, $this->processed_terms);
38 }
39
40 /*
41 * overrides WP_Import::process_post to remap posts translations
42 * also merges strings translations from the WXR file to the existing ones
43 *
44 * @since 1.2
45 */
46 function process_posts() {
47 // store this for future usage as parent function unset $this->posts
48 foreach ($this->posts as $post) {
49 if ('nav_menu_item' == $post['post_type'])
50 $menu_items[] = $post;
51
52 if (0 === strpos($post['post_title'], 'polylang_mo_'))
53 $mo_posts[] = $post;
54 }
55
56 if (!empty($mo_posts))
57 new PLL_MO(); // just to register the polylang_mo post type before processing posts
58
59 parent::process_posts();
60
61 global $polylang;
62 $polylang->model->clean_languages_cache(); // to update the posts count in (cached) languages list
63
64 $this->remap_translations($this->post_translations, $this->processed_posts);
65 unset($this->post_translations);
66
67 // language switcher menu items
68 foreach ($menu_items as $item) {
69 foreach ($item['postmeta'] as $meta) {
70 if ('_pll_menu_item' == $meta['key'])
71 update_post_meta($this->processed_menu_items[$item['post_id']], '_pll_menu_item', maybe_unserialize($meta['value']));
72 }
73 }
74
75 // merge strings translations
76 foreach ($mo_posts as $post) {
77 $lang_id = (int) substr($post['post_title'], 12);
78
79 if (!empty($this->processed_terms[$lang_id])) {
80 if ($strings = unserialize($post['post_content'])) {
81 $mo = new PLL_MO();
82 $mo->import_from_db($this->processed_terms[$lang_id]);
83 foreach ($strings as $msg)
84 $mo->add_entry_or_merge($mo->make_entry($msg[0], $msg[1]));
85 $mo->export_to_db($this->processed_terms[$lang_id]);
86 }
87 }
88 // delete the now useless imported post
89 wp_delete_post($this->processed_posts[$post['post_id']], true);
90 }
91 }
92
93 /*
94 * remaps terms languages
95 *
96 * @since 1.2
97 *
98 * @param array $terms array of terms in 'term_translations' taxonomy
99 */
100 function remap_terms_relations(&$terms){
101 global $polylang, $wpdb;
102
103 foreach ($terms as $term) {
104 $translations = unserialize($term['term_description']);
105 foreach($translations as $slug => $old_id)
106 if ($old_id && !empty($this->processed_terms[$old_id]) && $lang = $polylang->model->get_language($slug)) {
107 // language relationship
108 $trs[] = $wpdb->prepare('(%d, %d)', $this->processed_terms[$old_id], $lang->tl_term_taxonomy_id);
109
110 // translation relationship
111 $trs[] = $wpdb->prepare('(%d, %d)', $this->processed_terms[$old_id], get_term($this->processed_terms[$term['term_id']], 'term_translations')->term_taxonomy_id );
112 }
113 }
114
115 // insert term_relationships
116 if (!empty($trs)) {
117 $trs = array_unique($trs);
118
119 // make sure we don't attempt to insert already existing term relationships
120 $existing_trs = $wpdb->get_results("
121 SELECT tr.object_id, tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr
122 INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
123 WHERE tt.taxonomy IN ('term_language', 'term_translations')
124 ");
125
126 foreach ($existing_trs as $key => $tr)
127 $existing_trs[$key] = $wpdb->prepare('(%d, %d)', $tr->object_id, $tr->term_taxonomy_id);
128
129 $trs = array_diff($trs, $existing_trs);
130
131 if (!empty($trs))
132 $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $trs));
133 }
134 }
135
136 /*
137 * remaps translations for both posts and terms
138 *
139 * @since 1.2
140 *
141 * @param array $terms array of terms in 'post_translations' or 'term_translations' taxonomies
142 * @param array $processed_objects array of posts or terms processed by WordPress Importer
143 */
144 function remap_translations(&$terms, &$processed_objects) {
145 global $wpdb;
146
147 foreach ($terms as $term) {
148 $translations = unserialize($term['term_description']);
149 $new_translations = array();
150
151 foreach($translations as $slug => $old_id)
152 if ($old_id && !empty($processed_objects[$old_id]))
153 $new_translations[$slug] = $processed_objects[$old_id];
154
155 if (!empty($new_translations)) {
156 $u['case'][] = $wpdb->prepare('WHEN %d THEN %s', $this->processed_terms[$term['term_id']], serialize($new_translations));
157 $u['in'][] = (int) $this->processed_terms[$term['term_id']];
158 }
159 }
160
161 if (!empty($u)) {
162 $wpdb->query("UPDATE $wpdb->term_taxonomy
163 SET description = ( CASE term_id " . implode(' ', $u['case']) . " END )
164 WHERE term_id IN ( " . implode(',', $u['in']) . " )");
165 }
166 }
167 }
168