@times-design-system/components-wordpress 1.8.0 → 1.8.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +44 -0
  2. package/dist/blocks/ad-container/block.json +3 -1
  3. package/dist/blocks/button/block.json +5 -6
  4. package/dist/blocks/button/edit.js +15 -21
  5. package/dist/blocks/button/render.php +43 -46
  6. package/dist/blocks/button/save.js +13 -19
  7. package/dist/blocks/button/style.css +8 -25
  8. package/dist/blocks/chip/block.json +7 -3
  9. package/dist/blocks/chip/render.php +1 -1
  10. package/dist/blocks/chip/style.css +3 -3
  11. package/dist/blocks/dialog-box/block.json +2 -1
  12. package/dist/blocks/divider/block.json +3 -1
  13. package/dist/blocks/flag/block.json +7 -3
  14. package/dist/blocks/flag/style.css +19 -22
  15. package/dist/blocks/icon/block.json +3 -2
  16. package/dist/blocks/icon/render.php +1 -1
  17. package/dist/blocks/icon-button/block.json +5 -2
  18. package/dist/blocks/icon-button/render.php +3 -2
  19. package/dist/blocks/icon-button/style.css +9 -8
  20. package/dist/blocks/input/block.json +3 -1
  21. package/dist/blocks/input/style.css +31 -17
  22. package/dist/blocks/input-helper-message/block.json +2 -1
  23. package/dist/blocks/link/block.json +7 -3
  24. package/dist/blocks/link/style.css +123 -21
  25. package/dist/blocks/tab/block.json +2 -1
  26. package/dist/blocks/tab/style.css +146 -17
  27. package/dist/blocks/tab-group/block.json +2 -1
  28. package/dist/blocks/text/block.json +3 -1
  29. package/dist/blocks/toast/block.json +3 -1
  30. package/dist/blocks/toast/style.css +32 -31
  31. package/dist/blocks/utils/render-icon.php +39 -0
  32. package/dist/blocks/utils/sanitize-data-attributes.php +37 -0
  33. package/dist/icon-data.json +3895 -1952
  34. package/dist/index.cjs +997 -278
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.css +1 -1
  37. package/dist/index.css.map +1 -1
  38. package/dist/index.js +998 -279
  39. package/dist/index.js.map +1 -1
  40. package/dist/plugin.php +162 -12
  41. package/dist/utils/render-icon.php +32 -6
  42. package/package.json +9 -6
  43. package/plugin.php +162 -12
  44. package/dist/blocks/button/style-editor.css +0 -15
package/dist/plugin.php CHANGED
@@ -22,26 +22,81 @@ if ( ! defined( 'ABSPATH' ) ) {
22
22
  }
23
23
 
24
24
  /**
25
- * Register all blocks from the block.json manifest.
25
+ * Register all Times Design System blocks.
26
+ * Uses PHP-side registration with render callbacks pointing to render.php files.
26
27
  *
27
28
  * @since 1.1.0
28
29
  * @return void
29
30
  */
