* @license GPL-3.0 * @copyright Barn2 Media Ltd */ class Simple_Posts_Table { /** * Stores the number of tables on this page. Used to generate the table ID. * * @var int */ private static $table_count = 1; /** * The complete list of table attributes, and their defaults. * * @var array */ public static $default_args = array( 'columns' => 'title,content,date,author,category', 'rows_per_page' => 20, 'sort_by' => 'date', 'sort_order' => '', 'category' => '', 'tag' => '', 'author' => '', 'post_status' => '', 'date_format' => 'Y/m/d', 'search_on_click' => true, 'wrap' => true, 'content_length' => 15, 'scroll_offset' => 15 ); /** * An array of all possible columns and their default heading, priority, and column width. * * @var array */ private static $column_defaults = array(); /** * An array of all allowed column keys. * * @var array */ private static $allowed_columns = array(); public static function get_defaults() { return \wp_parse_args( Settings::get_table_args(), self::$default_args ); } public static function get_column_defaults() { if ( empty( self::$column_defaults ) ) { /** * Priority values are used to determine visiblity at small screen sizes (1 = highest priority, 6 = lowest priority). * Column widths are automatically calculated by DataTables, but can be overridden by using filter 'posts_data_table_column_defaults'. */ self::$column_defaults = array( 'id' => array( 'heading' => __( 'ID', 'posts-data-table' ), 'priority' => 3, 'width' => '' ), 'title' => array( 'heading' => __( 'Title', 'posts-data-table' ), 'priority' => 1, 'width' => '' ), 'category' => array( 'heading' => __( 'Categories', 'posts-data-table' ), 'priority' => 6, 'width' => '' ), 'tags' => array( 'heading' => __( 'Tags', 'posts-data-table' ), 'priority' => 7, 'width' => '' ), 'date' => array( 'heading' => __( 'Date', 'posts-data-table' ), 'priority' => 2, 'width' => '' ), 'author' => array( 'heading' => __( 'Author', 'posts-data-table' ), 'priority' => 4, 'width' => '' ), 'content' => array( 'heading' => __( 'Content', 'posts-data-table' ), 'priority' => 5, 'width' => '' ) ); } return self::$column_defaults; } public static function get_allowed_columns() { if ( empty( self::$allowed_columns ) ) { self::$allowed_columns = \array_keys( self::get_column_defaults() ); } return self::$allowed_columns; } /** * Retrieves a data table containing a list of posts based on the specified arguments. * * @param array $args An array of options used to display the posts table * @return string The posts table HTML output */ public function get_table( $args ) { $args = \wp_parse_args( $args, self::get_defaults() ); if ( empty( $args['columns'] ) ) { $args['columns'] = self::$default_args['columns']; } $args['rows_per_page'] = \filter_var( $args['rows_per_page'], \FILTER_VALIDATE_INT ); if ( $args['rows_per_page'] < 1 || ! $args['rows_per_page'] ) { $args['rows_per_page'] = false; } if ( ! \in_array( $args['sort_by'], self::get_allowed_columns() ) ) { $args['sort_by'] = self::$default_args['sort_by']; } if ( ! \in_array( $args['sort_order'], array( 'asc', 'desc' ) ) ) { $args['sort_order'] = self::$default_args['sort_order']; } // Set default sort direction if ( ! $args['sort_order'] ) { if ( $args['sort_by'] === 'date' ) { $args['sort_order'] = 'desc'; } else { $args['sort_order'] = 'asc'; } } $args['search_on_click'] = filter_var( $args['search_on_click'], \FILTER_VALIDATE_BOOLEAN ); $args['wrap'] = filter_var( $args['wrap'], \FILTER_VALIDATE_BOOLEAN ); $args['content_length'] = filter_var( $args['content_length'], \FILTER_VALIDATE_INT ); $args['scroll_offset'] = filter_var( $args['scroll_offset'], \FILTER_VALIDATE_INT ); if ( empty( $args['date_format'] ) ) { $args['date_format'] = self::$default_args['date_format']; } $output = $table_head = $table_body = $body_row_fmt = ''; // Start building the args needed for our posts query $post_args = array( 'post_type' => 'post', 'posts_per_page' => \apply_filters( 'posts_data_table_post_limit', 1000 ), 'post_status' => 'publish', 'suppress_filters' => false // Ensure WPML filters run on this query ); if ( ! empty( $args['category'] ) ) { if ( \is_numeric( $args['category'] ) ) { $post_args['cat'] = $args['category']; } else { $category = \get_category_by_slug( $args['category'] ); if ( $category ) { $post_args['category_name'] = $category->slug; } } } if ( ! empty( $args['tag'] ) ) { if ( \is_numeric( $args['tag'] ) ) { $post_args['tag_id'] = $args['tag']; } else { $post_args['tag'] = $args['tag']; } } if ( ! empty( $args['author'] ) ) { $author = $args['author']; if ( \is_numeric( $author ) || false !== \strpos( $author, ',' ) ) { $post_args['author'] = $author; } else { $post_args['author_name'] = $author; } } if ( ! empty( $args['post_status'] ) ) { $post_args['post_status'] = $args['post_status']; } // Get all published posts in the current language $all_posts = \get_posts( \apply_filters( 'posts_data_table_query_args', $post_args, $args ) ); // Bail early if no posts found if ( ! $all_posts || ! \is_array( $all_posts ) ) { return $output; } // Allow theme/plugins to override defaults $column_defaults = \apply_filters( 'posts_data_table_column_defaults_' . self::$table_count, \apply_filters( 'posts_data_table_column_defaults', self::get_column_defaults() ) ); // Get the columns to be used in this table $columns = \array_filter( \array_map( 'trim', \explode( ',', \strtolower( $args['columns'] ) ) ) ); $hidden_columns = array(); // If none of the user-specfied columns are valid, use the default columns instead if ( ! \array_intersect( self::get_allowed_columns(), $columns ) ) { $columns = \explode( ',', self::$default_args['columns'] ); } // Set hidden columns and sort indexes $table_sort_index = \array_search( $args['sort_by'], $columns ); $date_sort_index = false; $hidden_date = \in_array( 'date', $columns ) || 'date' === $args['sort_by']; if ( $hidden_date ) { $hidden_columns[] = 'timestamp'; $date_sort_index = \count( $columns ); // If we're sorting by date, make sure we use this hidden column for the initial table sort if ( 'date' === $args['sort_by'] && false === $table_sort_index ) { $table_sort_index = $date_sort_index; } } if ( false === $table_sort_index ) { // Sort column is not in list of displayed columns so we'll add it as a hidden column at end of table $hidden_columns[] = $args['sort_by']; // Set the table sort index to the index of the hidden sort column $table_sort_index = $date_sort_index ? $date_sort_index + 1 : \count( $columns ); } // Build table header $heading_fmt = '