PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.13
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.13
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
table-builder-block / includes / Elementor / TablekitElementorRest.php

TablekitElementorRest.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.13, at includes/Elementor/TablekitElementorRest.php

119 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Elementor REST helpers for TableKit
4 *
5 * @package TableKit
6 */
7
8 use TableBuilder\Shortcode\ShortcodeUtils;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
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' ) );
26 }
27
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 {
35 register_rest_route(
36 'tablekit/v1',
37 '/table-blocks',
38 array(
39 'methods' => \WP_REST_Server::READABLE,
40 'callback' => array( __CLASS__, 'rest_table_blocks' ),
41 'permission_callback' => static function (): bool {
42 return current_user_can( 'edit_posts' );
43 },
44 'args' => array(
45 'table_id' => array(
46 'required' => true,
47 'validate_callback' => static function ( $value ): bool {
48 return is_numeric( $value ) && (int) $value > 0;
49 },
50 'sanitize_callback' => 'absint',
51 ),
52 ),
53 )
54 );
55 }
56
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;
68
69 if ( $post ) {
70 if ( 'publish' !== $post->post_status && ! current_user_can( 'read_post', $table_id ) ) {
71 return new \WP_Error(
72 'tablekit_forbidden',
73 __( 'You are not allowed to view this table.', 'table-builder-block' ),
74 array( 'status' => 403 )
75 );
76 }
77
78 $content = $post->post_content;
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 );
82 $content = $inline ?? null;
83 }
84
85 if ( null === $content ) {
86 return new \WP_Error(
87 'tablekit_not_found',
88 __( 'Table not found.', 'table-builder-block' ),
89 array( 'status' => 404 )
90 );
91 }
92
93 $blocks = parse_blocks( $content );
94 $table_blocks = self::filter_table_blocks( $blocks );
95 $result = array();
96
97 foreach ( $table_blocks as $index => $block ) {
98 $block_name = $block['blockName'] ?? '';
99 $result[] = array(
100 'index' => $index,
101 'label' => ShortcodeUtils::get_block_label( $block_name ),
102 'block_name' => $block_name,
103 );
104 }
105
106 return rest_ensure_response( $result );
107 }
108
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 );
117 }
118 }
119