@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,681 @@
1
+ <?php
2
+ /**
3
+ * Field types: read, render, sanitize and store.
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Content;
9
+
10
+ use Platform\Languages;
11
+
12
+ defined( 'ABSPATH' ) || exit;
13
+
14
+ final class Fields {
15
+
16
+ const VIDEO_SOURCES = [
17
+ 'youtube' => 'YouTube',
18
+ 'vimeo' => 'Vimeo',
19
+ 'external_url' => 'File',
20
+ 'html5' => 'Embed',
21
+ 'shortcode' => 'Shortcode',
22
+ ];
23
+
24
+ private static array $languages = [];
25
+
26
+ private static string $folder = '';
27
+
28
+ private static int $editor_count = 0;
29
+
30
+ public static function use_languages( array $languages ): void {
31
+ self::$languages = $languages ?: Languages::all();
32
+ }
33
+
34
+ public static function use_folder( string $folder ): void {
35
+ self::$folder = $folder;
36
+ }
37
+
38
+ /* ---------------------------------------------------------------- reading */
39
+
40
+ public static function values( string $object, int $id, array $fields ): array {
41
+ $values = [];
42
+
43
+ foreach ( $fields as $field ) {
44
+ $values[ $field['key'] ] = self::read( $object, $id, $field );
45
+ }
46
+
47
+ return $values;
48
+ }
49
+
50
+ public static function read( string $object, int $id, array $field ) {
51
+ $mode = $field['languages'] ?? '';
52
+
53
+ if ( 'suffix' === $mode ) {
54
+ $value = [];
55
+ foreach ( array_keys( self::$languages ) as $lang ) {
56
+ $value[ $lang ] = self::meta( $object, $id, $field['key'] . '_' . $lang );
57
+ }
58
+ return $value;
59
+ }
60
+
61
+ if ( 'scoring' === $field['type'] ) {
62
+ return [
63
+ 'model' => (string) self::meta( $object, $id, $field['key'] ),
64
+ 'params' => (array) self::meta( $object, $id, self::params_key( $field ) ),
65
+ ];
66
+ }
67
+
68
+ return self::meta( $object, $id, $field['key'] );
69
+ }
70
+
71
+ private static function meta( string $object, int $id, string $key ) {
72
+ if ( ! $id ) {
73
+ return null;
74
+ }
75
+
76
+ $value = get_metadata( $object, $id, $key, true );
77
+
78
+ while ( is_string( $value ) && is_serialized( $value ) ) {
79
+ $value = maybe_unserialize( $value );
80
+ }
81
+
82
+ return $value;
83
+ }
84
+
85
+ /* ---------------------------------------------------------------- saving */
86
+
87
+ public static function save( string $object, int $id, array $fields, array $input ): void {
88
+ foreach ( $fields as $field ) {
89
+ if ( ! array_key_exists( $field['key'], $input ) ) {
90
+ continue;
91
+ }
92
+
93
+ $raw = $input[ $field['key'] ];
94
+ $mode = $field['languages'] ?? '';
95
+
96
+ if ( 'scoring' === $field['type'] ) {
97
+ $model = self::sanitize( $field, $raw['model'] ?? '' );
98
+ update_metadata( $object, $id, $field['key'], $model );
99
+ update_metadata( $object, $id, self::params_key( $field ), Scoring::sanitize_params( $model, (array) ( $raw['params'][ $model ] ?? [] ) ) );
100
+ continue;
101
+ }
102
+
103
+ if ( 'suffix' === $mode ) {
104
+ foreach ( array_keys( self::$languages ) as $lang ) {
105
+ if ( array_key_exists( $lang, (array) $raw ) ) {
106
+ update_metadata( $object, $id, $field['key'] . '_' . $lang, self::sanitize( $field, $raw[ $lang ] ) );
107
+ }
108
+ }
109
+ continue;
110
+ }
111
+
112
+ update_metadata( $object, $id, $field['key'], self::clean( $field, $raw ) );
113
+ }
114
+ }
115
+
116
+ public static function clean( array $field, $raw ) {
117
+ if ( 'map' === ( $field['languages'] ?? '' ) ) {
118
+ $value = [];
119
+ foreach ( array_keys( self::$languages ) as $lang ) {
120
+ $value[ $lang ] = self::sanitize( $field, $raw[ $lang ] ?? '' );
121
+ }
122
+ return $value;
123
+ }
124
+
125
+ return self::sanitize( $field, $raw );
126
+ }
127
+
128
+ public static function sanitize( array $field, $raw ) {
129
+ switch ( $field['type'] ) {
130
+ case 'key':
131
+ return sanitize_key( is_scalar( $raw ) ? (string) $raw : '' );
132
+
133
+ case 'url':
134
+ return esc_url_raw( is_scalar( $raw ) ? trim( (string) $raw ) : '' );
135
+
136
+ case 'email':
137
+ return sanitize_email( is_scalar( $raw ) ? (string) $raw : '' );
138
+
139
+ case 'textarea':
140
+ return sanitize_textarea_field( is_scalar( $raw ) ? (string) $raw : '' );
141
+
142
+ case 'html':
143
+ case 'wysiwyg':
144
+ return wp_kses_post( is_scalar( $raw ) ? (string) $raw : '' );
145
+
146
+ case 'number':
147
+ return self::number( $raw );
148
+
149
+ case 'checkbox':
150
+ return empty( $raw ) ? 0 : 1;
151
+
152
+ case 'select':
153
+ case 'scoring':
154
+ $options = 'scoring' === $field['type'] ? Scoring::choices() : (array) ( $field['options'] ?? [] );
155
+ $value = is_scalar( $raw ) ? (string) $raw : '';
156
+ return array_key_exists( $value, $options ) ? $value : (string) ( $field['default'] ?? array_key_first( $options ) ?? '' );
157
+
158
+ case 'image':
159
+ return absint( $raw ) ?: '';
160
+
161
+ case 'files':
162
+ return array_values( array_filter( array_map( 'absint', (array) $raw ) ) );
163
+
164
+ case 'lines':
165
+ return self::lines( $raw );
166
+
167
+ case 'pairs':
168
+ return self::pairs( $raw, ! empty( $field['numeric'] ) );
169
+
170
+ case 'repeater':
171
+ return self::sanitize_rows( $field, $raw );
172
+
173
+ case 'video':
174
+ return self::sanitize_video( (array) $raw );
175
+
176
+ case 'audio':
177
+ return self::sanitize_audio( $field, (array) $raw );
178
+
179
+ case 'ebook':
180
+ return esc_url_raw( is_scalar( $raw ) ? trim( (string) $raw ) : '' );
181
+
182
+ default:
183
+ return sanitize_text_field( is_scalar( $raw ) ? (string) $raw : '' );
184
+ }
185
+ }
186
+
187
+ private static function sanitize_rows( array $field, $raw ): array {
188
+ $rows = [];
189
+ $required = (string) ( $field['required'] ?? '' );
190
+
191
+ foreach ( (array) $raw as $row ) {
192
+ if ( ! is_array( $row ) ) {
193
+ continue;
194
+ }
195
+
196
+ $clean = [];
197
+
198
+ foreach ( $field['fields'] as $sub ) {
199
+ if ( 'audio' === $sub['type'] && ! empty( $sub['inline'] ) ) {
200
+ $clean += self::sanitize_audio( $sub, $row );
201
+ continue;
202
+ }
203
+
204
+ $clean[ $sub['key'] ] = self::clean( $sub, $row[ $sub['key'] ] ?? ( 'checkbox' === $sub['type'] ? 0 : '' ) );
205
+ }
206
+
207
+ if ( '' !== $required && ( $clean[ $required ] ?? '' ) === '' ) {
208
+ continue;
209
+ }
210
+
211
+ if ( '' === $required && ! array_filter( $clean, static fn( $value ) => '' !== $value && [] !== $value && 0 !== $value ) ) {
212
+ continue;
213
+ }
214
+
215
+ $rows[] = $clean;
216
+ }
217
+
218
+ return $rows;
219
+ }
220
+
221
+ private static function sanitize_video( array $raw ): array {
222
+ $type = array_key_exists( $raw['source_type'] ?? '', self::VIDEO_SOURCES ) ? $raw['source_type'] : 'youtube';
223
+ $source = $raw['sources'][ $type ] ?? ( $raw['source'] ?? '' );
224
+ $source = is_scalar( $source ) ? (string) $source : '';
225
+
226
+ if ( 'html5' === $type ) {
227
+ $source = wp_kses( $source, self::embed_tags() );
228
+ } elseif ( 'shortcode' === $type ) {
229
+ $source = sanitize_text_field( $source );
230
+ } else {
231
+ $source = esc_url_raw( trim( $source ) );
232
+ }
233
+
234
+ $runtime = (array) ( $raw['runtime'] ?? [] );
235
+
236
+ return [
237
+ 'source_type' => $type,
238
+ 'source' => $source,
239
+ 'runtime' => [
240
+ 'hours' => str_pad( (string) absint( $runtime['hours'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
241
+ 'minutes' => str_pad( (string) absint( $runtime['minutes'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
242
+ 'seconds' => str_pad( (string) absint( $runtime['seconds'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
243
+ ],
244
+ ];
245
+ }
246
+
247
+ private static function sanitize_audio( array $field, array $raw ): array {
248
+ $urls = [];
249
+ $durations = [];
250
+
251
+ foreach ( array_keys( self::$languages ) as $lang ) {
252
+ $url = esc_url_raw( trim( (string) ( $raw['audio_url'][ $lang ] ?? '' ) ) );
253
+
254
+ if ( '' !== $url ) {
255
+ $urls[ $lang ] = $url;
256
+ $durations[ $lang ] = absint( $raw['duration'][ $lang ] ?? 0 );
257
+ }
258
+ }
259
+
260
+ return [
261
+ 'audio_url' => $urls,
262
+ 'duration' => $durations,
263
+ 'allow_download' => empty( $raw['allow_download'] ) ? 0 : 1,
264
+ ];
265
+ }
266
+
267
+ private static function embed_tags(): array {
268
+ $common = [ 'src' => true, 'width' => true, 'height' => true, 'title' => true, 'class' => true, 'style' => true ];
269
+
270
+ return [
271
+ 'iframe' => $common + [ 'frameborder' => true, 'allow' => true, 'allowfullscreen' => true, 'loading' => true, 'referrerpolicy' => true ],
272
+ 'video' => $common + [ 'controls' => true, 'poster' => true, 'preload' => true, 'autoplay' => true, 'muted' => true, 'loop' => true, 'playsinline' => true ],
273
+ 'source' => [ 'src' => true, 'type' => true ],
274
+ 'div' => [ 'class' => true, 'style' => true ],
275
+ ];
276
+ }
277
+
278
+ public static function number( $raw ) {
279
+ return is_scalar( $raw ) && is_numeric( $raw ) ? $raw + 0 : '';
280
+ }
281
+
282
+ public static function lines( $raw ): array {
283
+ if ( is_array( $raw ) ) {
284
+ $raw = implode( "\n", $raw );
285
+ }
286
+
287
+ $lines = preg_split( '/\r\n|\r|\n/', (string) $raw );
288
+
289
+ return array_values( array_filter( array_map( 'sanitize_text_field', $lines ), 'strlen' ) );
290
+ }
291
+
292
+ public static function pairs( $raw, bool $numeric = false ): array {
293
+ $out = [];
294
+
295
+ foreach ( self::lines( $raw ) as $line ) {
296
+ if ( false === strpos( $line, ':' ) ) {
297
+ continue;
298
+ }
299
+
300
+ [ $name, $value ] = array_map( 'trim', explode( ':', $line, 2 ) );
301
+ $name = preg_replace( '/[^A-Za-z0-9_\-]/', '', $name );
302
+
303
+ if ( '' !== $name ) {
304
+ $out[ $name ] = $numeric ? self::number( $value ) : $value;
305
+ }
306
+ }
307
+
308
+ return $out;
309
+ }
310
+
311
+ private static function params_key( array $field ): string {
312
+ return (string) ( $field['params_key'] ?? $field['key'] . '_params' );
313
+ }
314
+
315
+ /* ---------------------------------------------------------------- rendering */
316
+
317
+ public static function render( array $fields, array $values, string $prefix, int $depth = 0 ): void {
318
+ foreach ( $fields as $field ) {
319
+ if ( 'audio' === $field['type'] && ! empty( $field['inline'] ) ) {
320
+ echo '<div class="platform-field platform-field--audio">';
321
+ if ( '' !== $field['label'] ) {
322
+ printf( '<span class="platform-field__label">%s</span>', esc_html( $field['label'] ) );
323
+ }
324
+ self::render_audio( $field, $values, $prefix );
325
+ echo '</div>';
326
+ continue;
327
+ }
328
+
329
+ $value = $values[ $field['key'] ] ?? null;
330
+
331
+ if ( null === $value || '' === $value ) {
332
+ $value = $field['default'] ?? $value;
333
+ }
334
+
335
+ self::render_field( $field, $value, $prefix . '[' . $field['key'] . ']', $depth );
336
+ }
337
+ }
338
+
339
+ private static function render_field( array $field, $value, string $name, int $depth ): void {
340
+ $classes = [ 'platform-field', 'platform-field--' . $field['type'] ];
341
+
342
+ if ( ! empty( $field['width'] ) ) {
343
+ $classes[] = 'platform-field--' . sanitize_html_class( $field['width'] );
344
+ }
345
+
346
+ printf(
347
+ '<div class="%s"%s data-key="%s">',
348
+ esc_attr( implode( ' ', $classes ) ),
349
+ empty( $field['needs'] ) ? '' : ' data-needs="' . esc_attr( $field['needs'] ) . '"',
350
+ esc_attr( $field['key'] )
351
+ );
352
+
353
+ if ( '' !== $field['label'] && 'checkbox' !== $field['type'] ) {
354
+ printf( '<span class="platform-field__label">%s</span>', esc_html( $field['label'] ) );
355
+ }
356
+
357
+ $mode = $field['languages'] ?? '';
358
+
359
+ if ( in_array( $mode, [ 'suffix', 'map' ], true ) || 'ebook' === $field['type'] ) {
360
+ $value = is_array( $value ) ? $value : [];
361
+ self::render_languages(
362
+ static function ( string $lang ) use ( $field, $value, $name, $depth ): void {
363
+ self::control( $field, $value[ $lang ] ?? null, $name . '[' . $lang . ']', $depth );
364
+ }
365
+ );
366
+ } else {
367
+ self::control( $field, $value, $name, $depth );
368
+ }
369
+
370
+ if ( ! empty( $field['help'] ) ) {
371
+ printf( '<p class="description">%s</p>', esc_html( $field['help'] ) );
372
+ }
373
+
374
+ echo '</div>';
375
+ }
376
+
377
+ private static function render_languages( callable $panel ): void {
378
+ $languages = self::$languages;
379
+
380
+ if ( count( $languages ) < 2 ) {
381
+ $panel( (string) array_key_first( $languages ) );
382
+ return;
383
+ }
384
+
385
+ echo '<div class="platform-langs"><div class="platform-langs__tabs" role="tablist">';
386
+ $first = true;
387
+ foreach ( $languages as $code => $name ) {
388
+ printf(
389
+ '<button type="button" class="platform-langs__tab%s" data-lang="%s">%s</button>',
390
+ $first ? ' is-active' : '',
391
+ esc_attr( $code ),
392
+ esc_html( $name )
393
+ );
394
+ $first = false;
395
+ }
396
+ echo '</div>';
397
+
398
+ $first = true;
399
+ foreach ( array_keys( $languages ) as $code ) {
400
+ printf( '<div class="platform-langs__panel%s" data-lang="%s">', $first ? ' is-active' : '', esc_attr( $code ) );
401
+ $panel( $code );
402
+ echo '</div>';
403
+ $first = false;
404
+ }
405
+ echo '</div>';
406
+ }
407
+
408
+ private static function control( array $field, $value, string $name, int $depth ): void {
409
+ $attrs = self::attrs( $field );
410
+
411
+ switch ( $field['type'] ) {
412
+ case 'textarea':
413
+ case 'html':
414
+ printf( '<textarea name="%s" rows="%d"%s>%s</textarea>', esc_attr( $name ), (int) ( $field['rows'] ?? 3 ), $attrs, esc_textarea( (string) $value ) );
415
+ break;
416
+
417
+ case 'wysiwyg':
418
+ if ( $depth > 0 || wp_doing_ajax() ) {
419
+ printf( '<textarea name="%s" rows="%d" class="platform-html">%s</textarea>', esc_attr( $name ), (int) ( $field['rows'] ?? 6 ), esc_textarea( (string) $value ) );
420
+ break;
421
+ }
422
+ wp_editor(
423
+ (string) $value,
424
+ 'platformeditor' . ( ++self::$editor_count ),
425
+ [
426
+ 'textarea_name' => $name,
427
+ 'textarea_rows' => (int) ( $field['rows'] ?? 8 ),
428
+ 'media_buttons' => true,
429
+ ]
430
+ );
431
+ break;
432
+
433
+ case 'number':
434
+ printf( '<input type="number" step="%s" name="%s" value="%s"%s />', esc_attr( (string) ( $field['step'] ?? 'any' ) ), esc_attr( $name ), esc_attr( (string) $value ), $attrs );
435
+ break;
436
+
437
+ case 'checkbox':
438
+ printf(
439
+ '<label><input type="hidden" name="%1$s" value="0" /><input type="checkbox" name="%1$s" value="1"%2$s /> %3$s</label>',
440
+ esc_attr( $name ),
441
+ checked( ! empty( $value ), true, false ),
442
+ esc_html( $field['label'] )
443
+ );
444
+ break;
445
+
446
+ case 'select':
447
+ printf( '<select name="%s"%s>', esc_attr( $name ), $attrs );
448
+ if ( isset( $field['empty'] ) ) {
449
+ printf( '<option value="">%s</option>', esc_html( (string) $field['empty'] ) );
450
+ }
451
+ foreach ( (array) ( $field['options'] ?? [] ) as $option => $label ) {
452
+ printf( '<option value="%s"%s>%s</option>', esc_attr( (string) $option ), selected( (string) $value, (string) $option, false ), esc_html( (string) $label ) );
453
+ }
454
+ echo '</select>';
455
+ break;
456
+
457
+ case 'lines':
458
+ printf( '<textarea name="%s" rows="%d"%s>%s</textarea>', esc_attr( $name ), (int) ( $field['rows'] ?? 3 ), $attrs, esc_textarea( is_array( $value ) ? implode( "\n", $value ) : '' ) );
459
+ break;
460
+
461
+ case 'pairs':
462
+ $lines = [];
463
+ foreach ( is_array( $value ) ? $value : [] as $pair => $pair_value ) {
464
+ $lines[] = $pair . ': ' . ( is_scalar( $pair_value ) ? $pair_value : '' );
465
+ }
466
+ printf( '<textarea name="%s" rows="%d"%s>%s</textarea>', esc_attr( $name ), (int) ( $field['rows'] ?? 2 ), $attrs, esc_textarea( implode( "\n", $lines ) ) );
467
+ break;
468
+
469
+ case 'image':
470
+ self::render_image( $name, absint( $value ) );
471
+ break;
472
+
473
+ case 'files':
474
+ self::render_files( $name, is_array( $value ) ? $value : [] );
475
+ break;
476
+
477
+ case 'repeater':
478
+ self::render_repeater( $field, is_array( $value ) ? $value : [], $name, $depth );
479
+ break;
480
+
481
+ case 'video':
482
+ self::render_video( $field, is_array( $value ) ? $value : [], $name );
483
+ break;
484
+
485
+ case 'audio':
486
+ self::render_audio( $field, is_array( $value ) ? $value : [], $name );
487
+ break;
488
+
489
+ case 'ebook':
490
+ self::render_upload( $name, (string) $value, $field, 'application/pdf', 'Upload PDF' );
491
+ break;
492
+
493
+ case 'scoring':
494
+ Scoring::render( $field, is_array( $value ) ? $value : [], $name );
495
+ break;
496
+
497
+ default:
498
+ $type = in_array( $field['type'], [ 'url', 'email' ], true ) ? $field['type'] : 'text';
499
+ printf( '<input type="%s" name="%s" value="%s"%s />', esc_attr( $type ), esc_attr( $name ), esc_attr( is_scalar( $value ) ? (string) $value : '' ), $attrs );
500
+ }
501
+ }
502
+
503
+ private static function attrs( array $field ): string {
504
+ $out = '';
505
+
506
+ if ( ! empty( $field['placeholder'] ) ) {
507
+ $out .= ' placeholder="' . esc_attr( $field['placeholder'] ) . '"';
508
+ }
509
+
510
+ if ( ! empty( $field['list'] ) ) {
511
+ $out .= ' list="' . esc_attr( $field['list'] ) . '"';
512
+ }
513
+
514
+ if ( ! empty( $field['title_source'] ) ) {
515
+ $out .= ' data-row-title="1"';
516
+ }
517
+
518
+ return $out;
519
+ }
520
+
521
+ private static function render_repeater( array $field, array $rows, string $name, int $depth ): void {
522
+ $token = '__row' . $depth . '__';
523
+
524
+ printf( '<div class="platform-repeater" data-token="%s">', esc_attr( $token ) );
525
+ printf( '<input type="hidden" name="%s" value="" />', esc_attr( $name ) );
526
+ echo '<div class="platform-repeater__rows">';
527
+
528
+ $collapsed = count( $rows ) > 3;
529
+
530
+ foreach ( array_values( $rows ) as $index => $row ) {
531
+ self::render_row( $field, is_array( $row ) ? $row : [], $name . '[' . $index . ']', $depth, $collapsed );
532
+ }
533
+
534
+ echo '</div><template class="platform-repeater__template">';
535
+ self::render_row( $field, [], $name . '[' . $token . ']', $depth );
536
+ echo '</template>';
537
+
538
+ printf( '<button type="button" class="button platform-repeater__add">%s</button>', esc_html( (string) ( $field['add'] ?? 'Add row' ) ) );
539
+ echo '</div>';
540
+ }
541
+
542
+ private static function render_row( array $field, array $row, string $name, int $depth, bool $collapsed = false ): void {
543
+ printf( '<div class="platform-repeater__row%s">', $collapsed ? ' is-collapsed' : '' );
544
+ echo '<div class="platform-repeater__bar"><span class="platform-repeater__handle dashicons dashicons-menu" aria-hidden="true"></span>';
545
+ echo '<span class="platform-repeater__title"></span>';
546
+ echo '<button type="button" class="platform-icon-button platform-repeater__toggle" aria-label="Collapse"><span class="dashicons dashicons-arrow-up-alt2"></span></button>';
547
+ echo '<button type="button" class="platform-icon-button platform-repeater__remove" aria-label="Remove"><span class="dashicons dashicons-trash"></span></button></div>';
548
+ echo '<div class="platform-repeater__body">';
549
+ self::render( $field['fields'], $row, $name, $depth + 1 );
550
+ echo '</div></div>';
551
+ }
552
+
553
+ private static function render_image( string $name, int $id ): void {
554
+ $url = $id ? (string) ( wp_get_attachment_image_url( $id, 'medium' ) ?: wp_get_attachment_url( $id ) ) : '';
555
+
556
+ printf( '<div class="platform-image%s">', $id ? ' has-value' : '' );
557
+ printf( '<input type="hidden" class="platform-image__id" name="%s" value="%s" />', esc_attr( $name ), esc_attr( $id ? (string) $id : '' ) );
558
+ printf( '<a class="platform-image__frame" href="%1$s" target="_blank" rel="noopener"><img class="platform-image__preview" src="%1$s" alt="" /><span class="platform-image__name">%2$s</span></a>', esc_url( $url ), esc_html( $url ? rawurldecode( wp_basename( (string) wp_parse_url( $url, PHP_URL_PATH ) ) ) : '' ) );
559
+ echo '<span class="platform-image__actions"><button type="button" class="button platform-image__choose">Choose image</button>';
560
+ echo '<button type="button" class="button-link platform-image__remove">Remove</button></span>';
561
+ echo '</div>';
562
+ }
563
+
564
+ private static function render_files( string $name, array $ids ): void {
565
+ printf( '<div class="platform-files" data-name="%s"><input type="hidden" name="%s" value="" /><ul class="platform-files__list">', esc_attr( $name . '[]' ), esc_attr( $name ) );
566
+
567
+ foreach ( $ids as $id ) {
568
+ $id = absint( $id );
569
+ if ( ! $id ) {
570
+ continue;
571
+ }
572
+ printf(
573
+ '<li><input type="hidden" name="%s[]" value="%d" /><a href="%s" target="_blank" rel="noopener">%s</a> <button type="button" class="button-link platform-files__remove">Remove</button></li>',
574
+ esc_attr( $name ),
575
+ $id,
576
+ esc_url( (string) wp_get_attachment_url( $id ) ),
577
+ esc_html( get_the_title( $id ) ?: '#' . $id )
578
+ );
579
+ }
580
+
581
+ echo '</ul><button type="button" class="button platform-files__add">Add files</button></div>';
582
+ }
583
+
584
+ private static function render_video( array $field, array $value, string $name ): void {
585
+ $sources = array_intersect_key( self::VIDEO_SOURCES, array_flip( (array) ( $field['sources'] ?? array_keys( self::VIDEO_SOURCES ) ) ) );
586
+ $active = isset( $sources[ $value['source_type'] ?? '' ] ) ? $value['source_type'] : (string) array_key_first( $sources );
587
+ $source = (string) ( $value['source'] ?? '' );
588
+ $runtime = (array) ( $value['runtime'] ?? [] );
589
+
590
+ echo '<div class="platform-video">';
591
+ printf( '<input type="hidden" class="platform-video__type" name="%s[source_type]" value="%s" />', esc_attr( $name ), esc_attr( $active ) );
592
+ echo '<div class="platform-segmented">';
593
+ foreach ( $sources as $key => $label ) {
594
+ printf( '<button type="button" class="platform-segmented__item%s" data-source="%s">%s</button>', $key === $active ? ' is-active' : '', esc_attr( $key ), esc_html( $label ) );
595
+ }
596
+ echo '</div>';
597
+
598
+ foreach ( array_keys( $sources ) as $key ) {
599
+ $current = $key === $active ? $source : '';
600
+ $input = $name . '[sources][' . $key . ']';
601
+
602
+ printf( '<div class="platform-video__panel%s" data-source="%s">', $key === $active ? ' is-active' : '', esc_attr( $key ) );
603
+
604
+ if ( 'external_url' === $key ) {
605
+ self::render_upload( $input, $current, $field, 'video/*', 'Upload video' );
606
+ } elseif ( 'html5' === $key ) {
607
+ printf( '<textarea name="%s" rows="3" placeholder="&lt;iframe ...&gt;&lt;/iframe&gt;">%s</textarea>', esc_attr( $input ), esc_textarea( $current ) );
608
+ } elseif ( 'shortcode' === $key ) {
609
+ printf( '<input type="text" name="%s" value="%s" placeholder="[video id=&quot;1&quot;]" />', esc_attr( $input ), esc_attr( $current ) );
610
+ } else {
611
+ printf( '<input type="url" class="platform-video__link" name="%s" value="%s" placeholder="https://" />', esc_attr( $input ), esc_attr( $current ) );
612
+ }
613
+
614
+ echo '</div>';
615
+ }
616
+
617
+ echo '<div class="platform-runtime"><span class="platform-runtime__label">Duration</span>';
618
+ foreach ( [ 'hours' => 'h', 'minutes' => 'm', 'seconds' => 's' ] as $unit => $suffix ) {
619
+ printf(
620
+ '<label><input type="number" min="0" class="platform-runtime__%1$s" name="%2$s[runtime][%1$s]" value="%3$s" />%4$s</label>',
621
+ esc_attr( $unit ),
622
+ esc_attr( $name ),
623
+ esc_attr( (string) ( $runtime[ $unit ] ?? '00' ) ),
624
+ esc_html( $suffix )
625
+ );
626
+ }
627
+ echo '</div></div>';
628
+ }
629
+
630
+ private static function render_audio( array $field, $value, string $name ): void {
631
+ $value = is_array( $value ) ? $value : [];
632
+
633
+ self::render_languages(
634
+ static function ( string $lang ) use ( $field, $value, $name ): void {
635
+ $url = (string) ( $value['audio_url'][ $lang ] ?? '' );
636
+ echo '<div class="platform-audio">';
637
+ self::render_upload( $name . '[audio_url][' . $lang . ']', $url, $field, 'audio/*', 'Upload audio' );
638
+ printf(
639
+ '<label class="platform-audio__duration">Seconds <input type="number" min="0" class="platform-duration" name="%s[duration][%s]" value="%s" /></label>',
640
+ esc_attr( $name ),
641
+ esc_attr( $lang ),
642
+ esc_attr( (string) ( $value['duration'][ $lang ] ?? '' ) )
643
+ );
644
+ echo '</div>';
645
+ }
646
+ );
647
+
648
+ if ( false !== ( $field['download'] ?? true ) ) {
649
+ printf(
650
+ '<label class="platform-audio__download"><input type="hidden" name="%1$s[allow_download]" value="0" /><input type="checkbox" name="%1$s[allow_download]" value="1"%2$s /> Allow download</label>',
651
+ esc_attr( $name ),
652
+ checked( ! empty( $value['allow_download'] ), true, false )
653
+ );
654
+ }
655
+ }
656
+
657
+ private static function render_upload( string $name, string $url, array $field, string $accept, string $button ): void {
658
+ printf(
659
+ '<div class="platform-upload%s" data-accept="%s" data-folder="%s">',
660
+ '' !== $url ? ' has-value' : '',
661
+ esc_attr( (string) ( $field['accept'] ?? $accept ) ),
662
+ esc_attr( (string) ( $field['folder'] ?? self::$folder ) )
663
+ );
664
+ printf( '<input type="url" class="platform-upload__url" name="%s" value="%s" readonly />', esc_attr( $name ), esc_attr( $url ) );
665
+ printf( '<a class="platform-upload__link" href="%s" target="_blank" rel="noopener">%s</a>', esc_url( $url ), esc_html( '' !== $url ? rawurldecode( wp_basename( (string) wp_parse_url( $url, PHP_URL_PATH ) ) ) : '' ) );
666
+ echo '<span class="platform-upload__actions">';
667
+ printf( '<button type="button" class="button platform-upload__pick">%s</button>', esc_html( $button ) );
668
+ echo '<button type="button" class="button-link platform-upload__edit">Use a link</button>';
669
+ echo '<button type="button" class="button-link platform-upload__clear">Remove</button>';
670
+ echo '</span><span class="platform-upload__progress" hidden><span></span></span></div>';
671
+ }
672
+
673
+ public static function input(): array {
674
+ if ( isset( $_POST['platform_fields'] ) ) {
675
+ $data = json_decode( wp_unslash( (string) $_POST['platform_fields'] ), true );
676
+ return is_array( $data ) ? $data : [];
677
+ }
678
+
679
+ return (array) wp_unslash( $_POST['fields'] ?? [] );
680
+ }
681
+ }