PluginProbe
Posts Table with Search & Sort / 1.0.1
Posts Table with Search & Sort v1.0.1
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 1.3.8 All 39 releases
posts-data-table / posts-data-table.php

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

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