| @@ -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,11 +195,11 @@ | ||
| 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_MO[] $mos An array of PLL_MO objects. | |
| 200 | + * @param string $s Searched string. | |
| 201 | + * @return string[] Found strings. | |
| 169 | 202 | */ |
| 170 | 203 | protected function search_in_translations( $mos, $s ) { |
| 171 | 204 | $founds = array(); |
| 172 | 205 | |
| @@ -181,14 +214,14 @@ | ||
| 181 | 214 | return array_unique( $founds ); |
| 182 | 215 | } |
| 183 | 216 | |
| 184 | 217 | /** |
| 185 | - * Sort items | |
| 218 | + * Sorts registered string items. | |
| 186 | 219 | * |
| 187 | 220 | * @since 0.6 |
| 188 | 221 | * |
| 189 | - * @param object $a The first object to compare | |
| 190 | - * @param object $b The second object to compare | |
| 222 | + * @param array $a The first item to compare. | |
| 223 | + * @param array $b The second item to compare. | |
| 191 | 224 | * @return int -1 or 1 if $a is considered to be respectively less than or greater than $b. |
| 192 | 225 | */ |
| 193 | 226 | protected function usort_reorder( $a, $b ) { |
| 194 | 227 | if ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -202,11 +235,13 @@ | ||
| 202 | 235 | return 0; |
| 203 | 236 | } |
| 204 | 237 | |
| 205 | 238 | /** |
| 206 | - * Prepares the list of items for displaying | |
| 239 | + * Prepares the list of registered strings for display. | |
| 207 | 240 | * |
| 208 | 241 | * @since 0.6 |
| 242 | + * | |
| 243 | + * @return void | |
| 209 | 244 | */ |
| 210 | 245 | public function prepare_items() { |
| 211 | 246 | // Is admin language filter active? |
| 212 | 247 | if ( $lg = get_user_meta( get_current_user_id(), 'pll_filter_content', true ) ) { |
| @@ -256,9 +291,9 @@ | ||
| 256 | 291 | $this->set_pagination_args( |
| 257 | 292 | array( |
| 258 | 293 | 'total_items' => $total_items, |
| 259 | 294 | 'per_page' => $per_page, |
| 260 | - 'total_pages' => ceil( $total_items / $per_page ), | |
| 295 | + 'total_pages' => (int) ceil( $total_items / $per_page ), | |
| 261 | 296 | ) |
| 262 | 297 | ); |
| 263 | 298 | |
| 264 | 299 | // Translate strings |
| @@ -271,13 +306,13 @@ | ||
| 271 | 306 | } |
| 272 | 307 | } |
| 273 | 308 | |
| 274 | 309 | /** |
| 275 | - * Get the list of possible bulk actions | |
| 310 | + * Get the list of possible bulk actions. | |
| 276 | 311 | * |
| 277 | 312 | * @since 1.1 |
| 278 | 313 | * |
| 279 | - * @return array | |
| 314 | + * @return string[] Array of bulk actions. | |
| 280 | 315 | */ |
| 281 | 316 | public function get_bulk_actions() { |
| 282 | 317 | return array( 'delete' => __( 'Delete', 'polylang' ) ); |
| 283 | 318 | } |
| @@ -299,8 +334,9 @@ | ||
| 299 | 334 | * |
| 300 | 335 | * @since 1.1 |
| 301 | 336 | * |
| 302 | 337 | * @param string $which only 'top' is supported |
| 338 | + * @return void | |
| 303 | 339 | */ |
| 304 | 340 | public function extra_tablenav( $which ) { |
| 305 | 341 | if ( 'top' !== $which ) { |
| 306 | 342 | return; |
| @@ -314,9 +350,9 @@ | ||
| 314 | 350 | ); |
| 315 | 351 | echo '<select id="select-group" name="group">' . "\n"; |
| 316 | 352 | printf( |
| 317 | 353 | '<option value="-1"%s>%s</option>' . "\n", |
| 318 | - selected( $this->group_selected, -1, false ), | |
| 354 | + selected( $this->selected_group, -1, false ), | |
| 319 | 355 | esc_html__( 'View all groups', 'polylang' ) |
| 320 | 356 | ); |
| 321 | 357 | |
| 322 | 358 | foreach ( $this->groups as $group ) { |
| @@ -334,11 +370,13 @@ | ||
| 334 | 370 | } |
| 335 | 371 | |
| 336 | 372 | /** |
| 337 | 373 | * Saves the strings translations in DB |
| 338 | - * Optionaly clean the DB | |
| 374 | + * Optionally clean the DB | |
| 339 | 375 | * |
| 340 | 376 | * @since 1.9 |
| 377 | + * | |
| 378 | + * @return void | |
| 341 | 379 | */ |
| 342 | 380 | public function save_translations() { |
| 343 | 381 | check_admin_referer( 'string-translation', '_wpnonce_string-translation' ); |
| 344 | 382 | |
| @@ -343,16 +381,17 @@ | ||
| 343 | 381 | check_admin_referer( 'string-translation', '_wpnonce_string-translation' ); |
| 344 | 382 | |
| 345 | 383 | if ( ! empty( $_POST['submit'] ) ) { |
| 346 | 384 | foreach ( $this->languages as $language ) { |
| 347 | - if ( empty( $_POST['translation'][ $language->slug ] ) ) { // In case the language filter is active ( thanks to John P. Bloch ) | |
| 385 | + 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 | 386 | continue; |
| 349 | 387 | } |
| 350 | 388 | |
| 389 | + $translations = array_map( 'trim', (array) wp_unslash( $_POST['translation'][ $language->slug ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 390 | + | |
| 351 | 391 | $mo = new PLL_MO(); |
| 352 | 392 | $mo->import_from_db( $language ); |
| 353 | 393 | |
| 354 | - $translations = array_map( 'trim', wp_unslash( $_POST['translation'][ $language->slug ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 355 | 394 | foreach ( $translations as $key => $translation ) { |
| 356 | 395 | /** |
| 357 | 396 | * Filter the string translation before it is saved in DB |
| 358 | 397 | * Allows to sanitize strings registered with pll_register_string |
| @@ -379,9 +418,9 @@ | ||
| 379 | 418 | |
| 380 | 419 | isset( $new_mo ) ? $new_mo->export_to_db( $language ) : $mo->export_to_db( $language ); |
| 381 | 420 | } |
| 382 | 421 | |
| 383 | - add_settings_error( 'general', 'pll_strings_translations_updated', __( 'Translations updated.', 'polylang' ), 'updated' ); | |
| 422 | + pll_add_notice( new WP_Error( 'pll_strings_translations_updated', __( 'Translations updated.', 'polylang' ), 'success' ) ); | |
| 384 | 423 | |
| 385 | 424 | /** |
| 386 | 425 | * Fires after the strings translations are saved in DB |
| 387 | 426 | * |