PluginProbe
Posts Table with Search & Sort / 1.0.2
Posts Table with Search & Sort v1.0.2
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
posts-data-table / posts-data-table.php

posts-data-table.php in Posts Table with Search & Sort 1.0.2, at posts-data-table.php

349 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The main plugin file for Posts Table with Search & Sort.
4 *
5 * @package Posts_Data_Table
6 * @author Andrew Keith <andy@barn2.co.uk>
7 * @license GPL-2.0+
8 * @link http://barn2.co.uk
9 * @copyright 2016 Barn2 Media
10 *
11 * @wordpress-plugin
12 * Plugin Name: Posts Table with Search & Sort
13 * Description: This plugin provides a shortcode to show a list of your site's posts in a searchable and sortable table.
14 * Version: 1.0.2
15 * Author: Barn2 Media
16 * Author URI: http://barn2.co.uk
17 * Text Domain: posts-data-table
18 * Domain Path: /languages
19 * License: GPL-2.0+
20 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
21 */
22
23 // Prevent direct file access
24 if ( ! defined ( 'ABSPATH' ) ) {
25 exit;
26 }
27
28 class Posts_Data_Table_Plugin {
29
30 private $shortcode = 'posts_data_table';
31
32 private $shortcode_defaults = array(
33 'columns' => 'title,content,date,author,category',
34 'rows_per_page' => 20,
35 'sort_by' => 'date',
36 'sort_order' => '',
37 'category' => '',
38 'search_on_click' => true,
39 'wrap' => true,
40 'content_length' => 15,
41 'scroll_offset' => 15
42 );
43
44 private $table_count = 1;
45
46 public function __construct() {
47
48 // Load the text domain - should go on 'plugins_loaded' hook
49 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
50
51 // Register shortcode
52 add_shortcode( $this->shortcode, array( $this, 'shortcode' ) );
53
54 // Register styles and scripts
55 add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ) );
56 add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
57 }
58
59 public function load_textdomain() {
60 load_plugin_textdomain( 'posts-data-table', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
61 }
62
63 public function register_styles() {
64 wp_enqueue_style( 'posts-data-table', plugins_url( 'assets/css/posts-data-table.min.css', __FILE__ ) );
65 //wp_enqueue_style( 'data-tables-plugin', plugins_url( 'assets/css/datatables.css', __FILE__ ) );
66 //wp_enqueue_style( 'posts-data-table', plugins_url( 'assets/css/posts-data-table.css', __FILE__ ), array( 'data-tables-plugin' ) );
67 }
68
69 public function register_scripts() {
70 wp_enqueue_script( 'posts-data-table', plugins_url( 'assets/js/posts-data-table.min.js', __FILE__ ), array( 'jquery' ), false, true );
71 //wp_enqueue_script( 'data-tables-plugin', plugins_url( 'assets/js/datatables.js', __FILE__ ), array( 'jquery' ), false, true );
72 //wp_enqueue_script( 'posts-data-table', plugins_url( 'assets/js/posts-data-table.js', __FILE__ ), array( 'data-tables-plugin' ), false, true );
73
74 $locale = get_locale();
75 $supported_locales = $this->get_supported_locales();
76
77 // Add language file to script if locale is supported (English file is not added as this is the default language)
78 if ( array_key_exists( $locale, $supported_locales ) ) {
79 wp_localize_script( 'pdt-script', 'posts_data_table', array(
80 'langurl' => $supported_locales[$locale]
81 ) );
82 }
83 }
84
85 /**
86 * Handles our posts data table shortcode.
87 *
88 * @param array $atts The attributes passed in to the shortcode
89 * @param string $content The content passed to the shortcode (not used)
90 * @return string The shortcode output
91 */
92 public function shortcode( $atts, $content = '' ) {
93 $atts = shortcode_atts( $this->shortcode_defaults, $atts, $this->shortcode );
94 return $this->get_posts_data_table( $atts );
95 }
96
97 /**
98 * Returns an array of locales supported by the plugin.
99 * The array returned uses the locale as the array key mapped to the URL of the corresponding translation file.
100 *
101 * @return array The supported locales
102 */
103 private function get_supported_locales() {
104 $lang_file_base_url = plugins_url( 'languages/data-tables/', __FILE__ );
105
106 return array(
107 'es_ES' => $lang_file_base_url . 'Spanish.json',
108 'fr_FR' => $lang_file_base_url . 'French.json',
109 'de_DE' => $lang_file_base_url . 'German.json',
110 );
111 }
112
113 /**
114 * Retrieves a data table containing a list of posts based on the specified arguments.
115 *
116 * @param array $args An array of options used to display the posts table
117 * @return string The posts table HTML output
118 */
119 private function get_posts_data_table( $args ) {
120
121 if ( empty( $args['columns'] ) ) {
122 $args['columns'] = $this->shortcode_defaults['columns'];
123 }
124
125 $args['rows_per_page'] = filter_var( $args['rows_per_page'], FILTER_VALIDATE_INT );
126 if ( ($args['rows_per_page'] < 1) || !$args['rows_per_page'] ) {
127 $args['rows_per_page'] = false;
128 }
129
130 if ( !in_array( $args['sort_by'], array('id', 'title', 'category', 'date', 'author', 'content') ) ) {
131 $args['sort_by'] = $this->shortcode_defaults['sort_by'];
132 }
133
134 if ( !in_array( $args['sort_order'], array('asc', 'desc') ) ) {
135 $args['sort_order'] = $this->shortcode_defaults['sort_order'];
136 }
137
138 // Set default sort direction
139 if ( !$args['sort_order'] ) {
140 if ( $args['sort_by'] === 'date' ) {
141 $args['sort_order'] = 'desc';
142 } else {
143 $args['sort_order'] = 'asc';
144 }
145 }
146
147 $args['search_on_click'] = filter_var( $args['search_on_click'], FILTER_VALIDATE_BOOLEAN );
148 $args['wrap'] = filter_var( $args['wrap'], FILTER_VALIDATE_BOOLEAN );
149 $args['content_length'] = filter_var( $args['content_length'], FILTER_VALIDATE_INT );
150 $args['scroll_offset'] = filter_var( $args['scroll_offset'], FILTER_VALIDATE_INT );
151
152 $date_format = 'Y/m/d';
153 $output = $table_head = $table_body = $body_row_fmt = '';
154
155 // Start building the args needed for our posts query
156 $post_args = array(
157 'post_type' => 'post',
158 'posts_per_page' => -1,
159 'post_status' => 'publish',
160 'suppress_filters' => false // Ensure WPML filters run on this query
161 );
162
163 if ( $args['category'] ) {
164 $category = get_category_by_slug( $args['category'] );
165
166 if ( $category ) {
167 $post_args['category_name'] = $category->slug;
168 }
169 }
170
171 // Get all published posts in the current language
172 $all_posts_curr_lang = get_posts( $post_args );
173
174 if ( is_array( $all_posts_curr_lang ) && $all_posts_curr_lang ) { // if we have posts
175
176 /* Define an array of all possible columns.
177 * Priority values are used to determine visiblity at small screen sizes (1 = highest priority, 6 = lowest priority).
178 * Column widths are automatically calculated by DataTables, but can be overridden by applying a filter to this array.
179 */
180 $all_columns = apply_filters( 'posts_data_table_column_defaults', array(
181 'id' => array(
182 'heading' => __('ID', 'posts-data-table'),
183 'priority' => 3,
184 'width' => ''
185 ),
186 'title' => array(
187 'heading' => __('Title', 'posts-data-table'),
188 'priority' => 1,
189 'width' => ''
190 ),
191 'category' => array(
192 'heading' => __('Categories', 'posts-data-table'),
193 'priority' => 6,
194 'width' => ''
195 ),
196 'date' => array(
197 'heading' => __('Date', 'posts-data-table'),
198 'priority' => 2,
199 'width' => ''
200 ),
201 'author' => array(
202 'heading' => __('Author', 'posts-data-table'),
203 'priority' => 4,
204 'width' => ''
205 ),
206 'content' => array(
207 'heading' => __('Content', 'posts-data-table'),
208 'priority' => 5,
209 'width' => ''
210 ),
211 ) );
212
213 $all_column_keys = array_keys( $all_columns );
214
215 // Get the columns to be used in this table
216 $columns = array_map( 'trim', explode( ',', strtolower( $args['columns'] ) ) );
217
218 // If none of the user-specfied columns are valid, use the default columns instead
219 if ( !array_intersect($all_column_keys, $columns) ) {
220 $columns = explode( ',', $this->shortcode_defaults['columns'] );
221 }
222
223 // Build table header
224 $heading_fmt = '<th data-name="%1$s" data-priority="%2$u" data-width="%3$s">%4$s</th>';
225 $cell_fmt = '<td>%s</td>';
226
227 foreach( $columns as $column ) {
228 if ( in_array($column, $all_column_keys) ) { // Double-check column name is valid
229
230 // Add heading to table
231 $table_head .= sprintf( $heading_fmt, $column, $all_columns[$column]['priority'], $all_columns[$column]['width'], $all_columns[$column]['heading'] );
232
233 // Add placeholder to table body format string so that content for this column is included in table
234 $body_row_fmt .= sprintf($cell_fmt, $column);
235 }
236 }
237
238 $sort_column = $args['sort_by'];
239 $sort_index = array_search( $sort_column, $columns );
240
241 if ( $sort_index === false ) {
242 // Sort column is not in list of displayed columns so we'll add it as a hidden column at end of table
243 $table_head .= sprintf( '<th data-name="%1$s" data-visible="false">%2$s</th>', $sort_column, $all_columns[$sort_column]['heading'] );
244
245 // Make sure data for this column is included in table content
246 $body_row_fmt .= sprintf($cell_fmt, $sort_column);
247
248 // Set the sort column index to be this hidden column
249 $sort_index = count($columns);
250 }
251
252 $table_head = sprintf( '<thead><tr>%s</tr></thead>', $table_head );
253 // end table header
254
255 // Build table body
256 $body_row_fmt = '<tr>' . $body_row_fmt . '</tr>';
257
258 // Loop through posts and add a row for each
259 foreach ( (array) $all_posts_curr_lang as $_post ) {
260 setup_postdata( $_post );
261
262 // Format title
263 $title = sprintf( '<a href="%1$s">%2$s</a>', get_permalink($_post), get_the_title( $_post ) );
264
265 // Format author
266 $author = sprintf(
267 '<a href="%1$s" title="%2$s" rel="author">%3$s</a>',
268 esc_url( get_author_posts_url( $_post->post_author ) ),
269 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
270 get_the_author()
271 );
272
273 $all_post_data = array(
274 'id' => $_post->ID,
275 'title' => $title,
276 'category' => get_the_category_list( ', ', '', $_post->ID ),
277 'date' => get_the_date( $date_format, $_post ),
278 'author' => $author,
279 'content' => $this->get_post_content( $args['content_length'] )
280 );
281 $table_body .= str_replace( $all_column_keys, $all_post_data, $body_row_fmt );
282
283 } // foreach post
284
285 wp_reset_postdata();
286
287 $table_body = sprintf( '<tbody>%s</tbody>', $table_body );
288 // end table body
289
290 $paging_attr = 'false';
291 if ( ( $args['rows_per_page'] > 1 ) && ( $args['rows_per_page'] < count($all_posts_curr_lang) ) ) {
292 $paging_attr = 'true';
293 }
294
295 $order_attr = sprintf( '[[%u, "%s"]]', $sort_index, $args['sort_order'] );
296 $offset_attr = ( $args['scroll_offset'] === false ) ? 'false' : $args['scroll_offset'];
297
298 $table_class = 'posts-data-table';
299 if ( !$args['wrap'] ) {
300 $table_class .= ' nowrap';
301 }
302
303 $output = sprintf(
304 '<table '
305 . 'id="posts-table-%1$u" '
306 . 'class="%2$s" '
307 . 'data-page-length="%3$u" '
308 . 'data-paging="%4$s" '
309 . 'data-order=\'%5$s\' '
310 . 'data-click-filter="%6$s" '
311 . 'data-scroll-offset="%7$s" '
312 . 'cellspacing="0" width="100%%">'
313 . '%8$s%9$s' .
314 '</table>',
315 $this->table_count,
316 esc_attr( $table_class ),
317 esc_attr( $args['rows_per_page'] ),
318 esc_attr( $paging_attr ),
319 esc_attr( $order_attr ),
320 ( $args['search_on_click'] ? 'true' : 'false' ),
321 esc_attr( $offset_attr ),
322 $table_head,
323 $table_body
324 );
325
326 $this->table_count++;
327 } // if posts found
328
329 return $output;
330 } // get_comments_list
331
332 /**
333 * Retrieve the post content, truncated to the number of words specified by $num_words.
334 *
335 * Must be called with the Loop or a secondary loop after a call to setup_postdata().
336 *
337 * @param int $num_words The number of words to trim the content to
338 * @return string The (truncated) post content
339 */
340 private function get_post_content( $num_words = 15 ) {
341 $text = get_the_content('');
342 $text = strip_shortcodes( $text );
343 $text = apply_filters( 'the_content', $text );
344 $text = wp_trim_words( $text, $num_words, ' &hellip;' );
345 return $text;
346 }
347
348 } // end class
349 $posts_data_table = new Posts_Data_Table_Plugin();