@wp-typia/project-tools 0.11.1

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 (187) hide show
  1. package/README.md +32 -0
  2. package/dist/runtime/cli-add.d.ts +38 -0
  3. package/dist/runtime/cli-add.js +561 -0
  4. package/dist/runtime/cli-core.d.ts +25 -0
  5. package/dist/runtime/cli-core.js +25 -0
  6. package/dist/runtime/cli-doctor.d.ts +34 -0
  7. package/dist/runtime/cli-doctor.js +131 -0
  8. package/dist/runtime/cli-help.d.ts +9 -0
  9. package/dist/runtime/cli-help.js +37 -0
  10. package/dist/runtime/cli-prompt.d.ts +21 -0
  11. package/dist/runtime/cli-prompt.js +53 -0
  12. package/dist/runtime/cli-scaffold.d.ts +79 -0
  13. package/dist/runtime/cli-scaffold.js +206 -0
  14. package/dist/runtime/cli-templates.d.ts +30 -0
  15. package/dist/runtime/cli-templates.js +61 -0
  16. package/dist/runtime/index.d.ts +9 -0
  17. package/dist/runtime/index.js +7 -0
  18. package/dist/runtime/json-utils.d.ts +10 -0
  19. package/dist/runtime/json-utils.js +12 -0
  20. package/dist/runtime/local-dev-presets.d.ts +26 -0
  21. package/dist/runtime/local-dev-presets.js +132 -0
  22. package/dist/runtime/metadata-analysis.d.ts +11 -0
  23. package/dist/runtime/metadata-analysis.js +285 -0
  24. package/dist/runtime/metadata-model.d.ts +84 -0
  25. package/dist/runtime/metadata-model.js +59 -0
  26. package/dist/runtime/metadata-parser.d.ts +53 -0
  27. package/dist/runtime/metadata-parser.js +794 -0
  28. package/dist/runtime/metadata-php-render.d.ts +29 -0
  29. package/dist/runtime/metadata-php-render.js +549 -0
  30. package/dist/runtime/metadata-projection.d.ts +7 -0
  31. package/dist/runtime/metadata-projection.js +233 -0
  32. package/dist/runtime/migration-constants.d.ts +15 -0
  33. package/dist/runtime/migration-constants.js +16 -0
  34. package/dist/runtime/migration-diff.d.ts +2 -0
  35. package/dist/runtime/migration-diff.js +537 -0
  36. package/dist/runtime/migration-fixtures.d.ts +8 -0
  37. package/dist/runtime/migration-fixtures.js +94 -0
  38. package/dist/runtime/migration-fuzz-plan.d.ts +2 -0
  39. package/dist/runtime/migration-fuzz-plan.js +50 -0
  40. package/dist/runtime/migration-manifest.d.ts +19 -0
  41. package/dist/runtime/migration-manifest.js +129 -0
  42. package/dist/runtime/migration-project.d.ts +94 -0
  43. package/dist/runtime/migration-project.js +1101 -0
  44. package/dist/runtime/migration-render.d.ts +11 -0
  45. package/dist/runtime/migration-render.js +741 -0
  46. package/dist/runtime/migration-risk.d.ts +4 -0
  47. package/dist/runtime/migration-risk.js +52 -0
  48. package/dist/runtime/migration-types.d.ts +249 -0
  49. package/dist/runtime/migration-types.js +1 -0
  50. package/dist/runtime/migration-ui-capability.d.ts +17 -0
  51. package/dist/runtime/migration-ui-capability.js +190 -0
  52. package/dist/runtime/migration-utils.d.ts +69 -0
  53. package/dist/runtime/migration-utils.js +246 -0
  54. package/dist/runtime/migrations.d.ts +249 -0
  55. package/dist/runtime/migrations.js +1061 -0
  56. package/dist/runtime/object-utils.d.ts +12 -0
  57. package/dist/runtime/object-utils.js +14 -0
  58. package/dist/runtime/package-managers.d.ts +28 -0
  59. package/dist/runtime/package-managers.js +156 -0
  60. package/dist/runtime/package-versions.d.ts +10 -0
  61. package/dist/runtime/package-versions.js +68 -0
  62. package/dist/runtime/scaffold-onboarding.d.ts +32 -0
  63. package/dist/runtime/scaffold-onboarding.js +99 -0
  64. package/dist/runtime/scaffold.d.ts +146 -0
  65. package/dist/runtime/scaffold.js +612 -0
  66. package/dist/runtime/schema-core.d.ts +267 -0
  67. package/dist/runtime/schema-core.js +597 -0
  68. package/dist/runtime/starter-manifests.d.ts +25 -0
  69. package/dist/runtime/starter-manifests.js +383 -0
  70. package/dist/runtime/string-case.d.ts +36 -0
  71. package/dist/runtime/string-case.js +69 -0
  72. package/dist/runtime/template-builtins.d.ts +38 -0
  73. package/dist/runtime/template-builtins.js +72 -0
  74. package/dist/runtime/template-defaults.d.ts +75 -0
  75. package/dist/runtime/template-defaults.js +65 -0
  76. package/dist/runtime/template-registry.d.ts +36 -0
  77. package/dist/runtime/template-registry.js +94 -0
  78. package/dist/runtime/template-render.d.ts +24 -0
  79. package/dist/runtime/template-render.js +113 -0
  80. package/dist/runtime/template-source.d.ts +71 -0
  81. package/dist/runtime/template-source.js +821 -0
  82. package/dist/runtime/typia-tags.d.ts +1 -0
  83. package/dist/runtime/typia-tags.js +1 -0
  84. package/package.json +79 -0
  85. package/templates/_shared/base/languages/.gitkeep +1 -0
  86. package/templates/_shared/base/package.json.mustache +41 -0
  87. package/templates/_shared/base/scripts/sync-types-to-block-json.ts.mustache +118 -0
  88. package/templates/_shared/base/src/hooks.ts.mustache +19 -0
  89. package/templates/_shared/base/src/validator-toolkit.ts.mustache +31 -0
  90. package/templates/_shared/base/tsconfig.json.mustache +21 -0
  91. package/templates/_shared/base/webpack.config.js.mustache +99 -0
  92. package/templates/_shared/base/{{slugKebabCase}}.php.mustache +53 -0
  93. package/templates/_shared/compound/core/package.json.mustache +45 -0
  94. package/templates/_shared/compound/core/scripts/add-compound-child.ts.mustache +559 -0
  95. package/templates/_shared/compound/core/scripts/block-config.ts.mustache +13 -0
  96. package/templates/_shared/compound/core/scripts/sync-types-to-block-json.ts.mustache +53 -0
  97. package/templates/_shared/compound/core/webpack.config.js.mustache +141 -0
  98. package/templates/_shared/compound/core/{{slugKebabCase}}.php.mustache +51 -0
  99. package/templates/_shared/compound/persistence/package.json.mustache +50 -0
  100. package/templates/_shared/compound/persistence/scripts/block-config.ts.mustache +59 -0
  101. package/templates/_shared/compound/persistence/scripts/sync-rest-contracts.ts.mustache +101 -0
  102. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/api-types.ts.mustache +21 -0
  103. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/api-validators.ts.mustache +32 -0
  104. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/api.ts.mustache +68 -0
  105. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/block.json.mustache +52 -0
  106. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/data.ts.mustache +192 -0
  107. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/edit.tsx.mustache +123 -0
  108. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/hooks.ts.mustache +11 -0
  109. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/interactivity.ts.mustache +132 -0
  110. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/render.php.mustache +158 -0
  111. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/save.tsx.mustache +3 -0
  112. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/types.ts.mustache +56 -0
  113. package/templates/_shared/compound/persistence/src/blocks/{{slugKebabCase}}/validators.ts.mustache +32 -0
  114. package/templates/_shared/compound/persistence-auth/{{slugKebabCase}}.php.mustache +294 -0
  115. package/templates/_shared/compound/persistence-public/{{slugKebabCase}}.php.mustache +312 -0
  116. package/templates/_shared/migration-ui/common/src/admin/migration-dashboard.tsx +394 -0
  117. package/templates/_shared/migration-ui/common/src/migration-detector.ts +9 -0
  118. package/templates/_shared/migration-ui/common/src/migrations/helpers.ts +490 -0
  119. package/templates/_shared/migration-ui/common/src/migrations/index.ts +886 -0
  120. package/templates/_shared/persistence/auth/{{slugKebabCase}}.php.mustache +290 -0
  121. package/templates/_shared/persistence/core/package.json.mustache +46 -0
  122. package/templates/_shared/persistence/core/scripts/sync-rest-contracts.ts.mustache +113 -0
  123. package/templates/_shared/persistence/core/scripts/sync-types-to-block-json.ts.mustache +125 -0
  124. package/templates/_shared/persistence/core/src/api-types.ts.mustache +21 -0
  125. package/templates/_shared/persistence/core/src/api-validators.ts.mustache +32 -0
  126. package/templates/_shared/persistence/core/src/api.ts.mustache +68 -0
  127. package/templates/_shared/persistence/core/src/data.ts.mustache +192 -0
  128. package/templates/_shared/persistence/core/src/index.tsx.mustache +25 -0
  129. package/templates/_shared/persistence/core/src/interactivity.ts.mustache +134 -0
  130. package/templates/_shared/persistence/core/src/save.tsx.mustache +5 -0
  131. package/templates/_shared/persistence/core/src/validators.ts.mustache +32 -0
  132. package/templates/_shared/persistence/core/{{slugKebabCase}}.php.mustache +336 -0
  133. package/templates/_shared/persistence/public/{{slugKebabCase}}.php.mustache +308 -0
  134. package/templates/_shared/presets/test-preset/.wp-env.test.json.mustache +16 -0
  135. package/templates/_shared/presets/test-preset/playwright.config.ts.mustache +22 -0
  136. package/templates/_shared/presets/test-preset/scripts/wait-for-wp-env.mjs.mustache +102 -0
  137. package/templates/_shared/presets/test-preset/scripts/wp-env-utils.cjs.mustache +32 -0
  138. package/templates/_shared/presets/test-preset/tests/e2e/smoke.spec.ts.mustache +34 -0
  139. package/templates/_shared/presets/wp-env/.wp-env.json.mustache +16 -0
  140. package/templates/_shared/rest-helpers/auth/inc/rest-auth.php.mustache +37 -0
  141. package/templates/_shared/rest-helpers/public/inc/rest-public.php.mustache +314 -0
  142. package/templates/_shared/rest-helpers/shared/inc/rest-shared.php.mustache +58 -0
  143. package/templates/_shared/workspace/persistence-auth/inc/rest-auth.php.mustache +36 -0
  144. package/templates/_shared/workspace/persistence-auth/inc/rest-shared.php.mustache +55 -0
  145. package/templates/_shared/workspace/persistence-auth/server.php.mustache +237 -0
  146. package/templates/_shared/workspace/persistence-public/inc/rest-public.php.mustache +273 -0
  147. package/templates/_shared/workspace/persistence-public/inc/rest-shared.php.mustache +55 -0
  148. package/templates/_shared/workspace/persistence-public/server.php.mustache +252 -0
  149. package/templates/basic/src/block.json.mustache +51 -0
  150. package/templates/basic/src/edit.tsx.mustache +128 -0
  151. package/templates/basic/src/editor.scss.mustache +8 -0
  152. package/templates/basic/src/hooks.ts.mustache +18 -0
  153. package/templates/basic/src/index.tsx.mustache +45 -0
  154. package/templates/basic/src/save.tsx.mustache +30 -0
  155. package/templates/basic/src/style.scss.mustache +40 -0
  156. package/templates/basic/src/types.ts.mustache +56 -0
  157. package/templates/basic/src/validators.ts.mustache +26 -0
  158. package/templates/compound/src/blocks/{{slugKebabCase}}/block.json.mustache +37 -0
  159. package/templates/compound/src/blocks/{{slugKebabCase}}/children.ts.mustache +25 -0
  160. package/templates/compound/src/blocks/{{slugKebabCase}}/edit.tsx.mustache +93 -0
  161. package/templates/compound/src/blocks/{{slugKebabCase}}/hooks.ts.mustache +11 -0
  162. package/templates/compound/src/blocks/{{slugKebabCase}}/index.tsx.mustache +25 -0
  163. package/templates/compound/src/blocks/{{slugKebabCase}}/save.tsx.mustache +32 -0
  164. package/templates/compound/src/blocks/{{slugKebabCase}}/style.scss.mustache +31 -0
  165. package/templates/compound/src/blocks/{{slugKebabCase}}/types.ts.mustache +13 -0
  166. package/templates/compound/src/blocks/{{slugKebabCase}}/validators.ts.mustache +17 -0
  167. package/templates/compound/src/blocks/{{slugKebabCase}}-item/block.json.mustache +35 -0
  168. package/templates/compound/src/blocks/{{slugKebabCase}}-item/edit.tsx.mustache +50 -0
  169. package/templates/compound/src/blocks/{{slugKebabCase}}-item/hooks.ts.mustache +11 -0
  170. package/templates/compound/src/blocks/{{slugKebabCase}}-item/index.tsx.mustache +25 -0
  171. package/templates/compound/src/blocks/{{slugKebabCase}}-item/save.tsx.mustache +24 -0
  172. package/templates/compound/src/blocks/{{slugKebabCase}}-item/types.ts.mustache +12 -0
  173. package/templates/compound/src/blocks/{{slugKebabCase}}-item/validators.ts.mustache +17 -0
  174. package/templates/interactivity/package.json.mustache +42 -0
  175. package/templates/interactivity/src/block.json.mustache +73 -0
  176. package/templates/interactivity/src/edit.tsx.mustache +270 -0
  177. package/templates/interactivity/src/index.tsx.mustache +32 -0
  178. package/templates/interactivity/src/interactivity.ts.mustache +152 -0
  179. package/templates/interactivity/src/save.tsx.mustache +101 -0
  180. package/templates/interactivity/src/style.scss.mustache +60 -0
  181. package/templates/interactivity/src/types.ts.mustache +32 -0
  182. package/templates/interactivity/src/validators.ts.mustache +36 -0
  183. package/templates/persistence/src/block.json.mustache +52 -0
  184. package/templates/persistence/src/edit.tsx.mustache +165 -0
  185. package/templates/persistence/src/render.php.mustache +126 -0
  186. package/templates/persistence/src/style.scss.mustache +46 -0
  187. package/templates/persistence/src/types.ts.mustache +55 -0
