@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,253 @@
1
+ <?php
2
+ /**
3
+ * Scoring models for interactive content (surveys, quizzes, assessments).
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Content;
9
+
10
+ defined( 'ABSPATH' ) || exit;
11
+
12
+ final class Scoring {
13
+
14
+ public static function models(): array {
15
+ return [
16
+ 'profile' => [
17
+ 'label' => 'Ranked Profile',
18
+ 'summary' => 'Every subscale gets an average score. They are ranked against each other and the highest one is the answer. There is no pass mark and no comparison to other people.',
19
+ 'examples' => 'CliftonStrengths top 5 · Holland career codes · Enneagram · Love Languages · Belbin team roles',
20
+ 'uses' => [ 'subscales', 'option_value' ],
21
+ 'params' => [
22
+ 'show_top' => [
23
+ 'label' => 'How many results to show',
24
+ 'type' => 'number',
25
+ 'default' => 3,
26
+ ],
27
+ 'tie_break' => [
28
+ 'label' => 'If two subscales tie',
29
+ 'type' => 'select',
30
+ 'default' => 'order',
31
+ 'options' => [
32
+ 'order' => 'Keep the order they are listed in',
33
+ 'items' => 'More answered questions wins',
34
+ ],
35
+ ],
36
+ ],
37
+ ],
38
+
39
+ 'band' => [
40
+ 'label' => 'Score Band',
41
+ 'summary' => 'All answers add up to one total. The total falls into a band you define, and that band is the result. Nothing is compared to other people.',
42
+ 'examples' => 'GAD-7 anxiety · PHQ-9 depression · AUDIT · Perceived Stress Scale · Epworth Sleepiness Scale',
43
+ 'uses' => [ 'bands', 'option_value' ],
44
+ 'params' => [
45
+ 'reduce' => [
46
+ 'label' => 'Combine answers by',
47
+ 'type' => 'select',
48
+ 'default' => 'sum',
49
+ 'options' => [
50
+ 'sum' => 'Adding them up',
51
+ 'mean' => 'Averaging them',
52
+ ],
53
+ ],
54
+ ],
55
+ ],
56
+
57
+ 'subscale_bands' => [
58
+ 'label' => 'Band Per Subscale',
59
+ 'summary' => 'Like Score Band, but each subscale is banded on its own. The result is a list of levels rather than a single one, so a report can say which part is high and which is low.',
60
+ 'examples' => 'DASS-21 depression / anxiety / stress · Maslach Burnout Inventory · Strengths and Difficulties Questionnaire',
61
+ 'uses' => [ 'subscales', 'bands', 'option_value' ],
62
+ 'params' => [
63
+ 'reduce' => [
64
+ 'label' => 'Combine answers by',
65
+ 'type' => 'select',
66
+ 'default' => 'sum',
67
+ 'options' => [
68
+ 'sum' => 'Adding them up',
69
+ 'mean' => 'Averaging them',
70
+ ],
71
+ ],
72
+ ],
73
+ ],
74
+
75
+ 'typology' => [
76
+ 'label' => 'Type From Axes',
77
+ 'summary' => 'Each axis is one subscale split at a midpoint into a low side and a high side. The sides combine into a named type, so the result is a type rather than a score.',
78
+ 'examples' => 'MBTI 16 types · DISC quadrants · Attachment style from anxiety and avoidance · Political Compass',
79
+ 'uses' => [ 'subscales', 'axes', 'types', 'option_value' ],
80
+ 'params' => [],
81
+ ],
82
+
83
+ 'weighted_outcome' => [
84
+ 'label' => 'Points To Outcomes',
85
+ 'summary' => 'Points sit on the answer options, not on the questions. One option can send points to several outcomes. The outcome with the most points wins. This is the model for recommendation and sign-up funnels.',
86
+ 'examples' => 'Which plan suits you · product and skin-type finders · diet matchers · lead scoring',
87
+ 'uses' => [ 'outcomes', 'option_points' ],
88
+ 'params' => [
89
+ 'tie_break' => [
90
+ 'label' => 'If two outcomes tie',
91
+ 'type' => 'select',
92
+ 'default' => 'order',
93
+ 'options' => [
94
+ 'order' => 'Keep the order they are listed in',
95
+ 'latest' => 'The most recently scored wins',
96
+ ],
97
+ ],
98
+ 'min_points' => [
99
+ 'label' => 'Minimum points to accept a winner',
100
+ 'type' => 'number',
101
+ 'default' => 0,
102
+ ],
103
+ ],
104
+ ],
105
+
106
+ 'graded' => [
107
+ 'label' => 'Graded Answers',
108
+ 'summary' => 'Options are marked right or wrong. Right answers earn their question weight, and the total can be banded or checked against a pass mark.',
109
+ 'examples' => 'Certification and licensing exams · driving theory tests · placement tests · pattern puzzles',
110
+ 'uses' => [ 'bands', 'option_correct' ],
111
+ 'params' => [
112
+ 'weighting' => [
113
+ 'label' => 'Question weights',
114
+ 'type' => 'select',
115
+ 'default' => 'flat',
116
+ 'options' => [
117
+ 'flat' => 'Every question counts the same',
118
+ 'item' => 'Use the weight set on each question',
119
+ ],
120
+ ],
121
+ 'pass_mark' => [
122
+ 'label' => 'Pass mark as a percentage',
123
+ 'type' => 'number',
124
+ 'default' => 60,
125
+ ],
126
+ 'negative_marking' => [
127
+ 'label' => 'Points removed for a wrong answer',
128
+ 'type' => 'number',
129
+ 'default' => 0,
130
+ ],
131
+ ],
132
+ ],
133
+
134
+ 'normed' => [
135
+ 'label' => 'Standardised Index',
136
+ 'summary' => 'The raw score is converted onto a standard scale so it can be read as a percentile and an index number. Subscales can be mixed at different weights, and completion time can nudge the result.',
137
+ 'examples' => 'WAIS and Stanford-Binet IQ · Raven\'s Progressive Matrices · clinical T-scores · stanines · percentile ranks',
138
+ 'uses' => [ 'subscales', 'mix', 'bands', 'option_correct' ],
139
+ 'params' => [
140
+ 'mean' => [
141
+ 'label' => 'Index mean',
142
+ 'type' => 'number',
143
+ 'default' => 100,
144
+ ],
145
+ 'sd' => [
146
+ 'label' => 'Index standard deviation',
147
+ 'type' => 'number',
148
+ 'default' => 15,
149
+ ],
150
+ 'floor' => [
151
+ 'label' => 'Lowest index allowed',
152
+ 'type' => 'number',
153
+ 'default' => 55,
154
+ ],
155
+ 'ceiling' => [
156
+ 'label' => 'Highest index allowed',
157
+ 'type' => 'number',
158
+ 'default' => 160,
159
+ ],
160
+ 'expected_seconds' => [
161
+ 'label' => 'Expected completion time in seconds',
162
+ 'type' => 'number',
163
+ 'default' => 2100,
164
+ ],
165
+ 'time_min' => [
166
+ 'label' => 'Smallest time multiplier',
167
+ 'type' => 'text',
168
+ 'default' => '0.95',
169
+ ],
170
+ 'time_max' => [
171
+ 'label' => 'Largest time multiplier',
172
+ 'type' => 'text',
173
+ 'default' => '1.05',
174
+ ],
175
+ ],
176
+ ],
177
+ ];
178
+ }
179
+
180
+ public static function choices(): array {
181
+ return array_map( static fn( array $model ): string => $model['label'], self::models() );
182
+ }
183
+
184
+ public static function uses( string $model ): array {
185
+ return self::models()[ $model ]['uses'] ?? [];
186
+ }
187
+
188
+ public static function sanitize_params( string $model, array $raw ): array {
189
+ $params = [];
190
+
191
+ foreach ( self::models()[ $model ]['params'] ?? [] as $key => $param ) {
192
+ $value = $raw[ $key ] ?? $param['default'];
193
+ $params[ $key ] = 'select' === $param['type'] ? sanitize_key( (string) $value ) : Fields::number( $value );
194
+ }
195
+
196
+ return $params;
197
+ }
198
+
199
+ public static function render( array $field, array $value, string $name ): void {
200
+ $models = self::models();
201
+ $active = isset( $models[ $value['model'] ?? '' ] ) ? $value['model'] : (string) array_key_first( $models );
202
+ $saved = (array) ( $value['params'] ?? [] );
203
+ $script = [];
204
+
205
+ foreach ( $models as $key => $model ) {
206
+ $script[ $key ] = [
207
+ 'summary' => $model['summary'],
208
+ 'examples' => $model['examples'],
209
+ 'uses' => $model['uses'],
210
+ ];
211
+ }
212
+
213
+ printf( '<div class="platform-scoring" data-models="%s">', esc_attr( (string) wp_json_encode( $script ) ) );
214
+ printf( '<select class="platform-scoring__model" name="%s[model]">', esc_attr( $name ) );
215
+
216
+ foreach ( $models as $key => $model ) {
217
+ printf( '<option value="%s"%s>%s</option>', esc_attr( $key ), selected( $active, $key, false ), esc_html( $model['label'] ) );
218
+ }
219
+
220
+ echo '</select><p class="platform-scoring__summary description"></p><p class="platform-scoring__examples description"></p>';
221
+
222
+ foreach ( $models as $key => $model ) {
223
+ if ( empty( $model['params'] ) ) {
224
+ continue;
225
+ }
226
+
227
+ printf( '<div class="platform-scoring__params" data-model="%s"%s>', esc_attr( $key ), $key === $active ? '' : ' hidden' );
228
+
229
+ foreach ( $model['params'] as $param_key => $param ) {
230
+ $current = $key === $active && isset( $saved[ $param_key ] ) ? (string) $saved[ $param_key ] : (string) $param['default'];
231
+ $input = sprintf( '%s[params][%s][%s]', $name, $key, $param_key );
232
+
233
+ printf( '<label class="platform-scoring__param"><span>%s</span>', esc_html( $param['label'] ) );
234
+
235
+ if ( 'select' === $param['type'] ) {
236
+ printf( '<select name="%s">', esc_attr( $input ) );
237
+ foreach ( $param['options'] as $option => $label ) {
238
+ printf( '<option value="%s"%s>%s</option>', esc_attr( $option ), selected( $current, (string) $option, false ), esc_html( $label ) );
239
+ }
240
+ echo '</select>';
241
+ } else {
242
+ printf( '<input type="%s" step="any" name="%s" value="%s" />', 'number' === $param['type'] ? 'number' : 'text', esc_attr( $input ), esc_attr( $current ) );
243
+ }
244
+
245
+ echo '</label>';
246
+ }
247
+
248
+ echo '</div>';
249
+ }
250
+
251
+ echo '</div>';
252
+ }
253
+ }
@@ -0,0 +1,293 @@
1
+ <?php
2
+ /**
3
+ * Nested content builder: children of a content type (and their children)
4
+ *
5
+ * @package Platform
6
+ */
7
+
8
+ namespace Platform\Content;
9
+
10
+ use Platform\Config;
11
+ use Platform\Languages;
12
+
13
+ defined( 'ABSPATH' ) || exit;
14
+
15
+ final class Structure {
16
+
17
+ const ACTIONS = [ 'add', 'form', 'save', 'delete', 'sort' ];
18
+
19
+ public static function register(): void {
20
+ foreach ( self::ACTIONS as $action ) {
21
+ add_action( 'wp_ajax_platform_structure_' . $action, [ __CLASS__, 'ajax_' . $action ] );
22
+ }
23
+
24
+ add_action( 'before_delete_post', [ __CLASS__, 'delete_descendants' ] );
25
+ }
26
+
27
+ public static function children( int $parent_id, string $type ): array {
28
+ return get_posts(
29
+ [
30
+ 'post_type' => $type,
31
+ 'post_parent' => $parent_id,
32
+ 'post_status' => 'any',
33
+ 'posts_per_page' => -1,
34
+ 'orderby' => 'menu_order',
35
+ 'order' => 'ASC',
36
+ ]
37
+ );
38
+ }
39
+
40
+ public static function render( \WP_Post $post ): void {
41
+ $type = Config::type( $post->post_type );
42
+
43
+ if ( 'auto-draft' === $post->post_status ) {
44
+ printf( '<p class="description">Save this %s first, then add %s.</p>', esc_html( strtolower( $type['singular'] ) ), esc_html( strtolower( Config::type( $type['children'] )['label'] ) ) );
45
+ return;
46
+ }
47
+
48
+ printf( '<div class="platform-structure" data-uses="%s">', esc_attr( (string) wp_json_encode( self::uses( $post ) ) ) );
49
+ self::render_level( $post->ID, $type['children'] );
50
+ echo '</div>';
51
+
52
+ add_action( 'admin_footer', [ __CLASS__, 'render_modal' ] );
53
+ }
54
+
55
+ public static function render_modal(): void {
56
+ echo '<div class="platform-modal" hidden><div class="platform-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="platform-modal-title">';
57
+ echo '<div class="platform-modal__head"><h2 class="platform-modal__title" id="platform-modal-title"></h2><button type="button" class="platform-icon-button platform-modal__close" aria-label="Close"><span class="dashicons dashicons-no-alt"></span></button></div>';
58
+ echo '<form class="platform-modal__body" novalidate></form>';
59
+ echo '<div class="platform-modal__foot"><span class="platform-modal__status" role="status"></span><button type="button" class="button platform-modal__cancel">Cancel</button><button type="button" class="button button-primary platform-modal__save">Save</button></div>';
60
+ echo '</div></div>';
61
+ }
62
+
63
+ private static function render_level( int $parent_id, string $type_key ): void {
64
+ $type = Config::type( $type_key );
65
+
66
+ printf( '<div class="platform-structure__list" data-parent="%d" data-type="%s">', $parent_id, esc_attr( $type_key ) );
67
+
68
+ foreach ( self::children( $parent_id, $type_key ) as $child ) {
69
+ self::render_item( $child );
70
+ }
71
+
72
+ printf(
73
+ '</div><button type="button" class="button platform-structure__add" data-parent="%d" data-type="%s"><span class="dashicons dashicons-plus-alt2" aria-hidden="true"></span> Add %s</button>',
74
+ $parent_id,
75
+ esc_attr( $type_key ),
76
+ esc_html( strtolower( $type['singular'] ) )
77
+ );
78
+ }
79
+
80
+ private static function render_item( \WP_Post $post ): void {
81
+ $type = Config::type( $post->post_type );
82
+
83
+ printf( '<div class="platform-structure__item" data-id="%d">', (int) $post->ID );
84
+ self::render_bar( $post );
85
+
86
+ if ( '' !== $type['children'] ) {
87
+ echo '<div class="platform-structure__children">';
88
+ self::render_level( $post->ID, $type['children'] );
89
+ echo '</div>';
90
+ }
91
+
92
+ echo '</div>';
93
+ }
94
+
95
+ private static function render_bar( \WP_Post $post ): void {
96
+ $type = Config::type( $post->post_type );
97
+ $count = '' !== $type['children'] ? count( self::children( $post->ID, $type['children'] ) ) : null;
98
+
99
+ echo '<div class="platform-structure__bar">';
100
+ echo '<span class="platform-structure__handle dashicons dashicons-menu" aria-hidden="true"></span>';
101
+ printf( '<span class="platform-structure__title">%s</span>', esc_html( $post->post_title ?: '(no title)' ) );
102
+
103
+ if ( 'publish' !== $post->post_status ) {
104
+ printf( '<span class="platform-badge">%s</span>', esc_html( get_post_status_object( $post->post_status )->label ?? $post->post_status ) );
105
+ }
106
+
107
+ if ( null !== $count ) {
108
+ printf( '<span class="platform-structure__count">%d %s</span>', $count, esc_html( strtolower( 1 === $count ? Config::type( $type['children'] )['singular'] : Config::type( $type['children'] )['label'] ) ) );
109
+ }
110
+
111
+ printf( '<button type="button" class="platform-icon-button platform-structure__edit" aria-label="Edit %s"><span class="dashicons dashicons-edit"></span></button>', esc_attr( strtolower( $type['singular'] ) ) );
112
+ printf( '<button type="button" class="platform-icon-button platform-structure__delete" aria-label="Delete %s"><span class="dashicons dashicons-trash"></span></button>', esc_attr( strtolower( $type['singular'] ) ) );
113
+
114
+ if ( null !== $count ) {
115
+ echo '<button type="button" class="platform-icon-button platform-structure__toggle" aria-label="Show or hide"><span class="dashicons dashicons-arrow-down-alt2"></span></button>';
116
+ }
117
+
118
+ echo '</div>';
119
+ }
120
+
121
+ private static function render_form( \WP_Post $post ): void {
122
+ $type = Config::type( $post->post_type );
123
+
124
+ Fields::use_languages( Languages::for_type( $post->post_type ) );
125
+ Fields::use_folder( Config::root( $post->post_type )['folder'] ?? '' );
126
+
127
+ printf( '<input type="hidden" name="id" value="%d" />', (int) $post->ID );
128
+ echo '<div class="platform-fields">';
129
+ printf( '<div class="platform-field"><span class="platform-field__label">Title</span><input type="text" name="post[title]" value="%s" required /></div>', esc_attr( $post->post_title ) );
130
+
131
+ if ( in_array( 'editor', $type['supports'], true ) ) {
132
+ printf( '<div class="platform-field"><span class="platform-field__label">Content</span><textarea name="post[content]" rows="5">%s</textarea></div>', esc_textarea( $post->post_content ) );
133
+ }
134
+
135
+ if ( in_array( 'excerpt', $type['supports'], true ) ) {
136
+ printf( '<div class="platform-field"><span class="platform-field__label">Excerpt</span><textarea name="post[excerpt]" rows="2">%s</textarea></div>', esc_textarea( $post->post_excerpt ) );
137
+ }
138
+
139
+ if ( in_array( 'thumbnail', $type['supports'], true ) ) {
140
+ Fields::render( [ [ 'key' => 'thumbnail', 'type' => 'image', 'label' => 'Thumbnail' ] ], [ 'thumbnail' => get_post_thumbnail_id( $post ) ], 'post' );
141
+ }
142
+
143
+ printf(
144
+ '<div class="platform-field platform-field--checkbox"><label><input type="checkbox" name="post[draft]" value="1"%s /> Draft</label></div>',
145
+ checked( 'draft', $post->post_status, false )
146
+ );
147
+
148
+ Fields::render( $type['fields'], Fields::values( 'post', $post->ID, $type['fields'] ), 'fields' );
149
+ echo '</div>';
150
+ }
151
+
152
+ private static function uses( \WP_Post $post ): array {
153
+ foreach ( Config::type( $post->post_type )['fields'] as $field ) {
154
+ if ( 'scoring' === $field['type'] ) {
155
+ return Scoring::uses( (string) get_post_meta( $post->ID, $field['key'], true ) );
156
+ }
157
+ }
158
+
159
+ return [];
160
+ }
161
+
162
+ /* ---------------------------------------------------------------- ajax */
163
+
164
+ public static function ajax_add(): void {
165
+ self::verify();
166
+
167
+ $parent_id = absint( $_POST['parent'] ?? 0 );
168
+ $type = Config::type( sanitize_key( wp_unslash( $_POST['type'] ?? '' ) ) );
169
+
170
+ if ( ! $type || ! $parent_id || get_post_type( $parent_id ) !== $type['parent'] || ! current_user_can( 'edit_post', $parent_id ) ) {
171
+ wp_send_json_error( [ 'message' => 'Not allowed.' ], 403 );
172
+ }
173
+
174
+ $id = wp_insert_post(
175
+ [
176
+ 'post_type' => $type['key'],
177
+ 'post_title' => sprintf( 'New %s', strtolower( $type['singular'] ) ),
178
+ 'post_status' => 'publish',
179
+ 'post_parent' => $parent_id,
180
+ 'menu_order' => count( self::children( $parent_id, $type['key'] ) ) + 1,
181
+ ],
182
+ true
183
+ );
184
+
185
+ if ( is_wp_error( $id ) ) {
186
+ wp_send_json_error( [ 'message' => $id->get_error_message() ], 500 );
187
+ }
188
+
189
+ ob_start();
190
+ self::render_item( get_post( $id ) );
191
+ wp_send_json_success( [ 'id' => $id, 'html' => ob_get_clean() ] );
192
+ }
193
+
194
+ public static function ajax_form(): void {
195
+ $post = self::item();
196
+
197
+ ob_start();
198
+ self::render_form( $post );
199
+ wp_send_json_success(
200
+ [
201
+ 'title' => sprintf( 'Edit %s', strtolower( Config::type( $post->post_type )['singular'] ) ),
202
+ 'html' => ob_get_clean(),
203
+ ]
204
+ );
205
+ }
206
+
207
+ public static function ajax_save(): void {
208
+ $post = self::item();
209
+ $type = Config::type( $post->post_type );
210
+ $data = (array) wp_unslash( $_POST['post'] ?? [] );
211
+
212
+ $update = [
213
+ 'ID' => $post->ID,
214
+ 'post_title' => sanitize_text_field( (string) ( $data['title'] ?? $post->post_title ) ),
215
+ 'post_status' => empty( $data['draft'] ) ? 'publish' : 'draft',
216
+ ];
217
+
218
+ if ( isset( $data['content'] ) ) {
219
+ $update['post_content'] = wp_kses_post( (string) $data['content'] );
220
+ }
221
+
222
+ if ( isset( $data['excerpt'] ) ) {
223
+ $update['post_excerpt'] = sanitize_textarea_field( (string) $data['excerpt'] );
224
+ }
225
+
226
+ $result = wp_update_post( $update, true );
227
+
228
+ if ( is_wp_error( $result ) ) {
229
+ wp_send_json_error( [ 'message' => $result->get_error_message() ], 500 );
230
+ }
231
+
232
+ if ( array_key_exists( 'thumbnail', $data ) ) {
233
+ absint( $data['thumbnail'] ) ? set_post_thumbnail( $post->ID, absint( $data['thumbnail'] ) ) : delete_post_thumbnail( $post->ID );
234
+ }
235
+
236
+ Fields::use_languages( Languages::for_type( $post->post_type ) );
237
+ Fields::save( 'post', $post->ID, $type['fields'], Fields::input() );
238
+
239
+ ob_start();
240
+ self::render_bar( get_post( $post->ID ) );
241
+ wp_send_json_success( [ 'bar' => ob_get_clean() ] );
242
+ }
243
+
244
+ public static function ajax_delete(): void {
245
+ $post = self::item( 'delete_post' );
246
+
247
+ wp_delete_post( $post->ID, true );
248
+ wp_send_json_success();
249
+ }
250
+
251
+ public static function ajax_sort(): void {
252
+ self::verify();
253
+
254
+ foreach ( array_values( array_map( 'absint', (array) ( $_POST['order'] ?? [] ) ) ) as $position => $id ) {
255
+ if ( $id && Config::type( (string) get_post_type( $id ) ) && current_user_can( 'edit_post', $id ) ) {
256
+ wp_update_post( [ 'ID' => $id, 'menu_order' => $position + 1 ] );
257
+ }
258
+ }
259
+
260
+ wp_send_json_success();
261
+ }
262
+
263
+ public static function delete_descendants( int $post_id ): void {
264
+ $type = Config::type( (string) get_post_type( $post_id ) );
265
+
266
+ if ( ! $type || '' === $type['children'] ) {
267
+ return;
268
+ }
269
+
270
+ foreach ( self::children( $post_id, $type['children'] ) as $child ) {
271
+ wp_delete_post( $child->ID, true );
272
+ }
273
+ }
274
+
275
+ private static function item( string $capability = 'edit_post' ): \WP_Post {
276
+ self::verify();
277
+
278
+ $post = get_post( absint( $_POST['id'] ?? 0 ) );
279
+ $type = $post ? Config::type( $post->post_type ) : [];
280
+
281
+ if ( ! $post || ! $type || '' === $type['parent'] || ! current_user_can( $capability, $post->ID ) ) {
282
+ wp_send_json_error( [ 'message' => 'Not allowed.' ], 403 );
283
+ }
284
+
285
+ return $post;
286
+ }
287
+
288
+ private static function verify(): void {
289
+ if ( ! check_ajax_referer( 'platform_admin', 'nonce', false ) ) {
290
+ wp_send_json_error( [ 'message' => 'Your session expired. Reload the page.' ], 403 );
291
+ }
292
+ }
293
+ }