@wp-operations/wp-ops 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +30 -27
  2. package/bin/wp-app.js +2 -0
  3. package/bin/wp-code.js +2 -0
  4. package/bin/wp-ide.js +2 -0
  5. package/package.json +12 -3
  6. package/src/commands/platform.js +16 -2
  7. package/src/commands/sandbox.js +3 -3
  8. package/src/main.js +10 -4
  9. package/wp-operations/CHANGELOG.md +14 -0
  10. package/wp-operations/README.md +39 -0
  11. package/wp-operations/agents/builder/AGENT.md +15 -0
  12. package/wp-operations/assets/admin.css +631 -0
  13. package/wp-operations/assets/admin.js +614 -0
  14. package/wp-operations/assets/app/app.css +1 -0
  15. package/wp-operations/assets/app/app.js +70 -0
  16. package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
  17. package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
  18. package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
  19. package/wp-operations/assets/app/login.css +42 -0
  20. package/wp-operations/assets/brand/icon.svg +20 -0
  21. package/wp-operations/assets/brand/mark.svg +19 -0
  22. package/wp-operations/assets/editors/audiobook.css +311 -0
  23. package/wp-operations/assets/editors/audiofile.css +286 -0
  24. package/wp-operations/assets/editors/course.css +701 -0
  25. package/wp-operations/assets/editors/course.js +698 -0
  26. package/wp-operations/assets/editors/faq.css +258 -0
  27. package/wp-operations/assets/editors/faq.js +196 -0
  28. package/wp-operations/assets/editors/interactive.css +220 -0
  29. package/wp-operations/assets/editors/interactive.js +378 -0
  30. package/wp-operations/assets/editors/media-sources.css +3 -0
  31. package/wp-operations/assets/editors/media-sources.js +40 -0
  32. package/wp-operations/assets/editors/open-media.css +114 -0
  33. package/wp-operations/assets/editors/open-media.js +182 -0
  34. package/wp-operations/assets/editors/thumbnails.css +335 -0
  35. package/wp-operations/assets/editors/thumbnails.js +447 -0
  36. package/wp-operations/assets/editors/translator.css +818 -0
  37. package/wp-operations/functions.php +24 -0
  38. package/wp-operations/index.php +14 -0
  39. package/wp-operations/platform.json +1836 -0
  40. package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
  41. package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
  42. package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
  43. package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
  44. package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
  45. package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
  46. package/wp-operations/skills/wp-security/SKILL.md +90 -0
  47. package/wp-operations/src/Admin.php +78 -0
  48. package/wp-operations/src/App/Access.php +57 -0
  49. package/wp-operations/src/App/Admin.php +442 -0
  50. package/wp-operations/src/App/App.php +157 -0
  51. package/wp-operations/src/App/Apps.php +379 -0
  52. package/wp-operations/src/App/Bundle.php +111 -0
  53. package/wp-operations/src/App/Contents.php +676 -0
  54. package/wp-operations/src/App/Engine.php +554 -0
  55. package/wp-operations/src/App/Github.php +308 -0
  56. package/wp-operations/src/App/Module.php +23 -0
  57. package/wp-operations/src/App/Ops.php +470 -0
  58. package/wp-operations/src/App/Packages.php +289 -0
  59. package/wp-operations/src/App/Rest.php +539 -0
  60. package/wp-operations/src/App/Sandboxes.php +206 -0
  61. package/wp-operations/src/App/Store.php +104 -0
  62. package/wp-operations/src/App/Usage.php +268 -0
  63. package/wp-operations/src/Config.php +249 -0
  64. package/wp-operations/src/Content/Editor.php +223 -0
  65. package/wp-operations/src/Content/Fields.php +681 -0
  66. package/wp-operations/src/Content/Scoring.php +253 -0
  67. package/wp-operations/src/Content/Structure.php +293 -0
  68. package/wp-operations/src/Content/Types.php +150 -0
  69. package/wp-operations/src/Content/Uploads.php +50 -0
  70. package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
  71. package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
  72. package/wp-operations/src/Editors/AudiobookCover.php +235 -0
  73. package/wp-operations/src/Editors/Audiofile.php +462 -0
  74. package/wp-operations/src/Editors/Course.php +784 -0
  75. package/wp-operations/src/Editors/Ebook.php +242 -0
  76. package/wp-operations/src/Editors/Editors.php +44 -0
  77. package/wp-operations/src/Editors/Faq.php +237 -0
  78. package/wp-operations/src/Editors/Files.php +139 -0
  79. package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
  80. package/wp-operations/src/Editors/Interactive/Card.php +76 -0
  81. package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
  82. package/wp-operations/src/Editors/Interactive/Field.php +272 -0
  83. package/wp-operations/src/Editors/Interactive/Map.php +84 -0
  84. package/wp-operations/src/Editors/Interactive/Models.php +227 -0
  85. package/wp-operations/src/Editors/Interactive/Report.php +59 -0
  86. package/wp-operations/src/Editors/Interactive/Save.php +323 -0
  87. package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
  88. package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
  89. package/wp-operations/src/Editors/Legal.php +235 -0
  90. package/wp-operations/src/Editors/OpenMedia.php +140 -0
  91. package/wp-operations/src/Editors/OriginalTitle.php +112 -0
  92. package/wp-operations/src/Editors/Sources.php +28 -0
  93. package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
  94. package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
  95. package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
  96. package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
  97. package/wp-operations/src/Editors/Translator/Admin.php +913 -0
  98. package/wp-operations/src/Editors/Translator/Api.php +63 -0
  99. package/wp-operations/src/Editors/Translator/Bar.php +83 -0
  100. package/wp-operations/src/Editors/Translator/Fields.php +273 -0
  101. package/wp-operations/src/Editors/Translator/Storage.php +131 -0
  102. package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
  103. package/wp-operations/src/Identity.php +104 -0
  104. package/wp-operations/src/Languages.php +45 -0
  105. package/wp-operations/src/Lockdown.php +85 -0
  106. package/wp-operations/src/Modules/Apps/Admin.php +472 -0
  107. package/wp-operations/src/Modules/Apps/Api.php +193 -0
  108. package/wp-operations/src/Modules/Apps/Apps.php +279 -0
  109. package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
  110. package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
  111. package/wp-operations/src/Modules/Apps/Table.php +101 -0
  112. package/wp-operations/src/Modules/Thumbnails.php +68 -0
  113. package/wp-operations/src/Modules/Translations.php +133 -0
  114. package/wp-operations/src/Platform.php +59 -0
  115. package/wp-operations/src/Setup.php +273 -0
  116. package/wp-operations/src/Status.php +99 -0
  117. package/wp-operations/src/Storage/Client.php +168 -0
  118. package/wp-operations/src/Storage/Offload.php +120 -0
  119. package/wp-operations/src/Storage/Settings.php +166 -0
  120. package/wp-operations/src/Storage/Storage.php +35 -0
  121. package/wp-operations/src/Updates.php +151 -0
  122. package/wp-operations/style.css +11 -0