@@ -0,0 +1,32 @@
1
+ import currentManifest from './typia.manifest.json';
2
+ import type {
3
+ {{pascalCase}}Attributes,
4
+ {{pascalCase}}ValidationResult,
5
+ } from './types';
6
+ import { generateResourceKey } from '@wp-typia/block-runtime/identifiers';
7
+ import { createTemplateValidatorToolkit } from '../../validator-toolkit';
8
+
9
+ const scaffoldValidators = createTemplateValidatorToolkit< {{pascalCase}}Attributes >( {
10
+ manifest: currentManifest,
11
+ finalize: ( normalized ) => ( {
12
+ ...normalized,
13
+ resourceKey:
14
+ normalized.resourceKey && normalized.resourceKey.length > 0
15
+ ? normalized.resourceKey
16
+ : generateResourceKey( '{{slugKebabCase}}' ),
17
+ } ),
18
+ } );
19
+
20
+ export const validators = scaffoldValidators.validators;
21
+
22
+ export const validate{{pascalCase}}Attributes =
23
+ scaffoldValidators.validateAttributes as (
24
+ attributes: unknown
25
+ ) => {{pascalCase}}ValidationResult;
26
+
27
+ export const sanitize{{pascalCase}}Attributes =
28
+ scaffoldValidators.sanitizeAttributes as (
29
+ attributes: Partial< {{pascalCase}}Attributes >
30
+ ) => {{pascalCase}}Attributes;
31
+
32
+ export const createAttributeUpdater = scaffoldValidators.createAttributeUpdater;
@@ -0,0 +1,294 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: {{title}}
4
+ * Description: {{description}}
5
+ * Version: 0.1.0
6
+ * Requires at least: 6.7
7
+ * Tested up to: 6.9
8
+ * Requires PHP: 8.0
9
+ * Author: {{author}}
10
+ * License: GPL-2.0-or-later
11
+ * License URI: https://www.gnu.org/licenses/gpl-2.0.html
12
+ * Text Domain: {{textDomain}}
13
+ * Domain Path: /languages
14
+ */
15
+
16
+ if ( ! defined( 'ABSPATH' ) ) {
17
+ exit;
18
+ }
19
+
20
+ function {{phpPrefix}}_load_textdomain() {
21
+ load_plugin_textdomain(
22
+ '{{textDomain}}',
23
+ false,
24
+ dirname( plugin_basename( __FILE__ ) ) . '/languages'
25
+ );
26
+ }
27
+
28
+ define( '{{phpPrefixUpper}}_DATA_STORAGE_MODE', '{{dataStorageMode}}' );
29
+ // These helper files are scaffold-owned plumbing. Customize them only when you are changing auth or request validation behavior.
30
+ require_once __DIR__ . '/inc/rest-shared.php';
31
+ require_once __DIR__ . '/inc/rest-auth.php';
32
+
33
+ function {{phpPrefix}}_get_build_root() {
34
+ return __DIR__ . '/build/blocks';
35
+ }
36
+
37
+ function {{phpPrefix}}_get_block_build_dir( $block_slug ) {
38
+ $candidate = trailingslashit( {{phpPrefix}}_get_build_root() ) . $block_slug;
39
+
40
+ return file_exists( $candidate . '/block.json' ) ? $candidate : null;
41
+ }
42
+
43
+ function {{phpPrefix}}_get_counter_table_name() {
44
+ global $wpdb;
45
+ return $wpdb->prefix . '{{phpPrefix}}_counters';
46
+ }
47
+
48
+ function {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key ) {
49
+ return 'wpt_pcl_' . md5(
50
+ '{{phpPrefix}}|' . (int) $post_id . '|' . (string) $resource_key
51
+ );
52
+ }
53
+
54
+ function {{phpPrefix}}_with_counter_lock( $post_id, $resource_key, $callback ) {
55
+ global $wpdb;
56
+
57
+ $lock_name = {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key );
58
+ $acquired = (int) $wpdb->get_var(
59
+ $wpdb->prepare(
60
+ 'SELECT GET_LOCK(%s, 5)',
61
+ $lock_name
62
+ )
63
+ );
64
+
65
+ if ( 1 !== $acquired ) {
66
+ return new WP_Error( 'counter_lock_timeout', 'Could not acquire the counter lock.', array( 'status' => 503 ) );
67
+ }
68
+
69
+ try {
70
+ return $callback();
71
+ } finally {
72
+ $wpdb->get_var(
73
+ $wpdb->prepare(
74
+ 'SELECT RELEASE_LOCK(%s)',
75
+ $lock_name
76
+ )
77
+ );
78
+ }
79
+ }
80
+
81
+ function {{phpPrefix}}_maybe_install_storage() {
82
+ if ( 'custom-table' !== '{{dataStorageMode}}' ) {
83
+ return;
84
+ }
85
+
86
+ global $wpdb;
87
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
88
+
89
+ $table_name = {{phpPrefix}}_get_counter_table_name();
90
+ $charset_collate = $wpdb->get_charset_collate();
91
+ $sql = "CREATE TABLE {$table_name} (
92
+ post_id bigint(20) unsigned NOT NULL,
93
+ resource_key varchar(100) NOT NULL,
94
+ count bigint(20) unsigned NOT NULL DEFAULT 0,
95
+ updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
96
+ PRIMARY KEY (post_id, resource_key)
97
+ ) {$charset_collate};";
98
+
99
+ dbDelta( $sql );
100
+ $table_exists = $wpdb->get_var(
101
+ $wpdb->prepare(
102
+ 'SHOW TABLES LIKE %s',
103
+ $table_name
104
+ )
105
+ );
106
+
107
+ if ( $table_name === $table_exists ) {
108
+ update_option( '{{phpPrefix}}_storage_version', '1.0.0' );
109
+ }
110
+ }
111
+
112
+ function {{phpPrefix}}_ensure_storage_installed() {
113
+ if ( 'custom-table' === '{{dataStorageMode}}' && '1.0.0' !== get_option( '{{phpPrefix}}_storage_version', '' ) ) {
114
+ {{phpPrefix}}_maybe_install_storage();
115
+ }
116
+ }
117
+
118
+ function {{phpPrefix}}_get_rest_build_dir() {
119
+ return {{phpPrefix}}_get_block_build_dir( '{{slugKebabCase}}' );
120
+ }
121
+
122
+ function {{phpPrefix}}_get_counter( $post_id, $resource_key ) {
123
+ global $wpdb;
124
+
125
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
126
+ $table_name = {{phpPrefix}}_get_counter_table_name();
127
+ $count = $wpdb->get_var(
128
+ $wpdb->prepare(
129
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
130
+ "SELECT count FROM {$table_name} WHERE post_id = %d AND resource_key = %s",
131
+ $post_id,
132
+ $resource_key
133
+ )
134
+ );
135
+
136
+ return null === $count ? 0 : (int) $count;
137
+ }
138
+
139
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
140
+ return (int) get_post_meta( $post_id, $meta_key, true );
141
+ }
142
+
143
+ // Customize storage helpers here when you need a different backend or aggregate behavior for the parent block.
144
+ function {{phpPrefix}}_increment_counter( $post_id, $resource_key, $delta ) {
145
+ global $wpdb;
146
+
147
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
148
+ $table_name = {{phpPrefix}}_get_counter_table_name();
149
+ $delta_value = (int) $delta;
150
+ $initial_count = max( 0, $delta_value );
151
+ $result = $wpdb->query(
152
+ $wpdb->prepare(
153
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
154
+ "INSERT INTO {$table_name} (post_id, resource_key, count, updated_at)
155
+ VALUES (%d, %s, %d, %s)
156
+ ON DUPLICATE KEY UPDATE
157
+ count = GREATEST(0, count + %d),
158
+ updated_at = VALUES(updated_at)",
159
+ $post_id,
160
+ $resource_key,
161
+ $initial_count,
162
+ current_time( 'mysql', true ),
163
+ $delta_value
164
+ )
165
+ );
166
+
167
+ if ( false === $result ) {
168
+ return new WP_Error( 'counter_update_failed', 'Failed to update the counter.', array( 'status' => 500 ) );
169
+ }
170
+
171
+ return {{phpPrefix}}_get_counter( $post_id, $resource_key );
172
+ }
173
+
174
+ return {{phpPrefix}}_with_counter_lock(
175
+ $post_id,
176
+ $resource_key,
177
+ function () use ( $delta, $post_id, $resource_key ) {
178
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
179
+ $next_count = max( 0, {{phpPrefix}}_get_counter( $post_id, $resource_key ) + (int) $delta );
180
+ update_post_meta( $post_id, $meta_key, $next_count );
181
+ return $next_count;
182
+ }
183
+ );
184
+ }
185
+
186
+ function {{phpPrefix}}_build_state_response( $post_id, $resource_key, $count ) {
187
+ return array(
188
+ 'postId' => (int) $post_id,
189
+ 'resourceKey' => (string) $resource_key,
190
+ 'count' => (int) $count,
191
+ 'storage' => '{{dataStorageMode}}',
192
+ );
193
+ }
194
+
195
+ // Route handlers are the main product-level extension point for parent-block request/response shaping.
196
+ function {{phpPrefix}}_handle_get_state( WP_REST_Request $request ) {
197
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
198
+ array(
199
+ 'postId' => $request->get_param( 'postId' ),
200
+ 'resourceKey' => $request->get_param( 'resourceKey' ),
201
+ ),
202
+ {{phpPrefix}}_get_rest_build_dir(),
203
+ 'state-query',
204
+ 'query'
205
+ );
206
+
207
+ if ( is_wp_error( $payload ) ) {
208
+ return $payload;
209
+ }
210
+
211
+ $count = {{phpPrefix}}_get_counter( (int) $payload['postId'], (string) $payload['resourceKey'] );
212
+ return rest_ensure_response(
213
+ {{phpPrefix}}_build_state_response(
214
+ (int) $payload['postId'],
215
+ (string) $payload['resourceKey'],
216
+ $count
217
+ )
218
+ );
219
+ }
220
+
221
+ function {{phpPrefix}}_handle_write_state( WP_REST_Request $request ) {
222
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
223
+ $request->get_json_params(),
224
+ {{phpPrefix}}_get_rest_build_dir(),
225
+ 'write-state-request',
226
+ 'body'
227
+ );
228
+
229
+ if ( is_wp_error( $payload ) ) {
230
+ return $payload;
231
+ }
232
+
233
+ $count = {{phpPrefix}}_increment_counter(
234
+ (int) $payload['postId'],
235
+ (string) $payload['resourceKey'],
236
+ isset( $payload['delta'] ) ? (int) $payload['delta'] : 1
237
+ );
238
+
239
+ if ( is_wp_error( $count ) ) {
240
+ return $count;
241
+ }
242
+
243
+ return rest_ensure_response(
244
+ {{phpPrefix}}_build_state_response(
245
+ (int) $payload['postId'],
246
+ (string) $payload['resourceKey'],
247
+ $count
248
+ )
249
+ );
250
+ }
251
+
252
+ // Add or reorganize parent-block endpoints here as the REST surface grows.
253
+ function {{phpPrefix}}_register_routes() {
254
+ register_rest_route(
255
+ '{{namespace}}/v1',
256
+ '/{{slugKebabCase}}/state',
257
+ array(
258
+ array(
259
+ 'methods' => WP_REST_Server::READABLE,
260
+ 'callback' => '{{phpPrefix}}_handle_get_state',
261
+ 'permission_callback' => '__return_true',
262
+ ),
263
+ array(
264
+ 'methods' => WP_REST_Server::CREATABLE,
265
+ 'callback' => '{{phpPrefix}}_handle_write_state',
266
+ 'permission_callback' => '{{phpPrefix}}_can_write_authenticated',
267
+ ),
268
+ )
269
+ );
270
+ }
271
+
272
+ function {{phpPrefix}}_register_blocks() {
273
+ $build_root = {{phpPrefix}}_get_build_root();
274
+ if ( ! is_dir( $build_root ) ) {
275
+ return;
276
+ }
277
+
278
+ $block_dirs = glob( $build_root . '/*', GLOB_ONLYDIR );
279
+ if ( ! is_array( $block_dirs ) ) {
280
+ return;
281
+ }
282
+
283
+ foreach ( $block_dirs as $block_dir ) {
284
+ if ( file_exists( $block_dir . '/block.json' ) ) {
285
+ register_block_type( $block_dir );
286
+ }
287
+ }
288
+ }
289
+
290
+ register_activation_hook( __FILE__, '{{phpPrefix}}_maybe_install_storage' );
291
+ add_action( 'init', '{{phpPrefix}}_load_textdomain' );
292
+ add_action( 'init', '{{phpPrefix}}_ensure_storage_installed' );
293
+ add_action( 'init', '{{phpPrefix}}_register_blocks' );
294
+ add_action( 'rest_api_init', '{{phpPrefix}}_register_routes' );
@@ -0,0 +1,312 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: {{title}}
4
+ * Description: {{description}}
5
+ * Version: 0.1.0
6
+ * Requires at least: 6.7
7
+ * Tested up to: 6.9
8
+ * Requires PHP: 8.0
9
+ * Author: {{author}}
10
+ * License: GPL-2.0-or-later
11
+ * License URI: https://www.gnu.org/licenses/gpl-2.0.html
12
+ * Text Domain: {{textDomain}}
13
+ * Domain Path: /languages
14
+ */
15
+
16
+ if ( ! defined( 'ABSPATH' ) ) {
17
+ exit;
18
+ }
19
+
20
+ function {{phpPrefix}}_load_textdomain() {
21
+ load_plugin_textdomain(
22
+ '{{textDomain}}',
23
+ false,
24
+ dirname( plugin_basename( __FILE__ ) ) . '/languages'
25
+ );
26
+ }
27
+
28
+ define( '{{phpPrefixUpper}}_DATA_STORAGE_MODE', '{{dataStorageMode}}' );
29
+ define( '{{phpPrefixUpper}}_PUBLIC_WRITE_TTL', HOUR_IN_SECONDS );
30
+ define( '{{phpPrefixUpper}}_PUBLIC_WRITE_RATE_LIMIT_WINDOW', MINUTE_IN_SECONDS );
31
+ define( '{{phpPrefixUpper}}_PUBLIC_WRITE_RATE_LIMIT_MAX', 10 );
32
+ // These helper files are scaffold-owned plumbing. Customize them only when you are changing auth or request validation behavior.
33
+ require_once __DIR__ . '/inc/rest-shared.php';
34
+ require_once __DIR__ . '/inc/rest-public.php';
35
+
36
+ function {{phpPrefix}}_get_build_root() {
37
+ return __DIR__ . '/build/blocks';
38
+ }
39
+
40
+ function {{phpPrefix}}_get_block_build_dir( $block_slug ) {
41
+ $candidate = trailingslashit( {{phpPrefix}}_get_build_root() ) . $block_slug;
42
+
43
+ return file_exists( $candidate . '/block.json' ) ? $candidate : null;
44
+ }
45
+
46
+ function {{phpPrefix}}_get_counter_table_name() {
47
+ global $wpdb;
48
+ return $wpdb->prefix . '{{phpPrefix}}_counters';
49
+ }
50
+
51
+ function {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key ) {
52
+ return 'wpt_pcl_' . md5(
53
+ '{{phpPrefix}}|' . (int) $post_id . '|' . (string) $resource_key
54
+ );
55
+ }
56
+
57
+ function {{phpPrefix}}_with_counter_lock( $post_id, $resource_key, $callback ) {
58
+ global $wpdb;
59
+
60
+ $lock_name = {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key );
61
+ $acquired = (int) $wpdb->get_var(
62
+ $wpdb->prepare(
63
+ 'SELECT GET_LOCK(%s, 5)',
64
+ $lock_name
65
+ )
66
+ );
67
+
68
+ if ( 1 !== $acquired ) {
69
+ return new WP_Error( 'counter_lock_timeout', 'Could not acquire the counter lock.', array( 'status' => 503 ) );
70
+ }
71
+
72
+ try {
73
+ return $callback();
74
+ } finally {
75
+ $wpdb->get_var(
76
+ $wpdb->prepare(
77
+ 'SELECT RELEASE_LOCK(%s)',
78
+ $lock_name
79
+ )
80
+ );
81
+ }
82
+ }
83
+
84
+ function {{phpPrefix}}_maybe_install_storage() {
85
+ if ( 'custom-table' !== '{{dataStorageMode}}' ) {
86
+ return;
87
+ }
88
+
89
+ global $wpdb;
90
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
91
+
92
+ $table_name = {{phpPrefix}}_get_counter_table_name();
93
+ $charset_collate = $wpdb->get_charset_collate();
94
+ $sql = "CREATE TABLE {$table_name} (
95
+ post_id bigint(20) unsigned NOT NULL,
96
+ resource_key varchar(100) NOT NULL,
97
+ count bigint(20) unsigned NOT NULL DEFAULT 0,
98
+ updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
99
+ PRIMARY KEY (post_id, resource_key)
100
+ ) {$charset_collate};";
101
+
102
+ dbDelta( $sql );
103
+ $table_exists = $wpdb->get_var(
104
+ $wpdb->prepare(
105
+ 'SHOW TABLES LIKE %s',
106
+ $table_name
107
+ )
108
+ );
109
+
110
+ if ( $table_name === $table_exists ) {
111
+ update_option( '{{phpPrefix}}_storage_version', '1.0.0' );
112
+ }
113
+ }
114
+
115
+ function {{phpPrefix}}_ensure_storage_installed() {
116
+ if ( 'custom-table' === '{{dataStorageMode}}' && '1.0.0' !== get_option( '{{phpPrefix}}_storage_version', '' ) ) {
117
+ {{phpPrefix}}_maybe_install_storage();
118
+ }
119
+ }
120
+
121
+ function {{phpPrefix}}_get_rest_build_dir() {
122
+ return {{phpPrefix}}_get_block_build_dir( '{{slugKebabCase}}' );
123
+ }
124
+
125
+ function {{phpPrefix}}_get_counter( $post_id, $resource_key ) {
126
+ global $wpdb;
127
+
128
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
129
+ $table_name = {{phpPrefix}}_get_counter_table_name();
130
+ $count = $wpdb->get_var(
131
+ $wpdb->prepare(
132
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
133
+ "SELECT count FROM {$table_name} WHERE post_id = %d AND resource_key = %s",
134
+ $post_id,
135
+ $resource_key
136
+ )
137
+ );
138
+
139
+ return null === $count ? 0 : (int) $count;
140
+ }
141
+
142
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
143
+ return (int) get_post_meta( $post_id, $meta_key, true );
144
+ }
145
+
146
+ // Customize storage helpers here when you need a different backend or aggregate behavior for the parent block.
147
+ function {{phpPrefix}}_increment_counter( $post_id, $resource_key, $delta ) {
148
+ global $wpdb;
149
+
150
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
151
+ $table_name = {{phpPrefix}}_get_counter_table_name();
152
+ $delta_value = (int) $delta;
153
+ $initial_count = max( 0, $delta_value );
154
+ $result = $wpdb->query(
155
+ $wpdb->prepare(
156
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
157
+ "INSERT INTO {$table_name} (post_id, resource_key, count, updated_at)
158
+ VALUES (%d, %s, %d, %s)
159
+ ON DUPLICATE KEY UPDATE
160
+ count = GREATEST(0, count + %d),
161
+ updated_at = VALUES(updated_at)",
162
+ $post_id,
163
+ $resource_key,
164
+ $initial_count,
165
+ current_time( 'mysql', true ),
166
+ $delta_value
167
+ )
168
+ );
169
+
170
+ if ( false === $result ) {
171
+ return new WP_Error( 'counter_update_failed', 'Failed to update the counter.', array( 'status' => 500 ) );
172
+ }
173
+
174
+ return {{phpPrefix}}_get_counter( $post_id, $resource_key );
175
+ }
176
+
177
+ return {{phpPrefix}}_with_counter_lock(
178
+ $post_id,
179
+ $resource_key,
180
+ function () use ( $delta, $post_id, $resource_key ) {
181
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
182
+ $next_count = max( 0, {{phpPrefix}}_get_counter( $post_id, $resource_key ) + (int) $delta );
183
+ update_post_meta( $post_id, $meta_key, $next_count );
184
+ return $next_count;
185
+ }
186
+ );
187
+ }
188
+
189
+ function {{phpPrefix}}_build_state_response( $post_id, $resource_key, $count ) {
190
+ return array(
191
+ 'postId' => (int) $post_id,
192
+ 'resourceKey' => (string) $resource_key,
193
+ 'count' => (int) $count,
194
+ 'storage' => '{{dataStorageMode}}',
195
+ );
196
+ }
197
+
198
+ // Route handlers are the main product-level extension point for parent-block request/response shaping.
199
+ function {{phpPrefix}}_handle_get_state( WP_REST_Request $request ) {
200
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
201
+ array(
202
+ 'postId' => $request->get_param( 'postId' ),
203
+ 'resourceKey' => $request->get_param( 'resourceKey' ),
204
+ ),
205
+ {{phpPrefix}}_get_rest_build_dir(),
206
+ 'state-query',
207
+ 'query'
208
+ );
209
+
210
+ if ( is_wp_error( $payload ) ) {
211
+ return $payload;
212
+ }
213
+
214
+ $count = {{phpPrefix}}_get_counter( (int) $payload['postId'], (string) $payload['resourceKey'] );
215
+ return rest_ensure_response(
216
+ {{phpPrefix}}_build_state_response(
217
+ (int) $payload['postId'],
218
+ (string) $payload['resourceKey'],
219
+ $count
220
+ )
221
+ );
222
+ }
223
+
224
+ function {{phpPrefix}}_handle_write_state( WP_REST_Request $request ) {
225
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
226
+ $request->get_json_params(),
227
+ {{phpPrefix}}_get_rest_build_dir(),
228
+ 'write-state-request',
229
+ 'body'
230
+ );
231
+
232
+ if ( is_wp_error( $payload ) ) {
233
+ return $payload;
234
+ }
235
+
236
+ $request_consumed = {{phpPrefix}}_consume_public_write_request_id(
237
+ (int) $payload['postId'],
238
+ (string) $payload['resourceKey'],
239
+ isset( $payload['publicWriteRequestId'] ) ? (string) $payload['publicWriteRequestId'] : ''
240
+ );
241
+
242
+ if ( is_wp_error( $request_consumed ) ) {
243
+ return $request_consumed;
244
+ }
245
+
246
+ $count = {{phpPrefix}}_increment_counter(
247
+ (int) $payload['postId'],
248
+ (string) $payload['resourceKey'],
249
+ isset( $payload['delta'] ) ? (int) $payload['delta'] : 1
250
+ );
251
+
252
+ if ( is_wp_error( $count ) ) {
253
+ {{phpPrefix}}_release_public_write_request_id(
254
+ (int) $payload['postId'],
255
+ (string) $payload['resourceKey'],
256
+ isset( $payload['publicWriteRequestId'] ) ? (string) $payload['publicWriteRequestId'] : ''
257
+ );
258
+ return $count;
259
+ }
260
+
261
+ return rest_ensure_response(
262
+ {{phpPrefix}}_build_state_response(
263
+ (int) $payload['postId'],
264
+ (string) $payload['resourceKey'],
265
+ $count
266
+ )
267
+ );
268
+ }
269
+
270
+ // Add or reorganize parent-block endpoints here as the REST surface grows.
271
+ function {{phpPrefix}}_register_routes() {
272
+ register_rest_route(
273
+ '{{namespace}}/v1',
274
+ '/{{slugKebabCase}}/state',
275
+ array(
276
+ array(
277
+ 'methods' => WP_REST_Server::READABLE,
278
+ 'callback' => '{{phpPrefix}}_handle_get_state',
279
+ 'permission_callback' => '__return_true',
280
+ ),
281
+ array(
282
+ 'methods' => WP_REST_Server::CREATABLE,
283
+ 'callback' => '{{phpPrefix}}_handle_write_state',
284
+ 'permission_callback' => '{{phpPrefix}}_can_write_publicly',
285
+ ),
286
+ )
287
+ );
288
+ }
289
+
290
+ function {{phpPrefix}}_register_blocks() {
291
+ $build_root = {{phpPrefix}}_get_build_root();
292
+ if ( ! is_dir( $build_root ) ) {
293
+ return;
294
+ }
295
+
296
+ $block_dirs = glob( $build_root . '/*', GLOB_ONLYDIR );
297
+ if ( ! is_array( $block_dirs ) ) {
298
+ return;
299
+ }
300
+
301
+ foreach ( $block_dirs as $block_dir ) {
302
+ if ( file_exists( $block_dir . '/block.json' ) ) {
303
+ register_block_type( $block_dir );
304
+ }
305
+ }
306
+ }
307
+
308
+ register_activation_hook( __FILE__, '{{phpPrefix}}_maybe_install_storage' );
309
+ add_action( 'init', '{{phpPrefix}}_load_textdomain' );
310
+ add_action( 'init', '{{phpPrefix}}_ensure_storage_installed' );
311
+ add_action( 'init', '{{phpPrefix}}_register_blocks' );
312
+ add_action( 'rest_api_init', '{{phpPrefix}}_register_routes' );