| @@ -78,23 +78,19 @@ | ||
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * Filters whether TablePress should load its frontend CSS files on all pages. |
| 81 | 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 | 82 | * |
| 84 | 83 | * @since 3.0.1 |
| 85 | 84 | * |
| 86 | 85 | * @param bool $use_legacy_css_loading Whether TablePress should load its frontend CSS files on all pages. |
| 87 | 86 | */ |
| 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' ) ) ); | |
| 87 | + $this->use_legacy_css_loading = apply_filters( 'tablepress_frontend_legacy_css_loading', ! wp_is_block_theme() ); | |
| 89 | 88 | |
| 90 | 89 | if ( $this->use_legacy_css_loading ) { |
| 91 | 90 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_css' ) ); |
| 92 | 91 | } |
| 93 | 92 | |
| 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 | 93 | add_action( 'wp_print_footer_scripts', array( $this, 'add_datatables_calls' ), 9 ); // Priority 9 so that this runs before `_wp_footer_scripts()`. |
| 98 | 94 | |
| 99 | 95 | // Register TablePress Shortcodes. Priority 20 is kept for backwards-compatibility purposes. |
| 100 | 96 | add_action( 'init', array( $this, 'init_shortcodes' ), 20 ); |
| @@ -118,12 +114,15 @@ | ||
| 118 | 114 | |
| 119 | 115 | /** |
| 120 | 116 | * Register the tablepress/table block and its dependencies. |
| 121 | 117 | */ |
| 122 | - wp_register_block_metadata_collection( | |
| 123 | - TABLEPRESS_ABSPATH . 'blocks', | |
| 124 | - TABLEPRESS_ABSPATH . 'blocks/blocks-manifest.php', | |
| 125 | - ); | |
| 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 | + } | |
| 126 | 125 | register_block_type_from_metadata( |
| 127 | 126 | TABLEPRESS_ABSPATH . 'blocks/table/block.json', |
| 128 | 127 | array( |
| 129 | 128 | 'render_callback' => array( $this, 'table_block_render_callback' ), |
| @@ -128,14 +127,8 @@ | ||
| 128 | 127 | array( |
| 129 | 128 | 'render_callback' => array( $this, 'table_block_render_callback' ), |
| 130 | 129 | ), |
| 131 | 130 | ); |
| 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 | 131 | } |
| 139 | 132 | |
| 140 | 133 | /** |
| 141 | 134 | * Registers TablePress Shortcodes. |
| @@ -323,11 +316,9 @@ | ||
| 323 | 316 | * @param string[] $dependencies The dependencies for the DataTables JS library. |
| 324 | 317 | */ |
| 325 | 318 | $dependencies = apply_filters( 'tablepress_datatables_js_dependencies', $dependencies ); |
| 326 | 319 | |
| 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 ) ); | |
| 320 | + wp_enqueue_script( 'tablepress-datatables', $js_url, $dependencies, TablePress::version, true ); | |
| 330 | 321 | } |
| 331 | 322 | |
| 332 | 323 | /** |
| 333 | 324 | * Adds the JavaScript code for the invocation of the DataTables JS library. |
| @@ -557,21 +548,22 @@ | ||
| 557 | 548 | } // foreach table instance |
| 558 | 549 | } // foreach table ID |
| 559 | 550 | |
| 560 | 551 | // 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"; | |
| 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 | + } | |
| 563 | 558 | |
| 564 | 559 | // DataTables datetime format string handling. |
| 565 | 560 | if ( ! empty( $this->datatables_datetime_formats ) ) { |
| 566 | - // Create a command like `DataTable.datetime("MM/DD/YYYY");DataTable.datetime("DD.MM.YYYY");`. | |
| 561 | + // Create a command like `DataTable.datetime('MM/DD/YYYY');DataTable.datetime('DD.MM.YYYY');`. | |
| 567 | 562 | $datatables_datetime_command = implode( |
| 568 | 563 | '', |
| 569 | 564 | 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 | - }, | |
| 565 | + static fn( string $datetime_format ): string => "DataTable.datetime('{$datetime_format}');", | |
| 574 | 566 | $this->datatables_datetime_formats, |
| 575 | 567 | ) |
| 576 | 568 | ) . "\n"; |
| 577 | 569 | } else { |
| @@ -629,12 +621,14 @@ | ||
| 629 | 621 | * Handles the Shortcode [table id=<ID> /]. |
| 630 | 622 | * |
| 631 | 623 | * @since 1.0.0 |
| 632 | 624 | * |
| 633 | - * @param array<string, mixed> $shortcode_atts List of attributes that where included in the Shortcode. | |
| 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 /]. | |
| 634 | 626 | * @return string Resulting HTML code for the table with the ID <ID>. |
| 635 | 627 | */ |
| 636 | - public function shortcode_table( array $shortcode_atts ): string { | |
| 628 | + public function shortcode_table( /* array|string */ $shortcode_atts ): string { | |
| 629 | + $shortcode_atts = (array) $shortcode_atts; | |
| 630 | + | |
| 637 | 631 | $this->maybe_enqueue_css(); |
| 638 | 632 | |
| 639 | 633 | $_render = TablePress::load_class( 'TablePress_Render', 'class-render.php', 'classes' ); |
| 640 | 634 | |
| @@ -704,26 +698,19 @@ | ||
| 704 | 698 | $message = apply_filters( 'tablepress_table_corrupted_message', $message, $table_id, $table['json_error'] ); |
| 705 | 699 | return $message; |
| 706 | 700 | } |
| 707 | 701 | |
| 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 | - } | |
| 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; | |
| 726 | 713 | } |
| 727 | 714 | |
| 728 | 715 | // Determine options to use (if set in Shortcode, use those, otherwise use stored options, from the "Edit" screen). |
| 729 | 716 | $render_options = array(); |
| @@ -899,9 +886,9 @@ | ||
| 899 | 886 | } |
| 900 | 887 | |
| 901 | 888 | // Maybe print a list of used render options. |
| 902 | 889 | 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 | |
| 890 | + $output .= '<pre>' . var_export( $render_options, true ) . '</pre>'; // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export | |
| 904 | 891 | } |
| 905 | 892 | |
| 906 | 893 | return $output; |
| 907 | 894 | } |
| @@ -910,12 +897,14 @@ | ||
| 910 | 897 | * Handles the Shortcode [table-info id=<ID> field=<name> /]. |
| 911 | 898 | * |
| 912 | 899 | * @since 1.0.0 |
| 913 | 900 | * |
| 914 | - * @param array<string, mixed> $shortcode_atts List of attributes that where included in the Shortcode. | |
| 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 /]. | |
| 915 | 902 | * @return string Text that replaces the Shortcode (error message or asked-for information). |
| 916 | 903 | */ |
| 917 | - public function shortcode_table_info( array $shortcode_atts ): string { | |
| 904 | + public function shortcode_table_info( /* array|string */ $shortcode_atts ): string { | |
| 905 | + $shortcode_atts = (array) $shortcode_atts; | |
| 906 | + | |
| 918 | 907 | // Parse Shortcode attributes, only allow those that are specified. |
| 919 | 908 | $default_shortcode_atts = array( |
| 920 | 909 | 'id' => '', |
| 921 | 910 | 'field' => '', |
| @@ -977,15 +966,8 @@ | ||
| 977 | 966 | switch ( $field ) { |
| 978 | 967 | case 'name': |
| 979 | 968 | case 'description': |
| 980 | 969 | $output = $table[ $field ]; |
| 981 | - // Replace any & with & that is not already an encoded entity (from function htmlentities2 in WP 2.8). | |
| 982 | - // A complete htmlentities2() or htmlspecialchars() would encode <HTML> tags, which we don't want. | |
| 983 | - $output = (string) preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&', $output ); | |
| 984 | - /** This filter is documented in classes/class-render.php */ | |
| 985 | - if ( apply_filters( 'tablepress_apply_nl2br', true, $table_id ) ) { | |
| 986 | - $output = nl2br( $output ); | |
| 987 | - } | |
| 988 | 970 | break; |
| 989 | 971 | case 'last_modified': |
| 990 | 972 | switch ( $format ) { |
| 991 | 973 | case 'raw': |
| @@ -1101,10 +1083,8 @@ | ||
| 1101 | 1083 | $table_ids = TablePress::$model_table->load_all( true, false ); |
| 1102 | 1084 | // Array of all search words that were found, and the table IDs where they were found. |
| 1103 | 1085 | $query_result = array(); |
| 1104 | 1086 | |
| 1105 | - $fn_stripos = function_exists( 'mb_stripos' ) ? 'mb_stripos' : 'stripos'; | |
| 1106 | - | |
| 1107 | 1087 | foreach ( $table_ids as $table_id ) { |
| 1108 | 1088 | // Load table, with table data, options, and visibility settings. |
| 1109 | 1089 | $table = TablePress::$model_table->load( $table_id, true, true ); |
| 1110 | 1090 | |
| @@ -1118,10 +1098,10 @@ | ||
| 1118 | 1098 | continue; |
| 1119 | 1099 | } |
| 1120 | 1100 | |
| 1121 | 1101 | foreach ( $search_terms as $search_term ) { |
| 1122 | - if ( ( $table['options']['print_name'] && false !== $fn_stripos( $table['name'], (string) $search_term ) ) | |
| 1123 | - || ( $table['options']['print_description'] && false !== $fn_stripos( $table['description'], (string) $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 ) ) ) { | |
| 1124 | 1104 | // Found the search term in the name or description (and they are shown). |
| 1125 | 1105 | $query_result[ $search_term ][] = $table_id; // Add table ID to result list. |
| 1126 | 1106 | // No need to continue searching this search term in this table. |
| 1127 | 1107 | continue; |
| @@ -1138,9 +1118,9 @@ | ||
| 1138 | 1118 | // Column is hidden, so don't search in it. |
| 1139 | 1119 | continue; |
| 1140 | 1120 | } |
| 1141 | 1121 | // @todo Cells are not evaluated here, so math formulas are searched. |
| 1142 | - if ( false !== $fn_stripos( $table_cell, (string) $search_term ) ) { | |
| 1122 | + if ( false !== stripos( $table_cell, (string) $search_term ) ) { | |
| 1143 | 1123 | // Found the search term in the cell content. |
| 1144 | 1124 | $query_result[ $search_term ][] = $table_id; // Add table ID to result list |
| 1145 | 1125 | // No need to continue searching this search term in this table. |
| 1146 | 1126 | continue 3; |
| @@ -1182,51 +1162,15 @@ | ||
| 1182 | 1162 | if ( '' === $block_attributes['id'] ) { |
| 1183 | 1163 | return ''; |
| 1184 | 1164 | } |
| 1185 | 1165 | |
| 1186 | - $render_attributes = shortcode_parse_atts( $block_attributes['parameters'] ); | |
| 1166 | + if ( '' !== trim( $block_attributes['parameters'] ) ) { | |
| 1167 | + $render_attributes = shortcode_parse_atts( $block_attributes['parameters'] ); | |
| 1168 | + } else { | |
| 1169 | + $render_attributes = array(); | |
| 1170 | + } | |
| 1187 | 1171 | $render_attributes['id'] = $block_attributes['id']; |
| 1188 | 1172 | |
| 1189 | 1173 | return $this->shortcode_table( $render_attributes ); |
| 1190 | - } | |
| 1191 | - | |
| 1192 | - /** | |
| 1193 | - * Registers the TablePress Elementor widgets. | |
| 1194 | - * | |
| 1195 | - * @since 3.1.0 | |
| 1196 | - * | |
| 1197 | - * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. | |
| 1198 | - */ | |
| 1199 | - public function register_elementor_widgets( \Elementor\Widgets_Manager $widgets_manager ): void { | |
| 1200 | - TablePress::load_file( 'class-elementor-widget-table.php', 'classes' ); | |
| 1201 | - $widgets_manager->register( new TablePress\Elementor\TablePressTableWidget() ); // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.) | |
| 1202 | - } | |
| 1203 | - | |
| 1204 | - /** | |
| 1205 | - * Enqueues the TablePress Elementor Editor CSS styles. | |
| 1206 | - * | |
| 1207 | - * @since 3.1.0 | |
| 1208 | - */ | |
| 1209 | - public function enqueue_elementor_editor_styles(): void { | |
| 1210 | - $svg_url = plugins_url( 'admin/img/tablepress-editor-button.svg', TABLEPRESS__FILE__ ); | |
| 1211 | - wp_register_style( 'tablepress-elementor', false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| 1212 | - wp_add_inline_style( | |
| 1213 | - 'tablepress-elementor', | |
| 1214 | - <<<CSS | |
| 1215 | - .elementor-panel .elementor-element .icon:has(> .tablepress-elementor-icon) { | |
| 1216 | - height: 43.5px; | |
| 1217 | - } | |
| 1218 | - .tablepress-elementor-icon { | |
| 1219 | - display: inline-block; | |
| 1220 | - height: 28px; | |
| 1221 | - width: 28px; | |
| 1222 | - background-image: url({$svg_url}); | |
| 1223 | - background-repeat: no-repeat; | |
| 1224 | - background-position: center; | |
| 1225 | - background-size: 28px auto; | |
| 1226 | - } | |
| 1227 | - CSS | |
| 1228 | - ); | |
| 1229 | - wp_enqueue_style( 'tablepress-elementor' ); | |
| 1230 | 1174 | } |
| 1231 | 1175 | |
| 1232 | 1176 | } // class TablePress_Frontend_Controller |