@@ -0,0 +1,632 @@
1
+ <?php
2
+ namespace Platform\Editors;
3
+
4
+ use Platform\Languages;
5
+
6
+ defined( 'ABSPATH' ) || exit;
7
+
8
+ final class AudiobookChapters {
9
+ public function __construct() {
10
+
11
+ add_action( 'add_meta_boxes', array( $this, 'add_chapters_metabox' ) );
12
+ add_action( 'save_post', array( $this, 'save_chapters_meta' ) );
13
+ add_action( 'wp_ajax_upload_chapter_audio', array( $this, 'handle_audio_upload' ) );
14
+ add_action( 'wp_ajax_delete_chapter_audio', array( $this, 'handle_audio_delete' ) );
15
+ add_action( 'admin_post_secure_chapter_download', array( $this, 'secure_chapter_download' ) );
16
+ add_action( 'admin_post_nopriv_secure_chapter_download', array( $this, 'secure_chapter_download' ) );
17
+ }
18
+
19
+ private function get_active_languages() {
20
+ return array_keys( Languages::for_type( 'audiobook' ) );
21
+ }
22
+
23
+ private function normalize_lang( $code ) {
24
+ return strtolower( substr( trim( $code ), 0, 2 ) );
25
+ }
26
+
27
+ private function resolve_audio_url( $audio_url_data, $lang_code ) {
28
+ if ( empty( $audio_url_data ) ) {
29
+ return '';
30
+ }
31
+
32
+ if ( is_string( $audio_url_data ) ) {
33
+ $site_default = $this->normalize_lang( get_locale() );
34
+ if ( $this->normalize_lang( $lang_code ) === $site_default ) {
35
+ return $audio_url_data;
36
+ }
37
+ return '';
38
+ }
39
+
40
+ if ( ! is_array( $audio_url_data ) ) {
41
+ return '';
42
+ }
43
+
44
+ if ( isset( $audio_url_data[ $lang_code ] ) && $audio_url_data[ $lang_code ] !== '' ) {
45
+ return $audio_url_data[ $lang_code ];
46
+ }
47
+
48
+ $requested_short = $this->normalize_lang( $lang_code );
49
+ foreach ( $audio_url_data as $stored_key => $url ) {
50
+ if ( empty( $url ) ) {
51
+ continue;
52
+ }
53
+ if ( $this->normalize_lang( $stored_key ) === $requested_short ) {
54
+ return $url;
55
+ }
56
+ }
57
+
58
+ return '';
59
+ }
60
+
61
+ public function add_chapters_metabox() {
62
+ add_meta_box(
63
+ 'audiobook_chapters',
64
+ __( 'Chapters' ),
65
+ array( $this, 'chapters_metabox_callback' ),
66
+ 'audiobook',
67
+ 'normal',
68
+ 'high'
69
+ );
70
+ }
71
+
72
+ public function chapters_metabox_callback( $post ) {
73
+ wp_nonce_field( 'audiobook_chapters_nonce', 'chapters_nonce' );
74
+
75
+ $chapters = get_post_meta( $post->ID, '_audiobook_chapters', true );
76
+ if ( ! is_array( $chapters ) ) {
77
+ $chapters = array();
78
+ }
79
+ ?>
80
+ <div id="audiobook-chapters-container">
81
+ <div id="chapters-list">
82
+ <?php foreach ( $chapters as $index => $chapter ) : ?>
83
+ <?php $this->render_chapter_row( $index, $chapter ); ?>
84
+ <?php endforeach; ?>
85
+ </div>
86
+
87
+ <button type="button" id="add-chapter" class="button button-primary">
88
+ <?php _e( 'Add Chapter' ); ?>
89
+ </button>
90
+ </div>
91
+
92
+ <script>
93
+ jQuery(document).ready(function($) {
94
+ function getNextIndex() {
95
+ return $('#chapters-list .chapter-row').length;
96
+ }
97
+
98
+ function reindexChapters() {
99
+ $('#chapters-list .chapter-row').each(function(newIndex) {
100
+ const $row = $(this);
101
+
102
+ $row.find('h4').contents().filter(function() {
103
+ return this.nodeType === 3;
104
+ }).first().replaceWith('Chapter ' + (newIndex + 1) + ' ');
105
+
106
+ $row.find('[name]').each(function() {
107
+ const $el = $(this);
108
+ const name = $el.attr('name');
109
+ if (name) {
110
+ $el.attr('name', name.replace(/chapters\[\d+\]/, 'chapters[' + newIndex + ']'));
111
+ }
112
+ });
113
+
114
+ $row.find('[data-index]').attr('data-index', newIndex);
115
+ $row.find('.file-action').attr('data-index', newIndex);
116
+ });
117
+ }
118
+
119
+ $('#add-chapter').click(function() {
120
+ const chapterIndex = getNextIndex();
121
+
122
+ const template = `
123
+ <div class="chapter-row">
124
+ <h4>
125
+ Chapter ${chapterIndex + 1}
126
+ <span class="remove-chapter dashicons dashicons-trash" title="Remove Chapter"></span>
127
+ </h4>
128
+ <div class="chapter-content open">
129
+ <div class="chapter-field">
130
+ <label>Title:</label>
131
+ <input type="text" name="chapters[${chapterIndex}][title]" />
132
+ </div>
133
+ <div class="chapter-field">
134
+ <label>Short Description:</label>
135
+ <textarea name="chapters[${chapterIndex}][description]" rows="3"></textarea>
136
+ </div>
137
+ <div class="chapter-field">
138
+ <label>
139
+ <input type="checkbox"
140
+ name="chapters[${chapterIndex}][allow_download]"
141
+ value="1" />
142
+ Enable Download for this Chapter
143
+ </label>
144
+ </div>
145
+ <div class="chapter-field">
146
+ <label>Audio Files:</label>
147
+ <?php
148
+ $languages = $this->get_active_languages();
149
+ foreach ( $languages as $lang_code ) :
150
+ ?>
151
+ <div style="margin-bottom:15px; border:1px solid #ddd; padding:10px; border-radius:4px;">
152
+ <strong><?php echo strtoupper( $lang_code ); ?></strong>
153
+ ${createAudioUploadArea(chapterIndex, '<?php echo esc_js( $lang_code ); ?>')}
154
+ <input type="hidden"
155
+ name="chapters[${chapterIndex}][audio_url][<?php echo esc_attr( $lang_code ); ?>]" />
156
+ <input type="hidden"
157
+ name="chapters[${chapterIndex}][duration][<?php echo esc_attr( $lang_code ); ?>]" />
158
+ </div>
159
+ <?php endforeach; ?>
160
+ </div>
161
+ </div>
162
+ </div>
163
+ `;
164
+
165
+ $('#chapters-list').append(template);
166
+
167
+ $(`.audio-upload-area[data-index="${chapterIndex}"]`).each(function() {
168
+ initializeUploadArea(chapterIndex, $(this).data('lang'));
169
+ });
170
+
171
+ reindexChapters();
172
+ });
173
+
174
+ function createAudioUploadArea(index, lang) {
175
+ return `
176
+ <div class="audio-upload-area" data-index="${index}" data-lang="${lang}">
177
+ <div class="upload-content">
178
+ <p>Drag and drop your audio file here or</p>
179
+ <button type="button" class="upload-button">Choose File</button>
180
+ <input type="file" class="chapter-audio-upload" accept="audio/*" style="display:none;"
181
+ data-index="${index}" data-lang="${lang}" />
182
+ <p><small>Supported formats: MP3, WAV, OGG, M4A</small></p>
183
+ </div>
184
+ <div class="upload-progress" style="display:none;">
185
+ <div class="upload-progress-bar">0%</div>
186
+ </div>
187
+ <div class="upload-status"></div>
188
+ <div class="file-preview" style="display:none;"></div>
189
+ </div>
190
+ `;
191
+ }
192
+
193
+ function initializeUploadArea(index, lang) {
194
+ const $area = $(`.audio-upload-area[data-index="${index}"][data-lang="${lang}"]`);
195
+ const $fileInput = $area.find('.chapter-audio-upload');
196
+ const $uploadButton = $area.find('.upload-button');
197
+
198
+ $area.on('dragover dragenter', function(e) {
199
+ e.preventDefault();
200
+ $(this).addClass('dragover');
201
+ });
202
+
203
+ $area.on('dragleave dragend drop', function(e) {
204
+ e.preventDefault();
205
+ $(this).removeClass('dragover');
206
+ });
207
+
208
+ $area.on('drop', function(e) {
209
+ e.preventDefault();
210
+ const files = e.originalEvent.dataTransfer.files;
211
+ if (files.length > 0) {
212
+ handleFileUpload(files[0], $(this).data('index'), $(this).data('lang'));
213
+ }
214
+ });
215
+
216
+ $uploadButton.click(function() {
217
+ $fileInput.click();
218
+ });
219
+
220
+ $fileInput.change(function() {
221
+ if (this.files[0]) {
222
+ handleFileUpload(this.files[0], $(this).data('index'), $(this).data('lang'));
223
+ }
224
+ });
225
+ }
226
+
227
+ function captureDuration( audioEl ) {
228
+ const $area = $( audioEl ).closest( '.audio-upload-area' );
229
+ const lang = $area.data( 'lang' );
230
+ const index = $area.data( 'index' );
231
+ const secs = Math.round( audioEl.duration );
232
+ if ( secs > 0 ) {
233
+ $( `input[name="chapters[${index}][duration][${lang}]"]` ).val( secs );
234
+ }
235
+ }
236
+
237
+ function handleFileUpload(file, index, lang) {
238
+ const $area = $(`.audio-upload-area[data-index="${index}"][data-lang="${lang}"]`);
239
+ const $progress = $area.find('.upload-progress');
240
+ const $progressBar = $area.find('.upload-progress-bar');
241
+ const $status = $area.find('.upload-status');
242
+ const $preview = $area.find('.file-preview');
243
+ const $uploadButton = $area.find('.upload-button');
244
+
245
+ const allowedTypes = [
246
+ 'audio/mpeg', 'audio/wav', 'audio/ogg',
247
+ 'audio/mp4', 'audio/x-m4a', 'audio/m4a',
248
+ ];
249
+ if ( ! allowedTypes.includes(file.type) ) {
250
+ showStatus($status, 'error', 'Invalid file type. Please select an audio file.');
251
+ return;
252
+ }
253
+
254
+ if ( file.size > 50 * 1024 * 1024 ) {
255
+ showStatus($status, 'error', 'File too large. Maximum size is 50MB.');
256
+ return;
257
+ }
258
+
259
+ const formData = new FormData();
260
+ formData.append('action', 'upload_chapter_audio');
261
+ formData.append('audio_file', file);
262
+ formData.append('nonce', '<?php echo wp_create_nonce( "chapter_audio_upload" ); ?>');
263
+
264
+ $progress.show();
265
+ $uploadButton.prop('disabled', true);
266
+ showStatus($status, 'uploading', 'Uploading...');
267
+
268
+ $.ajax({
269
+ url: ajaxurl,
270
+ type: 'POST',
271
+ data: formData,
272
+ processData: false,
273
+ contentType: false,
274
+ xhr: function() {
275
+ const xhr = new window.XMLHttpRequest();
276
+ xhr.upload.addEventListener('progress', function(evt) {
277
+ if (evt.lengthComputable) {
278
+ const pct = Math.round((evt.loaded / evt.total) * 100);
279
+ $progressBar.css('width', pct + '%').text(pct + '%');
280
+ }
281
+ }, false);
282
+ return xhr;
283
+ },
284
+ success: function(response) {
285
+ if (response.success) {
286
+ const currentLang = $area.data('lang');
287
+ const currentIndex = $area.data('index');
288
+
289
+ $(`input[name="chapters[${currentIndex}][audio_url][${currentLang}]"]`)
290
+ .val(response.data.url);
291
+
292
+ showFilePreview($preview, file.name, response.data.url, currentIndex, currentLang);
293
+
294
+ $area.addClass('has-file');
295
+ $area.find('.upload-content').hide();
296
+
297
+ showStatus($status, 'success', 'Upload successful!');
298
+ setTimeout(() => $status.fadeOut(), 3000);
299
+ } else {
300
+ showStatus($status, 'error', 'Upload failed: ' + response.data);
301
+ }
302
+ },
303
+ error: function(xhr, status, error) {
304
+ showStatus($status, 'error', 'Upload error: ' + error);
305
+ },
306
+ complete: function() {
307
+ $progress.hide();
308
+ $progressBar.css('width', '0%').text('0%');
309
+ $uploadButton.prop('disabled', false);
310
+ }
311
+ });
312
+ }
313
+
314
+ function showFilePreview($preview, fileName, fileUrl, index, lang) {
315
+ $preview.html(`
316
+ <div class="file-info">
317
+ <span class="file-name">${fileName}</span>
318
+ <div class="file-actions">
319
+ <button type="button" class="file-action replace" data-index="${index}" data-lang="${lang}">Replace</button>
320
+ <button type="button" class="file-action remove" data-index="${index}" data-lang="${lang}">Remove</button>
321
+ </div>
322
+ </div>
323
+ <audio controls style="width:100%; margin-top:10px;">
324
+ <source src="${fileUrl}" type="audio/mpeg">
325
+ Your browser does not support the audio element.
326
+ </audio>
327
+ `).show();
328
+
329
+ const audioEl = $preview.find('audio')[0];
330
+ if ( audioEl.readyState >= 1 ) {
331
+ captureDuration( audioEl );
332
+ } else {
333
+ audioEl.addEventListener( 'loadedmetadata', function() {
334
+ captureDuration( audioEl );
335
+ }, { once: true } );
336
+ }
337
+ }
338
+
339
+ function showStatus($status, type, message) {
340
+ $status.removeClass('success error uploading')
341
+ .addClass(type)
342
+ .text(message)
343
+ .show();
344
+ }
345
+
346
+ function deleteFileFromServer(fileUrl) {
347
+ $.ajax({
348
+ url: ajaxurl,
349
+ type: 'POST',
350
+ data: {
351
+ action: 'delete_chapter_audio',
352
+ file_url: fileUrl,
353
+ nonce: '<?php echo wp_create_nonce( "chapter_audio_delete" ); ?>'
354
+ },
355
+ success: function(response) {
356
+ if ( ! response.success) {
357
+ console.error('Delete failed:', response.data);
358
+ alert('Delete failed: ' + response.data);
359
+ }
360
+ },
361
+ error: function(xhr, status, error) {
362
+ console.error('AJAX error:', error);
363
+ alert('Delete request failed.');
364
+ }
365
+ });
366
+ }
367
+
368
+ $('.audio-upload-area').each(function() {
369
+ initializeUploadArea($(this).data('index'), $(this).data('lang'));
370
+ });
371
+
372
+ $(document).on('click', '.file-action.replace', function() {
373
+ const index = $(this).data('index');
374
+ const lang = $(this).data('lang');
375
+ $(`.audio-upload-area[data-index="${index}"][data-lang="${lang}"] .chapter-audio-upload`).click();
376
+ });
377
+
378
+ $(document).on('click', '.file-action.remove', function() {
379
+ const index = $(this).data('index');
380
+ const lang = $(this).data('lang');
381
+
382
+ if ( ! confirm('Are you sure you want to remove this audio file?')) {
383
+ return;
384
+ }
385
+
386
+ const $hiddenUrl = $(`input[name="chapters[${index}][audio_url][${lang}]"]`);
387
+ const $hiddenDuration = $(`input[name="chapters[${index}][duration][${lang}]"]`);
388
+ const fileUrl = $hiddenUrl.val();
389
+
390
+ deleteFileFromServer(fileUrl);
391
+
392
+ const $area = $(`.audio-upload-area[data-index="${index}"][data-lang="${lang}"]`);
393
+ $area.removeClass('has-file');
394
+ $area.find('.file-preview').hide().empty();
395
+ $area.find('.upload-status').hide();
396
+ $area.find('.upload-content').show();
397
+ $hiddenUrl.val('');
398
+ $hiddenDuration.val('');
399
+ });
400
+
401
+ $(document).on('click', '.chapter-row h4', function(e) {
402
+ if ( ! $(e.target).hasClass('remove-chapter')) {
403
+ $(this).next('.chapter-content').toggleClass('open');
404
+ }
405
+ });
406
+
407
+ $(document).on('click', '.remove-chapter', function(e) {
408
+ e.stopPropagation();
409
+ if (confirm('Are you sure you want to remove this chapter?')) {
410
+ $(this).closest('.chapter-row').remove();
411
+ }
412
+ });
413
+ });
414
+ </script>
415
+ <?php
416
+ }
417
+
418
+ private function render_chapter_row( $index, $chapter ) {
419
+ ?>
420
+ <div class="chapter-row">
421
+ <h4>
422
+ Chapter <?php echo $index + 1; ?>
423
+ <span class="remove-chapter dashicons dashicons-trash" title="Remove Chapter"></span>
424
+ </h4>
425
+ <div class="chapter-content">
426
+
427
+ <div class="chapter-field">
428
+ <label><?php _e( 'Title:' ); ?></label>
429
+ <input type="text"
430
+ name="chapters[<?php echo $index; ?>][title]"
431
+ value="<?php echo esc_attr( $chapter['title'] ?? '' ); ?>" />
432
+ </div>
433
+
434
+ <div class="chapter-field">
435
+ <label><?php _e( 'Short Description:' ); ?></label>
436
+ <textarea name="chapters[<?php echo $index; ?>][description]" rows="3"><?php
437
+ echo esc_textarea( $chapter['description'] ?? '' );
438
+ ?></textarea>
439
+ </div>
440
+
441
+ <div class="chapter-field">
442
+ <label>
443
+ <input type="checkbox"
444
+ name="chapters[<?php echo $index; ?>][allow_download]"
445
+ value="1"
446
+ <?php checked( ! empty( $chapter['allow_download'] ) ); ?> />
447
+ Enable Download for this Chapter
448
+ </label>
449
+ </div>
450
+
451
+ <div class="chapter-field">
452
+ <label><?php _e( 'Audio Files:' ); ?></label>
453
+
454
+ <?php
455
+ $languages = $this->get_active_languages();
456
+ foreach ( $languages as $lang_code ) :
457
+ $audio_url = $this->resolve_audio_url( $chapter['audio_url'] ?? '', $lang_code );
458
+ $duration = isset( $chapter['duration'][ $lang_code ] )
459
+ ? intval( $chapter['duration'][ $lang_code ] )
460
+ : 0;
461
+ ?>
462
+ <div style="margin-bottom:20px; border:1px solid #ddd; padding:10px; border-radius:4px;">
463
+ <strong><?php echo strtoupper( $lang_code ); ?></strong>
464
+
465
+ <div class="audio-upload-area <?php echo ! empty( $audio_url ) ? 'has-file' : ''; ?>"
466
+ data-index="<?php echo $index; ?>"
467
+ data-lang="<?php echo esc_attr( $lang_code ); ?>">
468
+
469
+ <div class="upload-content" <?php echo ! empty( $audio_url ) ? 'style="display:none;"' : ''; ?>>
470
+ <p>Upload <?php echo strtoupper( $lang_code ); ?> audio</p>
471
+ <button type="button" class="upload-button">Choose File</button>
472
+ <input type="file"
473
+ class="chapter-audio-upload"
474
+ accept="audio/*"
475
+ style="display:none;"
476
+ data-index="<?php echo $index; ?>"
477
+ data-lang="<?php echo esc_attr( $lang_code ); ?>" />
478
+ </div>
479
+
480
+ <div class="upload-progress" style="display:none;">
481
+ <div class="upload-progress-bar">0%</div>
482
+ </div>
483
+
484
+ <div class="upload-status"></div>
485
+
486
+ <?php if ( ! empty( $audio_url ) ) : ?>
487
+ <div class="file-preview">
488
+ <div class="file-info">
489
+ <span class="file-name"><?php echo esc_html( basename( $audio_url ) ); ?></span>
490
+ <div class="file-actions">
491
+ <button type="button" class="file-action replace"
492
+ data-index="<?php echo $index; ?>"
493
+ data-lang="<?php echo esc_attr( $lang_code ); ?>">Replace</button>
494
+ <button type="button" class="file-action remove"
495
+ data-index="<?php echo $index; ?>"
496
+ data-lang="<?php echo esc_attr( $lang_code ); ?>">Remove</button>
497
+ </div>
498
+ </div>
499
+ <audio controls style="width:100%; margin-top:10px;">
500
+ <source src="<?php echo esc_url( $audio_url ); ?>" type="audio/mpeg">
501
+ </audio>
502
+ </div>
503
+ <?php else : ?>
504
+ <div class="file-preview" style="display:none;"></div>
505
+ <?php endif; ?>
506
+ </div>
507
+
508
+ <input type="hidden"
509
+ name="chapters[<?php echo $index; ?>][audio_url][<?php echo esc_attr( $lang_code ); ?>]"
510
+ value="<?php echo esc_attr( $audio_url ); ?>"
511
+ class="audio-url-input" />
512
+
513
+ <input type="hidden"
514
+ name="chapters[<?php echo $index; ?>][duration][<?php echo esc_attr( $lang_code ); ?>]"
515
+ value="<?php echo esc_attr( $duration ); ?>"
516
+ class="audio-duration-input" />
517
+ </div>
518
+ <?php endforeach; ?>
519
+ </div>
520
+
521
+ </div>
522
+ </div>
523
+ <?php
524
+ }
525
+
526
+ public function save_chapters_meta( $post_id ) {
527
+ if ( get_post_type( $post_id ) !== 'audiobook' ) {
528
+ return;
529
+ }
530
+
531
+ if ( ! isset( $_POST['chapters_nonce'] ) ||
532
+ ! wp_verify_nonce( $_POST['chapters_nonce'], 'audiobook_chapters_nonce' ) ) {
533
+ return;
534
+ }
535
+
536
+ if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
537
+ return;
538
+ }
539
+
540
+ if ( ! current_user_can( 'edit_post', $post_id ) ) {
541
+ return;
542
+ }
543
+
544
+ if ( ! isset( $_POST['chapters'] ) ) {
545
+ return;
546
+ }
547
+
548
+ $chapters = array();
549
+
550
+ foreach ( $_POST['chapters'] as $chapter_data ) {
551
+ $title = isset( $chapter_data['title'] )
552
+ ? sanitize_text_field( $chapter_data['title'] )
553
+ : '';
554
+
555
+ $description = isset( $chapter_data['description'] )
556
+ ? sanitize_textarea_field( $chapter_data['description'] )
557
+ : '';
558
+
559
+ $audio_urls = array();
560
+ $has_audio = false;
561
+
562
+ if ( ! empty( $chapter_data['audio_url'] ) && is_array( $chapter_data['audio_url'] ) ) {
563
+ foreach ( $chapter_data['audio_url'] as $lang => $url ) {
564
+ $lang_key = $this->normalize_lang( $lang );
565
+ $clean_url = esc_url_raw( $url );
566
+ if ( ! empty( $clean_url ) ) {
567
+ $audio_urls[ $lang_key ] = $clean_url;
568
+ $has_audio = true;
569
+ }
570
+ }
571
+ }
572
+
573
+ $durations = array();
574
+ if ( ! empty( $chapter_data['duration'] ) && is_array( $chapter_data['duration'] ) ) {
575
+ foreach ( $chapter_data['duration'] as $lang => $seconds ) {
576
+ $lang_key = $this->normalize_lang( $lang );
577
+ $clean = absint( $seconds );
578
+ if ( $clean > 0 ) {
579
+ $durations[ $lang_key ] = $clean;
580
+ }
581
+ }
582
+ }
583
+
584
+ if ( empty( $title ) && ! $has_audio ) {
585
+ continue;
586
+ }
587
+
588
+ $chapters[] = array(
589
+ 'title' => $title,
590
+ 'description' => $description,
591
+ 'audio_url' => $audio_urls,
592
+ 'duration' => $durations,
593
+ 'allow_download' => ! empty( $chapter_data['allow_download'] ) ? 1 : 0,
594
+ );
595
+ }
596
+
597
+ update_post_meta( $post_id, '_audiobook_chapters', $chapters );
598
+ }
599
+
600
+
601
+ public function handle_audio_upload() {
602
+ check_ajax_referer( 'chapter_audio_upload', 'nonce' );
603
+ Files::upload_audio( 'audio_file', 'audiobooks' );
604
+ }
605
+
606
+ public function handle_audio_delete() {
607
+ check_ajax_referer( 'chapter_audio_delete', 'nonce' );
608
+ Files::delete_audio( 'audiobooks' );
609
+ }
610
+
611
+ public function secure_chapter_download() {
612
+ if ( ! is_user_logged_in() ) {
613
+ wp_redirect( wp_login_url() );
614
+ exit;
615
+ }
616
+
617
+ $post_id = isset( $_GET['book'] ) ? (int) $_GET['book'] : 0;
618
+ $index = isset( $_GET['chapter'] ) ? (int) $_GET['chapter'] : -1;
619
+ $lang = isset( $_GET['lang'] ) ? sanitize_text_field( wp_unslash( $_GET['lang'] ) ) : '';
620
+ $chapters = get_post_meta( $post_id, '_audiobook_chapters', true );
621
+ $chapter = is_array( $chapters ) && $index >= 0 ? ( $chapters[ $index ] ?? null ) : null;
622
+
623
+ if ( ! $chapter || empty( $chapter['allow_download'] ) || empty( $chapter['audio_url'] ) || ! is_array( $chapter['audio_url'] ) ) {
624
+ wp_die( 'Download not allowed.' );
625
+ }
626
+
627
+ $short = strtolower( substr( $lang, 0, 2 ) );
628
+ $url = $chapter['audio_url'][ $lang ] ?? ( $chapter['audio_url'][ $short ] ?? reset( $chapter['audio_url'] ) );
629
+
630
+ Files::send( (string) $url );
631
+ }
632
+ }