PluginProbe
Polylang / 2.0.8
Polylang v2.0.8
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 / admin-model.php

admin-model.php in Polylang 2.0.8, at admin/admin-model.php

527 lines 18.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Extends the PLL_Model class with methods needed only in Polylang settings pages
5 *
6 * @since 1.2
7 */
8 class PLL_Admin_Model extends PLL_Model {
9
10 /**
11 * Adds a new language
12 * Creates a default category for this language
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 * flag -> country code, see flags.php
24 *
25 * @since 1.2
26 *
27 * @param array $args
28 * @return bool true if success / false if failed
29 */
30 public function add_language( $args ) {
31 if ( ! $this->validate_lang( $args ) ) {
32 return false;
33 }
34
35 // First the language taxonomy
36 $description = serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
37 $r = wp_insert_term( $args['name'], 'language', array( 'slug' => $args['slug'], 'description' => $description ) );
38 if ( is_wp_error( $r ) ) {
39 // Avoid an ugly fatal error if something went wrong ( reported once in the forum )
40 add_settings_error( 'general', 'pll_add_language', __( 'Impossible to add the language.', 'polylang' ) );
41 return false;
42 }
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
44
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'] ) );
48
49 $this->clean_languages_cache(); // Udpate the languages list now !
50
51 if ( ! isset( $this->options['default_lang'] ) ) {
52 // If this is the first language created, set it as default language
53 $this->options['default_lang'] = $args['slug'];
54 update_option( 'polylang', $this->options );
55
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'] );
60 }
61
62 // Init a mo_id for this language
63 $mo = new PLL_MO();
64 $mo->export_to_db( $this->get_language( $args['slug'] ) );
65
66 /**
67 * Fires when a language is added
68 *
69 * @since 1.9
70 *
71 * @param array $args arguments used to create the 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
77
78 add_settings_error( 'general', 'pll_languages_created', __( 'Language added.', 'polylang' ), 'updated' );
79 return true;
80 }
81
82 /**
83 * Delete a language
84 *
85 * @since 1.2
86 *
87 * @param int $lang_id language term_id
88 */
89 public function delete_language( $lang_id ) {
90 $lang = $this->get_language( (int) $lang_id );
91
92 if ( empty( $lang ) ) {
93 return;
94 }
95
96 // Oops ! we are deleting the default language...
97 // Need to do this before loosing the information for default category translations
98 if ( $this->options['default_lang'] == $lang->slug ) {
99 $slugs = $this->get_languages_list( array( 'fields' => 'slug' ) );
100 $slugs = array_diff( $slugs, array( $lang->slug ) );
101
102 if ( ! empty( $slugs ) ) {
103 $this->update_default_lang( reset( $slugs ) ); // Arbitrary choice...
104 } else {
105 unset( $this->options['default_lang'] );
106 }
107 }
108
109 // Delete the translations
110 $this->update_translations( $lang->slug );
111
112 // Delete language option in widgets
113 foreach ( $GLOBALS['wp_registered_widgets'] as $widget ) {
114 if ( ! empty( $widget['callback'][0] ) && ! empty( $widget['params'][0]['number'] ) ) {
115 $obj = $widget['callback'][0];
116 $number = $widget['params'][0]['number'];
117 if ( is_object( $obj ) && method_exists( $obj, 'get_settings' ) && method_exists( $obj, 'save_settings' ) ) {
118 $settings = $obj->get_settings();
119 if ( isset( $settings[ $number ]['pll_lang'] ) && $settings[ $number ]['pll_lang'] == $lang->slug ) {
120 unset( $settings[ $number ]['pll_lang'] );
121 $obj->save_settings( $settings );
122 }
123 }
124 }
125 }
126
127 // Delete menus locations
128 if ( ! empty( $this->options['nav_menus'] ) ) {
129 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
130 foreach ( $locations as $location => $languages ) {
131 unset( $this->options['nav_menus'][ $theme ][ $location ][ $lang->slug ] );
132 }
133 }
134 }
135
136 // Delete users options
137 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
138 delete_user_meta( $user_id, 'pll_filter_content', $lang->slug );
139 delete_user_meta( $user_id, 'description_'.$lang->slug );
140 }
141
142 // Delete the string translations
143 $post = wpcom_vip_get_page_by_title( 'polylang_mo_' . $lang->term_id, OBJECT, 'polylang_mo' );
144 if ( ! empty( $post ) ) {
145 wp_delete_post( $post->ID );
146 }
147
148 // Delete domain
149 unset( $this->options['domains'][ $lang->slug ] );
150
151 // Delete the language itself
152 wp_delete_term( $lang->term_id, 'language' );
153 wp_delete_term( $lang->tl_term_id, 'term_language' );
154
155 // Update languages list
156 $this->clean_languages_cache();
157
158 update_option( 'polylang', $this->options );
159 flush_rewrite_rules(); // refresh rewrite rules
160 add_settings_error( 'general', 'pll_languages_deleted', __( 'Language deleted.', 'polylang' ), 'updated' );
161 }
162
163 /**
164 * Update language properties
165 *
166 * List of arguments that $args must contain:
167 * lang_id -> term_id of the language to modify
168 * name -> language name ( used only for display )
169 * slug -> language code ( ideally 2-letters ISO 639-1 language code
170 * locale -> WordPress locale. If something wrong is used for the locale, the .mo files will not be loaded...
171 * rtl -> 1 if rtl language, 0 otherwise
172 * term_group -> language order when displayed
173 *
174 * Optional arguments that $args can contain:
175 * flag -> country code, see flags.php
176 *
177 * @since 1.2
178 *
179 * @param array $args
180 * @return bool true if success / false if failed
181 */
182 public function update_language( $args ) {
183 $lang = $this->get_language( (int) $args['lang_id'] );
184 if ( ! $this->validate_lang( $args, $lang ) ) {
185 return false;
186 }
187
188 // Update links to this language in posts and terms in case the slug has been modified
189 $slug = $args['slug'];
190 $old_slug = $lang->slug;
191
192 if ( $old_slug != $slug ) {
193 // Update the language slug in translations
194 $this->update_translations( $old_slug, $slug );
195
196 // Update language option in widgets
197 foreach ( $GLOBALS['wp_registered_widgets'] as $widget ) {
198 if ( ! empty( $widget['callback'][0] ) && ! empty( $widget['params'][0]['number'] ) ) {
199 $obj = $widget['callback'][0];
200 $number = $widget['params'][0]['number'];
201 if ( is_object( $obj ) && method_exists( $obj, 'get_settings' ) && method_exists( $obj, 'save_settings' ) ) {
202 $settings = $obj->get_settings();
203 if ( isset( $settings[ $number ]['pll_lang'] ) && $settings[ $number ]['pll_lang'] == $old_slug ) {
204 $settings[ $number ]['pll_lang'] = $slug;
205 $obj->save_settings( $settings );
206 }
207 }
208 }
209 }
210
211 // Update menus locations
212 if ( ! empty( $this->options['nav_menus'] ) ) {
213 foreach ( $this->options['nav_menus'] as $theme => $locations ) {
214 foreach ( $locations as $location => $languages ) {
215 if ( ! empty( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] ) ) {
216 $this->options['nav_menus'][ $theme ][ $location ][ $slug ] = $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ];
217 unset( $this->options['nav_menus'][ $theme ][ $location ][ $old_slug ] );
218 }
219 }
220 }
221 }
222
223 // Update domains
224 if ( ! empty( $this->options['domains'][ $old_slug ] ) ) {
225 $this->options['domains'][ $slug ] = $this->options['domains'][ $old_slug ];
226 unset( $this->options['domains'][ $old_slug ] );
227 }
228
229 // Update the default language option if necessary
230 if ( $this->options['default_lang'] == $old_slug ) {
231 $this->options['default_lang'] = $slug;
232 }
233 }
234
235 update_option( 'polylang', $this->options );
236
237 // And finally update the language itself
238 $description = serialize( array( 'locale' => $args['locale'], 'rtl' => (int) $args['rtl'], 'flag_code' => empty( $args['flag'] ) ? '' : $args['flag'] ) );
239 wp_update_term( (int) $lang->term_id, 'language', array( 'slug' => $slug, 'name' => $args['name'], 'description' => $description, 'term_group' => (int) $args['term_group'] ) );
240 wp_update_term( (int) $lang->tl_term_id, 'term_language', array( 'slug' => 'pll_' . $slug, 'name' => $args['name'] ) );
241
242 /**
243 * Fires when a language is added
244 *
245 * @since 1.9
246 *
247 * @param array $args arguments used to modify the language
248 */
249 do_action( 'pll_update_language', $args );
250
251 $this->clean_languages_cache();
252 flush_rewrite_rules(); // Refresh rewrite rules
253 add_settings_error( 'general', 'pll_languages_updated', __( 'Language updated.', 'polylang' ), 'updated' );
254 return true;
255 }
256
257 /**
258 * Validates data entered when creating or updating a language
259 * @see PLL_Admin_Model::add_language
260 *
261 * @since 0.4
262 *
263 * @param array $args
264 * @param object $lang optional the language currently updated, the language is created if not set
265 * @return bool true if success / false if failed
266 */
267 protected function validate_lang( $args, $lang = null ) {
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 add_settings_error( 'general', 'pll_invalid_locale', __( 'Enter a valid WordPress locale', 'polylang' ) );
271 }
272
273 // Validate slug characters
274 if ( ! preg_match( '#^[a-z_-]+$#', $args['slug'] ) ) {
275 add_settings_error( 'general', 'pll_invalid_slug', __( 'The language code contains invalid characters', 'polylang' ) );
276 }
277
278 // Validate slug is unique
279 if ( $this->get_language( $args['slug'] ) && ( null === $lang || ( isset( $lang ) && $lang->slug != $args['slug'] ) ) ) {
280 add_settings_error( 'general', 'pll_non_unique_slug', __( 'The language code must be unique', 'polylang' ) );
281 }
282
283 // Validate name
284 // No need to sanitize it as wp_insert_term will do it for us
285 if ( empty( $args['name'] ) ) {
286 add_settings_error( 'general', 'pll_invalid_name', __( 'The language must have a name', 'polylang' ) );
287 }
288
289 // Validate flag
290 if ( ! empty( $args['flag'] ) && ! file_exists( POLYLANG_DIR . '/flags/' . $args['flag'] . '.png' ) ) {
291 add_settings_error( 'general', 'pll_invalid_flag', __( 'The flag does not exist', 'polylang' ) );
292 }
293
294 return get_settings_errors() ? false : true;
295 }
296
297 /**
298 * Used to set the language of posts or terms in mass
299 *
300 * @since 1.2
301 *
302 * @param string $type either 'post' or 'term'
303 * @param array $ids array of post ids or term ids
304 * @param object|string $lang object or slug
305 */
306 public function set_language_in_mass( $type, $ids, $lang ) {
307 global $wpdb;
308
309 $ids = array_map( 'intval', $ids );
310 $lang = $this->get_language( $lang );
311 $tt_id = 'term' == $type ? $lang->tl_term_taxonomy_id : $lang->term_taxonomy_id;
312
313 foreach ( $ids as $id ) {
314 $values[] = $wpdb->prepare( '( %d, %d )', $id, $tt_id );
315 }
316
317 if ( ! empty( $values ) ) {
318 $values = array_unique( $values );
319 $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $values ) );
320 $lang->update_count(); // Updating term count is mandatory ( thanks to AndyDeGroo )
321 }
322
323 if ( 'term' == $type ) {
324 foreach ( $ids as $id ) {
325 $translations[] = array( $lang->slug => $id );
326 }
327
328 if ( ! empty( $translations ) ) {
329 $this->set_translation_in_mass( 'term', $translations );
330 }
331 }
332 }
333
334 /**
335 * Used to create a translations groups in mass
336 *
337 * @since 1.6.3
338 *
339 * @param string $type either 'post' or 'term'
340 * @param array $translations array of translations arrays
341 */
342 public function set_translation_in_mass( $type, $translations ) {
343 global $wpdb;
344
345 foreach ( $translations as $t ) {
346 $term = uniqid( 'pll_' ); // the term name
347 $terms[] = $wpdb->prepare( '( "%1$s", "%1$s" )', $term );
348 $slugs[] = $wpdb->prepare( '"%s"', $term );
349 $description[ $term ] = serialize( $t );
350 $count[ $term ] = count( $t );
351 }
352
353 // Insert terms
354 if ( ! empty( $terms ) ) {
355 $terms = array_unique( $terms );
356 $wpdb->query( "INSERT INTO $wpdb->terms ( slug, name ) VALUES " . implode( ',', $terms ) );
357 }
358
359 // Get all terms with their term_id
360 $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug IN ( " . implode( ',', $slugs ) . " )" );
361
362 // Prepare terms taxonomy relationship
363 foreach ( $terms as $term ) {
364 $tts[] = $wpdb->prepare( '( %d, "%s", "%s", %d )', $term->term_id, $type . '_translations', $description[ $term->slug ], $count[ $term->slug ] );
365 }
366
367 // Insert term_taxonomy
368 if ( ! empty( $tts ) ) {
369 $tts = array_unique( $tts );
370 $wpdb->query( "INSERT INTO $wpdb->term_taxonomy ( term_id, taxonomy, description, count ) VALUES " . implode( ',', $tts ) );
371 }
372
373 // Get all terms with term_taxonomy_id
374 $terms = get_terms( $type . '_translations', array( 'hide_empty' => false ) );
375
376 // Prepare objects relationships
377 foreach ( $terms as $term ) {
378 $t = unserialize( $term->description );
379 if ( in_array( $t, $translations ) ) {
380 foreach ( $t as $object_id ) {
381 if ( ! empty( $object_id ) ) {
382 $trs[] = $wpdb->prepare( '( %d, %d )', $object_id, $term->term_taxonomy_id );
383 }
384 }
385 }
386 }
387
388 // Insert term_relationships
389 if ( ! empty( $trs ) ) {
390 $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) );
391 $trs = array_unique( $trs );
392 }
393 }
394
395 /**
396 * Returns unstranslated posts and terms ids ( used in settings )
397 *
398 * @since 0.9
399 *
400 * @return array array made of an array of post ids and an array of term ids
401 */
402 public function get_objects_with_no_lang() {
403 $posts = get_posts( array(
404 'numberposts' => -1,
405 'nopaging' => true,
406 'post_type' => $this->get_translated_post_types(),
407 'post_status' => 'any',
408 'fields' => 'ids',
409 'tax_query' => array( array(
410 'taxonomy' => 'language',
411 'terms' => $this->get_languages_list( array( 'fields' => 'term_id' ) ),
412 'operator' => 'NOT IN',
413 ) )
414 ) );
415
416 $terms = get_terms( $this->get_translated_taxonomies(), array( 'get' => 'all', 'fields' => 'ids' ) );
417 $groups = $this->get_languages_list( array( 'fields' => 'tl_term_id' ) );
418 $tr_terms = get_objects_in_term( $groups, 'term_language' );
419 $terms = array_unique( array_diff( $terms, $tr_terms ) ); // array_unique to avoid duplicates if a term is in more than one taxonomy
420 $terms = array_map( 'intval', $terms );
421
422 /**
423 * Filter the list of untranslated posts ids and terms ids
424 *
425 * @since 0.9
426 *
427 * @param bool|array $objects false if no ids found, list of post and/or term ids otherwise
428 */
429 return apply_filters( 'pll_get_objects_with_no_lang', empty( $posts ) && empty( $terms ) ? false : array( 'posts' => $posts, 'terms' => $terms ) );
430 }
431
432 /**
433 * Used to delete translations or update the translations when a language slug has been modified in settings
434 *
435 * @since 0.5
436 *
437 * @param string $old_slug the old language slug
438 * @param string $new_slug optional, the new language slug, if not set it means the correspondant has been deleted
439 */
440 public function update_translations( $old_slug, $new_slug = '' ) {
441 global $wpdb;
442
443 $terms = get_terms( array( 'post_translations', 'term_translations' ) );
444
445 foreach ( $terms as $term ) {
446 $tr = unserialize( $term->description );
447 if ( ! empty( $tr[ $old_slug ] ) ) {
448 if ( $new_slug ) {
449 $tr[ $new_slug ] = $tr[ $old_slug ]; // Suppress this for delete
450 } else {
451 $dr['id'][] = (int) $tr[ $old_slug ];
452 $dr['tt'][] = (int) $term->term_taxonomy_id;
453 }
454 unset( $tr[ $old_slug ] );
455
456 if ( empty( $tr ) || 1 == count( $tr ) ) {
457 $dt['t'][] = (int) $term->term_id;
458 $dt['tt'][] = (int) $term->term_taxonomy_id;
459 } else {
460 $ut['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $term->term_id, serialize( $tr ) );
461 $ut['in'][] = (int) $term->term_id;
462 }
463 }
464 }
465
466 // Delete relationships
467 if ( ! empty( $dr ) ) {
468 $wpdb->query( "
469 DELETE FROM $wpdb->term_relationships
470 WHERE object_id IN ( " . implode( ',', $dr['id'] ) . " )
471 AND term_taxonomy_id IN ( " . implode( ',', $dr['tt'] ) . " )
472 " );
473 }
474
475 // Delete terms
476 if ( ! empty( $dt ) ) {
477 $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $dt['t'] ) . " ) " );
478 $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ( " . implode( ',', $dt['tt'] ) . " ) " );
479 }
480
481 // Update terms
482 if ( ! empty( $ut ) ) {
483 $wpdb->query( "
484 UPDATE $wpdb->term_taxonomy
485 SET description = ( CASE term_id " . implode( ' ', $ut['case'] ) . " END )
486 WHERE term_id IN ( " . implode( ',', $ut['in'] ) . " )
487 " );
488 }
489
490 foreach ( $terms as $term ) {
491 clean_term_cache( $term->term_id, $term->taxonomy );
492 }
493 }
494
495 /**
496 * Updates the default language
497 * taking care to update the default category & the nav menu locations
498 *
499 * @since 1.8
500 *
501 * @param string $slug new language slug
502 */
503 public function update_default_lang( $slug ) {
504 // The nav menus stored in theme locations should be in the default language
505 $theme = get_stylesheet();
506 if ( ! empty( $this->options['nav_menus'][ $theme ] ) ) {
507 foreach ( $this->options['nav_menus'][ $theme ] as $key => $loc ) {
508 $menus[ $key ] = empty( $loc[ $slug ] ) ? 0 : $loc[ $slug ];
509 }
510 set_theme_mod( 'nav_menu_locations', $menus );
511 }
512
513 // The default category should be in the default language
514 $default_cats = $this->term->get_translations( get_option( 'default_category' ) );
515 if ( isset( $default_cats[ $slug ] ) ) {
516 update_option( 'default_category', $default_cats[ $slug ] );
517 }
518
519 // Update options
520 $this->options['default_lang'] = $slug;
521 update_option( 'polylang', $this->options );
522
523 $this->clean_languages_cache();
524 flush_rewrite_rules();
525 }
526 }
527