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