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