PluginProbe
Posts Table with Search & Sort / 1.4.11
Posts Table with Search & Sort v1.4.11
1.4.13 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2 1.3 1.3.1 1.3.1-v2 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 40 releases
← All changes | src/Simple_Posts_Table.php +35 -12 1.3.71.4.11 View file →
@@ -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,24 @@
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 + if ( apply_filters( 'posts_data_table_load_scripts', true ) ) {
261 + wp_enqueue_style( 'posts-data-table' );
262 +
263 + wp_localize_script(
264 + 'posts-data-table',
265 + 'configOptions',
266 + [
267 + 'lengthMenu' => json_encode( $this->get_page_lengths( $args['rows_per_page'] ) ),
268 + 'displayLength' => $args['rows_per_page']
269 + ]
270 + );
271 +
272 + wp_enqueue_script( 'posts-data-table' );
273 + }
274 +
265 275 // Build table header
266 276 $heading_fmt = '<th data-name="%1$s" data-priority="%2$u" data-width="%3$s"%5$s>%4$s</th>';
267 277 $cell_fmt = '<td>{%s}</td>';
268 278
@@ -335,14 +345,10 @@
335 345 wp_reset_postdata();
336 346
337 347 $table_body = sprintf( '<tbody>%s</tbody>', $table_body );
338 348
339 - $paging_attr = 'false';
349 + $paging_attr = 'true';
340 350
341 - if ( $args['rows_per_page'] && $args['rows_per_page'] < count( $all_posts ) ) {
342 - $paging_attr = 'true';
343 - }
344 -
345 351 $order_attr = '';
346 352
347 353 if ( false !== $table_sort_index ) {
348 354 // 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"
@@ -410,7 +416,24 @@
410 416 $text = wp_trim_words( $text, $num_words, ' &hellip;' );
411 417 }
412 418
413 419 return $text;
420 + }
421 +
422 + public function get_page_lengths( $default_length ) {
423 + $length_numbers = apply_filters( 'posts_data_table_default_page_lengths', [ 10, 25, 50, 100 ] );
424 +
425 + if ( $default_length != -1 && !in_array( $default_length, $length_numbers ) ) {
426 + $length_numbers[] = $default_length;
427 + }
428 +
429 + sort( $length_numbers );
430 +
431 + $lengths = [
432 + array_merge( $length_numbers, [ -1 ] ),
433 + array_merge( $length_numbers, [ __( 'All', 'posts-data-table' ) ] )
434 + ];
435 +
436 + return $lengths;
414 437 }
415 438
416 439 }