@wp-operations/wp-ops 0.3.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 (117) hide show
  1. package/package.json +6 -2
  2. package/src/commands/platform.js +16 -2
  3. package/src/main.js +9 -3
  4. package/wp-operations/CHANGELOG.md +14 -0
  5. package/wp-operations/README.md +39 -0
  6. package/wp-operations/agents/builder/AGENT.md +15 -0
  7. package/wp-operations/assets/admin.css +631 -0
  8. package/wp-operations/assets/admin.js +614 -0
  9. package/wp-operations/assets/app/app.css +1 -0
  10. package/wp-operations/assets/app/app.js +70 -0
  11. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  12. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  13. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  14. package/wp-operations/assets/app/login.css +42 -0
  15. package/wp-operations/assets/brand/icon.svg +20 -0
  16. package/wp-operations/assets/brand/mark.svg +19 -0
  17. package/wp-operations/assets/editors/audiobook.css +311 -0
  18. package/wp-operations/assets/editors/audiofile.css +286 -0
  19. package/wp-operations/assets/editors/course.css +701 -0
  20. package/wp-operations/assets/editors/course.js +698 -0
  21. package/wp-operations/assets/editors/faq.css +258 -0
  22. package/wp-operations/assets/editors/faq.js +196 -0
  23. package/wp-operations/assets/editors/interactive.css +220 -0
  24. package/wp-operations/assets/editors/interactive.js +378 -0
  25. package/wp-operations/assets/editors/media-sources.css +3 -0
  26. package/wp-operations/assets/editors/media-sources.js +40 -0
  27. package/wp-operations/assets/editors/open-media.css +114 -0
  28. package/wp-operations/assets/editors/open-media.js +182 -0
  29. package/wp-operations/assets/editors/thumbnails.css +335 -0
  30. package/wp-operations/assets/editors/thumbnails.js +447 -0
  31. package/wp-operations/assets/editors/translator.css +818 -0
  32. package/wp-operations/functions.php +24 -0
  33. package/wp-operations/index.php +14 -0
  34. package/wp-operations/platform.json +1836 -0
  35. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  36. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  37. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  38. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  39. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  40. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  41. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  42. package/wp-operations/src/Admin.php +78 -0
  43. package/wp-operations/src/App/Access.php +57 -0
  44. package/wp-operations/src/App/Admin.php +442 -0
  45. package/wp-operations/src/App/App.php +157 -0
  46. package/wp-operations/src/App/Apps.php +379 -0
  47. package/wp-operations/src/App/Bundle.php +111 -0
  48. package/wp-operations/src/App/Contents.php +676 -0
  49. package/wp-operations/src/App/Engine.php +554 -0
  50. package/wp-operations/src/App/Github.php +308 -0
  51. package/wp-operations/src/App/Module.php +23 -0
  52. package/wp-operations/src/App/Ops.php +470 -0
  53. package/wp-operations/src/App/Packages.php +289 -0
  54. package/wp-operations/src/App/Rest.php +539 -0
  55. package/wp-operations/src/App/Sandboxes.php +206 -0
  56. package/wp-operations/src/App/Store.php +104 -0
  57. package/wp-operations/src/App/Usage.php +268 -0
  58. package/wp-operations/src/Config.php +249 -0
  59. package/wp-operations/src/Content/Editor.php +223 -0
  60. package/wp-operations/src/Content/Fields.php +681 -0
  61. package/wp-operations/src/Content/Scoring.php +253 -0
  62. package/wp-operations/src/Content/Structure.php +293 -0
  63. package/wp-operations/src/Content/Types.php +150 -0
  64. package/wp-operations/src/Content/Uploads.php +50 -0
  65. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  66. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  67. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  68. package/wp-operations/src/Editors/Audiofile.php +462 -0
  69. package/wp-operations/src/Editors/Course.php +784 -0
  70. package/wp-operations/src/Editors/Ebook.php +242 -0
  71. package/wp-operations/src/Editors/Editors.php +44 -0
  72. package/wp-operations/src/Editors/Faq.php +237 -0
  73. package/wp-operations/src/Editors/Files.php +139 -0
  74. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  75. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  76. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  77. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  78. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  79. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  80. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  81. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  82. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  83. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  84. package/wp-operations/src/Editors/Legal.php +235 -0
  85. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  86. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  87. package/wp-operations/src/Editors/Sources.php +28 -0
  88. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  89. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  90. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  91. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  92. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  93. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  94. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  95. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  96. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  97. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  98. package/wp-operations/src/Identity.php +104 -0
  99. package/wp-operations/src/Languages.php +45 -0
  100. package/wp-operations/src/Lockdown.php +85 -0
  101. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  102. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  103. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  104. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  105. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  106. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  107. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  108. package/wp-operations/src/Modules/Translations.php +133 -0
  109. package/wp-operations/src/Platform.php +59 -0
  110. package/wp-operations/src/Setup.php +273 -0
  111. package/wp-operations/src/Status.php +99 -0
  112. package/wp-operations/src/Storage/Client.php +168 -0
  113. package/wp-operations/src/Storage/Offload.php +120 -0
  114. package/wp-operations/src/Storage/Settings.php +166 -0
  115. package/wp-operations/src/Storage/Storage.php +35 -0
  116. package/wp-operations/src/Updates.php +151 -0
  117. package/wp-operations/style.css +11 -0
