@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.
- package/package.json +6 -2
- package/src/commands/platform.js +16 -2
- package/src/main.js +9 -3
- package/wp-operations/CHANGELOG.md +14 -0
- package/wp-operations/README.md +39 -0
- package/wp-operations/agents/builder/AGENT.md +15 -0
- package/wp-operations/assets/admin.css +631 -0
- package/wp-operations/assets/admin.js +614 -0
- package/wp-operations/assets/app/app.css +1 -0
- package/wp-operations/assets/app/app.js +70 -0
- package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
- package/wp-operations/assets/app/login.css +42 -0
- package/wp-operations/assets/brand/icon.svg +20 -0
- package/wp-operations/assets/brand/mark.svg +19 -0
- package/wp-operations/assets/editors/audiobook.css +311 -0
- package/wp-operations/assets/editors/audiofile.css +286 -0
- package/wp-operations/assets/editors/course.css +701 -0
- package/wp-operations/assets/editors/course.js +698 -0
- package/wp-operations/assets/editors/faq.css +258 -0
- package/wp-operations/assets/editors/faq.js +196 -0
- package/wp-operations/assets/editors/interactive.css +220 -0
- package/wp-operations/assets/editors/interactive.js +378 -0
- package/wp-operations/assets/editors/media-sources.css +3 -0
- package/wp-operations/assets/editors/media-sources.js +40 -0
- package/wp-operations/assets/editors/open-media.css +114 -0
- package/wp-operations/assets/editors/open-media.js +182 -0
- package/wp-operations/assets/editors/thumbnails.css +335 -0
- package/wp-operations/assets/editors/thumbnails.js +447 -0
- package/wp-operations/assets/editors/translator.css +818 -0
- package/wp-operations/functions.php +24 -0
- package/wp-operations/index.php +14 -0
- package/wp-operations/platform.json +1836 -0
- package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
- package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
- package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
- package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
- package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
- package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
- package/wp-operations/skills/wp-security/SKILL.md +90 -0
- package/wp-operations/src/Admin.php +78 -0
- package/wp-operations/src/App/Access.php +57 -0
- package/wp-operations/src/App/Admin.php +442 -0
- package/wp-operations/src/App/App.php +157 -0
- package/wp-operations/src/App/Apps.php +379 -0
- package/wp-operations/src/App/Bundle.php +111 -0
- package/wp-operations/src/App/Contents.php +676 -0
- package/wp-operations/src/App/Engine.php +554 -0
- package/wp-operations/src/App/Github.php +308 -0
- package/wp-operations/src/App/Module.php +23 -0
- package/wp-operations/src/App/Ops.php +470 -0
- package/wp-operations/src/App/Packages.php +289 -0
- package/wp-operations/src/App/Rest.php +539 -0
- package/wp-operations/src/App/Sandboxes.php +206 -0
- package/wp-operations/src/App/Store.php +104 -0
- package/wp-operations/src/App/Usage.php +268 -0
- package/wp-operations/src/Config.php +249 -0
- package/wp-operations/src/Content/Editor.php +223 -0
- package/wp-operations/src/Content/Fields.php +681 -0
- package/wp-operations/src/Content/Scoring.php +253 -0
- package/wp-operations/src/Content/Structure.php +293 -0
- package/wp-operations/src/Content/Types.php +150 -0
- package/wp-operations/src/Content/Uploads.php +50 -0
- package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
- package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
- package/wp-operations/src/Editors/AudiobookCover.php +235 -0
- package/wp-operations/src/Editors/Audiofile.php +462 -0
- package/wp-operations/src/Editors/Course.php +784 -0
- package/wp-operations/src/Editors/Ebook.php +242 -0
- package/wp-operations/src/Editors/Editors.php +44 -0
- package/wp-operations/src/Editors/Faq.php +237 -0
- package/wp-operations/src/Editors/Files.php +139 -0
- package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
- package/wp-operations/src/Editors/Interactive/Card.php +76 -0
- package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
- package/wp-operations/src/Editors/Interactive/Field.php +272 -0
- package/wp-operations/src/Editors/Interactive/Map.php +84 -0
- package/wp-operations/src/Editors/Interactive/Models.php +227 -0
- package/wp-operations/src/Editors/Interactive/Report.php +59 -0
- package/wp-operations/src/Editors/Interactive/Save.php +323 -0
- package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
- package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
- package/wp-operations/src/Editors/Legal.php +235 -0
- package/wp-operations/src/Editors/OpenMedia.php +140 -0
- package/wp-operations/src/Editors/OriginalTitle.php +112 -0
- package/wp-operations/src/Editors/Sources.php +28 -0
- package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
- package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
- package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
- package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
- package/wp-operations/src/Editors/Translator/Admin.php +913 -0
- package/wp-operations/src/Editors/Translator/Api.php +63 -0
- package/wp-operations/src/Editors/Translator/Bar.php +83 -0
- package/wp-operations/src/Editors/Translator/Fields.php +273 -0
- package/wp-operations/src/Editors/Translator/Storage.php +131 -0
- package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
- package/wp-operations/src/Identity.php +104 -0
- package/wp-operations/src/Languages.php +45 -0
- package/wp-operations/src/Lockdown.php +85 -0
- package/wp-operations/src/Modules/Apps/Admin.php +472 -0
- package/wp-operations/src/Modules/Apps/Api.php +193 -0
- package/wp-operations/src/Modules/Apps/Apps.php +279 -0
- package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
- package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
- package/wp-operations/src/Modules/Apps/Table.php +101 -0
- package/wp-operations/src/Modules/Thumbnails.php +68 -0
- package/wp-operations/src/Modules/Translations.php +133 -0
- package/wp-operations/src/Platform.php +59 -0
- package/wp-operations/src/Setup.php +273 -0
- package/wp-operations/src/Status.php +99 -0
- package/wp-operations/src/Storage/Client.php +168 -0
- package/wp-operations/src/Storage/Offload.php +120 -0
- package/wp-operations/src/Storage/Settings.php +166 -0
- package/wp-operations/src/Storage/Storage.php +35 -0
- package/wp-operations/src/Updates.php +151 -0
- package/wp-operations/style.css +11 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors;
|
|
3
|
+
|
|
4
|
+
use Platform\Languages;
|
|
5
|
+
use Platform\Platform;
|
|
6
|
+
|
|
7
|
+
defined( 'ABSPATH' ) || exit;
|
|
8
|
+
|
|
9
|
+
final class AudiobookCover {
|
|
10
|
+
|
|
11
|
+
public function __construct() {
|
|
12
|
+
add_action( 'add_meta_boxes', function() {
|
|
13
|
+
remove_meta_box( 'postimagediv', 'audiobook', 'side' );
|
|
14
|
+
}, 20 );
|
|
15
|
+
|
|
16
|
+
add_action( 'add_meta_boxes', array( $this, 'register_metabox' ) );
|
|
17
|
+
add_action( 'save_post_audiobook', array( $this, 'save_meta' ) );
|
|
18
|
+
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private function get_languages() {
|
|
22
|
+
return array_keys( Languages::for_type( 'audiobook' ) );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private function meta_key( $lang_code ) {
|
|
26
|
+
return '_audiobook_cover_image_' . sanitize_key( $lang_code );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public function enqueue_assets( $hook ) {
|
|
30
|
+
if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) return;
|
|
31
|
+
if ( get_post_type() !== 'audiobook' ) return;
|
|
32
|
+
|
|
33
|
+
wp_enqueue_media();
|
|
34
|
+
wp_enqueue_style(
|
|
35
|
+
'audiobook-cover-image',
|
|
36
|
+
Platform::url( 'assets/editors/audiobook.css' ),
|
|
37
|
+
array(),
|
|
38
|
+
Platform::asset_version( 'assets/editors/audiobook.css' )
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public function register_metabox() {
|
|
43
|
+
add_meta_box(
|
|
44
|
+
'audiobook_cover_image',
|
|
45
|
+
__( 'Cover Image' ),
|
|
46
|
+
array( $this, 'render_metabox' ),
|
|
47
|
+
'audiobook',
|
|
48
|
+
'side',
|
|
49
|
+
'default'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public function render_metabox( $post ) {
|
|
54
|
+
wp_nonce_field( 'audiobook_cover_image_save', 'audiobook_cover_image_nonce' );
|
|
55
|
+
|
|
56
|
+
$languages = $this->get_languages();
|
|
57
|
+
|
|
58
|
+
if ( empty( $languages ) ) {
|
|
59
|
+
echo '<p>' . esc_html__( 'No languages configured. Add languages in platform.json.' ) . '</p>';
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
foreach ( $languages as $lang_code ) :
|
|
64
|
+
$attachment_id = (int) get_post_meta( $post->ID, $this->meta_key( $lang_code ), true );
|
|
65
|
+
$img_url = $attachment_id ? wp_get_attachment_image_url( $attachment_id, 'medium' ) : '';
|
|
66
|
+
?>
|
|
67
|
+
|
|
68
|
+
<div class="aci-cover-block" style="margin-bottom:16px; padding-bottom:16px; border-bottom:1px solid #ddd;">
|
|
69
|
+
|
|
70
|
+
<strong style="display:block; margin-bottom:8px; font-size:12px; text-transform:uppercase; letter-spacing:.5px;">
|
|
71
|
+
<?php
|
|
72
|
+
printf(
|
|
73
|
+
|
|
74
|
+
esc_html__( 'Cover — %s' ),
|
|
75
|
+
esc_html( strtoupper( $lang_code ) )
|
|
76
|
+
);
|
|
77
|
+
?>
|
|
78
|
+
</strong>
|
|
79
|
+
|
|
80
|
+
<div class="aci-preview" <?php echo $img_url ? '' : 'style="display:none;"'; ?>>
|
|
81
|
+
<img src="<?php echo esc_url( $img_url ); ?>"
|
|
82
|
+
alt=""
|
|
83
|
+
style="width:100%; max-width:200px; display:block; border:1px solid #ddd;
|
|
84
|
+
border-radius:4px; margin-bottom:8px; object-fit:cover;" />
|
|
85
|
+
</div>
|
|
86
|
+
|
|
87
|
+
<div class="aci-actions">
|
|
88
|
+
<button type="button"
|
|
89
|
+
class="button aci-upload-btn"
|
|
90
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>"
|
|
91
|
+
<?php echo $img_url ? 'style="display:none;"' : ''; ?>>
|
|
92
|
+
<?php esc_html_e( 'Set Cover Image' ); ?>
|
|
93
|
+
</button>
|
|
94
|
+
|
|
95
|
+
<button type="button"
|
|
96
|
+
class="button aci-change-btn"
|
|
97
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>"
|
|
98
|
+
<?php echo $img_url ? '' : 'style="display:none;"'; ?>>
|
|
99
|
+
<?php esc_html_e( 'Change Image' ); ?>
|
|
100
|
+
</button>
|
|
101
|
+
|
|
102
|
+
<button type="button"
|
|
103
|
+
class="button aci-remove-btn"
|
|
104
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>"
|
|
105
|
+
style="color:#b32d2e; <?php echo $img_url ? '' : 'display:none;'; ?>">
|
|
106
|
+
<?php esc_html_e( 'Remove' ); ?>
|
|
107
|
+
</button>
|
|
108
|
+
</div>
|
|
109
|
+
|
|
110
|
+
<input type="hidden"
|
|
111
|
+
name="audiobook_cover_image[<?php echo esc_attr( $lang_code ); ?>]"
|
|
112
|
+
class="aci-id-input"
|
|
113
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>"
|
|
114
|
+
value="<?php echo esc_attr( $attachment_id ?: '' ); ?>" />
|
|
115
|
+
|
|
116
|
+
</div>
|
|
117
|
+
|
|
118
|
+
<?php endforeach;
|
|
119
|
+
|
|
120
|
+
$this->render_js();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private function render_js() {
|
|
124
|
+
?>
|
|
125
|
+
<script>
|
|
126
|
+
jQuery(function($) {
|
|
127
|
+
function openFrame(lang, $block) {
|
|
128
|
+
var frame = wp.media({
|
|
129
|
+
title: '<?php echo esc_js( __( 'Select Cover Image' ) ); ?>',
|
|
130
|
+
button: { text: '<?php echo esc_js( __( 'Use this image' ) ); ?>' },
|
|
131
|
+
multiple: false,
|
|
132
|
+
library: { type: 'image' }
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
frame.on('select', function() {
|
|
136
|
+
var attachment = frame.state().get('selection').first().toJSON();
|
|
137
|
+
var thumb = attachment.sizes && attachment.sizes.medium
|
|
138
|
+
? attachment.sizes.medium.url
|
|
139
|
+
: attachment.url;
|
|
140
|
+
|
|
141
|
+
$block.find('.aci-id-input').val(attachment.id);
|
|
142
|
+
$block.find('.aci-preview img').attr('src', thumb);
|
|
143
|
+
$block.find('.aci-preview').show();
|
|
144
|
+
$block.find('.aci-upload-btn').hide();
|
|
145
|
+
$block.find('.aci-change-btn').show();
|
|
146
|
+
$block.find('.aci-remove-btn').show();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
frame.open();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
$(document).on('click', '.aci-upload-btn, .aci-change-btn', function() {
|
|
153
|
+
var lang = $(this).data('lang');
|
|
154
|
+
var $block = $(this).closest('.aci-cover-block');
|
|
155
|
+
openFrame(lang, $block);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
$(document).on('click', '.aci-remove-btn', function() {
|
|
159
|
+
var $block = $(this).closest('.aci-cover-block');
|
|
160
|
+
$block.find('.aci-id-input').val('');
|
|
161
|
+
$block.find('.aci-preview').hide();
|
|
162
|
+
$block.find('.aci-preview img').attr('src', '');
|
|
163
|
+
$block.find('.aci-upload-btn').show();
|
|
164
|
+
$block.find('.aci-change-btn').hide();
|
|
165
|
+
$block.find('.aci-remove-btn').hide();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
</script>
|
|
169
|
+
<?php
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public function save_meta( $post_id ) {
|
|
173
|
+
if ( ! isset( $_POST['audiobook_cover_image_nonce'] ) ||
|
|
174
|
+
! wp_verify_nonce( $_POST['audiobook_cover_image_nonce'], 'audiobook_cover_image_save' ) ) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if ( empty( $_POST['audiobook_cover_image'] ) || ! is_array( $_POST['audiobook_cover_image'] ) ) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
foreach ( $_POST['audiobook_cover_image'] as $lang_code => $attachment_id ) {
|
|
191
|
+
$lang_code = sanitize_key( $lang_code );
|
|
192
|
+
$attachment_id = absint( $attachment_id );
|
|
193
|
+
|
|
194
|
+
if ( $attachment_id ) {
|
|
195
|
+
update_post_meta( $post_id, $this->meta_key( $lang_code ), $attachment_id );
|
|
196
|
+
} else {
|
|
197
|
+
delete_post_meta( $post_id, $this->meta_key( $lang_code ) );
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
public static function get_cover_url( $post_id, $lang_code = '', $size = 'full' ) {
|
|
203
|
+
$post_id = absint( $post_id );
|
|
204
|
+
if ( ! $post_id ) return '';
|
|
205
|
+
|
|
206
|
+
if ( empty( $lang_code ) ) {
|
|
207
|
+
$lang_code = get_locale();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
$short = strtolower( substr( trim( $lang_code ), 0, 2 ) );
|
|
211
|
+
$try_keys = array_unique( array(
|
|
212
|
+
'_audiobook_cover_image_' . sanitize_key( $lang_code ),
|
|
213
|
+
'_audiobook_cover_image_' . $short,
|
|
214
|
+
) );
|
|
215
|
+
|
|
216
|
+
foreach ( $try_keys as $key ) {
|
|
217
|
+
$id = (int) get_post_meta( $post_id, $key, true );
|
|
218
|
+
if ( $id ) {
|
|
219
|
+
$url = wp_get_attachment_image_url( $id, $size );
|
|
220
|
+
if ( $url ) return $url;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
$all_meta = get_post_meta( $post_id );
|
|
225
|
+
foreach ( $all_meta as $key => $values ) {
|
|
226
|
+
if ( strpos( $key, '_audiobook_cover_image_' ) === 0 && ! empty( $values[0] ) ) {
|
|
227
|
+
$url = wp_get_attachment_image_url( (int) $values[0], $size );
|
|
228
|
+
if ( $url ) return $url;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return '';
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
}
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors;
|
|
3
|
+
|
|
4
|
+
use Platform\Languages;
|
|
5
|
+
use Platform\Platform;
|
|
6
|
+
|
|
7
|
+
defined( 'ABSPATH' ) || exit;
|
|
8
|
+
|
|
9
|
+
final class Audiofile {
|
|
10
|
+
|
|
11
|
+
public function __construct() {
|
|
12
|
+
|
|
13
|
+
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
|
|
14
|
+
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
|
|
15
|
+
add_action( 'save_post', array( $this, 'save_meta' ) );
|
|
16
|
+
add_action( 'wp_ajax_upload_audiofile_audio', array( $this, 'handle_audio_upload' ) );
|
|
17
|
+
add_action( 'wp_ajax_delete_audiofile_audio', array( $this, 'handle_audio_delete' ) );
|
|
18
|
+
add_action( 'admin_post_secure_audiofile_download', array( $this, 'secure_audiofile_download' ) );
|
|
19
|
+
add_action( 'admin_post_nopriv_secure_audiofile_download', array( $this, 'secure_audiofile_download' ) );
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private function get_active_languages() {
|
|
23
|
+
return array_keys( Languages::for_type( 'audiofile' ) );
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private function normalize_lang( $code ) {
|
|
27
|
+
return strtolower( substr( trim( $code ), 0, 2 ) );
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private function resolve_audio_url( $audio_url_data, $lang_code ) {
|
|
31
|
+
if ( empty( $audio_url_data ) ) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if ( is_string( $audio_url_data ) ) {
|
|
36
|
+
$site_default = $this->normalize_lang( get_locale() );
|
|
37
|
+
if ( $this->normalize_lang( $lang_code ) === $site_default ) {
|
|
38
|
+
return $audio_url_data;
|
|
39
|
+
}
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ( ! is_array( $audio_url_data ) ) {
|
|
44
|
+
return '';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if ( isset( $audio_url_data[ $lang_code ] ) && $audio_url_data[ $lang_code ] !== '' ) {
|
|
48
|
+
return $audio_url_data[ $lang_code ];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
$requested_short = $this->normalize_lang( $lang_code );
|
|
52
|
+
foreach ( $audio_url_data as $stored_key => $url ) {
|
|
53
|
+
if ( empty( $url ) ) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if ( $this->normalize_lang( $stored_key ) === $requested_short ) {
|
|
57
|
+
return $url;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public function add_metabox() {
|
|
65
|
+
add_meta_box(
|
|
66
|
+
'audiofile_data',
|
|
67
|
+
__( 'Audio File Upload' ),
|
|
68
|
+
array( $this, 'metabox_callback' ),
|
|
69
|
+
'audiofile',
|
|
70
|
+
'normal',
|
|
71
|
+
'high'
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public function metabox_callback( $post ) {
|
|
76
|
+
wp_nonce_field( 'audiofile_nonce', 'audiofile_nonce_field' );
|
|
77
|
+
|
|
78
|
+
$data = get_post_meta( $post->ID, '_audiofile_data', true );
|
|
79
|
+
if ( ! is_array( $data ) ) {
|
|
80
|
+
$data = array();
|
|
81
|
+
}
|
|
82
|
+
?>
|
|
83
|
+
<div id="audiofile-container">
|
|
84
|
+
<div class="audiofile-field">
|
|
85
|
+
<label>
|
|
86
|
+
<input type="checkbox"
|
|
87
|
+
name="audiofile[allow_download]"
|
|
88
|
+
value="1"
|
|
89
|
+
<?php checked( ! empty( $data['allow_download'] ) ); ?> />
|
|
90
|
+
<?php _e( 'Enable Download for this Audio File' ); ?>
|
|
91
|
+
</label>
|
|
92
|
+
</div>
|
|
93
|
+
|
|
94
|
+
<div class="audiofile-field" style="margin-top: 20px;">
|
|
95
|
+
<label><?php _e( 'Audio Files by Language:' ); ?></label>
|
|
96
|
+
|
|
97
|
+
<?php
|
|
98
|
+
$languages = $this->get_active_languages();
|
|
99
|
+
foreach ( $languages as $lang_code ) :
|
|
100
|
+
$audio_url = $this->resolve_audio_url( $data['audio_url'] ?? '', $lang_code );
|
|
101
|
+
$duration = isset( $data['duration'][ $lang_code ] )
|
|
102
|
+
? intval( $data['duration'][ $lang_code ] )
|
|
103
|
+
: 0;
|
|
104
|
+
?>
|
|
105
|
+
<div style="margin-bottom:20px; border:1px solid #ddd; padding:15px; border-radius:4px; background: #fafafa;">
|
|
106
|
+
<strong style="display:block; margin-bottom: 10px; font-size: 14px;"><?php echo strtoupper( $lang_code ); ?> <?php _e( 'Audio' ); ?></strong>
|
|
107
|
+
|
|
108
|
+
<div class="audio-upload-area <?php echo ! empty( $audio_url ) ? 'has-file' : ''; ?>"
|
|
109
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>">
|
|
110
|
+
|
|
111
|
+
<div class="upload-content" <?php echo ! empty( $audio_url ) ? 'style="display:none;"' : ''; ?>>
|
|
112
|
+
<p><?php _e( 'Drag and drop your audio file here or' ); ?></p>
|
|
113
|
+
<button type="button" class="upload-button"><?php _e( 'Choose File' ); ?></button>
|
|
114
|
+
<input type="file"
|
|
115
|
+
class="audiofile-audio-upload"
|
|
116
|
+
accept="audio/*"
|
|
117
|
+
style="display:none;"
|
|
118
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>" />
|
|
119
|
+
</div>
|
|
120
|
+
|
|
121
|
+
<div class="upload-progress" style="display:none;">
|
|
122
|
+
<div class="upload-progress-bar">0%</div>
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
<div class="upload-status"></div>
|
|
126
|
+
|
|
127
|
+
<?php if ( ! empty( $audio_url ) ) : ?>
|
|
128
|
+
<div class="file-preview">
|
|
129
|
+
<div class="file-info">
|
|
130
|
+
<span class="file-name"><?php echo esc_html( basename( $audio_url ) ); ?></span>
|
|
131
|
+
<div class="file-actions">
|
|
132
|
+
<button type="button" class="file-action replace"
|
|
133
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>">Replace</button>
|
|
134
|
+
<button type="button" class="file-action remove"
|
|
135
|
+
data-lang="<?php echo esc_attr( $lang_code ); ?>">Remove</button>
|
|
136
|
+
</div>
|
|
137
|
+
</div>
|
|
138
|
+
<audio controls style="width:100%; margin-top:10px;">
|
|
139
|
+
<source src="<?php echo esc_url( $audio_url ); ?>" type="audio/mpeg">
|
|
140
|
+
</audio>
|
|
141
|
+
</div>
|
|
142
|
+
<?php else : ?>
|
|
143
|
+
<div class="file-preview" style="display:none;"></div>
|
|
144
|
+
<?php endif; ?>
|
|
145
|
+
</div>
|
|
146
|
+
|
|
147
|
+
<input type="hidden"
|
|
148
|
+
name="audiofile[audio_url][<?php echo esc_attr( $lang_code ); ?>]"
|
|
149
|
+
value="<?php echo esc_attr( $audio_url ); ?>"
|
|
150
|
+
class="audio-url-input" />
|
|
151
|
+
|
|
152
|
+
<input type="hidden"
|
|
153
|
+
name="audiofile[duration][<?php echo esc_attr( $lang_code ); ?>]"
|
|
154
|
+
value="<?php echo esc_attr( $duration ); ?>"
|
|
155
|
+
class="audio-duration-input" />
|
|
156
|
+
</div>
|
|
157
|
+
<?php endforeach; ?>
|
|
158
|
+
</div>
|
|
159
|
+
</div>
|
|
160
|
+
|
|
161
|
+
<script>
|
|
162
|
+
jQuery(document).ready(function($) {
|
|
163
|
+
function initializeUploadArea(lang) {
|
|
164
|
+
const $area = $(`.audio-upload-area[data-lang="${lang}"]`);
|
|
165
|
+
const $fileInput = $area.find('.audiofile-audio-upload');
|
|
166
|
+
const $uploadButton = $area.find('.upload-button');
|
|
167
|
+
|
|
168
|
+
$area.on('dragover dragenter', function(e) {
|
|
169
|
+
e.preventDefault();
|
|
170
|
+
$(this).addClass('dragover');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
$area.on('dragleave dragend drop', function(e) {
|
|
174
|
+
e.preventDefault();
|
|
175
|
+
$(this).removeClass('dragover');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
$area.on('drop', function(e) {
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
const files = e.originalEvent.dataTransfer.files;
|
|
181
|
+
if (files.length > 0) {
|
|
182
|
+
handleFileUpload(files[0], $(this).data('lang'));
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
$uploadButton.click(function() {
|
|
187
|
+
$fileInput.click();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
$fileInput.change(function() {
|
|
191
|
+
if (this.files[0]) {
|
|
192
|
+
handleFileUpload(this.files[0], $(this).data('lang'));
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function captureDuration( audioEl ) {
|
|
198
|
+
const $area = $( audioEl ).closest( '.audio-upload-area' );
|
|
199
|
+
const lang = $area.data( 'lang' );
|
|
200
|
+
const secs = Math.round( audioEl.duration );
|
|
201
|
+
if ( secs > 0 ) {
|
|
202
|
+
$( `input[name="audiofile[duration][${lang}]"]` ).val( secs );
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function handleFileUpload(file, lang) {
|
|
207
|
+
const $area = $(`.audio-upload-area[data-lang="${lang}"]`);
|
|
208
|
+
const $progress = $area.find('.upload-progress');
|
|
209
|
+
const $progressBar = $area.find('.upload-progress-bar');
|
|
210
|
+
const $status = $area.find('.upload-status');
|
|
211
|
+
const $preview = $area.find('.file-preview');
|
|
212
|
+
const $uploadButton = $area.find('.upload-button');
|
|
213
|
+
|
|
214
|
+
const allowedTypes = [
|
|
215
|
+
'audio/mpeg', 'audio/wav', 'audio/ogg',
|
|
216
|
+
'audio/mp4', 'audio/x-m4a', 'audio/m4a',
|
|
217
|
+
];
|
|
218
|
+
if ( ! allowedTypes.includes(file.type) ) {
|
|
219
|
+
showStatus($status, 'error', 'Invalid file type. Please select an audio file.');
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if ( file.size > 200 * 1024 * 1024 ) {
|
|
224
|
+
showStatus($status, 'error', 'File too large. Maximum size is 200MB.');
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const formData = new FormData();
|
|
229
|
+
formData.append('action', 'upload_audiofile_audio');
|
|
230
|
+
formData.append('audio_file', file);
|
|
231
|
+
formData.append('nonce', '<?php echo wp_create_nonce( "audiofile_audio_upload" ); ?>');
|
|
232
|
+
|
|
233
|
+
$progress.show();
|
|
234
|
+
$uploadButton.prop('disabled', true);
|
|
235
|
+
showStatus($status, 'uploading', 'Uploading...');
|
|
236
|
+
|
|
237
|
+
$.ajax({
|
|
238
|
+
url: ajaxurl,
|
|
239
|
+
type: 'POST',
|
|
240
|
+
data: formData,
|
|
241
|
+
processData: false,
|
|
242
|
+
contentType: false,
|
|
243
|
+
xhr: function() {
|
|
244
|
+
const xhr = new window.XMLHttpRequest();
|
|
245
|
+
xhr.upload.addEventListener('progress', function(evt) {
|
|
246
|
+
if (evt.lengthComputable) {
|
|
247
|
+
const pct = Math.round((evt.loaded / evt.total) * 100);
|
|
248
|
+
$progressBar.css('width', pct + '%').text(pct + '%');
|
|
249
|
+
}
|
|
250
|
+
}, false);
|
|
251
|
+
return xhr;
|
|
252
|
+
},
|
|
253
|
+
success: function(response) {
|
|
254
|
+
if (response.success) {
|
|
255
|
+
const currentLang = $area.data('lang');
|
|
256
|
+
|
|
257
|
+
$(`input[name="audiofile[audio_url][${currentLang}]"]`)
|
|
258
|
+
.val(response.data.url);
|
|
259
|
+
|
|
260
|
+
showFilePreview($preview, file.name, response.data.url, currentLang);
|
|
261
|
+
|
|
262
|
+
$area.addClass('has-file');
|
|
263
|
+
$area.find('.upload-content').hide();
|
|
264
|
+
|
|
265
|
+
showStatus($status, 'success', 'Upload successful!');
|
|
266
|
+
setTimeout(() => $status.fadeOut(), 3000);
|
|
267
|
+
} else {
|
|
268
|
+
showStatus($status, 'error', 'Upload failed: ' + response.data);
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
error: function(xhr, status, error) {
|
|
272
|
+
showStatus($status, 'error', 'Upload error: ' + error);
|
|
273
|
+
},
|
|
274
|
+
complete: function() {
|
|
275
|
+
$progress.hide();
|
|
276
|
+
$progressBar.css('width', '0%').text('0%');
|
|
277
|
+
$uploadButton.prop('disabled', false);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function showFilePreview($preview, fileName, fileUrl, lang) {
|
|
283
|
+
$preview.html(`
|
|
284
|
+
<div class="file-info">
|
|
285
|
+
<span class="file-name">${fileName}</span>
|
|
286
|
+
<div class="file-actions">
|
|
287
|
+
<button type="button" class="file-action replace" data-lang="${lang}">Replace</button>
|
|
288
|
+
<button type="button" class="file-action remove" data-lang="${lang}">Remove</button>
|
|
289
|
+
</div>
|
|
290
|
+
</div>
|
|
291
|
+
<audio controls style="width:100%; margin-top:10px;">
|
|
292
|
+
<source src="${fileUrl}" type="audio/mpeg">
|
|
293
|
+
Your browser does not support the audio element.
|
|
294
|
+
</audio>
|
|
295
|
+
`).show();
|
|
296
|
+
|
|
297
|
+
const audioEl = $preview.find('audio')[0];
|
|
298
|
+
if ( audioEl.readyState >= 1 ) {
|
|
299
|
+
captureDuration( audioEl );
|
|
300
|
+
} else {
|
|
301
|
+
audioEl.addEventListener( 'loadedmetadata', function() {
|
|
302
|
+
captureDuration( audioEl );
|
|
303
|
+
}, { once: true } );
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function showStatus($status, type, message) {
|
|
308
|
+
$status.removeClass('success error uploading')
|
|
309
|
+
.addClass(type)
|
|
310
|
+
.text(message)
|
|
311
|
+
.show();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function deleteFileFromServer(fileUrl) {
|
|
315
|
+
$.ajax({
|
|
316
|
+
url: ajaxurl,
|
|
317
|
+
type: 'POST',
|
|
318
|
+
data: {
|
|
319
|
+
action: 'delete_audiofile_audio',
|
|
320
|
+
file_url: fileUrl,
|
|
321
|
+
nonce: '<?php echo wp_create_nonce( "audiofile_audio_delete" ); ?>'
|
|
322
|
+
},
|
|
323
|
+
success: function(response) {
|
|
324
|
+
if ( ! response.success) {
|
|
325
|
+
console.error('Delete failed:', response.data);
|
|
326
|
+
alert('Delete failed: ' + response.data);
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
error: function(xhr, status, error) {
|
|
330
|
+
console.error('AJAX error:', error);
|
|
331
|
+
alert('Delete request failed.');
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
$('.audio-upload-area').each(function() {
|
|
337
|
+
initializeUploadArea($(this).data('lang'));
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
$(document).on('click', '.file-action.replace', function() {
|
|
341
|
+
const lang = $(this).data('lang');
|
|
342
|
+
$(`.audio-upload-area[data-lang="${lang}"] .audiofile-audio-upload`).click();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
$(document).on('click', '.file-action.remove', function() {
|
|
346
|
+
const lang = $(this).data('lang');
|
|
347
|
+
|
|
348
|
+
if ( ! confirm('Are you sure you want to remove this audio file?')) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const $hiddenUrl = $(`input[name="audiofile[audio_url][${lang}]"]`);
|
|
353
|
+
const $hiddenDuration = $(`input[name="audiofile[duration][${lang}]"]`);
|
|
354
|
+
const fileUrl = $hiddenUrl.val();
|
|
355
|
+
|
|
356
|
+
deleteFileFromServer(fileUrl);
|
|
357
|
+
|
|
358
|
+
const $area = $(`.audio-upload-area[data-lang="${lang}"]`);
|
|
359
|
+
$area.removeClass('has-file');
|
|
360
|
+
$area.find('.file-preview').hide().empty();
|
|
361
|
+
$area.find('.upload-status').hide();
|
|
362
|
+
$area.find('.upload-content').show();
|
|
363
|
+
$hiddenUrl.val('');
|
|
364
|
+
$hiddenDuration.val('');
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
</script>
|
|
368
|
+
<?php
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
public function save_meta( $post_id ) {
|
|
372
|
+
if ( get_post_type( $post_id ) !== 'audiofile' ) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if ( ! isset( $_POST['audiofile_nonce_field'] ) ||
|
|
377
|
+
! wp_verify_nonce( $_POST['audiofile_nonce_field'], 'audiofile_nonce' ) ) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if ( ! isset( $_POST['audiofile'] ) ) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
$input_data = $_POST['audiofile'];
|
|
394
|
+
$audio_urls = array();
|
|
395
|
+
$durations = array();
|
|
396
|
+
|
|
397
|
+
if ( ! empty( $input_data['audio_url'] ) && is_array( $input_data['audio_url'] ) ) {
|
|
398
|
+
foreach ( $input_data['audio_url'] as $lang => $url ) {
|
|
399
|
+
$lang_key = $this->normalize_lang( $lang );
|
|
400
|
+
$clean_url = esc_url_raw( $url );
|
|
401
|
+
if ( ! empty( $clean_url ) ) {
|
|
402
|
+
$audio_urls[ $lang_key ] = $clean_url;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if ( ! empty( $input_data['duration'] ) && is_array( $input_data['duration'] ) ) {
|
|
408
|
+
foreach ( $input_data['duration'] as $lang => $seconds ) {
|
|
409
|
+
$lang_key = $this->normalize_lang( $lang );
|
|
410
|
+
$clean = absint( $seconds );
|
|
411
|
+
if ( $clean > 0 ) {
|
|
412
|
+
$durations[ $lang_key ] = $clean;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
$data = array(
|
|
418
|
+
'audio_url' => $audio_urls,
|
|
419
|
+
'duration' => $durations,
|
|
420
|
+
'allow_download' => ! empty( $input_data['allow_download'] ) ? 1 : 0,
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
update_post_meta( $post_id, '_audiofile_data', $data );
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
public function handle_audio_upload() {
|
|
428
|
+
check_ajax_referer( 'audiofile_audio_upload', 'nonce' );
|
|
429
|
+
Files::upload_audio( 'audio_file', 'audiofiles' );
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
public function handle_audio_delete() {
|
|
433
|
+
check_ajax_referer( 'audiofile_audio_delete', 'nonce' );
|
|
434
|
+
Files::delete_audio( 'audiofiles' );
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
public function secure_audiofile_download() {
|
|
438
|
+
if ( ! is_user_logged_in() ) {
|
|
439
|
+
wp_redirect( wp_login_url() );
|
|
440
|
+
exit;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
$post_id = isset( $_GET['file_id'] ) ? (int) $_GET['file_id'] : 0;
|
|
444
|
+
$lang = isset( $_GET['lang'] ) ? sanitize_text_field( wp_unslash( $_GET['lang'] ) ) : '';
|
|
445
|
+
$data = get_post_meta( $post_id, '_audiofile_data', true );
|
|
446
|
+
|
|
447
|
+
if ( ! is_array( $data ) || empty( $data['allow_download'] ) || empty( $data['audio_url'] ) || ! is_array( $data['audio_url'] ) ) {
|
|
448
|
+
wp_die( 'Download not allowed.' );
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
$short = strtolower( substr( $lang, 0, 2 ) );
|
|
452
|
+
$url = $data['audio_url'][ $lang ] ?? ( $data['audio_url'][ $short ] ?? reset( $data['audio_url'] ) );
|
|
453
|
+
|
|
454
|
+
Files::send( (string) $url );
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
public function assets( $hook ) {
|
|
458
|
+
if ( in_array( $hook, [ 'post.php', 'post-new.php' ], true ) && 'audiofile' === get_post_type() ) {
|
|
459
|
+
wp_enqueue_style( 'audiofile-editor', Platform::url( 'assets/editors/audiofile.css' ), [], Platform::asset_version( 'assets/editors/audiofile.css' ) );
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|