| 1 |
<?php |
| 2 |
/** |
| 3 |
* Frontend Controller for TablePress with functionality for the frontend |
| 4 |
* |
| 5 |
* @package TablePress |
| 6 |
* @subpackage Controllers |
| 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 |
* Frontend Controller class, extends Base Controller Class |
| 16 |
* |
| 17 |
* @package TablePress |
| 18 |
* @subpackage Controllers |
| 19 |
* @author Tobias Bäthge |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class TablePress_Frontend_Controller extends TablePress_Controller { |
| 23 |
|
| 24 |
/** |
| 25 |
* Whether to use the legacy CSS loading method of enqueuing all CSS files on all pages. |
| 26 |
* |
| 27 |
* @since 3.0.1 |
| 28 |
*/ |
| 29 |
public bool $use_legacy_css_loading = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* File name of the admin screens' parent page in the admin menu. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public string $parent_page = 'middle'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Whether TablePress admin screens are a top-level menu item in the admin menu. |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public bool $is_top_level_page = false; |
| 44 |
|
| 45 |
/** |
| 46 |
* List of tables that are shown for the current request. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* @var array<string, array{count: int, instances: array<string, array<string, mixed>>}> |
| 50 |
*/ |
| 51 |
protected array $shown_tables = array(); |
| 52 |
|
| 53 |
/** |
| 54 |
* List of registered DataTables datetime formats. |
| 55 |
* |
| 56 |
* @since 3.0.0 |
| 57 |
* @var string[] |
| 58 |
*/ |
| 59 |
protected array $datatables_datetime_formats = array(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Initiates Frontend functionality. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
parent::__construct(); |
| 68 |
|
| 69 |
/** |
| 70 |
* Filters the admin menu parent page, which is needed for the construction of plugin URLs. |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
* |
| 74 |
* @param string $parent_page Current admin menu parent page. |
| 75 |
*/ |
| 76 |
$this->parent_page = apply_filters( 'tablepress_admin_menu_parent_page', TablePress::$model_options->get( 'admin_menu_parent_page' ) ); |
| 77 |
$this->is_top_level_page = in_array( $this->parent_page, array( 'top', 'middle', 'bottom' ), true ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Filters whether TablePress should load its frontend CSS files on all pages. |
| 81 |
* For block themes, the default behavior is to only load the CSS files when a table is encountered on the page. |
| 82 |
* If Elementor is active, the CSS is also loaded on the editor page. |
| 83 |
* |
| 84 |
* @since 3.0.1 |
| 85 |
* |
| 86 |
* @param bool $use_legacy_css_loading Whether TablePress should load its frontend CSS files on all pages. |
| 87 |
*/ |
| 88 |
$this->use_legacy_css_loading = apply_filters( 'tablepress_frontend_legacy_css_loading', ! wp_is_block_theme() || ( isset( $_GET['elementor-preview'] ) && is_plugin_active( 'elementor/elementor.php' ) ) ); |
| 89 |
|
| 90 |
if ( $this->use_legacy_css_loading ) { |
| 91 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_css' ) ); |
| 92 |
} |
| 93 |
|
| 94 |
// Register a placeholder script handle so that plugins can properly declare it as a dependency, even before the actual script is enqueued in `enqueue_datatables_files()`. |
| 95 |
wp_register_script( 'tablepress-datatables', '', array(), TablePress::version, array( 'in_footer' => true ) ); |
| 96 |
|
| 97 |
add_action( 'wp_print_footer_scripts', array( $this, 'add_datatables_calls' ), 9 ); // Priority 9 so that this runs before `_wp_footer_scripts()`. |
| 98 |
|
| 99 |
// Register TablePress Shortcodes. Priority 20 is kept for backwards-compatibility purposes. |
| 100 |
add_action( 'init', array( $this, 'init_shortcodes' ), 20 ); |
| 101 |
|
| 102 |
/** |
| 103 |
* Filters whether the WordPress search shall also search TablePress tables. |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* |
| 107 |
* @param bool $search Whether the TablePress tables shall be searched. Default true. |
| 108 |
*/ |
| 109 |
if ( apply_filters( 'tablepress_wp_search_integration', true ) ) { |
| 110 |
// Extend WordPress Search to also find posts/pages that have a table with the one of the search terms in title (if shown), description (if shown), or content. |
| 111 |
add_filter( 'posts_search', array( $this, 'posts_search_filter' ) ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Load TablePress Template Tag functions. |
| 116 |
*/ |
| 117 |
TablePress::load_file( 'template-tag-functions.php', 'controllers' ); |
| 118 |
|
| 119 |
/** |
| 120 |
* Register the tablepress/table block and its dependencies. |
| 121 |
*/ |
| 122 |
wp_register_block_metadata_collection( |
| 123 |
TABLEPRESS_ABSPATH . 'blocks', |
| 124 |
TABLEPRESS_ABSPATH . 'blocks/blocks-manifest.php', |
| 125 |
); |
| 126 |
register_block_type_from_metadata( |
| 127 |
TABLEPRESS_ABSPATH . 'blocks/table/block.json', |
| 128 |
array( |
| 129 |
'render_callback' => array( $this, 'table_block_render_callback' ), |
| 130 |
), |
| 131 |
); |
| 132 |
|
| 133 |
/** |
| 134 |
* Register the TablePress Elementor widgets. |
| 135 |
*/ |
| 136 |
add_action( 'elementor/widgets/register', array( $this, 'register_elementor_widgets' ) ); |
| 137 |
add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'enqueue_elementor_editor_styles' ), 10, 0 ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Registers TablePress Shortcodes. |
| 142 |
* |
| 143 |
* @since 1.0.0 |
| 144 |
*/ |
| 145 |
public function init_shortcodes(): void { |
| 146 |
add_shortcode( TablePress::$shortcode, array( $this, 'shortcode_table' ) ); |
| 147 |
add_shortcode( TablePress::$shortcode_info, array( $this, 'shortcode_table_info' ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Checks if the CSS files for TablePress default CSS and "Custom CSS" should be loaded. |
| 152 |
* |
| 153 |
* This function is only called when a [table /] Shortcode or "TablePress Table" block is evaluated, so that CSS files are only loaded when needed. |
| 154 |
* |
| 155 |
* @since 3.0.0 |
| 156 |
*/ |
| 157 |
public function maybe_enqueue_css(): void { |
| 158 |
// Bail early if the legacy CSS loading mechanism is used, as the files will then have been enqueued already. |
| 159 |
if ( $this->use_legacy_css_loading && ! doing_action( 'enqueue_block_assets' ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* Bail early if the function is called from some action hook outside of the normal rendering process. |
| 165 |
* These are often used by e.g. SEO plugins that render the content in additional contexts, e.g. to get an excerpt via an output buffer. |
| 166 |
* In these cases, we don't want to enqueue the CSS, as it would likely not be printed on the page. |
| 167 |
*/ |
| 168 |
if ( doing_action( 'wp_head' ) || doing_action( 'wp_footer' ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// Prevent repeated execution via a static variable. |
| 173 |
static $css_enqueued = false; |
| 174 |
if ( $css_enqueued && ! doing_action( 'enqueue_block_assets' ) ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
$css_enqueued = true; |
| 178 |
|
| 179 |
$this->enqueue_css(); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enqueues CSS files for TablePress default CSS and "Custom CSS" (if desired). |
| 184 |
* |
| 185 |
* If styles have not been printed to the page (in the `<head>`), the TablePress CSS files will be enqueued. |
| 186 |
* If styles have already been printed to the page, the TablePress CSS files will be printed right away (likely in the `<body`>). |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
*/ |
| 190 |
public function enqueue_css(): void { |
| 191 |
/** |
| 192 |
* Filters whether the TablePress Default CSS code shall be loaded. |
| 193 |
* |
| 194 |
* @since 1.0.0 |
| 195 |
* |
| 196 |
* @param bool $use Whether the Default CSS shall be loaded. Default true. |
| 197 |
*/ |
| 198 |
$use_default_css = apply_filters( 'tablepress_use_default_css', true ); |
| 199 |
$use_custom_css = TablePress::$model_options->get( 'use_custom_css' ); |
| 200 |
|
| 201 |
if ( ! $use_default_css && ! $use_custom_css ) { |
| 202 |
// Register a placeholder dependency, so that the handle is known for other styles. |
| 203 |
wp_register_style( 'tablepress-default', false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
$custom_css = TablePress::$model_options->get( 'custom_css' ); |
| 208 |
$use_custom_css = $use_custom_css && '' !== $custom_css; |
| 209 |
$use_custom_css_file = $use_custom_css && TablePress::$model_options->get( 'use_custom_css_file' ); |
| 210 |
/** |
| 211 |
* Filters the "Custom CSS" version number that is appended to the enqueued CSS files |
| 212 |
* |
| 213 |
* @since 1.0.0 |
| 214 |
* |
| 215 |
* @param int $version The "Custom CSS" version. |
| 216 |
*/ |
| 217 |
$custom_css_version = (string) apply_filters( 'tablepress_custom_css_version', TablePress::$model_options->get( 'custom_css_version' ) ); |
| 218 |
|
| 219 |
$tablepress_css = TablePress::load_class( 'TablePress_CSS', 'class-css.php', 'classes' ); |
| 220 |
|
| 221 |
// Determine Default CSS URL. |
| 222 |
$rtl = ( is_rtl() ) ? '-rtl' : ''; |
| 223 |
$unfiltered_default_css_url = plugins_url( "css/build/default{$rtl}.css", TABLEPRESS__FILE__ ); |
| 224 |
/** |
| 225 |
* Filters the URL from which the TablePress Default CSS file is loaded. |
| 226 |
* |
| 227 |
* @since 1.0.0 |
| 228 |
* |
| 229 |
* @param string $unfiltered_default_css_url URL of the TablePress Default CSS file. |
| 230 |
*/ |
| 231 |
$default_css_url = apply_filters( 'tablepress_default_css_url', $unfiltered_default_css_url ); |
| 232 |
|
| 233 |
$use_custom_css_combined_file = ( $use_default_css && $use_custom_css_file && ! SCRIPT_DEBUG && ! is_rtl() && $unfiltered_default_css_url === $default_css_url && $tablepress_css->load_custom_css_from_file( 'combined' ) ); |
| 234 |
|
| 235 |
if ( $use_custom_css_combined_file ) { |
| 236 |
$custom_css_combined_url = $tablepress_css->get_custom_css_location( 'combined', 'url' ); |
| 237 |
// Need to use 'tablepress-default' instead of 'tablepress-combined' to not break existing TablePress Extensions. |
| 238 |
wp_enqueue_style( 'tablepress-default', $custom_css_combined_url, array(), $custom_css_version ); |
| 239 |
if ( did_action( 'wp_print_styles' ) ) { |
| 240 |
wp_print_styles( 'tablepress-default' ); |
| 241 |
} |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
if ( $use_default_css ) { |
| 246 |
wp_enqueue_style( 'tablepress-default', $default_css_url, array(), TablePress::version ); |
| 247 |
} else { |
| 248 |
// Register a placeholder dependency, so that the handle is known for other styles. |
| 249 |
wp_register_style( 'tablepress-default', false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 250 |
} |
| 251 |
|
| 252 |
$use_custom_css_minified_file = ( $use_custom_css_file && ! SCRIPT_DEBUG && $tablepress_css->load_custom_css_from_file( 'minified' ) ); |
| 253 |
if ( $use_custom_css_minified_file ) { |
| 254 |
$custom_css_minified_url = $tablepress_css->get_custom_css_location( 'minified', 'url' ); |
| 255 |
wp_enqueue_style( 'tablepress-custom', $custom_css_minified_url, array( 'tablepress-default' ), $custom_css_version ); |
| 256 |
if ( did_action( 'wp_print_styles' ) ) { |
| 257 |
wp_print_styles( 'tablepress-custom' ); |
| 258 |
} |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
$use_custom_css_normal_file = ( $use_custom_css_file && $tablepress_css->load_custom_css_from_file( 'normal' ) ); |
| 263 |
if ( $use_custom_css_normal_file ) { |
| 264 |
$custom_css_normal_url = $tablepress_css->get_custom_css_location( 'normal', 'url' ); |
| 265 |
wp_enqueue_style( 'tablepress-custom', $custom_css_normal_url, array( 'tablepress-default' ), $custom_css_version ); |
| 266 |
if ( did_action( 'wp_print_styles' ) ) { |
| 267 |
wp_print_styles( 'tablepress-custom' ); |
| 268 |
} |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
if ( $use_custom_css ) { |
| 273 |
// Get "Custom CSS" from options, try minified Custom CSS first. |
| 274 |
$custom_css_minified = TablePress::$model_options->get( 'custom_css_minified' ); |
| 275 |
if ( ! empty( $custom_css_minified ) ) { |
| 276 |
$custom_css = $custom_css_minified; |
| 277 |
} |
| 278 |
/** |
| 279 |
* Filters the "Custom CSS" code that is to be loaded as inline CSS. |
| 280 |
* |
| 281 |
* @since 1.0.0 |
| 282 |
* |
| 283 |
* @param string $custom_css The "Custom CSS" code. |
| 284 |
*/ |
| 285 |
$custom_css = apply_filters( 'tablepress_custom_css', $custom_css ); |
| 286 |
if ( ! empty( $custom_css ) ) { |
| 287 |
wp_add_inline_style( 'tablepress-default', $custom_css ); |
| 288 |
if ( did_action( 'wp_print_styles' ) ) { |
| 289 |
wp_print_styles( 'tablepress-default' ); |
| 290 |
} |
| 291 |
return; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Enqueues the DataTables JavaScript library and its dependencies. |
| 298 |
* |
| 299 |
* @since 3.0.0 |
| 300 |
*/ |
| 301 |
protected function enqueue_datatables_files(): void { |
| 302 |
$js_file = 'js/jquery.datatables.min.js'; |
| 303 |
$js_url = plugins_url( $js_file, TABLEPRESS__FILE__ ); |
| 304 |
/** |
| 305 |
* Filters the URL from which the DataTables JavaScript library file is loaded. |
| 306 |
* |
| 307 |
* @since 1.0.0 |
| 308 |
* |
| 309 |
* @param string $js_url URL of the DataTables JS library file. |
| 310 |
* @param string $js_file Path and file name of the DataTables JS library file. |
| 311 |
*/ |
| 312 |
$js_url = apply_filters( 'tablepress_datatables_js_url', $js_url, $js_file ); |
| 313 |
|
| 314 |
$dependencies = array( 'jquery-core' ); |
| 315 |
if ( ! empty( $this->datatables_datetime_formats ) ) { |
| 316 |
$dependencies[] = 'moment'; |
| 317 |
} |
| 318 |
/** |
| 319 |
* Filters the dependencies for the DataTables JavaScript library. |
| 320 |
* |
| 321 |
* @since 3.0.0 |
| 322 |
* |
| 323 |
* @param string[] $dependencies The dependencies for the DataTables JS library. |
| 324 |
*/ |
| 325 |
$dependencies = apply_filters( 'tablepress_datatables_js_dependencies', $dependencies ); |
| 326 |
|
| 327 |
// De-register the placeholder script handle and enqueue the actual script, so that the dependencies are correct. |
| 328 |
wp_deregister_script( 'tablepress-datatables' ); |
| 329 |
wp_enqueue_script( 'tablepress-datatables', $js_url, $dependencies, TablePress::version, array( 'in_footer' => true ) ); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Adds the JavaScript code for the invocation of the DataTables JS library. |
| 334 |
* |
| 335 |
* @since 1.0.0 |
| 336 |
*/ |
| 337 |
public function add_datatables_calls(): void { |
| 338 |
// Prevent repeated execution (which would lead to DataTables error messages) via a static variable. |
| 339 |
static $datatables_calls_printed = false; |
| 340 |
if ( $datatables_calls_printed ) { |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
// Bail early if there are no TablePress tables on the page. |
| 345 |
if ( empty( $this->shown_tables ) ) { |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
/* |
| 350 |
* Don't add the DataTables function calls in the scope of the block editor iframe. |
| 351 |
* This is necessary for non-block themes, for others, the repeated execution check above is sufficient. |
| 352 |
*/ |
| 353 |
if ( function_exists( 'get_current_screen' ) ) { |
| 354 |
$current_screen = get_current_screen(); |
| 355 |
if ( ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor() ) { |
| 356 |
return; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Filter out all tables that use DataTables. |
| 361 |
$shown_tables_with_datatables = array(); |
| 362 |
foreach ( $this->shown_tables as $table_id => $table_store ) { |
| 363 |
if ( ! empty( $table_store['instances'] ) ) { |
| 364 |
$shown_tables_with_datatables[ (string) $table_id ] = $table_store; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
// Bail early if there are no tables with activated DataTables on the page. |
| 369 |
if ( empty( $shown_tables_with_datatables ) ) { |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
$this->enqueue_datatables_files(); |
| 374 |
|
| 375 |
// Storage for the DataTables language strings. |
| 376 |
$datatables_language = array(); |
| 377 |
// Generate the specific JS commands, depending on chosen features on the "Edit" screen and the Shortcode parameters. |
| 378 |
$commands = array(); |
| 379 |
|
| 380 |
foreach ( $shown_tables_with_datatables as $table_id => $table_store ) { |
| 381 |
$table_id = (string) $table_id; // Ensure that the table ID is a string, as it comes from an array key where numeric strings are converted to integers. |
| 382 |
|
| 383 |
foreach ( $table_store['instances'] as $html_id => $js_options ) { |
| 384 |
$parameters = array(); |
| 385 |
|
| 386 |
// Settle dependencies/conflicts between certain features. |
| 387 |
if ( false !== $js_options['datatables_scrolly'] ) { // datatables_scrolly can be a string, so that the explicit `false` check is needed. |
| 388 |
// Vertical scrolling and pagination don't work together. |
| 389 |
$js_options['datatables_paginate'] = false; |
| 390 |
} |
| 391 |
// Sanitize, as it may come from a Shortcode attribute. |
| 392 |
$js_options['datatables_paginate_entries'] = (int) $js_options['datatables_paginate_entries']; |
| 393 |
|
| 394 |
/* |
| 395 |
* DataTables language/translation handling. |
| 396 |
*/ |
| 397 |
|
| 398 |
/** |
| 399 |
* Filters the locale/language for the DataTables JavaScript library. |
| 400 |
* |
| 401 |
* @since 1.0.0 |
| 402 |
* |
| 403 |
* @param string $locale The DataTables JS library locale. |
| 404 |
* @param string $table_id The current table ID. |
| 405 |
*/ |
| 406 |
$datatables_locale = apply_filters( 'tablepress_datatables_locale', $js_options['datatables_locale'], $table_id ); |
| 407 |
|
| 408 |
// Only load each locale's language file once. |
| 409 |
if ( ! isset( $datatables_language[ $datatables_locale ] ) ) { |
| 410 |
$orig_language_file = TABLEPRESS_ABSPATH . "i18n/datatables/lang-{$datatables_locale}.php"; |
| 411 |
|
| 412 |
/** |
| 413 |
* Filters the language file path for the DataTables JavaScript library. |
| 414 |
* |
| 415 |
* PHP files that return an array and JSON files are supported. |
| 416 |
* The JSON file method is deprecated and should no longer be used. |
| 417 |
* |
| 418 |
* @since 1.0.0 |
| 419 |
* |
| 420 |
* @param string $orig_language_file Language file path for the DataTables JS library. |
| 421 |
* @param string $datatables_locale Current locale/language for the DataTables JS library. |
| 422 |
* @param string $tablepress_abspath Base path of the TablePress plugin. |
| 423 |
*/ |
| 424 |
$language_file = apply_filters( 'tablepress_datatables_language_file', $orig_language_file, $datatables_locale, TABLEPRESS_ABSPATH ); |
| 425 |
|
| 426 |
/* |
| 427 |
* Load translation file if it's not "en_US" (included as the default in DataTables) |
| 428 |
* or if the filter was used to change the language file, and the language file exists. |
| 429 |
* Otherwise, use an empty en_US placeholder, so that the strings are filterable later. |
| 430 |
*/ |
| 431 |
if ( ( 'en_US' !== $datatables_locale || $orig_language_file !== $language_file ) && file_exists( $language_file ) ) { |
| 432 |
if ( str_ends_with( $language_file, '.php' ) ) { |
| 433 |
$datatables_strings = require $language_file; |
| 434 |
if ( ! is_array( $datatables_strings ) ) { |
| 435 |
$datatables_strings = array(); |
| 436 |
} |
| 437 |
} elseif ( str_ends_with( $language_file, '.json' ) ) { |
| 438 |
$datatables_strings = file_get_contents( $language_file ); |
| 439 |
$datatables_strings = json_decode( $datatables_strings, true ); // @phpstan-ignore argument.type |
| 440 |
// Check if JSON could be decoded. |
| 441 |
if ( is_null( $datatables_strings ) ) { |
| 442 |
$datatables_strings = array(); |
| 443 |
} |
| 444 |
$datatables_strings = (array) $datatables_strings; |
| 445 |
} else { |
| 446 |
// The filtered language file exists, but is not a .php or .json file, so don't use it. |
| 447 |
$datatables_strings = array(); |
| 448 |
} |
| 449 |
} else { |
| 450 |
// If no translation file for the defined locale exists or is needed, use "en_US", as that's built-in. |
| 451 |
$datatables_locale = 'en_US'; |
| 452 |
$datatables_strings = array(); |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Filters the language strings for the DataTables JavaScript library's features. |
| 457 |
* |
| 458 |
* @since 2.0.0 |
| 459 |
* |
| 460 |
* @param array<string, mixed> $datatables_strings The language strings for DataTables. |
| 461 |
* @param string $datatables_locale Current locale/language for the DataTables JS library. |
| 462 |
*/ |
| 463 |
$datatables_language[ $datatables_locale ] = apply_filters( 'tablepress_datatables_language_strings', $datatables_strings, $datatables_locale ); |
| 464 |
} |
| 465 |
$parameters['language'] = "language:DT_language['{$datatables_locale}']"; |
| 466 |
|
| 467 |
// These parameters need to be added for performance gain or to overwrite unwanted default behavior. |
| 468 |
if ( $js_options['datatables_sort'] ) { |
| 469 |
// No initial sort. |
| 470 |
$parameters['order'] = 'order:[]'; |
| 471 |
// Don't add additional classes, to speed up sorting. |
| 472 |
$parameters['orderClasses'] = 'orderClasses:false'; |
| 473 |
} |
| 474 |
|
| 475 |
// The following options are activated by default, so we only need to "false" them if we don't want them, but don't need to "true" them if we do. |
| 476 |
if ( ! $js_options['datatables_sort'] ) { |
| 477 |
$parameters['ordering'] = 'ordering:false'; |
| 478 |
} |
| 479 |
if ( $js_options['datatables_paginate'] ) { |
| 480 |
$parameters['pagingType'] = "pagingType:'simple_numbers'"; |
| 481 |
if ( $js_options['datatables_lengthchange'] ) { |
| 482 |
$length_menu = array( 10, 25, 50, 100 ); |
| 483 |
if ( ! in_array( $js_options['datatables_paginate_entries'], $length_menu, true ) ) { |
| 484 |
$length_menu[] = $js_options['datatables_paginate_entries']; |
| 485 |
sort( $length_menu, SORT_NUMERIC ); |
| 486 |
$parameters['lengthMenu'] = 'lengthMenu:[' . implode( ',', $length_menu ) . ']'; |
| 487 |
} |
| 488 |
} else { |
| 489 |
$parameters['lengthChange'] = 'lengthChange:false'; |
| 490 |
} |
| 491 |
if ( 10 !== $js_options['datatables_paginate_entries'] ) { |
| 492 |
$parameters['pageLength'] = "pageLength:{$js_options['datatables_paginate_entries']}"; |
| 493 |
} |
| 494 |
} else { |
| 495 |
$parameters['paging'] = 'paging:false'; |
| 496 |
} |
| 497 |
if ( ! $js_options['datatables_filter'] ) { |
| 498 |
$parameters['searching'] = 'searching:false'; |
| 499 |
} |
| 500 |
if ( ! $js_options['datatables_info'] ) { |
| 501 |
$parameters['info'] = 'info:false'; |
| 502 |
} |
| 503 |
if ( $js_options['datatables_scrollx'] ) { |
| 504 |
$parameters['scrollX'] = 'scrollX:true'; |
| 505 |
} |
| 506 |
if ( false !== $js_options['datatables_scrolly'] ) { |
| 507 |
$parameters['scrollY'] = 'scrollY:"' . preg_replace( '#[^0-9a-z.%]#', '', $js_options['datatables_scrolly'] ) . '"'; |
| 508 |
$parameters['scrollCollapse'] = 'scrollCollapse:true'; |
| 509 |
} |
| 510 |
if ( '' !== $js_options['datatables_custom_commands'] ) { |
| 511 |
$parameters['custom_commands'] = trim( $js_options['datatables_custom_commands'] ); // Remove leading and trailing whitespace. |
| 512 |
$parameters['custom_commands'] = trim( $parameters['custom_commands'], ',' ); // Remove potentially leading and trailing commas to prevent JS script errors. |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Filters the parameters that are passed to the DataTables JavaScript library. |
| 517 |
* |
| 518 |
* @since 1.0.0 |
| 519 |
* |
| 520 |
* @param array<string, mixed> $parameters The parameters for the DataTables JS library. |
| 521 |
* @param string $table_id The current table ID. |
| 522 |
* @param string $html_id The ID of the table HTML element. |
| 523 |
* @param array<string, mixed> $js_options The options for the JS library. |
| 524 |
*/ |
| 525 |
$parameters = apply_filters( 'tablepress_datatables_parameters', $parameters, $table_id, $html_id, $js_options ); |
| 526 |
|
| 527 |
// If an existing parameter is set as an object key in the "Custom Commands", remove its separate value, to allow for full overrides. |
| 528 |
if ( isset( $parameters['custom_commands'] ) && '' !== $parameters['custom_commands'] ) { |
| 529 |
$parameters_in_custom_commands = TablePress::extract_keys_from_js_object_string( '{' . $parameters['custom_commands'] . '}' ); |
| 530 |
foreach ( $parameters_in_custom_commands as $parameter_in_custom_commands ) { |
| 531 |
unset( $parameters[ $parameter_in_custom_commands ] ); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
$name = substr( $html_id, 11 ); // Remove "tablepress-" from the HTML ID. |
| 536 |
$name = "DT_TP['" . str_replace( '-', '_', $name ) . "']"; |
| 537 |
$parameters = implode( ',', $parameters ); |
| 538 |
$parameters = ( ! empty( $parameters ) ) ? '{' . $parameters . '}' : ''; |
| 539 |
|
| 540 |
$command = "{$name} = new DataTable('#{$html_id}',{$parameters});"; |
| 541 |
/** |
| 542 |
* Filters the JavaScript command that invokes the DataTables JavaScript library on one table. |
| 543 |
* |
| 544 |
* @since 1.0.0 |
| 545 |
* |
| 546 |
* @param string $command The JS command for the DataTables JS library. |
| 547 |
* @param string $html_id The ID of the table HTML element. |
| 548 |
* @param string $parameters The parameters for the DataTables JS library. |
| 549 |
* @param string $table_id The current table ID. |
| 550 |
* @param array<string, mixed> $js_options The options for the JS library. |
| 551 |
* @param string $name The name of the DataTable instance. |
| 552 |
*/ |
| 553 |
$command = apply_filters( 'tablepress_datatables_command', $command, $html_id, $parameters, $table_id, $js_options, $name ); |
| 554 |
if ( ! empty( $command ) ) { |
| 555 |
$commands[] = $command; |
| 556 |
} |
| 557 |
} // foreach table instance |
| 558 |
} // foreach table ID |
| 559 |
|
| 560 |
// DataTables language/translation handling. |
| 561 |
$datatables_language_command = wp_json_encode( $datatables_language, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT ); |
| 562 |
$datatables_language_command = "var DT_language={$datatables_language_command};\n"; |
| 563 |
|
| 564 |
// DataTables datetime format string handling. |
| 565 |
if ( ! empty( $this->datatables_datetime_formats ) ) { |
| 566 |
// Create a command like `DataTable.datetime("MM/DD/YYYY");DataTable.datetime("DD.MM.YYYY");`. |
| 567 |
$datatables_datetime_command = implode( |
| 568 |
'', |
| 569 |
array_map( |
| 570 |
static function ( string $datetime_format ): string { |
| 571 |
$datetime_format = wp_json_encode( $datetime_format, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); |
| 572 |
return "DataTable.datetime({$datetime_format});"; |
| 573 |
}, |
| 574 |
$this->datatables_datetime_formats, |
| 575 |
) |
| 576 |
) . "\n"; |
| 577 |
} else { |
| 578 |
$datatables_datetime_command = ''; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Filters the JavaScript code for the DataTables JavaScript library that initializes the automatically detected date/time formats via moment.js. |
| 583 |
* |
| 584 |
* @since 3.0.0 |
| 585 |
* |
| 586 |
* @param string $datatables_datetime_command The JS code for the DataTables JS library that initializes the date/time formats. |
| 587 |
* @param string[] $datatables_datetime_formats The date/time formats for moment.js. |
| 588 |
*/ |
| 589 |
$datatables_datetime_command = apply_filters( 'tablepress_datatables_datetime_command', $datatables_datetime_command, $this->datatables_datetime_formats ); |
| 590 |
|
| 591 |
$datatables_pre_commands = $datatables_language_command . $datatables_datetime_command; |
| 592 |
|
| 593 |
$commands = implode( "\n", $commands ); |
| 594 |
/** |
| 595 |
* Filters the JavaScript commands that invoke the DataTables JavaScript library on all tables on the page. |
| 596 |
* |
| 597 |
* @since 1.0.0 |
| 598 |
* |
| 599 |
* @param string $commands The JS commands for the DataTables JS library. |
| 600 |
*/ |
| 601 |
$commands = apply_filters( 'tablepress_all_datatables_commands', $commands ); |
| 602 |
if ( '' === $commands ) { |
| 603 |
return; |
| 604 |
} |
| 605 |
|
| 606 |
$script_template = <<<'JS' |
| 607 |
var DT_TP = {}; |
| 608 |
jQuery(($)=>{ |
| 609 |
%1$s%2$s |
| 610 |
}); |
| 611 |
JS; |
| 612 |
/** |
| 613 |
* Filters the script/jQuery wrapper code for the DataTables commands calls. |
| 614 |
* |
| 615 |
* @since 1.14.0 |
| 616 |
* |
| 617 |
* @param string $script_template Default script/jQuery wrapper code for the DataTables commands calls. |
| 618 |
*/ |
| 619 |
$script_template = apply_filters( 'tablepress_all_datatables_commands_wrapper', $script_template ); |
| 620 |
|
| 621 |
$script = sprintf( $script_template, $datatables_pre_commands, $commands ); |
| 622 |
wp_add_inline_script( 'tablepress-datatables', $script ); |
| 623 |
|
| 624 |
// Prevent repeated execution (which would lead to DataTables error messages) via a static variable. |
| 625 |
$datatables_calls_printed = true; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Handles the Shortcode [table id=<ID> /]. |
| 630 |
* |
| 631 |
* @since 1.0.0 |
| 632 |
* |
| 633 |
* @param array<string, mixed> $shortcode_atts List of attributes that where included in the Shortcode. |
| 634 |
* @return string Resulting HTML code for the table with the ID <ID>. |
| 635 |
*/ |
| 636 |
public function shortcode_table( array $shortcode_atts ): string { |
| 637 |
$this->maybe_enqueue_css(); |
| 638 |
|
| 639 |
$_render = TablePress::load_class( 'TablePress_Render', 'class-render.php', 'classes' ); |
| 640 |
|
| 641 |
$default_shortcode_atts = $_render->get_default_render_options(); |
| 642 |
/** |
| 643 |
* Filters the available/default attributes for the [table] Shortcode. |
| 644 |
* |
| 645 |
* @since 1.0.0 |
| 646 |
* |
| 647 |
* @param array<string, mixed> $default_shortcode_atts The [table] Shortcode default attributes. |
| 648 |
*/ |
| 649 |
$default_shortcode_atts = apply_filters( 'tablepress_shortcode_table_default_shortcode_atts', $default_shortcode_atts ); |
| 650 |
// Parse Shortcode attributes, only allow those that are specified. |
| 651 |
$shortcode_atts = shortcode_atts( $default_shortcode_atts, $shortcode_atts ); // Optional third argument left out on purpose. Use filter in the next line instead. |
| 652 |
/** |
| 653 |
* Filters the attributes that were passed to the [table] Shortcode. |
| 654 |
* |
| 655 |
* @since 1.0.0 |
| 656 |
* |
| 657 |
* @param array<string, mixed> $shortcode_atts The attributes passed to the [table] Shortcode. |
| 658 |
*/ |
| 659 |
$shortcode_atts = apply_filters( 'tablepress_shortcode_table_shortcode_atts', $shortcode_atts ); |
| 660 |
|
| 661 |
// Check, if a table with the given ID exists. |
| 662 |
$table_id = (string) preg_replace( '/[^a-zA-Z0-9_-]/', '', $shortcode_atts['id'] ); |
| 663 |
if ( ! TablePress::$model_table->table_exists( $table_id ) ) { |
| 664 |
$message = "[table “{$table_id}” not found /]<br />\n"; |
| 665 |
/** |
| 666 |
* Filters the "Table not found" message. |
| 667 |
* |
| 668 |
* @since 1.0.0 |
| 669 |
* |
| 670 |
* @param string $message The "Table not found" message. |
| 671 |
* @param string $table_id The current table ID. |
| 672 |
*/ |
| 673 |
$message = apply_filters( 'tablepress_table_not_found_message', $message, $table_id ); |
| 674 |
return $message; |
| 675 |
} |
| 676 |
|
| 677 |
// Load table, with table data, options, and visibility settings. |
| 678 |
$table = TablePress::$model_table->load( $table_id, true, true ); |
| 679 |
if ( is_wp_error( $table ) ) { |
| 680 |
$message = "[table “{$table_id}” could not be loaded /]<br />\n"; |
| 681 |
/** |
| 682 |
* Filters the "Table could not be loaded" message. |
| 683 |
* |
| 684 |
* @since 1.0.0 |
| 685 |
* |
| 686 |
* @param string $message The "Table could not be loaded" message. |
| 687 |
* @param string $table_id The current table ID. |
| 688 |
* @param WP_Error $table The error object for the table. |
| 689 |
*/ |
| 690 |
$message = apply_filters( 'tablepress_table_load_error_message', $message, $table_id, $table ); |
| 691 |
return $message; |
| 692 |
} |
| 693 |
if ( isset( $table['is_corrupted'] ) && $table['is_corrupted'] ) { |
| 694 |
$message = "<div>Attention: The internal data of table “{$table_id}” is corrupted!</div>"; |
| 695 |
/** |
| 696 |
* Filters the "Table data is corrupted" message. |
| 697 |
* |
| 698 |
* @since 1.0.0 |
| 699 |
* |
| 700 |
* @param string $message The "Table data is corrupted" message. |
| 701 |
* @param string $table_id The current table ID. |
| 702 |
* @param string $json_error The JSON error with information about the corrupted table. |
| 703 |
*/ |
| 704 |
$message = apply_filters( 'tablepress_table_corrupted_message', $message, $table_id, $table['json_error'] ); |
| 705 |
return $message; |
| 706 |
} |
| 707 |
|
| 708 |
if ( ! is_null( $shortcode_atts['datatables_custom_commands'] ) ) { |
| 709 |
/** |
| 710 |
* Filters whether the "datatables_custom_commands" Shortcode parameter is disabled. |
| 711 |
* |
| 712 |
* By default, the "datatables_custom_commands" Shortcode parameter is disabled for security reasons. |
| 713 |
* |
| 714 |
* @since 1.0.0 |
| 715 |
* |
| 716 |
* @param bool $disable Whether to disable the "datatables_custom_commands" Shortcode parameter. Default true. |
| 717 |
*/ |
| 718 |
if ( apply_filters( 'tablepress_disable_custom_commands_shortcode_parameter', true ) ) { |
| 719 |
$shortcode_atts['datatables_custom_commands'] = null; |
| 720 |
} else { |
| 721 |
// Convert the HTML entity `&` back to `&` manually, as entities in Shortcodes in normal text paragraphs are sometimes double-encoded. |
| 722 |
$shortcode_atts['datatables_custom_commands'] = str_replace( '&', '&', $shortcode_atts['datatables_custom_commands'] ); |
| 723 |
// Convert HTML entities like `<`, `[`, `[`, and `&` back to their respective characters. |
| 724 |
$shortcode_atts['datatables_custom_commands'] = html_entity_decode( $shortcode_atts['datatables_custom_commands'], ENT_QUOTES | ENT_HTML5, get_option( 'blog_charset' ) ); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
// Determine options to use (if set in Shortcode, use those, otherwise use stored options, from the "Edit" screen). |
| 729 |
$render_options = array(); |
| 730 |
foreach ( $shortcode_atts as $key => $value ) { |
| 731 |
if ( is_null( $value ) && isset( $table['options'][ $key ] ) ) { |
| 732 |
// Use the table's stored option value, if the Shortcode parameter was not set. |
| 733 |
$render_options[ $key ] = $table['options'][ $key ]; |
| 734 |
} elseif ( is_string( $value ) ) { |
| 735 |
// Convert strings 'true' or 'false' to boolean, keep others. |
| 736 |
$value_lowercase = strtolower( $value ); |
| 737 |
if ( 'true' === $value_lowercase ) { |
| 738 |
$render_options[ $key ] = true; |
| 739 |
} elseif ( 'false' === $value_lowercase ) { |
| 740 |
$render_options[ $key ] = false; |
| 741 |
} else { |
| 742 |
$render_options[ $key ] = $value; |
| 743 |
} |
| 744 |
} else { |
| 745 |
// Keep all other values. |
| 746 |
$render_options[ $key ] = $value; |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
// Backward compatibility: Convert boolean or numeric string "table_head" and "table_foot" options to integer. |
| 751 |
$render_options['table_head'] = absint( $render_options['table_head'] ); |
| 752 |
$render_options['table_foot'] = absint( $render_options['table_foot'] ); |
| 753 |
|
| 754 |
// Generate unique HTML ID, depending on how often this table has already been shown on this page. |
| 755 |
if ( ! isset( $this->shown_tables[ $table_id ] ) ) { |
| 756 |
$this->shown_tables[ $table_id ] = array( |
| 757 |
'count' => 0, |
| 758 |
'instances' => array(), |
| 759 |
); |
| 760 |
} |
| 761 |
++$this->shown_tables[ $table_id ]['count']; |
| 762 |
$count = $this->shown_tables[ $table_id ]['count']; |
| 763 |
$render_options['html_id'] = "tablepress-{$table_id}"; |
| 764 |
if ( $count > 1 ) { |
| 765 |
$render_options['html_id'] .= "-no-{$count}"; |
| 766 |
} |
| 767 |
/** |
| 768 |
* Filters the ID of the table HTML element. |
| 769 |
* |
| 770 |
* @since 1.0.0 |
| 771 |
* |
| 772 |
* @param string $html_id The ID of the table HTML element. |
| 773 |
* @param string $table_id The current table ID. |
| 774 |
* @param int $count Number of copies of the table with this table ID on the page. |
| 775 |
*/ |
| 776 |
$render_options['html_id'] = apply_filters( 'tablepress_html_id', $render_options['html_id'], $table_id, $count ); |
| 777 |
|
| 778 |
// Generate the "Edit Table" link. |
| 779 |
$render_options['edit_table_url'] = ''; |
| 780 |
/** |
| 781 |
* Filters whether the "Edit" link below the table shall be shown. |
| 782 |
* |
| 783 |
* The "Edit" link is only shown to logged-in users who possess the necessary capability to edit the table. |
| 784 |
* |
| 785 |
* @since 1.0.0 |
| 786 |
* |
| 787 |
* @param bool $show Whether to show the "Edit" link below the table. Default true. |
| 788 |
* @param string $table_id The current table ID. |
| 789 |
*/ |
| 790 |
if ( is_user_logged_in() && ! $render_options['block_preview'] && apply_filters( 'tablepress_edit_link_below_table', true, $table['id'] ) && current_user_can( 'tablepress_edit_table', $table['id'] ) ) { |
| 791 |
$render_options['edit_table_url'] = TablePress::url( array( 'action' => 'edit', 'table_id' => $table['id'] ) ); |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* Filters the render options for the table. |
| 796 |
* |
| 797 |
* The render options are determined from the settings on a table's "Edit" screen and the Shortcode parameters. |
| 798 |
* |
| 799 |
* @since 1.0.0 |
| 800 |
* |
| 801 |
* @param array<string, mixed> $render_options The render options for the table. |
| 802 |
* @param array<string, mixed> $table The current table. |
| 803 |
*/ |
| 804 |
$render_options = apply_filters( 'tablepress_table_render_options', $render_options, $table ); |
| 805 |
|
| 806 |
// Backward compatibility: Convert boolean "table_head" and "table_foot" options to integer, in case they were overwritten via the filter hook. |
| 807 |
$render_options['table_head'] = absint( $render_options['table_head'] ); |
| 808 |
$render_options['table_foot'] = absint( $render_options['table_foot'] ); |
| 809 |
|
| 810 |
// Check if table output shall and can be loaded from the transient cache, otherwise generate the output. |
| 811 |
if ( $render_options['cache_table_output'] && ! is_user_logged_in() ) { |
| 812 |
// Hash the Render Options array to get a unique cache identifier. |
| 813 |
$table_hash = md5( wp_json_encode( $render_options, TABLEPRESS_JSON_OPTIONS ) ); // @phpstan-ignore argument.type |
| 814 |
$transient_name = 'tablepress_' . $table_hash; // Attention: This string must not be longer than 45 characters! |
| 815 |
$output = get_transient( $transient_name ); |
| 816 |
if ( false === $output || '' === $output ) { |
| 817 |
// Render/generate the table HTML, as it was not found in the cache. |
| 818 |
$_render->set_input( $table, $render_options ); |
| 819 |
$output = $_render->get_output( 'html' ); |
| 820 |
// Save render output in a transient, set cache timeout to 24 hours. |
| 821 |
set_transient( $transient_name, $output, DAY_IN_SECONDS ); |
| 822 |
// Update output caches list transient (necessary for cache invalidation upon table saving). |
| 823 |
$caches_list_transient_name = 'tablepress_c_' . md5( $table_id ); |
| 824 |
$caches_list = get_transient( $caches_list_transient_name ); |
| 825 |
if ( false === $caches_list ) { |
| 826 |
$caches_list = array(); |
| 827 |
} else { |
| 828 |
$caches_list = (array) json_decode( $caches_list, true ); |
| 829 |
} |
| 830 |
if ( ! in_array( $transient_name, $caches_list, true ) ) { |
| 831 |
$caches_list[] = $transient_name; |
| 832 |
} |
| 833 |
set_transient( $caches_list_transient_name, wp_json_encode( $caches_list, TABLEPRESS_JSON_OPTIONS ), 2 * DAY_IN_SECONDS ); |
| 834 |
} else { |
| 835 |
/** |
| 836 |
* Filters the cache hit comment message. |
| 837 |
* |
| 838 |
* @since 1.0.0 |
| 839 |
* |
| 840 |
* @param string $comment The cache hit comment message. |
| 841 |
*/ |
| 842 |
$output .= apply_filters( 'tablepress_cache_hit_comment', "<!-- #{$render_options['html_id']} from cache -->" ); |
| 843 |
} |
| 844 |
} else { |
| 845 |
// Render/generate the table HTML, as no cache is to be used. |
| 846 |
$_render->set_input( $table, $render_options ); |
| 847 |
$output = $_render->get_output( 'html' ); |
| 848 |
} |
| 849 |
|
| 850 |
// If DataTables is to be and can be used with this instance of a table, process its parameters and register the call for inclusion in the footer. |
| 851 |
if ( $render_options['use_datatables'] |
| 852 |
&& 0 < $render_options['table_head'] |
| 853 |
&& ! str_contains( $output, 'tbody-has-connected-cells' ) // The Render class adds this CSS class to the `<table>` element if the table has connected cells in the `<tbody>`. |
| 854 |
) { |
| 855 |
// Get options for the DataTables JavaScript library from the table's render options. |
| 856 |
$js_options = array(); |
| 857 |
foreach ( array( |
| 858 |
'alternating_row_colors', |
| 859 |
'datatables_sort', |
| 860 |
'datatables_paginate', |
| 861 |
'datatables_paginate', |
| 862 |
'datatables_paginate_entries', |
| 863 |
'datatables_lengthchange', |
| 864 |
'datatables_filter', |
| 865 |
'datatables_info', |
| 866 |
'datatables_scrollx', |
| 867 |
'datatables_scrolly', |
| 868 |
'datatables_locale', |
| 869 |
'datatables_custom_commands', |
| 870 |
) as $option ) { |
| 871 |
$js_options[ $option ] = $render_options[ $option ]; |
| 872 |
} |
| 873 |
/** |
| 874 |
* Filters the JavaScript options for the table. |
| 875 |
* |
| 876 |
* The JavaScript options are determined from the settings on a table's "Edit" screen and the Shortcode parameters. |
| 877 |
* They are part of the render options and can be overwritten with Shortcode parameters. |
| 878 |
* |
| 879 |
* @since 1.0.0 |
| 880 |
* |
| 881 |
* @param array<string, mixed> $js_options The JavaScript options for the table. |
| 882 |
* @param string $table_id The current table ID. |
| 883 |
* @param array<string, mixed> $render_options The render options for the table. |
| 884 |
*/ |
| 885 |
$js_options = apply_filters( 'tablepress_table_js_options', $js_options, $table_id, $render_options ); |
| 886 |
|
| 887 |
$this->shown_tables[ $table_id ]['instances'][ (string) $render_options['html_id'] ] = $js_options; |
| 888 |
|
| 889 |
// DataTables datetime format string handling. |
| 890 |
if ( '' !== $render_options['datatables_datetime'] ) { |
| 891 |
$render_options['datatables_datetime'] = explode( '|', $render_options['datatables_datetime'] ); |
| 892 |
foreach ( $render_options['datatables_datetime'] as $datetime_format ) { |
| 893 |
$datetime_format = trim( $datetime_format ); |
| 894 |
if ( '' !== $datetime_format && ! in_array( $datetime_format, $this->datatables_datetime_formats, true ) ) { |
| 895 |
$this->datatables_datetime_formats[] = $datetime_format; |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
// Maybe print a list of used render options. |
| 902 |
if ( $render_options['shortcode_debug'] && is_user_logged_in() ) { |
| 903 |
$output .= '<pre>' . esc_html( wp_json_encode( $render_options, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ) . '</pre>'; // @phpstan-ignore argument.type |
| 904 |
} |
| 905 |
|
| 906 |
return $output; |
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
* Handles the Shortcode [table-info id=<ID> field=<name> /]. |
| 911 |
* |
| 912 |
* @since 1.0.0 |
| 913 |
* |
| 914 |
* @param array<string, mixed> $shortcode_atts List of attributes that where included in the Shortcode. |
| 915 |
* @return string Text that replaces the Shortcode (error message or asked-for information). |
| 916 |
*/ |
| 917 |
public function shortcode_table_info( array $shortcode_atts ): string { |
| 918 |
// Parse Shortcode attributes, only allow those that are specified. |
| 919 |
$default_shortcode_atts = array( |
| 920 |
'id' => '', |
| 921 |
'field' => '', |
| 922 |
'format' => '', |
| 923 |
); |
| 924 |
/** |
| 925 |
* Filters the available/default attributes for the [table-info] Shortcode. |
| 926 |
* |
| 927 |
* @since 1.0.0 |
| 928 |
* |
| 929 |
* @param array<string, mixed> $default_shortcode_atts The [table-info] Shortcode default attributes. |
| 930 |
*/ |
| 931 |
$default_shortcode_atts = apply_filters( 'tablepress_shortcode_table_info_default_shortcode_atts', $default_shortcode_atts ); |
| 932 |
$shortcode_atts = shortcode_atts( $default_shortcode_atts, $shortcode_atts ); // Optional third argument left out on purpose. Use filter in the next line instead. |
| 933 |
/** |
| 934 |
* Filters the attributes that were passed to the [table-info] Shortcode. |
| 935 |
* |
| 936 |
* @since 1.0.0 |
| 937 |
* |
| 938 |
* @param array<string, mixed> $shortcode_atts The attributes passed to the [table-info] Shortcode. |
| 939 |
*/ |
| 940 |
$shortcode_atts = apply_filters( 'tablepress_shortcode_table_info_shortcode_atts', $shortcode_atts ); |
| 941 |
|
| 942 |
/** |
| 943 |
* Filters whether the output of the [table-info] Shortcode is overwritten/short-circuited. |
| 944 |
* |
| 945 |
* @since 1.0.0 |
| 946 |
* |
| 947 |
* @param false|string $overwrite Whether the [table-info] output is overwritten. Return false for the regular content, and a string to overwrite the output. |
| 948 |
* @param array<string, mixed> $shortcode_atts The attributes passed to the [table-info] Shortcode. |
| 949 |
*/ |
| 950 |
$overwrite = apply_filters( 'tablepress_shortcode_table_info_overwrite', false, $shortcode_atts ); |
| 951 |
if ( is_string( $overwrite ) ) { |
| 952 |
return $overwrite; |
| 953 |
} |
| 954 |
|
| 955 |
// Check, if a table with the given ID exists. |
| 956 |
$table_id = preg_replace( '/[^a-zA-Z0-9_-]/', '', $shortcode_atts['id'] ); |
| 957 |
if ( ! TablePress::$model_table->table_exists( $table_id ) ) { |
| 958 |
$message = "[table “{$table_id}” not found /]<br />\n"; |
| 959 |
/** This filter is documented in controllers/controller-frontend.php */ |
| 960 |
$message = apply_filters( 'tablepress_table_not_found_message', $message, $table_id ); |
| 961 |
return $message; |
| 962 |
} |
| 963 |
|
| 964 |
// Load table, with table data, options, and visibility settings. |
| 965 |
$table = TablePress::$model_table->load( $table_id, true, true ); |
| 966 |
if ( is_wp_error( $table ) ) { |
| 967 |
$message = "[table “{$table_id}” could not be loaded /]<br />\n"; |
| 968 |
/** This filter is documented in controllers/controller-frontend.php */ |
| 969 |
$message = apply_filters( 'tablepress_table_load_error_message', $message, $table_id, $table ); |
| 970 |
return $message; |
| 971 |
} |
| 972 |
|
| 973 |
$field = (string) preg_replace( '/[^a-z_]/', '', strtolower( $shortcode_atts['field'] ) ); |
| 974 |
$format = (string) preg_replace( '/[^a-z]/', '', strtolower( $shortcode_atts['format'] ) ); |
| 975 |
|
| 976 |
// Generate output, depending on what information (field) was asked for. |
| 977 |
switch ( $field ) { |
| 978 |
case 'name': |
| 979 |
case 'description': |
| 980 |
$output = $table[ $field ]; |
| 981 |
break; |
| 982 |
case 'last_modified': |
| 983 |
switch ( $format ) { |
| 984 |
case 'raw': |
| 985 |
case 'mysql': |
| 986 |
$output = $table['last_modified']; |
| 987 |
break; |
| 988 |
case 'human': |
| 989 |
$modified_timestamp = date_create( $table['last_modified'], wp_timezone() ); |
| 990 |
if ( false === $modified_timestamp ) { |
| 991 |
$modified_timestamp = $table['last_modified']; |
| 992 |
} else { |
| 993 |
$modified_timestamp = $modified_timestamp->getTimestamp(); |
| 994 |
} |
| 995 |
$current_timestamp = time(); |
| 996 |
$time_diff = $current_timestamp - $modified_timestamp; |
| 997 |
// Time difference is only shown up to one week. |
| 998 |
if ( $time_diff >= 0 && $time_diff < WEEK_IN_SECONDS ) { |
| 999 |
$output = sprintf( __( '%s ago', 'default' ), human_time_diff( $modified_timestamp, $current_timestamp ) ); |
| 1000 |
} else { |
| 1001 |
$output = TablePress::format_datetime( $table['last_modified'], '<br />' ); |
| 1002 |
} |
| 1003 |
break; |
| 1004 |
case 'date': |
| 1005 |
$output = TablePress::format_datetime( $table['last_modified'], get_option( 'date_format' ) ); |
| 1006 |
break; |
| 1007 |
case 'time': |
| 1008 |
$output = TablePress::format_datetime( $table['last_modified'], get_option( 'time_format' ) ); |
| 1009 |
break; |
| 1010 |
default: |
| 1011 |
$output = TablePress::format_datetime( $table['last_modified'] ); |
| 1012 |
break; |
| 1013 |
} |
| 1014 |
break; |
| 1015 |
case 'last_editor': |
| 1016 |
$output = TablePress::get_user_display_name( $table['options']['last_editor'] ); |
| 1017 |
break; |
| 1018 |
case 'author': |
| 1019 |
$output = TablePress::get_user_display_name( $table['author'] ); |
| 1020 |
break; |
| 1021 |
case 'number_rows': |
| 1022 |
$output = count( $table['data'] ); |
| 1023 |
if ( 'raw' !== $format ) { |
| 1024 |
$output -= $table['options']['table_head']; |
| 1025 |
$output -= $table['options']['table_foot']; |
| 1026 |
} |
| 1027 |
break; |
| 1028 |
case 'number_columns': |
| 1029 |
$output = count( $table['data'][0] ); |
| 1030 |
break; |
| 1031 |
default: |
| 1032 |
$output = "[table-info field “{$field}” not found in table “{$table_id}” /]<br />\n"; |
| 1033 |
/** |
| 1034 |
* Filters the "table info field not found" message. |
| 1035 |
* |
| 1036 |
* @since 1.0.0 |
| 1037 |
* |
| 1038 |
* @param string $output The "table info field not found" message. |
| 1039 |
* @param array<string, mixed> $table The current table. |
| 1040 |
* @param string $field The field that was not found. |
| 1041 |
* @param string $format The return format for the field. |
| 1042 |
*/ |
| 1043 |
$output = apply_filters( 'tablepress_table_info_not_found_message', $output, $table, $field, $format ); |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** |
| 1047 |
* Filters the output of the [table-info] Shortcode. |
| 1048 |
* |
| 1049 |
* @since 1.0.0 |
| 1050 |
* |
| 1051 |
* @param string $output The output of the [table-info] Shortcode. |
| 1052 |
* @param array<string, mixed> $table The current table. |
| 1053 |
* @param array<string, mixed> $shortcode_atts The attributes passed to the [table-info] Shortcode. |
| 1054 |
*/ |
| 1055 |
$output = apply_filters( 'tablepress_shortcode_table_info_output', $output, $table, $shortcode_atts ); |
| 1056 |
return $output; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/** |
| 1060 |
* Expands the WP Search to also find posts and pages that have a search term in a table that is shown in them. |
| 1061 |
* |
| 1062 |
* This is done by looping through all search terms and TablePress tables and searching there for the search term, |
| 1063 |
* saving all tables's IDs that have a search term and then expanding the WP query to search for posts or pages that have the |
| 1064 |
* Shortcode for one of these tables in their content. |
| 1065 |
* |
| 1066 |
* @since 1.0.0 |
| 1067 |
* |
| 1068 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 1069 |
* |
| 1070 |
* @param string $search_sql Current part of the "WHERE" clause of the SQL statement used to get posts/pages from the WP database that is related to searching. |
| 1071 |
* @return string Eventually extended SQL "WHERE" clause, to also find posts/pages with Shortcodes in them. |
| 1072 |
*/ |
| 1073 |
public function posts_search_filter( /* string */ $search_sql ): string { |
| 1074 |
// Don't use a type hint in the method declaration as there can be cases where `null` is passed to the filter hook callback somehow. |
| 1075 |
|
| 1076 |
global $wpdb; |
| 1077 |
|
| 1078 |
// Protect against cases where `null` is somehow passed to the filter hook callback. |
| 1079 |
if ( ! is_string( $search_sql ) ) { // @phpstan-ignore function.alreadyNarrowedType (The `is_string()` check is needed as the input is coming from a filter hook.) |
| 1080 |
return ''; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if ( ! is_search() || ! is_main_query() ) { |
| 1084 |
return $search_sql; |
| 1085 |
} |
| 1086 |
|
| 1087 |
// Get variable that contains all search terms, parsed from $_GET['s'] by WP. |
| 1088 |
$search_terms = get_query_var( 'search_terms' ); |
| 1089 |
if ( empty( $search_terms ) || ! is_array( $search_terms ) ) { |
| 1090 |
return $search_sql; |
| 1091 |
} |
| 1092 |
|
| 1093 |
// Load all table IDs and prime post meta cache for cached access to options and visibility settings of the tables, don't run filter hook. |
| 1094 |
$table_ids = TablePress::$model_table->load_all( true, false ); |
| 1095 |
// Array of all search words that were found, and the table IDs where they were found. |
| 1096 |
$query_result = array(); |
| 1097 |
|
| 1098 |
$fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos'; |
| 1099 |
|
| 1100 |
foreach ( $table_ids as $table_id ) { |
| 1101 |
// Load table, with table data, options, and visibility settings. |
| 1102 |
$table = TablePress::$model_table->load( $table_id, true, true ); |
| 1103 |
|
| 1104 |
// Skip tables that could not be loaded. |
| 1105 |
if ( is_wp_error( $table ) ) { |
| 1106 |
continue; |
| 1107 |
} |
| 1108 |
|
| 1109 |
// Do not search in corrupted tables. |
| 1110 |
if ( isset( $table['is_corrupted'] ) && $table['is_corrupted'] ) { |
| 1111 |
continue; |
| 1112 |
} |
| 1113 |
|
| 1114 |
foreach ( $search_terms as $search_term ) { |
| 1115 |
if ( ( $table['options']['print_name'] && false !== $fn_stripos( $table['name'], (string) $search_term ) ) |
| 1116 |
|| ( $table['options']['print_description'] && false !== $fn_stripos( $table['description'], (string) $search_term ) ) ) { |
| 1117 |
// Found the search term in the name or description (and they are shown). |
| 1118 |
$query_result[ $search_term ][] = $table_id; // Add table ID to result list. |
| 1119 |
// No need to continue searching this search term in this table. |
| 1120 |
continue; |
| 1121 |
} |
| 1122 |
|
| 1123 |
// Search search term in visible table cells (without taking Shortcode parameters into account!). |
| 1124 |
foreach ( $table['data'] as $row_idx => $table_row ) { |
| 1125 |
if ( 0 === $table['visibility']['rows'][ $row_idx ] ) { |
| 1126 |
// Row is hidden, so don't search in it. |
| 1127 |
continue; |
| 1128 |
} |
| 1129 |
foreach ( $table_row as $col_idx => $table_cell ) { |
| 1130 |
if ( 0 === $table['visibility']['columns'][ $col_idx ] ) { |
| 1131 |
// Column is hidden, so don't search in it. |
| 1132 |
continue; |
| 1133 |
} |
| 1134 |
// @todo Cells are not evaluated here, so math formulas are searched. |
| 1135 |
if ( false !== $fn_stripos( $table_cell, (string) $search_term ) ) { |
| 1136 |
// Found the search term in the cell content. |
| 1137 |
$query_result[ $search_term ][] = $table_id; // Add table ID to result list |
| 1138 |
// No need to continue searching this search term in this table. |
| 1139 |
continue 3; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
// For all found table IDs for each search term, add additional OR statement to the SQL "WHERE" clause. |
| 1147 |
|
| 1148 |
// If $_GET['exact'] is set, WordPress doesn't use % in SQL LIKE clauses. |
| 1149 |
$exact = get_query_var( 'exact' ); |
| 1150 |
$n = ( empty( $exact ) ) ? '%' : ''; |
| 1151 |
$search_sql = $wpdb->remove_placeholder_escape( $search_sql ); |
| 1152 |
foreach ( $query_result as $search_term => $table_ids ) { |
| 1153 |
$search_term = esc_sql( $wpdb->esc_like( $search_term ) ); |
| 1154 |
$old_or = "OR ({$wpdb->posts}.post_content LIKE '{$n}{$search_term}{$n}')"; // @phpstan-ignore encapsedStringPart.nonString (The esc_sql() call above returns a string, as a string is passed.) |
| 1155 |
$table_ids = implode( '|', $table_ids ); |
| 1156 |
$regexp = '\\\\[' . TablePress::$shortcode . ' id=(["\\\']?)(' . $table_ids . ')([\]"\\\' /])'; // ' needs to be single escaped, [ double escaped (with \\) in mySQL |
| 1157 |
$new_or = $old_or . " OR ({$wpdb->posts}.post_content REGEXP '{$regexp}')"; |
| 1158 |
$search_sql = str_replace( $old_or, $new_or, $search_sql ); |
| 1159 |
} |
| 1160 |
$search_sql = $wpdb->add_placeholder_escape( $search_sql ); |
| 1161 |
|
| 1162 |
return $search_sql; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/** |
| 1166 |
* Callback function for rendering the tablepress/table block. |
| 1167 |
* |
| 1168 |
* @since 2.0.0 |
| 1169 |
* |
| 1170 |
* @param array<string, string> $block_attributes List of attributes that where included in the block settings. |
| 1171 |
* @return string Resulting HTML code for the table. |
| 1172 |
*/ |
| 1173 |
public function table_block_render_callback( array $block_attributes ): string { |
| 1174 |
// Don't return anything if no table was selected. |
| 1175 |
if ( '' === $block_attributes['id'] ) { |
| 1176 |
return ''; |
| 1177 |
} |
| 1178 |
|
| 1179 |
$render_attributes = shortcode_parse_atts( $block_attributes['parameters'] ); |
| 1180 |
$render_attributes['id'] = $block_attributes['id']; |
| 1181 |
|
| 1182 |
return $this->shortcode_table( $render_attributes ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
/** |
| 1186 |
* Registers the TablePress Elementor widgets. |
| 1187 |
* |
| 1188 |
* @since 3.1.0 |
| 1189 |
* |
| 1190 |
* @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. |
| 1191 |
*/ |
| 1192 |
public function register_elementor_widgets( \Elementor\Widgets_Manager $widgets_manager ): void { |
| 1193 |
TablePress::load_file( 'class-elementor-widget-table.php', 'classes' ); |
| 1194 |
$widgets_manager->register( new TablePress\Elementor\TablePressTableWidget() ); // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.) |
| 1195 |
} |
| 1196 |
|
| 1197 |
/** |
| 1198 |
* Enqueues the TablePress Elementor Editor CSS styles. |
| 1199 |
* |
| 1200 |
* @since 3.1.0 |
| 1201 |
*/ |
| 1202 |
public function enqueue_elementor_editor_styles(): void { |
| 1203 |
$svg_url = plugins_url( 'admin/img/tablepress-editor-button.svg', TABLEPRESS__FILE__ ); |
| 1204 |
wp_register_style( 'tablepress-elementor', false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 1205 |
wp_add_inline_style( |
| 1206 |
'tablepress-elementor', |
| 1207 |
<<<CSS |
| 1208 |
.elementor-panel .elementor-element .icon:has(> .tablepress-elementor-icon) { |
| 1209 |
height: 43.5px; |
| 1210 |
} |
| 1211 |
.tablepress-elementor-icon { |
| 1212 |
display: inline-block; |
| 1213 |
height: 28px; |
| 1214 |
width: 28px; |
| 1215 |
background-image: url({$svg_url}); |
| 1216 |
background-repeat: no-repeat; |
| 1217 |
background-position: center; |
| 1218 |
background-size: 28px auto; |
| 1219 |
} |
| 1220 |
CSS |
| 1221 |
); |
| 1222 |
wp_enqueue_style( 'tablepress-elementor' ); |
| 1223 |
} |
| 1224 |
|
| 1225 |
} // class TablePress_Frontend_Controller |
| 1226 |
|