@@ -0,0 +1,470 @@
1
+ <?php
2
+ namespace Platform\App;
3
+
4
+ use Platform\Config;
5
+ use Platform\Modules\Apps\Apps as Registry;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ final class Ops {
10
+
11
+ const OPTION = 'platform_ops';
12
+ const PAGE = 'operations';
13
+ const LAYOUT = '.github/platform.json';
14
+
15
+ public static function defaults(): array {
16
+ return [
17
+ 'url' => '',
18
+ 'repo' => '',
19
+ 'branch' => '',
20
+ 'packages' => 'packages',
21
+ 'kinds' => [
22
+ [ 'key' => 'service-apps', 'label' => 'Service apps', 'folder' => 'service-apps', 'manifest' => '.github/service-apps.json' ],
23
+ [ 'key' => 'homepages', 'label' => 'Homepages', 'folder' => 'homepages', 'manifest' => '.github/homepages.json' ],
24
+ ],
25
+ 'apps' => [],
26
+ 'workflows' => [ 'apps' => 'deploy-apps.yml', 'platform' => 'deploy-platform.yml', 'composer' => 'composer.yml' ],
27
+ 'sandboxes' => [],
28
+ ];
29
+ }
30
+
31
+ public static function settings(): array {
32
+ $saved = (array) get_option( self::OPTION, [] );
33
+ $settings = array_merge( self::defaults(), array_intersect_key( $saved, self::defaults() ) );
34
+
35
+ $settings['kinds'] = array_values( array_filter( (array) $settings['kinds'], static fn( $kind ): bool => is_array( $kind ) && '' !== (string) ( $kind['key'] ?? '' ) ) );
36
+ $settings['workflows'] = (array) $settings['workflows'] + self::defaults()['workflows'];
37
+
38
+ return $settings;
39
+ }
40
+
41
+ private static function store( array $settings ): void {
42
+ update_option( self::OPTION, array_intersect_key( $settings, self::defaults() ), false );
43
+ delete_transient( 'platform_ops_default_branch' );
44
+ }
45
+
46
+ public static function slug( string $value ): string {
47
+ $slug = trim( preg_replace( '/[^A-Za-z0-9._-]+/', '-', trim( $value ) ), '-' );
48
+
49
+ return '.' === $slug || '..' === $slug ? '' : rtrim( $slug, '.' );
50
+ }
51
+
52
+ public static function folder( string $value ): string {
53
+ $parts = array_filter( array_map( [ __CLASS__, 'slug' ], explode( '/', str_replace( '\\', '/', $value ) ) ), 'strlen' );
54
+
55
+ return implode( '/', $parts );
56
+ }
57
+
58
+ public static function kind( string $key ): ?array {
59
+ foreach ( self::settings()['kinds'] as $kind ) {
60
+ if ( $kind['key'] === $key ) {
61
+ return $kind;
62
+ }
63
+ }
64
+
65
+ return null;
66
+ }
67
+
68
+ public static function mapping( int $app_id, string $name = '' ): array {
69
+ $settings = self::settings();
70
+ $saved = (array) ( $settings['apps'][ $app_id ] ?? [] );
71
+ $kind = self::kind( (string) ( $saved['kind'] ?? '' ) ) ?? ( $settings['kinds'][0] ?? null );
72
+ $slug = self::slug( (string) ( $saved['slug'] ?? '' ) ?: $name );
73
+
74
+ return [
75
+ 'kind' => $kind ? $kind['key'] : '',
76
+ 'label' => $kind ? (string) $kind['label'] : '',
77
+ 'slug' => $slug,
78
+ 'path' => $kind && '' !== $slug ? $kind['folder'] . '/' . $slug : '',
79
+ 'mapped' => ! empty( $saved['kind'] ),
80
+ ];
81
+ }
82
+
83
+ public static function app_path( array $app ): string {
84
+ return self::mapping( (int) $app['id'], (string) $app['name'] )['path'];
85
+ }
86
+
87
+ public static function summary(): array {
88
+ $settings = self::settings();
89
+
90
+ return [
91
+ 'repo' => $settings['repo'],
92
+ 'url' => $settings['url'],
93
+ 'branch' => $settings['branch'],
94
+ 'token' => '' !== $settings['repo'] && Github::ready(),
95
+ 'packages' => $settings['packages'],
96
+ 'kinds' => array_map( static fn( array $k ): array => [ 'key' => $k['key'], 'label' => $k['label'], 'folder' => $k['folder'] ], $settings['kinds'] ),
97
+ 'sandboxes' => count( $settings['sandboxes'] ),
98
+ 'admin' => '/settings/operations',
99
+ ];
100
+ }
101
+
102
+ public static function links( string $path ): array {
103
+ $settings = self::settings();
104
+
105
+ if ( '' === $settings['repo'] ) {
106
+ return [];
107
+ }
108
+
109
+ [ $owner, $repo ] = explode( '/', $settings['repo'] );
110
+ $branch = $settings['branch'] ?: 'main';
111
+ $links = [];
112
+
113
+ foreach ( (array) Config::get( 'ops.sandboxes', [] ) as $sandbox ) {
114
+ $links[] = [
115
+ 'key' => (string) ( $sandbox['key'] ?? '' ),
116
+ 'label' => (string) ( $sandbox['label'] ?? '' ),
117
+ 'url' => strtr( (string) ( $sandbox['url'] ?? '' ), [ '{owner}' => $owner, '{repo}' => $repo, '{branch}' => rawurlencode( $branch ), '{path}' => $path ] ),
118
+ ];
119
+ }
120
+
121
+ return $links;
122
+ }
123
+
124
+ public static function layout() {
125
+ $tree = Github::tree();
126
+
127
+ if ( is_wp_error( $tree ) ) {
128
+ return $tree;
129
+ }
130
+
131
+ $data = Github::json( self::LAYOUT, $tree['files'] );
132
+
133
+ if ( null === $data ) {
134
+ return new \WP_Error( 'layout', sprintf( '%s was not found in the repository.', self::LAYOUT ), [ 'status' => 404 ] );
135
+ }
136
+
137
+ if ( is_wp_error( $data ) ) {
138
+ return $data;
139
+ }
140
+
141
+ $kinds = [];
142
+
143
+ foreach ( (array) ( $data['apps'] ?? [] ) as $key => $kind ) {
144
+ $key = self::slug( (string) $key );
145
+ $kind = is_string( $kind ) ? [ 'manifest' => $kind ] : (array) $kind;
146
+ $folder = self::folder( (string) ( $kind['folder'] ?? $key ) );
147
+
148
+ if ( '' !== $key && '' !== $folder ) {
149
+ $kinds[] = [
150
+ 'key' => $key,
151
+ 'label' => sanitize_text_field( (string) ( $kind['label'] ?? ucwords( str_replace( '-', ' ', $key ) ) ) ),
152
+ 'folder' => $folder,
153
+ 'manifest' => self::folder( (string) ( $kind['manifest'] ?? '.github/' . $key . '.json' ) ),
154
+ ];
155
+ }
156
+ }
157
+
158
+ return [
159
+ 'platform' => (array) ( $data['platform'] ?? [] ),
160
+ 'packages' => self::folder( (string) ( $data['packages'] ?? 'packages' ) ) ?: 'packages',
161
+ 'kinds' => $kinds,
162
+ ];
163
+ }
164
+
165
+ public static function tree( int $app_id ) {
166
+ $app = Registry::get( $app_id );
167
+
168
+ if ( ! $app ) {
169
+ return new \WP_Error( 'not_found', 'App not found.', [ 'status' => 404 ] );
170
+ }
171
+
172
+ $settings = self::settings();
173
+ $mapping = self::mapping( $app_id, (string) $app['name'] );
174
+ $result = [
175
+ 'connected' => '' !== $settings['repo'],
176
+ 'ready' => Github::ready(),
177
+ 'repo' => $settings['repo'],
178
+ 'branch' => $settings['branch'],
179
+ 'path' => $mapping['path'],
180
+ 'kind' => $mapping['label'],
181
+ 'files' => [],
182
+ 'links' => self::links( $mapping['path'] ),
183
+ 'truncated' => false,
184
+ 'error' => '',
185
+ ];
186
+
187
+ if ( ! $result['ready'] || '' === $mapping['path'] ) {
188
+ return $result;
189
+ }
190
+
191
+ $tree = Github::tree();
192
+
193
+ if ( is_wp_error( $tree ) ) {
194
+ $result['error'] = $tree->get_error_message();
195
+
196
+ return $result;
197
+ }
198
+
199
+ $prefix = $mapping['path'] . '/';
200
+
201
+ $result['branch'] = $tree['branch'];
202
+ $result['truncated'] = $tree['truncated'];
203
+ $result['files'] = array_values(
204
+ array_map(
205
+ static fn( array $file ): array => [ 'path' => substr( $file['path'], strlen( $prefix ) ), 'size' => $file['size'] ],
206
+ array_filter( $tree['files'], static fn( array $file ): bool => 0 === strpos( $file['path'], $prefix ) && false === strpos( $file['path'], '/node_modules/' ) && false === strpos( $file['path'], '/vendor/' ) )
207
+ )
208
+ );
209
+
210
+ if ( ! $result['files'] ) {
211
+ $result['error'] = sprintf( 'Nothing in %s on %s yet.', $mapping['path'], $tree['branch'] );
212
+ }
213
+
214
+ return $result;
215
+ }
216
+
217
+ public static function file( int $app_id, string $file ) {
218
+ $app = Registry::get( $app_id );
219
+
220
+ if ( ! $app ) {
221
+ return new \WP_Error( 'not_found', 'App not found.', [ 'status' => 404 ] );
222
+ }
223
+
224
+ $file = ltrim( str_replace( '\\', '/', $file ), '/' );
225
+
226
+ if ( '' === $file || false !== strpos( $file, '..' ) ) {
227
+ return new \WP_Error( 'path', 'Invalid file path.', [ 'status' => 400 ] );
228
+ }
229
+
230
+ $path = self::app_path( $app );
231
+ $data = Github::read( $path . '/' . $file );
232
+
233
+ if ( is_wp_error( $data ) ) {
234
+ return $data;
235
+ }
236
+
237
+ $binary = false !== strpos( substr( $data['content'], 0, 8000 ), "\0" );
238
+
239
+ return [ 'path' => $file, 'binary' => $binary, 'content' => $binary ? '' : $data['content'], 'url' => $data['url'] ];
240
+ }
241
+
242
+ public static function status(): array {
243
+ $settings = self::settings();
244
+ $status = [ 'repo' => null, 'branches' => [], 'workflows' => [], 'runs' => [], 'secrets' => [], 'environments' => [], 'error' => '' ];
245
+
246
+ if ( ! Github::ready() ) {
247
+ return $status;
248
+ }
249
+
250
+ $repo = Github::repo_call( 'GET', '' );
251
+
252
+ if ( is_wp_error( $repo ) ) {
253
+ $status['error'] = $repo->get_error_message();
254
+
255
+ return $status;
256
+ }
257
+
258
+ $status['repo'] = [ 'name' => (string) $repo['full_name'], 'private' => ! empty( $repo['private'] ), 'default' => (string) $repo['default_branch'], 'url' => (string) $repo['html_url'] ];
259
+
260
+ $calls = [
261
+ 'branches' => [ 'branches?per_page=100', null ],
262
+ 'workflows' => [ 'actions/workflows?per_page=100', 'workflows' ],
263
+ 'runs' => [ 'actions/runs?per_page=10', 'workflow_runs' ],
264
+ 'secrets' => [ 'actions/secrets?per_page=100', 'secrets' ],
265
+ 'environments' => [ 'environments?per_page=100', 'environments' ],
266
+ ];
267
+
268
+ foreach ( $calls as $key => [ $path, $field ] ) {
269
+ $data = Github::repo_call( 'GET', $path );
270
+
271
+ if ( ! is_wp_error( $data ) ) {
272
+ $status[ $key ] = null === $field ? $data : (array) ( $data[ $field ] ?? [] );
273
+ }
274
+ }
275
+
276
+ foreach ( $status['environments'] as $index => $environment ) {
277
+ $secrets = Github::repo_call( 'GET', Github::secrets_path( (string) $environment['name'] ) . '?per_page=100' );
278
+
279
+ $status['environments'][ $index ]['secrets'] = is_wp_error( $secrets ) ? [] : (array) ( $secrets['secrets'] ?? [] );
280
+ }
281
+
282
+ unset( $settings );
283
+
284
+ return $status;
285
+ }
286
+
287
+ public static function put_secret( string $name, string $value, string $environment ) {
288
+ $name = strtoupper( preg_replace( '/[^A-Za-z0-9_]/', '', $name ) );
289
+ $environment = sanitize_text_field( $environment );
290
+
291
+ if ( '' === $name || 0 === strpos( $name, 'GITHUB_' ) || ctype_digit( $name[0] ) ) {
292
+ return new \WP_Error( 'secret', 'Secret names use letters, digits and underscores, cannot start with a digit or GITHUB_.', [ 'status' => 400 ] );
293
+ }
294
+
295
+ if ( '' === $value ) {
296
+ return new \WP_Error( 'secret', 'Enter a value.', [ 'status' => 400 ] );
297
+ }
298
+
299
+ $sealed = Github::seal( $value, $environment );
300
+
301
+ if ( is_wp_error( $sealed ) ) {
302
+ return $sealed;
303
+ }
304
+
305
+ $result = Github::repo_call( 'PUT', Github::secrets_path( $environment ) . '/' . $name, $sealed );
306
+
307
+ return is_wp_error( $result ) ? $result : [ 'ok' => true, 'name' => $name ];
308
+ }
309
+
310
+ public static function delete_secret( string $name, string $environment ) {
311
+ $result = Github::repo_call( 'DELETE', Github::secrets_path( sanitize_text_field( $environment ) ) . '/' . strtoupper( preg_replace( '/[^A-Za-z0-9_]/', '', $name ) ) );
312
+
313
+ return is_wp_error( $result ) ? $result : [ 'ok' => true ];
314
+ }
315
+
316
+
317
+ public static function save( array $input ) {
318
+ $settings = self::settings();
319
+ $url = trim( (string) ( $input['url'] ?? $settings['url'] ) );
320
+ $repo = '' === $url ? '' : Github::parse( $url );
321
+
322
+ if ( '' !== $url && '' === $repo ) {
323
+ return new \WP_Error( 'repo', 'Use a GitHub repository URL, like https://github.com/owner/platform.', [ 'status' => 400 ] );
324
+ }
325
+
326
+ $settings['url'] = '' === $repo ? '' : 'https://github.com/' . $repo;
327
+ $settings['repo'] = $repo;
328
+ $settings['branch'] = preg_replace( '#[^A-Za-z0-9._/-]#', '', (string) ( $input['branch'] ?? $settings['branch'] ) );
329
+ $settings['packages'] = self::folder( (string) ( $input['packages'] ?? $settings['packages'] ) ) ?: 'packages';
330
+
331
+ foreach ( array_keys( self::defaults()['workflows'] ) as $key ) {
332
+ if ( isset( $input['workflows'][ $key ] ) ) {
333
+ $settings['workflows'][ $key ] = preg_replace( '#[^A-Za-z0-9._-]#', '', (string) $input['workflows'][ $key ] );
334
+ }
335
+ }
336
+
337
+ $seen = [];
338
+
339
+ foreach ( $settings['kinds'] as $kind ) {
340
+ $seen[ $kind['key'] ] = true;
341
+ }
342
+
343
+ if ( isset( $input['kinds'] ) ) {
344
+ $kinds = [];
345
+ $seen = [];
346
+
347
+ foreach ( (array) $input['kinds'] as $kind ) {
348
+ $folder = self::folder( (string) ( $kind['folder'] ?? '' ) );
349
+ $key = self::slug( (string) ( $kind['key'] ?? '' ) ?: $folder );
350
+
351
+ if ( '' === $folder || '' === $key || isset( $seen[ $key ] ) ) {
352
+ continue;
353
+ }
354
+
355
+ $seen[ $key ] = true;
356
+ $kinds[] = [
357
+ 'key' => $key,
358
+ 'label' => sanitize_text_field( (string) ( $kind['label'] ?? '' ) ) ?: ucwords( str_replace( '-', ' ', $key ) ),
359
+ 'folder' => $folder,
360
+ 'manifest' => self::folder( (string) ( $kind['manifest'] ?? '' ) ) ?: '.github/' . $key . '.json',
361
+ ];
362
+ }
363
+
364
+ $settings['kinds'] = $kinds;
365
+ }
366
+
367
+ if ( isset( $input['apps'] ) ) {
368
+ $apps = [];
369
+
370
+ foreach ( (array) $input['apps'] as $id => $app ) {
371
+ $kind = self::slug( (string) ( $app['kind'] ?? '' ) );
372
+ $slug = self::slug( (string) ( $app['slug'] ?? '' ) );
373
+
374
+ if ( (int) $id && ( isset( $seen[ $kind ] ) || '' !== $slug ) ) {
375
+ $apps[ (int) $id ] = [ 'kind' => isset( $seen[ $kind ] ) ? $kind : '', 'slug' => $slug ];
376
+ }
377
+ }
378
+
379
+ $settings['apps'] = $apps;
380
+ }
381
+
382
+ self::store( $settings );
383
+
384
+ return self::admin();
385
+ }
386
+
387
+ public static function import() {
388
+ $layout = self::layout();
389
+
390
+ if ( is_wp_error( $layout ) ) {
391
+ return $layout;
392
+ }
393
+
394
+ $settings = self::settings();
395
+ $settings['packages'] = $layout['packages'];
396
+
397
+ if ( $layout['kinds'] ) {
398
+ $settings['kinds'] = $layout['kinds'];
399
+ }
400
+
401
+ self::store( $settings );
402
+
403
+ return self::admin();
404
+ }
405
+
406
+ public static function detect() {
407
+ $tree = Github::tree();
408
+
409
+ if ( is_wp_error( $tree ) ) {
410
+ return $tree;
411
+ }
412
+
413
+ $settings = self::settings();
414
+ $folders = [];
415
+
416
+ foreach ( $tree['files'] as $file ) {
417
+ foreach ( $settings['kinds'] as $kind ) {
418
+ $prefix = $kind['folder'] . '/';
419
+ $rest = 0 === strpos( $file['path'], $prefix ) ? substr( $file['path'], strlen( $prefix ) ) : '';
420
+
421
+ if ( false !== strpos( $rest, '/' ) ) {
422
+ $name = strtok( $rest, '/' );
423
+ $folders[ strtolower( $name ) ] = [ $kind['key'], $name ];
424
+ }
425
+ }
426
+ }
427
+
428
+ $found = 0;
429
+
430
+ foreach ( Registry::all() as $app ) {
431
+ $id = (int) $app['id'];
432
+ $name = strtolower( self::slug( (string) $app['name'] ) );
433
+
434
+ if ( isset( $folders[ $name ] ) && empty( $settings['apps'][ $id ]['kind'] ) ) {
435
+ $settings['apps'][ $id ] = [ 'kind' => $folders[ $name ][0], 'slug' => $folders[ $name ][1] ];
436
+ ++$found;
437
+ }
438
+ }
439
+
440
+ self::store( $settings );
441
+
442
+ return [ 'matched' => $found ] + self::admin();
443
+ }
444
+
445
+ public static function sandboxes(): array {
446
+ $health = Sandboxes::health();
447
+
448
+ return array_map( static fn( array $s ): array => $s + [ 'state' => $health[ $s['key'] ] ?? [] ], Sandboxes::all() );
449
+ }
450
+
451
+ public static function admin(): array {
452
+ $settings = self::settings();
453
+ $constant = Github::constant();
454
+ $apps = [];
455
+
456
+ foreach ( Registry::all() as $app ) {
457
+ $map = self::mapping( (int) $app['id'], (string) $app['name'] );
458
+ $apps[] = [ 'id' => (int) $app['id'], 'name' => (string) $app['name'], 'kind' => $map['kind'], 'slug' => (string) ( $settings['apps'][ (int) $app['id'] ]['slug'] ?? '' ), 'default' => self::slug( (string) $app['name'] ), 'path' => $map['path'], 'mapped' => $map['mapped'] ];
459
+ }
460
+
461
+ return [
462
+ 'settings' => array_diff_key( $settings, [ 'apps' => 1, 'sandboxes' => 1 ] ),
463
+ 'token' => [ 'constant' => $constant, 'set' => defined( $constant ) && '' !== trim( (string) constant( $constant ) ) ],
464
+ 'layout' => self::LAYOUT,
465
+ 'presets' => array_values( array_map( 'strval', (array) Config::get( 'ops.secrets', [] ) ) ),
466
+ 'apps' => $apps,
467
+ 'sandboxes' => self::sandboxes(),
468
+ ];
469
+ }
470
+ }