PluginProbe
Polylang / 3.0.6
Polylang v3.0.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 / integrations / wp-importer / wp-import.php

wp-import.php in Polylang 3.0.6, at integrations/wp-importer/wp-import.php

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