@wp-typia/project-tools 0.23.1 → 0.24.0

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 (152) hide show
  1. package/dist/runtime/built-in-block-non-ts-basic-artifacts.d.ts +9 -0
  2. package/dist/runtime/built-in-block-non-ts-basic-artifacts.js +84 -0
  3. package/dist/runtime/built-in-block-non-ts-compound-artifacts.d.ts +9 -0
  4. package/dist/runtime/built-in-block-non-ts-compound-artifacts.js +36 -0
  5. package/dist/runtime/built-in-block-non-ts-compound-templates.d.ts +23 -0
  6. package/dist/runtime/built-in-block-non-ts-compound-templates.js +453 -0
  7. package/dist/runtime/built-in-block-non-ts-family-artifacts.d.ts +8 -26
  8. package/dist/runtime/built-in-block-non-ts-family-artifacts.js +8 -1034
  9. package/dist/runtime/built-in-block-non-ts-interactivity-artifacts.d.ts +9 -0
  10. package/dist/runtime/built-in-block-non-ts-interactivity-artifacts.js +83 -0
  11. package/dist/runtime/built-in-block-non-ts-persistence-artifacts.d.ts +9 -0
  12. package/dist/runtime/built-in-block-non-ts-persistence-artifacts.js +33 -0
  13. package/dist/runtime/built-in-block-non-ts-persistence-templates.d.ts +23 -0
  14. package/dist/runtime/built-in-block-non-ts-persistence-templates.js +395 -0
  15. package/dist/runtime/cli-add-collision.js +8 -0
  16. package/dist/runtime/cli-add-help.js +10 -7
  17. package/dist/runtime/cli-add-kind-ids.d.ts +1 -1
  18. package/dist/runtime/cli-add-kind-ids.js +1 -0
  19. package/dist/runtime/cli-add-types.d.ts +28 -1
  20. package/dist/runtime/cli-add-types.js +2 -0
  21. package/dist/runtime/cli-add-workspace-ability-anchors.d.ts +24 -0
  22. package/dist/runtime/cli-add-workspace-ability-anchors.js +294 -0
  23. package/dist/runtime/cli-add-workspace-ability-registry.d.ts +10 -0
  24. package/dist/runtime/cli-add-workspace-ability-registry.js +51 -0
  25. package/dist/runtime/cli-add-workspace-ability-scaffold.d.ts +1 -1
  26. package/dist/runtime/cli-add-workspace-ability-scaffold.js +5 -311
  27. package/dist/runtime/cli-add-workspace-admin-view-scaffold.js +1 -1
  28. package/dist/runtime/cli-add-workspace-ai-anchors.d.ts +4 -4
  29. package/dist/runtime/cli-add-workspace-ai-anchors.js +4 -232
  30. package/dist/runtime/cli-add-workspace-ai-scaffold.js +4 -2
  31. package/dist/runtime/cli-add-workspace-ai-source-emitters.d.ts +1 -4
  32. package/dist/runtime/cli-add-workspace-ai-source-emitters.js +1 -145
  33. package/dist/runtime/cli-add-workspace-ai-sync-rest-anchors.d.ts +5 -0
  34. package/dist/runtime/cli-add-workspace-ai-sync-rest-anchors.js +236 -0
  35. package/dist/runtime/cli-add-workspace-ai-sync-script-source.d.ts +4 -0
  36. package/dist/runtime/cli-add-workspace-ai-sync-script-source.js +145 -0
  37. package/dist/runtime/cli-add-workspace-assets.d.ts +6 -63
  38. package/dist/runtime/cli-add-workspace-assets.js +6 -950
  39. package/dist/runtime/cli-add-workspace-binding-source-anchors.d.ts +23 -0
  40. package/dist/runtime/cli-add-workspace-binding-source-anchors.js +112 -0
  41. package/dist/runtime/cli-add-workspace-binding-source-source-emitters.d.ts +33 -0
  42. package/dist/runtime/cli-add-workspace-binding-source-source-emitters.js +436 -0
  43. package/dist/runtime/cli-add-workspace-binding-source-types.d.ts +20 -0
  44. package/dist/runtime/cli-add-workspace-binding-source-types.js +1 -0
  45. package/dist/runtime/cli-add-workspace-binding-source.d.ts +40 -0
  46. package/dist/runtime/cli-add-workspace-binding-source.js +275 -0
  47. package/dist/runtime/cli-add-workspace-block-style.d.ts +22 -0
  48. package/dist/runtime/cli-add-workspace-block-style.js +148 -0
  49. package/dist/runtime/cli-add-workspace-block-transform.d.ts +32 -0
  50. package/dist/runtime/cli-add-workspace-block-transform.js +197 -0
  51. package/dist/runtime/cli-add-workspace-contract.js +1 -1
  52. package/dist/runtime/cli-add-workspace-core-variation.d.ts +20 -0
  53. package/dist/runtime/cli-add-workspace-core-variation.js +322 -0
  54. package/dist/runtime/cli-add-workspace-editor-plugin-anchors.d.ts +37 -0
  55. package/dist/runtime/cli-add-workspace-editor-plugin-anchors.js +206 -0
  56. package/dist/runtime/cli-add-workspace-editor-plugin-source-emitters.d.ts +47 -0
  57. package/dist/runtime/cli-add-workspace-editor-plugin-source-emitters.js +219 -0
  58. package/dist/runtime/cli-add-workspace-editor-plugin.d.ts +22 -0
  59. package/dist/runtime/cli-add-workspace-editor-plugin.js +78 -0
  60. package/dist/runtime/cli-add-workspace-hooked-block.d.ts +23 -0
  61. package/dist/runtime/cli-add-workspace-hooked-block.js +57 -0
  62. package/dist/runtime/cli-add-workspace-integration-env-files.d.ts +33 -0
  63. package/dist/runtime/cli-add-workspace-integration-env-files.js +65 -0
  64. package/dist/runtime/cli-add-workspace-integration-env-package-json.d.ts +38 -0
  65. package/dist/runtime/cli-add-workspace-integration-env-package-json.js +122 -0
  66. package/dist/runtime/cli-add-workspace-integration-env-source-emitters.d.ts +44 -0
  67. package/dist/runtime/cli-add-workspace-integration-env-source-emitters.js +262 -0
  68. package/dist/runtime/cli-add-workspace-integration-env.js +5 -345
  69. package/dist/runtime/cli-add-workspace-pattern-anchors.d.ts +10 -0
  70. package/dist/runtime/cli-add-workspace-pattern-anchors.js +95 -0
  71. package/dist/runtime/cli-add-workspace-pattern-options.d.ts +20 -0
  72. package/dist/runtime/cli-add-workspace-pattern-options.js +113 -0
  73. package/dist/runtime/cli-add-workspace-pattern-source-emitters.d.ts +20 -0
  74. package/dist/runtime/cli-add-workspace-pattern-source-emitters.js +57 -0
  75. package/dist/runtime/cli-add-workspace-pattern.d.ts +42 -0
  76. package/dist/runtime/cli-add-workspace-pattern.js +99 -0
  77. package/dist/runtime/cli-add-workspace-post-meta.js +1 -1
  78. package/dist/runtime/cli-add-workspace-registration-hooks.d.ts +50 -0
  79. package/dist/runtime/cli-add-workspace-registration-hooks.js +162 -0
  80. package/dist/runtime/cli-add-workspace-rest-anchors.d.ts +6 -9
  81. package/dist/runtime/cli-add-workspace-rest-anchors.js +6 -466
  82. package/dist/runtime/cli-add-workspace-rest-bootstrap-anchors.d.ts +17 -0
  83. package/dist/runtime/cli-add-workspace-rest-bootstrap-anchors.js +108 -0
  84. package/dist/runtime/cli-add-workspace-rest-contract-sync-anchors.d.ts +9 -0
  85. package/dist/runtime/cli-add-workspace-rest-contract-sync-anchors.js +142 -0
  86. package/dist/runtime/cli-add-workspace-rest-generated-source-emitters.d.ts +51 -0
  87. package/dist/runtime/cli-add-workspace-rest-generated-source-emitters.js +415 -0
  88. package/dist/runtime/cli-add-workspace-rest-generated.js +5 -3
  89. package/dist/runtime/cli-add-workspace-rest-manual-source-emitters.d.ts +80 -0
  90. package/dist/runtime/cli-add-workspace-rest-manual-source-emitters.js +238 -0
  91. package/dist/runtime/cli-add-workspace-rest-manual.js +3 -16
  92. package/dist/runtime/cli-add-workspace-rest-php-templates.d.ts +1 -7
  93. package/dist/runtime/cli-add-workspace-rest-php-templates.js +3 -322
  94. package/dist/runtime/cli-add-workspace-rest-resource-php-routing-template.d.ts +33 -0
  95. package/dist/runtime/cli-add-workspace-rest-resource-php-routing-template.js +145 -0
  96. package/dist/runtime/cli-add-workspace-rest-resource-sync-anchors.d.ts +9 -0
  97. package/dist/runtime/cli-add-workspace-rest-resource-sync-anchors.js +162 -0
  98. package/dist/runtime/cli-add-workspace-rest-schema-helper-php-template.d.ts +7 -0
  99. package/dist/runtime/cli-add-workspace-rest-schema-helper-php-template.js +193 -0
  100. package/dist/runtime/cli-add-workspace-rest-source-emitters.d.ts +5 -99
  101. package/dist/runtime/cli-add-workspace-rest-source-emitters.js +5 -663
  102. package/dist/runtime/cli-add-workspace-rest-source-utils.d.ts +17 -0
  103. package/dist/runtime/cli-add-workspace-rest-source-utils.js +50 -0
  104. package/dist/runtime/cli-add-workspace-rest-sync-script-shared.d.ts +56 -0
  105. package/dist/runtime/cli-add-workspace-rest-sync-script-shared.js +122 -0
  106. package/dist/runtime/cli-add-workspace-rest-types.d.ts +3 -3
  107. package/dist/runtime/cli-add-workspace-variation.d.ts +22 -0
  108. package/dist/runtime/cli-add-workspace-variation.js +162 -0
  109. package/dist/runtime/cli-add-workspace.d.ts +42 -107
  110. package/dist/runtime/cli-add-workspace.js +42 -674
  111. package/dist/runtime/cli-add.d.ts +3 -3
  112. package/dist/runtime/cli-add.js +2 -2
  113. package/dist/runtime/cli-core.d.ts +2 -1
  114. package/dist/runtime/cli-core.js +1 -1
  115. package/dist/runtime/cli-doctor-workspace-bindings.js +59 -0
  116. package/dist/runtime/cli-doctor-workspace-block-addons.js +33 -5
  117. package/dist/runtime/cli-doctor.d.ts +2 -0
  118. package/dist/runtime/cli-doctor.js +13 -2
  119. package/dist/runtime/cli-help.js +6 -4
  120. package/dist/runtime/index.d.ts +5 -2
  121. package/dist/runtime/index.js +4 -2
  122. package/dist/runtime/local-dev-presets.js +2 -1
  123. package/dist/runtime/package-versions.d.ts +1 -0
  124. package/dist/runtime/package-versions.js +10 -2
  125. package/dist/runtime/pattern-catalog.d.ts +122 -0
  126. package/dist/runtime/pattern-catalog.js +471 -0
  127. package/dist/runtime/post-meta-binding-fields.d.ts +46 -0
  128. package/dist/runtime/post-meta-binding-fields.js +135 -0
  129. package/dist/runtime/typia-llm-json-schema.d.ts +24 -0
  130. package/dist/runtime/typia-llm-json-schema.js +33 -0
  131. package/dist/runtime/typia-llm-openapi-constraints.d.ts +20 -0
  132. package/dist/runtime/typia-llm-openapi-constraints.js +254 -0
  133. package/dist/runtime/typia-llm-projection.d.ts +25 -0
  134. package/dist/runtime/typia-llm-projection.js +58 -0
  135. package/dist/runtime/typia-llm-render.d.ts +21 -0
  136. package/dist/runtime/typia-llm-render.js +252 -0
  137. package/dist/runtime/typia-llm-sync.d.ts +10 -0
  138. package/dist/runtime/typia-llm-sync.js +63 -0
  139. package/dist/runtime/typia-llm-types.d.ts +197 -0
  140. package/dist/runtime/typia-llm-types.js +1 -0
  141. package/dist/runtime/typia-llm.d.ts +9 -255
  142. package/dist/runtime/typia-llm.js +5 -634
  143. package/dist/runtime/workspace-inventory-mutations.js +13 -0
  144. package/dist/runtime/workspace-inventory-section-descriptors.js +9 -1
  145. package/dist/runtime/workspace-inventory-templates.d.ts +2 -2
  146. package/dist/runtime/workspace-inventory-templates.js +9 -1
  147. package/dist/runtime/workspace-inventory-types.d.ts +9 -1
  148. package/package.json +8 -3
  149. package/templates/_shared/compound/core/scripts/block-config.ts.mustache +22 -0
  150. package/templates/_shared/compound/core/scripts/sync-types-to-block-json.ts.mustache +103 -2
  151. package/templates/_shared/compound/core/src/inner-blocks-templates.ts.mustache +13 -0
  152. package/templates/_shared/compound/persistence/scripts/block-config.ts.mustache +22 -1
