PluginProbe
Polylang / 2.0
Polylang v2.0
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
← All changes | settings/table-string.php +58 -137 2.82.0 View file →
@@ -1,11 +1,8 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 if ( ! class_exists( 'WP_List_Table' ) ) {
7 - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; // since WP 3.1
4 + require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); // since WP 3.1
8 5 }
9 6
10 7 /**
11 8 * A class to create the strings translations table
@@ -22,29 +19,19 @@
22 19 * @since 0.6
23 20 *
24 21 * @param array $languages list of languages
25 22 */
26 - public function __construct( $languages ) {
27 - parent::__construct(
28 - array(
29 - 'plural' => 'Strings translations', // Do not translate ( used for css class )
30 - 'ajax' => false,
31 - )
32 - );
23 + function __construct( $languages ) {
24 + parent::__construct( array(
25 + 'plural' => 'Strings translations', // Do not translate ( used for css class )
26 + 'ajax' => false,
27 + ) );
33 28
34 29 $this->languages = $languages;
35 30 $this->strings = PLL_Admin_Strings::get_strings();
36 31 $this->groups = array_unique( wp_list_pluck( $this->strings, 'context' ) );
32 + $this->selected_group = empty( $_GET['group'] ) || ! in_array( $_GET['group'], $this->groups ) ? -1 : $_GET['group'];
37 33
38 - $this->selected_group = -1;
39 -
40 - if ( ! empty( $_GET['group'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
41 - $group = sanitize_text_field( wp_unslash( $_GET['group'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
42 - if ( in_array( $group, $this->groups ) ) {
43 - $this->selected_group = $group;
44 - }
45 - }
46 -
47 34 add_action( 'mlang_action_string-translation', array( $this, 'save_translations' ) );
48 35 }
49 36
50 37 /**
@@ -55,9 +42,9 @@
55 42 * @param array $item
56 43 * @param string $column_name
57 44 * @return string
58 45 */
59 - public function column_default( $item, $column_name ) {
46 + function column_default( $item, $column_name ) {
60 47 return $item[ $column_name ];
61 48 }
62 49
63 50 /**
@@ -67,14 +54,14 @@
67 54 *
68 55 * @param array $item
69 56 * @return string
70 57 */
71 - public function column_cb( $item ) {
58 + function column_cb( $item ) {
72 59 return sprintf(
73 60 '<label class="screen-reader-text" for="cb-select-%1$s">%2$s</label><input id="cb-select-%1$s" type="checkbox" name="strings[]" value="%1$s" %3$s />',
74 61 esc_attr( $item['row'] ),
75 62 /* translators: accessibility text, %s is a string potentially in any language */
76 - sprintf( __( 'Select %s', 'polylang' ), format_to_edit( $item['string'] ) ),
63 + sprintf( __( 'Select %s' ), format_to_edit( $item['string'] ) ),
77 64 empty( $item['icl'] ) ? 'disabled' : '' // Only strings registered with WPML API can be removed
78 65 );
79 66 }
80 67
@@ -85,9 +72,9 @@
85 72 *
86 73 * @param array $item
87 74 * @return string
88 75 */
89 - public function column_string( $item ) {
76 + function column_string( $item ) {
90 77 return format_to_edit( $item['string'] ); // Don't interpret special chars for the string column
91 78 }
92 79
93 80 /**
@@ -97,9 +84,9 @@
97 84 *
98 85 * @param array $item
99 86 * @return string
100 87 */
101 - public function column_translations( $item ) {
88 + function column_translations( $item ) {
102 89 $languages = array_combine( wp_list_pluck( $this->languages, 'slug' ), wp_list_pluck( $this->languages, 'name' ) );
103 90 $out = '';
104 91
105 92 foreach ( $item['translations'] as $key => $translation ) {
@@ -105,15 +92,13 @@
105 92 foreach ( $item['translations'] as $key => $translation ) {
106 93 $input_type = $item['multiline'] ?
107 94 '<textarea name="translation[%1$s][%2$s]" id="%1$s-%2$s">%4$s</textarea>' :
108 95 '<input type="text" name="translation[%1$s][%2$s]" id="%1$s-%2$s" value="%4$s" />';
109 - $out .= sprintf(
110 - '<div class="translation"><label for="%1$s-%2$s">%3$s</label>' . $input_type . '</div>' . "\n",
96 + $out .= sprintf( '<div class="translation"><label for="%1$s-%2$s">%3$s</label>'.$input_type.'</div>'."\n",
111 97 esc_attr( $key ),
112 98 esc_attr( $item['row'] ),
113 99 esc_html( $languages[ $key ] ),
114 - format_to_edit( $translation ) // Don't interpret special chars
115 - );
100 + format_to_edit( $translation ) ); // Don't interpret special chars
116 101 }
117 102
118 103 return $out;
119 104 }
@@ -124,9 +109,9 @@
124 109 * @since 0.6
125 110 *
126 111 * @return array the list of column titles
127 112 */
128 - public function get_columns() {
113 + function get_columns() {
129 114 return array(
130 115 'cb' => '<input type="checkbox" />', // Checkbox
131 116 'string' => esc_html__( 'String', 'polylang' ),
132 117 'name' => esc_html__( 'Name', 'polylang' ),
@@ -141,9 +126,9 @@
141 126 * @since 0.6
142 127 *
143 128 * @return array
144 129 */
145 - public function get_sortable_columns() {
130 + function get_sortable_columns() {
146 131 return array(
147 132 'string' => array( 'string', false ),
148 133 'name' => array( 'name', false ),
149 134 'context' => array( 'context', false ),
@@ -150,42 +135,8 @@
150 135 );
151 136 }
152 137
153 138 /**
154 - * Gets the name of the default primary column.
155 - *
156 - * @since 2.1
157 - *
158 - * @return string Name of the default primary column, in this case, 'string'.
159 - */
160 - protected function get_default_primary_column_name() {
161 - return 'string';
162 - }
163 -
164 - /**
165 - * Search for a string in translations. Case insensitive.
166 - *
167 - * @since 2.6
168 - *
169 - * @param array $mos An array of PLL_MO objects
170 - * @param string $s Searched string
171 - * @return array Found strings
172 - */
173 - protected function search_in_translations( $mos, $s ) {
174 - $founds = array();
175 -
176 - foreach ( $mos as $mo ) {
177 - foreach ( wp_list_pluck( $mo->entries, 'translations' ) as $string => $translation ) {
178 - if ( false !== stripos( $translation[0], $s ) ) {
179 - $founds[] = $string;
180 - }
181 - }
182 - }
183 -
184 - return array_unique( $founds );
185 - }
186 -
187 - /**
188 139 * Sort items
189 140 *
190 141 * @since 0.6
191 142 *
@@ -193,17 +144,10 @@
193 144 * @param object $b The second object to compare
194 145 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
195 146 */
196 147 protected function usort_reorder( $a, $b ) {
197 - if ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
198 - $orderby = sanitize_key( $_GET['orderby'] ); // phpcs:ignore WordPress.Security.NonceVerification
199 - if ( isset( $a[ $orderby ], $b[ $orderby ] ) ) {
200 - $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order
201 - return ( empty( $_GET['order'] ) || 'asc' === $_GET['order'] ) ? $result : -$result; // phpcs:ignore WordPress.Security.NonceVerification
202 - }
203 - }
204 -
205 - return 0;
148 + $result = strcmp( $a[ $_GET['orderby'] ], $b[ $_GET['orderby'] ] ); // determine sort order
149 + return ( empty( $_GET['order'] ) || 'asc' === $_GET['order'] ) ? $result : -$result; // send final sort direction to usort
206 150 }
207 151
208 152 /**
209 153 * Prepares the list of items for displaying
@@ -209,70 +153,49 @@
209 153 * Prepares the list of items for displaying
210 154 *
211 155 * @since 0.6
212 156 */
213 - public function prepare_items() {
214 - // Is admin language filter active?
215 - if ( $lg = get_user_meta( get_current_user_id(), 'pll_filter_content', true ) ) {
216 - $languages = wp_list_filter( $this->languages, array( 'slug' => $lg ) );
217 - } else {
218 - $languages = $this->languages;
219 - }
220 -
221 - // Load translations
222 - $mo = array();
223 - foreach ( $languages as $language ) {
224 - $mo[ $language->slug ] = new PLL_MO();
225 - $mo[ $language->slug ]->import_from_db( $language );
226 - }
227 -
157 + function prepare_items() {
228 158 $data = $this->strings;
229 159
230 - // Filter by selected group
231 - if ( -1 !== $this->selected_group ) {
232 - $data = wp_list_filter( $data, array( 'context' => $this->selected_group ) );
160 + // Filter for search string
161 + $s = empty( $_GET['s'] ) ? '' : wp_unslash( $_GET['s'] );
162 + foreach ( $data as $key => $row ) {
163 + if ( ( -1 !== $this->selected_group && $row['context'] !== $this->selected_group ) || ( ! empty( $s ) && stripos( $row['name'], $s ) === false && stripos( $row['string'], $s ) === false ) ) {
164 + unset( $data[ $key ] );
165 + }
233 166 }
234 167
235 - // Filter by searched string
236 - $s = empty( $_GET['s'] ) ? '' : wp_unslash( $_GET['s'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
168 + // Load translations
169 + foreach ( $this->languages as $language ) {
170 + // Filters by language if requested
171 + if ( ( $lg = get_user_meta( get_current_user_id(), 'pll_filter_content', true ) ) && $language->slug !== $lg ) {
172 + continue;
173 + }
237 174
238 - if ( ! empty( $s ) ) {
239 - // Search in translations
240 - $in_translations = $this->search_in_translations( $mo, $s );
241 -
175 + $mo = new PLL_MO();
176 + $mo->import_from_db( $language );
242 177 foreach ( $data as $key => $row ) {
243 - if ( stripos( $row['name'], $s ) === false && stripos( $row['string'], $s ) === false && ! in_array( $row['string'], $in_translations ) ) {
244 - unset( $data[ $key ] );
245 - }
178 + $data[ $key ]['translations'][ $language->slug ] = $mo->translate( $row['string'] );
179 + $data[ $key ]['row'] = $key; // Store the row number for convenience
246 180 }
247 181 }
248 182
249 - // Sorting
250 - uasort( $data, array( $this, 'usort_reorder' ) );
251 -
252 - // Paging
253 183 $per_page = $this->get_items_per_page( 'pll_strings_per_page' );
254 184 $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
255 185
186 + if ( ! empty( $_GET['orderby'] ) ) { // No sort by default
187 + usort( $data, array( $this, 'usort_reorder' ) );
188 + }
189 +
256 190 $total_items = count( $data );
257 - $this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page, true );
191 + $this->items = array_slice( $data, ( $this->get_pagenum() - 1 ) * $per_page, $per_page );
258 192
259 - $this->set_pagination_args(
260 - array(
261 - 'total_items' => $total_items,
262 - 'per_page' => $per_page,
263 - 'total_pages' => ceil( $total_items / $per_page ),
264 - )
265 - );
266 -
267 - // Translate strings
268 - // Kept for the end as it is a slow process
269 - foreach ( $languages as $language ) {
270 - foreach ( $this->items as $key => $row ) {
271 - $this->items[ $key ]['translations'][ $language->slug ] = $mo[ $language->slug ]->translate( $row['string'] );
272 - $this->items[ $key ]['row'] = $key; // Store the row number for convenience
273 - }
274 - }
193 + $this->set_pagination_args( array(
194 + 'total_items' => $total_items,
195 + 'per_page' => $per_page,
196 + 'total_pages' => ceil( $total_items / $per_page ),
197 + ) );
275 198 }
276 199
277 200 /**
278 201 * Get the list of possible bulk actions
@@ -280,10 +203,10 @@
280 203 * @since 1.1
281 204 *
282 205 * @return array
283 206 */
284 - public function get_bulk_actions() {
285 - return array( 'delete' => __( 'Delete', 'polylang' ) );
207 + function get_bulk_actions() {
208 + return array( 'delete' => __( 'Delete','polylang' ) );
286 209 }
287 210
288 211 /**
289 212 * Get the current action selected from the bulk actions dropdown.
@@ -293,9 +216,9 @@
293 216 *
294 217 * @return string|false The action name or False if no action was selected
295 218 */
296 219 public function current_action() {
297 - return empty( $_POST['submit'] ) ? parent::current_action() : false; // phpcs:ignore WordPress.Security.NonceVerification
220 + return empty( $_POST['submit'] ) ? parent::current_action() : false;
298 221 }
299 222
300 223 /**
301 224 * Displays the dropdown list to filter strings per group
@@ -303,9 +226,9 @@
303 226 * @since 1.1
304 227 *
305 228 * @param string $which only 'top' is supported
306 229 */
307 - public function extra_tablenav( $which ) {
230 + function extra_tablenav( $which ) {
308 231 if ( 'top' !== $which ) {
309 232 return;
310 233 }
311 234
@@ -317,9 +240,9 @@
317 240 );
318 241 echo '<select id="select-group" name="group">' . "\n";
319 242 printf(
320 243 '<option value="-1"%s>%s</option>' . "\n",
321 - selected( $this->group_selected, -1, false ),
244 + -1 === $this->group_selected ? ' selected="selected"' : '',
322 245 esc_html__( 'View all groups', 'polylang' )
323 246 );
324 247
325 248 foreach ( $this->groups as $group ) {
@@ -325,15 +248,15 @@
325 248 foreach ( $this->groups as $group ) {
326 249 printf(
327 250 '<option value="%s"%s>%s</option>' . "\n",
328 251 esc_attr( urlencode( $group ) ),
329 - selected( $this->selected_group, $group, false ),
252 + $this->selected_group === $group ? ' selected="selected"' : '',
330 253 esc_html( $group )
331 254 );
332 255 }
333 - echo '</select>' . "\n";
256 + echo '</select>'."\n";
334 257
335 - submit_button( __( 'Filter', 'polylang' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
258 + submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
336 259 echo '</div>';
337 260 }
338 261
339 262 /**
@@ -353,20 +276,18 @@
353 276
354 277 $mo = new PLL_MO();
355 278 $mo->import_from_db( $language );
356 279
357 - $translations = array_map( 'trim', wp_unslash( $_POST['translation'][ $language->slug ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
358 - foreach ( $translations as $key => $translation ) {
280 + foreach ( $_POST['translation'][ $language->slug ] as $key => $translation ) {
359 281 /**
360 282 * Filter the string translation before it is saved in DB
361 283 * Allows to sanitize strings registered with pll_register_string
362 284 *
363 285 * @since 1.6
364 - * @since 2.7 The translation passed to the filter is unslashed.
365 286 *
366 - * @param string $translation The string translation.
367 - * @param string $name The name as defined in pll_register_string.
368 - * @param string $context The context as defined in pll_register_string.
287 + * @param string $translation the string translation
288 + * @param string $name the name as defined in pll_register_string
289 + * @param string $context the context as defined in pll_register_string
369 290 */
370 291 $translation = apply_filters( 'pll_sanitize_string_translation', $translation, $this->strings[ $key ]['name'], $this->strings[ $key ]['context'] );
371 292 $mo->add_entry( $mo->make_entry( $this->strings[ $key ]['string'], $translation ) );
372 293 }
@@ -394,9 +315,9 @@
394 315 }
395 316
396 317 // Unregisters strings registered through WPML API
397 318 if ( $this->current_action() === 'delete' && ! empty( $_POST['strings'] ) && function_exists( 'icl_unregister_string' ) ) {
398 - foreach ( array_map( 'sanitize_key', $_POST['strings'] ) as $key ) {
319 + foreach ( $_POST['strings'] as $key ) {
399 320 icl_unregister_string( $this->strings[ $key ]['context'], $this->strings[ $key ]['name'] );
400 321 }
401 322 }
402 323