PluginProbe
Polylang / 0.4.1
Polylang v0.4.1
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 / include / admin.php

admin.php in Polylang 0.4.1, at include/admin.php

675 lines 23.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once( ABSPATH . WPINC . '/pluggable.php' ); // to ensure that 'get_current_user_id' is defined
4
5 require_once(INC_DIR.'/list-table.php');
6
7 class Polylang_Admin extends Polylang_Base {
8 function __construct() {
9 add_action('admin_init', array(&$this, 'admin_init'));
10
11 // adds a 'settings' link in the plugins table
12 $plugin_file = basename(POLYLANG_DIR).'/polylang.php';
13 add_filter('plugin_action_links_'.$plugin_file, array(&$this, 'plugin_action_links'));
14
15 // adds the link to the languages panel in the wordpress admin menu
16 add_action('admin_menu', array(&$this, 'add_menus'));
17
18 // setup js scripts andd css styles
19 add_action('admin_print_scripts', array($this,'admin_js'));
20 add_action('admin_print_styles', array($this,'admin_css'));
21
22 // add the language column (as well as a filter by language) in 'All Posts' an 'All Pages' panels
23 add_filter('manage_posts_columns', array(&$this, 'add_post_column'), 10, 2);
24 add_filter('manage_pages_columns', array(&$this, 'add_post_column'));
25 add_action('manage_posts_custom_column', array(&$this, 'post_column'), 10, 2);
26 add_action('manage_pages_custom_column', array(&$this, 'post_column'), 10, 2);
27 add_filter('parse_query',array(&$this,'parse_query'));
28 add_action('restrict_manage_posts', array(&$this, 'restrict_manage_posts'));
29
30 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels
31 add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));
32
33 // ajax response for post metabox
34 add_action('wp_ajax_post_lang_choice', array($this,'post_lang_choice'));
35
36 // adds actions related to languages when saving or deleting posts and pages
37 add_action('save_post', array(&$this, 'save_post'));
38 add_action('before_delete_post', array(&$this, 'delete_post'));
39
40 // ajax response for edit term form
41 add_action('wp_ajax_term_lang_choice', array($this,'term_lang_choice'));
42
43 // modifies the theme location nav menu metabox
44 add_filter('admin_head-nav-menus.php', array(&$this, 'nav_menu_theme_locations'));
45
46 // widgets languages filter
47 add_action('in_widget_form', array(&$this, 'in_widget_form'));
48 add_filter('widget_update_callback', array(&$this, 'widget_update_callback'), 10, 4);
49
50 // language management for users
51 add_filter('locale', array(&$this, 'get_locale'));
52 add_action('personal_options_update', array(&$this, 'personal_options_update'));
53 add_action('personal_options', array(&$this, 'personal_options'));
54
55 // refresh rewrite rules if the 'page_on_front' option is modified
56 add_action('update_option', array(&$this, 'update_option'));
57 }
58
59 function admin_init() {
60 // add these actions and filters here and not in the constructor to be sure all taxonomies are registered
61 foreach (get_taxonomies(array('show_ui'=>true)) as $tax) {
62 // adds the language field in the 'Categories' and 'Post Tags' panels
63 add_action($tax.'_add_form_fields', array(&$this, 'add_term_form'));
64
65 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
66 add_action($tax.'_edit_form_fields', array(&$this, 'edit_term_form'));
67
68 // adds the language column in the 'Categories' and 'Post Tags' tables
69 add_filter('manage_edit-'.$tax.'_columns', array(&$this, 'add_term_column'));
70 add_action('manage_'.$tax.'_custom_column', array(&$this, 'term_column'), 10, 3);
71
72 // adds actions related to languages when saving or deleting categories and post tags
73 add_action('created_'.$tax, array(&$this, 'save_term'));
74 add_action('edited_'.$tax, array(&$this, 'save_term'));
75 add_action('delete_'.$tax, array(&$this, 'delete_term'));
76 }
77
78 // manage versions
79 $options = get_option('polylang');
80 if ($options['version'] < POLYLANG_VERSION) {
81
82 if($options['version'] < '0.4')
83 $options['hide_default'] = 0; // option introduced in 0.4
84
85 $options['version'] = POLYLANG_VERSION;
86 update_option('polylang', $options);
87 }
88 }
89
90 // adds a 'settings' link in the plugins table
91 function plugin_action_links($links) {
92 $settings_link = '<a href="admin.php?page=mlang">' . __('Settings') . '</a>';
93 array_unshift( $links, $settings_link );
94 return $links;
95 }
96
97 // adds the link to the languages panel in the wordpress admin menu
98 function add_menus() {
99 add_submenu_page('options-general.php', __('Languages', 'polylang'), __('Languages', 'polylang'), 'manage_options', 'mlang', array($this, 'languages_page'));
100 }
101
102 // setup js scripts
103 function admin_js() {
104 wp_enqueue_script('polylang_admin', WP_PLUGIN_URL .'/polylang/js/admin.js');
105 }
106
107 // setup css styles
108 function admin_css() {
109 wp_enqueue_style('polylang_admin', WP_PLUGIN_URL .'/polylang/css/admin.css');
110 }
111
112 // the languages panel
113 function languages_page() {
114 global $wp_rewrite;
115 $options = get_option('polylang');
116
117 $listlanguages = $this->get_languages_list();
118 $list_table = new Polylang_List_Table();
119
120 // for nav menus form
121 $locations = get_registered_nav_menus();
122 $menus = wp_get_nav_menus();
123 $menu_lang = get_option('polylang_nav_menus');
124
125 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
126
127 switch ($action) {
128 case 'add':
129 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
130 $error = $this->validate_lang();
131
132 if ($error == 0) {
133 wp_insert_term($_POST['name'],'language',array('slug'=>$_POST['slug'], 'description'=>$_POST['description']));
134 $wp_rewrite->flush_rules(); // refresh rewrite rules
135
136 if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language
137 $options['default_lang'] = $_POST['slug'];
138 update_option('polylang', $options);
139 }
140 }
141
142 wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true)
143 exit;
144 break;
145
146 case 'delete':
147 check_admin_referer( 'delete-lang');
148
149 if (isset($_GET['lang']) && $_GET['lang']) {
150 $lang_id = (int) $_GET['lang'];
151 $lang = $this->get_language($lang_id);
152 $lang_slug = $lang->slug;
153
154 // delete all translations in posts in this language
155 $args = array('numberposts'=>-1, 'taxonomy' => 'language', 'term' => $lang_slug, 'post_type'=>'any', 'post_status'=>'any');
156 $posts = get_posts($args);
157 foreach ($posts as $post) {
158 foreach ($listlanguages as $language) {
159 delete_post_meta($post->ID, '_lang-'.$language->slug);
160 }
161 }
162
163 // delete references to this language in all posts
164 $args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$lang_slug, 'post_type'=>'any', 'post_status'=>'any');
165 $posts = get_posts($args);
166 foreach ($posts as $post) {
167 delete_post_meta($post->ID, '_lang-'.$lang_slug);
168 }
169
170 // delete references to this language in categories & post tags
171 $terms = get_terms(get_taxonomies(array('show_ui'=>true)), 'get=all');
172 foreach ($terms as $term) {
173 if ($this->get_term_language($term->term_id) == $lang) {
174 foreach ($listlanguages as $language) {
175 delete_metadata('term', $term->term_id, '_lang-'.$language->slug); // deletes translations of this term
176 }
177 delete_metadata('term', $term->term_id, '_language', $lang_id); // delete language of this term
178 }
179 delete_metadata('term', $term->term_id, '_lang-'.$lang_slug); // deletes references to this term in translated term
180 }
181
182 // delete the language itself
183 wp_delete_term($lang_id, 'language');
184 $wp_rewrite->flush_rules(); // refresh rewrite rules
185
186 // oops ! we deleted the default language...
187 if ($options['default_lang'] == $lang_slug) {
188 if (!empty($listlanguages))
189 $options['default_lang'] = reset($this->get_languages_list())->slug; // arbitrary choice...
190 else
191 unset($options['default_lang']);
192 update_option('polylang', $options);
193 }
194 }
195 wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true)
196 exit;
197 break;
198
199 case 'edit':
200 if (isset($_GET['lang']) && $_GET['lang'])
201 $edit_lang = $this->get_language((int) $_GET['lang']);
202 break;
203
204 case 'update':
205 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
206 $lang_id = (int) $_POST['lang'];
207 $lang = $this->get_language($lang_id);
208 $error = $this->validate_lang($lang);
209
210 if ($error == 0) {
211 // Update links to this language in posts and terms in case the slug has been modified
212 $old_slug = $lang->slug;
213
214 if ($old_slug != $_POST['slug']) {
215 // update the language slug in posts meta
216 $args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$old_slug, 'post_type'=>'any', 'post_status'=>'any');
217 $posts = get_posts($args);
218 foreach ($posts as $post) {
219 $post_id = get_post_meta($post->ID, '_lang-'.$old_slug, true);
220 delete_post_meta($post->ID, '_lang-'.$old_slug);
221 update_post_meta($post->ID, '_lang-'.$_POST['slug'], $post_id);
222 }
223
224 // update the language slug in categories & post tags meta
225 $terms = get_terms(get_taxonomies(array('show_ui'=>true)), 'get=all');
226 foreach ($terms as $term) {
227 if ($term_id = get_metadata('term', $term->term_id, '_lang-'.$old_slug, true)) {
228 delete_metadata('term', $term->term_id, '_lang-'.$old_slug);
229 update_metadata('term', $term->term_id, '_lang-'.$_POST['slug'], $term_id);
230 }
231 }
232 }
233
234 // and finally update the language itself
235 wp_update_term($lang_id, 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description']));
236 $wp_rewrite->flush_rules(); // refresh rewrite rules
237 }
238
239 wp_redirect('admin.php?page=mlang'. ($error ? '&error='.$error : '') ); // to refresh the page (possible thanks to the $_GET['noheader']=true)
240 exit;
241 break;
242
243 case 'nav-menus':
244 check_admin_referer( 'nav-menus-lang', '_wpnonce_nav-menus-lang' );
245
246 $menu_lang = $_POST['menu-lang'];
247 update_option('polylang_nav_menus', $menu_lang);
248 break;
249
250 case 'options':
251 check_admin_referer( 'options-lang', '_wpnonce_options-lang' );
252
253 $options['default_lang'] = $_POST['default_lang'];
254 $options['browser'] = isset($_POST['browser']) ? 1 : 0;
255 $options['rewrite'] = $_POST['rewrite'];
256 $options['hide_default'] = isset($_POST['hide_default']) ? 1 : 0;
257 update_option('polylang', $options);
258
259 // refresh refresh permalink structure and rewrite rules in case rewrite or hide_default options have been modified
260 $wp_rewrite->extra_permastructs['language'][0] = $options['rewrite'] ? '%language%' : '/language/%language%';
261 $wp_rewrite->flush_rules();
262
263 // fills existing posts & terms with default language
264 if (isset($_POST['fill_languages'])) {
265 if(isset($_POST['posts'])) {
266 foreach(explode(',', $_POST['posts']) as $post_id) {
267 wp_set_post_terms($post_id, $options['default_lang'], 'language' );
268 }
269 }
270 if(isset($_POST['terms'])) {
271 foreach(explode(',', $_POST['terms']) as $term_id) {
272 update_metadata('term', $term_id, '_language', $options['default_lang'] );
273 }
274 }
275 }
276 break;
277
278 default:
279 }
280
281 // prepare the list table of languages
282 $data = array();
283 foreach ($listlanguages as $lang)
284 $data[] = (array) $lang;
285
286 $list_table->prepare_items($data);
287
288
289 // detects posts & pages without language set
290 $q = array(
291 'numberposts'=>-1,
292 'post_type' => 'any',
293 'post_status'=>'any',
294 'fields' => 'ids',
295 'tax_query' => array(array(
296 'taxonomy'=> 'language',
297 'terms'=> get_terms('language', array('fields'=>'ids')),
298 'operator'=>'NOT IN'
299 ))
300 );
301 $posts = implode(',', get_posts($q));
302
303 // detects categories & post tags without language set
304 $terms = get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids'));
305 foreach ($terms as $key => $term_id) {
306 if (get_metadata('term', $term_id, '_language'))
307 unset($terms[$key]);
308 }
309 $terms = implode(',', $terms);
310
311 $errors[1] = __('Enter a valid WorPress locale', 'polylang');
312 $errors[2] = __('The language code must be 2 characters long', 'polylang');
313 $errors[3] = __('The language code must be unique', 'polylang');
314 $errors[4] = __('The language must have a name', 'polylang');
315
316 // displays the page
317 include(INC_DIR.'/languages-form.php');
318 }
319
320 // validates data entered when creating or updating a language
321 function validate_lang($lang = null) {
322 // validate locale
323 $loc = $_POST['description'];
324 if ( !preg_match('#^[a-z]{2}$#', $loc) && !preg_match('#^[a-z]{2}_[A-Z]{2}$#', $loc) )
325 $error = 1;
326
327 // validate slug length
328 if (strlen($_POST['slug']) != 2)
329 $error = 2;
330
331 // validate slug is unique
332 if ($this->get_language($_POST['slug']) != null && isset($lang) && $lang->slug != $_POST['slug'])
333 $error = 3;
334
335 // validate name
336 if ($_POST['name'] == '')
337 $error = 4;
338
339 return isset($error) ? $error : 0;
340 }
341
342 // adds the language column (before the date column) in the posts and pages list table
343 function add_post_column($columns, $post_type ='') {
344 if ($post_type == '' || get_post_type_object($post_type)->show_ui) {
345 foreach (array( 'date', 'comments' ) as $k) {
346 if (array_key_exists($k, $columns))
347 $end[$k] = array_pop($columns);
348 }
349
350 $columns['language'] = __('Language', 'polylang');
351
352 if (isset($end))
353 $columns = array_merge($columns, $end);
354 }
355 return $columns;
356 }
357
358 // fills the language column in the posts table
359 function post_column($column, $post_id) {
360 if ($column == 'language' && $lang = $this->get_post_language($post_id))
361 echo esc_attr($lang->name);
362 }
363
364 // converts language term_id to slug in $query
365 // needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts
366 function parse_query($query) {
367 global $pagenow;
368 $qvars = &$query->query_vars;
369 if ($pagenow=='edit.php' && isset($qvars['lang']) && $qvars['lang'] && is_numeric($qvars['lang']) && $lang = $this->get_language($qvars['lang']))
370 $qvars['lang'] = $lang->slug;
371 }
372
373 // adds a filter for languages in the Posts and Pages panels
374 function restrict_manage_posts() {
375 global $wp_query;
376 $screen = get_current_screen(); // since WP 3.1
377 $languages = $this->get_languages_list();
378
379 if (!empty($languages) && $screen->base == 'edit') {
380 $qvars = $wp_query->query;
381 wp_dropdown_categories(array(
382 'show_option_all' => __('Show all languages', 'polylang'),
383 'name' => 'lang',
384 'selected' => isset($qvars['lang']) ? $qvars['lang'] : 0,
385 'taxonomy' => 'language',
386 'hide_empty' => 0
387 ));
388 }
389 }
390
391 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels (as well as in custom post types panels
392 function add_meta_boxes() {
393 foreach(get_post_types( array( 'show_ui' => true ) ) as $ptype)
394 add_meta_box('ml_box', __('Languages','polylang'), array(&$this,'post_language'), $ptype, 'side','high');
395 }
396
397 // the Languages metabox in the 'Edit Post' and 'Edit Page' panels
398 function post_language() {
399 global $post_ID;
400 $post_type = get_post_type($post_ID);
401
402 $lang = $this->get_post_language($post_ID);
403 if (isset($_GET['new_lang']))
404 $lang = $this->get_language($_GET['new_lang']);
405
406 $listlanguages = $this->get_languages_list();
407
408 include(INC_DIR.'/post-metabox.php');
409 }
410
411 // ajax response for post metabox
412 function post_lang_choice() {
413 $listlanguages = $this->get_languages_list();
414
415 $lang = $this->get_language($_POST['lang']);
416 $post_ID = $_POST['post_id'];
417 $post_type = get_post_type($post_ID);
418
419 if ($lang && !is_wp_error($lang))
420 include(INC_DIR.'/post-translations.php');
421
422 die();
423 }
424
425 // called when a post (or page) is saved, published or updated
426 // the underscore in '_lang' hides the post meta in the Custom Fields metabox in the Edit Post screen
427 function save_post($post_id) {
428 if ($id = wp_is_post_revision($post_id))
429 $post_id = $id;
430
431 if (isset($_POST['post_lang_choice']))
432 wp_set_post_terms($post_id, $_POST['post_lang_choice'], 'language' );
433
434 $lang = $this->get_post_language($post_id);
435 $listlanguages = $this->get_languages_list();
436
437 foreach ($listlanguages as $language) {
438
439 if (isset($_POST[$language->slug]) && $_POST[$language->slug]) {
440 // FIXME I don't check that the translated post is in the right language !
441 update_post_meta($post_id, '_lang-'.$language->slug, $_POST[$language->slug]); // saves the links to translated posts
442 if ($lang)
443 update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug, $post_id); // tells the translated to link to this post
444 }
445 else {
446 // deletes the translation in case there was previously one
447 if ($lang)
448 delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // in the translated post
449
450 delete_post_meta($post_id, '_lang-'.$language->slug); // in this post
451 }
452 }
453
454 // propagates translations between them
455 foreach ($listlanguages as $language) {
456 foreach ($listlanguages as $lg) {
457 if ($lg != $lang && $lg != $language && $id = $this->get_translated_post($post_id, $lg))
458 update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lg->slug, $id);
459 }
460 }
461 }
462
463 // called when a post (or page) is deleted
464 function delete_post($post_id) {
465 if ($id = wp_is_post_revision($post_id))
466 $post_id = $id;
467
468 if($lang = $this->get_post_language($post_id)) {
469 foreach ($this->get_languages_list() as $language) {
470 delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // delete links to this post
471 // WP deletes post metas linked to this post so it is useless to do it before
472 }
473 }
474 }
475
476 // adds the language field in the 'Categories' and 'Post Tags' panels
477 function add_term_form() {
478 $taxonomy = $_GET['taxonomy'];
479 $lang = null;
480 if (isset($_GET['new_lang']))
481 $lang = $this->get_language($_GET['new_lang']);
482
483 $listlanguages = $this->get_languages_list();
484
485 // displays the language field
486 include(INC_DIR.'/add-term-form.php');
487 }
488
489 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
490 function edit_term_form($tag) {
491 $term_id = $tag->term_id;
492 $lang = $this->get_term_language($term_id);
493 $taxonomy = $tag->taxonomy;
494 $listlanguages = $this->get_languages_list();
495
496 include(INC_DIR.'/edit-term-form.php');
497 }
498
499 // adds the language column (before the posts column) in the 'Categories' or Post Tags table
500 function add_term_column($columns) {
501 if (array_key_exists('posts', $columns))
502 $end = array_pop($columns);
503
504 $columns['language'] = __('Language', 'polylang');
505
506 if (isset($end))
507 $columns['posts'] = $end;
508
509 return $columns;
510 }
511
512 // fills the language column in the 'Categories' or Post Tags table
513 function term_column($empty, $column, $term_id) {
514 if ($column == 'language' && $lang = $this->get_term_language($term_id))
515 echo esc_attr($lang->name);
516 }
517
518
519 // called when a category or post tag is created or edited
520 function save_term($term_id) {
521
522 if (isset($_POST['term_lang_choice']) && $_POST['term_lang_choice'])
523 update_metadata('term', $term_id, '_language', $_POST['term_lang_choice'] );
524 else
525 delete_metadata('term', $term_id, '_language');
526
527
528 $lang = $this->get_term_language($term_id);
529 $listlanguages = $this->get_languages_list();
530
531 foreach ($listlanguages as $language) {
532 $slug = '_lang-'.$language->slug;
533 if (isset($_POST[$slug]) && $_POST[$slug]) {
534 // FIXME I don't check that the translated term is in the right language !
535 update_metadata('term', $term_id, $slug, $_POST[$slug] );
536 if ($lang)
537 update_metadata('term', $_POST[$slug], '_lang-'.$lang->slug, $term_id );
538 }
539 else {
540 // deletes the translation in case there was previously one
541 if ($lang)
542 delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // in the translated term
543 delete_metadata('term', $term_id, '_lang-'.$language->slug); // in this term
544 }
545
546 }
547
548 // propagates translations between them
549 if (isset($lang)) {
550 foreach ($listlanguages as $language) {
551 foreach ($listlanguages as $lg) {
552 if ($lg != $lang && $lg != $language && $id = $this->get_translated_term($term_id, $lg))
553 update_metadata('term',$this->get_translated_term($term_id, $language), '_lang-'.$lg->slug, $id);
554 }
555 }
556 }
557
558 }
559
560 // called when a category or post tag is deleted
561 function delete_term($term_id) {
562 if($lang = $this->get_term_language($term_id)) {
563 delete_metadata('term', $term_id, '_language' );
564 $listlanguages = $this->get_languages_list();
565 foreach ($listlanguages as $language) {
566 delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // delete links to this term
567 }
568 foreach ($listlanguages as $language) {
569 delete_metadata('term', $term_id, '_lang-'.$language->slug);
570 }
571 }
572 }
573
574 // returns all terms in the $taxonomy in the $term_language which have no translation in the $translation_language
575 function get_terms_not_translated($taxonomy, $term_language, $translation_language) {
576 $new_terms = array();
577 foreach (get_terms($taxonomy, 'hide_empty=0') as $term) {
578 $lang = $this->get_term_language($term->term_id);
579 if ($lang && $lang->name == $term_language->name && !$this->get_translated_term($term->term_id, $translation_language))
580 $new_terms[] = $term;
581 }
582 return $new_terms;
583 }
584
585 // ajax response for edit term form
586 function term_lang_choice()
587 {
588 if ($_POST['lang']) {
589 $lang = $this->get_language($_POST['lang']);
590 $term_id = isset($_POST['term_id']) ? $_POST['term_id'] : null;
591 $taxonomy = $_POST['taxonomy'];
592
593 $listlanguages = $this->get_languages_list();
594
595 include(INC_DIR.'/term-translations.php');
596 }
597 die();
598 }
599
600 // modifies the theme location nav menu metabox
601 function nav_menu_theme_locations() {
602 // only if the theme supports nav menus and a nav menu exists
603 if ( ! current_theme_supports( 'menus' ) || !isset($_REQUEST['menu']) )
604 return;
605
606 // thanks to: http://wordpress.stackexchange.com/questions/2770/how-to-add-a-custom-metabox-to-the-menu-management-admin-screen
607 $metabox = &$GLOBALS['wp_meta_boxes']['nav-menus']['side']['default']['nav-menu-theme-locations'];
608 $metabox['callback'] = array(&$this,'nav_menu_language');
609 $metabox['title'] = __('Theme locations and languages', 'polylang');
610 }
611
612 // displays a message to redirect to the languages options page
613 function nav_menu_language() {
614 echo '<p class="howto">' . sprintf (__('Please go to the %slanguages page%s to set theme locations and languages', 'polylang'),
615 '<a href="' . admin_url('options-general.php?page=mlang#menus') . '">', '</a>') . '</p>';
616 }
617
618 // modifies the widgets forms to add our language dropdwown list
619 function in_widget_form($widget) {
620 $id = esc_attr($widget->id.'_lang_choice');
621 $widget_lang = get_option('polylang_widgets');
622
623 printf(
624 '<p><label for="%s">%s<select name="%s" id="%s" class="tags-input"><option value="0">%s</option>',
625 $id,
626 __('The widget is displayed for:', 'polylang'),
627 $id,
628 $id,
629 __('All languages', 'polylang')
630 );
631 foreach ($this->get_languages_list() as $language) {
632 printf(
633 "<option value='%s'%s>%s</option>\n",
634 esc_attr($language->slug),
635 isset($widget_lang[$widget->id]) && $language->slug == $widget_lang[$widget->id] ? ' selected="selected"' : '',
636 esc_attr($language->name)
637 );
638 }
639 echo '</select></label></p>';
640 }
641
642 // called when widget options are saved
643 function widget_update_callback($instance, $new_instance, $old_instance, $widget) {
644 $widget_lang = get_option('polylang_widgets');
645 $widget_lang[$widget->id] = $_POST[$widget->id.'_lang_choice'];
646 update_option('polylang_widgets', $widget_lang);
647 return $instance;
648 }
649
650 // returns the locale based on user preference
651 function get_locale($locale) {
652 $loc = get_user_meta(get_current_user_id(), 'user_lang', 'true');
653 return $loc ? $loc : $locale;
654 }
655
656 // updates language user preference
657 function personal_options_update($user_id) {
658 update_user_meta($user_id, 'user_lang', $_POST['user_lang']);
659 }
660
661 // form for language user preference
662 function personal_options($profileuser) {
663 include(INC_DIR.'/personal-options.php');
664 }
665
666 // refresh rewrite rules if the 'page_on_front' option is modified
667 function update_option($option) {
668 global $wp_rewrite;
669 if ($option == 'page_on_front')
670 $wp_rewrite->flush_rules();
671 }
672 } // class Polylang_Admin
673
674 ?>
675