| 1 |
<?php |
| 2 |
/** |
| 3 |
* Editor Button Thickbox List View |
| 4 |
* |
| 5 |
* @package TablePress |
| 6 |
* @subpackage Views |
| 7 |
* @author Tobias Bäthge |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
// Prohibit direct script loading. |
| 12 |
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 |
|
| 14 |
/** |
| 15 |
* Editor Button Thickbox List View class |
| 16 |
* @package TablePress |
| 17 |
* @subpackage Views |
| 18 |
* @author Tobias Bäthge |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
class TablePress_Editor_Button_Thickbox_View extends TablePress_View { |
| 22 |
|
| 23 |
/** |
| 24 |
* Object for the Editor Button Thickbox List Table. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @var TablePress_Editor_Button_Thickbox_List_Table |
| 28 |
*/ |
| 29 |
protected $wp_list_table; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize the View class. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
// Intentionally left empty, to void code from parent::__construct(). |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Set up the view with data and do things that are specific for this view. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @param string $action Action for this view. |
| 46 |
* @param array $data Data for this view. |
| 47 |
*/ |
| 48 |
public function setup( $action, array $data ) { |
| 49 |
$this->action = $action; |
| 50 |
$this->data = $data; |
| 51 |
|
| 52 |
$this->wp_list_table = new TablePress_Editor_Button_Thickbox_List_Table(); |
| 53 |
$this->wp_list_table->set_items( $this->data['table_ids'] ); |
| 54 |
$this->wp_list_table->prepare_items(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Render the current view. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
*/ |
| 62 |
public function render() { |
| 63 |
_wp_admin_html_begin(); |
| 64 |
|
| 65 |
wp_print_styles( 'colors' ); |
| 66 |
wp_print_scripts( 'jquery' ); |
| 67 |
?> |
| 68 |
<title><?php printf( __( '%1$s ‹ %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></title> |
| 69 |
<style type="text/css"> |
| 70 |
/* Account for .wp-toolbar */ |
| 71 |
html { |
| 72 |
padding-top: 0 !important; |
| 73 |
} |
| 74 |
body { |
| 75 |
margin: 0 0 15px 15px; |
| 76 |
} |
| 77 |
|
| 78 |
#tablepress-page .subtitle { |
| 79 |
float: left; |
| 80 |
padding: 10px 0 0; |
| 81 |
} |
| 82 |
|
| 83 |
/* Fix pagination layout */ |
| 84 |
#tablepress-page .tablenav-pages { |
| 85 |
text-align: left; |
| 86 |
} |
| 87 |
#tablepress-page .tablenav .tablenav-pages a { |
| 88 |
padding: 5px 12px; |
| 89 |
font-size: 16px; |
| 90 |
} |
| 91 |
#tablepress-page .tablenav-pages .pagination-links .paging-input { |
| 92 |
font-size: 16px; |
| 93 |
} |
| 94 |
|
| 95 |
#tablepress-page .tablenav-pages .pagination-links .current-page { |
| 96 |
padding: 4px; |
| 97 |
font-size: 16px; |
| 98 |
} |
| 99 |
|
| 100 |
/* Width and font weight for the columns */ |
| 101 |
.tablepress-editor-button-list thead .column-table_id { |
| 102 |
width: 50px; |
| 103 |
} |
| 104 |
.tablepress-editor-button-list tbody .column-table_id, |
| 105 |
.tablepress-editor-button-list tbody .column-table_name { |
| 106 |
font-weight: bold; |
| 107 |
} |
| 108 |
.tablepress-editor-button-list thead .column-table_action { |
| 109 |
min-width: 150px; |
| 110 |
} |
| 111 |
/* Responsiveness on the All Tables screen */ |
| 112 |
@media screen and (max-width: 782px) { |
| 113 |
.tablepress-editor-button-list .column-table_id { |
| 114 |
display: none !important; |
| 115 |
padding: 3px 8px 3px 35%; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* Shortcode input field */ |
| 120 |
#tablepress-page .table-shortcode-inline { |
| 121 |
background: transparent; |
| 122 |
border: none; |
| 123 |
color: #333333; |
| 124 |
width: 120px; |
| 125 |
margin: 0; |
| 126 |
padding: 0; |
| 127 |
font-weight: bold; |
| 128 |
box-shadow: none; |
| 129 |
text-align: center; |
| 130 |
} |
| 131 |
<?php if ( is_rtl() ) : ?> |
| 132 |
/* RTL CSS */ |
| 133 |
body.rtl { |
| 134 |
margin: 0 15px 15px 0; |
| 135 |
} |
| 136 |
.rtl #tablepress-page .subtitle { |
| 137 |
float: right; |
| 138 |
} |
| 139 |
.rtl #tablepress-page .table-shortcode-inline { |
| 140 |
width: 125px; |
| 141 |
font-size: 13px; |
| 142 |
} |
| 143 |
<?php endif; ?> |
| 144 |
</style> |
| 145 |
</head> |
| 146 |
<body class="wp-admin wp-core-ui js iframe<?php echo is_rtl() ? ' rtl' : ''; ?>"> |
| 147 |
<div id="tablepress-page" class="wrap"> |
| 148 |
<h1><?php printf( __( '%1$s ‹ %2$s', 'tablepress' ), __( 'List of Tables', 'tablepress' ), 'TablePress' ); ?></h1> |
| 149 |
<div id="poststuff"> |
| 150 |
<p> |
| 151 |
<?php _e( 'This is a list of all available tables.', 'tablepress' ); ?> <?php _e( 'You may insert a table into a post or page here.', 'tablepress' ); ?> |
| 152 |
</p><p> |
| 153 |
<?php printf( __( 'Click the “%1$s” button for the desired table to automatically insert the<br />corresponding Shortcode (%2$s) into the editor.', 'tablepress' ), __( 'Insert Shortcode', 'tablepress' ), '<input type="text" class="table-shortcode table-shortcode-inline ltr" value="' . esc_attr( '[' . TablePress::$shortcode . ' id=<ID> /]' ) . '" readonly="readonly" />' ); ?> |
| 154 |
</p> |
| 155 |
<?php |
| 156 |
if ( ! empty( $_GET['s'] ) ) { |
| 157 |
printf( '<span class="subtitle">' . __( 'Search results for “%s”', 'tablepress' ) . '</span>', esc_html( wp_unslash( $_GET['s'] ) ) ); |
| 158 |
} |
| 159 |
?> |
| 160 |
<form method="get" action=""> |
| 161 |
<input type="hidden" name="action" value="tablepress_<?php echo $this->action; ?>" /> |
| 162 |
<?php |
| 163 |
wp_nonce_field( TablePress::nonce( $this->action ), '_wpnonce', false ); |
| 164 |
$this->wp_list_table->search_box( __( 'Search Tables', 'tablepress' ), 'tables_search' ); |
| 165 |
?> |
| 166 |
</form> |
| 167 |
<?php $this->wp_list_table->display(); ?> |
| 168 |
</div> |
| 169 |
</div> |
| 170 |
<script type="text/javascript"> |
| 171 |
jQuery( function( $ ) { |
| 172 |
// Toggle list table rows on small screens |
| 173 |
$( '.tablepress-editor-button-list' ) |
| 174 |
.on( 'click', '.toggle-row', function() { |
| 175 |
$( this ).closest( 'tr' ).toggleClass( 'is-expanded' ); |
| 176 |
}) |
| 177 |
.on( 'click', '.insert-shortcode', function() { |
| 178 |
var win = window.dialogArguments || opener || parent || top; |
| 179 |
win.send_to_editor( $(this).attr( 'title' ) ); |
| 180 |
} ); |
| 181 |
} ); |
| 182 |
</script> |
| 183 |
</body> |
| 184 |
</html> |
| 185 |
<?php |
| 186 |
} |
| 187 |
|
| 188 |
} // class TablePress_Editor_Button_View |
| 189 |
|
| 190 |
/** |
| 191 |
* TablePress Editor Button Thickbox List Table Class |
| 192 |
* @package TablePress |
| 193 |
* @subpackage Views |
| 194 |
* @author Tobias Bäthge |
| 195 |
* @link https://codex.wordpress.org/Class_Reference/WP_List_Table |
| 196 |
* @since 1.0.0 |
| 197 |
*/ |
| 198 |
class TablePress_Editor_Button_Thickbox_List_Table extends WP_List_Table { |
| 199 |
|
| 200 |
/** |
| 201 |
* Number of items of the initial data set (before sort, search, and pagination). |
| 202 |
* |
| 203 |
* @since 1.0.0 |
| 204 |
* @var int |
| 205 |
*/ |
| 206 |
protected $items_count = 0; |
| 207 |
|
| 208 |
/** |
| 209 |
* Initialize the List Table. |
| 210 |
* |
| 211 |
* @since 1.0.0 |
| 212 |
*/ |
| 213 |
public function __construct() { |
| 214 |
parent::__construct( array( |
| 215 |
'singular' => 'tablepress-table', // Singular name of the listed records. |
| 216 |
'plural' => 'tablepress-editor-button-list', // Plural name of the listed records. |
| 217 |
'ajax' => false, // Does this list table support AJAX? |
| 218 |
'screen' => get_current_screen(), // WP_Screen object. |
| 219 |
) ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Set the data items (here: tables) that are to be displayed by the List Tables, and their original count. |
| 224 |
* |
| 225 |
* @since 1.0.0 |
| 226 |
* |
| 227 |
* @param array $items Tables to be displayed in the List Table. |
| 228 |
*/ |
| 229 |
public function set_items( array $items ) { |
| 230 |
$this->items = $items; |
| 231 |
$this->items_count = count( $items ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Check whether the user has permissions for certain AJAX actions. |
| 236 |
* (not used, but must be implemented in this child class) |
| 237 |
* |
| 238 |
* @since 1.0.0 |
| 239 |
* |
| 240 |
* @return bool true (Default value). |
| 241 |
*/ |
| 242 |
public function ajax_user_can() { |
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get a list of columns in this List Table. |
| 248 |
* |
| 249 |
* Format: 'internal-name' => 'Column Title'. |
| 250 |
* |
| 251 |
* @since 1.0.0 |
| 252 |
* |
| 253 |
* @return array List of columns in this List Table. |
| 254 |
*/ |
| 255 |
public function get_columns() { |
| 256 |
$columns = array( |
| 257 |
// "name" is special in WP, which is why we prefix every entry here, to be safe! |
| 258 |
'table_id' => __( 'ID', 'tablepress' ), |
| 259 |
'table_name' => __( 'Table Name', 'tablepress' ), |
| 260 |
'table_description' => __( 'Description', 'tablepress' ), |
| 261 |
'table_action' => __( 'Action', 'tablepress' ), |
| 262 |
); |
| 263 |
return $columns; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get a list of columns that are sortable. |
| 268 |
* |
| 269 |
* Format: 'internal-name' => array( $field for $item[ $field ], true for already sorted ). |
| 270 |
* |
| 271 |
* @since 1.0.0 |
| 272 |
* |
| 273 |
* @return array List of sortable columns in this List Table. |
| 274 |
*/ |
| 275 |
protected function get_sortable_columns() { |
| 276 |
// No sorting on the Empty List placeholder. |
| 277 |
if ( ! $this->has_items() ) { |
| 278 |
return array(); |
| 279 |
} |
| 280 |
|
| 281 |
$sortable_columns = array( |
| 282 |
'table_id' => array( 'id', true ), // true means its already sorted |
| 283 |
'table_name' => array( 'name', false ), |
| 284 |
'table_description' => array( 'description', false ), |
| 285 |
); |
| 286 |
return $sortable_columns; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Gets the name of the default primary column. |
| 291 |
* |
| 292 |
* @since 1.7.0 |
| 293 |
* |
| 294 |
* @return string Name of the default primary column, in this case, the table name. |
| 295 |
*/ |
| 296 |
protected function get_default_primary_column_name() { |
| 297 |
return 'table_name'; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Render a cell in the "table_id" column. |
| 302 |
* |
| 303 |
* @since 1.0.0 |
| 304 |
* |
| 305 |
* @param array $item Data item for the current row. |
| 306 |
* @return string HTML content of the cell. |
| 307 |
*/ |
| 308 |
protected function column_table_id( array $item ) { |
| 309 |
return esc_html( $item['id'] ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Render a cell in the "table_name" column. |
| 314 |
* |
| 315 |
* @since 1.0.0 |
| 316 |
* |
| 317 |
* @param array $item Data item for the current row. |
| 318 |
* @return string HTML content of the cell. |
| 319 |
*/ |
| 320 |
protected function column_table_name( array $item ) { |
| 321 |
if ( '' === trim( $item['name'] ) ) { |
| 322 |
$item['name'] = __( '(no name)', 'tablepress' ); |
| 323 |
} |
| 324 |
return esc_html( $item['name'] ); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Render a cell in the "table_description" column. |
| 329 |
* |
| 330 |
* @since 1.0.0 |
| 331 |
* |
| 332 |
* @param array $item Data item for the current row. |
| 333 |
* @return string HTML content of the cell. |
| 334 |
*/ |
| 335 |
protected function column_table_description( array $item ) { |
| 336 |
if ( '' === trim( $item['description'] ) ) { |
| 337 |
$item['description'] = __( '(no description)', 'tablepress' ); |
| 338 |
} |
| 339 |
return esc_html( $item['description'] ); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Render a cell in the "table_action" column, i.e. the "Insert" link. |
| 344 |
* |
| 345 |
* @since 1.0.0 |
| 346 |
* |
| 347 |
* @param array $item Data item for the current row. |
| 348 |
* @return string HTML content of the cell. |
| 349 |
*/ |
| 350 |
protected function column_table_action( array $item ) { |
| 351 |
return '<input type="button" class="insert-shortcode button" title="' . esc_attr( '[' . TablePress::$shortcode . " id={$item['id']} /]" ) . '" value="' . esc_attr__( 'Insert Shortcode', 'tablepress' ) . '" />'; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Holds the message to be displayed when there are no items in the table. |
| 356 |
* |
| 357 |
* @since 1.0.0 |
| 358 |
*/ |
| 359 |
public function no_items() { |
| 360 |
_e( 'No tables found.', 'tablepress' ); |
| 361 |
if ( 0 === $this->items_count ) { |
| 362 |
echo ' ' . __( 'You should add or import a table on the TablePress screens to get started!', 'tablepress' ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Generate the elements above or below the table (like bulk actions and pagination). |
| 368 |
* |
| 369 |
* In comparison with parent class, this has modified HTML (no nonce field), and a check whether there are items. |
| 370 |
* |
| 371 |
* @since 1.0.0 |
| 372 |
* |
| 373 |
* @param string $which Location ("top" or "bottom"). |
| 374 |
*/ |
| 375 |
protected function display_tablenav( $which ) { |
| 376 |
if ( ! $this->has_items() ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
?> |
| 380 |
<div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 381 |
<div class="alignleft actions"> |
| 382 |
<?php $this->bulk_actions( $which ); ?> |
| 383 |
</div> |
| 384 |
<?php |
| 385 |
$this->extra_tablenav( $which ); |
| 386 |
$this->pagination( $which ); |
| 387 |
?> |
| 388 |
<br class="clear" /> |
| 389 |
</div> |
| 390 |
<?php |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Callback to determine whether the given $item contains the search term. |
| 395 |
* |
| 396 |
* @since 1.0.0 |
| 397 |
* |
| 398 |
* @param string $item Table ID that shall be searched. |
| 399 |
* @return bool Whether the search term was found or not. |
| 400 |
*/ |
| 401 |
protected function _search_callback( $item ) { |
| 402 |
static $term, $json_encoded_term; |
| 403 |
if ( is_null( $term ) || is_null( $json_encoded_term ) ) { |
| 404 |
$term = wp_unslash( $_GET['s'] ); |
| 405 |
$json_encoded_term = substr( wp_json_encode( $term, TABLEPRESS_JSON_OPTIONS ), 1, -1 ); |
| 406 |
} |
| 407 |
|
| 408 |
// Load table again, with table data, but without options and visibility settings. |
| 409 |
$item = TablePress::$model_table->load( $item, true, false ); |
| 410 |
|
| 411 |
if ( isset( $item['is_corrupted'] ) && $item['is_corrupted'] ) { |
| 412 |
return false; // Don't search corrupted tables |
| 413 |
} |
| 414 |
|
| 415 |
// Search from easy to hard, so that "expensive" code maybe doesn't have to run. |
| 416 |
if ( false !== stripos( $item['id'], $term ) |
| 417 |
|| false !== stripos( $item['name'], $term ) |
| 418 |
|| false !== stripos( $item['description'], $term ) |
| 419 |
|| false !== stripos( TablePress::get_user_display_name( $item['author'] ), $term ) |
| 420 |
|| false !== stripos( TablePress::format_datetime( $item['last_modified'] ), $term ) |
| 421 |
|| false !== stripos( wp_json_encode( $item['data'], TABLEPRESS_JSON_OPTIONS ), $json_encoded_term ) ) { |
| 422 |
return true; |
| 423 |
} |
| 424 |
|
| 425 |
return false; |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Callback to for the array sort function. |
| 430 |
* |
| 431 |
* @since 1.0.0 |
| 432 |
* |
| 433 |
* @param array $item_a First item that shall be compared to. |
| 434 |
* @param array $item_b The second item for the comparison. |
| 435 |
* @return int (-1, 0, 1) depending on which item sorts "higher". |
| 436 |
*/ |
| 437 |
protected function _order_callback( array $item_a, array $item_b ) { |
| 438 |
global $orderby, $order; |
| 439 |
|
| 440 |
if ( $item_a[ $orderby ] === $item_b[ $orderby ] ) { |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
// Fields in this list table are all strings. |
| 445 |
$result = strnatcasecmp( $item_a[ $orderby ], $item_b[ $orderby ] ); |
| 446 |
|
| 447 |
return ( 'asc' === $order ) ? $result : - $result; |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Prepares the list of items for displaying, by maybe searching and sorting, and by doing pagination. |
| 452 |
* |
| 453 |
* @since 1.0.0 |
| 454 |
*/ |
| 455 |
public function prepare_items() { |
| 456 |
global $orderby, $order, $s; |
| 457 |
wp_reset_vars( array( 'orderby', 'order', 's' ) ); |
| 458 |
|
| 459 |
// Maybe search in the items. |
| 460 |
if ( $s ) { |
| 461 |
$this->items = array_filter( $this->items, array( $this, '_search_callback' ) ); |
| 462 |
} |
| 463 |
|
| 464 |
// Load actual tables after search for less memory consumption. |
| 465 |
foreach ( $this->items as &$item ) { |
| 466 |
// Don't load data nor table options. |
| 467 |
$item = TablePress::$model_table->load( $item, false, false ); |
| 468 |
} |
| 469 |
// Break reference in foreach iterator. |
| 470 |
unset( $item ); |
| 471 |
|
| 472 |
// Maybe sort the items. |
| 473 |
$_sortable_columns = $this->get_sortable_columns(); |
| 474 |
if ( $orderby && ! empty( $this->items ) && isset( $_sortable_columns[ "table_{$orderby}" ] ) ) { |
| 475 |
usort( $this->items, array( $this, '_order_callback' ) ); |
| 476 |
} |
| 477 |
|
| 478 |
// Number of records to show per page. |
| 479 |
$per_page = 15; // hard-coded, as there's no possibility to change this in the Thickbox |
| 480 |
// Page number the user is currently viewing. |
| 481 |
$current_page = $this->get_pagenum(); |
| 482 |
// Number of records in the array. |
| 483 |
$total_items = count( $this->items ); |
| 484 |
|
| 485 |
// Slice items array to hold only items for the current page. |
| 486 |
$this->items = array_slice( $this->items, ( ( $current_page - 1 ) * $per_page ), $per_page ); |
| 487 |
|
| 488 |
// Register pagination options and calculation results. |
| 489 |
$this->set_pagination_args( array( |
| 490 |
'total_items' => $total_items, // Total number of records/items |
| 491 |
'per_page' => $per_page, // Number of items per page |
| 492 |
'total_pages' => ceil( $total_items / $per_page ), // Total number of pages |
| 493 |
) ); |
| 494 |
} |
| 495 |
|
| 496 |
} // class TablePress_Editor_Button_Thickbox_List_Table |
| 497 |
|