PluginProbe
Polylang / 1.5.2
Polylang v1.5.2
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
← All changes | admin/admin-model.php +262 -481 3.01.5.2 View file →
@@ -1,617 +1,398 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 -/**
7 - * Extends the PLL_Model class with methods needed only in Polylang settings pages.
3 +/*
4 + * extends the PLL_Model class with methods needed only in Polylang settings pages
8 5 *
9 6 * @since 1.2
10 7 */
11 8 class PLL_Admin_Model extends PLL_Model {
12 9
13 - /**
14 - * Adds a new language
15 - * and creates a default category for this language.
10 + /*
11 + * adds a new language
12 + * creates a default category for this language
16 13 *
14 + * list of arguments that $args must contain:
15 + * name -> language name (used only for display)
16 + * slug -> language code (ideally 2-letters ISO 639-1 language code
17 + * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
18 + * rtl -> 1 if rtl language, 0 otherwise
19 + * term_group -> language order when displayed
20 + *
21 + * optional arguments that $args can contain:
22 + * no_default_cat -> if set, no default category will be created for this language
23 + *
17 24 * @since 1.2
18 25 *
19 - * @param array $args {
20 - * @type string $name Language name ( used only for display ).
21 - * @type string $slug Language code ( ideally 2-letters ISO 639-1 language code ).
22 - * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
23 - * @type int $rtl 1 if rtl language, 0 otherwise.
24 - * @type int $term_group Language order when displayed.
25 - * @type string $no_default_cat Optional, if set, no default category will be created for this language.
26 - * @type string $flag Optional, country code, @see flags.php.
27 - * }
28 - * @return WP_Error|true true if success / WP_Error if failed.
26 + * @param array $args
27 + * @return bool true if success / false if failed
29 28 */
30 - public function add_language( $args ) {
31 - $errors = $this->validate_lang( $args );
32 - if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0
33 - return $errors;
34 - }
29 + public function add_language($args) {
30 + if (!$this->validate_lang($args))
31 + return false;
35 32
36 - // First the language taxonomy
37 - $description = maybe_serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
38 - $r = wp_insert_term( $args['name'], 'language', array( 'slug' => $args['slug'], 'description' => $description ) );
39 - if ( is_wp_error( $r ) ) {
40 - // Avoid an ugly fatal error if something went wrong ( reported once in the forum )
41 - return new WP_Error( 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) );
33 + // first the language taxonomy
34 + $description = serialize(array('locale' => $args['locale'], 'rtl' => $args['rtl']));
35 + $r = wp_insert_term($args['name'], 'language', array('slug' => $args['slug'], 'description' => $description));
36 + if (is_wp_error($r)) {
37 + // avoid an ugly fatal error if something went wrong (reported once in the forum)
38 + add_settings_error('general', 'pll_add_language', __('Impossible to add the language.', 'polylang'));
39 + return false;
42 40 }
43 - wp_update_term( (int) $r['term_id'], 'language', array( 'term_group' => (int) $args['term_group'] ) ); // can't set the term group directly in wp_insert_term
41 + wp_update_term((int) $r['term_id'], 'language', array('term_group' => $args['term_group'])); // can't set the term group directly in wp_insert_term
44 42
45 - // The term_language taxonomy
46 - // Don't want shared terms so use a different slug
47 - wp_insert_term( $args['name'], 'term_language', array( 'slug' => 'pll_' . $args['slug'] ) );
43 + // the term_language taxonomy
44 + // don't want shared terms so use a different slug
45 + wp_insert_term($args['name'], 'term_language', array('slug' => 'pll_' . $args['slug']));
48 46
49 - $this->clean_languages_cache(); // Update the languages list now !
47 + $this->clean_languages_cache(); // udpate the languages list now !
50 48
51 - if ( ! isset( $this->options['default_lang'] ) ) {
52 - // If this is the first language created, set it as default language
49 + if (!isset($this->options['default_lang'])) {
50 + // if this is the first language created, set it as default language
53 51 $this->options['default_lang'] = $args['slug'];
54 - update_option( 'polylang', $this->options );
52 + update_option('polylang', $this->options);
55 53
56 - // And assign default language to default category
57 - $this->term->set_language( (int) get_option( 'default_category' ), (int) $r['term_id'] );
58 - } elseif ( empty( $args['no_default_cat'] ) ) {
59 - $this->create_default_category( $args['slug'] );
54 + // and assign default language to default category
55 + $this->set_term_language((int) get_option('default_category'), (int) $r['term_id']);
60 56 }
57 + elseif (empty($args['no_default_cat']))
58 + $this->create_default_category($args['slug']);
61 59
62 - // Init a mo_id for this language
63 - $mo = new PLL_MO();
64 - $mo->export_to_db( $this->get_language( $args['slug'] ) );
60 + flush_rewrite_rules(); // refresh rewrite rules
65 61
66 - /**
67 - * Fires when a language is added.
68 - *
69 - * @since 1.9
70 - *
71 - * @param array $args Arguments used to create the language. @see PLL_Admin_Model::add_language().
72 - */
73 - do_action( 'pll_add_language', $args );
74 -
75 - $this->clean_languages_cache(); // Again to set add mo_id in the cached languages list
76 - flush_rewrite_rules(); // Refresh rewrite rules
62 + add_settings_error('general', 'pll_languages_created', __('Language added.', 'polylang'), 'updated');
77 63 return true;
78 64 }
79 65
80 - /**
81 - * Delete a language.
66 + /*
67 + * create a default category for a language
82 68 *
83 69 * @since 1.2
84 70 *
85 - * @param int $lang_id Language term_id.
86 - * @return bool
71 + * @param object|string|int $lang language
87 72 */
88 - public function delete_language( $lang_id ) {
89 - $lang = $this->get_language( (int) $lang_id );
73 + public function create_default_category($lang) {
74 + $lang = $this->get_language($lang);
90 75
91 - if ( empty( $lang ) ) {
92 - return false;
76 + // create a new category
77 + $cat_name = __('Uncategorized');
78 + $cat_slug = sanitize_title($cat_name . '-' . $lang->slug);
79 + $cat = wp_insert_term($cat_name, 'category', array('slug' => $cat_slug));
80 +
81 + // check that the category was not previously created (in case the language was deleted and recreated)
82 + $cat = isset($cat->error_data['term_exists']) ? $cat->error_data['term_exists'] : $cat['term_id'];
83 +
84 + // set language
85 + $this->set_term_language((int) $cat, $lang);
86 +
87 + // this is a translation of the default category
88 + $default = (int) get_option('default_category');
89 + $translations = $this->get_translations('term', $default);
90 + if (empty($translations)) {
91 + if ($lg = $this->get_term_language($default))
92 + $translations[$lg->slug] = $default;
93 + else
94 + $translations = array();
93 95 }
94 96
95 - // Oops ! we are deleting the default language...
96 - // Need to do this before loosing the information for default category translations
97 - if ( $this->options['default_lang'] == $lang->slug ) {
98 - $slugs = $this->get_languages_list( array( 'fields' => 'slug' ) );
99 - $slugs = array_diff( $slugs, array( $lang->slug ) );
97 + $this->save_translations('term', (int) $cat, $translations);
98 + }
100 99
101 - if ( ! empty( $slugs ) ) {
102 - $this->update_default_lang( reset( $slugs ) ); // Arbitrary choice...
103 - } else {
104 - unset( $this->options['default_lang'] );
105 - }
106 - }
100 + /*
101 + * delete a language
102 + *
103 + * @since 1.2
104 + *
105 + * @param int $lang_id language term_id
106 + */
107 + public function delete_language($lang_id) {
108 + $lang = $this->get_language((int) $lang_id);
109 + $default_cats = $this->get_translations('term', get_option('default_category'));
107 110
108 - // Delete the translations
109 - $this->update_translations( $lang->slug );
111 + // delete the translations
112 + $this->update_translations($lang->slug);
110 113
111 - // Delete language option in widgets
112 - foreach ( $GLOBALS['wp_registered_widgets'] as $widget ) {
113 - if ( ! empty( $widget['callback'][0] ) && ! empty( $widget['params'][0]['number'] ) ) {
114 + // delete language option in widgets
115 + foreach ($GLOBALS['wp_registered_widgets'] as $widget) {
116 + if (!empty($widget['callback'][0]) && !empty($widget['params'][0]['number'])) {
114 117 $obj = $widget['callback'][0];
115 118 $number = $widget['params'][0]['number'];
116 - if ( is_object( $obj ) && method_exists( $obj, 'get_settings' ) && method_exists( $obj, 'save_settings' ) ) {
119 + if (is_object($obj) && method_exists($obj, 'get_settings') && method_exists($obj, 'save_settings')) {
117 120 $settings = $obj->get_settings();
118 - if ( isset( $settings[ $number ]['pll_lang'] ) && $settings[ $number ]['pll_lang'] == $lang->slug ) {
119 - unset( $settings[ $number ]['pll_lang'] );
120 - $obj->save_settings( $settings );
121 + if (isset($settings[$number]['pll_lang']) && $settings[$number]['pll_lang'] == $lang->slug) {
122 + unset($settings[$number]['pll_lang']);
123 + $obj->save_settings($settings);
121 124 }
122 125 }
123 126 }
124 127 }
125 128
126 - // Delete menus locations
127 - if ( ! empty( $this->options['nav_menus'] ) ) {
128 - foreach ( $this->options['nav_menus'] as $theme => $locations ) {
129 - foreach ( array_keys( $locations ) as $location ) {
130 - unset( $this->options['nav_menus'][ $theme ][ $location ][ $lang->slug ] );
129 +
130 + // delete menus locations
131 + if (!empty($this->options['nav_menus'])) {
132 + foreach ($this->options['nav_menus'] as $theme => $locations) {
133 + foreach ($locations as $location => $languages) {
134 + unset($this->options['nav_menus'][$theme][$location][$lang->slug]);
131 135 }
132 136 }
133 137 }
134 138
135 - // Delete users options
136 - foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
137 - delete_user_meta( $user_id, 'pll_filter_content', $lang->slug );
138 - delete_user_meta( $user_id, 'description_' . $lang->slug );
139 + // delete users options
140 + foreach (get_users(array('fields' => 'ID')) as $user_id) {
141 + delete_user_meta($user_id, 'user_lang', $lang->locale);
142 + delete_user_meta($user_id, 'pll_filter_content', $lang->slug);
143 + delete_user_meta($user_id, 'description_'.$lang->slug);
139 144 }
140 145
141 - // Delete the string translations
142 - $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' );
143 - if ( $post instanceof WP_Post ) {
144 - wp_delete_post( $post->ID );
145 - }
146 + // delete the string translations
147 + $post = get_page_by_title('polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo');
148 + if (!empty($post))
149 + wp_delete_post($post->ID);
146 150
147 - // Delete domain
148 - unset( $this->options['domains'][ $lang->slug ] );
151 + // delete domain
152 + unset($this->options['domains'][$lang->slug]);
149 153
150 - // Delete the language itself
151 - wp_delete_term( $lang->term_id, 'language' );
152 - wp_delete_term( $lang->tl_term_id, 'term_language' );
154 + // delete the language itself
155 + wp_delete_term($lang->term_id, 'language');
156 + wp_delete_term($lang->tl_term_id, 'term_language');
153 157
154 - // Update languages list
158 + // update languages list
155 159 $this->clean_languages_cache();
156 160
157 - update_option( 'polylang', $this->options );
161 + // oops ! we deleted the default language...
162 + if ($this->options['default_lang'] == $lang->slug) {
163 + if ($slugs = $this->get_languages_list(array('fields' => 'slug'))) {
164 + $this->options['default_lang'] = $slug = reset($slugs); // arbitrary choice...
165 +
166 + // the default category should be in the default language
167 + if (isset($default_cats[$slug]))
168 + update_option('default_category', $default_cats[$slug]);
169 + }
170 + else
171 + unset($this->options['default_lang']);
172 + }
173 +
174 + update_option('polylang', $this->options);
158 175 flush_rewrite_rules(); // refresh rewrite rules
159 - return true;
176 + add_settings_error('general', 'pll_languages_deleted', __('Language deleted.', 'polylang'), 'updated');
160 177 }
161 178
162 - /**
163 - * Updates language properties.
179 + /*
180 + * update language properties
164 181 *
182 + * list of arguments that $args must contain:
183 + * lang_id -> term_id of the language to modify
184 + * name -> language name (used only for display)
185 + * slug -> language code (ideally 2-letters ISO 639-1 language code
186 + * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
187 + * rtl -> 1 if rtl language, 0 otherwise
188 + * term_group -> language order when displayed
189 + *
165 190 * @since 1.2
166 191 *
167 - * @param array $args {
168 - * @type int $lang_id Id of the language to modify.
169 - * @type string $name Language name ( used only for display ).
170 - * @type string $slug Language code ( ideally 2-letters ISO 639-1 language code ).
171 - * @type string $locale WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
172 - * @type int $rtl 1 if rtl language, 0 otherwise.
173 - * @type int $term_group Language order when displayed.
174 - * @type string $flag Optional, country code, @see flags.php.
175 - * }
176 - * @return WP_Error|true true if success / WP_Error if failed.
192 + * @param array $args
193 + * @return bool true if success / false if failed
177 194 */
178 - public function update_language( $args ) {
179 - $lang = $this->get_language( (int) $args['lang_id'] );
195 + public function update_language($args) {
196 + $lang = $this->get_language((int) $args['lang_id']);
197 + if (!$this->validate_lang($args, $lang))
198 + return false;
180 199
181 - $errors = $this->validate_lang( $args, $lang );
182 - if ( $errors->get_error_code() ) { // Using has_errors() would be more meaningful but is available only since WP 5.0
183 - return $errors;
184 - }
185 -
186 200 // Update links to this language in posts and terms in case the slug has been modified
187 201 $slug = $args['slug'];
188 202 $old_slug = $lang->slug;
189 203
190 - if ( $old_slug != $slug ) {
191 - // Update the language slug in translations
192 - $this->update_translations( $old_slug, $slug );
204 + // FIXME should do this in an action 'edit_term' to prevent translations to break when sharing a term with nav_menu?
205 + if ($old_slug != $slug) {
206 + // update the language slug in translations
207 + $this->update_translations($old_slug, $slug);
193 208
194 - // Update language option in widgets
195 - foreach ( $GLOBALS['wp_registered_widgets'] as $widget ) {
196 - if ( ! empty( $widget['callback'][0] ) && ! empty( $widget['params'][0]['number'] ) ) {
209 + // update language option in widgets
210 + foreach ($GLOBALS['wp_registered_widgets'] as $widget) {
211 + if (!empty($widget['callback'][0]) && !empty($widget['params'][0]['number'])) {
197 212 $obj = $widget['callback'][0];
198 213 $number = $widget['params'][0]['number'];
199 - if ( is_object( $obj ) && method_exists( $obj, 'get_settings' ) && method_exists( $obj, 'save_settings' ) ) {
214 + if (is_object($obj) && method_exists($obj, 'get_settings') && method_exists($obj, 'save_settings')) {
200 215 $settings = $obj->get_settings();
201 - if ( isset( $settings[ $number ]['pll_lang'] ) && $settings[ $number ]['pll_lang'] == $old_slug ) {
202 - $settings[ $number ]['pll_lang'] = $slug;
203 - $obj->save_settings( $settings );
216 + if (isset($settings[$number]['pll_lang']) && $settings[$number]['pll_lang'] == $old_slug) {
217 + $settings[$number]['pll_lang'] = $slug;
218 + $obj->save_settings($settings);
204 219 }
205 220 }
206 221 }
207 222 }
208 223
209 - // Update menus locations
210 - if ( ! empty( $this->options['nav_menus'] ) ) {
211 - foreach ( $this->options['nav_menus'] as $theme => $locations ) {
212 - foreach ( array_keys( $locations ) as $location ) {
213 - if ( ! empty( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ) ) {
214 - $this->options['nav_menus'][ $theme ][ $location ][ $slug ] = $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ];
215 - unset( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] );
224 + // update menus locations
225 + if (!empty($this->options['nav_menus'])) {
226 + foreach ($this->options['nav_menus'] as $theme => $locations) {
227 + foreach ($locations as $location => $languages) {
228 + if (!empty($this->options['nav_menus'][$theme][$location][$old_slug])) {
229 + $this->options['nav_menus'][$theme][$location][$slug] = $this->options['nav_menus'][$theme][$location][$old_slug];
230 + unset($this->options['nav_menus'][$theme][$location][$old_slug]);
216 231 }
232 +
217 233 }
218 234 }
219 235 }
220 236
221 - // Update domains
222 - if ( ! empty( $this->options['domains'][ $old_slug ] ) ) {
223 - $this->options['domains'][ $slug ] = $this->options['domains'][ $old_slug ];
224 - unset( $this->options['domains'][ $old_slug ] );
237 + // update domains
238 + if (!empty($this->options['domains'][$old_slug])) {
239 + $this->options['domains'][$slug] = $this->options['domains'][$old_slug];
240 + unset($this->options['domains'][$slug]);
225 241 }
226 242
227 - // Update the default language option if necessary
228 - if ( $this->options['default_lang'] == $old_slug ) {
243 + // update the default language option if necessary
244 + if ($this->options['default_lang'] == $old_slug)
229 245 $this->options['default_lang'] = $slug;
230 - }
231 246 }
232 247
233 - update_option( 'polylang', $this->options );
248 + update_option('polylang', $this->options);
234 249
235 - // And finally update the language itself
236 - $description = maybe_serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
237 - wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) );
238 - wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) );
250 + // and finally update the language itself
251 + $description = serialize(array('locale' => $args['locale'], 'rtl' => $args['rtl']));
252 + wp_update_term((int) $lang->term_id, 'language', array('slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => $args['term_group']));
253 + wp_update_term((int) $lang->tl_term_id, 'term_language', array('slug' => 'pll_' . $slug, 'name' => $args['name']));
239 254
240 - /**
241 - * Fires when a language is updated.
242 - *
243 - * @since 1.9
244 - *
245 - * @param array $args Arguments used to modify the language. @see PLL_Admin_Model::update_language().
246 - */
247 - do_action( 'pll_update_language', $args );
248 -
249 255 $this->clean_languages_cache();
250 - flush_rewrite_rules(); // Refresh rewrite rules
256 + flush_rewrite_rules(); // refresh rewrite rules
257 + add_settings_error('general', 'pll_languages_updated', __('Language updated.', 'polylang'), 'updated');
251 258 return true;
252 259 }
253 260
254 - /**
255 - * Validates data entered when creating or updating a language.
261 + /*
262 + * validates data entered when creating or updating a language
256 263 *
257 - * @see PLL_Admin_Model::add_language().
258 - *
259 264 * @since 0.4
260 265 *
261 - * @param array $args Parameters of {@see PLL_Admin_Model::add_language() or @see PLL_Admin_Model::update_language()}.
262 - * @param PLL_Language $lang Optional the language currently updated, the language is created if not set.
263 - * @return WP_Error
266 + * @param array $args
267 + * @param object $lang optional the language currently updated, the language is created if not set
268 + * @return bool true if success / false if failed
269 + * @see PLL_Admin_Model::add_language
264 270 */
265 - protected function validate_lang( $args, $lang = null ) {
266 - $errors = new WP_Error();
271 + protected function validate_lang($args, $lang = null) {
272 + // validate locale
273 + if ( !preg_match('#^[A-Za-z_]+$#', $args['locale']))
274 + add_settings_error('general', 'pll_invalid_locale', __('Enter a valid WordPress locale', 'polylang'));
267 275
268 - // Validate locale with the same pattern as WP 4.3. See #28303
269 - if ( ! preg_match( '#^[a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?$#', $args['locale'], $matches ) ) {
270 - $errors->add( 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) );
271 - }
276 + // validate slug characters
277 + if (!preg_match('#^[a-z_-]+$#', $args['slug']))
278 + add_settings_error('general', 'pll_invalid_slug', __('The language code contains invalid characters', 'polylang'));
272 279
273 - // Validate slug characters
274 - if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) {
275 - $errors->add( 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
276 - }
280 + // validate slug is unique
281 + if ($this->get_language($args['slug']) && ($lang === null || (isset($lang) && $lang->slug != $args['slug'])))
282 + add_settings_error('general', 'pll_non_unique_slug', __('The language code must be unique', 'polylang'));
277 283
278 - // Validate slug is unique
279 - foreach ( $this->get_languages_list() as $language ) {
280 - if ( $language->slug === $args['slug'] && ( null === $lang || $lang->term_id !== $language->term_id ) ) {
281 - $errors->add( 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
282 - }
283 - }
284 + // validate name
285 + if ($args['name'] == '')
286 + add_settings_error('general', 'pll_invalid_name', __('The language must have a name', 'polylang'));
284 287
285 - // Validate name
286 - // No need to sanitize it as wp_insert_term will do it for us
287 - if ( empty( $args['name'] ) ) {
288 - $errors->add( 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
289 - }
290 -
291 - // Validate flag
292 - if ( ! empty( $args['flag'] ) && ! file_exists( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) {
293 - $flag = PLL_Language::get_flag_informations( $args['flag'] );
294 -
295 - if ( ! empty( $flag['url'] ) ) {
296 - $response = function_exists( 'vip_safe_wp_remote_get' ) ? vip_safe_wp_remote_get( esc_url_raw( $flag['url'] ) ) : wp_remote_get( esc_url_raw( $flag['url'] ) );
297 - }
298 -
299 - if ( empty( $response ) || is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
300 - $errors->add( 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) );
301 - }
302 - }
303 -
304 - return $errors;
288 + return get_settings_errors() ? false : true;
305 289 }
306 290
307 - /**
308 - * Assigns a language to posts or terms in mass.
291 + /*
292 + * used to set the language of posts or terms in mass
309 293 *
310 294 * @since 1.2
311 295 *
312 - * @param string $type Either 'post' or 'term'.
313 - * @param int[] $ids Array of post ids or term ids.
314 - * @param PLL_Language|string $lang Language to assign to the posts or terms.
315 - * @return void
296 + * @param string $type either 'post' or 'term'
297 + * @param array $ids array of post ids or term ids
298 + * @param object|string $lang object or slug
316 299 */
317 - public function set_language_in_mass( $type, $ids, $lang ) {
300 + public function set_language_in_mass($type, $ids, $lang) {
318 301 global $wpdb;
319 302
320 - $lang = $this->get_language( $lang );
303 + $lang = $this->get_language($lang);
304 + $tt_id = 'term' == $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
321 305
322 - if ( empty( $lang ) ) {
323 - return;
324 - }
306 + foreach ($ids as $id)
307 + $values[] = $wpdb->prepare("(%d, %d)", $id, $tt_id);
325 308
326 - $tt_id = 'term' === $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
327 - $values = array();
328 - $ids = array_map( 'intval', $ids );
329 -
330 - foreach ( $ids as $id ) {
331 - $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
309 + if (!empty($values)) {
310 + $values = array_unique($values);
311 + $wpdb->query("INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id) VALUES " . implode(',', $values));
312 + $lang->update_count(); // updating term count is mandatory (thanks to AndyDeGroo)
332 313 }
333 -
334 - if ( ! empty( $values ) ) {
335 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
336 - $wpdb->query( "INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode( ',', array_unique( $values ) ) );
337 - $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo )
338 - }
339 -
340 - if ( 'term' === $type ) {
341 - clean_term_cache( $ids, 'term_language' );
342 - $translations = array();
343 -
344 - foreach ( $ids as $id ) {
345 - $translations[] = array( $lang->slug => $id );
346 - }
347 -
348 - if ( ! empty( $translations ) ) {
349 - $this->set_translation_in_mass( 'term', $translations );
350 - }
351 - } else {
352 - clean_term_cache( $ids, 'language' );
353 - }
354 314 }
355 315
356 - /**
357 - * Creates translations groups in mass.
316 + /*
317 + * returns unstranslated posts and terms ids (used in settings)
358 318 *
359 - * @since 1.6.3
360 - *
361 - * @param string $type Either 'post' or 'term'
362 - * @param array $translations Array of translations arrays.
363 - * @return void
364 - */
365 - public function set_translation_in_mass( $type, $translations ) {
366 - global $wpdb;
367 -
368 - $taxonomy = $type . '_translations';
369 - $terms = array();
370 - $slugs = array();
371 - $description = array();
372 - $count = array();
373 -
374 - foreach ( $translations as $t ) {
375 - $term = uniqid( 'pll_' ); // the term name
376 - $terms[] = $wpdb->prepare( '( %s, %s )', $term, $term );
377 - $slugs[] = $wpdb->prepare( '%s', $term );
378 - $description[ $term ] = maybe_serialize( $t );
379 - $count[ $term ] = count( $t );
380 - }
381 -
382 - // Insert terms
383 - if ( ! empty( $terms ) ) {
384 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
385 - $wpdb->query( "INSERT INTO {$wpdb->terms} ( slug, name ) VALUES " . implode( ',', array_unique( $terms ) ) );
386 - }
387 -
388 - // Get all terms with their term_id
389 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
390 - $terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug IN ( " . implode( ',', $slugs ) . ' )' );
391 - $term_ids = array();
392 - $tts = array();
393 -
394 - // Prepare terms taxonomy relationship
395 - foreach ( $terms as $term ) {
396 - $term_ids[] = $term->term_id;
397 - $tts[] = $wpdb->prepare( '( %d, %s, %s, %d )', $term->term_id, $taxonomy, $description[ $term->slug ], $count[ $term->slug ] );
398 - }
399 -
400 - // Insert term_taxonomy
401 - if ( ! empty( $tts ) ) {
402 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
403 - $wpdb->query( "INSERT INTO {$wpdb->term_taxonomy} ( term_id, taxonomy, description, count ) VALUES " . implode( ',', array_unique( $tts ) ) );
404 - }
405 -
406 - // Get all terms with term_taxonomy_id
407 - $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
408 - $trs = array();
409 -
410 - // Prepare objects relationships.
411 - if ( is_array( $terms ) ) {
412 - foreach ( $terms as $term ) {
413 - $t = maybe_unserialize( $term->description );
414 - if ( in_array( $t, $translations ) ) {
415 - foreach ( $t as $object_id ) {
416 - if ( ! empty( $object_id ) ) {
417 - $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
418 - }
419 - }
420 - }
421 - }
422 - }
423 -
424 - // Insert term_relationships
425 - if ( ! empty( $trs ) ) {
426 - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
427 - $wpdb->query( "INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) );
428 - $trs = array_unique( $trs );
429 - }
430 -
431 - clean_term_cache( $term_ids, $taxonomy );
432 - }
433 -
434 - /**
435 - * Returns untranslated posts and terms ids ( used in settings ).
436 - *
437 319 * @since 0.9
438 - * @since 2.2.6 Add the $limit argument.
439 320 *
440 - * @param int $limit Max number of posts or terms to return. Defaults to -1 (no limit).
441 - * @return array {
442 - * Objects without language.
443 - *
444 - * @type int[] $posts Array of post ids.
445 - * @type int[] $terms Array of term ids.
446 - * }
321 + * @return array array made of an array of post ids and an array of term ids
447 322 */
448 - public function get_objects_with_no_lang( $limit = -1 ) {
449 - global $wpdb;
323 + public function get_objects_with_no_lang() {
324 + $posts = get_posts(array(
325 + 'numberposts' => -1,
326 + 'nopaging' => true,
327 + 'post_type' => $this->get_translated_post_types(),
328 + 'post_status' => 'any',
329 + 'fields' => 'ids',
330 + 'tax_query' => array(array(
331 + 'taxonomy' => 'language',
332 + 'terms' => $this->get_languages_list(array('fields' => 'term_id')),
333 + 'operator' => 'NOT IN'
334 + ))
335 + ));
450 336
451 - /**
452 - * Filters the max number of posts or terms to return when searching objects with no language.
453 - * This filter can be used to decrease the memory usage in case the number of objects
454 - * without language is too big. Using a negative value is equivalent to have no limit.
455 - *
456 - * @since 2.2.6
457 - *
458 - * @param int $limit Max number of posts or terms to retrieve from the database.
459 - */
460 - $limit = (int) apply_filters( 'get_objects_with_no_lang_limit', $limit );
337 + $terms = get_terms($this->get_translated_taxonomies(), array('get'=>'all', 'fields'=>'ids'));
338 + $groups = $this->get_languages_list(array('fields' => 'tl_term_id'));
339 + $tr_terms = get_objects_in_term($groups, 'term_language');
340 + $terms = array_unique(array_diff($terms, $tr_terms)); // array_unique to avoid duplicates if a term is in more than one taxonomy
461 341
462 - $posts = get_posts(
463 - array(
464 - 'numberposts' => $limit,
465 - 'nopaging' => $limit <= 0,
466 - 'post_type' => $this->get_translated_post_types(),
467 - 'post_status' => 'any',
468 - 'fields' => 'ids',
469 - 'tax_query' => array(
470 - array(
471 - 'taxonomy' => 'language',
472 - 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ),
473 - 'operator' => 'NOT IN',
474 - ),
475 - ),
476 - )
477 - );
478 -
479 - // PHPCS:disable WordPress.DB.PreparedSQL
480 - $terms = $wpdb->get_col(
481 - sprintf(
482 - "SELECT {$wpdb->term_taxonomy}.term_id FROM {$wpdb->term_taxonomy}
483 - WHERE taxonomy IN ('%s')
484 - AND {$wpdb->term_taxonomy}.term_id NOT IN (
485 - SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s)
486 - )
487 - %s",
488 - implode( "','", array_map( 'esc_sql', $this->get_translated_taxonomies() ) ),
489 - implode( ',', array_map( 'intval', $this->get_languages_list( array( 'fields' => 'tl_term_taxonomy_id' ) ) ) ),
490 - $limit > 0 ? "LIMIT {$limit}" : ''
491 - )
492 - );
493 - // PHPCS:enable
494 -
495 - /**
496 - * Filters the list of untranslated posts ids and terms ids
497 - *
498 - * @since 0.9
499 - *
500 - * @param array|false $objects false if no ids found, list of post and/or term ids otherwise.
501 - */
502 - return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) );
342 + return apply_filters('pll_get_objects_with_no_lang', empty($posts) && empty($terms) ? false : array('posts' => $posts, 'terms' => $terms));
503 343 }
504 344
505 - /**
506 - * Updates the translations when a language slug has been modified in settings
507 - * or deletes them when a language is removed.
345 + /*
346 + * used to delete translations or update the translations when a language slug has been modified in settings
508 347 *
509 348 * @since 0.5
510 349 *
511 - * @param string $old_slug The old language slug.
512 - * @param string $new_slug Optional, the new language slug, if not set it means that the language has been deleted.
513 - * @return void
350 + * @param string $old_slug the old language slug
351 + * @param string $new_slug optional, the new language slug, if not set it means the correspondant has been deleted
514 352 */
515 - public function update_translations( $old_slug, $new_slug = '' ) {
353 + public function update_translations($old_slug, $new_slug = '') {
516 354 global $wpdb;
517 355
518 - $terms = get_terms( array( 'post_translations', 'term_translations' ) );
519 - $term_ids = array();
520 - $dr = array();
521 - $dt = array();
522 - $ut = array();
356 + $terms = get_terms(array('post_translations', 'term_translations'));
523 357
524 - if ( is_array( $terms ) ) {
525 - foreach ( $terms as $term ) {
526 - $term_ids[ $term->taxonomy ][] = $term->term_id;
527 - $tr = maybe_unserialize( $term->description );
528 - if ( ! empty( $tr[ $old_slug ] ) ) {
529 - if ( $new_slug ) {
530 - $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
531 - } else {
532 - $dr['id'][] = (int) $tr[ $old_slug ];
533 - $dr['tt'][] = (int) $term->term_taxonomy_id;
534 - }
535 - unset( $tr[ $old_slug ] );
358 + foreach ($terms as $term) {
359 + $tr = unserialize($term->description);
360 + if (!empty($tr[$old_slug])) {
361 + if ($new_slug)
362 + $tr[$new_slug] = $tr[$old_slug]; // suppress this for delete
363 + else {
364 + $dr['id'][] = (int) $tr[$old_slug];
365 + $dr['tt'][] = (int) $term->term_taxonomy_id;
366 + }
367 + unset($tr[$old_slug]);
536 368
537 - if ( empty( $tr ) || 1 == count( $tr ) ) {
538 - $dt['t'][] = (int) $term->term_id;
539 - $dt['tt'][] = (int) $term->term_taxonomy_id;
540 - } else {
541 - $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, maybe_serialize( $tr ) );
542 - $ut['in'][] = (int) $term->term_id;
543 - }
369 + if (empty($tr) || 1 == count($tr)) {
370 + $dt['t'][] = (int) $term->term_id;
371 + $dt['tt'][] = (int) $term->term_taxonomy_id;
544 372 }
373 + else {
374 + $ut['case'][] = $wpdb->prepare('WHEN %d THEN %s', $term->term_id, serialize($tr));
375 + $ut['in'][] = (int) $term->term_id;
376 + }
545 377 }
546 378 }
547 379
548 - // Delete relationships
549 - if ( ! empty( $dr ) ) {
550 - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared
551 - $wpdb->query(
552 - "DELETE FROM $wpdb->term_relationships
553 - WHERE object_id IN ( " . implode( ',', $dr['id'] ) . ' )
554 - AND term_taxonomy_id IN ( ' . implode( ',', $dr['tt'] ) . ' )'
555 - );
556 - // PHPCS:enable
557 - }
380 + // delete relationships
381 + if (!empty($dr))
382 + $wpdb->query("DELETE FROM $wpdb->term_relationships
383 + WHERE object_id IN ( " . implode(',', $dr['id']) . " )
384 + AND term_taxonomy_id IN ( " . implode(',', $dr['tt']) . " )");
558 385
559 - // Delete terms
560 - if ( ! empty( $dt ) ) {
561 - $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . ' )' ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
562 - $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode( ',', $dt['tt'] ) . ' )' ); // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared
386 + // delete terms
387 + if (!empty($dt)) {
388 + $wpdb->query("DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode(',', $dt['t']) . " ) ");
389 + $wpdb->query("DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode(',', $dt['tt']) . " ) ");
563 390 }
564 391
565 - // Update terms
566 - if ( ! empty( $ut ) ) {
567 - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared
568 - $wpdb->query(
569 - "UPDATE $wpdb->term_taxonomy
570 - SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . ' END )
571 - WHERE term_id IN ( ' . implode( ',', $ut['in'] ) . ' )'
572 - );
573 - // PHPCS:enable
574 - }
575 -
576 - if ( ! empty( $term_ids ) ) {
577 - foreach ( $term_ids as $taxonomy => $ids ) {
578 - clean_term_cache( $ids, $taxonomy );
579 - }
580 - }
581 - }
582 -
583 - /**
584 - * Updates the default language
585 - * taking care to update the default category & the nav menu locations.
586 - *
587 - * @since 1.8
588 - *
589 - * @param string $slug New language slug.
590 - * @return void
591 - */
592 - public function update_default_lang( $slug ) {
593 - // The nav menus stored in theme locations should be in the default language
594 - $theme = get_stylesheet();
595 - if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) {
596 - $menus = array();
597 -
598 - foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) {
599 - $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ];
600 - }
601 - set_theme_mod( 'nav_menu_locations', $menus );
602 - }
603 -
604 - // The default category should be in the default language
605 - $default_cats = $this->term->get_translations( get_option( 'default_category' ) );
606 - if ( isset( $default_cats[ $slug ] ) ) {
607 - update_option( 'default_category', $default_cats[ $slug ] );
608 - }
609 -
610 - // Update options
611 - $this->options['default_lang'] = $slug;
612 - update_option( 'polylang', $this->options );
613 -
614 - $this->clean_languages_cache();
615 - flush_rewrite_rules();
392 + // update terms
393 + if (!empty($ut))
394 + $wpdb->query("UPDATE $wpdb->term_taxonomy
395 + SET description = ( CASE term_id " . implode(' ', $ut['case']) . " END )
396 + WHERE term_id IN ( " . implode(',', $ut['in']) . " )");
616 397 }
617 398 }