PluginProbe
Polylang / 3.7.1
Polylang v3.7.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
← All changes | settings/table-string.php +91 -51 2.9.23.7.1 View file →
@@ -13,16 +13,42 @@
13 13 *
14 14 * @since 0.6
15 15 */
16 16 class PLL_Table_String extends WP_List_Table {
17 - protected $languages, $strings, $groups, $selected_group;
17 + /**
18 + * The list of languages.
19 + *
20 + * @var PLL_Language[]
21 + */
22 + protected $languages;
18 23
19 24 /**
20 - * Constructor
25 + * Registered strings.
21 26 *
27 + * @var array
28 + */
29 + protected $strings;
30 +
31 + /**
32 + * The string groups.
33 + *
34 + * @var string[]
35 + */
36 + protected $groups;
37 +
38 + /**
39 + * The selected string group or -1 if none is selected.
40 + *
41 + * @var string|int
42 + */
43 + protected $selected_group;
44 +
45 + /**
46 + * Constructor.
47 + *
22 48 * @since 0.6
23 49 *
24 - * @param array $languages list of languages
50 + * @param PLL_Language[] $languages List of languages.
25 51 */
26 52 public function __construct( $languages ) {
27 53 parent::__construct(
28 54 array(
@@ -47,14 +73,14 @@
47 73 add_action( 'mlang_action_string-translation', array( $this, 'save_translations' ) );
48 74 }
49 75
50 76 /**
51 - * Displays the item information in a column ( default case )
77 + * Displays the item information in a column (default case).
52 78 *
53 79 * @since 0.6
54 80 *
55 - * @param array $item
56 - * @param string $column_name
81 + * @param array $item Data related to the current string.
82 + * @param string $column_name The current column name.
57 83 * @return string
58 84 */
59 85 public function column_default( $item, $column_name ) {
60 86 return $item[ $column_name ];
@@ -60,13 +86,13 @@
60 86 return $item[ $column_name ];
61 87 }
62 88
63 89 /**
64 - * Displays the checkbox in first column
90 + * Displays the checkbox in first column.
65 91 *
66 92 * @since 1.1
67 93 *
68 - * @param array $item
94 + * @param array $item Data related to the current string.
69 95 * @return string
70 96 */
71 97 public function column_cb( $item ) {
72 98 return sprintf(
@@ -73,36 +99,40 @@
73 99 '<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 100 esc_attr( $item['row'] ),
75 101 /* translators: accessibility text, %s is a string potentially in any language */
76 102 sprintf( __( 'Select %s', 'polylang' ), format_to_edit( $item['string'] ) ),
77 - empty( $item['icl'] ) ? 'disabled' : '' // Only strings registered with WPML API can be removed
103 + empty( $item['icl'] ) ? 'disabled' : '' // Only strings registered with WPML API can be removed.
78 104 );
79 105 }
80 106
81 107 /**
82 - * Displays the string to translate
108 + * Displays the string to translate.
83 109 *
84 110 * @since 1.0
85 111 *
86 - * @param array $item
112 + * @param array $item Data related to the current string.
87 113 * @return string
88 114 */
89 115 public function column_string( $item ) {
90 - return format_to_edit( $item['string'] ); // Don't interpret special chars for the string column
116 + return format_to_edit( $item['string'] ); // Don't interpret special chars for the string column.
91 117 }
92 118
93 119 /**
94 - * Displays the translations to edit
120 + * Displays the translations to edit.
95 121 *
96 122 * @since 0.6
97 123 *
98 - * @param array $item
124 + * @param array $item Data related to the current string.
99 125 * @return string
100 126 */
101 127 public function column_translations( $item ) {
102 - $languages = array_combine( wp_list_pluck( $this->languages, 'slug' ), wp_list_pluck( $this->languages, 'name' ) );
103 - $out = '';
128 + $out = '';
129 + $languages = array();
104 130
131 + foreach ( $this->languages as $language ) {
132 + $languages[ $language->slug ] = $language->name;
133 + }
134 +
105 135 foreach ( $item['translations'] as $key => $translation ) {
106 136 $input_type = $item['multiline'] ?
107 137 '<textarea name="translation[%1$s][%2$s]" id="%1$s-%2$s">%4$s</textarea>' :
108 138 '<input type="text" name="translation[%1$s][%2$s]" id="%1$s-%2$s" value="%4$s" />';
@@ -110,9 +140,9 @@
110 140 '<div class="translation"><label for="%1$s-%2$s">%3$s</label>' . $input_type . '</div>' . "\n",
111 141 esc_attr( $key ),
112 142 esc_attr( $item['row'] ),
113 143 esc_html( $languages[ $key ] ),
114 - format_to_edit( $translation ) // Don't interpret special chars
144 + format_to_edit( $translation ) // Don't interpret special chars.
115 145 );
116 146 }
117 147
118 148 return $out;
@@ -118,17 +148,17 @@
118 148 return $out;
119 149 }
120 150
121 151 /**
122 - * Gets the list of columns
152 + * Gets the list of columns.
123 153 *
124 154 * @since 0.6
125 155 *
126 - * @return array the list of column titles
156 + * @return string[] The list of column titles.
127 157 */
128 158 public function get_columns() {
129 159 return array(
130 - 'cb' => '<input type="checkbox" />', // Checkbox
160 + 'cb' => '<input type="checkbox" />', // Checkbox.
131 161 'string' => esc_html__( 'String', 'polylang' ),
132 162 'name' => esc_html__( 'Name', 'polylang' ),
133 163 'context' => esc_html__( 'Group', 'polylang' ),
134 164 'translations' => esc_html__( 'Translations', 'polylang' ),
@@ -165,16 +195,18 @@
165 195 * Search for a string in translations. Case insensitive.
166 196 *
167 197 * @since 2.6
168 198 *
169 - * @param array $mos An array of PLL_MO objects
170 - * @param string $s Searched string
171 - * @return array Found strings
199 + * @param PLL_Language[] $languages An array of language objects.
200 + * @param string $s Searched string.
201 + * @return string[] Found strings.
172 202 */
173 - protected function search_in_translations( $mos, $s ) {
203 + protected function search_in_translations( $languages, $s ) {
174 204 $founds = array();
175 205
176 - foreach ( $mos as $mo ) {
206 + foreach ( $languages as $language ) {
207 + $mo = new PLL_MO();
208 + $mo->import_from_db( $language );
177 209 foreach ( wp_list_pluck( $mo->entries, 'translations' ) as $string => $translation ) {
178 210 if ( false !== stripos( $translation[0], $s ) ) {
179 211 $founds[] = $string;
180 212 }
@@ -184,14 +216,14 @@
184 216 return array_unique( $founds );
185 217 }
186 218
187 219 /**
188 - * Sort items
220 + * Sorts registered string items.
189 221 *
190 222 * @since 0.6
191 223 *
192 - * @param object $a The first object to compare
193 - * @param object $b The second object to compare
224 + * @param array $a The first item to compare.
225 + * @param array $b The second item to compare.
194 226 * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b.
195 227 */
196 228 protected function usort_reorder( $a, $b ) {
197 229 if ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
@@ -205,11 +237,13 @@
205 237 return 0;
206 238 }
207 239
208 240 /**
209 - * Prepares the list of items for displaying
241 + * Prepares the list of registered strings for display.
210 242 *
211 243 * @since 0.6
244 + *
245 + * @return void
212 246 */
213 247 public function prepare_items() {
214 248 // Is admin language filter active?
215 249 if ( $lg = get_user_meta( get_current_user_id(), 'pll_filter_content', true ) ) {
@@ -217,15 +251,8 @@
217 251 } else {
218 252 $languages = $this->languages;
219 253 }
220 254
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 -
228 255 $data = $this->strings;
229 256
230 257 // Filter by selected group
231 258 if ( -1 !== $this->selected_group ) {
@@ -236,9 +263,9 @@
236 263 $s = empty( $_GET['s'] ) ? '' : wp_unslash( $_GET['s'] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
237 264
238 265 if ( ! empty( $s ) ) {
239 266 // Search in translations
240 - $in_translations = $this->search_in_translations( $mo, $s );
267 + $in_translations = $this->search_in_translations( $languages, $s );
241 268
242 269 foreach ( $data as $key => $row ) {
243 270 if ( stripos( $row['name'], $s ) === false && stripos( $row['string'], $s ) === false && ! in_array( $row['string'], $in_translations ) ) {
244 271 unset( $data[ $key ] );
@@ -259,9 +286,9 @@
259 286 $this->set_pagination_args(
260 287 array(
261 288 'total_items' => $total_items,
262 289 'per_page' => $per_page,
263 - 'total_pages' => ceil( $total_items / $per_page ),
290 + 'total_pages' => (int) ceil( $total_items / $per_page ),
264 291 )
265 292 );
266 293
267 294 // Translate strings
@@ -266,21 +293,23 @@
266 293
267 294 // Translate strings
268 295 // Kept for the end as it is a slow process
269 296 foreach ( $languages as $language ) {
297 + $mo = new PLL_MO();
298 + $mo->import_from_db( $language );
270 299 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
300 + $this->items[ $key ]['translations'][ $language->slug ] = $mo->translate_if_any( $row['string'] );
301 + $this->items[ $key ]['row'] = $key; // Store the row number for convenience
273 302 }
274 303 }
275 304 }
276 305
277 306 /**
278 - * Get the list of possible bulk actions
307 + * Get the list of possible bulk actions.
279 308 *
280 309 * @since 1.1
281 310 *
282 - * @return array
311 + * @return string[] Array of bulk actions.
283 312 */
284 313 public function get_bulk_actions() {
285 314 return array( 'delete' => __( 'Delete', 'polylang' ) );
286 315 }
@@ -302,8 +331,9 @@
302 331 *
303 332 * @since 1.1
304 333 *
305 334 * @param string $which only 'top' is supported
335 + * @return void
306 336 */
307 337 public function extra_tablenav( $which ) {
308 338 if ( 'top' !== $which ) {
309 339 return;
@@ -317,9 +347,9 @@
317 347 );
318 348 echo '<select id="select-group" name="group">' . "\n";
319 349 printf(
320 350 '<option value="-1"%s>%s</option>' . "\n",
321 - selected( $this->group_selected, -1, false ),
351 + selected( $this->selected_group, -1, false ),
322 352 esc_html__( 'View all groups', 'polylang' )
323 353 );
324 354
325 355 foreach ( $this->groups as $group ) {
@@ -337,11 +367,13 @@
337 367 }
338 368
339 369 /**
340 370 * Saves the strings translations in DB
341 - * Optionaly clean the DB
371 + * Optionally clean the DB
342 372 *
343 373 * @since 1.9
374 + *
375 + * @return void
344 376 */
345 377 public function save_translations() {
346 378 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
347 379
@@ -346,30 +378,38 @@
346 378 check_admin_referer( 'string-translation', '_wpnonce_string-translation' );
347 379
348 380 if ( ! empty( $_POST['submit'] ) ) {
349 381 foreach ( $this->languages as $language ) {
350 - if ( empty( $_POST['translation'][ $language->slug ] ) ) { // In case the language filter is active ( thanks to John P. Bloch )
382 + if ( empty( $_POST['translation'][ $language->slug ] ) || ! is_array( $_POST['translation'][ $language->slug ] ) ) { // In case the language filter is active ( thanks to John P. Bloch )
351 383 continue;
352 384 }
353 385
386 + $translations = array_map( 'trim', (array) wp_unslash( $_POST['translation'][ $language->slug ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
387 +
354 388 $mo = new PLL_MO();
355 389 $mo->import_from_db( $language );
356 390
357 - $translations = array_map( 'trim', wp_unslash( $_POST['translation'][ $language->slug ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
358 391 foreach ( $translations as $key => $translation ) {
359 392 /**
360 - * Filter the string translation before it is saved in DB
361 - * Allows to sanitize strings registered with pll_register_string
393 + * Filters the string translation before it is saved in DB.
394 + * Allows to sanitize strings registered with pll_register_string().
362 395 *
363 396 * @since 1.6
364 397 * @since 2.7 The translation passed to the filter is unslashed.
398 + * @since 3.7 Add original string as 4th parameter.
365 399 *
366 400 * @param string $translation The string translation.
367 401 * @param string $name The name as defined in pll_register_string.
368 402 * @param string $context The context as defined in pll_register_string.
403 + * @param string $original The original string to translate.
369 404 */
370 - $translation = apply_filters( 'pll_sanitize_string_translation', $translation, $this->strings[ $key ]['name'], $this->strings[ $key ]['context'] );
371 - $mo->add_entry( $mo->make_entry( $this->strings[ $key ]['string'], $translation ) );
405 + $translation = apply_filters( 'pll_sanitize_string_translation', $translation, $this->strings[ $key ]['name'], $this->strings[ $key ]['context'], $this->strings[ $key ]['string'] );
406 + $mo->add_entry(
407 + $mo->make_entry(
408 + $this->strings[ $key ]['string'],
409 + $translation
410 + )
411 + );
372 412 }
373 413
374 414 // Clean database ( removes all strings which were registered some day but are no more )
375 415 if ( ! empty( $_POST['clean'] ) ) {
@@ -382,9 +422,9 @@
382 422
383 423 isset( $new_mo ) ? $new_mo->export_to_db( $language ) : $mo->export_to_db( $language );
384 424 }
385 425
386 - add_settings_error( 'general', 'pll_strings_translations_updated', __( 'Translations updated.', 'polylang' ), 'updated' );
426 + pll_add_notice( new WP_Error( 'pll_strings_translations_updated', __( 'Translations updated.', 'polylang' ), 'success' ) );
387 427
388 428 /**
389 429 * Fires after the strings translations are saved in DB
390 430 *