PluginProbe
TablePress – Tables in WordPress made easy / 3.0.4
TablePress – Tables in WordPress made easy v3.0.4
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / controllers / controller-frontend.php

controller-frontend.php in TablePress – Tables in WordPress made easy 3.0.4, at controllers/controller-frontend.php

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