@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,237 @@
1
+ <?php
2
+
3
+ if ( ! defined( 'ABSPATH' ) ) {
4
+ exit;
5
+ }
6
+
7
+ require_once __DIR__ . '/inc/rest-shared.php';
8
+ require_once __DIR__ . '/inc/rest-auth.php';
9
+
10
+ function {{phpPrefix}}_get_rest_build_dir() {
11
+ return dirname( __DIR__, 3 ) . '/build/blocks/{{slugKebabCase}}';
12
+ }
13
+
14
+ function {{phpPrefix}}_get_counter_table_name() {
15
+ global $wpdb;
16
+ return $wpdb->prefix . '{{phpPrefix}}_counters';
17
+ }
18
+
19
+ function {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key ) {
20
+ return 'wpt_pcl_' . md5(
21
+ '{{phpPrefix}}|' . (int) $post_id . '|' . (string) $resource_key
22
+ );
23
+ }
24
+
25
+ function {{phpPrefix}}_with_counter_lock( $post_id, $resource_key, $callback ) {
26
+ global $wpdb;
27
+
28
+ $lock_name = {{phpPrefix}}_get_counter_lock_name( $post_id, $resource_key );
29
+ $acquired = (int) $wpdb->get_var(
30
+ $wpdb->prepare(
31
+ 'SELECT GET_LOCK(%s, 5)',
32
+ $lock_name
33
+ )
34
+ );
35
+
36
+ if ( 1 !== $acquired ) {
37
+ return new WP_Error( 'counter_lock_timeout', 'Could not acquire the counter lock.', array( 'status' => 503 ) );
38
+ }
39
+
40
+ try {
41
+ return $callback();
42
+ } finally {
43
+ $wpdb->get_var(
44
+ $wpdb->prepare(
45
+ 'SELECT RELEASE_LOCK(%s)',
46
+ $lock_name
47
+ )
48
+ );
49
+ }
50
+ }
51
+
52
+ function {{phpPrefix}}_maybe_install_storage() {
53
+ if ( 'custom-table' !== '{{dataStorageMode}}' ) {
54
+ return;
55
+ }
56
+
57
+ global $wpdb;
58
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
59
+
60
+ $table_name = {{phpPrefix}}_get_counter_table_name();
61
+ $charset_collate = $wpdb->get_charset_collate();
62
+ $sql = "CREATE TABLE {$table_name} (
63
+ post_id bigint(20) unsigned NOT NULL,
64
+ resource_key varchar(100) NOT NULL,
65
+ count bigint(20) unsigned NOT NULL DEFAULT 0,
66
+ updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
67
+ PRIMARY KEY (post_id, resource_key)
68
+ ) {$charset_collate};";
69
+
70
+ dbDelta( $sql );
71
+ $table_exists = $wpdb->get_var(
72
+ $wpdb->prepare(
73
+ 'SHOW TABLES LIKE %s',
74
+ $table_name
75
+ )
76
+ );
77
+
78
+ if ( $table_name === $table_exists ) {
79
+ update_option( '{{phpPrefix}}_storage_version', '1.0.0' );
80
+ }
81
+ }
82
+
83
+ function {{phpPrefix}}_ensure_storage_installed() {
84
+ if ( 'custom-table' === '{{dataStorageMode}}' && '1.0.0' !== get_option( '{{phpPrefix}}_storage_version', '' ) ) {
85
+ {{phpPrefix}}_maybe_install_storage();
86
+ }
87
+ }
88
+
89
+ function {{phpPrefix}}_get_counter( $post_id, $resource_key ) {
90
+ global $wpdb;
91
+
92
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
93
+ $table_name = {{phpPrefix}}_get_counter_table_name();
94
+ $count = $wpdb->get_var(
95
+ $wpdb->prepare(
96
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
97
+ "SELECT count FROM {$table_name} WHERE post_id = %d AND resource_key = %s",
98
+ $post_id,
99
+ $resource_key
100
+ )
101
+ );
102
+
103
+ return null === $count ? 0 : (int) $count;
104
+ }
105
+
106
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
107
+ return (int) get_post_meta( $post_id, $meta_key, true );
108
+ }
109
+
110
+ function {{phpPrefix}}_increment_counter( $post_id, $resource_key, $delta ) {
111
+ global $wpdb;
112
+
113
+ if ( 'custom-table' === '{{dataStorageMode}}' ) {
114
+ $table_name = {{phpPrefix}}_get_counter_table_name();
115
+ $delta_value = (int) $delta;
116
+ $initial_count = max( 0, $delta_value );
117
+ $result = $wpdb->query(
118
+ $wpdb->prepare(
119
+ // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name comes from an internal helper.
120
+ "INSERT INTO {$table_name} (post_id, resource_key, count, updated_at)
121
+ VALUES (%d, %s, %d, %s)
122
+ ON DUPLICATE KEY UPDATE
123
+ count = GREATEST(0, count + %d),
124
+ updated_at = VALUES(updated_at)",
125
+ $post_id,
126
+ $resource_key,
127
+ $initial_count,
128
+ current_time( 'mysql', true ),
129
+ $delta_value
130
+ )
131
+ );
132
+
133
+ if ( false === $result ) {
134
+ return new WP_Error( 'counter_update_failed', 'Failed to update the counter.', array( 'status' => 500 ) );
135
+ }
136
+
137
+ return {{phpPrefix}}_get_counter( $post_id, $resource_key );
138
+ }
139
+
140
+ return {{phpPrefix}}_with_counter_lock(
141
+ $post_id,
142
+ $resource_key,
143
+ function () use ( $delta, $post_id, $resource_key ) {
144
+ $meta_key = '_' . '{{phpPrefix}}' . '_counter_' . sanitize_key( $resource_key );
145
+ $next_count = max( 0, {{phpPrefix}}_get_counter( $post_id, $resource_key ) + (int) $delta );
146
+ update_post_meta( $post_id, $meta_key, $next_count );
147
+ return $next_count;
148
+ }
149
+ );
150
+ }
151
+
152
+ function {{phpPrefix}}_build_state_response( $post_id, $resource_key, $count ) {
153
+ return array(
154
+ 'postId' => (int) $post_id,
155
+ 'resourceKey' => (string) $resource_key,
156
+ 'count' => (int) $count,
157
+ 'storage' => '{{dataStorageMode}}',
158
+ );
159
+ }
160
+
161
+ function {{phpPrefix}}_handle_get_state( WP_REST_Request $request ) {
162
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
163
+ array(
164
+ 'postId' => $request->get_param( 'postId' ),
165
+ 'resourceKey' => $request->get_param( 'resourceKey' ),
166
+ ),
167
+ {{phpPrefix}}_get_rest_build_dir(),
168
+ 'state-query',
169
+ 'query'
170
+ );
171
+
172
+ if ( is_wp_error( $payload ) ) {
173
+ return $payload;
174
+ }
175
+
176
+ $count = {{phpPrefix}}_get_counter( (int) $payload['postId'], (string) $payload['resourceKey'] );
177
+ return rest_ensure_response(
178
+ {{phpPrefix}}_build_state_response(
179
+ (int) $payload['postId'],
180
+ (string) $payload['resourceKey'],
181
+ $count
182
+ )
183
+ );
184
+ }
185
+
186
+ function {{phpPrefix}}_handle_write_state( WP_REST_Request $request ) {
187
+ $payload = {{phpPrefix}}_validate_and_sanitize_request(
188
+ $request->get_json_params(),
189
+ {{phpPrefix}}_get_rest_build_dir(),
190
+ 'write-state-request',
191
+ 'body'
192
+ );
193
+
194
+ if ( is_wp_error( $payload ) ) {
195
+ return $payload;
196
+ }
197
+
198
+ $count = {{phpPrefix}}_increment_counter(
199
+ (int) $payload['postId'],
200
+ (string) $payload['resourceKey'],
201
+ isset( $payload['delta'] ) ? (int) $payload['delta'] : 1
202
+ );
203
+
204
+ if ( is_wp_error( $count ) ) {
205
+ return $count;
206
+ }
207
+
208
+ return rest_ensure_response(
209
+ {{phpPrefix}}_build_state_response(
210
+ (int) $payload['postId'],
211
+ (string) $payload['resourceKey'],
212
+ $count
213
+ )
214
+ );
215
+ }
216
+
217
+ function {{phpPrefix}}_register_routes() {
218
+ register_rest_route(
219
+ '{{namespace}}/v1',
220
+ '/{{slugKebabCase}}/state',
221
+ array(
222
+ array(
223
+ 'methods' => WP_REST_Server::READABLE,
224
+ 'callback' => '{{phpPrefix}}_handle_get_state',
225
+ 'permission_callback' => '__return_true',
226
+ ),
227
+ array(
228
+ 'methods' => WP_REST_Server::CREATABLE,
229
+ 'callback' => '{{phpPrefix}}_handle_write_state',
230
+ 'permission_callback' => '{{phpPrefix}}_can_write_authenticated',
231
+ ),
232
+ )
233
+ );
234
+ }
235
+
236
+ add_action( 'init', '{{phpPrefix}}_ensure_storage_installed' );
237
+ add_action( 'rest_api_init', '{{phpPrefix}}_register_routes' );
@@ -0,0 +1,273 @@
1
+ <?php
2
+
3
+ function {{phpPrefix}}_base64url_encode( $value ) {
4
+ return rtrim( strtr( base64_encode( $value ), '+/', '-_' ), '=' );
5
+ }
6
+
7
+ function {{phpPrefix}}_base64url_decode( $value ) {
8
+ if ( ! is_string( $value ) || '' === $value ) {
9
+ return false;
10
+ }
11
+
12
+ $padding = strlen( $value ) % 4;
13
+ if ( $padding > 0 ) {
14
+ $value .= str_repeat( '=', 4 - $padding );
15
+ }
16
+
17
+ return base64_decode( strtr( $value, '-_', '+/' ), true );
18
+ }
19
+
20
+ function {{phpPrefix}}_get_public_write_action() {
21
+ return '{{namespace}}/{{slugKebabCase}}/state/write';
22
+ }
23
+
24
+ function {{phpPrefix}}_get_public_write_client_subject() {
25
+ $remote_addr = isset( $_SERVER['REMOTE_ADDR'] ) && is_string( $_SERVER['REMOTE_ADDR'] )
26
+ ? wp_unslash( $_SERVER['REMOTE_ADDR'] )
27
+ : '';
28
+ $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) && is_string( $_SERVER['HTTP_USER_AGENT'] )
29
+ ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] )
30
+ : '';
31
+
32
+ return md5( $remote_addr . '|' . $user_agent );
33
+ }
34
+
35
+ function {{phpPrefix}}_get_public_write_rate_limit_key( $post_id, $resource_key ) {
36
+ return '{{phpPrefix}}_public_write_rl_' . (int) $post_id . '_' . md5(
37
+ (string) $resource_key . '|' . {{phpPrefix}}_get_public_write_client_subject()
38
+ );
39
+ }
40
+
41
+ function {{phpPrefix}}_get_public_write_request_replay_key( $post_id, $resource_key, $request_id ) {
42
+ return '{{phpPrefix}}_public_write_req_' . (int) $post_id . '_' . md5(
43
+ (string) $resource_key . '|' . (string) $request_id
44
+ );
45
+ }
46
+
47
+ function {{phpPrefix}}_get_public_write_lock_key( $post_id, $resource_key, $scope, $lock_subject = '' ) {
48
+ $lock_subject = is_string( $lock_subject ) && '' !== $lock_subject
49
+ ? $lock_subject
50
+ : {{phpPrefix}}_get_public_write_client_subject();
51
+
52
+ return 'wpt_pwl_' . md5(
53
+ '{{phpPrefix}}|' . (string) $scope . '|' . (int) $post_id . '|' . (string) $resource_key . '|' . $lock_subject
54
+ );
55
+ }
56
+
57
+ function {{phpPrefix}}_with_public_write_lock( $post_id, $resource_key, $scope, $callback, $lock_subject = '' ) {
58
+ global $wpdb;
59
+
60
+ $lock_key = {{phpPrefix}}_get_public_write_lock_key( $post_id, $resource_key, $scope, $lock_subject );
61
+ $acquired = (int) $wpdb->get_var(
62
+ $wpdb->prepare(
63
+ 'SELECT GET_LOCK(%s, 5)',
64
+ $lock_key
65
+ )
66
+ );
67
+
68
+ if ( 1 === $acquired ) {
69
+ try {
70
+ return $callback();
71
+ } finally {
72
+ $wpdb->get_var(
73
+ $wpdb->prepare(
74
+ 'SELECT RELEASE_LOCK(%s)',
75
+ $lock_key
76
+ )
77
+ );
78
+ }
79
+ }
80
+
81
+ return new WP_Error(
82
+ 'rest_temporarily_unavailable',
83
+ 'Could not acquire the public write lock.',
84
+ array( 'status' => 503 )
85
+ );
86
+ }
87
+
88
+ function {{phpPrefix}}_enforce_public_write_rate_limit( $post_id, $resource_key ) {
89
+ return {{phpPrefix}}_with_public_write_lock(
90
+ $post_id,
91
+ $resource_key,
92
+ 'rate_limit',
93
+ function () use ( $post_id, $resource_key ) {
94
+ $key = {{phpPrefix}}_get_public_write_rate_limit_key( $post_id, $resource_key );
95
+ $count = (int) get_transient( $key );
96
+
97
+ if ( $count >= 10 ) {
98
+ return new WP_Error(
99
+ 'rest_rate_limited',
100
+ 'Too many public write attempts. Wait a minute and try again.',
101
+ array( 'status' => 429 )
102
+ );
103
+ }
104
+
105
+ set_transient( $key, $count + 1, MINUTE_IN_SECONDS );
106
+ return true;
107
+ }
108
+ );
109
+ }
110
+
111
+ function {{phpPrefix}}_consume_public_write_request_id( $post_id, $resource_key, $request_id ) {
112
+ if ( ! is_string( $request_id ) || '' === $request_id ) {
113
+ return new WP_Error(
114
+ 'rest_forbidden',
115
+ 'The public write request id is missing.',
116
+ array( 'status' => 403 )
117
+ );
118
+ }
119
+
120
+ return {{phpPrefix}}_with_public_write_lock(
121
+ $post_id,
122
+ $resource_key,
123
+ 'replay',
124
+ function () use ( $post_id, $resource_key, $request_id ) {
125
+ $key = {{phpPrefix}}_get_public_write_request_replay_key( $post_id, $resource_key, $request_id );
126
+ if ( false !== get_transient( $key ) ) {
127
+ return new WP_Error(
128
+ 'rest_conflict',
129
+ 'This public write request was already processed.',
130
+ array( 'status' => 409 )
131
+ );
132
+ }
133
+
134
+ set_transient( $key, 1, HOUR_IN_SECONDS );
135
+ return true;
136
+ },
137
+ $request_id
138
+ );
139
+ }
140
+
141
+ function {{phpPrefix}}_release_public_write_request_id( $post_id, $resource_key, $request_id ) {
142
+ if ( ! is_string( $request_id ) || '' === $request_id ) {
143
+ return;
144
+ }
145
+
146
+ delete_transient( {{phpPrefix}}_get_public_write_request_replay_key( $post_id, $resource_key, $request_id ) );
147
+ }
148
+
149
+ function {{phpPrefix}}_create_public_write_token( $post_id, $resource_key ) {
150
+ $expires_at = time() + HOUR_IN_SECONDS;
151
+ $payload = array(
152
+ 'action' => {{phpPrefix}}_get_public_write_action(),
153
+ 'exp' => $expires_at,
154
+ 'postId' => (int) $post_id,
155
+ 'resourceKey' => (string) $resource_key,
156
+ );
157
+ $json = wp_json_encode( $payload );
158
+
159
+ if ( ! is_string( $json ) || '' === $json ) {
160
+ return array(
161
+ 'expiresAt' => $expires_at,
162
+ 'token' => '',
163
+ );
164
+ }
165
+
166
+ $payload_segment = {{phpPrefix}}_base64url_encode( $json );
167
+ $signature_segment = {{phpPrefix}}_base64url_encode(
168
+ hash_hmac( 'sha256', $payload_segment, wp_salt( 'nonce' ), true )
169
+ );
170
+
171
+ return array(
172
+ 'expiresAt' => $expires_at,
173
+ 'token' => $payload_segment . '.' . $signature_segment,
174
+ );
175
+ }
176
+
177
+ function {{phpPrefix}}_verify_public_write_token( $token, $post_id, $resource_key ) {
178
+ if ( ! is_string( $token ) || '' === $token ) {
179
+ return new WP_Error(
180
+ 'rest_forbidden',
181
+ 'The public write token is missing.',
182
+ array( 'status' => 403 )
183
+ );
184
+ }
185
+
186
+ $segments = explode( '.', $token );
187
+ if ( 2 !== count( $segments ) ) {
188
+ return new WP_Error(
189
+ 'rest_forbidden',
190
+ 'The public write token format is invalid.',
191
+ array( 'status' => 403 )
192
+ );
193
+ }
194
+
195
+ list( $payload_segment, $signature_segment ) = $segments;
196
+ $expected_signature = {{phpPrefix}}_base64url_encode(
197
+ hash_hmac( 'sha256', $payload_segment, wp_salt( 'nonce' ), true )
198
+ );
199
+
200
+ if ( ! hash_equals( $expected_signature, $signature_segment ) ) {
201
+ return new WP_Error(
202
+ 'rest_forbidden',
203
+ 'The public write token signature is invalid.',
204
+ array( 'status' => 403 )
205
+ );
206
+ }
207
+
208
+ $payload_json = {{phpPrefix}}_base64url_decode( $payload_segment );
209
+ if ( false === $payload_json ) {
210
+ return new WP_Error(
211
+ 'rest_forbidden',
212
+ 'The public write token payload is invalid.',
213
+ array( 'status' => 403 )
214
+ );
215
+ }
216
+
217
+ $payload = json_decode( $payload_json, true );
218
+ if ( ! is_array( $payload ) ) {
219
+ return new WP_Error(
220
+ 'rest_forbidden',
221
+ 'The public write token payload is invalid.',
222
+ array( 'status' => 403 )
223
+ );
224
+ }
225
+
226
+ if ( time() > (int) ( $payload['exp'] ?? 0 ) ) {
227
+ return new WP_Error(
228
+ 'rest_forbidden',
229
+ 'The public write token has expired. Reload the page and try again.',
230
+ array( 'status' => 403 )
231
+ );
232
+ }
233
+
234
+ if ( {{phpPrefix}}_get_public_write_action() !== (string) ( $payload['action'] ?? '' ) ) {
235
+ return new WP_Error(
236
+ 'rest_forbidden',
237
+ 'The public write token action is invalid.',
238
+ array( 'status' => 403 )
239
+ );
240
+ }
241
+
242
+ if ( (int) ( $payload['postId'] ?? 0 ) !== (int) $post_id ) {
243
+ return new WP_Error(
244
+ 'rest_forbidden',
245
+ 'The public write token is not valid for this post.',
246
+ array( 'status' => 403 )
247
+ );
248
+ }
249
+
250
+ if ( (string) ( $payload['resourceKey'] ?? '' ) !== (string) $resource_key ) {
251
+ return new WP_Error(
252
+ 'rest_forbidden',
253
+ 'The public write token is not valid for this resource key.',
254
+ array( 'status' => 403 )
255
+ );
256
+ }
257
+
258
+ return true;
259
+ }
260
+
261
+ function {{phpPrefix}}_can_write_publicly( WP_REST_Request $request ) {
262
+ $body = $request->get_json_params();
263
+ $post_id = (int) ( is_array( $body ) ? ( $body['postId'] ?? 0 ) : 0 );
264
+ $resource_key = (string) ( is_array( $body ) ? ( $body['resourceKey'] ?? '' ) : '' );
265
+ $token = (string) ( is_array( $body ) ? ( $body['publicWriteToken'] ?? '' ) : '' );
266
+
267
+ $rate_limit = {{phpPrefix}}_enforce_public_write_rate_limit( $post_id, $resource_key );
268
+ if ( is_wp_error( $rate_limit ) ) {
269
+ return $rate_limit;
270
+ }
271
+
272
+ return {{phpPrefix}}_verify_public_write_token( $token, $post_id, $resource_key );
273
+ }
@@ -0,0 +1,55 @@
1
+ <?php
2
+
3
+ function {{phpPrefix}}_load_schema_from_build_dir( $build_dir, $schema_name ) {
4
+ if ( ! is_string( $build_dir ) || '' === $build_dir ) {
5
+ return null;
6
+ }
7
+
8
+ $path = $build_dir . '/api-schemas/' . $schema_name . '.schema.json';
9
+ if ( ! is_readable( $path ) ) {
10
+ return null;
11
+ }
12
+
13
+ $contents = file_get_contents( $path );
14
+ if ( false === $contents ) {
15
+ return null;
16
+ }
17
+
18
+ $decoded = json_decode( $contents, true );
19
+ return is_array( $decoded ) ? $decoded : null;
20
+ }
21
+
22
+ function {{phpPrefix}}_sanitize_rest_schema( $schema ) {
23
+ if ( ! is_array( $schema ) ) {
24
+ return $schema;
25
+ }
26
+
27
+ unset( $schema['$schema'], $schema['title'] );
28
+
29
+ if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
30
+ foreach ( $schema['properties'] as $key => $property_schema ) {
31
+ $schema['properties'][ $key ] = {{phpPrefix}}_sanitize_rest_schema( $property_schema );
32
+ }
33
+ }
34
+
35
+ if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
36
+ $schema['items'] = {{phpPrefix}}_sanitize_rest_schema( $schema['items'] );
37
+ }
38
+
39
+ return $schema;
40
+ }
41
+
42
+ function {{phpPrefix}}_validate_and_sanitize_request( $value, $build_dir, $schema_name, $param_name ) {
43
+ $schema = {{phpPrefix}}_load_schema_from_build_dir( $build_dir, $schema_name );
44
+ if ( ! is_array( $schema ) ) {
45
+ return new WP_Error( 'missing_schema', 'Missing REST schema.', array( 'status' => 500 ) );
46
+ }
47
+
48
+ $rest_schema = {{phpPrefix}}_sanitize_rest_schema( $schema );
49
+ $validation = rest_validate_value_from_schema( $value, $rest_schema, $param_name );
50
+ if ( is_wp_error( $validation ) ) {
51
+ return $validation;
52
+ }
53
+
54
+ return rest_sanitize_value_from_schema( $value, $rest_schema, $param_name );
55
+ }