@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,279 @@
1
+ <?php
2
+ /**
3
+ * Apps: each app is one row with its environments, content rules, linked
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Modules\Apps;
9
+
10
+ use Platform\Config;
11
+ use Platform\Setup;
12
+
13
+ defined( 'ABSPATH' ) || exit;
14
+
15
+ final class Apps {
16
+
17
+ const ROLE = 'service_client';
18
+ const CAP = 'access_service_content';
19
+ const ALLOWED_IPS = 'apps_allowed_ips';
20
+ const JSON = [ 'environments', 'contents', 'links', 'languages', 'metadata' ];
21
+
22
+ public static function table(): string {
23
+ return Setup::table( 'apps' );
24
+ }
25
+
26
+ /* ---------------------------------------------------------------- config */
27
+
28
+ public static function environments(): array {
29
+ $environments = [];
30
+
31
+ foreach ( (array) Config::get( 'apps.environments', [] ) as $environment ) {
32
+ $key = sanitize_key( $environment['key'] ?? '' );
33
+ if ( '' !== $key ) {
34
+ $environments[ $key ] = (string) ( $environment['label'] ?? $key );
35
+ }
36
+ }
37
+
38
+ return $environments ?: [ 'prod' => 'Production' ];
39
+ }
40
+
41
+ public static function metadata_fields(): array {
42
+ $fields = [];
43
+
44
+ foreach ( (array) Config::get( 'apps.metadata', [] ) as $field ) {
45
+ if ( is_array( $field ) && ! empty( $field['key'] ) ) {
46
+ $fields[] = [
47
+ 'key' => sanitize_key( $field['key'] ),
48
+ 'type' => (string) ( $field['type'] ?? 'text' ),
49
+ 'label' => (string) ( $field['label'] ?? $field['key'] ),
50
+ ] + $field;
51
+ }
52
+ }
53
+
54
+ return $fields;
55
+ }
56
+
57
+ public static function links(): array {
58
+ $links = [];
59
+
60
+ foreach ( (array) Config::get( 'apps.links', [] ) as $link ) {
61
+ $key = sanitize_key( $link['key'] ?? '' );
62
+ if ( '' !== $key ) {
63
+ $links[ $key ] = $link + [ 'label' => $key ];
64
+ }
65
+ }
66
+
67
+ return $links;
68
+ }
69
+
70
+ public static function webhook(): string {
71
+ return (string) Config::get( 'apps.webhook', '/wp-json/app/webhooks/content/pull' );
72
+ }
73
+
74
+ /* ---------------------------------------------------------------- data */
75
+
76
+ public static function get( int $id ): ?array {
77
+ global $wpdb;
78
+
79
+ $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::table() . ' WHERE id = %d', $id ), ARRAY_A );
80
+
81
+ return $row ? self::decode( $row ) : null;
82
+ }
83
+
84
+ public static function get_by_user( int $user_id ): ?array {
85
+ global $wpdb;
86
+
87
+ $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . self::table() . ' WHERE user_id = %d LIMIT 1', $user_id ), ARRAY_A );
88
+
89
+ return $row ? self::decode( $row ) : null;
90
+ }
91
+
92
+ public static function all( string $search = '' ): array {
93
+ global $wpdb;
94
+
95
+ if ( '' !== $search ) {
96
+ $like = '%' . $wpdb->esc_like( $search ) . '%';
97
+ $sql = $wpdb->prepare( 'SELECT * FROM ' . self::table() . ' WHERE name LIKE %s OR environments LIKE %s ORDER BY name ASC', $like, $like );
98
+ } else {
99
+ $sql = 'SELECT * FROM ' . self::table() . ' ORDER BY name ASC';
100
+ }
101
+
102
+ return array_map( [ __CLASS__, 'decode' ], (array) $wpdb->get_results( $sql, ARRAY_A ) );
103
+ }
104
+
105
+ public static function save( int $id, array $data ): int {
106
+ global $wpdb;
107
+
108
+ $row = [
109
+ 'name' => sanitize_text_field( (string) ( $data['name'] ?? '' ) ),
110
+ 'user_id' => (int) ( $data['user_id'] ?? 0 ),
111
+ ];
112
+
113
+ foreach ( self::JSON as $column ) {
114
+ $row[ $column ] = wp_json_encode( $data[ $column ] ?? [] );
115
+ }
116
+
117
+ if ( $id ) {
118
+ $wpdb->update( self::table(), $row, [ 'id' => $id ] );
119
+ return $id;
120
+ }
121
+
122
+ return $wpdb->insert( self::table(), $row ) ? (int) $wpdb->insert_id : 0;
123
+ }
124
+
125
+ public static function delete( int $id ): void {
126
+ global $wpdb;
127
+
128
+ $wpdb->delete( self::table(), [ 'id' => $id ], [ '%d' ] );
129
+ }
130
+
131
+ public static function set_pull( int $id, string $env, string $value ): void {
132
+ $app = self::get( $id );
133
+
134
+ if ( $app ) {
135
+ $app['environments'][ $env ]['last_pull'] = $value;
136
+ self::save( $id, $app );
137
+ }
138
+ }
139
+
140
+ /* ---------------------------------------------------------------- push */
141
+
142
+ /**
143
+ * @return true|\WP_Error
144
+ */
145
+ public static function push( int $id, string $env ) {
146
+ $app = self::get( $id );
147
+ $base = (string) ( $app['environments'][ $env ]['url'] ?? '' );
148
+
149
+ if ( ! $app || ! isset( self::environments()[ $env ] ) ) {
150
+ return new \WP_Error( 'app_not_found', 'App not found.' );
151
+ }
152
+
153
+ if ( '' === $base ) {
154
+ return new \WP_Error( 'app_no_url', sprintf( 'This app has no %s URL.', strtolower( self::environments()[ $env ] ) ) );
155
+ }
156
+
157
+ $user = $app['user_id'] ? get_userdata( $app['user_id'] ) : false;
158
+ $hashes = $user ? self::password_hashes( $user->ID ) : [];
159
+
160
+ if ( ! $hashes ) {
161
+ return new \WP_Error( 'app_auth_failed', 'App auth failed or not set up.' );
162
+ }
163
+
164
+ $previous = (string) ( $app['environments'][ $env ]['last_pull'] ?? '' );
165
+ $restore = static function ( \WP_Error $error ) use ( $id, $env, $previous ): \WP_Error {
166
+ self::set_pull( $id, $env, $previous );
167
+ return $error;
168
+ };
169
+
170
+ self::set_pull( $id, $env, '' );
171
+
172
+ $last = '';
173
+
174
+ foreach ( $hashes as $hash ) {
175
+ $response = wp_remote_post(
176
+ untrailingslashit( self::https( $base ) ) . self::webhook(),
177
+ [
178
+ 'timeout' => 300,
179
+ 'headers' => [ 'Authorization' => 'Basic ' . base64_encode( $user->user_login . ':' . $hash ) ],
180
+ ]
181
+ );
182
+
183
+ if ( is_wp_error( $response ) ) {
184
+ return $restore( new \WP_Error( 'app_unreachable', 'Could not reach the app: ' . $response->get_error_message() ) );
185
+ }
186
+
187
+ $code = (int) wp_remote_retrieve_response_code( $response );
188
+
189
+ if ( $code >= 200 && $code < 300 ) {
190
+ return true;
191
+ }
192
+
193
+ if ( 409 === $code ) {
194
+ return $restore( new \WP_Error( 'app_pull_running', 'A pull is already in progress on the app.' ) );
195
+ }
196
+
197
+ if ( 401 !== $code && 403 !== $code ) {
198
+ return $restore( new \WP_Error( 'app_webhook_failed', 'Webhook failed with HTTP ' . $code . '.' ) );
199
+ }
200
+
201
+ $body = json_decode( wp_remote_retrieve_body( $response ), true );
202
+ $last = 'HTTP ' . $code . ': ' . ( $body['message'] ?? wp_remote_retrieve_body( $response ) );
203
+ }
204
+
205
+ return $restore( new \WP_Error( 'app_auth_failed', 'App auth failed (' . $last . ')' ) );
206
+ }
207
+
208
+ /**
209
+ * @return int|\WP_Error
210
+ */
211
+ public static function create_user( string $name ) {
212
+ $login = 'service_' . sanitize_title( $name );
213
+
214
+ return wp_insert_user(
215
+ [
216
+ 'user_login' => $login,
217
+ 'user_pass' => wp_generate_password( 32 ),
218
+ 'user_email' => $login . '@service.local',
219
+ 'display_name' => $name,
220
+ 'role' => self::ROLE,
221
+ ]
222
+ );
223
+ }
224
+
225
+ /* ---------------------------------------------------------------- helpers */
226
+
227
+ public static function host( string $url ): string {
228
+ $parts = wp_parse_url( trim( $url ) );
229
+
230
+ if ( empty( $parts['host'] ) ) {
231
+ return '';
232
+ }
233
+
234
+ $host = strtolower( $parts['host'] );
235
+
236
+ if ( ! empty( $parts['port'] ) && ! in_array( (int) $parts['port'], [ 80, 443 ], true ) ) {
237
+ $host .= ':' . $parts['port'];
238
+ }
239
+
240
+ return $host;
241
+ }
242
+
243
+ public static function https( string $url ): string {
244
+ $url = esc_url_raw( trim( $url ) );
245
+
246
+ return '' === $url ? '' : set_url_scheme( $url, 'https' );
247
+ }
248
+
249
+ public static function format_pull( string $raw ): string {
250
+ $raw = trim( $raw );
251
+
252
+ if ( '' === $raw ) {
253
+ return '';
254
+ }
255
+
256
+ $time = ctype_digit( $raw ) ? (int) $raw : strtotime( $raw . ( preg_match( '/(Z|[+-]\d{2}:?\d{2}|UTC|GMT)$/i', $raw ) ? '' : ' UTC' ) );
257
+
258
+ return $time ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time ) : $raw;
259
+ }
260
+
261
+ private static function password_hashes( int $user_id ): array {
262
+ $items = \WP_Application_Passwords::get_user_application_passwords( $user_id );
263
+
264
+ usort( $items, static fn( $a, $b ) => (int) ( $b['created'] ?? 0 ) <=> (int) ( $a['created'] ?? 0 ) );
265
+
266
+ return array_values( array_filter( array_column( $items, 'password' ) ) );
267
+ }
268
+
269
+ private static function decode( array $row ): array {
270
+ foreach ( self::JSON as $column ) {
271
+ $row[ $column ] = json_decode( (string) ( $row[ $column ] ?? '' ), true ) ?: [];
272
+ }
273
+
274
+ $row['id'] = (int) $row['id'];
275
+ $row['user_id'] = (int) $row['user_id'];
276
+
277
+ return $row;
278
+ }
279
+ }