@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,784 @@
1
+ <?php
2
+ namespace Platform\Editors;
3
+
4
+ use Platform\Languages;
5
+ use Platform\Platform;
6
+
7
+ defined( 'ABSPATH' ) || exit;
8
+
9
+ final class Course {
10
+
11
+ public static function languages() {
12
+ return self::languages_for( 'courses' );
13
+ }
14
+
15
+ public static function languages_for( string $type ) {
16
+ $languages = [];
17
+ foreach ( Languages::for_type( $type ) as $code => $name ) {
18
+ $languages[] = [ 'code' => $code, 'name' => $name ];
19
+ }
20
+ return $languages;
21
+ }
22
+
23
+ public static function register() {
24
+ add_action( 'add_meta_boxes', [ __CLASS__, 'register_meta_boxes' ] );
25
+ add_action( 'admin_init', function () {
26
+ remove_post_type_support( 'courses', 'editor' );
27
+ } );
28
+ add_action( 'save_post_courses', [ __CLASS__, 'save_course_meta' ], 10, 2 );
29
+ add_action( 'wp_ajax_course_add_topic', [ __CLASS__, 'ajax_add_topic' ] );
30
+ add_action( 'wp_ajax_course_update_topic', [ __CLASS__, 'ajax_update_topic' ] );
31
+ add_action( 'wp_ajax_course_delete_topic', [ __CLASS__, 'ajax_delete_topic' ] );
32
+ add_action( 'wp_ajax_course_sort_topics', [ __CLASS__, 'ajax_sort_topics' ] );
33
+ add_action( 'wp_ajax_course_add_lesson', [ __CLASS__, 'ajax_add_lesson' ] );
34
+ add_action( 'wp_ajax_course_get_lesson', [ __CLASS__, 'ajax_get_lesson' ] );
35
+ add_action( 'wp_ajax_course_update_lesson', [ __CLASS__, 'ajax_update_lesson' ] );
36
+ add_action( 'wp_ajax_course_delete_lesson', [ __CLASS__, 'ajax_delete_lesson' ] );
37
+ add_action( 'wp_ajax_course_sort_lessons', [ __CLASS__, 'ajax_sort_lessons' ] );
38
+ add_filter( 'parent_file', [ __CLASS__, 'menu_parent_file' ] );
39
+ add_filter( 'submenu_file', [ __CLASS__, 'menu_submenu_file' ] );
40
+ add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue' ] );
41
+ }
42
+
43
+ public static function get_course_topics( $course_id ) {
44
+ return get_posts( [
45
+ 'post_type' => 'topics',
46
+ 'post_parent' => $course_id,
47
+ 'posts_per_page' => -1,
48
+ 'orderby' => 'menu_order',
49
+ 'order' => 'ASC',
50
+ 'post_status' => 'any',
51
+ ] );
52
+ }
53
+
54
+ public static function get_topic_lessons( $topic_id ) {
55
+ return get_posts( [
56
+ 'post_type' => 'lessons',
57
+ 'post_parent' => $topic_id,
58
+ 'posts_per_page' => -1,
59
+ 'orderby' => 'menu_order',
60
+ 'order' => 'ASC',
61
+ 'post_status' => 'any',
62
+ ] );
63
+ }
64
+
65
+ public static function default_video_meta() {
66
+ return [
67
+ 'source_type' => 'youtube',
68
+ 'source' => '',
69
+ 'runtime' => [
70
+ 'hours' => '00',
71
+ 'minutes' => '00',
72
+ 'seconds' => '00',
73
+ ],
74
+ ];
75
+ }
76
+
77
+ public static function get_video_meta( $post_id, $meta_key = '_video' ) {
78
+ $raw = get_post_meta( $post_id, $meta_key, true );
79
+ $data = $raw ? maybe_unserialize( $raw ) : [];
80
+ return wp_parse_args( $data, self::default_video_meta() );
81
+ }
82
+
83
+ public static function sanitize_video_meta( $input ) {
84
+ $source_types = [ 'youtube', 'vimeo', 'html5', 'external_url', 'shortcode' ];
85
+ $type = in_array( $input['source_type'] ?? '', $source_types, true )
86
+ ? $input['source_type']
87
+ : 'youtube';
88
+
89
+ if ( $type === 'html5' ) {
90
+ $source = wp_kses_post( $input['source'] ?? '' );
91
+ } elseif ( $type === 'shortcode' ) {
92
+ $source = sanitize_text_field( $input['source'] ?? '' );
93
+ } else {
94
+ $source = esc_url_raw( $input['source'] ?? '' );
95
+ }
96
+
97
+ $data = [
98
+ 'source_type' => $type,
99
+ 'source' => $source,
100
+ 'runtime' => [
101
+ 'hours' => str_pad( absint( $input['runtime']['hours'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
102
+ 'minutes' => str_pad( absint( $input['runtime']['minutes'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
103
+ 'seconds' => str_pad( absint( $input['runtime']['seconds'] ?? 0 ), 2, '0', STR_PAD_LEFT ),
104
+ ],
105
+ ];
106
+ return serialize( $data );
107
+ }
108
+
109
+ public static function difficulty_levels() {
110
+ return [
111
+ 'beginner' => __( 'Beginner' ),
112
+ 'intermediate' => __( 'Intermediate' ),
113
+ 'advanced' => __( 'Advanced' ),
114
+ ];
115
+ }
116
+
117
+ public static function register_meta_boxes() {
118
+ add_meta_box(
119
+ 'course_builder',
120
+ __( 'Course Builder' ),
121
+ [ __CLASS__, 'render_course_builder' ],
122
+ 'courses',
123
+ 'normal',
124
+ 'high'
125
+ );
126
+ }
127
+
128
+ public static function save_course_meta( $post_id, $post ) {
129
+ if ( ! isset( $_POST['course_course_nonce'] ) ) return;
130
+ if ( ! wp_verify_nonce( $_POST['course_course_nonce'], 'course_save_course' ) ) return;
131
+ if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
132
+ if ( ! current_user_can( 'edit_post', $post_id ) ) return;
133
+
134
+ $levels = array_keys( self::difficulty_levels() );
135
+ $level = sanitize_text_field( $_POST['course_level'] ?? '' );
136
+ update_post_meta( $post_id, 'course_level', in_array( $level, $levels, true ) ? $level : 'beginner' );
137
+
138
+ $languages = self::languages();
139
+ foreach ( $languages as $lang ) {
140
+ $code = $lang['code'];
141
+ $field = 'course_video_' . $code;
142
+ $meta_key = 'video_' . $code;
143
+ if ( isset( $_POST[ $field ] ) ) {
144
+ update_post_meta( $post_id, $meta_key, self::sanitize_video_meta( $_POST[ $field ] ) );
145
+ }
146
+ }
147
+ }
148
+
149
+ public static function render_course_builder( $post ) {
150
+ wp_nonce_field( 'course_save_course', 'course_course_nonce' );
151
+
152
+ $level = get_post_meta( $post->ID, 'course_level', true ) ?: 'beginner';
153
+ $topics = self::get_course_topics( $post->ID );
154
+ ?>
155
+ <div class="course-builder" data-course-id="<?php echo esc_attr( $post->ID ); ?>">
156
+
157
+ <ul class="course-tabs">
158
+ <li class="course-tab active" data-tab="basics">
159
+ <span class="dashicons dashicons-edit"></span>
160
+ <?php esc_html_e( 'Basics' ); ?>
161
+ </li>
162
+ <li class="course-tab" data-tab="curriculum">
163
+ <span class="dashicons dashicons-list-view"></span>
164
+ <?php esc_html_e( 'Curriculum' ); ?>
165
+ </li>
166
+ </ul>
167
+
168
+ <div class="course-tab-panel active" id="course-tab-basics">
169
+
170
+ <div class="course-field-group">
171
+ <label class="course-label"><?php esc_html_e( 'Course Description' ); ?></label>
172
+ <?php
173
+ wp_editor( $post->post_content, 'post_content', [
174
+ 'textarea_name' => 'post_content',
175
+ 'media_buttons' => true,
176
+ 'textarea_rows' => 10,
177
+ ] );
178
+ ?>
179
+ </div>
180
+
181
+ <div class="course-field-row">
182
+ <div class="course-field-group">
183
+ <label class="course-label" for="course_level">
184
+ <?php esc_html_e( 'Difficulty Level' ); ?>
185
+ </label>
186
+ <select name="course_level" id="course_level" class="course-select">
187
+ <?php foreach ( self::difficulty_levels() as $slug => $label ) : ?>
188
+ <option value="<?php echo esc_attr( $slug ); ?>"
189
+ <?php selected( $level, $slug ); ?>>
190
+ <?php echo esc_html( $label ); ?>
191
+ </option>
192
+ <?php endforeach; ?>
193
+ </select>
194
+ </div>
195
+ </div>
196
+
197
+ <?php self::render_multilang_video( $post->ID, 'course_video', 'video' ); ?>
198
+
199
+ </div>
200
+
201
+ <div class="course-tab-panel" id="course-tab-curriculum">
202
+ <div class="course-curriculum-wrap">
203
+
204
+ <div class="course-topics-list" id="course-topics-list">
205
+ <?php foreach ( $topics as $topic ) : ?>
206
+ <?php self::render_topic_row( $topic, $post->ID ); ?>
207
+ <?php endforeach; ?>
208
+ </div>
209
+
210
+ <button type="button" class="button button-primary course-add-topic" data-course-id="<?php echo esc_attr( $post->ID ); ?>">
211
+ <span class="dashicons dashicons-plus-alt2"></span>
212
+ <?php esc_html_e( 'Add Topic' ); ?>
213
+ </button>
214
+
215
+ </div>
216
+ </div>
217
+
218
+ </div>
219
+ <?php
220
+ }
221
+
222
+ public static function render_topic_row( $topic, $course_id ) {
223
+ $lessons = self::get_topic_lessons( $topic->ID );
224
+ ?>
225
+ <div class="course-topic-item" data-topic-id="<?php echo esc_attr( $topic->ID ); ?>">
226
+ <div class="course-topic-header">
227
+ <span class="course-drag-handle dashicons dashicons-move"></span>
228
+ <span class="course-topic-title"><?php echo esc_html( $topic->post_title ); ?></span>
229
+
230
+ <div class="course-topic-actions">
231
+ <button type="button" class="course-btn-icon course-edit-topic"
232
+ data-topic-id="<?php echo esc_attr( $topic->ID ); ?>"
233
+ title="<?php esc_attr_e( 'Edit Topic' ); ?>">
234
+ <span class="dashicons dashicons-edit"></span>
235
+ </button>
236
+ <button type="button" class="course-btn-icon course-delete-topic"
237
+ data-topic-id="<?php echo esc_attr( $topic->ID ); ?>"
238
+ data-course-id="<?php echo esc_attr( $course_id ); ?>"
239
+ title="<?php esc_attr_e( 'Delete Topic' ); ?>">
240
+ <span class="dashicons dashicons-trash"></span>
241
+ </button>
242
+ <span class="course-toggle dashicons dashicons-arrow-down-alt2"></span>
243
+ </div>
244
+ </div>
245
+
246
+ <div class="course-topic-edit-form" style="display:none;">
247
+ <input type="text" class="course-input course-topic-name-input"
248
+ placeholder="<?php esc_attr_e( 'Topic Title' ); ?>"
249
+ value="<?php echo esc_attr( $topic->post_title ); ?>" />
250
+ <textarea class="course-input course-topic-content-input"
251
+ name="topic_content[<?php echo esc_attr( $topic->ID ); ?>]"
252
+ rows="5"
253
+ placeholder="<?php esc_attr_e( 'Topic Description' ); ?>"><?php echo esc_textarea( $topic->post_content ); ?></textarea>
254
+ <button type="button" class="button course-update-topic"
255
+ data-topic-id="<?php echo esc_attr( $topic->ID ); ?>">
256
+ <?php esc_html_e( 'Update Topic' ); ?>
257
+ </button>
258
+ </div>
259
+
260
+ <div class="course-lessons-list" data-topic-id="<?php echo esc_attr( $topic->ID ); ?>">
261
+ <?php foreach ( $lessons as $lesson ) : ?>
262
+ <?php self::render_lesson_row( $lesson, $topic->ID ); ?>
263
+ <?php endforeach; ?>
264
+ </div>
265
+
266
+ <div class="course-topic-footer">
267
+ <button type="button" class="button course-add-lesson"
268
+ data-topic-id="<?php echo esc_attr( $topic->ID ); ?>"
269
+ data-course-id="<?php echo esc_attr( $course_id ); ?>">
270
+ <span class="dashicons dashicons-plus-alt2"></span>
271
+ <?php esc_html_e( 'Add Lesson' ); ?>
272
+ </button>
273
+ </div>
274
+
275
+ </div>
276
+ <?php
277
+ }
278
+
279
+ public static function render_lesson_row( $lesson, $topic_id ) {
280
+ $is_preview = (int) get_post_meta( $lesson->ID, '_is_preview', true );
281
+ ?>
282
+ <div class="course-lesson-item" data-lesson-id="<?php echo esc_attr( $lesson->ID ); ?>">
283
+ <span class="course-drag-handle dashicons dashicons-move"></span>
284
+ <span class="dashicons dashicons-media-document"></span>
285
+ <span class="course-lesson-title"><?php echo esc_html( $lesson->post_title ); ?></span>
286
+ <?php if ( $is_preview ) : ?>
287
+ <span class="course-badge course-badge-preview"><?php esc_html_e( 'Preview' ); ?></span>
288
+ <?php endif; ?>
289
+
290
+ <div class="course-lesson-actions">
291
+ <button type="button" class="course-btn-icon course-edit-lesson"
292
+ data-lesson-id="<?php echo esc_attr( $lesson->ID ); ?>"
293
+ data-topic-id="<?php echo esc_attr( $topic_id ); ?>"
294
+ title="<?php esc_attr_e( 'Edit Lesson' ); ?>">
295
+ <span class="dashicons dashicons-edit"></span>
296
+ </button>
297
+ <button type="button" class="course-btn-icon course-delete-lesson"
298
+ data-lesson-id="<?php echo esc_attr( $lesson->ID ); ?>"
299
+ data-topic-id="<?php echo esc_attr( $topic_id ); ?>"
300
+ title="<?php esc_attr_e( 'Delete Lesson' ); ?>">
301
+ <span class="dashicons dashicons-trash"></span>
302
+ </button>
303
+ </div>
304
+ </div>
305
+ <?php
306
+ }
307
+
308
+ public static function render_multilang_video( $post_id, $field_prefix, $meta_prefix ) {
309
+ $languages = self::languages();
310
+
311
+ if ( empty( $languages ) ) {
312
+ echo '<p class="description">' . esc_html__( 'No languages configured. Add languages under Courses → Languages.' ) . '</p>';
313
+ return;
314
+ }
315
+
316
+ $first = true;
317
+ ?>
318
+ <div class="course-field-group">
319
+ <label class="course-label"><?php esc_html_e( 'Intro / Promo Video' ); ?></label>
320
+
321
+ <div class="course-lang-video-tabs">
322
+ <?php foreach ( $languages as $lang ) : ?>
323
+ <button type="button"
324
+ class="course-lang-video-tab <?php echo $first ? 'active' : ''; ?>"
325
+ data-lang="<?php echo esc_attr( $lang['code'] ); ?>">
326
+ <?php echo esc_html( $lang['name'] ); ?>
327
+ </button>
328
+ <?php $first = false; ?>
329
+ <?php endforeach; ?>
330
+ </div>
331
+
332
+ <?php $first = true; foreach ( $languages as $lang ) :
333
+ $code = $lang['code'];
334
+ $field = $field_prefix . '_' . $code;
335
+ $meta_key = $meta_prefix . '_' . $code;
336
+ $video = self::get_video_meta( $post_id, $meta_key );
337
+ ?>
338
+ <div class="course-lang-video-panel course-video-box <?php echo $first ? 'active' : ''; ?>"
339
+ data-lang="<?php echo esc_attr( $code ); ?>">
340
+ <?php self::render_video_fields( $field, $video ); ?>
341
+ </div>
342
+ <?php $first = false; endforeach; ?>
343
+ </div>
344
+ <?php
345
+ }
346
+
347
+ public static function render_video_fields( $field_name, $video, $source_types = [] ) {
348
+ $all_types = [
349
+ 'youtube' => __( 'YouTube' ),
350
+ 'vimeo' => __( 'Vimeo' ),
351
+ 'external_url' => __( 'External URL' ),
352
+ 'html5' => __( 'Embed' ),
353
+ 'shortcode' => __( 'Shortcode' ),
354
+ ];
355
+ $types = $source_types ? array_intersect_key( $all_types, array_flip( $source_types ) ) : $all_types;
356
+ $active_type = $video['source_type'] ?: 'youtube';
357
+ $source = $video['source'] ?? '';
358
+ $uid = 'vf_' . md5( $field_name );
359
+ ?>
360
+ <div class="course-video-fields" data-field="<?php echo esc_attr( $field_name ); ?>">
361
+
362
+ <div class="course-video-tabs">
363
+ <?php foreach ( $types as $slug => $label ) : ?>
364
+ <button type="button"
365
+ class="course-video-tab <?php echo $active_type === $slug ? 'active' : ''; ?>"
366
+ data-type="<?php echo esc_attr( $slug ); ?>">
367
+ <?php echo esc_html( $label ); ?>
368
+ </button>
369
+ <?php endforeach; ?>
370
+ </div>
371
+
372
+ <input type="hidden"
373
+ name="<?php echo esc_attr( $field_name ); ?>[source_type]"
374
+ class="course-video-source-type-input"
375
+ value="<?php echo esc_attr( $active_type ); ?>" />
376
+
377
+ <div class="course-video-input-wrap <?php echo $active_type === 'youtube' ? 'active' : ''; ?>" data-type="youtube">
378
+ <input type="url"
379
+ class="course-input course-video-source-field"
380
+ placeholder="https://www.youtube.com/watch?v=..."
381
+ value="<?php echo $active_type === 'youtube' ? esc_attr( $source ) : ''; ?>"
382
+ data-type="youtube" />
383
+ <p class="course-field-hint"><?php esc_html_e( 'Paste a YouTube video URL. Short URLs (youtu.be) are also accepted.' ); ?></p>
384
+ <span class="course-video-error" style="display:none;"></span>
385
+ </div>
386
+
387
+ <div class="course-video-input-wrap <?php echo $active_type === 'vimeo' ? 'active' : ''; ?>" data-type="vimeo">
388
+ <input type="url"
389
+ class="course-input course-video-source-field"
390
+ placeholder="https://vimeo.com/..."
391
+ value="<?php echo $active_type === 'vimeo' ? esc_attr( $source ) : ''; ?>"
392
+ data-type="vimeo" />
393
+ <p class="course-field-hint"><?php esc_html_e( 'Paste a Vimeo video URL.' ); ?></p>
394
+ <span class="course-video-error" style="display:none;"></span>
395
+ </div>
396
+
397
+ <div class="course-video-input-wrap <?php echo $active_type === 'external_url' ? 'active' : ''; ?>" data-type="external_url">
398
+ <input type="url"
399
+ class="course-input course-video-source-field"
400
+ placeholder="https://example.com/video.mp4"
401
+ value="<?php echo $active_type === 'external_url' ? esc_attr( $source ) : ''; ?>"
402
+ data-type="external_url" />
403
+ <p class="course-field-hint"><?php esc_html_e( 'Direct URL to a video file (.mp4, .webm, .ogg).' ); ?></p>
404
+ <span class="course-video-error" style="display:none;"></span>
405
+ </div>
406
+
407
+ <div class="course-video-input-wrap <?php echo $active_type === 'html5' ? 'active' : ''; ?>" data-type="html5">
408
+ <textarea class="course-input course-video-source-field course-embed-textarea"
409
+ placeholder="<iframe ...></iframe> or <video ...></video>"
410
+ data-type="html5"
411
+ rows="4"><?php echo $active_type === 'html5' ? esc_textarea( $source ) : ''; ?></textarea>
412
+ <p class="course-field-hint"><?php esc_html_e( 'Paste any embed code — iframe, video tag, etc.' ); ?></p>
413
+ </div>
414
+
415
+ <?php if ( isset( $types['shortcode'] ) ) : ?>
416
+
417
+ <div class="course-video-input-wrap <?php echo $active_type === 'shortcode' ? 'active' : ''; ?>" data-type="shortcode">
418
+ <input type="text"
419
+ class="course-input course-video-source-field"
420
+ placeholder="[video_shortcode id=&quot;123&quot;]"
421
+ value="<?php echo $active_type === 'shortcode' ? esc_attr( $source ) : ''; ?>"
422
+ data-type="shortcode" />
423
+ <p class="course-field-hint"><?php esc_html_e( 'Paste a WordPress shortcode that outputs a video.' ); ?></p>
424
+ </div>
425
+ <?php endif; ?>
426
+
427
+ <input type="hidden"
428
+ name="<?php echo esc_attr( $field_name ); ?>[source]"
429
+ class="course-video-source-hidden"
430
+ value="<?php echo esc_attr( $source ); ?>" />
431
+
432
+ <div class="course-video-preview-wrap" style="<?php echo $source ? '' : 'display:none;'; ?>">
433
+ <div class="course-video-preview-inner">
434
+ <?php if ( $source ) self::render_video_preview( $active_type, $source ); ?>
435
+ </div>
436
+ </div>
437
+
438
+ <input type="hidden" name="<?php echo esc_attr( $field_name ); ?>[runtime][hours]" class="course-video-runtime-hours" value="<?php echo esc_attr( $video['runtime']['hours'] ?? '00' ); ?>" />
439
+ <input type="hidden" name="<?php echo esc_attr( $field_name ); ?>[runtime][minutes]" class="course-video-runtime-minutes" value="<?php echo esc_attr( $video['runtime']['minutes'] ?? '00' ); ?>" />
440
+ <input type="hidden" name="<?php echo esc_attr( $field_name ); ?>[runtime][seconds]" class="course-video-runtime-seconds" value="<?php echo esc_attr( $video['runtime']['seconds'] ?? '00' ); ?>" />
441
+
442
+ <?php
443
+ $h = $video['runtime']['hours'] ?? '00';
444
+ $m = $video['runtime']['minutes'] ?? '00';
445
+ $s = $video['runtime']['seconds'] ?? '00';
446
+ $has_duration = ( $h !== '00' || $m !== '00' || $s !== '00' );
447
+ ?>
448
+ <div class="course-video-duration-display" <?php echo $has_duration ? '' : 'style="display:none;"'; ?>>
449
+ <span class="dashicons dashicons-clock"></span>
450
+ <span class="course-duration-text"><?php echo esc_html( "{$h}:{$m}:{$s}" ); ?></span>
451
+ </div>
452
+
453
+ </div>
454
+ <?php
455
+ }
456
+
457
+ public static function render_video_preview( $type, $source ) {
458
+ switch ( $type ) {
459
+ case 'youtube':
460
+ $id = self::extract_youtube_id( $source );
461
+ if ( $id ) {
462
+ echo '<iframe src="https://www.youtube.com/embed/' . esc_attr( $id ) . '" frameborder="0" allowfullscreen></iframe>';
463
+ }
464
+ break;
465
+ case 'vimeo':
466
+ $id = self::extract_vimeo_id( $source );
467
+ if ( $id ) {
468
+ echo '<iframe src="https://player.vimeo.com/video/' . esc_attr( $id ) . '" frameborder="0" allowfullscreen></iframe>';
469
+ }
470
+ break;
471
+ case 'external_url':
472
+ echo '<video controls src="' . esc_url( $source ) . '"></video>';
473
+ break;
474
+ case 'html5':
475
+ echo $source;
476
+ break;
477
+ case 'shortcode':
478
+ echo '<p class="course-shortcode-note">[shortcode preview not available in editor]</p>';
479
+ break;
480
+ }
481
+ }
482
+
483
+ public static function extract_youtube_id( $url ) {
484
+ preg_match(
485
+ '/(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/',
486
+ $url,
487
+ $m
488
+ );
489
+ return $m[1] ?? '';
490
+ }
491
+
492
+ public static function extract_vimeo_id( $url ) {
493
+ preg_match( '/vimeo\.com\/(?:video\/)?(\d+)/', $url, $m );
494
+ return $m[1] ?? '';
495
+ }
496
+
497
+ public static function ajax_add_topic() {
498
+ self::verify_ajax_nonce();
499
+
500
+ $course_id = absint( $_POST['course_id'] ?? 0 );
501
+ if ( ! $course_id || ! current_user_can( 'edit_post', $course_id ) ) {
502
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
503
+ }
504
+
505
+ $existing = self::get_course_topics( $course_id );
506
+ $order = count( $existing ) + 1;
507
+
508
+ $topic_id = wp_insert_post( [
509
+ 'post_type' => 'topics',
510
+ 'post_title' => sanitize_text_field( $_POST['title'] ?? __( 'New Topic' ) ),
511
+ 'post_content' => wp_kses_post( $_POST['content'] ?? '' ),
512
+ 'post_status' => 'publish',
513
+ 'post_parent' => $course_id,
514
+ 'menu_order' => $order,
515
+ ] );
516
+
517
+ if ( is_wp_error( $topic_id ) ) {
518
+ wp_send_json_error( [ 'message' => $topic_id->get_error_message() ] );
519
+ }
520
+
521
+ $topic = get_post( $topic_id );
522
+ ob_start();
523
+ self::render_topic_row( $topic, $course_id );
524
+ $html = ob_get_clean();
525
+
526
+ wp_send_json_success( [ 'html' => $html, 'topic_id' => $topic_id ] );
527
+ }
528
+
529
+ public static function ajax_update_topic() {
530
+ self::verify_ajax_nonce();
531
+
532
+ $topic_id = absint( $_POST['topic_id'] ?? 0 );
533
+ if ( ! $topic_id || ! current_user_can( 'edit_post', $topic_id ) ) {
534
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
535
+ }
536
+
537
+ $result = wp_update_post( [
538
+ 'ID' => $topic_id,
539
+ 'post_title' => sanitize_text_field( $_POST['title'] ?? '' ),
540
+ 'post_content' => wp_kses_post( $_POST['content'] ?? '' ),
541
+ ] );
542
+
543
+ if ( is_wp_error( $result ) ) {
544
+ wp_send_json_error( [ 'message' => $result->get_error_message() ] );
545
+ }
546
+
547
+ wp_send_json_success( [
548
+ 'topic_id' => $topic_id,
549
+ 'title' => get_post( $topic_id )->post_title,
550
+ ] );
551
+ }
552
+
553
+ public static function ajax_delete_topic() {
554
+ self::verify_ajax_nonce();
555
+
556
+ $topic_id = absint( $_POST['topic_id'] ?? 0 );
557
+ $course_id = absint( $_POST['course_id'] ?? 0 );
558
+
559
+ if ( ! $topic_id || ! current_user_can( 'edit_post', $topic_id ) ) {
560
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
561
+ }
562
+
563
+ $lessons = self::get_topic_lessons( $topic_id );
564
+ foreach ( $lessons as $lesson ) {
565
+ wp_delete_post( $lesson->ID, true );
566
+ }
567
+
568
+ wp_delete_post( $topic_id, true );
569
+
570
+ wp_send_json_success( [ 'topic_id' => $topic_id ] );
571
+ }
572
+
573
+ public static function ajax_sort_topics() {
574
+ self::verify_ajax_nonce();
575
+
576
+ $order = array_map( 'absint', $_POST['order'] ?? [] );
577
+ foreach ( $order as $menu_order => $topic_id ) {
578
+ if ( current_user_can( 'edit_post', $topic_id ) ) {
579
+ wp_update_post( [
580
+ 'ID' => $topic_id,
581
+ 'menu_order' => $menu_order,
582
+ ] );
583
+ }
584
+ }
585
+
586
+ wp_send_json_success();
587
+ }
588
+
589
+ public static function ajax_add_lesson() {
590
+ self::verify_ajax_nonce();
591
+
592
+ $topic_id = absint( $_POST['topic_id'] ?? 0 );
593
+ $course_id = absint( $_POST['course_id'] ?? 0 );
594
+
595
+ if ( ! $topic_id || ! current_user_can( 'edit_post', $topic_id ) ) {
596
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
597
+ }
598
+
599
+ $existing = self::get_topic_lessons( $topic_id );
600
+ $order = count( $existing ) + 1;
601
+
602
+ $lesson_id = wp_insert_post( [
603
+ 'post_type' => 'lessons',
604
+ 'post_title' => sanitize_text_field( $_POST['title'] ?? __( 'New Lesson' ) ),
605
+ 'post_status' => 'publish',
606
+ 'post_parent' => $topic_id,
607
+ 'menu_order' => $order,
608
+ ] );
609
+
610
+ if ( is_wp_error( $lesson_id ) ) {
611
+ wp_send_json_error( [ 'message' => $lesson_id->get_error_message() ] );
612
+ }
613
+
614
+ $lesson = get_post( $lesson_id );
615
+ ob_start();
616
+ self::render_lesson_row( $lesson, $topic_id );
617
+ $html = ob_get_clean();
618
+
619
+ wp_send_json_success( [ 'html' => $html, 'lesson_id' => $lesson_id ] );
620
+ }
621
+
622
+ public static function ajax_get_lesson() {
623
+ self::verify_ajax_nonce();
624
+
625
+ $lesson_id = absint( $_POST['lesson_id'] ?? 0 );
626
+ if ( ! $lesson_id || ! current_user_can( 'edit_post', $lesson_id ) ) {
627
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
628
+ }
629
+
630
+ $lesson = get_post( $lesson_id );
631
+ $is_preview = (int) get_post_meta( $lesson_id, '_is_preview', true );
632
+ $attach_ids = maybe_unserialize( get_post_meta( $lesson_id, '_tutor_attachments', true ) ) ?: [];
633
+ $thumbnail = get_post_thumbnail_id( $lesson_id );
634
+
635
+ $languages = self::languages();
636
+ $videos = [];
637
+ foreach ( $languages as $lang ) {
638
+ $videos[ $lang['code'] ] = self::get_video_meta( $lesson_id, '_video_' . $lang['code'] );
639
+ }
640
+
641
+ $attachments = [];
642
+ foreach ( (array) $attach_ids as $att_id ) {
643
+ $att_id = absint( $att_id );
644
+ if ( ! $att_id ) continue;
645
+ $attachments[] = [
646
+ 'id' => $att_id,
647
+ 'filename' => basename( get_attached_file( $att_id ) ),
648
+ 'url' => wp_get_attachment_url( $att_id ),
649
+ ];
650
+ }
651
+
652
+ wp_send_json_success( [
653
+ 'lesson_id' => $lesson_id,
654
+ 'title' => $lesson->post_title,
655
+ 'content' => $lesson->post_content,
656
+ 'videos' => $videos,
657
+ 'languages' => $languages,
658
+ 'is_preview' => $is_preview,
659
+ 'thumbnail' => $thumbnail,
660
+ 'thumb_url' => $thumbnail ? wp_get_attachment_image_url( $thumbnail, 'thumbnail' ) : '',
661
+ 'attachments' => $attachments,
662
+ ] );
663
+ }
664
+
665
+ public static function ajax_update_lesson() {
666
+ self::verify_ajax_nonce();
667
+
668
+ $lesson_id = absint( $_POST['lesson_id'] ?? 0 );
669
+ if ( ! $lesson_id || ! current_user_can( 'edit_post', $lesson_id ) ) {
670
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
671
+ }
672
+
673
+ wp_update_post( [
674
+ 'ID' => $lesson_id,
675
+ 'post_title' => sanitize_text_field( $_POST['title'] ?? '' ),
676
+ 'post_content' => wp_kses_post( $_POST['content'] ?? '' ),
677
+ ] );
678
+
679
+ $thumb_id = absint( $_POST['thumbnail_id'] ?? 0 );
680
+ if ( $thumb_id ) {
681
+ set_post_thumbnail( $lesson_id, $thumb_id );
682
+ } else {
683
+ delete_post_thumbnail( $lesson_id );
684
+ }
685
+
686
+ $languages = self::languages();
687
+ foreach ( $languages as $lang ) {
688
+ $code = $lang['code'];
689
+ $field = 'video_' . $code;
690
+ if ( isset( $_POST[ $field ] ) ) {
691
+ update_post_meta( $lesson_id, '_video_' . $code, self::sanitize_video_meta( $_POST[ $field ] ) );
692
+ }
693
+ }
694
+
695
+ update_post_meta( $lesson_id, '_is_preview', absint( $_POST['is_preview'] ?? 0 ) === 1 ? 1 : 0 );
696
+
697
+ $raw_ids = array_map( 'absint', (array) ( $_POST['attachment_ids'] ?? [] ) );
698
+ $att_ids = array_filter( $raw_ids );
699
+ update_post_meta( $lesson_id, '_tutor_attachments', serialize( array_values( $att_ids ) ) );
700
+
701
+ $lesson = get_post( $lesson_id );
702
+ ob_start();
703
+ self::render_lesson_row( $lesson, (int) $lesson->post_parent );
704
+ $html = ob_get_clean();
705
+
706
+ wp_send_json_success( [ 'html' => $html, 'lesson_id' => $lesson_id ] );
707
+ }
708
+
709
+ public static function ajax_delete_lesson() {
710
+ self::verify_ajax_nonce();
711
+
712
+ $lesson_id = absint( $_POST['lesson_id'] ?? 0 );
713
+ if ( ! $lesson_id || ! current_user_can( 'edit_post', $lesson_id ) ) {
714
+ wp_send_json_error( [ 'message' => __( 'Unauthorized.' ) ] );
715
+ }
716
+
717
+ wp_delete_post( $lesson_id, true );
718
+
719
+ wp_send_json_success( [ 'lesson_id' => $lesson_id ] );
720
+ }
721
+
722
+ public static function ajax_sort_lessons() {
723
+ self::verify_ajax_nonce();
724
+
725
+ $order = array_map( 'absint', $_POST['order'] ?? [] );
726
+ foreach ( $order as $menu_order => $lesson_id ) {
727
+ if ( current_user_can( 'edit_post', $lesson_id ) ) {
728
+ wp_update_post( [
729
+ 'ID' => $lesson_id,
730
+ 'menu_order' => $menu_order,
731
+ ] );
732
+ }
733
+ }
734
+
735
+ wp_send_json_success();
736
+ }
737
+
738
+ public static function verify_ajax_nonce() {
739
+ if ( ! check_ajax_referer( 'course_ajax', 'nonce', false ) ) {
740
+ wp_send_json_error( [ 'message' => __( 'Security check failed.' ) ] );
741
+ }
742
+ }
743
+
744
+ public static function menu_parent_file( $parent ) {
745
+ global $current_screen;
746
+ if ( in_array( $current_screen->taxonomy, [ 'course-category', 'course-tag' ], true ) ) {
747
+ return 'edit.php?post_type=courses';
748
+ }
749
+ return $parent;
750
+ }
751
+
752
+ public static function menu_submenu_file( $submenu ) {
753
+ global $current_screen;
754
+ if ( $current_screen->taxonomy === 'course-category' ) {
755
+ return 'edit-tags.php?taxonomy=course-category&post_type=courses';
756
+ }
757
+ if ( $current_screen->taxonomy === 'course-tag' ) {
758
+ return 'edit-tags.php?taxonomy=course-tag&post_type=courses';
759
+ }
760
+ return $submenu;
761
+ }
762
+
763
+ public static function admin_enqueue( $hook ) {
764
+ $screen = get_current_screen();
765
+ if ( ! $screen || 'courses' !== $screen->post_type || 'post' !== $screen->base ) {
766
+ return;
767
+ }
768
+ wp_enqueue_media();
769
+ wp_enqueue_script( 'jquery-ui-sortable' );
770
+ wp_enqueue_style( 'course-builder', Platform::url( 'assets/editors/course.css' ), [], Platform::asset_version( 'assets/editors/course.css' ) );
771
+ wp_enqueue_script( 'course-builder', Platform::url( 'assets/editors/course.js' ), [ 'jquery', 'jquery-ui-sortable', 'wp-util' ], Platform::asset_version( 'assets/editors/course.js' ), true );
772
+ wp_localize_script( 'course-builder', 'courseBuilder', [
773
+ 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
774
+ 'nonce' => wp_create_nonce( 'course_ajax' ),
775
+ 'i18n' => [
776
+ 'confirmDeleteTopic' => __( 'Delete this topic and all its lessons?' ),
777
+ 'confirmDeleteLesson' => __( 'Delete this lesson?' ),
778
+ 'saving' => __( 'Saving…' ),
779
+ 'saved' => __( 'Saved' ),
780
+ 'error' => __( 'An error occurred.' ),
781
+ ],
782
+ ] );
783
+ }
784
+ }