@@ -1,125 +1,7 @@
1
1
  import { quotePhpString } from "./php-utils.js";
2
2
  import { toTitleCase } from "./string-case.js";
3
- function buildRestResourceRouteRegistrations(restResourceSlug, methods, functions, options) {
4
- const collectionRoutes = [];
5
- const itemRoutes = [];
6
- const readPermissionCallback = options.permissionCallback
7
- ? quotePhpString(options.permissionCallback)
8
- : quotePhpString("__return_true");
9
- const writePermissionCallback = options.permissionCallback
10
- ? quotePhpString(options.permissionCallback)
11
- : options.controllerVariableName
12
- ? `array( ${options.controllerVariableName}, 'can_manage_rest_resource' )`
13
- : quotePhpString(functions.canWriteFunctionName);
14
- const buildRouteCallback = (functionName, methodName) => options.controllerVariableName
15
- ? `array( ${options.controllerVariableName}, '${methodName}' )`
16
- : quotePhpString(functionName);
17
- if (methods.includes("list")) {
18
- collectionRoutes.push(`\t\tarray(
19
- \t\t\t'methods' => WP_REST_Server::READABLE,
20
- \t\t\t'callback' => ${buildRouteCallback(functions.listHandlerName, "list_items")},
21
- \t\t\t'permission_callback' => ${readPermissionCallback},
22
- \t\t)`);
23
- }
24
- if (methods.includes("create")) {
25
- collectionRoutes.push(`\t\tarray(
26
- \t\t\t'methods' => WP_REST_Server::CREATABLE,
27
- \t\t\t'callback' => ${buildRouteCallback(functions.createHandlerName, "create_item")},
28
- \t\t\t'permission_callback' => ${writePermissionCallback},
29
- \t\t)`);
30
- }
31
- if (methods.includes("read")) {
32
- itemRoutes.push(`\t\tarray(
33
- \t\t\t'methods' => WP_REST_Server::READABLE,
34
- \t\t\t'callback' => ${buildRouteCallback(functions.readHandlerName, "read_item")},
35
- \t\t\t'permission_callback' => ${readPermissionCallback},
36
- \t\t)`);
37
- }
38
- if (methods.includes("update")) {
39
- itemRoutes.push(`\t\tarray(
40
- \t\t\t'methods' => WP_REST_Server::EDITABLE,
41
- \t\t\t'callback' => ${buildRouteCallback(functions.updateHandlerName, "update_item")},
42
- \t\t\t'permission_callback' => ${writePermissionCallback},
43
- \t\t)`);
44
- }
45
- if (methods.includes("delete")) {
46
- itemRoutes.push(`\t\tarray(
47
- \t\t\t'methods' => WP_REST_Server::DELETABLE,
48
- \t\t\t'callback' => ${buildRouteCallback(functions.deleteHandlerName, "delete_item")},
49
- \t\t\t'permission_callback' => ${writePermissionCallback},
50
- \t\t)`);
51
- }
52
- const registrations = [];
53
- if (collectionRoutes.length > 0) {
54
- registrations.push(`\tregister_rest_route(
55
- \t\t$namespace,
56
- \t\t'/${restResourceSlug}',
57
- \t\tarray(
58
- ${collectionRoutes.join(",\n")}
59
- \t\t)
60
- \t);`);
61
- }
62
- if (itemRoutes.length > 0) {
63
- registrations.push(`\tregister_rest_route(
64
- \t\t$namespace,
65
- \t\t${quotePhpString(options.routePattern)},
66
- \t\tarray(
67
- ${itemRoutes.join(",\n")}
68
- \t\t)
69
- \t);`);
70
- }
71
- return registrations.join("\n\n");
72
- }
73
- function normalizeGlobalPhpClassName(classReference) {
74
- const normalized = classReference.startsWith("\\")
75
- ? classReference.slice(1)
76
- : classReference;
77
- return /^[A-Za-z_][A-Za-z0-9_]*$/u.test(normalized) ? normalized : undefined;
78
- }
79
- function toPhpClassConstantReference(classReference) {
80
- const normalized = classReference.startsWith("\\")
81
- ? classReference
82
- : `\\${classReference}`;
83
- return `${normalized}::class`;
84
- }
85
- function buildRestResourceControllerClassSource(options) {
86
- const controllerClassName = normalizeGlobalPhpClassName(options.controllerClass);
87
- if (!controllerClassName) {
88
- return "";
89
- }
90
- const extendsClause = options.controllerExtends
91
- ? ` extends ${options.controllerExtends.startsWith("\\") ? options.controllerExtends : `\\${options.controllerExtends}`}`
92
- : "";
93
- return `
94
- if ( ! class_exists( ${quotePhpString(controllerClassName)} ) ) {
95
- \tclass ${controllerClassName}${extendsClause} {
96
- \t\tpublic function can_manage_rest_resource() {
97
- \t\t\treturn ${options.functions.canWriteFunctionName}();
98
- \t\t}
99
-
100
- \t\tpublic function list_items( WP_REST_Request $request ) {
101
- \t\t\treturn ${options.functions.listHandlerName}( $request );
102
- \t\t}
103
-
104
- \t\tpublic function read_item( WP_REST_Request $request ) {
105
- \t\t\treturn ${options.functions.readHandlerName}( $request );
106
- \t\t}
107
-
108
- \t\tpublic function create_item( WP_REST_Request $request ) {
109
- \t\t\treturn ${options.functions.createHandlerName}( $request );
110
- \t\t}
111
-
112
- \t\tpublic function update_item( WP_REST_Request $request ) {
113
- \t\t\treturn ${options.functions.updateHandlerName}( $request );
114
- \t\t}
115
-
116
- \t\tpublic function delete_item( WP_REST_Request $request ) {
117
- \t\t\treturn ${options.functions.deleteHandlerName}( $request );
118
- \t\t}
119
- \t}
120
- }
121
- `;
122
- }
3
+ import { buildRestResourceControllerBootstrapSource, buildRestResourceControllerClassSource, buildRestResourceRouteRegistrations, } from "./cli-add-workspace-rest-resource-php-routing-template.js";
4
+ export { buildWorkspaceRestSchemaHelperPhpSource, } from "./cli-add-workspace-rest-schema-helper-php-template.js";
123
5
  /**
124
6
  * Build the PHP route/controller glue for generated workspace REST resources.
125
7
  *
@@ -176,15 +58,7 @@ export function buildRestResourcePhpSource(restResourceSlug, namespace, phpPrefi
176
58
  },
177
59
  })
178
60
  : "";
179
- const controllerBootstrapSource = options.controllerClass
180
- ? `\t\t$controller_class = ${toPhpClassConstantReference(options.controllerClass)};
181
- \t\tif ( ! class_exists( $controller_class ) ) {
182
- \t\t\treturn;
183
- \t\t}
184
- \t\t$controller = new $controller_class();
185
-
186
- `
187
- : "";
61
+ const controllerBootstrapSource = buildRestResourceControllerBootstrapSource(options.controllerClass);
188
62
  return `<?php
189
63
  if ( ! defined( 'ABSPATH' ) ) {
190
64
  \treturn;
@@ -483,196 +357,3 @@ ${routeRegistrations}
483
357
  add_action( 'rest_api_init', '${registerRoutesFunctionName}' );
484
358
  `;
485
359
  }
486
- /**
487
- * Build the shared PHP helper loaded by workspace bootstraps for generated REST schemas.
488
- *
489
- * @param phpPrefix Plugin-scoped PHP function prefix.
490
- * @returns PHP source for `inc/rest-schema.php`.
491
- */
492
- export function buildWorkspaceRestSchemaHelperPhpSource(phpPrefix) {
493
- return `<?php
494
- if ( ! defined( 'ABSPATH' ) ) {
495
- \treturn;
496
- }
497
-
498
- if ( ! function_exists( '${phpPrefix}_is_valid_rest_schema_key' ) ) {
499
- \tfunction ${phpPrefix}_is_valid_rest_schema_key( $value ) {
500
- \t\treturn is_string( $value ) && 1 === preg_match( '/\\A[A-Za-z0-9_-]+\\z/', $value );
501
- \t}
502
- }
503
-
504
- if ( ! function_exists( '${phpPrefix}_is_valid_rest_resource_slug' ) ) {
505
- \tfunction ${phpPrefix}_is_valid_rest_resource_slug( $value ) {
506
- \t\treturn is_string( $value ) && 1 === preg_match( '/\\A[a-z0-9]+(?:-[a-z0-9]+)*\\z/', $value );
507
- \t}
508
- }
509
-
510
- if ( ! function_exists( '${phpPrefix}_resolve_rest_schema_paths' ) ) {
511
- \tfunction ${phpPrefix}_resolve_rest_schema_paths( $schema_name, $options = array() ) {
512
- \t\tif ( ! ${phpPrefix}_is_valid_rest_schema_key( $schema_name ) ) {
513
- \t\t\treturn new WP_Error(
514
- \t\t\t\t'invalid_rest_schema_name',
515
- \t\t\t\t'Invalid REST schema name.',
516
- \t\t\t\tarray( 'status' => 500 )
517
- \t\t\t);
518
- \t\t}
519
-
520
- \t\t$options = is_array( $options ) ? $options : array();
521
- \t\t$project_root = dirname( __DIR__ );
522
- \t\t$paths = array();
523
-
524
- \t\tif ( isset( $options['resource'] ) && '' !== $options['resource'] ) {
525
- \t\t\tif ( ! ${phpPrefix}_is_valid_rest_resource_slug( $options['resource'] ) ) {
526
- \t\t\t\treturn new WP_Error(
527
- \t\t\t\t\t'invalid_rest_schema_resource',
528
- \t\t\t\t\t'Invalid REST schema resource slug.',
529
- \t\t\t\t\tarray( 'status' => 500 )
530
- \t\t\t\t);
531
- \t\t\t}
532
-
533
- \t\t\t$resource_slug = $options['resource'];
534
- \t\t\t$paths[] = __DIR__ . '/rest-schemas/rest/' . $resource_slug . '/' . $schema_name . '.schema.json';
535
- \t\t\t$paths[] = $project_root . '/src/rest/' . $resource_slug . '/api-schemas/' . $schema_name . '.schema.json';
536
- \t\t}
537
-
538
- \t\tif ( isset( $options['paths'] ) && is_array( $options['paths'] ) ) {
539
- \t\t\tforeach ( $options['paths'] as $schema_path ) {
540
- \t\t\t\tif ( is_string( $schema_path ) && '' !== $schema_path && ! in_array( $schema_path, $paths, true ) ) {
541
- \t\t\t\t\t$paths[] = $schema_path;
542
- \t\t\t\t}
543
- \t\t\t}
544
- \t\t}
545
-
546
- \t\treturn $paths;
547
- \t}
548
- }
549
-
550
- if ( ! function_exists( '${phpPrefix}_load_rest_schema' ) ) {
551
- \tfunction ${phpPrefix}_load_rest_schema( $schema_name, $options = array() ) {
552
- \t\t$schema_paths = ${phpPrefix}_resolve_rest_schema_paths( $schema_name, $options );
553
- \t\tif ( is_wp_error( $schema_paths ) ) {
554
- \t\t\treturn $schema_paths;
555
- \t\t}
556
-
557
- \t\tforeach ( $schema_paths as $schema_path ) {
558
- \t\t\tif ( ! is_file( $schema_path ) ) {
559
- \t\t\t\tcontinue;
560
- \t\t\t}
561
-
562
- \t\t\tif ( ! is_readable( $schema_path ) ) {
563
- \t\t\t\treturn new WP_Error(
564
- \t\t\t\t\t'unreadable_rest_schema',
565
- \t\t\t\t\t'Generated REST schema is not readable.',
566
- \t\t\t\t\tarray(
567
- \t\t\t\t\t\t'path' => $schema_path,
568
- \t\t\t\t\t\t'status' => 500,
569
- \t\t\t\t\t)
570
- \t\t\t\t);
571
- \t\t\t}
572
-
573
- \t\t\t$schema_json = file_get_contents( $schema_path );
574
- \t\t\tif ( false === $schema_json ) {
575
- \t\t\t\treturn new WP_Error(
576
- \t\t\t\t\t'rest_schema_read_failed',
577
- \t\t\t\t\t'Generated REST schema could not be read.',
578
- \t\t\t\t\tarray(
579
- \t\t\t\t\t\t'path' => $schema_path,
580
- \t\t\t\t\t\t'status' => 500,
581
- \t\t\t\t\t)
582
- \t\t\t\t);
583
- \t\t\t}
584
-
585
- \t\t\t$decoded = json_decode( $schema_json, true );
586
- \t\t\tif ( ! is_array( $decoded ) ) {
587
- \t\t\t\treturn new WP_Error(
588
- \t\t\t\t\t'malformed_rest_schema',
589
- \t\t\t\t\t'Generated REST schema contains malformed JSON.',
590
- \t\t\t\t\tarray(
591
- \t\t\t\t\t\t'json_error' => json_last_error_msg(),
592
- \t\t\t\t\t\t'path' => $schema_path,
593
- \t\t\t\t\t\t'status' => 500,
594
- \t\t\t\t\t)
595
- \t\t\t\t);
596
- \t\t\t}
597
-
598
- \t\t\treturn $decoded;
599
- \t\t}
600
-
601
- \t\treturn new WP_Error(
602
- \t\t\t'missing_rest_schema',
603
- \t\t\t'Generated REST schema could not be found.',
604
- \t\t\tarray(
605
- \t\t\t\t'paths' => $schema_paths,
606
- \t\t\t\t'schema' => $schema_name,
607
- \t\t\t\t'status' => 500,
608
- \t\t\t)
609
- \t\t);
610
- \t}
611
- }
612
-
613
- if ( ! function_exists( '${phpPrefix}_prepare_rest_schema_for_wordpress' ) ) {
614
- \tfunction ${phpPrefix}_prepare_rest_schema_for_wordpress( $schema ) {
615
- \t\tif ( ! is_array( $schema ) ) {
616
- \t\t\treturn $schema;
617
- \t\t}
618
-
619
- \t\tunset( $schema['$schema'], $schema['title'] );
620
-
621
- \t\tforeach ( array( 'properties', 'patternProperties', 'definitions', '$defs' ) as $schema_map_key ) {
622
- \t\t\tif ( ! isset( $schema[ $schema_map_key ] ) || ! is_array( $schema[ $schema_map_key ] ) ) {
623
- \t\t\t\tcontinue;
624
- \t\t\t}
625
-
626
- \t\t\tforeach ( $schema[ $schema_map_key ] as $key => $property_schema ) {
627
- \t\t\t\t$schema[ $schema_map_key ][ $key ] = ${phpPrefix}_prepare_rest_schema_for_wordpress( $property_schema );
628
- \t\t\t}
629
- \t\t}
630
-
631
- \t\tforeach ( array( 'items', 'additionalProperties', 'contains', 'propertyNames', 'not', 'if', 'then', 'else' ) as $nested_schema_key ) {
632
- \t\t\tif ( isset( $schema[ $nested_schema_key ] ) && is_array( $schema[ $nested_schema_key ] ) ) {
633
- \t\t\t\t$schema[ $nested_schema_key ] = ${phpPrefix}_prepare_rest_schema_for_wordpress( $schema[ $nested_schema_key ] );
634
- \t\t\t}
635
- \t\t}
636
-
637
- \t\tforeach ( array( 'allOf', 'anyOf', 'oneOf' ) as $schema_list_key ) {
638
- \t\t\tif ( ! isset( $schema[ $schema_list_key ] ) || ! is_array( $schema[ $schema_list_key ] ) ) {
639
- \t\t\t\tcontinue;
640
- \t\t\t}
641
-
642
- \t\t\tforeach ( $schema[ $schema_list_key ] as $index => $variant_schema ) {
643
- \t\t\t\t$schema[ $schema_list_key ][ $index ] = ${phpPrefix}_prepare_rest_schema_for_wordpress( $variant_schema );
644
- \t\t\t}
645
- \t\t}
646
-
647
- \t\treturn $schema;
648
- \t}
649
- }
650
-
651
- if ( ! function_exists( '${phpPrefix}_get_wordpress_rest_schema' ) ) {
652
- \tfunction ${phpPrefix}_get_wordpress_rest_schema( $schema_name, $options = array() ) {
653
- \t\t$schema = ${phpPrefix}_load_rest_schema( $schema_name, $options );
654
- \t\tif ( is_wp_error( $schema ) ) {
655
- \t\t\treturn $schema;
656
- \t\t}
657
-
658
- \t\treturn ${phpPrefix}_prepare_rest_schema_for_wordpress( $schema );
659
- \t}
660
- }
661
-
662
- if ( ! function_exists( '${phpPrefix}_validate_and_sanitize_rest_payload' ) ) {
663
- \tfunction ${phpPrefix}_validate_and_sanitize_rest_payload( $value, $schema_name, $param_name, $options = array() ) {
664
- \t\t$rest_schema = ${phpPrefix}_get_wordpress_rest_schema( $schema_name, $options );
665
- \t\tif ( is_wp_error( $rest_schema ) ) {
666
- \t\t\treturn $rest_schema;
667
- \t\t}
668
-
669
- \t\t$validation = rest_validate_value_from_schema( $value, $rest_schema, $param_name );
670
- \t\tif ( is_wp_error( $validation ) ) {
671
- \t\t\treturn $validation;
672
- \t\t}
673
-
674
- \t\treturn rest_sanitize_value_from_schema( $value, $rest_schema, $param_name );
675
- \t}
676
- }
677
- `;
678
- }
@@ -0,0 +1,33 @@
1
+ import { type RestResourceMethodId } from "./cli-add-shared.js";
2
+ export interface RestResourcePhpFunctionNames {
3
+ canWriteFunctionName: string;
4
+ createHandlerName: string;
5
+ deleteHandlerName: string;
6
+ listHandlerName: string;
7
+ readHandlerName: string;
8
+ updateHandlerName: string;
9
+ }
10
+ /**
11
+ * Build the `register_rest_route` calls for generated REST resource PHP files.
12
+ */
13
+ export declare function buildRestResourceRouteRegistrations(restResourceSlug: string, methods: RestResourceMethodId[], functions: RestResourcePhpFunctionNames, options: {
14
+ controllerVariableName?: string;
15
+ permissionCallback?: string;
16
+ routePattern: string;
17
+ }): string;
18
+ /**
19
+ * Normalize a configured PHP class name into a global `::class` reference.
20
+ */
21
+ export declare function toPhpClassConstantReference(classReference: string): string;
22
+ /**
23
+ * Build an optional controller shim class for generated REST resource handlers.
24
+ */
25
+ export declare function buildRestResourceControllerClassSource(options: {
26
+ controllerClass: string;
27
+ controllerExtends?: string;
28
+ functions: RestResourcePhpFunctionNames;
29
+ }): string;
30
+ /**
31
+ * Build the controller instantiation block used inside REST route registration.
32
+ */
33
+ export declare function buildRestResourceControllerBootstrapSource(controllerClass: string | undefined): string;
@@ -0,0 +1,145 @@
1
+ import { quotePhpString } from "./php-utils.js";
2
+ /**
3
+ * Build the `register_rest_route` calls for generated REST resource PHP files.
4
+ */
5
+ export function buildRestResourceRouteRegistrations(restResourceSlug, methods, functions, options) {
6
+ const collectionRoutes = [];
7
+ const itemRoutes = [];
8
+ const readPermissionCallback = options.permissionCallback
9
+ ? quotePhpString(options.permissionCallback)
10
+ : quotePhpString("__return_true");
11
+ const writePermissionCallback = options.permissionCallback
12
+ ? quotePhpString(options.permissionCallback)
13
+ : options.controllerVariableName
14
+ ? `array( ${options.controllerVariableName}, 'can_manage_rest_resource' )`
15
+ : quotePhpString(functions.canWriteFunctionName);
16
+ const buildRouteCallback = (functionName, methodName) => options.controllerVariableName
17
+ ? `array( ${options.controllerVariableName}, '${methodName}' )`
18
+ : quotePhpString(functionName);
19
+ if (methods.includes("list")) {
20
+ collectionRoutes.push(`\t\tarray(
21
+ \t\t\t'methods' => WP_REST_Server::READABLE,
22
+ \t\t\t'callback' => ${buildRouteCallback(functions.listHandlerName, "list_items")},
23
+ \t\t\t'permission_callback' => ${readPermissionCallback},
24
+ \t\t)`);
25
+ }
26
+ if (methods.includes("create")) {
27
+ collectionRoutes.push(`\t\tarray(
28
+ \t\t\t'methods' => WP_REST_Server::CREATABLE,
29
+ \t\t\t'callback' => ${buildRouteCallback(functions.createHandlerName, "create_item")},
30
+ \t\t\t'permission_callback' => ${writePermissionCallback},
31
+ \t\t)`);
32
+ }
33
+ if (methods.includes("read")) {
34
+ itemRoutes.push(`\t\tarray(
35
+ \t\t\t'methods' => WP_REST_Server::READABLE,
36
+ \t\t\t'callback' => ${buildRouteCallback(functions.readHandlerName, "read_item")},
37
+ \t\t\t'permission_callback' => ${readPermissionCallback},
38
+ \t\t)`);
39
+ }
40
+ if (methods.includes("update")) {
41
+ itemRoutes.push(`\t\tarray(
42
+ \t\t\t'methods' => WP_REST_Server::EDITABLE,
43
+ \t\t\t'callback' => ${buildRouteCallback(functions.updateHandlerName, "update_item")},
44
+ \t\t\t'permission_callback' => ${writePermissionCallback},
45
+ \t\t)`);
46
+ }
47
+ if (methods.includes("delete")) {
48
+ itemRoutes.push(`\t\tarray(
49
+ \t\t\t'methods' => WP_REST_Server::DELETABLE,
50
+ \t\t\t'callback' => ${buildRouteCallback(functions.deleteHandlerName, "delete_item")},
51
+ \t\t\t'permission_callback' => ${writePermissionCallback},
52
+ \t\t)`);
53
+ }
54
+ const registrations = [];
55
+ if (collectionRoutes.length > 0) {
56
+ registrations.push(`\tregister_rest_route(
57
+ \t\t$namespace,
58
+ \t\t'/${restResourceSlug}',
59
+ \t\tarray(
60
+ ${collectionRoutes.join(",\n")}
61
+ \t\t)
62
+ \t);`);
63
+ }
64
+ if (itemRoutes.length > 0) {
65
+ registrations.push(`\tregister_rest_route(
66
+ \t\t$namespace,
67
+ \t\t${quotePhpString(options.routePattern)},
68
+ \t\tarray(
69
+ ${itemRoutes.join(",\n")}
70
+ \t\t)
71
+ \t);`);
72
+ }
73
+ return registrations.join("\n\n");
74
+ }
75
+ function normalizeGlobalPhpClassName(classReference) {
76
+ const normalized = classReference.startsWith("\\")
77
+ ? classReference.slice(1)
78
+ : classReference;
79
+ return /^[A-Za-z_][A-Za-z0-9_]*$/u.test(normalized) ? normalized : undefined;
80
+ }
81
+ /**
82
+ * Normalize a configured PHP class name into a global `::class` reference.
83
+ */
84
+ export function toPhpClassConstantReference(classReference) {
85
+ const normalized = classReference.startsWith("\\")
86
+ ? classReference
87
+ : `\\${classReference}`;
88
+ return `${normalized}::class`;
89
+ }
90
+ /**
91
+ * Build an optional controller shim class for generated REST resource handlers.
92
+ */
93
+ export function buildRestResourceControllerClassSource(options) {
94
+ const controllerClassName = normalizeGlobalPhpClassName(options.controllerClass);
95
+ if (!controllerClassName) {
96
+ return "";
97
+ }
98
+ const extendsClause = options.controllerExtends
99
+ ? ` extends ${options.controllerExtends.startsWith("\\") ? options.controllerExtends : `\\${options.controllerExtends}`}`
100
+ : "";
101
+ return `
102
+ if ( ! class_exists( ${quotePhpString(controllerClassName)} ) ) {
103
+ \tclass ${controllerClassName}${extendsClause} {
104
+ \t\tpublic function can_manage_rest_resource() {
105
+ \t\t\treturn ${options.functions.canWriteFunctionName}();
106
+ \t\t}
107
+
108
+ \t\tpublic function list_items( WP_REST_Request $request ) {
109
+ \t\t\treturn ${options.functions.listHandlerName}( $request );
110
+ \t\t}
111
+
112
+ \t\tpublic function read_item( WP_REST_Request $request ) {
113
+ \t\t\treturn ${options.functions.readHandlerName}( $request );
114
+ \t\t}
115
+
116
+ \t\tpublic function create_item( WP_REST_Request $request ) {
117
+ \t\t\treturn ${options.functions.createHandlerName}( $request );
118
+ \t\t}
119
+
120
+ \t\tpublic function update_item( WP_REST_Request $request ) {
121
+ \t\t\treturn ${options.functions.updateHandlerName}( $request );
122
+ \t\t}
123
+
124
+ \t\tpublic function delete_item( WP_REST_Request $request ) {
125
+ \t\t\treturn ${options.functions.deleteHandlerName}( $request );
126
+ \t\t}
127
+ \t}
128
+ }
129
+ `;
130
+ }
131
+ /**
132
+ * Build the controller instantiation block used inside REST route registration.
133
+ */
134
+ export function buildRestResourceControllerBootstrapSource(controllerClass) {
135
+ if (!controllerClass) {
136
+ return "";
137
+ }
138
+ return `\t\t$controller_class = ${toPhpClassConstantReference(controllerClass)};
139
+ \t\tif ( ! class_exists( $controller_class ) ) {
140
+ \t\t\treturn;
141
+ \t\t}
142
+ \t\t$controller = new $controller_class();
143
+
144
+ `;
145
+ }
@@ -0,0 +1,9 @@
1
+ import type { WorkspaceProject } from "./workspace-project.js";
2
+ /**
3
+ * Ensure sync-rest can repair and validate plugin-level REST resources.
4
+ *
5
+ * @param workspace Workspace project whose sync-rest script should be patched.
6
+ * @returns A promise that resolves after the sync-rest script is updated.
7
+ * @throws When the generated sync-rest anchors cannot be found.
8
+ */
9
+ export declare function ensureRestResourceSyncScriptAnchors(workspace: WorkspaceProject): Promise<void>;