@wp-operations/wp-ops 0.2.0 → 0.3.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 (122) hide show
  1. package/README.md +30 -27
  2. package/bin/wp-app.js +2 -0
  3. package/bin/wp-code.js +2 -0
  4. package/bin/wp-ide.js +2 -0
  5. package/package.json +12 -3
  6. package/src/commands/platform.js +16 -2
  7. package/src/commands/sandbox.js +3 -3
  8. package/src/main.js +10 -4
  9. package/wp-operations/CHANGELOG.md +14 -0
  10. package/wp-operations/README.md +39 -0
  11. package/wp-operations/agents/builder/AGENT.md +15 -0
  12. package/wp-operations/assets/admin.css +631 -0
  13. package/wp-operations/assets/admin.js +614 -0
  14. package/wp-operations/assets/app/app.css +1 -0
  15. package/wp-operations/assets/app/app.js +70 -0
  16. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  17. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  18. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  19. package/wp-operations/assets/app/login.css +42 -0
  20. package/wp-operations/assets/brand/icon.svg +20 -0
  21. package/wp-operations/assets/brand/mark.svg +19 -0
  22. package/wp-operations/assets/editors/audiobook.css +311 -0
  23. package/wp-operations/assets/editors/audiofile.css +286 -0
  24. package/wp-operations/assets/editors/course.css +701 -0
  25. package/wp-operations/assets/editors/course.js +698 -0
  26. package/wp-operations/assets/editors/faq.css +258 -0
  27. package/wp-operations/assets/editors/faq.js +196 -0
  28. package/wp-operations/assets/editors/interactive.css +220 -0
  29. package/wp-operations/assets/editors/interactive.js +378 -0
  30. package/wp-operations/assets/editors/media-sources.css +3 -0
  31. package/wp-operations/assets/editors/media-sources.js +40 -0
  32. package/wp-operations/assets/editors/open-media.css +114 -0
  33. package/wp-operations/assets/editors/open-media.js +182 -0
  34. package/wp-operations/assets/editors/thumbnails.css +335 -0
  35. package/wp-operations/assets/editors/thumbnails.js +447 -0
  36. package/wp-operations/assets/editors/translator.css +818 -0
  37. package/wp-operations/functions.php +24 -0
  38. package/wp-operations/index.php +14 -0
  39. package/wp-operations/platform.json +1836 -0
  40. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  41. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  42. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  43. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  44. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  45. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  46. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  47. package/wp-operations/src/Admin.php +78 -0
  48. package/wp-operations/src/App/Access.php +57 -0
  49. package/wp-operations/src/App/Admin.php +442 -0
  50. package/wp-operations/src/App/App.php +157 -0
  51. package/wp-operations/src/App/Apps.php +379 -0
  52. package/wp-operations/src/App/Bundle.php +111 -0
  53. package/wp-operations/src/App/Contents.php +676 -0
  54. package/wp-operations/src/App/Engine.php +554 -0
  55. package/wp-operations/src/App/Github.php +308 -0
  56. package/wp-operations/src/App/Module.php +23 -0
  57. package/wp-operations/src/App/Ops.php +470 -0
  58. package/wp-operations/src/App/Packages.php +289 -0
  59. package/wp-operations/src/App/Rest.php +539 -0
  60. package/wp-operations/src/App/Sandboxes.php +206 -0
  61. package/wp-operations/src/App/Store.php +104 -0
  62. package/wp-operations/src/App/Usage.php +268 -0
  63. package/wp-operations/src/Config.php +249 -0
  64. package/wp-operations/src/Content/Editor.php +223 -0
  65. package/wp-operations/src/Content/Fields.php +681 -0
  66. package/wp-operations/src/Content/Scoring.php +253 -0
  67. package/wp-operations/src/Content/Structure.php +293 -0
  68. package/wp-operations/src/Content/Types.php +150 -0
  69. package/wp-operations/src/Content/Uploads.php +50 -0
  70. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  71. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  72. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  73. package/wp-operations/src/Editors/Audiofile.php +462 -0
  74. package/wp-operations/src/Editors/Course.php +784 -0
  75. package/wp-operations/src/Editors/Ebook.php +242 -0
  76. package/wp-operations/src/Editors/Editors.php +44 -0
  77. package/wp-operations/src/Editors/Faq.php +237 -0
  78. package/wp-operations/src/Editors/Files.php +139 -0
  79. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  80. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  81. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  82. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  83. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  84. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  85. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  86. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  87. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  88. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  89. package/wp-operations/src/Editors/Legal.php +235 -0
  90. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  91. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  92. package/wp-operations/src/Editors/Sources.php +28 -0
  93. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  94. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  95. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  96. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  97. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  98. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  99. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  100. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  101. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  102. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  103. package/wp-operations/src/Identity.php +104 -0
  104. package/wp-operations/src/Languages.php +45 -0
  105. package/wp-operations/src/Lockdown.php +85 -0
  106. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  107. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  108. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  109. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  110. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  111. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  112. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  113. package/wp-operations/src/Modules/Translations.php +133 -0
  114. package/wp-operations/src/Platform.php +59 -0
  115. package/wp-operations/src/Setup.php +273 -0
  116. package/wp-operations/src/Status.php +99 -0
  117. package/wp-operations/src/Storage/Client.php +168 -0
  118. package/wp-operations/src/Storage/Offload.php +120 -0
  119. package/wp-operations/src/Storage/Settings.php +166 -0
  120. package/wp-operations/src/Storage/Storage.php +35 -0
  121. package/wp-operations/src/Updates.php +151 -0
  122. package/wp-operations/style.css +11 -0
