| @@ -125,14 +125,8 @@ | ||
| 125 | 125 | * @param array $args An array of options used to display the posts table |
| 126 | 126 | * @return string The posts table HTML output |
| 127 | 127 | */ |
| 128 | 128 | public function get_table( $args ) { |
| 129 | - // Load the scripts and styles. | |
| 130 | - if ( apply_filters( 'posts_data_table_load_scripts', true ) ) { | |
| 131 | - wp_enqueue_style( 'posts-data-table' ); | |
| 132 | - wp_enqueue_script( 'posts-data-table' ); | |
| 133 | - } | |
| 134 | - | |
| 135 | 129 | $args = wp_parse_args( $args, self::get_defaults() ); |
| 136 | 130 | $args = $this->back_compat_args( $args ); |
| 137 | 131 | |
| 138 | 132 | if ( empty( $args['columns'] ) ) { |
| @@ -149,9 +143,9 @@ | ||
| 149 | 143 | |
| 150 | 144 | $args['rows_per_page'] = filter_var( $args['rows_per_page'], FILTER_VALIDATE_INT ); |
| 151 | 145 | |
| 152 | 146 | if ( $args['rows_per_page'] < 1 || ! $args['rows_per_page'] ) { |
| 153 | - $args['rows_per_page'] = false; | |
| 147 | + $args['rows_per_page'] = -1; | |
| 154 | 148 | } |
| 155 | 149 | |
| 156 | 150 | if ( ! in_array( $args['sort_by'], self::get_allowed_columns() ) ) { |
| 157 | 151 | $args['sort_by'] = self::$default_args['sort_by']; |
| @@ -261,8 +255,26 @@ | ||
| 261 | 255 | |
| 262 | 256 | // Allow theme/plugins to override defaults |
| 263 | 257 | $column_defaults = apply_filters( 'posts_data_table_column_defaults_' . self::$table_count, apply_filters( 'posts_data_table_column_defaults', self::get_column_defaults() ) ); |
| 264 | 258 | |
| 259 | + // Load the scripts and styles. | |
| 260 | + $load_scripts = apply_filters( 'posts_data_table_load_scripts', true ); | |
| 261 | + | |
| 262 | + if ( $load_scripts ) { | |
| 263 | + wp_enqueue_style( 'posts-data-table' ); | |
| 264 | + | |
| 265 | + wp_localize_script( | |
| 266 | + 'posts-data-table', | |
| 267 | + 'configOptions', | |
| 268 | + [ | |
| 269 | + 'lengthMenu' => json_encode( $this->get_page_lengths( $args['rows_per_page'] ) ), | |
| 270 | + 'displayLength' => $args['rows_per_page'] | |
| 271 | + ] | |
| 272 | + ); | |
| 273 | + | |
| 274 | + wp_enqueue_script( 'posts-data-table' ); | |
| 275 | + } | |
| 276 | + | |
| 265 | 277 | // Build table header |
| 266 | 278 | $heading_fmt = '<th data-name="%1$s" data-priority="%2$u" data-width="%3$s"%5$s>%4$s</th>'; |
| 267 | 279 | $cell_fmt = '<td>{%s}</td>'; |
| 268 | 280 | |
| @@ -288,10 +300,11 @@ | ||
| 288 | 300 | |
| 289 | 301 | foreach ( $hidden_columns as $column ) { |
| 290 | 302 | $table_head .= sprintf( '<th data-name="%s" data-visible="false"></th>', $column ); |
| 291 | 303 | |
| 292 | - // Make sure data for the hidden column is included in table content | |
| 293 | - $body_row_fmt .= sprintf( $cell_fmt, $column ); | |
| 304 | + // Make sure data for the hidden column is included in table content. | |
| 305 | + // The 'pdt-hidden-column' class hides it via CSS so it never briefly flashes on screen while DataTables initialises. | |
| 306 | + $body_row_fmt .= sprintf( '<td class="pdt-hidden-column">{%s}</td>', $column ); | |
| 294 | 307 | } |
| 295 | 308 | |
| 296 | 309 | $table_head = sprintf( '<thead><tr>%s</tr></thead>', $table_head ); |
| 297 | 310 | |
| @@ -302,9 +315,9 @@ | ||
| 302 | 315 | foreach ( (array) $all_posts as $_post ) { |
| 303 | 316 | setup_postdata( $_post ); |
| 304 | 317 | |
| 305 | 318 | // Format title |
| 306 | - $title = sprintf( '<a href="%1$s">%2$s</a>', get_permalink( $_post ), get_the_title( $_post ) ); | |
| 319 | + $title = sprintf( '<a href="%1$s">%2$s</a>', esc_url( get_permalink( $_post ) ), esc_html( get_the_title( $_post ) ) ); | |
| 307 | 320 | |
| 308 | 321 | // Format author |
| 309 | 322 | $author = sprintf( |
| 310 | 323 | '<a href="%1$s" title="%2$s" rel="author">%3$s</a>', |
| @@ -310,9 +323,9 @@ | ||
| 310 | 323 | '<a href="%1$s" title="%2$s" rel="author">%3$s</a>', |
| 311 | 324 | esc_url( get_author_posts_url( $_post->post_author ) ), |
| 312 | 325 | /* translators: %s: the author's name */ |
| 313 | 326 | esc_attr( sprintf( __( 'Posts by %s', 'posts-data-table' ), get_the_author() ) ), |
| 314 | - get_the_author() | |
| 327 | + esc_html( get_the_author() ) | |
| 315 | 328 | ); |
| 316 | 329 | |
| 317 | 330 | $post_data_trans = apply_filters( |
| 318 | 331 | 'posts_data_table_row_data_format', |
| @@ -335,14 +348,10 @@ | ||
| 335 | 348 | wp_reset_postdata(); |
| 336 | 349 | |
| 337 | 350 | $table_body = sprintf( '<tbody>%s</tbody>', $table_body ); |
| 338 | 351 | |
| 339 | - $paging_attr = 'false'; | |
| 352 | + $paging_attr = 'true'; | |
| 340 | 353 | |
| 341 | - if ( $args['rows_per_page'] && $args['rows_per_page'] < count( $all_posts ) ) { | |
| 342 | - $paging_attr = 'true'; | |
| 343 | - } | |
| 344 | - | |
| 345 | 354 | $order_attr = ''; |
| 346 | 355 | |
| 347 | 356 | if ( false !== $table_sort_index ) { |
| 348 | 357 | // Order attribute should be escaped here rather than in sprintf below as we don't want to escape the double-quotes around "asc" or "desc" |
| @@ -355,8 +364,13 @@ | ||
| 355 | 364 | if ( ! $args['wrap'] ) { |
| 356 | 365 | $table_class .= ' nowrap'; |
| 357 | 366 | } |
| 358 | 367 | |
| 368 | + // Keep the raw table hidden until the DataTables JS has initialised, so it never briefly flashes unstyled on screen. | |
| 369 | + if ( $load_scripts ) { | |
| 370 | + $table_class .= ' pdt-loading'; | |
| 371 | + } | |
| 372 | + | |
| 359 | 373 | $table_attributes = sprintf( |
| 360 | 374 | 'id="posts-table-%1$u" class="%2$s" data-page-length="%3$u" data-paging="%4$s" data-order=\'%5$s\' data-click-filter="%6$s" data-scroll-offset="%7$s" cellspacing="0" width="100%%"', |
| 361 | 375 | self::$table_count, |
| 362 | 376 | esc_attr( $table_class ), |
| @@ -410,7 +424,24 @@ | ||
| 410 | 424 | $text = wp_trim_words( $text, $num_words, ' …' ); |
| 411 | 425 | } |
| 412 | 426 | |
| 413 | 427 | return $text; |
| 428 | + } | |
| 429 | + | |
| 430 | + public function get_page_lengths( $default_length ) { | |
| 431 | + $length_numbers = apply_filters( 'posts_data_table_default_page_lengths', [ 10, 25, 50, 100 ] ); | |
| 432 | + | |
| 433 | + if ( $default_length != -1 && !in_array( $default_length, $length_numbers ) ) { | |
| 434 | + $length_numbers[] = $default_length; | |
| 435 | + } | |
| 436 | + | |
| 437 | + sort( $length_numbers ); | |
| 438 | + | |
| 439 | + $lengths = [ | |
| 440 | + array_merge( $length_numbers, [ -1 ] ), | |
| 441 | + array_merge( $length_numbers, [ __( 'All', 'posts-data-table' ) ] ) | |
| 442 | + ]; | |
| 443 | + | |
| 444 | + return $lengths; | |
| 414 | 445 | } |
| 415 | 446 | |
| 416 | 447 | } |