@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,676 @@
1
+ <?php
2
+ namespace Platform\App;
3
+
4
+ use Platform\Config;
5
+ use Platform\Content\Fields;
6
+ use Platform\Content\Scoring;
7
+ use Platform\Languages;
8
+ use Platform\Modules\Apps\Api;
9
+ use Platform\Modules\Apps\Apps;
10
+ use Platform\Modules\Translations;
11
+
12
+ defined( 'ABSPATH' ) || exit;
13
+
14
+ final class Contents {
15
+
16
+ const STATUSES = [ 'publish', 'draft', 'pending', 'private', 'future' ];
17
+ const TEXTUAL = [ 'text', 'textarea', 'html', 'wysiwyg' ];
18
+
19
+ public static function taxonomy( string $key ): array {
20
+ $taxonomy = Config::taxonomy( $key );
21
+ $terms = [];
22
+
23
+ foreach ( get_terms( [ 'taxonomy' => $key, 'hide_empty' => false ] ) as $term ) {
24
+ $terms[] = [ 'id' => (int) $term->term_id, 'name' => $term->name, 'parent' => (int) $term->parent, 'count' => (int) $term->count ];
25
+ }
26
+
27
+ return [
28
+ 'key' => $key,
29
+ 'label' => (string) ( $taxonomy['label'] ?? $key ),
30
+ 'singular' => (string) ( $taxonomy['singular'] ?? $key ),
31
+ 'hierarchical' => ! empty( $taxonomy['hierarchical'] ),
32
+ 'relation' => (string) ( $taxonomy['relation'] ?? 'many' ),
33
+ 'terms' => $terms,
34
+ ];
35
+ }
36
+
37
+ public static function describe( string $key ): array {
38
+ $type = Config::type( $key );
39
+
40
+ return [
41
+ 'key' => $key,
42
+ 'label' => $type['label'],
43
+ 'singular' => $type['singular'],
44
+ 'kind' => $type['kind'],
45
+ 'supports' => $type['supports'],
46
+ 'fields' => $type['fields'],
47
+ 'boxes' => array_values( $type['boxes'] ),
48
+ 'languages' => array_keys( Languages::for_type( $key ) ),
49
+ 'taxonomies' => Config::taxonomies_for( $key ),
50
+ 'api' => (bool) $type['api'],
51
+ 'endpoint' => $type['api'] ? (string) ( $type['api']['name'] ?? $key ) : '',
52
+ 'translate' => $type['translate'],
53
+ 'folder' => $type['folder'],
54
+ 'child' => '' !== $type['children'] ? self::describe( $type['children'] ) : null,
55
+ ];
56
+ }
57
+
58
+ public static function schema(): array {
59
+ $types = [];
60
+ $taxonomies = [];
61
+
62
+ foreach ( Config::types() as $type ) {
63
+ if ( '' === $type['parent'] ) {
64
+ $types[] = self::describe( $type['key'] );
65
+ }
66
+ }
67
+
68
+ foreach ( array_keys( Config::taxonomies() ) as $key ) {
69
+ $taxonomies[ $key ] = self::taxonomy( $key );
70
+ }
71
+
72
+ return [ 'types' => $types, 'taxonomies' => $taxonomies, 'scoring' => Scoring::models() ];
73
+ }
74
+
75
+ private static function query_args( array $params ): array {
76
+ $all = array_column( array_filter( Config::types(), static fn( array $t ): bool => '' === $t['parent'] ), 'key' );
77
+ $types = array_values( array_intersect( array_map( 'sanitize_key', (array) ( $params['types'] ?? [] ) ), $all ) ) ?: $all;
78
+ $args = [
79
+ 'post_type' => $types,
80
+ 'post_status' => in_array( $params['status'] ?? '', self::STATUSES, true ) ? $params['status'] : self::STATUSES,
81
+ 'orderby' => in_array( $params['orderby'] ?? '', [ 'title', 'date', 'modified' ], true ) ? $params['orderby'] : 'modified',
82
+ 'order' => 'asc' === strtolower( (string) ( $params['order'] ?? '' ) ) ? 'ASC' : 'DESC',
83
+ 'post_parent' => 0,
84
+ ];
85
+ $tax = [];
86
+
87
+ foreach ( (array) ( $params['terms'] ?? [] ) as $taxonomy => $ids ) {
88
+ $ids = array_values( array_filter( array_map( 'intval', (array) $ids ) ) );
89
+
90
+ if ( $ids && taxonomy_exists( (string) $taxonomy ) ) {
91
+ $tax[] = [ 'taxonomy' => sanitize_key( (string) $taxonomy ), 'field' => 'term_id', 'terms' => $ids ];
92
+ }
93
+ }
94
+
95
+ if ( $tax ) {
96
+ $args['tax_query'] = array_merge( [ 'relation' => 'AND' ], $tax );
97
+ }
98
+
99
+ $search = trim( (string) ( $params['search'] ?? '' ) );
100
+
101
+ if ( '' !== $search ) {
102
+ $args['s'] = $search;
103
+ }
104
+
105
+ return $args;
106
+ }
107
+
108
+ public static function list( array $params ): array {
109
+ $per = max( 1, min( 100, (int) ( $params['per_page'] ?? 25 ) ) );
110
+ $page = max( 1, (int) ( $params['page'] ?? 1 ) );
111
+ $query = new \WP_Query( self::query_args( $params ) + [ 'posts_per_page' => $per, 'paged' => $page ] );
112
+ $rows = array_map( [ __CLASS__, 'row' ], $query->posts );
113
+
114
+ return [
115
+ 'items' => $rows,
116
+ 'total' => (int) $query->found_posts,
117
+ 'pages' => (int) $query->max_num_pages,
118
+ 'page' => $page,
119
+ ];
120
+ }
121
+
122
+ public static function ids( array $params ): array {
123
+ $query = new \WP_Query( self::query_args( $params ) + [ 'posts_per_page' => 5000, 'fields' => 'ids', 'no_found_rows' => true ] );
124
+
125
+ return array_map( 'intval', $query->posts );
126
+ }
127
+
128
+ public static function row( \WP_Post $post ): array {
129
+ $terms = [];
130
+
131
+ foreach ( Config::taxonomies_for( $post->post_type ) as $taxonomy ) {
132
+ $found = wp_get_object_terms( $post->ID, $taxonomy, [ 'fields' => 'ids' ] );
133
+ $terms[ $taxonomy ] = is_wp_error( $found ) ? [] : array_map( 'intval', $found );
134
+ }
135
+
136
+ $translated = Translations::get( $post->ID );
137
+ $languages = [];
138
+
139
+ foreach ( array_keys( Languages::for_type( $post->post_type ) ) as $lang ) {
140
+ $languages[ $lang ] = Languages::default() === $lang || '' !== trim( (string) ( $translated['post_title'][ $lang ] ?? '' ) );
141
+ }
142
+
143
+ $type = Config::type( $post->post_type );
144
+
145
+ return [
146
+ 'id' => (int) $post->ID,
147
+ 'title' => $post->post_title,
148
+ 'type' => $post->post_type,
149
+ 'status' => $post->post_status,
150
+ 'date' => $post->post_date_gmt,
151
+ 'modified' => $post->post_modified_gmt,
152
+ 'thumbnail' => (string) get_the_post_thumbnail_url( $post, 'medium' ),
153
+ 'terms' => $terms,
154
+ 'languages' => $languages,
155
+ 'children' => '' !== $type['children'] ? count( get_children( [ 'post_parent' => $post->ID, 'post_type' => $type['children'], 'fields' => 'ids', 'post_status' => 'any' ] ) ) : 0,
156
+ ];
157
+ }
158
+
159
+ public static function get( int $id, string $lang ): ?array {
160
+ $post = get_post( $id );
161
+ $type = $post ? Config::type( $post->post_type ) : [];
162
+
163
+ if ( ! $type ) {
164
+ return null;
165
+ }
166
+
167
+ $lang = self::lang( $post->post_type, $lang );
168
+ $default = Languages::default();
169
+ $languages = Languages::for_type( $post->post_type );
170
+
171
+ Fields::use_languages( $languages );
172
+
173
+ $translated = Translations::get( $id );
174
+ $texts = [];
175
+
176
+ foreach ( $translated as $key => $values ) {
177
+ if ( is_array( $values ) && isset( $values[ $lang ] ) ) {
178
+ $texts[ $key ] = (string) $values[ $lang ];
179
+ }
180
+ }
181
+
182
+ $thumbnails = [];
183
+
184
+ foreach ( array_keys( $languages ) as $code ) {
185
+ $attachment = (int) get_post_meta( $id, $code === $default ? '_thumbnail_id' : '_thumbnail_id_' . $code, true );
186
+ $thumbnails[ $code ] = [ 'id' => $attachment, 'url' => $attachment ? (string) wp_get_attachment_image_url( $attachment, 'medium' ) : '' ];
187
+ }
188
+
189
+ $terms = [];
190
+
191
+ foreach ( Config::taxonomies_for( $post->post_type ) as $taxonomy ) {
192
+ $found = wp_get_object_terms( $id, $taxonomy, [ 'fields' => 'ids' ] );
193
+ $terms[ $taxonomy ] = is_wp_error( $found ) ? [] : array_map( 'intval', $found );
194
+ }
195
+
196
+ $values = Fields::values( 'post', $id, $type['fields'] );
197
+ $media = [];
198
+
199
+ foreach ( array_unique( self::attachments( $type['fields'], $values ) ) as $attachment ) {
200
+ if ( get_post( $attachment ) ) {
201
+ $media[ $attachment ] = self::attachment( $attachment );
202
+ }
203
+ }
204
+
205
+ return [
206
+ 'id' => $id,
207
+ 'type' => $post->post_type,
208
+ 'parent' => (int) $post->post_parent,
209
+ 'status' => $post->post_status,
210
+ 'lang' => $lang,
211
+ 'default_lang' => $default,
212
+ 'core' => [
213
+ 'post_title' => $post->post_title,
214
+ 'post_excerpt' => $post->post_excerpt,
215
+ 'post_content' => $post->post_content,
216
+ ],
217
+ 'values' => $values,
218
+ 'media' => (object) $media,
219
+ 'translations' => $texts,
220
+ 'terms' => $terms,
221
+ 'thumbnails' => $thumbnails,
222
+ 'children' => '' !== $type['children'] ? self::tree( $id, $type['children'] ) : [],
223
+ 'modified' => $post->post_modified_gmt,
224
+ 'apps' => self::apps_for( $post ),
225
+ ];
226
+ }
227
+
228
+ private static function attachments( array $fields, $values ): array {
229
+ $ids = [];
230
+
231
+ foreach ( $fields as $field ) {
232
+ $value = is_array( $values ) ? ( $values[ $field['key'] ] ?? null ) : null;
233
+
234
+ if ( null === $value ) {
235
+ continue;
236
+ }
237
+
238
+ $slots = in_array( $field['languages'] ?? '', [ 'suffix', 'map' ], true ) && is_array( $value ) ? array_values( $value ) : [ $value ];
239
+
240
+ foreach ( $slots as $slot ) {
241
+ if ( 'image' === $field['type'] && (int) $slot ) {
242
+ $ids[] = (int) $slot;
243
+ } elseif ( 'files' === $field['type'] ) {
244
+ $ids = array_merge( $ids, array_map( 'intval', (array) $slot ) );
245
+ } elseif ( 'repeater' === $field['type'] && is_array( $slot ) ) {
246
+ foreach ( $slot as $row ) {
247
+ $ids = array_merge( $ids, self::attachments( (array) ( $field['fields'] ?? [] ), $row ) );
248
+ }
249
+ }
250
+ }
251
+ }
252
+
253
+ return array_filter( $ids );
254
+ }
255
+
256
+ private static function lang( string $type, string $lang ): string {
257
+ $languages = Languages::for_type( $type );
258
+
259
+ return isset( $languages[ $lang ] ) ? $lang : Languages::default();
260
+ }
261
+
262
+ private static function tree( int $parent, string $type ): array {
263
+ $child = Config::type( $type );
264
+ $out = [];
265
+
266
+ foreach ( get_posts( [ 'post_type' => $type, 'post_parent' => $parent, 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ] ) as $post ) {
267
+ $out[] = [
268
+ 'id' => (int) $post->ID,
269
+ 'title' => $post->post_title,
270
+ 'type' => $type,
271
+ 'status' => $post->post_status,
272
+ 'children' => '' !== $child['children'] ? self::tree( (int) $post->ID, $child['children'] ) : [],
273
+ ];
274
+ }
275
+
276
+ return $out;
277
+ }
278
+
279
+ private static function apps_for( \WP_Post $post ): array {
280
+ $endpoint = '';
281
+
282
+ foreach ( Api::endpoints() as $name => $type ) {
283
+ if ( $type['key'] === $post->post_type ) {
284
+ $endpoint = $name;
285
+ }
286
+ }
287
+
288
+ if ( '' === $endpoint ) {
289
+ return [];
290
+ }
291
+
292
+ $terms = [];
293
+
294
+ foreach ( Config::taxonomies_for( $post->post_type ) as $taxonomy ) {
295
+ $found = wp_get_object_terms( $post->ID, $taxonomy, [ 'fields' => 'ids' ] );
296
+ $terms = array_merge( $terms, is_wp_error( $found ) ? [] : array_map( 'intval', $found ) );
297
+ }
298
+
299
+ $apps = [];
300
+
301
+ foreach ( Apps::all() as $app ) {
302
+ $rule = $app['contents'][ $endpoint ] ?? null;
303
+
304
+ if ( ! is_array( $rule ) || in_array( $post->ID, array_map( 'intval', (array) ( $rule['blocked'] ?? [] ) ), true ) ) {
305
+ continue;
306
+ }
307
+
308
+ $pinned = in_array( $post->ID, array_map( 'intval', (array) ( $rule['pinned'] ?? [] ) ), true );
309
+ $included = array_intersect( $terms, array_map( 'intval', (array) ( $rule['include_by_terms'] ?? [] ) ) );
310
+ $excluded = array_intersect( $terms, array_map( 'intval', (array) ( $rule['exclude_by_terms'] ?? [] ) ) );
311
+
312
+ if ( $pinned || ( $included && ! $excluded ) ) {
313
+ $apps[] = [ 'id' => (int) $app['id'], 'name' => (string) $app['name'], 'pinned' => $pinned ];
314
+ }
315
+ }
316
+
317
+ return $apps;
318
+ }
319
+
320
+ public static function save( int $id, array $input ) {
321
+ $post = get_post( $id );
322
+ $type = $post ? Config::type( $post->post_type ) : [];
323
+
324
+ if ( ! $type ) {
325
+ return new \WP_Error( 'not_found', 'Content not found.', [ 'status' => 404 ] );
326
+ }
327
+
328
+ if ( ! current_user_can( 'edit_post', $id ) ) {
329
+ return new \WP_Error( 'forbidden', 'You cannot edit this item.', [ 'status' => 403 ] );
330
+ }
331
+
332
+ $lang = self::lang( $post->post_type, (string) ( $input['lang'] ?? '' ) );
333
+ $default = Languages::default();
334
+ $languages = Languages::for_type( $post->post_type );
335
+ $core = array_intersect_key( (array) ( $input['core'] ?? [] ), array_flip( [ 'post_title', 'post_excerpt', 'post_content' ] ) );
336
+
337
+ Fields::use_languages( $languages );
338
+ Fields::use_folder( $type['folder'] );
339
+
340
+ if ( $lang === $default ) {
341
+ $update = [ 'ID' => $id ];
342
+
343
+ foreach ( $core as $key => $value ) {
344
+ $update[ $key ] = 'post_title' === $key ? sanitize_text_field( (string) $value ) : wp_kses_post( (string) $value );
345
+ }
346
+
347
+ if ( isset( $input['status'] ) && in_array( $input['status'], self::STATUSES, true ) ) {
348
+ $update['post_status'] = $input['status'];
349
+ }
350
+
351
+ if ( count( $update ) > 1 ) {
352
+ wp_update_post( wp_slash( $update ) );
353
+ }
354
+ } else {
355
+ $texts = [];
356
+
357
+ foreach ( $core as $key => $value ) {
358
+ $texts[ $key ] = [ $lang => 'post_title' === $key ? sanitize_text_field( (string) $value ) : wp_kses_post( (string) $value ) ];
359
+ }
360
+
361
+ foreach ( (array) ( $input['translations'] ?? [] ) as $key => $value ) {
362
+ if ( 0 === strpos( (string) $key, 'meta_' ) && is_scalar( $value ) ) {
363
+ $texts[ (string) $key ] = [ $lang => wp_kses_post( (string) $value ) ];
364
+ }
365
+ }
366
+
367
+ if ( $texts ) {
368
+ Translations::put( $id, 'post', $texts );
369
+ }
370
+ }
371
+
372
+ $fields = (array) ( $input['fields'] ?? [] );
373
+
374
+ if ( $fields ) {
375
+ Fields::save( 'post', $id, $type['fields'], $fields );
376
+ }
377
+
378
+ foreach ( (array) ( $input['terms'] ?? [] ) as $taxonomy => $ids ) {
379
+ $taxonomy = sanitize_key( (string) $taxonomy );
380
+
381
+ if ( in_array( $taxonomy, Config::taxonomies_for( $post->post_type ), true ) ) {
382
+ $ids = array_values( array_filter( array_map( 'intval', (array) $ids ) ) );
383
+
384
+ if ( 'one' === ( Config::taxonomy( $taxonomy )['relation'] ?? '' ) ) {
385
+ $ids = array_slice( $ids, 0, 1 );
386
+ }
387
+
388
+ wp_set_object_terms( $id, $ids, $taxonomy );
389
+ }
390
+ }
391
+
392
+ foreach ( (array) ( $input['thumbnails'] ?? [] ) as $code => $attachment ) {
393
+ $code = sanitize_key( (string) $code );
394
+
395
+ if ( ! isset( $languages[ $code ] ) ) {
396
+ continue;
397
+ }
398
+
399
+ $key = $code === $default ? '_thumbnail_id' : '_thumbnail_id_' . $code;
400
+ (int) $attachment ? update_post_meta( $id, $key, (int) $attachment ) : delete_post_meta( $id, $key );
401
+ }
402
+
403
+ clean_post_cache( $id );
404
+
405
+ return self::get( $id, $lang );
406
+ }
407
+
408
+ public static function create( array $input ) {
409
+ $type = sanitize_key( (string) ( $input['type'] ?? '' ) );
410
+ $config = Config::type( $type );
411
+ $parent = (int) ( $input['parent'] ?? 0 );
412
+
413
+ if ( ! $config ) {
414
+ return new \WP_Error( 'type', 'Unknown content type.', [ 'status' => 400 ] );
415
+ }
416
+
417
+ if ( '' !== $config['parent'] && ( ! $parent || get_post_type( $parent ) !== $config['parent'] ) ) {
418
+ return new \WP_Error( 'parent', 'This item needs a parent.', [ 'status' => 400 ] );
419
+ }
420
+
421
+ $siblings = $parent ? count( get_children( [ 'post_parent' => $parent, 'post_type' => $type, 'fields' => 'ids', 'post_status' => 'any' ] ) ) : 0;
422
+ $id = wp_insert_post(
423
+ wp_slash(
424
+ [
425
+ 'post_type' => $type,
426
+ 'post_title' => sanitize_text_field( (string) ( $input['title'] ?? '' ) ) ?: sprintf( 'New %s', $config['singular'] ),
427
+ 'post_status' => $parent ? 'publish' : 'draft',
428
+ 'post_parent' => $parent,
429
+ 'menu_order' => $siblings + 1,
430
+ ]
431
+ ),
432
+ true
433
+ );
434
+
435
+ return is_wp_error( $id ) ? $id : self::get( (int) $id, Languages::default() );
436
+ }
437
+
438
+ public static function sort( array $ids ): void {
439
+ foreach ( array_values( array_map( 'intval', $ids ) ) as $position => $id ) {
440
+ if ( $id && current_user_can( 'edit_post', $id ) ) {
441
+ wp_update_post( [ 'ID' => $id, 'menu_order' => $position + 1 ] );
442
+ }
443
+ }
444
+ }
445
+
446
+ public static function bulk( string $action, array $ids, array $payload ) {
447
+ $ids = array_values( array_filter( array_map( 'intval', $ids ), static fn( int $id ): bool => $id && current_user_can( 'edit_post', $id ) && Config::type( (string) get_post_type( $id ) ) ) );
448
+ $done = 0;
449
+
450
+ if ( ! $ids ) {
451
+ return new \WP_Error( 'empty', 'Nothing selected that you can edit.', [ 'status' => 400 ] );
452
+ }
453
+
454
+ switch ( $action ) {
455
+ case 'delete':
456
+ foreach ( $ids as $id ) {
457
+ $done += wp_trash_post( $id ) ? 1 : 0;
458
+ }
459
+ break;
460
+
461
+ case 'status':
462
+ $status = (string) ( $payload['status'] ?? '' );
463
+
464
+ if ( ! in_array( $status, self::STATUSES, true ) ) {
465
+ return new \WP_Error( 'status', 'Choose a status.', [ 'status' => 400 ] );
466
+ }
467
+
468
+ foreach ( $ids as $id ) {
469
+ $done += wp_update_post( [ 'ID' => $id, 'post_status' => $status ] ) ? 1 : 0;
470
+ }
471
+ break;
472
+
473
+ case 'terms':
474
+ $taxonomy = sanitize_key( (string) ( $payload['taxonomy'] ?? '' ) );
475
+ $terms = array_values( array_filter( array_map( 'intval', (array) ( $payload['terms'] ?? [] ) ) ) );
476
+ $mode = in_array( $payload['mode'] ?? '', [ 'add', 'remove', 'set' ], true ) ? $payload['mode'] : 'add';
477
+
478
+ foreach ( $ids as $id ) {
479
+ if ( ! is_object_in_taxonomy( (string) get_post_type( $id ), $taxonomy ) ) {
480
+ continue;
481
+ }
482
+
483
+ if ( 'remove' === $mode ) {
484
+ wp_remove_object_terms( $id, $terms, $taxonomy );
485
+ } else {
486
+ wp_set_object_terms( $id, $terms, $taxonomy, 'add' === $mode );
487
+ }
488
+
489
+ ++$done;
490
+ }
491
+ break;
492
+
493
+ case 'thumbnail':
494
+ $items = (array) ( $payload['items'] ?? [] );
495
+ $lang = sanitize_key( (string) ( $payload['lang'] ?? Languages::default() ) );
496
+ $key = Languages::default() === $lang ? '_thumbnail_id' : '_thumbnail_id_' . $lang;
497
+
498
+ foreach ( $ids as $id ) {
499
+ $attachment = (int) ( $items[ $id ] ?? $payload['attachment'] ?? 0 );
500
+
501
+ if ( $attachment ) {
502
+ update_post_meta( $id, $key, $attachment );
503
+ ++$done;
504
+ }
505
+ }
506
+ break;
507
+
508
+ case 'push':
509
+ return self::push( $ids, array_map( 'intval', (array) ( $payload['apps'] ?? [] ) ), sanitize_key( (string) ( $payload['env'] ?? '' ) ) );
510
+
511
+ default:
512
+ return new \WP_Error( 'action', 'Unknown action.', [ 'status' => 400 ] );
513
+ }
514
+
515
+ return [ 'done' => $done, 'total' => count( $ids ) ];
516
+ }
517
+
518
+ private static function push( array $ids, array $apps, string $env ): array {
519
+ $endpoints = [];
520
+
521
+ foreach ( Api::endpoints() as $name => $type ) {
522
+ $endpoints[ $type['key'] ] = $name;
523
+ }
524
+
525
+ $results = [];
526
+
527
+ foreach ( $apps as $app_id ) {
528
+ $app = Apps::get( $app_id );
529
+
530
+ if ( ! $app ) {
531
+ continue;
532
+ }
533
+
534
+ $added = 0;
535
+
536
+ foreach ( $ids as $id ) {
537
+ $name = $endpoints[ (string) get_post_type( $id ) ] ?? '';
538
+
539
+ if ( '' === $name ) {
540
+ continue;
541
+ }
542
+
543
+ $rule = (array) ( $app['contents'][ $name ] ?? [ 'pinned' => [], 'blocked' => [], 'include_by_terms' => [], 'exclude_by_terms' => [] ] );
544
+
545
+ $rule['pinned'] = array_values( array_unique( array_merge( array_map( 'intval', (array) ( $rule['pinned'] ?? [] ) ), [ $id ] ) ) );
546
+ $rule['blocked'] = array_values( array_diff( array_map( 'intval', (array) ( $rule['blocked'] ?? [] ) ), [ $id ] ) );
547
+
548
+ $app['contents'][ $name ] = $rule;
549
+ ++$added;
550
+ }
551
+
552
+ Apps::save( $app_id, $app );
553
+
554
+ $result = [ 'id' => $app_id, 'name' => (string) $app['name'], 'added' => $added, 'pushed' => false, 'error' => '' ];
555
+
556
+ if ( '' !== $env ) {
557
+ $pushed = Apps::push( $app_id, $env );
558
+ $result['pushed'] = ! is_wp_error( $pushed );
559
+ $result['error'] = is_wp_error( $pushed ) ? $pushed->get_error_message() : '';
560
+ }
561
+
562
+ $results[] = $result;
563
+ }
564
+
565
+ return [ 'apps' => $results, 'total' => count( $ids ) ];
566
+ }
567
+
568
+ public static function for_app( int $app_id, string $endpoint, array $params ) {
569
+ $app = Apps::get( $app_id );
570
+ $type = Api::endpoints()[ $endpoint ] ?? null;
571
+ $rule = $app['contents'][ $endpoint ] ?? null;
572
+
573
+ if ( ! $app || ! $type || ! is_array( $rule ) ) {
574
+ return [ 'items' => [], 'total' => 0, 'pages' => 0, 'page' => 1 ];
575
+ }
576
+
577
+ $include = array_map( 'intval', (array) ( $rule['include_by_terms'] ?? [] ) );
578
+ $exclude = array_map( 'intval', (array) ( $rule['exclude_by_terms'] ?? [] ) );
579
+ $pinned = array_map( 'intval', (array) ( $rule['pinned'] ?? [] ) );
580
+ $blocked = array_map( 'intval', (array) ( $rule['blocked'] ?? [] ) );
581
+ $ids = [];
582
+
583
+ if ( $include ) {
584
+ $taxonomies = Config::taxonomies_for( $type['key'] );
585
+ $any = [ 'relation' => 'OR' ];
586
+
587
+ foreach ( $taxonomies as $taxonomy ) {
588
+ $any[] = [ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $include ];
589
+ }
590
+
591
+ $tax = [ 'relation' => 'AND', $any ];
592
+
593
+ foreach ( $exclude ? $taxonomies : [] as $taxonomy ) {
594
+ $tax[] = [ 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $exclude, 'operator' => 'NOT IN' ];
595
+ }
596
+
597
+ $ids = ( new \WP_Query(
598
+ [
599
+ 'post_type' => $type['key'],
600
+ 'post_status' => 'publish',
601
+ 'post_parent' => 0,
602
+ 'tax_query' => $tax,
603
+ 'posts_per_page' => 5000,
604
+ 'fields' => 'ids',
605
+ 'no_found_rows' => true,
606
+ 'orderby' => 'modified',
607
+ ]
608
+ ) )->posts;
609
+ }
610
+
611
+ $ids = array_values( array_diff( array_unique( array_merge( $pinned, array_map( 'intval', $ids ) ) ), $blocked, [ 0 ] ) );
612
+ $per = max( 1, min( 60, (int) ( $params['per_page'] ?? 24 ) ) );
613
+ $page = max( 1, (int) ( $params['page'] ?? 1 ) );
614
+ $slice = array_slice( $ids, ( $page - 1 ) * $per, $per );
615
+ $posts = $slice ? get_posts( [ 'post__in' => $slice, 'post_type' => $type['key'], 'post_status' => 'any', 'posts_per_page' => $per, 'orderby' => 'post__in' ] ) : [];
616
+
617
+ return [
618
+ 'items' => array_map( static fn( \WP_Post $post ): array => self::row( $post ) + [ 'pinned' => in_array( (int) $post->ID, $pinned, true ) ], $posts ),
619
+ 'total' => count( $ids ),
620
+ 'pages' => (int) ceil( count( $ids ) / $per ),
621
+ 'page' => $page,
622
+ ];
623
+ }
624
+
625
+ public static function upload( string $folder ) {
626
+ if ( empty( $_FILES['file']['name'] ) ) {
627
+ return new \WP_Error( 'missing', 'No file uploaded.', [ 'status' => 400 ] );
628
+ }
629
+
630
+ $check = wp_check_filetype( (string) $_FILES['file']['name'] );
631
+
632
+ if ( '' !== $folder ) {
633
+ $stored = \Platform\Editors\Files::store( 'file', sanitize_title( $folder ), get_allowed_mime_types(), (int) Module::config( 'uploads.media_max_mb', 2048 ) );
634
+
635
+ return is_wp_error( $stored ) ? new \WP_Error( $stored->get_error_code(), $stored->get_error_message(), [ 'status' => 400 ] ) : $stored + [ 'type' => (string) $check['type'] ];
636
+ }
637
+
638
+ require_once ABSPATH . 'wp-admin/includes/file.php';
639
+ require_once ABSPATH . 'wp-admin/includes/media.php';
640
+ require_once ABSPATH . 'wp-admin/includes/image.php';
641
+
642
+ $id = media_handle_upload( 'file', 0 );
643
+
644
+ return is_wp_error( $id ) ? new \WP_Error( $id->get_error_code(), $id->get_error_message(), [ 'status' => 400 ] ) : self::attachment( (int) $id );
645
+ }
646
+
647
+ public static function attachment( int $id ): array {
648
+ return [
649
+ 'id' => $id,
650
+ 'url' => (string) ( wp_get_attachment_image_url( $id, 'medium' ) ?: wp_get_attachment_url( $id ) ),
651
+ 'full' => (string) wp_get_attachment_url( $id ),
652
+ 'title' => get_the_title( $id ),
653
+ 'type' => (string) get_post_mime_type( $id ),
654
+ ];
655
+ }
656
+
657
+ public static function library( array $params ): array {
658
+ $query = new \WP_Query(
659
+ [
660
+ 'post_type' => 'attachment',
661
+ 'post_status' => 'inherit',
662
+ 'post_mime_type' => sanitize_text_field( (string) ( $params['mime'] ?? 'image' ) ),
663
+ 's' => sanitize_text_field( (string) ( $params['search'] ?? '' ) ),
664
+ 'posts_per_page' => 40,
665
+ 'paged' => max( 1, (int) ( $params['page'] ?? 1 ) ),
666
+ 'orderby' => 'date',
667
+ 'order' => 'DESC',
668
+ ]
669
+ );
670
+
671
+ return [
672
+ 'items' => array_map( static fn( \WP_Post $post ): array => self::attachment( (int) $post->ID ), $query->posts ),
673
+ 'pages' => (int) $query->max_num_pages,
674
+ ];
675
+ }
676
+ }