PluginProbe
Polylang / 0.5
Polylang v0.5
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-filters.php

admin-filters.php in Polylang 0.5, at include/admin-filters.php

452 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // all modifications of the WordPress admin ui
4 class Polylang_Admin_Filters extends Polylang_Base {
5 function __construct() {
6 // additionnal filters and actions
7 add_action('admin_init', array(&$this, 'admin_init'));
8
9 // setup js scripts andd css styles
10 add_action('admin_print_scripts', array(&$this,'admin_js'));
11 add_action('admin_print_styles', array(&$this,'admin_css'));
12
13 // add the language column (as well as a filter by language) in 'All Posts' an 'All Pages' panels
14 add_filter('manage_posts_columns', array(&$this, 'add_post_column'), 10, 2);
15 add_filter('manage_pages_columns', array(&$this, 'add_post_column'));
16 add_action('manage_posts_custom_column', array(&$this, 'post_column'), 10, 2);
17 add_action('manage_pages_custom_column', array(&$this, 'post_column'), 10, 2);
18 add_filter('parse_query',array(&$this,'parse_query'));
19 add_action('restrict_manage_posts', array(&$this, 'restrict_manage_posts'));
20
21 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels
22 add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));
23
24 // ajax response for changing the language in the post metabox
25 add_action('wp_ajax_post_lang_choice', array(&$this,'post_lang_choice'));
26
27 // adds actions related to languages when saving or deleting posts and pages
28 add_action('save_post', array(&$this, 'save_post'));
29 add_action('before_delete_post', array(&$this, 'delete_post'));
30
31 // filters categories and post tags by language
32 add_filter('terms_clauses', array(&$this, 'terms_clauses'), 10, 3);
33
34 // ajax response for edit term form
35 add_action('wp_ajax_term_lang_choice', array(&$this,'term_lang_choice'));
36
37 // modifies the theme location nav menu metabox
38 add_filter('admin_head-nav-menus.php', array(&$this, 'nav_menu_theme_locations'));
39
40 // widgets languages filter
41 add_action('in_widget_form', array(&$this, 'in_widget_form'));
42 add_filter('widget_update_callback', array(&$this, 'widget_update_callback'), 10, 4);
43
44 // language management for users
45 add_filter('locale', array(&$this, 'get_locale'));
46 add_action('personal_options_update', array(&$this, 'personal_options_update'));
47 add_action('personal_options', array(&$this, 'personal_options'));
48
49 // refresh rewrite rules if the 'page_on_front' option is modified
50 add_action('update_option', array(&$this, 'update_option'));
51 }
52
53 // add these actions and filters here and not in the constructor to be sure that all taxonomies are registered
54 function admin_init() {
55 foreach (get_taxonomies(array('show_ui'=>true)) as $tax) {
56 // adds the language field in the 'Categories' and 'Post Tags' panels
57 add_action($tax.'_add_form_fields', array(&$this, 'add_term_form'));
58
59 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
60 add_action($tax.'_edit_form_fields', array(&$this, 'edit_term_form'));
61
62 // adds the language column in the 'Categories' and 'Post Tags' tables
63 add_filter('manage_edit-'.$tax.'_columns', array(&$this, 'add_term_column'));
64 add_action('manage_'.$tax.'_custom_column', array(&$this, 'term_column'), 10, 3);
65
66 // adds actions related to languages when saving or deleting categories and post tags
67 add_action('created_'.$tax, array(&$this, 'save_term'));
68 add_action('edited_'.$tax, array(&$this, 'save_term'));
69 add_action('delete_'.$tax, array(&$this, 'delete_term'));
70 }
71 }
72
73 // setup js scripts
74 function admin_js() {
75 wp_enqueue_script('polylang_admin', WP_PLUGIN_URL .'/polylang/js/admin.js');
76 }
77
78 // setup css styles
79 function admin_css() {
80 wp_enqueue_style('polylang_admin', WP_PLUGIN_URL .'/polylang/css/admin.css');
81 }
82
83 // adds the language column (before the date column) in the posts and pages list table
84 function add_post_column($columns, $post_type ='') {
85 if ($post_type == '' || get_post_type_object($post_type)->show_ui) {
86 foreach (array( 'date', 'comments' ) as $k) {
87 if (array_key_exists($k, $columns))
88 $end[$k] = array_pop($columns);
89 }
90
91 $columns['language'] = __('Language', 'polylang');
92
93 if (isset($end))
94 $columns = array_merge($columns, $end);
95 }
96 return $columns;
97 }
98
99 // fills the language column in the posts table
100 function post_column($column, $post_id) {
101 if ($column == 'language' && $lang = $this->get_post_language($post_id))
102 echo esc_html($lang->name);
103 }
104
105 // converts language term_id to slug in $query
106 // needed to filter the posts by language with wp_dropdown_categories in restrict_manage_posts
107 function parse_query($query) {
108 global $pagenow;
109 $qvars = &$query->query_vars;
110 if ($pagenow=='edit.php' && isset($qvars['lang']) && $qvars['lang'] && is_numeric($qvars['lang']) && $lang = $this->get_language($qvars['lang']))
111 $qvars['lang'] = $lang->slug;
112 }
113
114 // adds a filter for languages in the Posts and Pages panels
115 function restrict_manage_posts() {
116 global $wp_query;
117 $screen = get_current_screen(); // since WP 3.1
118 $languages = $this->get_languages_list();
119
120 if (!empty($languages) && $screen->base == 'edit') {
121 $qvars = $wp_query->query;
122 wp_dropdown_categories(array(
123 'show_option_all' => __('Show all languages', 'polylang'),
124 'name' => 'lang',
125 'selected' => isset($qvars['lang']) ? $qvars['lang'] : 0,
126 'taxonomy' => 'language',
127 'hide_empty' => 0
128 ));
129 }
130 }
131
132 // adds the Languages box in the 'Edit Post' and 'Edit Page' panels (as well as in custom post types panels
133 function add_meta_boxes() {
134 foreach(get_post_types( array( 'show_ui' => true ) ) as $ptype)
135 add_meta_box('ml_box', __('Languages','polylang'), array(&$this,'post_language'), $ptype, 'side','high');
136 }
137
138 // the Languages metabox in the 'Edit Post' and 'Edit Page' panels
139 function post_language() {
140 global $post_ID;
141 $post_type = get_post_type($post_ID);
142
143 $lang = $this->get_post_language($post_ID);
144 if (isset($_GET['new_lang']))
145 $lang = $this->get_language($_GET['new_lang']);
146
147 if (!isset($lang)) {
148 $options = get_option('polylang');
149 $lang = $this->get_language($options['default_lang']);
150 }
151
152 $listlanguages = $this->get_languages_list();
153
154 include(PLL_INC.'/post-metabox.php');
155 }
156
157 // ajax response for changing the language in the post metabox
158 function post_lang_choice() {
159 global $post_ID; // obliged to use the global variable for wp_popular_terms_checklist
160 $post_ID = $_POST['post_id'];
161 $post_type = get_post_type($post_ID);
162 $listlanguages = $this->get_languages_list();
163 $lang = $this->get_language($_POST['lang']);
164
165 ob_start();
166 if ($lang && !is_wp_error($lang))
167 include(PLL_INC.'/post-translations.php');
168 $x = new WP_Ajax_Response(array('what' => 'translations', 'data' => ob_get_contents()));
169 ob_end_clean();
170
171 if (isset($_POST['taxonomies'])) {
172 // not set for pages
173 foreach ($_POST['taxonomies'] as $taxname) {
174 $taxonomy = get_taxonomy($taxname);
175
176 ob_start();
177 $popular_ids = wp_popular_terms_checklist($taxonomy->name);
178 $supplemental['populars'] = ob_get_contents();
179 ob_end_clean();
180
181 ob_start();
182 // use $post_ID to remember ckecked terms in case we come back to the original language
183 wp_terms_checklist( $post_ID, array( 'taxonomy' => $taxonomy->name, 'popular_cats' => $popular_ids ));
184 $supplemental['all'] = ob_get_contents();
185 ob_end_clean();
186
187 ob_start();
188 wp_dropdown_categories( array(
189 'taxonomy' => $taxonomy->name, 'hide_empty' => 0, 'name' => 'new'.$taxonomy->name.'_parent', 'orderby' => 'name',
190 'hierarchical' => 1, 'show_option_none' => '&mdash; '.$taxonomy->labels->parent_item.' &mdash;'
191 ) );
192 $supplemental['dropdown'] = ob_get_contents();
193 ob_end_clean();
194
195 $x->Add(array('what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental));
196 }
197 }
198
199 $x->send();
200 }
201
202 // saves translations for posts or terms
203 // the underscore in '_lang' hides the post meta in the Custom Fields metabox in the Edit Post screen
204 function save_translations($type, $id) {
205 $lang = call_user_func(array(&$this, 'get_'.$type.'_language'), $id);
206 if (!$lang)
207 return;
208
209 if (isset($_POST['tr_lang']) && is_array($_POST['tr_lang'])) {
210 $tr = serialize(array_merge(array($lang->slug => $id), $_POST['tr_lang']));
211 update_metadata($type, $id, '_translations', $tr);
212
213 foreach($_POST['tr_lang'] as $key=>$p)
214 update_metadata($type, $p, '_translations', $tr);
215 }
216 }
217
218 // called when a post (or page) is saved, published or updated
219 function save_post($post_id) {
220 // avoids breaking translations when using inline or bulk edit
221 if(isset($_POST['_inline_edit']) || isset($_GET['bulk_edit']))
222 return;
223
224 if ($id = wp_is_post_revision($post_id))
225 $post_id = $id;
226
227 if (isset($_POST['post_lang_choice']))
228 wp_set_post_terms($post_id, $_POST['post_lang_choice'], 'language' );
229
230 $this->save_translations('post', $post_id);
231 }
232
233 // called when a post (or page) is deleted
234 function delete_post($post_id) {
235 if ($id = wp_is_post_revision($post_id))
236 $post_id = $id;
237
238 $this->delete_translation('post', $post_id);
239 }
240
241 // filters categories and post tags by language when needed
242 function terms_clauses($clauses, $taxonomies, $args) {
243 // does nothing except on taxonomies which have show_ui set to 1 (includes category and post_tags)
244 foreach ($taxonomies as $tax) {
245 if(!get_taxonomy($tax)->show_ui)
246 return $clauses;
247 }
248
249 if (function_exists('get_current_screen'))
250 $screen = get_current_screen(); // since WP 3.1, may not be available the first time(s) get_terms is called
251
252 // does nothing in the Categories, Post tags, Languages and Posts* admin panels
253 // I test $_POST['action'] for ajax since $screen not defined in this case
254 // FIXME Can I improve the way I do that ?
255 if (!isset($_POST['action']) && (!isset($screen) || $screen->base == 'edit-tags' || $screen->base == 'toplevel_page_mlang' || $screen->base == 'edit'))
256 return $clauses;
257
258 // *FIXME I want all categories in the dropdown list and only the ones in the right language in the inline edit
259 // It seems that I need javascript to get the post_id as inline edit data are manipulated in inline-edit-post.js
260
261 global $post_ID;
262 $options = get_option('polylang');
263
264 // ajax response for changing the language in the post metabox
265 if (isset($_POST['lang']))
266 $lang = $this->get_language($_POST['lang']);
267
268 // the post is created with the 'add new' (translation) link
269 elseif (isset($_GET['new_lang']))
270 $lang = $this->get_language($_GET['new_lang']);
271
272 elseif (isset($_GET['post']))
273 $lang = $this->get_post_language($_GET['post']);
274
275 // when a new category is created in the edit post panel
276 elseif (isset($_POST['term_lang_choice']))
277 $lang = $this->get_language($_POST['term_lang_choice']);
278
279 // for a new post
280 elseif ($screen->base == 'post' && !PLL_DISPLAY_ALL)
281 $lang = $this->get_language($options['default_lang']);
282
283 // adds our clauses to filter by current language
284 return isset($lang) ? $this->_terms_clauses($clauses, $lang) : $clauses;
285 }
286
287 // adds the language field in the 'Categories' and 'Post Tags' panels
288 function add_term_form() {
289 $taxonomy = $_GET['taxonomy'];
290
291 if (isset($_GET['new_lang']))
292 $lang = $this->get_language($_GET['new_lang']);
293 else {
294 $options = get_option('polylang');
295 $lang = $this->get_language($options['default_lang']);
296 }
297
298 $listlanguages = $this->get_languages_list();
299
300 // displays the language field
301 include(PLL_INC.'/add-term-form.php');
302 }
303
304 // adds the language field and translations tables in the 'Edit Category' and 'Edit Tag' panels
305 function edit_term_form($tag) {
306 $term_id = $tag->term_id;
307 $lang = $this->get_term_language($term_id);
308 $taxonomy = $tag->taxonomy;
309 $listlanguages = $this->get_languages_list();
310
311 include(PLL_INC.'/edit-term-form.php');
312 }
313
314 // adds the language column (before the posts column) in the 'Categories' or Post Tags table
315 function add_term_column($columns) {
316 if (array_key_exists('posts', $columns))
317 $end = array_pop($columns);
318
319 $columns['language'] = __('Language', 'polylang');
320
321 if (isset($end))
322 $columns['posts'] = $end;
323
324 return $columns;
325 }
326
327 // fills the language column in the 'Categories' or Post Tags table
328 function term_column($empty, $column, $term_id) {
329 if ($column == 'language' && $lang = $this->get_term_language($term_id))
330 echo esc_html($lang->name);
331 }
332
333 // called when a category or post tag is created or edited
334 function save_term($term_id) {
335 // avoids breaking translations when using inline edit
336 if(isset($_POST['_inline_edit']))
337 return;
338
339 if (isset($_POST['term_lang_choice']) && $_POST['term_lang_choice'])
340 $this->update_term_language($term_id, $_POST['term_lang_choice']);
341 else
342 $this->delete_term_language($term_id);
343
344 $this->save_translations('term', $term_id);
345 }
346
347 // called when a category or post tag is deleted
348 function delete_term($term_id) {
349 $this->delete_term_language($term_id);
350 $this->delete_translation('term', $term_id);
351 }
352
353 // returns all terms in the $taxonomy in the $term_language which have no translation in the $translation_language
354 function get_terms_not_translated($taxonomy, $term_language, $translation_language) {
355 $new_terms = array();
356 foreach (get_terms($taxonomy, 'hide_empty=0') as $term) {
357 $lang = $this->get_term_language($term->term_id);
358 if ($lang && $lang->name == $term_language->name && !$this->get_translation('term', $term->term_id, $translation_language))
359 $new_terms[] = $term;
360 }
361 return $new_terms;
362 }
363
364 // ajax response for edit term form
365 function term_lang_choice()
366 {
367 if ($_POST['lang']) {
368 $lang = $this->get_language($_POST['lang']);
369 $term_id = isset($_POST['term_id']) ? $_POST['term_id'] : null;
370 $taxonomy = $_POST['taxonomy'];
371
372 $listlanguages = $this->get_languages_list();
373
374 include(PLL_INC.'/term-translations.php');
375 }
376 die();
377 }
378
379 // modifies the theme location nav menu metabox
380 // thanks to: http://wordpress.stackexchange.com/questions/2770/how-to-add-a-custom-metabox-to-the-menu-management-admin-screen
381 function nav_menu_theme_locations() {
382 // only if the theme supports nav menus and a nav menu exists
383 if ( ! current_theme_supports( 'menus' ) || ! $metabox = &$GLOBALS['wp_meta_boxes']['nav-menus']['side']['default']['nav-menu-theme-locations'] )
384 return;
385
386 $metabox['callback'] = array(&$this,'nav_menu_language');
387 $metabox['title'] = __('Theme locations and languages', 'polylang');
388 }
389
390 // displays a message to redirect to the languages options page
391 function nav_menu_language() {
392 echo '<p class="howto">' . sprintf (__('Please go to the %slanguages page%s to set theme locations and languages', 'polylang'),
393 '<a href="' . esc_url(admin_url('options-general.php?page=mlang&tab=menus')) . '">', '</a>') . '</p>';
394 }
395
396 // modifies the widgets forms to add our language dropdwown list
397 function in_widget_form($widget) {
398 $widget_lang = get_option('polylang_widgets');
399
400 printf(
401 '<p><label for="%1$s">%2$s<select name="%1$s" id="%1$s" class="tags-input"><option value="0">%3$s</option>',
402 esc_attr($widget->id.'_lang_choice'),
403 __('The widget is displayed for:', 'polylang'),
404 __('All languages', 'polylang')
405 );
406 foreach ($this->get_languages_list() as $language) {
407 printf(
408 "<option value='%s'%s>%s</option>\n",
409 esc_attr($language->slug),
410 isset($widget_lang[$widget->id]) && $language->slug == $widget_lang[$widget->id] ? ' selected="selected"' : '',
411 esc_html($language->name)
412 );
413 }
414 echo '</select></label></p>';
415 }
416
417 // called when widget options are saved
418 function widget_update_callback($instance, $new_instance, $old_instance, $widget) {
419 $widget_lang = get_option('polylang_widgets');
420 $widget_lang[$widget->id] = $_POST[$widget->id.'_lang_choice'];
421 update_option('polylang_widgets', $widget_lang);
422 return $instance;
423 }
424
425 // returns the locale based on user preference
426 function get_locale($locale) {
427 // get_current_user_id uses wp_get_current_user which may not be available the first time(s) get_locale is called
428 if (function_exists('wp_get_current_user'))
429 $loc = get_user_meta(get_current_user_id(), 'user_lang', 'true');
430 return isset($loc) && $loc ? $loc : $locale;
431 }
432
433 // updates language user preference
434 function personal_options_update($user_id) {
435 update_user_meta($user_id, 'user_lang', $_POST['user_lang']);
436 }
437
438 // form for language user preference
439 function personal_options($profileuser) {
440 include(PLL_INC.'/personal-options.php');
441 }
442
443 // refresh rewrite rules if the 'page_on_front' option is modified
444 function update_option($option) {
445 global $wp_rewrite;
446 if ($option == 'page_on_front')
447 $wp_rewrite->flush_rules();
448 }
449 }
450
451 ?>
452