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
polylang / admin / admin-filters-columns.php

admin-filters-columns.php in Polylang 1.5.2, at admin/admin-filters-columns.php

225 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * adds the language column in posts and terms list tables
5 * manages quick edit and bulk edit as well
6 *
7 * @since 1.2
8 */
9 class PLL_Admin_Filters_Columns {
10 public $links, $model, $curlang;
11
12 /*
13 * constructor: setups filters and actions
14 *
15 * @since 1.2
16 *
17 * @param object $polylang
18 */
19 public function __construct(&$polylang) {
20 $this->links = &$polylang->links;
21 $this->model = &$polylang->model;
22 $this->curlang = &$polylang->curlang;
23
24 // add the language and translations columns in 'All Posts', 'All Pages' and 'Media library' panels
25 foreach ($this->model->get_translated_post_types() as $type) {
26 // use the latest filter late as some plugins purely overwrite what's done by others :(
27 // specific case for media
28 add_filter('manage_'. ($type == 'attachment' ? 'upload' : 'edit-'. $type) .'_columns', array(&$this, 'add_post_column'), 100);
29 add_action('manage_'. ($type == 'attachment' ? 'media' : $type .'_posts') .'_custom_column', array(&$this, 'post_column'), 10, 2);
30 }
31
32 // quick edit and bulk edit
33 add_filter('quick_edit_custom_box', array(&$this, 'quick_edit_custom_box'), 10, 2);
34 add_filter('bulk_edit_custom_box', array(&$this, 'quick_edit_custom_box'), 10, 2);
35
36 // adds the language column in the 'Categories' and 'Post Tags' tables
37 foreach ($this->model->get_translated_taxonomies() as $tax) {
38 add_filter('manage_edit-'.$tax.'_columns', array(&$this, 'add_term_column'));
39 add_action('manage_'.$tax.'_custom_column', array(&$this, 'term_column'), 10, 3);
40 }
41
42 }
43
44 /*
45 * adds languages and translations columns in posts, pages, media, categories and tags tables
46 *
47 * @since 0.8.2
48 *
49 * @param array $columns list of table columns
50 * @param string $before the column before which we want to add our languages
51 * @return array modified list of columns
52 */
53 protected function add_column($columns, $before) {
54 if ($n = array_search($before, array_keys($columns))) {
55 $end = array_slice($columns, $n);
56 $columns = array_slice($columns, 0, $n);
57 }
58
59 foreach ($this->model->get_languages_list() as $language) {
60 // don't add the column for the filtered language
61 if (empty($this->curlang) || $language->slug != $this->curlang->slug)
62 $columns['language_'.$language->slug] = $language->flag ? $language->flag : esc_html($language->slug);
63 }
64
65 return isset($end) ? array_merge($columns, $end) : $columns;
66 }
67
68 /*
69 * returns the first language column in the posts, pages and media library tables
70 *
71 * @since 0.9
72 *
73 * @return string first language column name
74 */
75 protected function get_first_language_column() {
76 foreach ($this->model->get_languages_list() as $language) {
77 if (empty($this->curlang) || $language->slug != $this->curlang->slug)
78 $columns[] = 'language_'.$language->slug;
79 }
80
81 return empty($columns) ? '' : reset($columns);
82 }
83
84 /*
85 * adds the language and translations columns (before the comments column) in the posts, pages and media library tables
86 *
87 * @since 0.1
88 *
89 * @param array $columns list of posts table columns
90 * @return array modified list of columns
91 */
92 function add_post_column($columns) {
93 return $this->add_column($columns, 'comments');
94 }
95
96 /*
97 * fills the language and translations columns in the posts, pages and media library tables
98 * take care that when doing ajax inline edit, the post may not be updated in database yet
99 * FIXME if inline edit breaks translations, the rows corresponding to these translations are not updated
100 *
101 * @since 0.1
102 *
103 * @param string $column column name
104 * @param int $post_id
105 */
106 public function post_column($column, $post_id) {
107 $inline = defined('DOING_AJAX') && $_REQUEST['action'] == 'inline-save';
108 $lang = $inline ? $this->model->get_language($_POST['inline_lang_choice']) : $this->model->get_post_language($post_id);
109
110 if (false === strpos($column, 'language_') || !$lang)
111 return;
112
113 $language = $this->model->get_language(substr($column, 9));
114
115 // hidden field containing the post language for quick edit
116 if ($column == $this->get_first_language_column())
117 printf('<div class="hidden" id="lang_%d">%s</div>', esc_attr($post_id), esc_html($lang->slug));
118
119
120 // link to edit post (or a translation)
121 // use $_POST['old_lang'] to detect if the language has been modified in quick edit
122 if ($id = ($inline && $lang->slug != $_POST['old_lang']) ? ($language->slug == $lang->slug ? $post_id : 0) : $this->model->get_post($post_id, $language)) {
123 printf('<a class="%1$s" title="%2$s" href="%3$s"></a>',
124 $id == $post_id ? 'pll_icon_tick' : 'pll_icon_edit',
125 esc_attr(get_post($id)->post_title),
126 esc_url(get_edit_post_link($id))
127 );
128 }
129 // link to add a new translation
130 else {
131 printf('<a class="pll_icon_add" title="%1$s" href="%2$s"></a>',
132 __('Add new translation', 'polylang'),
133 esc_url($this->links->get_new_post_translation_link($post_id, $language))
134 );
135 }
136 }
137
138 /*
139 * quick edit & bulk edit
140 *
141 * @since 0.9
142 *
143 * @param string $column column name
144 * @param string $type either 'edit-tags' for terms list table or post type for posts list table
145 * @return string unmodified $column
146 */
147 public function quick_edit_custom_box($column, $type) {
148 if ($column == $this->get_first_language_column()) {
149
150 $elements = $this->model->get_languages_list();
151 if (current_filter() == 'bulk_edit_custom_box')
152 array_unshift($elements, (object) array('slug' => -1, 'name' => __('&mdash; No Change &mdash;')));
153
154 $dropdown = new PLL_Walker_Dropdown();
155 // the hidden field 'old_lang' allows to pass the old language to ajax request
156 printf(
157 '<fieldset class="inline-edit-col-left">
158 <div class="inline-edit-col">
159 <input type="hidden" value="" name="old_lang">
160 <label class="alignleft">
161 <span class="title">%s</span>
162 %s
163 </label>
164 </div>
165 </fieldset>',
166 __('Language', 'polylang'),
167 $dropdown->walk($elements, array('name' => 'inline_lang_choice', 'id' => ''))
168 );
169 }
170 return $column;
171 }
172
173 /*
174 * adds the language column (before the posts column) in the 'Categories' or 'Post Tags' table
175 *
176 * @since 0.1
177 *
178 * @param array $columns list of terms table columns
179 * @return array modified list of columns
180 */
181 public function add_term_column($columns) {
182 return $this->add_column($columns, 'posts');
183 }
184
185 /*
186 * fills the language column in the 'Categories' or 'Post Tags' table
187 * FIXME if inline edit breaks translations, the rows corresponding to these translations are not updated
188 *
189 * @since 0.1
190 *
191 * @param string $empty not used
192 * @param string $column column name
193 * @param int term_id
194 */
195 public function term_column($empty, $column, $term_id) {
196 $inline = defined('DOING_AJAX') && $_REQUEST['action'] == 'inline-save-tax' ? 1 : 0;
197 if (false === strpos($column, 'language_') || !($lang = $inline ? $this->model->get_language($_POST['inline_lang_choice']) : $this->model->get_term_language($term_id)))
198 return;
199
200 $post_type = isset($GLOBALS['post_type']) ? $GLOBALS['post_type'] : $_REQUEST['post_type']; // 2nd case for quick edit
201 $taxonomy = isset($GLOBALS['taxonomy']) ? $GLOBALS['taxonomy'] : $_REQUEST['taxonomy'];
202 $language = $this->model->get_language(substr($column, 9));
203
204 if ($column == $this->get_first_language_column())
205 printf('<div class="hidden" id="lang_%d">%s</div>', esc_attr($term_id), esc_html($lang->slug));
206
207 // link to edit term (or a translation)
208 if ($id = ($inline && $lang->slug != $_POST['old_lang']) ? ($language->slug == $lang->slug ? $term_id : 0) : $this->model->get_term($term_id, $language)) {
209 printf('<a class="%1$s" title="%2$s" href="%3$s"></a>',
210 $id == $term_id ? 'pll_icon_tick' : 'pll_icon_edit',
211 esc_attr(get_term($id, $taxonomy)->name),
212 esc_url(get_edit_term_link($id, $taxonomy, $post_type))
213 );
214 }
215
216 // link to add a new translation
217 else {
218 printf('<a class="pll_icon_add" title="%1$s" href="%2$s"></a>',
219 __('Add new translation', 'polylang'),
220 esc_url($this->links->get_new_term_translation_link($term_id, $taxonomy, $post_type, $language))
221 );
222 }
223 }
224 }
225