30
31
  function tds_register_blocks() {
32
+ $plugin_dir = plugin_dir_url( __FILE__ );
31
33
  $blocks_dir = plugin_dir_path( __FILE__ ) . 'blocks/';
32
34
 
33
35
  if ( ! is_dir( $blocks_dir ) ) {
36
+ error_log( 'TDS: blocks directory not found: ' . $blocks_dir );
34
37
  return;
35
38
  }
36
39
 
37
- // Register each block found in the blocks directory
40
+ // Get all block directories
38
41
  $block_dirs = glob( $blocks_dir . '*', GLOB_ONLYDIR );
42
+
43
+ if ( empty( $block_dirs ) ) {
44
+ error_log( 'TDS: no block directories found' );
45
+ return;
46
+ }
39
47
 
40
48
  foreach ( $block_dirs as $block_dir ) {
41
- $block_json = $block_dir . '/block.json';
49
+ $block_json_path = $block_dir . '/block.json';
50
+ $render_php_path = $block_dir . '/render.php';
51
+ $block_name = basename( $block_dir );
52
+
53
+ if ( ! file_exists( $block_json_path ) ) {
54
+ error_log( 'TDS: block.json not found: ' . $block_json_path );
55
+ continue;
56
+ }
57
+
58
+ if ( ! file_exists( $render_php_path ) ) {
59
+ error_log( 'TDS: render.php not found: ' . $render_php_path );
60
+ continue;
61
+ }
42
62
 
43
- if ( file_exists( $block_json ) ) {
44
- register_block_type( $block_json );
63
+ // Read block.json for metadata
64
+ $block_json = file_get_contents( $block_json_path );
65
+ $block_config = json_decode( $block_json, true );
66
+
67
+ if ( ! $block_config || ! isset( $block_config['name'] ) ) {
68
+ error_log( 'TDS: Invalid block.json in ' . $block_name );
69
+ continue;
70
+ }
71
+
72
+ $block_type = $block_config['name'];
73
+
74
+ // Create render callback closure that includes the render.php file
75
+ $render_callback = function( $attributes, $content, $block ) use ( $render_php_path ) {
76
+ ob_start();
77
+ include $render_php_path;
78
+ return ob_get_clean();
79
+ };
80
+
81
+ // Register block with PHP-side render callback
82
+ $result = register_block_type(
83
+ $block_type,
84
+ array(
85
+ 'render_callback' => $render_callback,
86
+ // Merge in other config from block.json
87
+ 'attributes' => $block_config['attributes'] ?? array(),
88
+ 'supports' => $block_config['supports'] ?? array(),
89
+ 'category' => $block_config['category'] ?? 'common',
90
+ 'title' => $block_config['title'] ?? ucwords( str_replace( '-', ' ', $block_name ) ),
91
+ 'description' => $block_config['description'] ?? '',
92
+ 'icon' => $block_config['icon'] ?? 'admin-generic',
93
+ )
94
+ );
95
+
96
+ if ( is_wp_error( $result ) ) {
97
+ error_log( 'TDS: Failed to register ' . $block_type . ': ' . $result->get_error_message() );
98
+ } else {
99
+ error_log( 'TDS: Registered ' . $block_type );
45
100
  }
46
101
  }
47
102
  }
@@ -49,22 +104,117 @@ function tds_register_blocks() {
49
104
  add_action( 'init', 'tds_register_blocks' );
50
105
 
51
106
  /**
52
- * Enqueue plugin styles and scripts.
107
+ * Get cache-busting version for assets.
108
+ * Uses file modification time to automatically invalidate cache when files change.
109
+ *
110
+ * @since 1.1.0
111
+ * @param string $file_path Path to the file in plugin directory.
112
+ * @return string Version string (either mtime hash or fallback)
113
+ */
114
+ function tds_get_asset_version( $file_path ) {
115
+ $full_path = plugin_dir_path( __FILE__ ) . $file_path;
116
+ if ( file_exists( $full_path ) ) {
117
+ return 'v' . filemtime( $full_path );
118
+ }
119
+ return '1.1.0'; // Fallback version
120
+ }
121
+
122
+ /**
123
+ * Enqueue plugin styles.
53
124
  *
54
125
  * @since 1.1.0
55
126
  * @return void
56
127
  */
57
128
  function tds_enqueue_assets() {
58
- // Enqueue theme-scss styles if available
59
- $stylesheet_handle = 'times-design-system-theme';
129
+ $plugin_dir = plugin_dir_url( __FILE__ );
60
130
 
61
- if ( wp_style_is( $stylesheet_handle, 'registered' ) || wp_style_is( $stylesheet_handle, 'enqueued' ) ) {
62
- wp_enqueue_style( $stylesheet_handle );
63
- }
131
+ // Enqueue block styles
132
+ wp_enqueue_style(
133
+ 'times-design-system-blocks',
134
+ $plugin_dir . 'index.css',
135
+ array(),
136
+ tds_get_asset_version( 'index.css' )
137
+ );
64
138
  }
65
139
 
66
140
  add_action( 'wp_enqueue_scripts', 'tds_enqueue_assets' );
67
- add_action( 'admin_enqueue_scripts', 'tds_enqueue_assets' );
141
+
142
+ /**
143
+ * Register Times Design System admin page in the menu.
144
+ *
145
+ * @since 1.1.0
146
+ * @return void
147
+ */
148
+ function tds_register_admin_menu() {
149
+ add_menu_page(
150
+ __( 'TDS Blocks', 'times-design-system' ),
151
+ __( 'TDS Blocks', 'times-design-system' ),
152
+ 'manage_options',
153
+ 'tds-blocks-preview',
154
+ 'tds_render_admin_page',
155
+ 'dashicons-admin-generic',
156
+ 65
157
+ );
158
+ }
159
+
160
+ add_action( 'admin_menu', 'tds_register_admin_menu' );
161
+
162
+ /**
163
+ * Render the TDS blocks admin page.
164
+ *
165
+ * @since 1.1.0
166
+ * @return void
167
+ */
168
+ function tds_render_admin_page() {
169
+ require_once plugin_dir_path( __FILE__ ) . 'admin/page.php';
170
+ }
171
+
172
+ /**
173
+ * AJAX handler to preview a block with given attributes.
174
+ *
175
+ * @since 1.1.0
176
+ * @return void
177
+ */
178
+ function tds_ajax_preview_block() {
179
+ // Verify nonce
180
+ $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
181
+ if ( ! wp_verify_nonce( $nonce, 'tds_preview_nonce' ) ) {
182
+ wp_die( 'Security check failed' );
183
+ }
184
+
185
+ // Get block type and attributes
186
+ $block_type = isset( $_POST['block_type'] ) ? sanitize_text_field( wp_unslash( $_POST['block_type'] ) ) : '';
187
+ $attributes_json = isset( $_POST['attributes'] ) ? sanitize_text_field( wp_unslash( $_POST['attributes'] ) ) : '{}';
188
+ $attributes = json_decode( $attributes_json, true ) ?: array();
189
+
190
+ if ( ! $block_type || strpos( $block_type, 'tds/' ) !== 0 ) {
191
+ wp_die( 'Invalid block type' );
192
+ }
193
+
194
+ // Get the block type object
195
+ $registry = WP_Block_Type_Registry::get_instance();
196
+ $block = $registry->get_registered( $block_type );
197
+
198
+ if ( ! $block || ! $block->render_callback ) {
199
+ wp_die( 'Block not found or has no render callback' );
200
+ }
201
+
202
+ // Call render callback
203
+ try {
204
+ if ( is_callable( $block->render_callback ) ) {
205
+ $output = call_user_func( $block->render_callback, $attributes, '', null );
206
+ } else {
207
+ throw new Exception( 'Invalid render callback' );
208
+ }
209
+ echo wp_kses_post( $output );
210
+ } catch ( Exception $e ) {
211
+ echo '<p style="color: red;">Error rendering block: ' . esc_html( $e->getMessage() ) . '</p>';
212
+ }
213
+
214
+ wp_die();
215
+ }
216
+
217
+ add_action( 'wp_ajax_tds_preview_block', 'tds_ajax_preview_block' );
68
218
 
69
219
  // Activation hook
70
220
  register_activation_hook( __FILE__, function() {
@@ -55,9 +55,14 @@ function tds_load_icon_data() {
55
55
  }
56
56
 
57
57
  /**
58
- * Get icon SVG by name
58
+ * Get icon SVG by name and optional variant
59
59
  *
60
- * @param string $icon_name Icon name (e.g., 'arrow-left', 'check')
60
+ /**
61
+ * Get icon SVG content from icon data store
62
+ *
63
+ * Icon names include variant prefix: 'filled_arrow_left', 'outlined_check'
64
+ *
65
+ * @param string $icon_name Icon name with variant (e.g., 'filled_arrow_left', 'outlined_check')
61
66
  * @return string|false SVG content or false if not found
62
67
  */
63
68
  function tds_get_icon_svg($icon_name) {
@@ -70,14 +75,23 @@ function tds_get_icon_svg($icon_name) {
70
75
  return false;
71
76
  }
72
77
 
78
+ // Sanitize the icon name (includes variant prefix)
73
79
  $icon_name = sanitize_key($icon_name);
74
- return $icon_data['icons'][$icon_name] ?? false;
80
+
81
+ // Look up icon directly (icon name already includes variant)
82
+ if (isset($icon_data['icons'][$icon_name])) {
83
+ return $icon_data['icons'][$icon_name];
84
+ }
85
+
86
+ return false;
75
87
  }
76
88
 
77
89
  /**
78
90
  * Render an inline SVG icon with optional sizing
79
91
  *
80
- * @param string $icon_name Icon name (e.g., 'arrow-left')
92
+ * Icon names include variant prefix (e.g., 'filled_accessibility', 'outlined_check')
93
+ *
94
+ * @param string $icon_name Icon name with variant (e.g., 'filled_circle', 'outlined_circle')
81
95
  * @param string $size_name Size name (e.g., 'medium', 'large')
82
96
  * @param string $class Optional CSS class to add to the SVG
83
97
  * @return string HTML SVG string or empty string if icon not found
@@ -91,7 +105,7 @@ function tds_render_icon($icon_name, $size_name = 'medium', $class = '') {
91
105
  if (!$svg_content) {
92
106
  // Icon not found - log debug info if WP_DEBUG is enabled
93
107
  if (defined('WP_DEBUG') && WP_DEBUG) {
94
- error_log("Icon not found: {$icon_name}");
108
+ error_log("Icon not found: {$variant}-{$icon_name}");
95
109
  }
96
110
  return '';
97
111
  }
@@ -110,7 +124,7 @@ function tds_render_icon($icon_name, $size_name = 'medium', $class = '') {
110
124
  * Inject width/height attributes into SVG string
111
125
  *
112
126
  * Handles both existing SVG elements and injects sizing.
113
- * Also adds optional CSS class.
127
+ * Also adds optional CSS class and replaces color attributes with currentcolor.
114
128
  *
115
129
  * @param string $svg SVG content
116
130
  * @param int $width Width in pixels
@@ -124,6 +138,18 @@ function tds_inject_svg_dimensions($svg, $width, $height, $class = '') {
124
138
  $svg = str_replace('<svg', sprintf('<svg width="%d" height="%d"', $width, $height), $svg);
125
139
  }
126
140
 
141
+ // Replace fill attributes with currentcolor
142
+ $svg = preg_replace('/fill="[^"]*"/i', 'fill="currentcolor"', $svg);
143
+
144
+ // Replace stroke attributes with currentcolor
145
+ $svg = preg_replace('/stroke="[^"]*"/i', 'stroke="currentcolor"', $svg);
146
+
147
+ // Handle fill in style attributes: style="fill: #xxx" or style="...fill: #xxx..."
148
+ $svg = preg_replace('/style="([^"]*)fill:\s*[^;}"]+/i', 'style="$1fill: currentcolor', $svg);
149
+
150
+ // Handle stroke in style attributes: style="stroke: #xxx" or style="...stroke: #xxx..."
151
+ $svg = preg_replace('/style="([^"]*)stroke:\s*[^;}"]+/i', 'style="$1stroke: currentcolor', $svg);
152
+
127
153
  // Add class if provided
128
154
  if (!empty($class)) {
129
155
  $class_attr = sanitize_html_class($class);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@times-design-system/components-wordpress",
3
- "version": "1.8.0",
3
+ "version": "1.8.1-alpha.3+dce421db",
4
4
  "type": "module",
5
5
  "description": "Times Design System Gutenberg blocks for WordPress Full Site Editing",
6
6
  "keywords": [
@@ -32,7 +32,8 @@
32
32
  "requiresPhp": "7.4"
33
33
  },
34
34
  "scripts": {
35
- "build": "npm run clean && npm run extract:icons && npm run build:rollup && npm run build:plugin",
35
+ "build": "npm run check:css-imports && npm run clean && npm run extract:icons && npm run build:rollup && npm run build:plugin",
36
+ "check:css-imports": "bash scripts/check-css-imports.sh",
36
37
  "build:rollup": "rollup -c",
37
38
  "build:plugin": "node scripts/build-plugin.cjs",
38
39
  "clean": "rimraf dist",
@@ -49,7 +50,9 @@
49
50
  "block:checklist": "node scripts/create-wordpress-block.cjs --checklist",
50
51
  "block:checklist:all": "node scripts/create-wordpress-block.cjs --checklist:all",
51
52
  "block:status": "node scripts/create-wordpress-block.cjs --status",
52
- "block:list": "node scripts/create-wordpress-block.cjs --list"
53
+ "block:list": "node scripts/create-wordpress-block.cjs --list",
54
+ "preview:build": "node ../../scripts/generate-block-previews.js",
55
+ "preview:serve": "npm run preview:build && open ../../preview/index.html"
53
56
  },
54
57
  "bugs": {
55
58
  "url": "https://github.com/newsuk/times-design-system/issues"
@@ -65,7 +68,7 @@
65
68
  "access": "public"
66
69
  },
67
70
  "dependencies": {
68
- "@times-design-system/theme-scss": "^2.6.0",
71
+ "@times-design-system/theme-scss": "^2.6.1-alpha.5+dce421db",
69
72
  "@wordpress/block-editor": "^12.0.0",
70
73
  "@wordpress/blocks": "^12.0.0",
71
74
  "@wordpress/components": "^25.0.0",
@@ -83,7 +86,7 @@
83
86
  "@testing-library/jest-dom": "^6.1.5",
84
87
  "@testing-library/react": "^14.1.2",
85
88
  "@testing-library/user-event": "^14.5.1",
86
- "@times-design-system/icons": "^1.2.0",
89
+ "@times-design-system/icons": "^1.2.1-alpha.80+dce421db",
87
90
  "@types/react": "19.2.5",
88
91
  "@vitest/ui": "^1.0.4",
89
92
  "jsdom": "^23.0.0",
@@ -95,5 +98,5 @@
95
98
  "typescript": "^5.0.0",
96
99
  "vitest": "^1.0.4"
97
100
  },
98
- "gitHead": "0eead454d09b74d39f75b6421aa5a9a5bc307566"
101
+ "gitHead": "dce421dbbf4e46123759bd047faa1e3af1ffa501"
99
102
  }
package/plugin.php CHANGED
@@ -22,26 +22,81 @@ if ( ! defined( 'ABSPATH' ) ) {
22
22
  }
23
23
 
24
24
  /**
25
- * Register all blocks from the block.json manifest.
25
+ * Register all Times Design System blocks.
26
+ * Uses PHP-side registration with render callbacks pointing to render.php files.
26
27
  *
27
28
  * @since 1.1.0
28
29
  * @return void
29
30
  */
30
31
  function tds_register_blocks() {
32
+ $plugin_dir = plugin_dir_url( __FILE__ );
31
33
  $blocks_dir = plugin_dir_path( __FILE__ ) . 'blocks/';
32
34
 
33
35
  if ( ! is_dir( $blocks_dir ) ) {
36
+ error_log( 'TDS: blocks directory not found: ' . $blocks_dir );
34
37
  return;
35
38
  }
36
39
 
37
- // Register each block found in the blocks directory
40
+ // Get all block directories
38
41
  $block_dirs = glob( $blocks_dir . '*', GLOB_ONLYDIR );
42
+
43
+ if ( empty( $block_dirs ) ) {
44
+ error_log( 'TDS: no block directories found' );
45
+ return;
46
+ }
39
47
 
40
48
  foreach ( $block_dirs as $block_dir ) {
41
- $block_json = $block_dir . '/block.json';
49
+ $block_json_path = $block_dir . '/block.json';
50
+ $render_php_path = $block_dir . '/render.php';
51
+ $block_name = basename( $block_dir );
52
+
53
+ if ( ! file_exists( $block_json_path ) ) {
54
+ error_log( 'TDS: block.json not found: ' . $block_json_path );
55
+ continue;
56
+ }
57
+
58
+ if ( ! file_exists( $render_php_path ) ) {
59
+ error_log( 'TDS: render.php not found: ' . $render_php_path );
60
+ continue;
61
+ }
42
62
 
43
- if ( file_exists( $block_json ) ) {
44
- register_block_type( $block_json );
63
+ // Read block.json for metadata
64
+ $block_json = file_get_contents( $block_json_path );
65
+ $block_config = json_decode( $block_json, true );
66
+
67
+ if ( ! $block_config || ! isset( $block_config['name'] ) ) {
68
+ error_log( 'TDS: Invalid block.json in ' . $block_name );
69
+ continue;
70
+ }
71
+
72
+ $block_type = $block_config['name'];
73
+
74
+ // Create render callback closure that includes the render.php file
75
+ $render_callback = function( $attributes, $content, $block ) use ( $render_php_path ) {
76
+ ob_start();
77
+ include $render_php_path;
78
+ return ob_get_clean();
79
+ };
80
+
81
+ // Register block with PHP-side render callback
82
+ $result = register_block_type(
83
+ $block_type,
84
+ array(
85
+ 'render_callback' => $render_callback,
86
+ // Merge in other config from block.json
87
+ 'attributes' => $block_config['attributes'] ?? array(),
88
+ 'supports' => $block_config['supports'] ?? array(),
89
+ 'category' => $block_config['category'] ?? 'common',
90
+ 'title' => $block_config['title'] ?? ucwords( str_replace( '-', ' ', $block_name ) ),
91
+ 'description' => $block_config['description'] ?? '',
92
+ 'icon' => $block_config['icon'] ?? 'admin-generic',
93
+ )
94
+ );
95
+
96
+ if ( is_wp_error( $result ) ) {
97
+ error_log( 'TDS: Failed to register ' . $block_type . ': ' . $result->get_error_message() );
98
+ } else {
99
+ error_log( 'TDS: Registered ' . $block_type );
45
100
  }
46
101
  }
47
102
  }
@@ -49,22 +104,117 @@ function tds_register_blocks() {
49
104
  add_action( 'init', 'tds_register_blocks' );
50
105
 
51
106
  /**
52
- * Enqueue plugin styles and scripts.
107
+ * Get cache-busting version for assets.
108
+ * Uses file modification time to automatically invalidate cache when files change.
109
+ *
110
+ * @since 1.1.0
111
+ * @param string $file_path Path to the file in plugin directory.
112
+ * @return string Version string (either mtime hash or fallback)
113
+ */
114
+ function tds_get_asset_version( $file_path ) {
115
+ $full_path = plugin_dir_path( __FILE__ ) . $file_path;
116
+ if ( file_exists( $full_path ) ) {
117
+ return 'v' . filemtime( $full_path );
118
+ }
119
+ return '1.1.0'; // Fallback version
120
+ }
121
+
122
+ /**
123
+ * Enqueue plugin styles.
53
124
  *
54
125
  * @since 1.1.0
55
126
  * @return void
56
127
  */
57
128
  function tds_enqueue_assets() {
58
- // Enqueue theme-scss styles if available
59
- $stylesheet_handle = 'times-design-system-theme';
129
+ $plugin_dir = plugin_dir_url( __FILE__ );
60
130
 
61
- if ( wp_style_is( $stylesheet_handle, 'registered' ) || wp_style_is( $stylesheet_handle, 'enqueued' ) ) {
62
- wp_enqueue_style( $stylesheet_handle );
63
- }
131
+ // Enqueue block styles
132
+ wp_enqueue_style(
133
+ 'times-design-system-blocks',
134
+ $plugin_dir . 'index.css',
135
+ array(),
136
+ tds_get_asset_version( 'index.css' )
137
+ );
64
138
  }
65
139
 
66
140
  add_action( 'wp_enqueue_scripts', 'tds_enqueue_assets' );
67
- add_action( 'admin_enqueue_scripts', 'tds_enqueue_assets' );
141
+
142
+ /**
143
+ * Register Times Design System admin page in the menu.
144
+ *
145
+ * @since 1.1.0
146
+ * @return void
147
+ */
148
+ function tds_register_admin_menu() {
149
+ add_menu_page(
150
+ __( 'TDS Blocks', 'times-design-system' ),
151
+ __( 'TDS Blocks', 'times-design-system' ),
152
+ 'manage_options',
153
+ 'tds-blocks-preview',
154
+ 'tds_render_admin_page',
155
+ 'dashicons-admin-generic',
156
+ 65
157
+ );
158
+ }
159
+
160
+ add_action( 'admin_menu', 'tds_register_admin_menu' );
161
+
162
+ /**
163
+ * Render the TDS blocks admin page.
164
+ *
165
+ * @since 1.1.0
166
+ * @return void
167
+ */
168
+ function tds_render_admin_page() {
169
+ require_once plugin_dir_path( __FILE__ ) . 'admin/page.php';
170
+ }
171
+
172
+ /**
173
+ * AJAX handler to preview a block with given attributes.
174
+ *
175
+ * @since 1.1.0
176
+ * @return void
177
+ */
178
+ function tds_ajax_preview_block() {
179
+ // Verify nonce
180
+ $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
181
+ if ( ! wp_verify_nonce( $nonce, 'tds_preview_nonce' ) ) {
182
+ wp_die( 'Security check failed' );
183
+ }
184
+
185
+ // Get block type and attributes
186
+ $block_type = isset( $_POST['block_type'] ) ? sanitize_text_field( wp_unslash( $_POST['block_type'] ) ) : '';
187
+ $attributes_json = isset( $_POST['attributes'] ) ? sanitize_text_field( wp_unslash( $_POST['attributes'] ) ) : '{}';
188
+ $attributes = json_decode( $attributes_json, true ) ?: array();
189
+
190
+ if ( ! $block_type || strpos( $block_type, 'tds/' ) !== 0 ) {
191
+ wp_die( 'Invalid block type' );
192
+ }
193
+
194
+ // Get the block type object
195
+ $registry = WP_Block_Type_Registry::get_instance();
196
+ $block = $registry->get_registered( $block_type );
197
+
198
+ if ( ! $block || ! $block->render_callback ) {
199
+ wp_die( 'Block not found or has no render callback' );
200
+ }
201
+
202
+ // Call render callback
203
+ try {
204
+ if ( is_callable( $block->render_callback ) ) {
205
+ $output = call_user_func( $block->render_callback, $attributes, '', null );
206
+ } else {
207
+ throw new Exception( 'Invalid render callback' );
208
+ }
209
+ echo wp_kses_post( $output );
210
+ } catch ( Exception $e ) {
211
+ echo '<p style="color: red;">Error rendering block: ' . esc_html( $e->getMessage() ) . '</p>';
212
+ }
213
+
214
+ wp_die();
215
+ }
216
+
217
+ add_action( 'wp_ajax_tds_preview_block', 'tds_ajax_preview_block' );
68
218
 
69
219
  // Activation hook
70
220
  register_activation_hook( __FILE__, function() {
@@ -1,15 +0,0 @@
1
- /* Button Block Editor Styles */
2
-
3
- .tds-button-wrapper {
4
- margin-bottom: 1rem;
5
- }
6
-
7
- .tds-button-wrapper .tds-button {
8
- /* Styles inherited from theme-scss package */
9
- /* This file can be extended with editor-specific overrides */
10
- }
11
-
12
- /* Inspector Controls Styling */
13
- .block-editor .components-panel {
14
- /* WordPress will handle component styling */
15
- }