@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,472 @@
1
+ <?php
2
+ /**
3
+ * Apps admin: one screen per app with environments, content, linked
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Modules\Apps;
9
+
10
+ use Platform\Config;
11
+ use Platform\Content\Editor;
12
+ use Platform\Content\Fields;
13
+ use Platform\Languages;
14
+ use Platform\Modules\Thumbnails;
15
+
16
+ defined( 'ABSPATH' ) || exit;
17
+
18
+ final class Admin {
19
+
20
+ const PAGE = 'apps';
21
+
22
+ public static function register(): void {
23
+ add_action( 'admin_menu', [ __CLASS__, 'menu' ] );
24
+ add_action( 'admin_enqueue_scripts', [ __CLASS__, 'assets' ] );
25
+
26
+ foreach ( [ 'app_save', 'app_delete', 'app_push', 'app_ips' ] as $action ) {
27
+ add_action( 'admin_post_platform_' . $action, [ __CLASS__, $action ] );
28
+ }
29
+ }
30
+
31
+ public static function menu(): void {
32
+ $module = Config::module( 'apps' );
33
+
34
+ $hook = add_menu_page( 'Apps', (string) ( $module['label'] ?? 'Apps' ), 'manage_options', self::PAGE, [ __CLASS__, 'render' ], (string) ( $module['icon'] ?? 'dashicons-screenoptions' ), $module['position'] ?? null );
35
+
36
+ add_action( 'load-' . $hook, [ __CLASS__, 'bulk' ] );
37
+ add_submenu_page( self::PAGE, 'Apps', 'All Apps', 'manage_options', self::PAGE, [ __CLASS__, 'render' ] );
38
+ }
39
+
40
+ public static function assets( string $hook ): void {
41
+ if ( false !== strpos( $hook, 'page_' . self::PAGE ) || false !== strpos( $hook, 'page_app-' ) ) {
42
+ Editor::enqueue();
43
+ }
44
+ }
45
+
46
+ public static function render(): void {
47
+ $edit = sanitize_key( wp_unslash( (string) ( $_GET['app'] ?? '' ) ) );
48
+
49
+ echo '<div class="wrap">';
50
+ self::notice();
51
+
52
+ if ( '' !== $edit ) {
53
+ self::render_form( 'new' === $edit ? null : Apps::get( absint( $edit ) ) );
54
+ } else {
55
+ self::render_list();
56
+ }
57
+
58
+ echo '</div>';
59
+ }
60
+
61
+ /* ---------------------------------------------------------------- list */
62
+
63
+ private static function render_list(): void {
64
+ $table = new Table();
65
+ $table->prepare_items();
66
+ ?>
67
+ <h1 class="wp-heading-inline">Apps</h1>
68
+ <a class="page-title-action" href="<?php echo esc_url( self::url( [ 'app' => 'new' ] ) ); ?>">Add New App</a>
69
+ <hr class="wp-header-end" />
70
+ <form method="get">
71
+ <input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE ); ?>" />
72
+ <?php $table->search_box( 'Search Apps', 'app' ); ?>
73
+ </form>
74
+ <form method="post" action="<?php echo esc_url( self::url() ); ?>">
75
+ <?php $table->display(); ?>
76
+ </form>
77
+
78
+ <h2>Allowed IPs</h2>
79
+ <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
80
+ <input type="hidden" name="action" value="platform_app_ips" />
81
+ <?php wp_nonce_field( 'platform_app_ips' ); ?>
82
+ <table class="form-table" role="presentation">
83
+ <tr>
84
+ <th scope="row"><label for="allowed-ips">Allowed IPs</label></th>
85
+ <td>
86
+ <textarea class="large-text code" rows="5" id="allowed-ips" name="allowed_ips"><?php echo esc_textarea( (string) get_option( Apps::ALLOWED_IPS, '' ) ); ?></textarea>
87
+ <p class="description">One per line. Apps can only call the content API from these addresses unless Skip IP check is on.</p>
88
+ </td>
89
+ </tr>
90
+ </table>
91
+ <?php submit_button( 'Save IPs' ); ?>
92
+ </form>
93
+ <?php
94
+ }
95
+
96
+ public static function bulk(): void {
97
+ if ( 'POST' !== ( $_SERVER['REQUEST_METHOD'] ?? '' ) || empty( $_POST['ids'] ) ) {
98
+ return;
99
+ }
100
+
101
+ $table = new Table();
102
+ $action = (string) $table->current_action();
103
+
104
+ if ( '' === $action ) {
105
+ return;
106
+ }
107
+
108
+ if ( ! current_user_can( 'manage_options' ) ) {
109
+ wp_die( 'Not allowed.', 403 );
110
+ }
111
+
112
+ check_admin_referer( 'bulk-apps' );
113
+
114
+ $errors = [];
115
+
116
+ foreach ( array_map( 'absint', (array) $_POST['ids'] ) as $id ) {
117
+ if ( 'delete' === $action ) {
118
+ Apps::delete( $id );
119
+ } elseif ( 0 === strpos( $action, 'push_' ) ) {
120
+ $result = Apps::push( $id, substr( $action, 5 ) );
121
+
122
+ if ( is_wp_error( $result ) ) {
123
+ $errors[] = $result->get_error_message();
124
+ }
125
+ }
126
+ }
127
+
128
+ self::redirect( $errors ? [ 'error' => rawurlencode( implode( ' ', array_unique( $errors ) ) ) ] : [ 'notice' => 'delete' === $action ? 'deleted' : 'pushed' ] );
129
+ }
130
+
131
+ /* ---------------------------------------------------------------- form */
132
+
133
+ private static function render_form( ?array $app ): void {
134
+ $languages = (array) ( $app['languages'] ?? [ Languages::default() ] );
135
+ $users = get_users( [ 'role' => Apps::ROLE, 'orderby' => 'login', 'fields' => [ 'ID', 'user_login' ] ] );
136
+ ?>
137
+ <h1><a href="<?php echo esc_url( self::url() ); ?>">Apps</a> › <?php echo $app ? esc_html( $app['name'] ) : 'New app'; ?></h1>
138
+ <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
139
+ <input type="hidden" name="action" value="platform_app_save" />
140
+ <input type="hidden" name="id" value="<?php echo (int) ( $app['id'] ?? 0 ); ?>" />
141
+ <?php wp_nonce_field( 'platform_app_save' ); ?>
142
+
143
+ <h2 class="title">App</h2>
144
+ <table class="form-table" role="presentation">
145
+ <tr><th><label for="app-name">Name</label></th><td><input class="regular-text" id="app-name" name="name" value="<?php echo esc_attr( $app['name'] ?? '' ); ?>" required /></td></tr>
146
+ <?php foreach ( Apps::environments() as $env => $label ) : ?>
147
+ <tr>
148
+ <th><label for="app-<?php echo esc_attr( $env ); ?>"><?php echo esc_html( $label ); ?> URL</label></th>
149
+ <td><input class="regular-text" type="url" id="app-<?php echo esc_attr( $env ); ?>" name="environments[<?php echo esc_attr( $env ); ?>]" value="<?php echo esc_attr( (string) ( $app['environments'][ $env ]['url'] ?? '' ) ); ?>" placeholder="https://" /></td>
150
+ </tr>
151
+ <?php endforeach; ?>
152
+ <tr>
153
+ <th><label for="app-user">User</label></th>
154
+ <td>
155
+ <select id="app-user" name="user_id">
156
+ <option value="0">None</option>
157
+ <?php foreach ( $users as $user ) : ?>
158
+ <option value="<?php echo (int) $user->ID; ?>"<?php selected( (int) ( $app['user_id'] ?? 0 ), (int) $user->ID ); ?>><?php echo esc_html( $user->user_login ); ?></option>
159
+ <?php endforeach; ?>
160
+ </select>
161
+ <label style="margin-left:12px"><input type="checkbox" name="create_user" value="1" /> Create a new user</label>
162
+ <p class="description">The app signs in as this user with an Application Password.</p>
163
+ </td>
164
+ </tr>
165
+ <tr>
166
+ <th>Languages</th>
167
+ <td>
168
+ <?php foreach ( Languages::all() as $code => $label ) : ?>
169
+ <label style="margin-right:12px"><input type="checkbox" name="languages[]" value="<?php echo esc_attr( $code ); ?>"<?php checked( in_array( $code, $languages, true ) ); ?> /> <?php echo esc_html( $label ); ?></label>
170
+ <?php endforeach; ?>
171
+ </td>
172
+ </tr>
173
+ </table>
174
+
175
+ <h2 class="title">Content</h2>
176
+ <?php
177
+ foreach ( Api::endpoints() as $name => $type ) {
178
+ if ( empty( $type['api']['link'] ) ) {
179
+ self::render_content_block( $name, $type, (array) ( $app['contents'][ $name ] ?? [] ), isset( $app['contents'][ $name ] ) );
180
+ }
181
+ }
182
+ ?>
183
+
184
+ <table class="form-table" role="presentation">
185
+ <?php foreach ( Apps::links() as $key => $link ) : ?>
186
+ <tr>
187
+ <th><label for="app-link-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $link['label'] ); ?></label></th>
188
+ <td><?php self::render_link( $key, $link, (int) ( $app['links'][ $key ] ?? 0 ) ); ?></td>
189
+ </tr>
190
+ <?php endforeach; ?>
191
+ <?php if ( Config::module( 'thumbnails' ) ) : ?>
192
+ <tr>
193
+ <th><label for="app-thumbnails">Thumbnail group</label></th>
194
+ <td>
195
+ <select id="app-thumbnails" name="thumbnail_group_id">
196
+ <option value="0">Default thumbnails</option>
197
+ <?php foreach ( Thumbnails::groups() as $group ) : ?>
198
+ <option value="<?php echo (int) $group['id']; ?>"<?php selected( (int) ( $app['contents']['thumbnail_group_id'] ?? 0 ), (int) $group['id'] ); ?>><?php echo esc_html( $group['name'] ); ?></option>
199
+ <?php endforeach; ?>
200
+ </select>
201
+ </td>
202
+ </tr>
203
+ <?php endif; ?>
204
+ </table>
205
+
206
+ <h2 class="title">Details</h2>
207
+ <?php self::render_details( (array) ( $app['metadata'] ?? [] ) ); ?>
208
+
209
+ <?php submit_button( $app ? 'Update app' : 'Add app' ); ?>
210
+ </form>
211
+ <?php
212
+ }
213
+
214
+ private static function render_content_block( string $name, array $type, array $saved, bool $enabled ): void {
215
+ $posts = get_posts( [ 'post_type' => $type['key'], 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'fields' => 'ids' ] );
216
+ $terms = [];
217
+
218
+ foreach ( Config::taxonomies_for( $type['key'] ) as $taxonomy ) {
219
+ $found = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => true, 'object_ids' => $posts ?: [ 0 ] ] );
220
+
221
+ foreach ( is_wp_error( $found ) ? [] : $found as $term ) {
222
+ $terms[ $term->term_id ] = $term->name . ' — ' . Config::taxonomy( $taxonomy )['singular'];
223
+ }
224
+ }
225
+
226
+ $titles = [];
227
+ foreach ( $posts as $id ) {
228
+ $titles[ $id ] = get_the_title( $id );
229
+ }
230
+
231
+ $lists = [
232
+ 'pinned' => [ 'Include', [ 0 => 'All ' . strtolower( $type['label'] ) ] + $titles ],
233
+ 'blocked' => [ 'Always exclude', $titles ],
234
+ 'include_by_terms' => [ 'Include by category or tag', $terms ],
235
+ 'exclude_by_terms' => [ 'Exclude by category or tag', $terms ],
236
+ ];
237
+ ?>
238
+ <details class="platform-card" style="margin-bottom:10px;max-width:1100px"<?php echo $enabled ? ' open' : ''; ?>>
239
+ <summary><label><input type="checkbox" name="contents[]" value="<?php echo esc_attr( $name ); ?>"<?php checked( $enabled ); ?> /> <strong><?php echo esc_html( $type['label'] ); ?></strong></label></summary>
240
+ <div class="platform-checklist" style="margin-top:12px">
241
+ <?php foreach ( $lists as $list => [ $label, $options ] ) : ?>
242
+ <?php $selected = array_map( 'intval', (array) ( $saved[ $list ] ?? [] ) ); ?>
243
+ <label>
244
+ <span class="platform-field__label"><?php echo esc_html( $label ); ?></span>
245
+ <input type="search" class="platform-filter" placeholder="Filter" />
246
+ <select multiple size="8" style="width:100%" name="<?php echo esc_attr( $list . '[' . $name . '][]' ); ?>">
247
+ <?php foreach ( $options as $id => $option ) : ?>
248
+ <option value="<?php echo (int) $id; ?>"<?php selected( in_array( (int) $id, $selected, true ) ); ?>><?php echo esc_html( $option ?: '(no title)' ); ?></option>
249
+ <?php endforeach; ?>
250
+ </select>
251
+ </label>
252
+ <?php endforeach; ?>
253
+ </div>
254
+ </details>
255
+ <?php
256
+ }
257
+
258
+ private static function render_link( string $key, array $link, int $current ): void {
259
+ printf( '<select id="app-link-%1$s" name="links[%1$s]"><option value="0">None</option>', esc_attr( $key ) );
260
+
261
+ if ( ! empty( $link['taxonomy'] ) ) {
262
+ $terms = get_terms( [ 'taxonomy' => (string) $link['taxonomy'], 'hide_empty' => false ] );
263
+ foreach ( is_wp_error( $terms ) ? [] : $terms as $term ) {
264
+ printf( '<option value="%d"%s>%s</option>', (int) $term->term_id, selected( $current, (int) $term->term_id, false ), esc_html( $term->name ) );
265
+ }
266
+ } elseif ( ! empty( $link['type'] ) ) {
267
+ foreach ( get_posts( [ 'post_type' => (string) $link['type'], 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC' ] ) as $post ) {
268
+ printf( '<option value="%d"%s>%s</option>', (int) $post->ID, selected( $current, (int) $post->ID, false ), esc_html( $post->post_title ) );
269
+ }
270
+ }
271
+
272
+ echo '</select>';
273
+ }
274
+
275
+ private static function render_details( array $metadata ): void {
276
+ $fields = Apps::metadata_fields();
277
+ $known = array_column( $fields, 'key' );
278
+
279
+ foreach ( Schemes::all() as $scheme ) {
280
+ $records = Schemes::records( $scheme['key'] );
281
+ $current = (string) ( $metadata[ $scheme['primary'] ] ?? '' );
282
+ ?>
283
+ <p>
284
+ <label><strong><?php echo esc_html( $scheme['singular'] ); ?></strong>
285
+ <select class="platform-scheme" data-fields="<?php echo esc_attr( (string) wp_json_encode( $scheme['fields'] ) ); ?>">
286
+ <option value="">Choose to fill in…</option>
287
+ <?php foreach ( $records as $record ) : ?>
288
+ <option value="<?php echo esc_attr( (string) wp_json_encode( $record['data'] ) ); ?>"<?php selected( strtolower( $current ), strtolower( $record['title'] ) ); ?>><?php echo esc_html( $record['title'] ); ?></option>
289
+ <?php endforeach; ?>
290
+ </select></label>
291
+ <a href="<?php echo esc_url( admin_url( 'admin.php?page=app-' . $scheme['key'] ) ); ?>">Manage <?php echo esc_html( strtolower( $scheme['label'] ) ); ?></a>
292
+ </p>
293
+ <?php
294
+ }
295
+
296
+ echo '<div class="platform-fields platform-details" style="max-width:1100px">';
297
+ Fields::render( $fields, $metadata, 'metadata' );
298
+ echo '</div>';
299
+
300
+ $extra = [];
301
+ foreach ( $metadata as $key => $value ) {
302
+ if ( ! in_array( $key, $known, true ) ) {
303
+ $extra[] = [ 'key' => $key, 'value' => is_scalar( $value ) ? (string) $value : '' ];
304
+ }
305
+ }
306
+
307
+ echo '<div style="max-width:1100px;margin-top:12px">';
308
+ Fields::render(
309
+ [
310
+ [
311
+ 'key' => 'extra',
312
+ 'type' => 'repeater',
313
+ 'label' => 'Other details',
314
+ 'add' => 'Add detail',
315
+ 'fields' => [
316
+ [ 'key' => 'key', 'type' => 'key', 'label' => 'Key', 'width' => 'key', 'title_source' => true ],
317
+ [ 'key' => 'value', 'type' => 'text', 'label' => 'Value', 'width' => 'half' ],
318
+ ],
319
+ ],
320
+ ],
321
+ [ 'extra' => $extra ],
322
+ 'app'
323
+ );
324
+ echo '</div>';
325
+ }
326
+
327
+ /* ---------------------------------------------------------------- actions */
328
+
329
+ public static function app_save(): void {
330
+ self::guard( 'platform_app_save' );
331
+
332
+ $input = wp_unslash( $_POST );
333
+ $id = absint( $input['id'] ?? 0 );
334
+ $existing = $id ? Apps::get( $id ) : null;
335
+ $contents = [];
336
+ $errors = [];
337
+
338
+ foreach ( array_map( 'sanitize_key', (array) ( $input['contents'] ?? [] ) ) as $name ) {
339
+ $pinned = array_map( 'intval', (array) ( $input['pinned'][ $name ] ?? [] ) );
340
+ $include = array_map( 'intval', (array) ( $input['include_by_terms'][ $name ] ?? [] ) );
341
+
342
+ $contents[ $name ] = [
343
+ 'pinned' => $pinned,
344
+ 'blocked' => array_values( array_diff( array_map( 'intval', (array) ( $input['blocked'][ $name ] ?? [] ) ), $pinned ) ),
345
+ 'include_by_terms' => $include,
346
+ 'exclude_by_terms' => array_values( array_diff( array_map( 'intval', (array) ( $input['exclude_by_terms'][ $name ] ?? [] ) ), $include ) ),
347
+ ];
348
+
349
+ if ( ! $pinned && ! $include ) {
350
+ $errors[] = sprintf( '"%s" has nothing included. Include posts or categories, or turn it off.', Api::endpoints()[ $name ]['label'] ?? $name );
351
+ }
352
+ }
353
+
354
+ if ( $errors ) {
355
+ self::redirect( [ 'app' => $id ?: 'new', 'error' => rawurlencode( implode( ' ', $errors ) ) ] );
356
+ }
357
+
358
+ $contents['thumbnail_group_id'] = absint( $input['thumbnail_group_id'] ?? 0 );
359
+
360
+ $environments = [];
361
+ foreach ( array_keys( Apps::environments() ) as $env ) {
362
+ $environments[ $env ] = [
363
+ 'url' => Apps::https( (string) ( $input['environments'][ $env ] ?? '' ) ),
364
+ 'last_pull' => (string) ( $existing['environments'][ $env ]['last_pull'] ?? '' ),
365
+ ];
366
+ }
367
+
368
+ $links = [];
369
+ foreach ( array_keys( Apps::links() ) as $key ) {
370
+ $links[ $key ] = absint( $input['links'][ $key ] ?? 0 );
371
+ }
372
+
373
+ $metadata = [];
374
+ foreach ( Apps::metadata_fields() as $field ) {
375
+ $metadata[ $field['key'] ] = 'checkbox' === $field['type']
376
+ ? ( empty( $input['metadata'][ $field['key'] ] ) ? '' : '1' )
377
+ : Fields::sanitize( $field, $input['metadata'][ $field['key'] ] ?? '' );
378
+ }
379
+
380
+ foreach ( (array) ( $input['app']['extra'] ?? [] ) as $row ) {
381
+ $key = sanitize_key( (string) ( $row['key'] ?? '' ) );
382
+ if ( '' !== $key && ! isset( $metadata[ $key ] ) ) {
383
+ $metadata[ $key ] = sanitize_text_field( (string) ( $row['value'] ?? '' ) );
384
+ }
385
+ }
386
+
387
+ $data = [
388
+ 'name' => sanitize_text_field( (string) ( $input['name'] ?? '' ) ),
389
+ 'user_id' => absint( $input['user_id'] ?? 0 ),
390
+ 'environments' => $environments,
391
+ 'contents' => $contents,
392
+ 'links' => $links,
393
+ 'languages' => array_values( array_intersect( (array) ( $input['languages'] ?? [] ), array_keys( Languages::all() ) ) ),
394
+ 'metadata' => $metadata,
395
+ ];
396
+
397
+ if ( ! empty( $input['create_user'] ) && '' !== $data['name'] ) {
398
+ $user_id = Apps::create_user( $data['name'] );
399
+
400
+ if ( is_wp_error( $user_id ) ) {
401
+ self::redirect( [ 'app' => $id ?: 'new', 'error' => rawurlencode( $user_id->get_error_message() ) ] );
402
+ }
403
+
404
+ $data['user_id'] = (int) $user_id;
405
+ }
406
+
407
+ $id = Apps::save( $id, $data );
408
+
409
+ self::redirect( [ 'app' => $id, 'notice' => 'saved' ] );
410
+ }
411
+
412
+ public static function app_delete(): void {
413
+ self::guard( 'platform_app_delete' );
414
+ Apps::delete( absint( $_GET['id'] ?? 0 ) );
415
+ self::redirect( [ 'notice' => 'deleted' ] );
416
+ }
417
+
418
+ public static function app_push(): void {
419
+ self::guard( 'platform_app_push' );
420
+
421
+ $result = Apps::push( absint( $_GET['id'] ?? 0 ), sanitize_key( wp_unslash( (string) ( $_GET['env'] ?? '' ) ) ) );
422
+
423
+ self::redirect( is_wp_error( $result ) ? [ 'error' => rawurlencode( $result->get_error_message() ) ] : [ 'notice' => 'pushed' ] );
424
+ }
425
+
426
+ public static function app_ips(): void {
427
+ self::guard( 'platform_app_ips' );
428
+ update_option( Apps::ALLOWED_IPS, sanitize_textarea_field( wp_unslash( (string) ( $_POST['allowed_ips'] ?? '' ) ) ), false );
429
+ self::redirect( [ 'notice' => 'saved' ] );
430
+ }
431
+
432
+ /* ---------------------------------------------------------------- helpers */
433
+
434
+ private static function notice(): void {
435
+ $messages = [
436
+ 'saved' => 'Saved.',
437
+ 'deleted' => 'Deleted.',
438
+ 'pushed' => 'The app was asked to pull content.',
439
+ ];
440
+ $notice = sanitize_key( wp_unslash( (string) ( $_GET['notice'] ?? '' ) ) );
441
+ $error = sanitize_text_field( rawurldecode( wp_unslash( (string) ( $_GET['error'] ?? '' ) ) ) );
442
+
443
+ if ( isset( $messages[ $notice ] ) ) {
444
+ printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html( $messages[ $notice ] ) );
445
+ }
446
+
447
+ if ( '' !== $error ) {
448
+ printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $error ) );
449
+ }
450
+ }
451
+
452
+ private static function guard( string $nonce ): void {
453
+ if ( ! current_user_can( 'manage_options' ) ) {
454
+ wp_die( 'Not allowed.', 403 );
455
+ }
456
+
457
+ check_admin_referer( $nonce );
458
+ }
459
+
460
+ public static function url( array $args = [] ): string {
461
+ return add_query_arg( $args, admin_url( 'admin.php?page=' . self::PAGE ) );
462
+ }
463
+
464
+ public static function action_url( string $action, array $args ): string {
465
+ return wp_nonce_url( add_query_arg( [ 'action' => 'platform_' . $action ] + $args, admin_url( 'admin-post.php' ) ), 'platform_' . $action );
466
+ }
467
+
468
+ private static function redirect( array $args ): void {
469
+ wp_safe_redirect( self::url( $args ) );
470
+ exit;
471
+ }
472
+ }
@@ -0,0 +1,193 @@
1
+ <?php
2
+ /**
3
+ * Content API for connected apps.
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Modules\Apps;
9
+
10
+ use Platform\Config;
11
+
12
+ defined( 'ABSPATH' ) || exit;
13
+
14
+ final class Api {
15
+
16
+ public static function register(): void {
17
+ self::basic_auth_header();
18
+ add_action( 'rest_api_init', [ __CLASS__, 'routes' ] );
19
+ }
20
+
21
+ private static function basic_auth_header(): void {
22
+ $header = (string) ( $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ?? '' );
23
+
24
+ if ( '' === $header || isset( $_SERVER['PHP_AUTH_USER'] ) || 0 !== stripos( $header, 'basic ' ) ) {
25
+ return;
26
+ }
27
+
28
+ $decoded = base64_decode( substr( $header, 6 ), true );
29
+
30
+ if ( false !== $decoded && false !== strpos( $decoded, ':' ) ) {
31
+ [ $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ] = explode( ':', $decoded, 2 );
32
+ }
33
+ }
34
+
35
+ public static function routes(): void {
36
+ register_rest_route(
37
+ 'cms/v1',
38
+ '/content',
39
+ [
40
+ 'methods' => 'GET',
41
+ 'callback' => [ __CLASS__, 'content' ],
42
+ 'permission_callback' => '__return_true',
43
+ ]
44
+ );
45
+
46
+ register_rest_route(
47
+ 'cms/v1',
48
+ '/apps/alerts/content/pull-success',
49
+ [
50
+ 'methods' => 'POST',
51
+ 'callback' => [ __CLASS__, 'pull_success' ],
52
+ 'permission_callback' => '__return_true',
53
+ ]
54
+ );
55
+ }
56
+
57
+ /**
58
+ * @return array|\WP_Error The signed-in app.
59
+ */
60
+ public static function client( \WP_REST_Request $request ) {
61
+ if ( ! is_user_logged_in() ) {
62
+ return new \WP_Error( 'unauthorized', 'Authentication required.', [ 'status' => 401 ] );
63
+ }
64
+
65
+ if ( ! current_user_can( Apps::CAP ) ) {
66
+ return new \WP_Error( 'forbidden', 'Insufficient permissions.', [ 'status' => 403 ] );
67
+ }
68
+
69
+ $client = Apps::get_by_user( get_current_user_id() );
70
+
71
+ if ( ! $client ) {
72
+ return new \WP_Error( 'no_client', 'No service client found for this user.', [ 'status' => 403 ] );
73
+ }
74
+
75
+ if ( empty( $client['metadata']['skip_ip_check'] ) ) {
76
+ $allowed = array_filter( array_map( 'trim', explode( "\n", (string) get_option( Apps::ALLOWED_IPS, '' ) ) ) );
77
+
78
+ if ( ! in_array( (string) ( $_SERVER['REMOTE_ADDR'] ?? '' ), $allowed, true ) ) {
79
+ return new \WP_Error( 'origin_denied', 'Request origin not allowed.', [ 'status' => 403 ] );
80
+ }
81
+ }
82
+
83
+ return $client;
84
+ }
85
+
86
+ public static function endpoints(): array {
87
+ $endpoints = [];
88
+
89
+ foreach ( Config::types() as $type ) {
90
+ if ( $type['api'] ) {
91
+ $endpoints[ (string) ( $type['api']['name'] ?? $type['key'] ) ] = $type;
92
+ }
93
+ }
94
+
95
+ return $endpoints;
96
+ }
97
+
98
+ public static function content( \WP_REST_Request $request ) {
99
+ $client = self::client( $request );
100
+
101
+ if ( is_wp_error( $client ) ) {
102
+ return $client;
103
+ }
104
+
105
+ $endpoints = self::endpoints();
106
+ $allowed = array_keys( $client['contents'] );
107
+
108
+ foreach ( $endpoints as $name => $type ) {
109
+ $link = (string) ( $type['api']['link'] ?? '' );
110
+
111
+ if ( '' !== $link && ! empty( $client['links'][ $link ] ) && ! in_array( $name, $allowed, true ) ) {
112
+ $allowed[] = $name;
113
+ }
114
+ }
115
+
116
+ $requested = (string) $request->get_param( 'type' );
117
+ $names = '' !== $requested ? array_intersect( array_map( 'trim', explode( ',', $requested ) ), $allowed ) : $allowed;
118
+ $meta = $client['metadata'] ?? [];
119
+
120
+ unset( $meta['skip_ip_check'] );
121
+
122
+ $response = [
123
+ 'name' => $client['name'] ?? '',
124
+ 'data' => [],
125
+ 'meta' => $meta,
126
+ ];
127
+
128
+ foreach ( $names as $name ) {
129
+ if ( isset( $endpoints[ $name ] ) && post_type_exists( $endpoints[ $name ]['key'] ) ) {
130
+ $response = Formatters::handle( $endpoints[ $name ], $name, $response, $client );
131
+ }
132
+ }
133
+
134
+ $placeholder_only = '' !== $requested && ! array_filter(
135
+ $names,
136
+ static fn( string $name ): bool => empty( $endpoints[ $name ]['api']['placeholders'] )
137
+ );
138
+
139
+ if ( $placeholder_only ) {
140
+ $map = [];
141
+ foreach ( $meta as $key => $value ) {
142
+ $map[ '[' . $key . ']' ] = esc_html( (string) $value );
143
+ }
144
+
145
+ array_walk_recursive(
146
+ $response['data'],
147
+ static function ( &$value ) use ( $map ): void {
148
+ if ( is_string( $value ) ) {
149
+ $value = strtr( $value, $map );
150
+ }
151
+ }
152
+ );
153
+ }
154
+
155
+ return new \WP_REST_Response( $response, 200 );
156
+ }
157
+
158
+ public static function pull_success( \WP_REST_Request $request ) {
159
+ $app = self::client( $request );
160
+
161
+ if ( is_wp_error( $app ) ) {
162
+ return $app;
163
+ }
164
+
165
+ $origin = Apps::host( (string) ( $request->get_header( 'origin' ) ?: $request->get_header( 'referer' ) ) );
166
+ $match = '';
167
+
168
+ foreach ( array_keys( Apps::environments() ) as $env ) {
169
+ $url = (string) ( $app['environments'][ $env ]['url'] ?? '' );
170
+
171
+ if ( '' !== $origin && '' !== $url && Apps::host( $url ) === $origin ) {
172
+ $match = $env;
173
+ break;
174
+ }
175
+ }
176
+
177
+ if ( '' === $match ) {
178
+ return new \WP_Error( 'origin_denied', 'Request origin is not the prod or staging url of this app.', [ 'status' => 403 ] );
179
+ }
180
+
181
+ $value = gmdate( 'Y-m-d H:i:s' );
182
+ Apps::set_pull( $app['id'], $match, $value );
183
+
184
+ return new \WP_REST_Response(
185
+ [
186
+ 'app_id' => $app['id'],
187
+ 'env' => $match,
188
+ 'last_pull' => $value . ' UTC',
189
+ ],
190
+ 200
191
+ );
192
+ }
193
+ }