@wp-operations/wp-ops 0.3.0 → 0.4.0

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 (119) hide show
  1. package/README.md +6 -3
  2. package/package.json +6 -2
  3. package/src/commands/platform.js +58 -2
  4. package/src/commands/start.js +2 -2
  5. package/src/main.js +19 -8
  6. package/wp-operations/CHANGELOG.md +14 -0
  7. package/wp-operations/README.md +39 -0
  8. package/wp-operations/agents/builder/AGENT.md +15 -0
  9. package/wp-operations/assets/admin.css +631 -0
  10. package/wp-operations/assets/admin.js +614 -0
  11. package/wp-operations/assets/app/app.css +1 -0
  12. package/wp-operations/assets/app/app.js +70 -0
  13. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  14. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  15. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  16. package/wp-operations/assets/app/login.css +42 -0
  17. package/wp-operations/assets/brand/icon.svg +20 -0
  18. package/wp-operations/assets/brand/mark.svg +19 -0
  19. package/wp-operations/assets/editors/audiobook.css +311 -0
  20. package/wp-operations/assets/editors/audiofile.css +286 -0
  21. package/wp-operations/assets/editors/course.css +701 -0
  22. package/wp-operations/assets/editors/course.js +698 -0
  23. package/wp-operations/assets/editors/faq.css +258 -0
  24. package/wp-operations/assets/editors/faq.js +196 -0
  25. package/wp-operations/assets/editors/interactive.css +220 -0
  26. package/wp-operations/assets/editors/interactive.js +378 -0
  27. package/wp-operations/assets/editors/media-sources.css +3 -0
  28. package/wp-operations/assets/editors/media-sources.js +40 -0
  29. package/wp-operations/assets/editors/open-media.css +114 -0
  30. package/wp-operations/assets/editors/open-media.js +182 -0
  31. package/wp-operations/assets/editors/thumbnails.css +335 -0
  32. package/wp-operations/assets/editors/thumbnails.js +447 -0
  33. package/wp-operations/assets/editors/translator.css +818 -0
  34. package/wp-operations/functions.php +24 -0
  35. package/wp-operations/index.php +14 -0
  36. package/wp-operations/platform.json +1836 -0
  37. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  38. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  39. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  40. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  41. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  42. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  43. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  44. package/wp-operations/src/Admin.php +78 -0
  45. package/wp-operations/src/App/Access.php +57 -0
  46. package/wp-operations/src/App/Admin.php +442 -0
  47. package/wp-operations/src/App/App.php +157 -0
  48. package/wp-operations/src/App/Apps.php +379 -0
  49. package/wp-operations/src/App/Bundle.php +111 -0
  50. package/wp-operations/src/App/Contents.php +676 -0
  51. package/wp-operations/src/App/Engine.php +554 -0
  52. package/wp-operations/src/App/Github.php +308 -0
  53. package/wp-operations/src/App/Module.php +23 -0
  54. package/wp-operations/src/App/Ops.php +470 -0
  55. package/wp-operations/src/App/Packages.php +289 -0
  56. package/wp-operations/src/App/Rest.php +539 -0
  57. package/wp-operations/src/App/Sandboxes.php +206 -0
  58. package/wp-operations/src/App/Store.php +104 -0
  59. package/wp-operations/src/App/Usage.php +268 -0
  60. package/wp-operations/src/Config.php +249 -0
  61. package/wp-operations/src/Content/Editor.php +223 -0
  62. package/wp-operations/src/Content/Fields.php +681 -0
  63. package/wp-operations/src/Content/Scoring.php +253 -0
  64. package/wp-operations/src/Content/Structure.php +293 -0
  65. package/wp-operations/src/Content/Types.php +150 -0
  66. package/wp-operations/src/Content/Uploads.php +50 -0
  67. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  68. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  69. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  70. package/wp-operations/src/Editors/Audiofile.php +462 -0
  71. package/wp-operations/src/Editors/Course.php +784 -0
  72. package/wp-operations/src/Editors/Ebook.php +242 -0
  73. package/wp-operations/src/Editors/Editors.php +44 -0
  74. package/wp-operations/src/Editors/Faq.php +237 -0
  75. package/wp-operations/src/Editors/Files.php +139 -0
  76. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  77. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  78. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  79. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  80. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  81. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  82. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  83. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  84. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  85. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  86. package/wp-operations/src/Editors/Legal.php +235 -0
  87. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  88. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  89. package/wp-operations/src/Editors/Sources.php +28 -0
  90. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  91. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  92. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  93. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  94. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  95. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  96. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  97. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  98. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  99. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  100. package/wp-operations/src/Identity.php +104 -0
  101. package/wp-operations/src/Languages.php +45 -0
  102. package/wp-operations/src/Lockdown.php +85 -0
  103. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  104. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  105. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  106. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  107. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  108. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  109. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  110. package/wp-operations/src/Modules/Translations.php +133 -0
  111. package/wp-operations/src/Platform.php +59 -0
  112. package/wp-operations/src/Setup.php +273 -0
  113. package/wp-operations/src/Status.php +99 -0
  114. package/wp-operations/src/Storage/Client.php +168 -0
  115. package/wp-operations/src/Storage/Offload.php +120 -0
  116. package/wp-operations/src/Storage/Settings.php +166 -0
  117. package/wp-operations/src/Storage/Storage.php +35 -0
  118. package/wp-operations/src/Updates.php +151 -0
  119. package/wp-operations/style.css +11 -0