@@ -0,0 +1,379 @@
1
+ <?php
2
+ namespace Platform\App;
3
+
4
+ use Platform\Config;
5
+ use Platform\Content\Fields;
6
+ use Platform\Languages;
7
+ use Platform\Modules\Apps\Api;
8
+ use Platform\Modules\Apps\Apps as Registry;
9
+ use Platform\Modules\Apps\Schemes;
10
+ use Platform\Modules\Thumbnails;
11
+
12
+ defined( 'ABSPATH' ) || exit;
13
+
14
+ final class Apps {
15
+
16
+ const META = 'platform_apps';
17
+ const RECENT = 'platform_recent_apps';
18
+
19
+ const PALETTE = [
20
+ [ 'key' => 'primary', 'label' => 'Primary', 'value' => '#F4F4F5' ],
21
+ [ 'key' => 'accent', 'label' => 'Accent', 'value' => '#A1A1AA' ],
22
+ [ 'key' => 'background', 'label' => 'Background', 'value' => '#09090B' ],
23
+ [ 'key' => 'surface', 'label' => 'Surface', 'value' => '#18181B' ],
24
+ [ 'key' => 'text', 'label' => 'Text', 'value' => '#FAFAFA' ],
25
+ [ 'key' => 'muted', 'label' => 'Muted', 'value' => '#71717A' ],
26
+ ];
27
+
28
+ public static function meta( int $id ): array {
29
+ $all = (array) get_option( self::META, [] );
30
+ $meta = (array) ( $all[ $id ] ?? [] );
31
+
32
+ return $meta + [
33
+ 'category' => '',
34
+ 'classification' => '',
35
+ 'figma' => '',
36
+ 'description' => '',
37
+ 'palette' => self::PALETTE,
38
+ 'updated' => '',
39
+ ];
40
+ }
41
+
42
+ public static function put_meta( int $id, array $input ): array {
43
+ $all = (array) get_option( self::META, [] );
44
+ $current = self::meta( $id );
45
+ $palette = [];
46
+
47
+ foreach ( (array) ( $input['palette'] ?? $current['palette'] ) as $color ) {
48
+ $value = sanitize_hex_color( (string) ( $color['value'] ?? '' ) );
49
+ $label = sanitize_text_field( (string) ( $color['label'] ?? '' ) );
50
+
51
+ if ( $value && '' !== $label ) {
52
+ $palette[] = [ 'key' => sanitize_key( (string) ( $color['key'] ?? $label ) ), 'label' => $label, 'value' => $value ];
53
+ }
54
+ }
55
+
56
+ $figma = esc_url_raw( (string) ( $input['figma'] ?? $current['figma'] ) );
57
+
58
+ $all[ $id ] = [
59
+ 'category' => self::option( 'categories', (string) ( $input['category'] ?? $current['category'] ) ),
60
+ 'classification' => self::option( 'classifications', (string) ( $input['classification'] ?? $current['classification'] ) ),
61
+ 'figma' => self::is_figma( $figma ) ? $figma : '',
62
+ 'description' => sanitize_textarea_field( (string) ( $input['description'] ?? $current['description'] ) ),
63
+ 'palette' => $palette ?: self::PALETTE,
64
+ 'updated' => current_time( 'mysql', true ),
65
+ ];
66
+
67
+ update_option( self::META, $all, false );
68
+
69
+ return $all[ $id ];
70
+ }
71
+
72
+ public static function is_figma( string $url ): bool {
73
+ $host = (string) wp_parse_url( $url, PHP_URL_HOST );
74
+
75
+ return '' !== $url && in_array( $host, [ 'figma.com', 'www.figma.com' ], true );
76
+ }
77
+
78
+ private static function option( string $list, string $value ): string {
79
+ return in_array( $value, (array) Module::config( 'apps.' . $list, [] ), true ) ? $value : '';
80
+ }
81
+
82
+ public static function summary( array $app ): array {
83
+ $id = (int) $app['id'];
84
+ $meta = self::meta( $id );
85
+ $envs = [];
86
+
87
+ foreach ( Registry::environments() as $key => $label ) {
88
+ $envs[] = [
89
+ 'key' => $key,
90
+ 'label' => $label,
91
+ 'url' => (string) ( $app['environments'][ $key ]['url'] ?? '' ),
92
+ 'last_pull' => self::iso( (string) ( $app['environments'][ $key ]['last_pull'] ?? '' ) ),
93
+ ];
94
+ }
95
+
96
+ $map = Ops::mapping( $id, (string) $app['name'] );
97
+ $types = array_values( array_filter( array_keys( (array) $app['contents'] ), static fn( $key ): bool => 'thumbnail_group_id' !== $key ) );
98
+ $user = $app['user_id'] ? get_userdata( (int) $app['user_id'] ) : false;
99
+
100
+ return [
101
+ 'id' => $id,
102
+ 'name' => (string) $app['name'],
103
+ 'created' => (string) ( $app['created_at'] ?? '' ),
104
+ 'updated' => $meta['updated'] ?: (string) ( $app['created_at'] ?? '' ),
105
+ 'environments' => $envs,
106
+ 'url' => (string) ( $app['environments']['prod']['url'] ?? '' ),
107
+ 'languages' => array_values( (array) $app['languages'] ),
108
+ 'types' => $types,
109
+ 'user' => $user ? $user->user_login : '',
110
+ 'category' => $meta['category'],
111
+ 'classification' => $meta['classification'],
112
+ 'figma' => $meta['figma'],
113
+ 'description' => $meta['description'],
114
+ 'palette' => $meta['palette'],
115
+ 'folder' => $map['path'],
116
+ 'kind' => $map['label'],
117
+ ];
118
+ }
119
+
120
+ private static function iso( string $raw ): string {
121
+ $raw = trim( $raw );
122
+
123
+ if ( '' === $raw ) {
124
+ return '';
125
+ }
126
+
127
+ $time = ctype_digit( $raw ) ? (int) $raw : strtotime( $raw . ( preg_match( '/(Z|[+-]\d{2}:?\d{2}|UTC|GMT)$/i', $raw ) ? '' : ' UTC' ) );
128
+
129
+ return $time ? gmdate( 'c', $time ) : '';
130
+ }
131
+
132
+ public static function all(): array {
133
+ $recent = (array) get_user_meta( get_current_user_id(), self::RECENT, true );
134
+
135
+ return [
136
+ 'items' => array_map( [ __CLASS__, 'summary' ], Registry::all() ),
137
+ 'recent' => array_values( array_map( 'intval', $recent ) ),
138
+ ];
139
+ }
140
+
141
+ public static function touch( int $id ): void {
142
+ $recent = array_values( array_diff( array_map( 'intval', (array) get_user_meta( get_current_user_id(), self::RECENT, true ) ), [ $id ] ) );
143
+ array_unshift( $recent, $id );
144
+ update_user_meta( get_current_user_id(), self::RECENT, array_slice( $recent, 0, 12 ) );
145
+ }
146
+
147
+ public static function detail( int $id ): ?array {
148
+ $app = Registry::get( $id );
149
+
150
+ if ( ! $app ) {
151
+ return null;
152
+ }
153
+
154
+ self::touch( $id );
155
+
156
+ $contents = [];
157
+
158
+ foreach ( Api::endpoints() as $name => $type ) {
159
+ $value = (array) ( $app['contents'][ $name ] ?? [] );
160
+
161
+ $contents[] = [
162
+ 'name' => $name,
163
+ 'label' => $type['label'],
164
+ 'type' => $type['key'],
165
+ 'enabled' => isset( $app['contents'][ $name ] ),
166
+ 'pinned' => self::posts( array_map( 'intval', (array) ( $value['pinned'] ?? [] ) ) ),
167
+ 'blocked' => self::posts( array_map( 'intval', (array) ( $value['blocked'] ?? [] ) ) ),
168
+ 'include_by_terms' => array_values( array_map( 'intval', (array) ( $value['include_by_terms'] ?? [] ) ) ),
169
+ 'exclude_by_terms' => array_values( array_map( 'intval', (array) ( $value['exclude_by_terms'] ?? [] ) ) ),
170
+ 'taxonomies' => array_values( array_map( [ Contents::class, 'taxonomy' ], Config::taxonomies_for( $type['key'] ) ) ),
171
+ ];
172
+ }
173
+
174
+ $links = [];
175
+
176
+ foreach ( Registry::links() as $key => $link ) {
177
+ $options = [];
178
+
179
+ if ( ! empty( $link['taxonomy'] ) ) {
180
+ foreach ( get_terms( [ 'taxonomy' => (string) $link['taxonomy'], 'hide_empty' => false ] ) as $term ) {
181
+ $options[] = [ 'id' => (int) $term->term_id, 'name' => $term->name ];
182
+ }
183
+ } elseif ( ! empty( $link['type'] ) ) {
184
+ foreach ( get_posts( [ 'post_type' => (string) $link['type'], 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ] ) as $post ) {
185
+ $options[] = [ 'id' => (int) $post->ID, 'name' => $post->post_title ];
186
+ }
187
+ }
188
+
189
+ $links[] = [ 'key' => $key, 'label' => (string) $link['label'], 'value' => (int) ( $app['links'][ $key ] ?? 0 ), 'options' => $options ];
190
+ }
191
+
192
+ $schemes = [];
193
+
194
+ foreach ( Schemes::all() as $key => $scheme ) {
195
+ $schemes[] = $scheme + [ 'records' => Schemes::records( $key ) ];
196
+ }
197
+
198
+ $users = [];
199
+
200
+ foreach ( get_users( [ 'role' => Registry::ROLE, 'orderby' => 'display_name', 'number' => 500 ] ) as $user ) {
201
+ $users[] = [ 'id' => (int) $user->ID, 'name' => $user->display_name, 'login' => $user->user_login ];
202
+ }
203
+
204
+ return self::summary( $app ) + [
205
+ 'user_id' => (int) $app['user_id'],
206
+ 'contents' => $contents,
207
+ 'thumbnail_group' => (int) ( $app['contents']['thumbnail_group_id'] ?? 0 ),
208
+ 'links' => $links,
209
+ 'metadata' => (array) $app['metadata'],
210
+ 'metadata_fields' => Registry::metadata_fields(),
211
+ 'schemes' => $schemes,
212
+ 'users' => $users,
213
+ 'groups' => array_map( static fn( array $g ): array => [ 'id' => (int) $g['id'], 'name' => (string) $g['name'], 'items' => (int) ( $g['items'] ?? 0 ) ], Thumbnails::groups() ),
214
+ 'all_languages' => Languages::all(),
215
+ ];
216
+ }
217
+
218
+ private static function posts( array $ids ): array {
219
+ $ids = array_values( array_filter( $ids ) );
220
+
221
+ if ( ! $ids ) {
222
+ return [];
223
+ }
224
+
225
+ return array_map(
226
+ static fn( \WP_Post $post ): array => [ 'id' => (int) $post->ID, 'title' => $post->post_title ],
227
+ get_posts( [ 'post__in' => $ids, 'post_type' => 'any', 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => 'post__in' ] )
228
+ );
229
+ }
230
+
231
+ public static function save( int $id, array $input ) {
232
+ $existing = $id ? Registry::get( $id ) : null;
233
+
234
+ if ( $id && ! $existing ) {
235
+ return new \WP_Error( 'not_found', 'App not found.', [ 'status' => 404 ] );
236
+ }
237
+
238
+ $name = sanitize_text_field( (string) ( $input['name'] ?? $existing['name'] ?? '' ) );
239
+
240
+ if ( '' === $name ) {
241
+ return new \WP_Error( 'name', 'Give the app a name.', [ 'status' => 400 ] );
242
+ }
243
+
244
+ $data = [
245
+ 'name' => $name,
246
+ 'user_id' => (int) ( $input['user_id'] ?? $existing['user_id'] ?? 0 ),
247
+ 'environments' => self::environments( (array) ( $input['environments'] ?? [] ), (array) ( $existing['environments'] ?? [] ) ),
248
+ 'contents' => isset( $input['contents'] ) ? self::contents( (array) $input['contents'] ) : (array) ( $existing['contents'] ?? [] ),
249
+ 'links' => self::links( (array) ( $input['links'] ?? [] ), (array) ( $existing['links'] ?? [] ) ),
250
+ 'languages' => array_values( array_intersect( array_map( 'sanitize_key', (array) ( $input['languages'] ?? $existing['languages'] ?? [] ) ), array_keys( Languages::all() ) ) ),
251
+ 'metadata' => isset( $input['metadata'] ) ? self::metadata( (array) $input['metadata'] ) : (array) ( $existing['metadata'] ?? [] ),
252
+ ];
253
+
254
+ if ( is_wp_error( $data['contents'] ) ) {
255
+ return $data['contents'];
256
+ }
257
+
258
+ if ( isset( $input['thumbnail_group'] ) ) {
259
+ $data['contents']['thumbnail_group_id'] = absint( $input['thumbnail_group'] );
260
+ }
261
+
262
+ if ( ! empty( $input['create_user'] ) && ! $data['user_id'] ) {
263
+ $user_id = Registry::create_user( $name );
264
+
265
+ if ( is_wp_error( $user_id ) ) {
266
+ return $user_id;
267
+ }
268
+
269
+ $data['user_id'] = (int) $user_id;
270
+ }
271
+
272
+ $id = Registry::save( $id, $data );
273
+
274
+ if ( ! $id ) {
275
+ return new \WP_Error( 'save', 'The app could not be saved.', [ 'status' => 500 ] );
276
+ }
277
+
278
+ self::put_meta( $id, $input );
279
+
280
+ return self::detail( $id );
281
+ }
282
+
283
+ private static function environments( array $input, array $existing ): array {
284
+ $out = [];
285
+
286
+ foreach ( array_keys( Registry::environments() ) as $env ) {
287
+ $url = array_key_exists( $env, $input ) ? (string) ( is_array( $input[ $env ] ) ? ( $input[ $env ]['url'] ?? '' ) : $input[ $env ] ) : (string) ( $existing[ $env ]['url'] ?? '' );
288
+
289
+ $out[ $env ] = [
290
+ 'url' => Registry::https( $url ),
291
+ 'last_pull' => (string) ( $existing[ $env ]['last_pull'] ?? '' ),
292
+ ];
293
+ }
294
+
295
+ return $out;
296
+ }
297
+
298
+ private static function contents( array $input ) {
299
+ $out = [];
300
+ $errors = [];
301
+ $names = Api::endpoints();
302
+
303
+ foreach ( $input as $row ) {
304
+ $name = sanitize_key( (string) ( $row['name'] ?? '' ) );
305
+
306
+ if ( ! isset( $names[ $name ] ) || empty( $row['enabled'] ) ) {
307
+ continue;
308
+ }
309
+
310
+ $pinned = self::ids( $row['pinned'] ?? [] );
311
+ $include = self::ids( $row['include_by_terms'] ?? [] );
312
+
313
+ $out[ $name ] = [
314
+ 'pinned' => $pinned,
315
+ 'blocked' => array_values( array_diff( self::ids( $row['blocked'] ?? [] ), $pinned ) ),
316
+ 'include_by_terms' => $include,
317
+ 'exclude_by_terms' => array_values( array_diff( self::ids( $row['exclude_by_terms'] ?? [] ), $include ) ),
318
+ ];
319
+
320
+ if ( ! $pinned && ! $include ) {
321
+ $errors[] = sprintf( '"%s" has nothing included. Include items or categories, or turn it off.', $names[ $name ]['label'] );
322
+ }
323
+ }
324
+
325
+ return $errors ? new \WP_Error( 'contents', implode( ' ', $errors ), [ 'status' => 400 ] ) : $out;
326
+ }
327
+
328
+ private static function ids( $list ): array {
329
+ $ids = [];
330
+
331
+ foreach ( (array) $list as $item ) {
332
+ $ids[] = (int) ( is_array( $item ) ? ( $item['id'] ?? 0 ) : $item );
333
+ }
334
+
335
+ return array_values( array_unique( array_filter( $ids ) ) );
336
+ }
337
+
338
+ private static function links( array $input, array $existing ): array {
339
+ $out = [];
340
+
341
+ foreach ( array_keys( Registry::links() ) as $key ) {
342
+ $out[ $key ] = absint( $input[ $key ] ?? $existing[ $key ] ?? 0 );
343
+ }
344
+
345
+ return $out;
346
+ }
347
+
348
+ private static function metadata( array $input ): array {
349
+ $out = [];
350
+
351
+ foreach ( Registry::metadata_fields() as $field ) {
352
+ $out[ $field['key'] ] = 'checkbox' === $field['type']
353
+ ? ( empty( $input[ $field['key'] ] ) ? '' : '1' )
354
+ : Fields::sanitize( $field, $input[ $field['key'] ] ?? '' );
355
+ }
356
+
357
+ foreach ( $input as $key => $value ) {
358
+ $key = sanitize_key( (string) $key );
359
+
360
+ if ( '' !== $key && ! isset( $out[ $key ] ) && is_scalar( $value ) ) {
361
+ $out[ $key ] = sanitize_text_field( (string) $value );
362
+ }
363
+ }
364
+
365
+ return $out;
366
+ }
367
+
368
+ public static function sync( int $id, array $input, string $env ) {
369
+ $saved = self::save( $id, $input );
370
+
371
+ if ( is_wp_error( $saved ) ) {
372
+ return $saved;
373
+ }
374
+
375
+ $pushed = Registry::push( $id, $env );
376
+
377
+ return is_wp_error( $pushed ) ? new \WP_Error( $pushed->get_error_code(), 'Saved, but the push failed: ' . $pushed->get_error_message(), [ 'status' => 502, 'app' => self::detail( $id ) ] ) : self::detail( $id );
378
+ }
379
+ }
@@ -0,0 +1,111 @@
1
+ <?php
2
+ namespace Platform\App;
3
+
4
+ use Platform\Platform;
5
+
6
+ defined( 'ABSPATH' ) || exit;
7
+
8
+ /**
9
+ * Puts the skills and agents shipped in the theme (skills/, agents/) into the CMS.
10
+ * A shipped item is refreshed on theme updates until someone edits it.
11
+ */
12
+ final class Bundle {
13
+
14
+ const VERSION = 'platform_bundle_synced';
15
+
16
+ const FOLDERS = [
17
+ 'skills' => 'skills/*/SKILL.md',
18
+ 'agents' => 'agents/*/AGENT.md',
19
+ ];
20
+
21
+ public static function maybe_sync(): void {
22
+ $version = (string) wp_get_theme( get_template() )->get( 'Version' );
23
+
24
+ if ( get_option( self::VERSION ) !== $version ) {
25
+ self::sync();
26
+ update_option( self::VERSION, $version, false );
27
+ }
28
+ }
29
+
30
+ public static function sync(): void {
31
+ foreach ( array_keys( self::FOLDERS ) as $collection ) {
32
+ $index = array_column( Store::all( $collection ), null, 'id' );
33
+
34
+ foreach ( self::shipped( $collection ) as $item ) {
35
+ $current = $index[ $item['id'] ] ?? null;
36
+
37
+ if ( null === $current ) {
38
+ $index[ $item['id'] ] = $item + [ 'apps' => [], 'enabled' => true, 'created' => current_time( 'mysql', true ) ];
39
+ } elseif ( 'theme' === ( $current['source'] ?? '' ) && md5( (string) ( $current['instructions'] ?? '' ) ) === ( $current['hash'] ?? '' ) ) {
40
+ $index[ $item['id'] ] = array_merge( $current, $item );
41
+ }
42
+ }
43
+
44
+ Store::replace( $collection, array_values( $index ) );
45
+ }
46
+ }
47
+
48
+ public static function shipped( string $collection ): array {
49
+ $items = [];
50
+
51
+ foreach ( (array) glob( Platform::path( self::FOLDERS[ $collection ] ) ) as $file ) {
52
+ $parsed = self::parse( (string) file_get_contents( $file ) );
53
+
54
+ if ( '' === $parsed['instructions'] ) {
55
+ continue;
56
+ }
57
+
58
+ $slug = basename( dirname( $file ) );
59
+ $item = [
60
+ 'id' => self::id( $slug ),
61
+ 'name' => $parsed['name'] ?: $slug,
62
+ 'description' => $parsed['description'],
63
+ 'instructions' => $parsed['instructions'],
64
+ 'source' => 'theme',
65
+ 'hash' => md5( $parsed['instructions'] ),
66
+ 'updated' => current_time( 'mysql', true ),
67
+ ];
68
+
69
+ $items[] = 'agents' === $collection
70
+ ? $item + [ 'role' => $parsed['role'] ?: 'builder', 'skills' => array_map( [ __CLASS__, 'id' ], $parsed['skills'] ), 'connectors' => [] ]
71
+ : $item + [ 'trigger' => $parsed['trigger'] ];
72
+ }
73
+
74
+ return $items;
75
+ }
76
+
77
+ public static function id( string $slug ): string {
78
+ return preg_replace( '/[^a-z0-9]/', '', strtolower( $slug ) );
79
+ }
80
+
81
+ private static function parse( string $text ): array {
82
+ $text = str_replace( "\r\n", "\n", $text );
83
+ $out = [ 'name' => '', 'description' => '', 'trigger' => '', 'role' => '', 'skills' => [], 'instructions' => trim( $text ) ];
84
+
85
+ if ( ! preg_match( '/\A---\n(.*?)\n---\n(.*)\z/s', $text, $parts ) ) {
86
+ return $out;
87
+ }
88
+
89
+ $out['instructions'] = trim( $parts[2] );
90
+ $key = '';
91
+
92
+ foreach ( explode( "\n", $parts[1] ) as $line ) {
93
+ if ( preg_match( '/^([a-z_]+):\s*(.*)$/', $line, $field ) ) {
94
+ $key = $field[1];
95
+ $value = trim( $field[2], " \"'" );
96
+
97
+ if ( 'skills' === $key ) {
98
+ $out['skills'] = '' === $value ? [] : array_filter( array_map( 'trim', explode( ',', trim( $value, '[]' ) ) ) );
99
+ } elseif ( isset( $out[ $key ] ) && 'instructions' !== $key ) {
100
+ $out[ $key ] = in_array( $value, [ '>', '|' ], true ) ? '' : $value;
101
+ }
102
+ } elseif ( 'skills' === $key && preg_match( '/^\s+-\s*(\S+)/', $line, $entry ) ) {
103
+ $out['skills'][] = $entry[1];
104
+ } elseif ( isset( $out[ $key ] ) && is_string( $out[ $key ] ) && 'instructions' !== $key && '' !== trim( $line ) ) {
105
+ $out[ $key ] = trim( $out[ $key ] . ' ' . trim( $line ) );
106
+ }
107
+ }
108
+
109
+ return $out;
110
+ }
111
+ }