| @@ -1,357 +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.3 | |
| 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.13 | |
| 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 | - | |
| 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 ) { | |
| 35 | +const PLUGIN_VERSION = '1.4.13'; | |
| 36 | +const PLUGIN_FILE = __FILE__; | |
| 120 | 37 | |
| 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 ); | |
| 38 | +// Autoloader. | |
| 39 | +require_once __DIR__ . '/vendor/autoload.php'; | |
| 173 | 40 | |
| 174 | - if ( is_array( $all_posts_curr_lang ) && $all_posts_curr_lang ) { // if we have posts | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Define an array of all possible columns and their default heading, priority, and column width. | |
| 178 | - * | |
| 179 | - * Priority values are used to determine visiblity at small screen sizes (1 = highest priority, 6 = lowest priority). | |
| 180 | - * Column widths are automatically calculated by DataTables, but can be overridden by using filter 'posts_data_table_column_defaults'. | |
| 181 | - */ | |
| 182 | - $column_defaults = array( | |
| 183 | - 'id' => array( | |
| 184 | - 'heading' => __('ID', 'posts-data-table'), | |
| 185 | - 'priority' => 3, | |
| 186 | - 'width' => '' | |
| 187 | - ), | |
| 188 | - 'title' => array( | |
| 189 | - 'heading' => __('Title', 'posts-data-table'), | |
| 190 | - 'priority' => 1, | |
| 191 | - 'width' => '' | |
| 192 | - ), | |
| 193 | - 'category' => array( | |
| 194 | - 'heading' => __('Categories', 'posts-data-table'), | |
| 195 | - 'priority' => 6, | |
| 196 | - 'width' => '' | |
| 197 | - ), | |
| 198 | - 'date' => array( | |
| 199 | - 'heading' => __('Date', 'posts-data-table'), | |
| 200 | - 'priority' => 2, | |
| 201 | - 'width' => '' | |
| 202 | - ), | |
| 203 | - 'author' => array( | |
| 204 | - 'heading' => __('Author', 'posts-data-table'), | |
| 205 | - 'priority' => 4, | |
| 206 | - 'width' => '' | |
| 207 | - ), | |
| 208 | - 'content' => array( | |
| 209 | - 'heading' => __('Content', 'posts-data-table'), | |
| 210 | - 'priority' => 5, | |
| 211 | - 'width' => '' | |
| 212 | - ), | |
| 213 | - ); | |
| 214 | - | |
| 215 | - $all_columns = array_keys( $column_defaults ); | |
| 216 | - | |
| 217 | - // Allow users to override defaults | |
| 218 | - //@todo Allow this filter to be used per table | |
| 219 | - $column_defaults = apply_filters( 'posts_data_table_column_defaults', $column_defaults ); | |
| 220 | - | |
| 221 | - // Get the columns to be used in this table | |
| 222 | - $columns = array_map( 'trim', explode( ',', strtolower( $args['columns'] ) ) ); | |
| 223 | - | |
| 224 | - // If none of the user-specfied columns are valid, use the default columns instead | |
| 225 | - if ( !array_intersect( $all_columns, $columns ) ) { | |
| 226 | - $columns = explode( ',', $this->shortcode_defaults['columns'] ); | |
| 227 | - } | |
| 228 | - | |
| 229 | - // Build table header | |
| 230 | - $heading_fmt = '<th data-name="%1$s" data-priority="%2$u" data-width="%3$s">%4$s</th>'; | |
| 231 | - $cell_fmt = '<td>%s</td>'; | |
| 232 | - | |
| 233 | - foreach( $columns as $column ) { | |
| 234 | - | |
| 235 | - if ( array_key_exists( $column, $column_defaults ) ) { // Double-check column name is valid | |
| 236 | - | |
| 237 | - // Add heading to table | |
| 238 | - $table_head .= sprintf( $heading_fmt, $column, $column_defaults[$column]['priority'], $column_defaults[$column]['width'], $column_defaults[$column]['heading'] ); | |
| 239 | - | |
| 240 | - // Add placeholder to table body format string so that content for this column is included in table output | |
| 241 | - $body_row_fmt .= sprintf($cell_fmt, '{' . $column . '}'); | |
| 242 | - } | |
| 243 | - } | |
| 244 | - | |
| 245 | - $sort_column = $args['sort_by']; | |
| 246 | - $sort_index = array_search( $sort_column, $columns ); | |
| 247 | - | |
| 248 | - if ( $sort_index === false && array_key_exists( $sort_column, $column_defaults ) ) { | |
| 249 | - // Sort column is not in list of displayed columns so we'll add it as a hidden column at end of table | |
| 250 | - $table_head .= sprintf( '<th data-name="%1$s" data-visible="false">%2$s</th>', $sort_column, $column_defaults[$sort_column]['heading'] ); | |
| 251 | - | |
| 252 | - // Make sure data for this column is included in table content | |
| 253 | - $body_row_fmt .= sprintf($cell_fmt, $sort_column); | |
| 254 | - | |
| 255 | - // Set the sort column index to be this hidden column | |
| 256 | - $sort_index = count($columns); | |
| 257 | - } | |
| 258 | - | |
| 259 | - $table_head = sprintf( '<thead><tr>%s</tr></thead>', $table_head ); | |
| 260 | - // end table header | |
| 261 | - | |
| 262 | - // Build table body | |
| 263 | - $body_row_fmt = '<tr>' . $body_row_fmt . '</tr>'; | |
| 264 | - | |
| 265 | - // Loop through posts and add a row for each | |
| 266 | - foreach ( (array) $all_posts_curr_lang as $_post ) { | |
| 267 | - setup_postdata( $_post ); | |
| 268 | - | |
| 269 | - // Format title | |
| 270 | - $title = sprintf( '<a href="%1$s">%2$s</a>', get_permalink($_post), get_the_title( $_post ) ); | |
| 271 | - | |
| 272 | - // Format author | |
| 273 | - $author = sprintf( | |
| 274 | - '<a href="%1$s" title="%2$s" rel="author">%3$s</a>', | |
| 275 | - esc_url( get_author_posts_url( $_post->post_author ) ), | |
| 276 | - esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ), | |
| 277 | - get_the_author() | |
| 278 | - ); | |
| 279 | - | |
| 280 | - $post_data_trans = array( | |
| 281 | - '{id}' => $_post->ID, | |
| 282 | - '{title}' => $title, | |
| 283 | - '{category}' => get_the_category_list( ', ', '', $_post->ID ), | |
| 284 | - '{date}' => get_the_date( $date_format, $_post ), | |
| 285 | - '{author}' => $author, | |
| 286 | - '{content}' => $this->get_post_content( $args['content_length'] ) | |
| 287 | - ); | |
| 288 | - | |
| 289 | - $table_body .= strtr( $body_row_fmt, $post_data_trans ); | |
| 290 | - | |
| 291 | - } // foreach post | |
| 292 | - | |
| 293 | - wp_reset_postdata(); | |
| 294 | - | |
| 295 | - $table_body = sprintf( '<tbody>%s</tbody>', $table_body ); | |
| 296 | - // end table body | |
| 297 | - | |
| 298 | - $paging_attr = 'false'; | |
| 299 | - if ( ( $args['rows_per_page'] > 1 ) && ( $args['rows_per_page'] < count($all_posts_curr_lang) ) ) { | |
| 300 | - $paging_attr = 'true'; | |
| 301 | - } | |
| 302 | - | |
| 303 | - $order_attr = ( $sort_index === false ) ? '' : sprintf( '[[%u, "%s"]]', $sort_index, $args['sort_order'] ); | |
| 304 | - $offset_attr = ( $args['scroll_offset'] === false ) ? 'false' : $args['scroll_offset']; | |
| 305 | - | |
| 306 | - $table_class = 'posts-data-table'; | |
| 307 | - if ( !$args['wrap'] ) { | |
| 308 | - $table_class .= ' nowrap'; | |
| 309 | - } | |
| 310 | - | |
| 311 | - $output = sprintf( | |
| 312 | - '<table ' | |
| 313 | - . 'id="posts-table-%1$u" ' | |
| 314 | - . 'class="%2$s" ' | |
| 315 | - . 'data-page-length="%3$u" ' | |
| 316 | - . 'data-paging="%4$s" ' | |
| 317 | - . 'data-order=\'%5$s\' ' | |
| 318 | - . 'data-click-filter="%6$s" ' | |
| 319 | - . 'data-scroll-offset="%7$s" ' | |
| 320 | - . 'cellspacing="0" width="100%%">' | |
| 321 | - . '%8$s%9$s' . | |
| 322 | - '</table>', | |
| 323 | - $this->table_count, | |
| 324 | - esc_attr( $table_class ), | |
| 325 | - esc_attr( $args['rows_per_page'] ), | |
| 326 | - esc_attr( $paging_attr ), | |
| 327 | - esc_attr( $order_attr ), | |
| 328 | - ( $args['search_on_click'] ? 'true' : 'false' ), | |
| 329 | - esc_attr( $offset_attr ), | |
| 330 | - $table_head, | |
| 331 | - $table_body | |
| 332 | - ); | |
| 333 | - | |
| 334 | - $this->table_count++; | |
| 335 | - } // 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 | +} | |
| 336 | 49 | |
| 337 | - return $output; | |
| 338 | - } // get_comments_list | |
| 339 | - | |
| 340 | - /** | |
| 341 | - * Retrieve the post content, truncated to the number of words specified by $num_words. | |
| 342 | - * | |
| 343 | - * Must be called with the Loop or a secondary loop after a call to setup_postdata(). | |
| 344 | - * | |
| 345 | - * @param int $num_words The number of words to trim the content to | |
| 346 | - * @return string The (truncated) post content | |
| 347 | - */ | |
| 348 | - private function get_post_content( $num_words = 15 ) { | |
| 349 | - $text = get_the_content(''); | |
| 350 | - $text = strip_shortcodes( $text ); | |
| 351 | - $text = apply_filters( 'the_content', $text ); | |
| 352 | - $text = wp_trim_words( $text, $num_words, ' …' ); | |
| 353 | - return $text; | |
| 354 | - } | |
| 355 | - | |
| 356 | -} // end class | |
| 357 | -$posts_data_table = new Posts_Data_Table_Plugin(); | |
| 50 | +// Load the plugin. | |
| 51 | +posts_table_search_sort()->register(); | |