@@ -0,0 +1,698 @@
1
+ ( function ( $, data ) {
2
+ 'use strict';
3
+
4
+ const { ajaxUrl, nonce, i18n } = data;
5
+
6
+ function lmsAjax( action, postData ) {
7
+ return $.post( ajaxUrl, $.extend( { action, nonce }, postData ) );
8
+ }
9
+
10
+ function spinner() {
11
+ return $( '<span class="course-spinner"></span>' );
12
+ }
13
+
14
+ $( document ).on( 'click', '.course-tab', function () {
15
+ const tab = $( this ).data( 'tab' );
16
+ const wrap = $( this ).closest( '.course-builder' );
17
+
18
+ wrap.find( '.course-tab' ).removeClass( 'active' );
19
+ wrap.find( '.course-tab-panel' ).removeClass( 'active' );
20
+
21
+ $( this ).addClass( 'active' );
22
+ wrap.find( '#course-tab-' + tab ).addClass( 'active' );
23
+ } );
24
+
25
+ function initTopicSortable() {
26
+ $( '#course-topics-list' ).sortable( {
27
+ items: '> .course-topic-item',
28
+ handle: '.course-topic-header',
29
+ placeholder: 'course-sortable-placeholder course-topic-item',
30
+ axis: 'y',
31
+ tolerance: 'pointer',
32
+ stop: function () {
33
+ const order = [];
34
+ $( '#course-topics-list .course-topic-item' ).each( function ( i ) {
35
+ order[ i ] = $( this ).data( 'topic-id' );
36
+ } );
37
+ lmsAjax( 'course_sort_topics', { order } );
38
+ }
39
+ } );
40
+ }
41
+
42
+ $( document ).on( 'click', '.course-add-topic', function () {
43
+ const btn = $( this );
44
+ const courseId = btn.data( 'course-id' );
45
+ const sp = spinner();
46
+
47
+ btn.prop( 'disabled', true ).prepend( sp );
48
+
49
+ lmsAjax( 'course_add_topic', {
50
+ course_id: courseId,
51
+ title: 'New Topic',
52
+ } )
53
+ .done( function ( res ) {
54
+ if ( res.success ) {
55
+ $( '#course-topics-list' ).append( res.data.html );
56
+ initLessonSortable( res.data.topic_id );
57
+ const $item = $( '[data-topic-id="' + res.data.topic_id + '"]' );
58
+ toggleTopicEdit( $item );
59
+ } else {
60
+ alert( res.data.message || i18n.error );
61
+ }
62
+ } )
63
+ .always( function () {
64
+ sp.remove();
65
+ btn.prop( 'disabled', false );
66
+ } );
67
+ } );
68
+
69
+ $( document ).on( 'click', '.course-toggle', function () {
70
+ const $item = $( this ).closest( '.course-topic-item' );
71
+ $item.toggleClass( 'is-open' );
72
+ $item.find( '> .course-lessons-list, > .course-topic-footer' ).toggle();
73
+ } );
74
+
75
+ $( document ).on( 'click', '.course-edit-topic', function () {
76
+ const $item = $( this ).closest( '.course-topic-item' );
77
+ toggleTopicEdit( $item );
78
+ } );
79
+
80
+ function toggleTopicEdit( $item ) {
81
+ $item.find( '.course-topic-edit-form' ).slideToggle( 150 );
82
+ }
83
+
84
+ $( document ).on( 'click', '.course-update-topic', function () {
85
+ const btn = $( this );
86
+ const topicId = btn.data( 'topic-id' );
87
+ const $item = btn.closest( '.course-topic-item' );
88
+ const title = $item.find( '.course-topic-name-input' ).val();
89
+ const editorId = 'topic_content_' + topicId;
90
+ let content = '';
91
+
92
+ if ( typeof tinymce !== 'undefined' && tinymce.get( editorId ) ) {
93
+ content = tinymce.get( editorId ).getContent();
94
+ } else {
95
+ content = $( '#' + editorId ).val();
96
+ }
97
+
98
+ const sp = spinner();
99
+ btn.prop( 'disabled', true ).prepend( sp );
100
+
101
+ lmsAjax( 'course_update_topic', { topic_id: topicId, title, content } )
102
+ .done( function ( res ) {
103
+ if ( res.success ) {
104
+ $item.find( '.course-topic-title' ).text( res.data.title );
105
+ $item.find( '.course-topic-edit-form' ).slideUp( 150 );
106
+ } else {
107
+ alert( res.data.message || i18n.error );
108
+ }
109
+ } )
110
+ .always( function () { sp.remove(); btn.prop( 'disabled', false ); } );
111
+ } );
112
+
113
+ $( document ).on( 'click', '.course-delete-topic', function () {
114
+ if ( ! confirm( i18n.confirmDeleteTopic ) ) return;
115
+
116
+ const btn = $( this );
117
+ const topicId = btn.data( 'topic-id' );
118
+ const courseId = btn.data( 'course-id' );
119
+ const $item = btn.closest( '.course-topic-item' );
120
+ const sp = spinner();
121
+
122
+ btn.prop( 'disabled', true ).prepend( sp );
123
+
124
+ lmsAjax( 'course_delete_topic', { topic_id: topicId, course_id: courseId } )
125
+ .done( function ( res ) {
126
+ if ( res.success ) {
127
+ $item.remove();
128
+ } else {
129
+ alert( res.data.message || i18n.error );
130
+ }
131
+ } )
132
+ .always( function () { sp.remove(); btn.prop( 'disabled', false ); } );
133
+ } );
134
+
135
+ function initLessonSortable( topicId ) {
136
+ const $list = topicId
137
+ ? $( '.course-lessons-list[data-topic-id="' + topicId + '"]' )
138
+ : $( '.course-lessons-list' );
139
+
140
+ $list.sortable( {
141
+ items: '> .course-lesson-item',
142
+ handle: '.course-drag-handle',
143
+ placeholder: 'course-sortable-placeholder course-lesson-item',
144
+ axis: 'y',
145
+ tolerance: 'pointer',
146
+ stop: function () {
147
+ const $thisTopicId = $( this ).data( 'topic-id' );
148
+ const order = [];
149
+ $( this ).find( '> .course-lesson-item' ).each( function ( i ) {
150
+ order[ i ] = $( this ).data( 'lesson-id' );
151
+ } );
152
+ lmsAjax( 'course_sort_lessons', { order } );
153
+ }
154
+ } );
155
+ }
156
+
157
+ $( document ).on( 'click', '.course-add-lesson', function () {
158
+ const btn = $( this );
159
+ const topicId = btn.data( 'topic-id' );
160
+ const courseId = btn.data( 'course-id' );
161
+ const sp = spinner();
162
+
163
+ btn.prop( 'disabled', true ).prepend( sp );
164
+
165
+ lmsAjax( 'course_add_lesson', {
166
+ topic_id: topicId,
167
+ course_id: courseId,
168
+ title: 'New Lesson',
169
+ } )
170
+ .done( function ( res ) {
171
+ if ( res.success ) {
172
+ const $list = $( '.course-lessons-list[data-topic-id="' + topicId + '"]' );
173
+ $list.append( res.data.html );
174
+ } else {
175
+ alert( res.data.message || i18n.error );
176
+ }
177
+ } )
178
+ .always( function () { sp.remove(); btn.prop( 'disabled', false ); } );
179
+ } );
180
+
181
+ $( document ).on( 'click', '.course-delete-lesson', function () {
182
+ if ( ! confirm( i18n.confirmDeleteLesson ) ) return;
183
+
184
+ const btn = $( this );
185
+ const lessonId = btn.data( 'lesson-id' );
186
+ const $item = btn.closest( '.course-lesson-item' );
187
+ const sp = spinner();
188
+
189
+ btn.prop( 'disabled', true ).prepend( sp );
190
+
191
+ lmsAjax( 'course_delete_lesson', {
192
+ lesson_id: lessonId,
193
+ topic_id: btn.data( 'topic-id' ),
194
+ } )
195
+ .done( function ( res ) {
196
+ if ( res.success ) {
197
+ $item.remove();
198
+ } else {
199
+ alert( res.data.message || i18n.error );
200
+ }
201
+ } )
202
+ .always( function () { sp.remove(); btn.prop( 'disabled', false ); } );
203
+ } );
204
+
205
+ $( document ).on( 'click', '.course-edit-lesson', function () {
206
+ const lessonId = $( this ).data( 'lesson-id' );
207
+ const topicId = $( this ).data( 'topic-id' );
208
+ openLessonModal( lessonId, topicId );
209
+ } );
210
+
211
+ function openLessonModal( lessonId, topicId ) {
212
+ lmsAjax( 'course_get_lesson', { lesson_id: lessonId } )
213
+ .done( function ( res ) {
214
+ if ( ! res.success ) { alert( res.data.message || i18n.error ); return; }
215
+ const d = res.data;
216
+ buildLessonModal( d, topicId );
217
+ } );
218
+ }
219
+
220
+ function buildVideoFields( video, langCode ) {
221
+ const fieldId = langCode ? `modal_video_${langCode}` : 'modal_video';
222
+ const types = { youtube: 'YouTube', vimeo: 'Vimeo', external_url: 'External URL', html5: 'Embed', shortcode: 'Shortcode' };
223
+ const active = video.source_type || 'youtube';
224
+ const source = video.source || '';
225
+
226
+ let tabs = '';
227
+ let panels = '';
228
+
229
+ Object.entries( types ).forEach( ( [ slug, label ] ) => {
230
+ tabs += `<button type="button" class="course-video-tab ${active === slug ? 'active' : ''}" data-type="${slug}">${label}</button>`;
231
+
232
+ let inputHtml = '';
233
+ const val = active === slug ? esc( source ) : '';
234
+
235
+ if ( slug === 'html5' ) {
236
+ inputHtml = `<textarea class="course-input course-video-source-field course-embed-textarea" data-type="html5" rows="4" placeholder="<iframe ...></iframe>">${val}</textarea>
237
+ <p class="course-field-hint">Paste any embed code — iframe, video tag, etc.</p>`;
238
+ } else if ( slug === 'shortcode' ) {
239
+ inputHtml = `<input type="text" class="course-input course-video-source-field" data-type="shortcode" value="${val}" placeholder="[video_shortcode id=&quot;123&quot;]" />
240
+ <p class="course-field-hint">Paste a WordPress shortcode that outputs a video.</p>`;
241
+ } else if ( slug === 'youtube' ) {
242
+ inputHtml = `<input type="url" class="course-input course-video-source-field" data-type="youtube" value="${val}" placeholder="https://www.youtube.com/watch?v=..." />
243
+ <p class="course-field-hint">Paste a YouTube video URL. Short URLs (youtu.be) are also accepted.</p>
244
+ <span class="course-video-error" style="display:none;"></span>`;
245
+ } else if ( slug === 'vimeo' ) {
246
+ inputHtml = `<input type="url" class="course-input course-video-source-field" data-type="vimeo" value="${val}" placeholder="https://vimeo.com/..." />
247
+ <p class="course-field-hint">Paste a Vimeo video URL.</p>
248
+ <span class="course-video-error" style="display:none;"></span>`;
249
+ } else if ( slug === 'external_url' ) {
250
+ inputHtml = `<input type="url" class="course-input course-video-source-field" data-type="external_url" value="${val}" placeholder="https://example.com/video.mp4" />
251
+ <p class="course-field-hint">Direct URL to a video file (.mp4, .webm, .ogg).</p>
252
+ <span class="course-video-error" style="display:none;"></span>`;
253
+ }
254
+
255
+ panels += `<div class="course-video-input-wrap ${active === slug ? 'active' : ''}" data-type="${slug}">${inputHtml}</div>`;
256
+ } );
257
+
258
+ const previewHtml = source ? VideoFields.buildPreview( active, source ) : '';
259
+ const previewStyle = previewHtml ? '' : 'display:none;';
260
+
261
+ return `
262
+ <div class="course-video-fields" data-field="modal_video">
263
+ <input type="hidden" class="course-video-source-type-input" value="${esc(active)}" />
264
+ <input type="hidden" class="course-video-source-hidden" value="${esc(source)}" />
265
+ <input type="hidden" class="course-video-runtime-hours" value="00" />
266
+ <input type="hidden" class="course-video-runtime-minutes" value="00" />
267
+ <input type="hidden" class="course-video-runtime-seconds" value="00" />
268
+ <div class="course-video-tabs">${tabs}</div>
269
+ ${panels}
270
+ <div class="course-video-preview-wrap" style="${previewStyle}">
271
+ <div class="course-video-preview-inner">${previewHtml}</div>
272
+ </div>
273
+ <div class="course-video-duration-display" style="display:none;">
274
+ <span class="dashicons dashicons-clock"></span>
275
+ <span class="course-duration-text"></span>
276
+ </div>
277
+ </div>`;
278
+ }
279
+
280
+ function buildMultilangVideo( videos, languages ) {
281
+ if ( ! languages || ! languages.length ) {
282
+ return '<p class="description">No languages configured. Add languages under Courses → Languages.</p>';
283
+ }
284
+
285
+ let tabs = '';
286
+ let panels = '';
287
+
288
+ languages.forEach( function( lang, i ) {
289
+ const code = lang.code;
290
+ const name = lang.name;
291
+ const video = ( videos && videos[ code ] ) ? videos[ code ] : { source_type: 'youtube', source: '', runtime: {} };
292
+
293
+ tabs += `<button type="button" class="course-lang-video-tab ${i === 0 ? 'active' : ''}" data-lang="${esc(code)}">${esc(name)}</button>`;
294
+ panels += `<div class="course-lang-video-panel course-video-box ${i === 0 ? 'active' : ''}" data-lang="${esc(code)}">
295
+ ${buildVideoFields( video, code )}
296
+ </div>`;
297
+ } );
298
+
299
+ return `<div class="course-lang-video-tabs">${tabs}</div>${panels}`;
300
+ }
301
+
302
+ function collectLessonVideos() {
303
+ const data = {};
304
+ $( '#course-lesson-modal .course-lang-video-panel' ).each( function() {
305
+ const code = $( this ).data( 'lang' );
306
+ data[ `video_${code}[source_type]` ] = $( this ).find( '.course-video-source-type-input' ).val();
307
+ data[ `video_${code}[source]` ] = $( this ).find( '.course-video-source-hidden' ).val();
308
+ data[ `video_${code}[runtime][hours]` ] = $( this ).find( '.course-video-runtime-hours' ).val();
309
+ data[ `video_${code}[runtime][minutes]` ] = $( this ).find( '.course-video-runtime-minutes' ).val();
310
+ data[ `video_${code}[runtime][seconds]` ] = $( this ).find( '.course-video-runtime-seconds' ).val();
311
+ } );
312
+ return data;
313
+ }
314
+
315
+ function buildLessonModal( d, topicId ) {
316
+ let attachHtml = '<ul class="course-attachment-list" id="course-attachment-list">';
317
+ d.attachments.forEach( function ( att ) {
318
+ attachHtml += `<li data-id="${att.id}">
319
+ <span class="dashicons dashicons-paperclip"></span>
320
+ <a href="${att.url}" target="_blank">${att.filename}</a>
321
+ <input type="hidden" name="attachment_ids[]" value="${att.id}" />
322
+ <button type="button" class="course-remove-attachment" title="Remove">&times;</button>
323
+ </li>`;
324
+ } );
325
+ attachHtml += '</ul>';
326
+
327
+ const thumbDisplay = d.thumb_url
328
+ ? `<img src="${d.thumb_url}" class="course-thumb-preview" id="course-lesson-thumb-preview" />`
329
+ : `<img src="" class="course-thumb-preview" id="course-lesson-thumb-preview" style="display:none;" />`;
330
+
331
+ const modal = $( `
332
+ <div class="course-modal-overlay" id="course-lesson-modal">
333
+ <div class="course-modal">
334
+ <div class="course-modal-header">
335
+ <span>Edit Lesson</span>
336
+ <button type="button" class="course-modal-close">&times;</button>
337
+ </div>
338
+ <div class="course-modal-body">
339
+
340
+ <!-- Title -->
341
+ <div class="course-field-group">
342
+ <label class="course-label">Lesson Title</label>
343
+ <input type="text" id="course-lesson-title" class="course-input"
344
+ value="${esc( d.title )}" />
345
+ </div>
346
+
347
+ <!-- Content -->
348
+ <div class="course-field-group">
349
+ <label class="course-label">Content</label>
350
+ <textarea id="course-lesson-content" rows="6" class="course-input">${esc( d.content )}</textarea>
351
+ </div>
352
+
353
+ <!-- Featured Image -->
354
+ <div class="course-field-group">
355
+ <label class="course-label">Featured Image</label>
356
+ ${thumbDisplay}
357
+ <input type="hidden" id="course-lesson-thumbnail-id" value="${d.thumbnail}" />
358
+ <button type="button" class="button" id="course-lesson-upload-thumb">
359
+ ${d.thumbnail ? 'Change Image' : 'Upload Image'}
360
+ </button>
361
+ ${d.thumbnail ? '<button type="button" class="button" id="course-lesson-remove-thumb" style="margin-left:4px;">Remove</button>' : ''}
362
+ </div>
363
+
364
+ <!-- Video — per language -->
365
+ <div class="course-field-group">
366
+ <label class="course-label">Video</label>
367
+ ${buildMultilangVideo( d.videos, d.languages )}
368
+ </div>
369
+
370
+ <!-- Preview Toggle -->
371
+ <div class="course-field-group">
372
+ <label>
373
+ <input type="checkbox" id="course-is-preview" ${d.is_preview ? 'checked' : ''} />
374
+ Free Preview
375
+ </label>
376
+ </div>
377
+
378
+ <!-- Attachments -->
379
+ <div class="course-field-group">
380
+ <label class="course-label">Downloadable Files</label>
381
+ ${attachHtml}
382
+ <button type="button" class="button" id="course-add-attachment">
383
+ + Add File
384
+ </button>
385
+ </div>
386
+
387
+ </div><!-- /.course-modal-body -->
388
+
389
+ <div class="course-modal-footer">
390
+ <button type="button" class="button" id="course-modal-cancel">Cancel</button>
391
+ <button type="button" class="button button-primary" id="course-modal-save"
392
+ data-lesson-id="${d.lesson_id}" data-topic-id="${topicId}">
393
+ Save Lesson
394
+ </button>
395
+ </div>
396
+ </div>
397
+ </div>
398
+ ` );
399
+
400
+ $( 'body' ).append( modal );
401
+ }
402
+
403
+ $( document ).on( 'click', '.course-modal-close, #course-modal-cancel', function () {
404
+ $( '#course-lesson-modal' ).remove();
405
+ } );
406
+
407
+ $( document ).on( 'click', '#course-lesson-modal', function ( e ) {
408
+ if ( $( e.target ).is( '#course-lesson-modal' ) ) {
409
+ $( '#course-lesson-modal' ).remove();
410
+ }
411
+ } );
412
+
413
+ $( document ).on( 'click', '.course-remove-attachment', function () {
414
+ $( this ).closest( 'li' ).remove();
415
+ } );
416
+
417
+ $( document ).on( 'click', '#course-lesson-upload-thumb', function () {
418
+ const frame = wp.media( {
419
+ title: 'Choose Featured Image',
420
+ button: { text: 'Use this image' },
421
+ multiple: false,
422
+ } );
423
+ frame.on( 'select', function () {
424
+ const attachment = frame.state().get( 'selection' ).first().toJSON();
425
+ $( '#course-lesson-thumbnail-id' ).val( attachment.id );
426
+ $( '#course-lesson-thumb-preview' ).attr( 'src', attachment.url ).show();
427
+ $( '#course-lesson-upload-thumb' ).text( 'Change Image' );
428
+ } );
429
+ frame.open();
430
+ } );
431
+
432
+ $( document ).on( 'click', '#course-lesson-remove-thumb', function () {
433
+ $( '#course-lesson-thumbnail-id' ).val( '' );
434
+ $( '#course-lesson-thumb-preview' ).hide();
435
+ $( '#course-lesson-upload-thumb' ).text( 'Upload Image' );
436
+ $( this ).remove();
437
+ } );
438
+
439
+ $( document ).on( 'click', '#course-add-attachment', function () {
440
+ const frame = wp.media( {
441
+ title: 'Choose Attachment',
442
+ button: { text: 'Attach file' },
443
+ multiple: true,
444
+ library: { type: '' },
445
+ } );
446
+ frame.on( 'select', function () {
447
+ const attachments = frame.state().get( 'selection' ).toJSON();
448
+ attachments.forEach( function ( att ) {
449
+ const filename = att.filename || att.url.split( '/' ).pop();
450
+ $( '#course-attachment-list' ).append(
451
+ `<li data-id="${att.id}">
452
+ <span class="dashicons dashicons-paperclip"></span>
453
+ <a href="${att.url}" target="_blank">${filename}</a>
454
+ <input type="hidden" name="attachment_ids[]" value="${att.id}" />
455
+ <button type="button" class="course-remove-attachment" title="Remove">&times;</button>
456
+ </li>`
457
+ );
458
+ } );
459
+ } );
460
+ frame.open();
461
+ } );
462
+
463
+ $( document ).on( 'click', '#course-modal-save', function () {
464
+ const btn = $( this );
465
+ const lessonId = btn.data( 'lesson-id' );
466
+ const topicId = btn.data( 'topic-id' );
467
+ const sp = spinner();
468
+
469
+ const attIds = [];
470
+ $( '#course-attachment-list li' ).each( function () {
471
+ attIds.push( $( this ).data( 'id' ) );
472
+ } );
473
+
474
+ btn.prop( 'disabled', true ).prepend( sp );
475
+
476
+ lmsAjax( 'course_update_lesson', {
477
+ lesson_id: lessonId,
478
+ title: $( '#course-lesson-title' ).val(),
479
+ content: $( '#course-lesson-content' ).val(),
480
+ thumbnail_id: $( '#course-lesson-thumbnail-id' ).val(),
481
+ is_preview: $( '#course-is-preview' ).is( ':checked' ) ? 1 : 0,
482
+ attachment_ids: attIds,
483
+ ...collectLessonVideos(),
484
+ } )
485
+ .done( function ( res ) {
486
+ if ( res.success ) {
487
+ $( '.course-lesson-item[data-lesson-id="' + lessonId + '"]' ).replaceWith( res.data.html );
488
+ $( '#course-lesson-modal' ).remove();
489
+ } else {
490
+ alert( res.data.message || i18n.error );
491
+ }
492
+ } )
493
+ .always( function () { sp.remove(); btn.prop( 'disabled', false ); } );
494
+ } );
495
+
496
+ $( document ).on( 'click', '.course-upload-thumb-btn', function () {
497
+ const $wrap = $( this ).closest( '.course-term-thumbnail-wrap' );
498
+ const frame = wp.media( {
499
+ title: 'Choose Thumbnail',
500
+ button: { text: 'Use this image' },
501
+ multiple: false,
502
+ } );
503
+ frame.on( 'select', function () {
504
+ const att = frame.state().get( 'selection' ).first().toJSON();
505
+ $wrap.find( '.course-term-thumbnail-id' ).val( att.id );
506
+ $wrap.find( '.course-term-thumb-preview' ).attr( 'src', att.sizes?.thumbnail?.url || att.url ).show();
507
+ $wrap.find( '.course-upload-thumb-btn' ).text( 'Change Image' );
508
+ } );
509
+ frame.open();
510
+ } );
511
+
512
+ $( document ).on( 'click', '.course-remove-thumb-btn', function () {
513
+ const $wrap = $( this ).closest( '.course-term-thumbnail-wrap' );
514
+ $wrap.find( '.course-term-thumbnail-id' ).val( '' );
515
+ $wrap.find( '.course-term-thumb-preview' ).hide();
516
+ $wrap.find( '.course-upload-thumb-btn' ).text( 'Upload Image' );
517
+ $( this ).remove();
518
+ } );
519
+
520
+ function esc( str ) {
521
+ return $( '<div>' ).text( str || '' ).html();
522
+ }
523
+
524
+ function ucFirst( str ) {
525
+ return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
526
+ }
527
+
528
+ const VideoFields = {
529
+
530
+ youtubeId( url ) {
531
+ const m = url.match( /(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/ );
532
+ return m ? m[1] : null;
533
+ },
534
+
535
+ vimeoId( url ) {
536
+ const m = url.match( /vimeo\.com\/(?:video\/)?(\d+)/ );
537
+ return m ? m[1] : null;
538
+ },
539
+
540
+ isValidUrl( url ) {
541
+ try { new URL( url ); return true; } catch { return false; }
542
+ },
543
+
544
+ isVideoUrl( url ) {
545
+ return /\.(mp4|webm|ogg|mov|m4v)(\?.*)?$/i.test( url );
546
+ },
547
+
548
+ validate( type, value ) {
549
+ if ( ! value ) return '';
550
+ if ( type === 'youtube' ) return this.youtubeId( value ) ? '' : 'Not a valid YouTube URL.';
551
+ if ( type === 'vimeo' ) return this.vimeoId( value ) ? '' : 'Not a valid Vimeo URL.';
552
+ if ( type === 'external_url' ) {
553
+ if ( ! this.isValidUrl( value ) ) return 'Not a valid URL.';
554
+ if ( ! this.isVideoUrl( value ) ) return 'URL does not appear to be a video file (.mp4, .webm…).';
555
+ }
556
+ return '';
557
+ },
558
+
559
+ buildPreview( type, source ) {
560
+ if ( ! source ) return '';
561
+ if ( type === 'youtube' ) {
562
+ const id = this.youtubeId( source );
563
+ return id ? `<iframe src="https://www.youtube.com/embed/${id}" frameborder="0" allowfullscreen></iframe>` : '';
564
+ }
565
+ if ( type === 'vimeo' ) {
566
+ const id = this.vimeoId( source );
567
+ return id ? `<iframe src="https://player.vimeo.com/video/${id}" frameborder="0" allowfullscreen></iframe>` : '';
568
+ }
569
+ if ( type === 'external_url' ) return `<video controls src="${source}"></video>`;
570
+ if ( type === 'html5' ) return source;
571
+ if ( type === 'shortcode' ) return `<p class="course-shortcode-note">[Shortcode preview not available in editor]</p>`;
572
+ return '';
573
+ },
574
+
575
+ fetchDuration( type, source, $wrap ) {
576
+ const setDuration = ( h, m, s ) => {
577
+ const pad = n => String( n ).padStart( 2, '0' );
578
+ $wrap.find( '.course-video-runtime-hours' ).val( pad( h ) );
579
+ $wrap.find( '.course-video-runtime-minutes' ).val( pad( m ) );
580
+ $wrap.find( '.course-video-runtime-seconds' ).val( pad( s ) );
581
+ const label = `${pad(h)}:${pad(m)}:${pad(s)}`;
582
+ $wrap.find( '.course-duration-text' ).text( label );
583
+ $wrap.find( '.course-video-duration-display' ).show();
584
+ };
585
+
586
+ if ( type === 'youtube' ) {
587
+ const id = this.youtubeId( source );
588
+ if ( ! id ) return;
589
+ $.getJSON( `https://noembed.com/embed?url=https://www.youtube.com/watch?v=${id}` )
590
+ .done( function( data ) {
591
+ $wrap.find( '.course-video-duration-display' ).hide();
592
+ } );
593
+ $wrap.find( '.course-video-duration-display' ).hide();
594
+ }
595
+
596
+ if ( type === 'vimeo' ) {
597
+ const id = this.vimeoId( source );
598
+ if ( ! id ) return;
599
+ $.getJSON( `https://vimeo.com/api/v2/video/${id}.json` )
600
+ .done( function( data ) {
601
+ if ( data && data[0] && data[0].duration ) {
602
+ const total = data[0].duration;
603
+ const h = Math.floor( total / 3600 );
604
+ const m = Math.floor( ( total % 3600 ) / 60 );
605
+ const s = total % 60;
606
+ setDuration( h, m, s );
607
+ }
608
+ } );
609
+ }
610
+
611
+ if ( type === 'external_url' ) {
612
+ const vid = document.createElement( 'video' );
613
+ vid.src = source;
614
+ vid.preload = 'metadata';
615
+ vid.onloadedmetadata = function() {
616
+ const total = Math.floor( vid.duration );
617
+ if ( isFinite( total ) ) {
618
+ const h = Math.floor( total / 3600 );
619
+ const m = Math.floor( ( total % 3600 ) / 60 );
620
+ const s = total % 60;
621
+ setDuration( h, m, s );
622
+ }
623
+ };
624
+ }
625
+ },
626
+
627
+ update( $wrap ) {
628
+ const type = $wrap.find( '.course-video-source-type-input' ).val();
629
+ const $panel = $wrap.find( `.course-video-input-wrap[data-type="${type}"]` );
630
+ const $input = $panel.find( '.course-video-source-field' );
631
+ const value = $input.val().trim();
632
+
633
+ $wrap.find( '.course-video-source-hidden' ).val( value );
634
+
635
+ const err = this.validate( type, value );
636
+ const $err = $panel.find( '.course-video-error' );
637
+ if ( err ) {
638
+ $input.addClass( 'is-invalid' ).removeClass( 'is-valid' );
639
+ $err.text( err ).show();
640
+ } else if ( value ) {
641
+ $input.addClass( 'is-valid' ).removeClass( 'is-invalid' );
642
+ $err.hide();
643
+ } else {
644
+ $input.removeClass( 'is-invalid is-valid' );
645
+ $err.hide();
646
+ }
647
+
648
+ const html = ( ! err && value ) ? this.buildPreview( type, value ) : '';
649
+ const $preview = $wrap.find( '.course-video-preview-wrap' );
650
+ if ( html ) {
651
+ $wrap.find( '.course-video-preview-inner' ).html( html );
652
+ $preview.show();
653
+ } else {
654
+ $preview.hide();
655
+ }
656
+
657
+ if ( ! err && value ) {
658
+ this.fetchDuration( type, value, $wrap );
659
+ }
660
+ },
661
+
662
+ init() {
663
+ $( document ).on( 'click', '.course-lang-video-tab', function () {
664
+ const lang = $( this ).data( 'lang' );
665
+ const $wrap = $( this ).closest( '.course-field-group, .course-modal-body' );
666
+ $wrap.find( '.course-lang-video-tab' ).removeClass( 'active' );
667
+ $wrap.find( '.course-lang-video-panel' ).removeClass( 'active' );
668
+ $( this ).addClass( 'active' );
669
+ $wrap.find( `.course-lang-video-panel[data-lang="${lang}"]` ).addClass( 'active' );
670
+ } );
671
+
672
+ $( document ).on( 'click', '.course-video-tab', function () {
673
+ const $wrap = $( this ).closest( '.course-video-fields' );
674
+ const type = $( this ).data( 'type' );
675
+ $wrap.find( '.course-video-tab' ).removeClass( 'active' );
676
+ $wrap.find( '.course-video-input-wrap' ).removeClass( 'active' );
677
+ $( this ).addClass( 'active' );
678
+ $wrap.find( `.course-video-input-wrap[data-type="${type}"]` ).addClass( 'active' );
679
+ $wrap.find( '.course-video-source-type-input' ).val( type );
680
+ VideoFields.update( $wrap );
681
+ } );
682
+
683
+ let timer;
684
+ $( document ).on( 'input', '.course-video-source-field', function () {
685
+ const $wrap = $( this ).closest( '.course-video-fields' );
686
+ clearTimeout( timer );
687
+ timer = setTimeout( () => VideoFields.update( $wrap ), 700 );
688
+ } );
689
+ }
690
+ };
691
+
692
+ $( function () {
693
+ initTopicSortable();
694
+ initLessonSortable();
695
+ VideoFields.init();
696
+ } );
697
+
698
+ } )( jQuery, courseBuilder );