PluginProbe
Polylang / 0.2
Polylang v0.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
polylang / admin.php

admin.php in Polylang 0.2, at admin.php

543 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once(POLYLANG_DIR.'/list-table.php');
4 define('POLYLANG_URL', WP_PLUGIN_URL .'/polylang');
5
6 class Polylang_Admin extends Polylang_Base {
7 function __construct() {
8 // adds a 'settings' link in the plugins table
9 $plugin_file = basename( dirname( __FILE__ ) ).'/polylang.php';
10 add_filter('plugin_action_links_'.$plugin_file, array(&$this, 'plugin_action_links'));
11
12 // adds the link to the languages panel in the wordpress admin menu
13 add_action('admin_menu', array(&$this, 'add_menus'));
14
15 // setup js scripts
16 add_action('admin_print_scripts', array($this,'admin_js'));
17
18 // add the language column (as well as a filter by language) in 'All Posts' an 'All Pages' panels
19 add_filter('manage_posts_columns', array(&$this, 'add_post_column'), 10, 2);
20 add_filter('manage_pages_columns', array(&$this, 'add_post_column'));
21 add_action('manage_posts_custom_column', array(&$this, 'post_column'), 10, 2);
22 add_action('manage_pages_custom_column', array(&$this, 'post_column'), 10, 2);
23 add_filter('parse_query',array(&$this,'parse_query'));
24 add_action('restrict_manage_posts', array(&$this, 'restrict_manage_posts'));
25
26 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels
27 add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));
28
29 // ajax response for post metabox
30 add_action('wp_ajax_post_lang_choice', array($this,'post_lang_choice'));
31
32 // adds actions related to languages when saving aor deleting posts and pages
33 add_action('save_post', array(&$this, 'save_post'));
34 add_action('before_delete_post', array(&$this, 'delete_post'));
35
36 // adds the language field in the 'Categories' and 'Post Tags' panels
37 add_action('category_add_form_fields', array(&$this, 'add_term_form'));
38 add_action('post_tag_add_form_fields', array(&$this, 'add_term_form'));
39
40 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
41 add_action('category_edit_form_fields', array(&$this, 'edit_term_form'));
42 add_action('post_tag_edit_form_fields', array(&$this, 'edit_term_form'));
43
44 // ajax response for edit term form
45 add_action('wp_ajax_term_lang_choice', array($this,'term_lang_choice'));
46
47 // adds the language column in the 'Categories' and 'Post Tags' tables
48 add_filter('manage_edit-category_columns', array(&$this, 'add_term_column'));
49 add_filter('manage_edit-post_tag_columns', array(&$this, 'add_term_column'));
50 add_action('manage_category_custom_column', array(&$this, 'term_column'), 10, 3);
51 add_action('manage_post_tag_custom_column', array(&$this, 'term_column'), 10, 3);
52
53 // adds actions related to languages when saving or deleting categories and post tags
54 add_action('created_category', array(&$this, 'save_term'));
55 add_action('created_post_tag', array(&$this, 'save_term'));
56 add_action('edited_category', array(&$this, 'save_term'));
57 add_action('edited_post_tag', array(&$this, 'save_term'));
58 add_action('delete_category', array(&$this, 'delete_term'));
59 add_action('delete_post_tag', array(&$this, 'delete_term'));
60
61 // modifies the theme location nav menu metabox
62 add_filter('admin_head-nav-menus.php', array(&$this, 'nav_menu_theme_locations'));
63
64 // refresh rewrite rules if the 'page_on_front' option is modified
65 add_action('update_option', array(&$this, 'update_option'));
66 }
67
68 // adds a 'settings' link in the plugins table
69 function plugin_action_links($links) {
70 $settings_link = '<a href="admin.php?page=mlang">' . __('Settings') . '</a>';
71 array_unshift( $links, $settings_link );
72 return $links;
73 }
74
75 // adds the link to the languages panel in the wordpress admin menu
76 function add_menus() {
77 add_submenu_page('options-general.php', __('Languages','polylang'), __('Languages','polylang'), 'manage_options', 'mlang', array($this, 'languages_page'));
78 }
79
80 // setup js scripts
81 function admin_js() {
82 wp_enqueue_script('polylang_admin', POLYLANG_URL.'/admin.js');
83 }
84
85 // the languages panel
86 function languages_page() {
87 global $wp_rewrite;
88 $options = get_option('polylang');
89
90 $listlanguages = $this->get_languages_list();
91 $list_table = new Polylang_List_Table();
92
93 // for nav menus form
94 $locations = get_registered_nav_menus();
95 $menus = wp_get_nav_menus();
96 $menu_lang = get_option('polylang_nav_menus');
97
98 $action = '';
99 if (isset($_REQUEST['action']))
100 $action = $_REQUEST['action'];
101
102 switch ($action) {
103 case 'add':
104 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
105
106 if (isset($_POST['name']) && isset($_POST['description'])) {
107 wp_insert_term($_POST['name'],'language',array('slug'=>$_POST['slug'], 'description'=>$_POST['description']));
108 $wp_rewrite->flush_rules(); // refresh rewrite rules
109 }
110 if (!isset($options['default_lang'])) { // if this is the first language created, set it as default language
111 $options['default_lang'] = $_POST['slug'];
112 update_option('polylang', $options);
113 }
114 wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true)
115 exit;
116 break;
117
118 case 'delete':
119 check_admin_referer( 'delete-lang');
120
121 if (isset($_GET['lang'])) {
122 $lang = $this->get_language($_GET['lang']);
123 $lang_slug = $lang->slug;
124
125 // delete all translations in posts in this language
126 $args = array('numberposts'=>-1, 'taxonomy' => 'language', 'term' => $lang_slug, 'post_type'=>'any', 'post_status'=>'any');
127 $posts = get_posts($args);
128 foreach ($posts as $post) {
129 foreach ($languages as $language) {
130 delete_post_meta($post->ID, '_lang-'.$language->slug);
131 }
132 }
133
134 // delete references to this language in all posts
135 $args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$lang_slug, 'post_type'=>'any', 'post_status'=>'any');
136 $posts = get_posts($args);
137 foreach ($posts as $post) {
138 delete_post_meta($post->ID, '_lang-'.$lang_slug);
139 }
140
141 // delete references to this language in categories & post tags
142 $terms = get_terms(array('category', 'post_tag'), 'get=all');
143 foreach ($terms as $term) {
144 if ($this->get_term_language($term->term_id) == $lang) {
145 foreach ($languages as $language) {
146 delete_metadata('term', $term->term_id, '_lang-'.$language->slug); // deletes translations of this term
147 }
148 delete_metadata('term', $term->term_id, '_language', $_GET['lang']); // delete language of this term
149 }
150 delete_metadata('term', $term->term_id, '_lang-'.$lang_slug); // deletes references to this term in translated term
151 }
152
153 // delete the language itself
154 wp_delete_term($_GET['lang'], 'language');
155 $wp_rewrite->flush_rules(); // refresh rewrite rules
156
157 // oops ! we deleted the default language...
158 if ($options['default_lang'] == $lang_slug) {
159 if (!empty($languages))
160 $options['default_lang'] = $languages[0]->slug; // arbitrary choice...
161 else
162 unset($options['default_lang']);
163 update_option('polylang', $options);
164 }
165 }
166 wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true)
167 exit;
168 break;
169
170 case 'edit':
171 if (isset($_GET['lang']))
172 $edit_lang = $this->get_language($_GET['lang']);
173 break;
174
175 case 'update':
176 check_admin_referer( 'add-lang', '_wpnonce_add-lang' );
177
178 if (isset($_POST['lang'])) {
179 // Update links to this language in posts and terms in case the slug has been modified
180 $lang = $this->get_language($_POST['lang']);
181 $old_slug = $lang->slug;
182
183 if ($old_slug != $_POST['slug']) {
184 // update the language slug in posts meta
185 $args = array('numberposts'=>-1, 'meta_key'=>'_lang-'.$old_slug, 'post_type'=>'any', 'post_status'=>'any');
186 $posts = get_posts($args);
187 foreach ($posts as $post) {
188 $post_id = get_post_meta($post->ID, '_lang-'.$old_slug, true);
189 delete_post_meta($post->ID, '_lang-'.$old_slug);
190 update_post_meta($post->ID, '_lang-'.$_POST['slug'], $post_id);
191 }
192
193 // update the language slug in categories & post tags meta
194 $terms = get_terms(array('category', 'post_tag'), 'get=all');
195 foreach ($terms as $term) {
196 $term_id = get_metadata('term', $term->term_id, '_lang-'.$old_slug, true);
197 if ($term_id) {
198 delete_metadata('term', $term->term_id, '_lang-'.$old_slug);
199 update_metadata('term', $term->term_id, '_lang-'.$_POST['slug'], $term_id);
200 }
201 }
202 }
203
204 // and finally update the language itself
205 wp_update_term($_POST['lang'], 'language', array('name'=>$_POST['name'], 'slug'=>$_POST['slug'], 'description'=>$_POST['description']));
206 $wp_rewrite->flush_rules(); // refresh rewrite rules
207 }
208 wp_redirect('admin.php?page=mlang'); // to refresh the page (possible thanks to the $_GET['noheader']=true)
209 exit;
210 break;
211
212 case 'nav-menus':
213 check_admin_referer( 'nav-menus-lang', '_wpnonce_nav-menus-lang' );
214 $menu_lang = $_POST['menu-lang'];
215 update_option('polylang_nav_menus', $menu_lang);
216 break;
217
218 case 'options':
219 check_admin_referer( 'options-lang', '_wpnonce_options-lang' );
220
221 $options['rewrite'] = $_POST['rewrite'];
222 isset($_POST['browser']) ? $options['browser'] = 1 : $options['browser'] = 0;
223 $options['default_lang'] = $_POST['default_lang'];
224 update_option('polylang', $options);
225
226 // refresh refresh permalink structure and rewrite rules in case rewrite option has been modified
227 $wp_rewrite->extra_permastructs['language'][0] = $options['rewrite'] ? '%language%' : '/language/%language%';
228 $wp_rewrite->flush_rules();
229 break;
230
231 default:
232 }
233
234 // prepare the list table of languages
235 $data = array();
236 foreach ($listlanguages as $lang)
237 $data[] = (array) $lang;
238
239 $list_table->prepare_items($data);
240
241 // displays the page
242 include(POLYLANG_DIR.'/languages-form.php');
243 }
244
245 // adds the language column (before the date column) in the posts and pages list table
246 function add_post_column($columns, $post_type ='') {
247 if (in_array($post_type, array('', 'post', 'page'))) {
248 $keys = array( 'date', 'comments' );
249 foreach ($keys as $k) {
250 if (array_key_exists($k, $columns))
251 $end[$k] = array_pop($columns);
252 }
253
254 $columns['language'] = __('Language', 'polylang');
255
256 if (isset($end))
257 $columns = array_merge($columns, $end);
258 }
259 return $columns;
260 }
261
262 // fills the language column in the posts table
263 function post_column($column, $post_id) {
264 $lang = $this->get_post_language($post_id);
265 if ($lang && $column == 'language')
266 echo $lang->name;
267 }
268
269 // converts language term_id to slug in $query
270 // needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts
271 function parse_query($query) {
272 global $pagenow;
273 $qvars = &$query->query_vars;
274 if ($pagenow=='edit.php' && isset($qvars['lang']) && is_numeric($qvars['lang'])) {
275 $lang = $this->get_language($qvars['lang']);
276 if ($lang)
277 $qvars['lang'] = $lang->slug;
278 }
279 }
280
281 // adds a filter for languages in the Posts and Pages panels
282 function restrict_manage_posts() {
283 global $wp_query;
284 $screen = get_current_screen();
285 $languages = $this->get_languages_list();
286
287 if (!empty($languages) && $screen->base == 'edit') {
288 $qvars = $wp_query->query;
289 wp_dropdown_categories(array(
290 'show_option_all' => __('Show all languages', 'polylang'),
291 'name' => 'lang',
292 'selected' => isset($qvars['lang']) ? $qvars['lang'] : 0,
293 'taxonomy' => 'language'));
294 }
295 }
296
297 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels
298 function add_meta_boxes() {
299 add_meta_box('ml_box', __('Languages','polylang'), array(&$this,'post_language'), 'post', 'side','high');
300 add_meta_box('ml_box', __('Languages','polylang'), array(&$this,'post_language'), 'page', 'side','high');
301 }
302
303 // the Languages metabox in the 'Edit Post' and 'Edit Page' panels
304 function post_language() {
305 global $post_ID;
306 $post_type = get_post_type($post_ID);
307
308 $lang = $this->get_post_language($post_ID);
309 if (isset($_GET['new_lang']))
310 $lang = $this->get_language($_GET['new_lang']);
311
312 $listlanguages = $this->get_languages_list();
313
314 include(POLYLANG_DIR.'/post-metabox.php');
315 }
316
317 // ajax response for post metabox
318 function post_lang_choice() {
319 $listlanguages = $this->get_languages_list();
320
321 $lang = $this->get_language($_POST['lang']);
322 $post_ID = $_POST['post_id'];
323
324 if ($lang)
325 include(POLYLANG_DIR.'/post-translations.php');
326
327 die();
328 }
329
330 // called when a post (or page) is saved, published or updated
331 // the underscore in '_lang' hides the post meta in the Custom Fields metabox in the Edit Post screen
332 function save_post($post_id) {
333
334 $id = wp_is_post_revision($post_id);
335 if ($id)
336 $post_id = $id;
337
338 if (isset($_POST['post_lang_choice']))
339 wp_set_post_terms($post_id, $_POST['post_lang_choice'], 'language' );
340
341 $lang = $this->get_post_language($post_id);
342 $listlanguages = $this->get_languages_list();
343
344 foreach ($listlanguages as $language) {
345
346 if (isset($_POST[$language->slug]) && $_POST[$language->slug]) {
347 // FIXME I don't check that the translated post is in the right language !
348 update_post_meta($post_id, '_lang-'.$language->slug, $_POST[$language->slug]); // saves the links to translated posts
349 if ($lang)
350 update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug, $post_id); // tells the translated to link to this post
351 }
352 else {
353 // deletes the translation in case there was previously one
354 if ($lang)
355 delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // in the translated post
356 delete_post_meta($post_id, '_lang-'.$language->slug); // in this post
357 }
358 }
359
360 // propagates translations between them
361 foreach ($listlanguages as $language) {
362 foreach ($listlanguages as $lg) {
363 $id = $this->get_translated_post($post_id, $lg);
364 if ($lg != $lang && $lg != $language && $id)
365 update_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lg->slug, $id);
366 }
367 }
368 }
369
370 // called when a post (or page) is deleted
371 function delete_post($post_id) {
372 $id = wp_is_post_revision($post_id);
373 if ($id)
374 $post_id = $id;
375
376 $lang = $this->get_post_language($post_id);
377 if($lang) {
378 $listlanguages = $this->get_languages_list();
379 foreach ($listlanguages as $language) {
380 delete_post_meta($this->get_translated_post($post_id, $language), '_lang-'.$lang->slug); // delete links to this post
381 // WP deletes post metas linked to this post so it is useless to do it before
382 }
383 }
384 }
385
386 // adds the language field in the 'Categories' and 'Post Tags' panels
387 function add_term_form() {
388 $taxonomy = $_GET['taxonomy'];
389 $lang = null;
390 if (isset($_GET['new_lang']))
391 $lang = $this->get_language($_GET['new_lang']);
392
393 $listlanguages = $this->get_languages_list();
394
395 // displays the language field
396 include(POLYLANG_DIR.'/add-term-form.php');
397 }
398
399 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
400 function edit_term_form($tag) {
401 $term_id = $tag->term_id;
402 $lang = $this->get_term_language($term_id);
403 $taxonomy = $tag->taxonomy;
404 $listlanguages = $this->get_languages_list();
405
406 include(POLYLANG_DIR.'/edit-term-form.php');
407 }
408
409 // ajax response for edit term form
410 function term_lang_choice()
411 {
412 if ($_POST['lang']) {
413 $lang = $this->get_language($_POST['lang']);
414 $term_id = isset($_POST['term_id']) ? $_POST['term_id'] : null;
415 $taxonomy = $_POST['taxonomy'];
416
417 $listlanguages = $this->get_languages_list();
418
419 include(POLYLANG_DIR.'/term-translations.php');
420 }
421 die();
422 }
423
424 // adds the language column (before the posts column) in the 'Categories' or Post Tags table
425 function add_term_column($columns) {
426 if (array_key_exists('posts', $columns))
427 $end = array_pop($columns);
428
429 $columns['language'] = __('Language', 'polylang');
430
431 if (isset($end))
432 $columns['posts'] = $end;
433
434 return $columns;
435 }
436
437 // fills the language column in the 'Categories' or Post Tags table
438 function term_column($empty, $column, $term_id) {
439 $lang = $this->get_term_language($term_id);
440 if ($lang && $column == 'language')
441 echo $lang->name;
442 }
443
444 // called when a category or post tag is created or edited
445 function save_term($term_id) {
446
447 if (isset($_POST['term_lang_choice']) && $_POST['term_lang_choice'])
448 update_metadata('term', $term_id, '_language', $_POST['term_lang_choice'] );
449 else
450 delete_metadata('term', $term_id, '_language');
451
452
453 $lang = $this->get_term_language($term_id);
454 $listlanguages = $this->get_languages_list();
455
456 foreach ($listlanguages as $language) {
457 $slug = '_lang-'.$language->slug;
458 if (isset($_POST[$slug]) && $_POST[$slug]) {
459 // FIXME I don't check that the translated term is in the right language !
460 update_metadata('term', $term_id, $slug, $_POST[$slug] );
461 if ($lang)
462 update_metadata('term', $_POST[$slug], '_lang-'.$lang->slug, $term_id );
463 }
464 else {
465 // deletes the translation in case there was previously one
466 if ($lang)
467 delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // in the translated term
468 delete_metadata('term', $term_id, '_lang-'.$language->slug); // in this term
469 }
470
471 }
472
473 // propagates translations between them
474 if (isset($lang)) {
475 foreach ($listlanguages as $language) {
476 foreach ($listlanguages as $lg) {
477 $id = $this->get_translated_term($term_id, $lg);
478 if ($lg != $lang && $lg != $language && $id)
479 update_metadata('term',$this->get_translated_term($term_id, $language), '_lang-'.$lg->slug, $id);
480 }
481 }
482 }
483
484 }
485
486 // called when a category or post tag is deleted
487 function delete_term($term_id) {
488 $lang = $this->get_term_language($term_id);
489 if($lang) {
490 delete_metadata('term', $term_id, '_language' );
491 $listlanguages = $this->get_languages_list();
492 foreach ($listlanguages as $language) {
493 delete_metadata('term', $this->get_translated_term($term_id, $language), '_lang-'.$lang->slug); // delete links to this term
494 }
495 foreach ($listlanguages as $language) {
496 delete_metadata('term', $term_id, '_lang-'.$language->slug);
497 }
498 }
499 }
500
501 // returns all terms in the $taxonomy in the $term_language which have no translation in the $translation_language
502 function get_terms_not_translated($taxonomy, $term_language, $translation_language) {
503 $new_terms = array();
504 $terms = get_terms($taxonomy, 'hide_empty=0');
505 foreach ($terms as $term) {
506 $lang = $this->get_term_language($term->term_id);
507 if ($lang && $lang->name == $term_language->name) {
508 $translated_term = $this->get_translated_term($term->term_id, $translation_language);
509 if (!$translated_term)
510 $new_terms[] = $term;
511 }
512 }
513 return $new_terms;
514 }
515
516 // modifies the theme location nav menu metabox
517 function nav_menu_theme_locations() {
518 // only if the theme supports nav menus
519 if ( ! current_theme_supports( 'menus' ) )
520 return;
521
522 // thanks to : http://wordpress.stackexchange.com/questions/2770/how-to-add-a-custom-metabox-to-the-menu-management-admin-screen
523 $metabox = &$GLOBALS['wp_meta_boxes']['nav-menus']['side']['default']['nav-menu-theme-locations'];
524 $metabox['callback'] = array(&$this,'nav_menu_language');
525 $metabox['title'] = __('Theme locations and languages', 'polylang');
526 }
527
528 // displays a message to redirect to the languages options page
529 function nav_menu_language() {
530 echo '<p class="howto">' . sprintf (__('Please go to the %slanguages page%s to set theme locations and languages', 'polylang'),
531 '<a href="' . admin_url('options-general.php?page=mlang#menus') . '">', '</a>') . '</p>';
532 }
533
534 // refresh rewrite rules if the 'page_on_front' option is modified
535 function update_option($option) {
536 global $wp_rewrite;
537 if ($option == 'page_on_front')
538 $wp_rewrite->flush_rules();
539 }
540 } // class Polylang_Admin
541
542 ?>
543