| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | * @author Tobias Bäthge |
| 8 | 8 | * @since 2.0.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +declare(strict_types=1); | |
| 12 | + | |
| 11 | 13 | // Prohibit direct script loading. |
| 12 | 14 | defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 | 15 | |
| 14 | 16 | /** |
| @@ -24,11 +26,10 @@ | ||
| 24 | 26 | /** |
| 25 | 27 | * Number of items of the initial data set (before sort, search, and pagination). |
| 26 | 28 | * |
| 27 | 29 | * @since 1.0.0 |
| 28 | - * @var int | |
| 29 | 30 | */ |
| 30 | - protected $items_count = 0; | |
| 31 | + protected int $items_count = 0; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * Initializes the List Table. |
| 34 | 35 | * |
| @@ -34,9 +35,9 @@ | ||
| 34 | 35 | * |
| 35 | 36 | * @since 1.0.0 |
| 36 | 37 | */ |
| 37 | 38 | public function __construct() { |
| 38 | - // @phpstan-ignore-next-line (WordPress Core's docblocks state wrong argument types in some places.) | |
| 39 | + // @phpstan-ignore argument.type (WordPress Core's docblocks state wrong argument types in some places.) | |
| 39 | 40 | parent::__construct( array( |
| 40 | 41 | 'singular' => 'tablepress-table', // Singular name of the listed records. |
| 41 | 42 | 'plural' => 'tablepress-editor-button-list', // Plural name of the listed records. |
| 42 | 43 | 'ajax' => false, // Does this list table support AJAX? |
| @@ -63,8 +64,9 @@ | ||
| 63 | 64 | * @since 1.0.0 |
| 64 | 65 | * |
| 65 | 66 | * @return bool true (Default value). |
| 66 | 67 | */ |
| 68 | + #[\Override] | |
| 67 | 69 | public function ajax_user_can(): bool { |
| 68 | 70 | return true; |
| 69 | 71 | } |
| 70 | 72 | |
| @@ -76,8 +78,9 @@ | ||
| 76 | 78 | * @since 1.0.0 |
| 77 | 79 | * |
| 78 | 80 | * @return array<string, string> List of columns in this List Table. |
| 79 | 81 | */ |
| 82 | + #[\Override] | |
| 80 | 83 | public function get_columns(): array { |
| 81 | 84 | $columns = array( |
| 82 | 85 | // "name" is special in WP, which is why we prefix every entry here, to be safe! |
| 83 | 86 | 'table_id' => __( 'ID', 'tablepress' ), |
| @@ -96,8 +99,9 @@ | ||
| 96 | 99 | * @since 1.0.0 |
| 97 | 100 | * |
| 98 | 101 | * @return array<string, array{string, bool}> List of sortable columns in this List Table. |
| 99 | 102 | */ |
| 103 | + #[\Override] | |
| 100 | 104 | protected function get_sortable_columns(): array { |
| 101 | 105 | // No sorting on the Empty List placeholder. |
| 102 | 106 | if ( ! $this->has_items() ) { |
| 103 | 107 | return array(); |
| @@ -117,8 +121,9 @@ | ||
| 117 | 121 | * @since 1.7.0 |
| 118 | 122 | * |
| 119 | 123 | * @return string Name of the default primary column, in this case, the table name. |
| 120 | 124 | */ |
| 125 | + #[\Override] | |
| 121 | 126 | protected function get_default_primary_column_name(): string { |
| 122 | 127 | return 'table_name'; |
| 123 | 128 | } |
| 124 | 129 | |
| @@ -134,8 +139,25 @@ | ||
| 134 | 139 | return esc_html( $item['id'] ); |
| 135 | 140 | } |
| 136 | 141 | |
| 137 | 142 | /** |
| 143 | + * Returns a clean label for the primary (Table Name) column's row header `aria-label`. | |
| 144 | + * | |
| 145 | + * Provides screen readers with just the table name as the row header name, | |
| 146 | + * preventing them from computing the name from the full cell content. | |
| 147 | + * | |
| 148 | + * @since 3.3.4 | |
| 149 | + * | |
| 150 | + * @param array<string, mixed> $item Data item for the current row. | |
| 151 | + * @return string The table name. | |
| 152 | + */ | |
| 153 | + protected function get_primary_column_aria_label( /* array */ $item ): string { | |
| 154 | + // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited. | |
| 155 | + | |
| 156 | + return trim( $item['name'] ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 138 | 160 | * Renders a cell in the "table_name" column. |
| 139 | 161 | * |
| 140 | 162 | * @since 1.0.0 |
| 141 | 163 | * |
| @@ -172,9 +194,9 @@ | ||
| 172 | 194 | * @param array<string, mixed> $item Data item for the current row. |
| 173 | 195 | * @return string HTML content of the cell. |
| 174 | 196 | */ |
| 175 | 197 | protected function column_table_action( array $item ): string { |
| 176 | - return '<input type="button" class="insert-shortcode button" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '" />'; | |
| 198 | + return '<input type="button" class="insert-shortcode button button-compact" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '">'; | |
| 177 | 199 | } |
| 178 | 200 | |
| 179 | 201 | /** |
| 180 | 202 | * Holds the message to be displayed when there are no items in the table. |
| @@ -180,8 +202,9 @@ | ||
| 180 | 202 | * Holds the message to be displayed when there are no items in the table. |
| 181 | 203 | * |
| 182 | 204 | * @since 1.0.0 |
| 183 | 205 | */ |
| 206 | + #[\Override] | |
| 184 | 207 | public function no_items(): void { |
| 185 | 208 | _e( 'No tables found.', 'tablepress' ); |
| 186 | 209 | if ( 0 === $this->items_count ) { |
| 187 | 210 | echo ' ' . __( 'You should add or import a table on the TablePress screens to get started!', 'tablepress' ); |
| @@ -196,8 +219,9 @@ | ||
| 196 | 219 | * @since 1.0.0 |
| 197 | 220 | * |
| 198 | 221 | * @param 'top'|'bottom' $which Location ("top" or "bottom"). |
| 199 | 222 | */ |
| 223 | + #[\Override] | |
| 200 | 224 | protected function display_tablenav( /* string */ $which ): void { |
| 201 | 225 | // Don't use type hints in the method declaration to prevent PHP errors, as the method is inherited. |
| 202 | 226 | |
| 203 | 227 | if ( ! $this->has_items() ) { |
| @@ -214,9 +238,9 @@ | ||
| 214 | 238 | add_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10, 5 ); |
| 215 | 239 | $this->pagination( $which ); |
| 216 | 240 | remove_filter( 'ngettext_default', array( $this, 'change_pagination_items_string' ), 10 ); |
| 217 | 241 | ?> |
| 218 | - <br class="clear" /> | |
| 242 | + <br class="clear"> | |
| 219 | 243 | </div> |
| 220 | 244 | <?php |
| 221 | 245 | } |
| 222 | 246 | |
| @@ -233,8 +257,9 @@ | ||
| 233 | 257 | * @return string The changed translation. |
| 234 | 258 | */ |
| 235 | 259 | public function change_pagination_items_string( string $translation, string $single, string $plural, int $number, string $domain ): string { |
| 236 | 260 | if ( '%s item' === $single && '%s items' === $plural ) { |
| 261 | + /* translators: %s: Number of tables */ | |
| 237 | 262 | $translation = _n( '%s table', '%s tables', $number, 'tablepress' ); |
| 238 | 263 | } |
| 239 | 264 | return $translation; |
| 240 | 265 | } |
| @@ -251,9 +276,9 @@ | ||
| 251 | 276 | static $term; |
| 252 | 277 | static $json_encoded_term; |
| 253 | 278 | if ( is_null( $term ) || is_null( $json_encoded_term ) ) { |
| 254 | 279 | $term = wp_unslash( $_GET['s'] ); |
| 255 | - $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore-line | |
| 280 | + $json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); // @phpstan-ignore argument.type | |
| 256 | 281 | } |
| 257 | 282 | |
| 258 | 283 | // Load table again, with table data, but without options and visibility settings. |
| 259 | 284 | $item = TablePress::$model_table->load( $item, true, false ); |
| @@ -266,15 +291,17 @@ | ||
| 266 | 291 | if ( isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) { |
| 267 | 292 | return false; |
| 268 | 293 | } |
| 269 | 294 | |
| 295 | + $fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos'; | |
| 296 | + | |
| 270 | 297 | // Search from easy to hard, so that "expensive" code maybe doesn't have to run. |
| 271 | - if ( false !== stripos( $item['id'], $term ) | |
| 272 | - || false !== stripos( $item['name'], $term ) | |
| 273 | - || false !== stripos( $item['description'], $term ) | |
| 274 | - || false !== stripos( TablePress::get_user_display_name( $item['author'] ), $term ) | |
| 275 | - || false !== stripos( TablePress::format_datetime( $item['last_modified'] ), $term ) | |
| 276 | - || false !== stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), $json_encoded_term ) ) { // @phpstan-ignore-line | |
| 298 | + if ( false !== $fn_stripos( $item['id'], (string) $term ) | |
| 299 | + || false !== $fn_stripos( $item['name'], (string) $term ) | |
| 300 | + || false !== $fn_stripos( $item['description'], (string) $term ) | |
| 301 | + || false !== $fn_stripos( TablePress::get_user_display_name( $item['author'] ), (string) $term ) | |
| 302 | + || false !== $fn_stripos( TablePress::format_datetime( $item['last_modified'] ), (string) $term ) | |
| 303 | + || false !== $fn_stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), (string) $json_encoded_term ) ) { // @phpstan-ignore argument.type | |
| 277 | 304 | return true; |
| 278 | 305 | } |
| 279 | 306 | |
| 280 | 307 | return false; |
| @@ -306,8 +333,9 @@ | ||
| 306 | 333 | * Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination. |
| 307 | 334 | * |
| 308 | 335 | * @since 1.0.0 |
| 309 | 336 | */ |
| 337 | + #[\Override] | |
| 310 | 338 | public function prepare_items(): void { |
| 311 | 339 | global $orderby, $order, $s; |
| 312 | 340 | wp_reset_vars( array( 'orderby', 'order', 's' ) ); |
| 313 | 341 | |
| @@ -320,10 +348,9 @@ | ||
| 320 | 348 | foreach ( $this->items as &$item ) { |
| 321 | 349 | // Don't load data nor table options. |
| 322 | 350 | $item = TablePress::$model_table->load( $item, false, false ); |
| 323 | 351 | } |
| 324 | - // Break reference in foreach iterator. | |
| 325 | - unset( $item ); | |
| 352 | + unset( $item ); // Unset use-by-reference parameter of foreach loop. | |
| 326 | 353 | |
| 327 | 354 | // Maybe sort the items. |
| 328 | 355 | $_sortable_columns = $this->get_sortable_columns(); |
| 329 | 356 | if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) { |