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
← All changes | classes/class-tablepress.php +293 -17 2.43.0.4 View file →
@@ -26,9 +26,9 @@
26 26 *
27 27 * @since 1.0.0
28 28 * @const string
29 29 */
30 - public const version = '2.4'; // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
30 + public const version = '3.0.4'; // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
31 31
32 32 /**
33 33 * TablePress internal plugin version ("options scheme" version).
34 34 *
@@ -36,9 +36,9 @@
36 36 *
37 37 * @since 1.0.0
38 38 * @const int
39 39 */
40 - public const db_version = 85; // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
40 + public const db_version = 100; // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
41 41
42 42 /**
43 43 * TablePress "table scheme" (data format structure) version.
44 44 *
@@ -53,27 +53,24 @@
53 53 /**
54 54 * Instance of the Options Model.
55 55 *
56 56 * @since 1.3.0
57 - * @var TablePress_Options_Model
58 57 */
59 - public static $model_options;
58 + public static \TablePress_Options_Model $model_options;
60 59
61 60 /**
62 61 * Instance of the Table Model.
63 62 *
64 63 * @since 1.3.0
65 - * @var TablePress_Table_Model
66 64 */
67 - public static $model_table;
65 + public static \TablePress_Table_Model $model_table;
68 66
69 67 /**
70 68 * Instance of the controller.
71 69 *
72 70 * @since 1.0.0
73 - * @var TablePress_Frontend_Controller
74 71 */
75 - public static $controller;
72 + public static \TablePress_Frontend_Controller $controller;
76 73
77 74 /**
78 75 * Name of the Shortcode to show a TablePress table.
79 76 *
@@ -79,11 +76,10 @@
79 76 *
80 77 * Should only be modified through the filter hook 'tablepress_table_shortcode'.
81 78 *
82 79 * @since 1.0.0
83 - * @var string
84 80 */
85 - public static $shortcode = 'table';
81 + public static string $shortcode = 'table';
86 82
87 83 /**
88 84 * Name of the Shortcode to show extra information of a TablePress table.
89 85 *
@@ -89,11 +85,10 @@
89 85 *
90 86 * Should only be modified through the filter hook 'tablepress_table_info_shortcode'.
91 87 *
92 88 * @since 1.0.0
93 - * @var string
94 89 */
95 - public static $shortcode_info = 'table-info';
90 + public static string $shortcode_info = 'table-info';
96 91
97 92 /**
98 93 * List of TablePress premium modules.
99 94 *
@@ -99,9 +94,9 @@
99 94 *
100 95 * @since 2.1.0
101 96 * @var array<string, array{name: string, description: string, category: string, class: string, incompatible_classes: string[], minimum_plan: string, default_active: bool}>
102 97 */
103 - public static $modules = array();
98 + public static array $modules = array();
104 99
105 100 /**
106 101 * Start-up TablePress (run on WordPress "init") and load the controller for the current state.
107 102 *
@@ -165,8 +160,13 @@
165 160 }
166 161 // Load the frontend controller in all scenarios, so that Shortcode render functions are always available.
167 162 self::$controller = self::load_controller( 'frontend' );
168 163
164 + // Add filters and actions for the integration into the WP WXR exporter and importer.
165 + add_action( 'wp_import_insert_post', array( TablePress::$model_table, 'add_table_id_on_wp_import' ), 10, 4 ); // phpcs:ignore Squiz.Classes.SelfMemberReference.NotUsed
166 + add_filter( 'wp_import_post_meta', array( TablePress::$model_table, 'prevent_table_id_post_meta_import_on_wp_import' ), 10, 3 ); // phpcs:ignore Squiz.Classes.SelfMemberReference.NotUsed
167 + add_filter( 'wxr_export_skip_postmeta', array( TablePress::$model_table, 'add_table_id_to_wp_export' ), 10, 3 ); // phpcs:ignore Squiz.Classes.SelfMemberReference.NotUsed
168 +
169 169 /**
170 170 * Fires after TablePress is loaded.
171 171 *
172 172 * The `tablepress_run` action hook can be used if code has to run before TablePress is loaded.
@@ -328,13 +328,14 @@
328 328 * @param string $column Column string.
329 329 * @return int Column number, 1-based.
330 330 */
331 331 public static function letter_to_number( string $column ): int {
332 + $column = (string) preg_replace( '/[^A-Za-z]/', '', $column );
332 333 $column = strtoupper( $column );
333 334 $count = strlen( $column );
334 335 $number = 0;
335 336 for ( $i = 0; $i < $count; $i++ ) {
336 - $number += ( ord( $column[ $count - 1 - $i ] ) - 64 ) * pow( 26, $i );
337 + $number += ( ord( $column[ $count - 1 - $i ] ) - 64 ) * 26 ** $i;
337 338 }
338 339 return $number;
339 340 }
340 341
@@ -400,9 +401,9 @@
400 401 * @return string Nickname of the WP user with the $user_id.
401 402 */
402 403 public static function get_user_display_name( int $user_id ): string {
403 404 $user = get_userdata( $user_id );
404 - return ( isset( $user->display_name ) ) ? $user->display_name : sprintf( '<em>%s</em>', __( 'unknown', 'tablepress' ) );
405 + return $user->display_name ?? sprintf( '<em>%s</em>', __( 'unknown', 'tablepress' ) );
405 406 }
406 407
407 408 /**
408 409 * Sanitizes a CSS class to ensure it only contains valid characters.
@@ -423,8 +424,256 @@
423 424 return $sanitized_css_class;
424 425 }
425 426
426 427 /**
428 + * Extracts the top-level keys from a JavaScript object string.
429 + *
430 + * This function is used to extract the keys of the "Custom Commands" JavaScript object string, to check for overrides.
431 + * It covers most cases, like normal object properties with and without quotes, shorthand properties, and shorthand methods,
432 + * and also ignores single-line and multi-line comments.
433 + * It does not cover all possible JavaScript syntax (like template literals, special characters, ...),
434 + * but should be sufficient for the use case.
435 + *
436 + * @since 3.0.0
437 + *
438 + * @param string $js_object_string A JavaScript object as a string.
439 + * @return string[] Array of top-level keys of the object.
440 + */
441 + public static function extract_keys_from_js_object_string( string $js_object_string ): array {
442 + $object_keys = array();
443 + $length = strlen( $js_object_string );
444 + $depth = 0;
445 + $key_expected = true;
446 + $in_quotes = false;
447 + $quote_char = '';
448 + $in_function_declaration = false;
449 + $in_single_line_comment = false;
450 + $in_multi_line_comment = false;
451 + $object_key = '';
452 +
453 + for ( $i = 0; $i < $length; $i++ ) {
454 + $char = $js_object_string[ $i ];
455 +
456 + // Skip parsing single-line comments.
457 + if ( $in_single_line_comment ) {
458 + if ( "\n" === $char ) {
459 + $in_single_line_comment = false;
460 + }
461 + continue;
462 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
463 + if ( '/' === $char && $i + 1 < $length && '/' === $js_object_string[ $i + 1 ] ) {
464 + $in_single_line_comment = true;
465 + ++$i; // Skip the second '/'.
466 + continue;
467 + }
468 + }
469 +
470 + // Skip parsing multi-line comments.
471 + if ( $in_multi_line_comment ) {
472 + if ( '*' === $char && $i + 1 < $length && '/' === $js_object_string[ $i + 1 ] ) {
473 + $in_multi_line_comment = false;
474 + ++$i; // Skip the '/' that ends the multi-line comment.
475 + }
476 + continue;
477 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
478 + if ( '/' === $char && $i + 1 < $length && '*' === $js_object_string[ $i + 1 ] ) {
479 + $in_multi_line_comment = true;
480 + ++$i; // Skip the '*'.
481 + continue;
482 + }
483 + }
484 +
485 + // Skip parsing while inside a quoted string.
486 + if ( $in_quotes ) {
487 + if ( $quote_char === $char ) {
488 + $in_quotes = false;
489 + }
490 + continue;
491 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
492 + if ( '"' === $char || "'" === $char ) {
493 + $in_quotes = true;
494 + $quote_char = $char;
495 + continue;
496 + }
497 + }
498 +
499 + /*
500 + * Skip parsing while inside a `function abc( ... )` declaration string.
501 + * The `$key_expected` check limits search the "function" string to object values.
502 + * The check for the plain `f` reduces expensive `substr()` calls.
503 + */
504 + if ( ! $key_expected ) {
505 + if ( $in_function_declaration ) {
506 + if ( ')' === $char ) {
507 + $in_function_declaration = false;
508 + }
509 + continue;
510 + } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
511 + if ( 'f' === $char && 'function' === substr( $js_object_string, $i, 8 ) ) {
512 + $in_function_declaration = true;
513 + $i += 7; // Skip the rest of the "function" string.
514 + continue;
515 + }
516 + }
517 + }
518 +
519 + // Handle object depth, so that most parsing can be limited to the top level.
520 + if ( '{' === $char || '[' === $char ) {
521 + ++$depth;
522 + }
523 +
524 + // Extract only keys at the top level.
525 + if ( 1 === $depth ) {
526 + if ( $key_expected ) {
527 + if ( ':' === $char ) {
528 + // Check for normal keys, with value after :.
529 +
530 + // Go backwards to find the start of the key.
531 + $j = $i - 1;
532 + while ( $j >= 0 && preg_match( '/\s/', $js_object_string[ $j ] ) ) {
533 + --$j;
534 + }
535 + $key_end = $j; // Position of the last character of the key (potentially with quote).
536 + if ( '"' === $js_object_string[ $j ] || "'" === $js_object_string[ $j ] ) {
537 + // Quoted key.
538 + $quote_char = $js_object_string[ $j ];
539 + --$j;
540 + while ( $j >= 0 && $quote_char !== $js_object_string[ $j ] ) {
541 + --$j;
542 + }
543 + $key_start = $j + 1;
544 + } else {
545 + // Unquoted key.
546 + while ( $j >= 0 && preg_match( '/[\w]/', $js_object_string[ $j ] ) ) {
547 + --$j;
548 + }
549 + $key_start = $j + 1;
550 + }
551 + $object_key = substr( $js_object_string, $key_start, $key_end - $key_start + 1 );
552 + $object_key = trim( $object_key, "\"'" );
553 + if ( '' !== $object_key && ! in_array( $object_key, $object_keys, true ) ) {
554 + $object_keys[] = $object_key;
555 + }
556 + $key_expected = false;
557 + } elseif ( ( ',' === $char || '}' === $char ) ) { // The `}` case is for the last key.
558 + // Check for shorthand properties (which must be unquoted).
559 +
560 + // Go backwards to find the start of the shorthand key.
561 + $j = $i - 1;
562 + while ( $j >= 0 && preg_match( '/\s/', $js_object_string[ $j ] ) ) {
563 + --$j;
564 + }
565 + $key_end = $j; // Position of the last character of the key (without a quote).
566 + while ( $j >= 0 && preg_match( '/[\w]/', $js_object_string[ $j ] ) ) {
567 + --$j;
568 + }
569 + $key_start = $j + 1;
570 + $object_key = substr( $js_object_string, $key_start, $key_end - $key_start + 1 );
571 + if ( '' !== $object_key && ! in_array( $object_key, $object_keys, true ) ) {
572 + $object_keys[] = $object_key;
573 + }
574 + } elseif ( '(' === $char ) {
575 + // Detect shorthand method definitions.
576 +
577 + // Go back to find the start of the method name.
578 + $j = $i - 1;
579 + while ( $j >= 0 && preg_match( '/\s/', $js_object_string[ $j ] ) ) {
580 + --$j;
581 + }
582 + $key_end = $j;
583 + while ( $j >= 0 && preg_match( '/[\w]/', $js_object_string[ $j ] ) ) {
584 + --$j;
585 + }
586 + $key_start = $j + 1;
587 + $object_key = substr( $js_object_string, $key_start, $key_end - $key_start + 1 );
588 + if ( '' !== $object_key && ! in_array( $object_key, $object_keys, true ) ) {
589 + $object_keys[] = $object_key;
590 + }
591 + }
592 + }
593 +
594 + // Reset the "key expected" flag after a comma or closing brace.
595 + if ( ',' === $char || '}' === $char ) {
596 + $key_expected = true;
597 + }
598 + }
599 +
600 + // Handle object depth.
601 + if ( '}' === $char || ']' === $char ) {
602 + --$depth;
603 + }
604 + }
605 +
606 + return $object_keys;
607 + }
608 +
609 + /**
610 + * Converts old DataTables 1.x CSS classes and parameters to the DataTables 2 variants.
611 + *
612 + * This function is used to modernize "Custom CSS" and "Custom Commands" for compatibility with DataTables 2.x.
613 + * It probably does not catch all possible cases.
614 + *
615 + * @since 3.0.0
616 + *
617 + * @param string $code Code that contains DataTables 1.x CSS classes and parameters.
618 + * @return string Updated code with DataTables 2.x CSS classes and parameters.
619 + */
620 + public static function convert_datatables_api_data( string $code ): string {
621 + /**
622 + * Mappings for DataTables 1.x CSS class or parameter to DataTables 2 variants.
623 + * As this array is used in `strtr()`, it's pre-sorted for descending string length of the array keys.
624 + */
625 + static $datatables_api_data_mappings = array(
626 + // CSS classes.
627 + '.tablepress thead .sorting:hover' => '.tablepress thead .dt-orderable-asc:hover,.tablepress thead .dt-orderable-desc:hover',
628 + '.tablepress thead .sorting_desc' => '.tablepress thead .dt-ordering-desc',
629 + '.dataTables_filter label input' => '.dt-container .dt-search input',
630 + '.tablepress thead .sorting_asc' => '.tablepress thead .dt-ordering-asc',
631 + '.dataTables_scrollFootInner' => '.dt-scroll-footInner',
632 + '.dataTables_scrollHeadInner' => '.dt-scroll-headInner',
633 + '.tablepress thead .sorting' => '.tablepress thead .dt-orderable-asc,.tablepress thead .dt-orderable-desc',
634 + '.dataTables_processing' => '.dt-processing',
635 + '.dataTables_scrollBody' => '.dt-scroll-body',
636 + '.dataTables_scrollFoot' => '.dt-scroll-foot',
637 + '.dataTables_scrollHead' => '.dt-scroll-head',
638 + '.dataTables_paginate' => '.dt-paging',
639 + '.tablepress .even td' => '.tablepress>:where(tbody.row-striping)>:nth-child(odd)>*',
640 + '.dataTables_wrapper' => '.dt-container',
641 + '.tablepress .odd td' => '.tablepress>:where(tbody.row-striping)>:nth-child(even)>*',
642 + '.dataTables_filter' => '.dt-search',
643 + '.dataTables_length' => '.dt-length',
644 + '.dataTables_scroll' => '.dt-scroll',
645 + '.dataTables_empty' => '.dt-empty',
646 + '.dataTables_info' => '.dt-info',
647 + '.paginate_button' => '.dt-paging-button',
648 + // DataTables API functions.
649 + '$.fn.dataTable.' => 'DataTable.',
650 + );
651 + $code = strtr( $code, $datatables_api_data_mappings );
652 +
653 + // HTML ID mappings, which were removed.
654 + if ( str_contains( $code, '#tablepress-' ) ) {
655 + $code = (string) preg_replace(
656 + array(
657 + '/#tablepress-([A-Za-z1-9_-]|[A-Za-z0-9_-]{2,})_paginate/',
658 + '/#tablepress-([A-Za-z1-9_-]|[A-Za-z0-9_-]{2,})_filter/',
659 + '/#tablepress-([A-Za-z1-9_-]|[A-Za-z0-9_-]{2,})_length/',
660 + '/#tablepress-([A-Za-z1-9_-]|[A-Za-z0-9_-]{2,})_info/',
661 + ),
662 + array(
663 + '#tablepress-$1_wrapper .dt-paging',
664 + '#tablepress-$1_wrapper .dt-search',
665 + '#tablepress-$1_wrapper .dt-length',
666 + '#tablepress-$1_wrapper .dt-info',
667 + ),
668 + $code,
669 + );
670 + }
671 +
672 + return $code;
673 + }
674 +
675 + /**
427 676 * Retrieves all information of a WP_Error object as a string.
428 677 *
429 678 * @since 1.4.0
430 679 *
@@ -636,9 +885,9 @@
636 885 'minimum_plan' => 'pro',
637 886 'default_active' => false,
638 887 ),
639 888 'datatables-buttons' => array(
640 - 'name' => __( 'Buttons', 'tablepress' ),
889 + 'name' => __( 'User Action Buttons', 'tablepress' ),
641 890 'description' => __( 'Add buttons for downloading, copying, printing, and changing column visibility of tables.', 'tablepress' ),
642 891 'category' => 'frontend',
643 892 'class' => 'TablePress_Module_DataTables_Buttons',
644 893 'incompatible_classes' => array( 'TablePress_DataTables_Buttons' ),
@@ -663,9 +912,9 @@
663 912 'minimum_plan' => 'pro',
664 913 'default_active' => false,
665 914 ),
666 915 'datatables-counter-column' => array(
667 - 'name' => __( 'Counter Column', 'tablepress' ),
916 + 'name' => __( 'Index Column', 'tablepress' ),
668 917 'description' => __( 'Make the first column an index or counter column with the row position.', 'tablepress' ),
669 918 'category' => 'frontend',
670 919 'class' => 'TablePress_Module_DataTables_Counter_Column',
671 920 'incompatible_classes' => array(),
@@ -683,8 +932,17 @@
683 932 ),
684 933 'minimum_plan' => 'pro',
685 934 'default_active' => true,
686 935 ),
936 + 'datatables-layout' => array(
937 + 'name' => __( 'Table Layout', 'tablepress' ),
938 + 'description' => __( 'Customize the layout and position of features around a table.', 'tablepress' ),
939 + 'category' => 'frontend',
940 + 'class' => 'TablePress_Module_DataTables_Layout',
941 + 'incompatible_classes' => array(),
942 + 'minimum_plan' => 'pro',
943 + 'default_active' => true,
944 + ),
687 945 'datatables-fuzzysearch' => array(
688 946 'name' => __( 'Fuzzy Search', 'tablepress' ),
689 947 'description' => __( 'Let the search account for spelling mistakes and typos and find similar matches.', 'tablepress' ),
690 948 'category' => 'search-filter',
@@ -690,8 +948,26 @@
690 948 'category' => 'search-filter',
691 949 'class' => 'TablePress_Module_DataTables_FuzzySearch',
692 950 'incompatible_classes' => array(),
693 951 'minimum_plan' => 'max',
952 + 'default_active' => false,
953 + ),
954 + 'datatables-inverted-filter' => array(
955 + 'name' => __( 'Inverted Filtering', 'tablepress' ),
956 + 'description' => __( 'Turn the filtering into a search and hide the table if no search term is entered.', 'tablepress' ),
957 + 'category' => 'search-filter',
958 + 'class' => 'TablePress_Module_DataTables_Inverted_Filter',
959 + 'incompatible_classes' => array(),
960 + 'minimum_plan' => 'max',
961 + 'default_active' => false,
962 + ),
963 + 'datatables-pagination' => array(
964 + 'name' => __( 'Advanced Pagination Settings', 'tablepress' ),
965 + 'description' => __( 'Customize the pagination settings of the table.', 'tablepress' ),
966 + 'category' => 'frontend',
967 + 'class' => 'TablePress_Module_DataTables_Pagination',
968 + 'incompatible_classes' => array(),
969 + 'minimum_plan' => 'pro',
694 970 'default_active' => false,
695 971 ),
696 972 'datatables-rowgroup' => array(
697 973 'name' => __( 'Row Grouping', 'tablepress' ),