PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
2.2.14 2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
← All changes | includes/Elementor/TablekitElementorRest.php +59 -37 2.2.82.2.14 View file →
@@ -1,8 +1,7 @@
1 1 <?php
2 -
3 2 /**
4 - * Elementor REST helpers for TableKit.
3 + * Elementor REST helpers for TableKit
5 4 *
6 5 * @package TableKit
7 6 */
8 7
@@ -7,35 +6,47 @@
7 6 */
8 7
9 8 use TableBuilder\Shortcode\ShortcodeUtils;
10 9
11 -if (! defined('ABSPATH')) {
10 +if ( ! defined( 'ABSPATH' ) ) {
12 11 exit;
13 12 }
14 13
15 -class TableKit_Elementor_Rest
16 -{
17 - public static function register(): void
18 - {
19 - add_action('rest_api_init', array(__CLASS__, 'register_rest_routes'));
14 +/**
15 + * Registers TableKit's Elementor-specific REST routes.
16 + */
17 +class TableKit_Elementor_Rest {
18 +
19 + /**
20 + * Hooks REST route registration into rest_api_init.
21 + *
22 + * @return void
23 + */
24 + public static function register(): void {
25 + add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
20 26 }
21 27
22 - public static function register_rest_routes(): void
23 - {
28 + /**
29 + * Registers the tablekit/v1/table-blocks REST route used by the
30 + * Elementor editor's block picker to list table blocks in a post.
31 + *
32 + * @return void
33 + */
34 + public static function register_rest_routes(): void {
24 35 register_rest_route(
25 36 'tablekit/v1',
26 37 '/table-blocks',
27 38 array(
28 39 'methods' => \WP_REST_Server::READABLE,
29 - 'callback' => array(__CLASS__, 'rest_table_blocks'),
40 + 'callback' => array( __CLASS__, 'rest_table_blocks' ),
30 41 'permission_callback' => static function (): bool {
31 - return current_user_can('edit_posts');
42 + return current_user_can( 'edit_posts' );
32 43 },
33 44 'args' => array(
34 45 'table_id' => array(
35 46 'required' => true,
36 - 'validate_callback' => static function ($value): bool {
37 - return is_numeric($value) && (int) $value > 0;
47 + 'validate_callback' => static function ( $value ): bool {
48 + return is_numeric( $value ) && (int) $value > 0;
38 49 },
39 50 'sanitize_callback' => 'absint',
40 51 ),
41 52 ),
@@ -42,55 +53,66 @@
42 53 )
43 54 );
44 55 }
45 56
46 - public static function rest_table_blocks(\WP_REST_Request $request)
47 - {
48 - $table_id = (int) $request->get_param('table_id');
49 - $post = get_post($table_id);
50 - $content = null;
57 + /**
58 + * REST callback: list the table blocks found in a given post (or inline
59 + * table CPT entry), for the Elementor widget's block-picker dropdown.
60 + *
61 + * @param \WP_REST_Request $request Request with a "table_id" param.
62 + * @return \WP_REST_Response|\WP_Error List of {index, label, block_name}, or an error.
63 + */
64 + public static function rest_table_blocks( \WP_REST_Request $request ) {
65 + $table_id = (int) $request->get_param( 'table_id' );
66 + $post = get_post( $table_id );
67 + $content = null;
51 68
52 - if ($post) {
53 - if ('publish' !== $post->post_status && !current_user_can('read_post', $table_id)) {
69 + if ( $post ) {
70 + if ( 'publish' !== $post->post_status && ! current_user_can( 'read_post', $table_id ) ) {
54 71 return new \WP_Error(
55 72 'tablekit_forbidden',
56 - __('You are not allowed to view this table.', 'tablekit'),
57 - array('status' => 403)
73 + __( 'You are not allowed to view this table.', 'table-builder-block' ),
74 + array( 'status' => 403 )
58 75 );
59 76 }
60 77
61 78 $content = $post->post_content;
62 - } elseif (class_exists('\\TableBuilder\\Config\\CPT\\TableCPT') && method_exists('\\TableBuilder\\Config\\CPT\\TableCPT', 'get_inline_table_content')) {
63 - $inline = \TableBuilder\Config\CPT\TableCPT::instance()
64 - ->get_inline_table_content($table_id);
79 + } elseif ( class_exists( '\\TableBuilder\\Config\\CPT\\TableCPT' ) && method_exists( '\\TableBuilder\\Config\\CPT\\TableCPT', 'get_inline_table_content' ) ) {
80 + $inline = \TableBuilder\Config\CPT\TableCPT::instance()
81 + ->get_inline_table_content( $table_id );
65 82 $content = $inline ?? null;
66 83 }
67 84
68 - if (null === $content) {
85 + if ( null === $content ) {
69 86 return new \WP_Error(
70 87 'tablekit_not_found',
71 - __('Table not found.', 'tablekit'),
72 - array('status' => 404)
88 + __( 'Table not found.', 'table-builder-block' ),
89 + array( 'status' => 404 )
73 90 );
74 91 }
75 92
76 - $blocks = parse_blocks($content);
77 - $table_blocks = self::filter_table_blocks($blocks);
93 + $blocks = parse_blocks( $content );
94 + $table_blocks = self::filter_table_blocks( $blocks );
78 95 $result = array();
79 96
80 - foreach ($table_blocks as $index => $block) {
97 + foreach ( $table_blocks as $index => $block ) {
81 98 $block_name = $block['blockName'] ?? '';
82 99 $result[] = array(
83 100 'index' => $index,
84 - 'label' => ShortcodeUtils::get_block_label($block_name),
101 + 'label' => ShortcodeUtils::get_block_label( $block_name ),
85 102 'block_name' => $block_name,
86 103 );
87 104 }
88 105
89 - return rest_ensure_response($result);
106 + return rest_ensure_response( $result );
90 107 }
91 108
92 - private static function filter_table_blocks(array $blocks): array
93 - {
94 - return ShortcodeUtils::filter_table_blocks($blocks);
109 + /**
110 + * Recursively filters a parsed block tree down to table blocks only.
111 + *
112 + * @param array $blocks Parsed block tree, as returned by parse_blocks().
113 + * @return array Matching table blocks.
114 + */
115 + private static function filter_table_blocks( array $blocks ): array {
116 + return ShortcodeUtils::filter_table_blocks( $blocks );
95 117 }
96 118 }