-
Notifications
You must be signed in to change notification settings - Fork 2
/
wavethumb.php
82 lines (73 loc) · 2.43 KB
/
wavethumb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* WaveThumb
*
* @package WaveThumb
* @copyright Copyright (c) 2017, Cedaro, LLC
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: WaveThumb
* Plugin URI: https://github.com/cedaro/wavethumb
* Description: An experimental plugin demonstrating how to generate and save waveforms for audio attachments using the WordPress REST API.
* Version: 1.0.0
* Author: Cedaro
* Author URI: https://www.cedaro.com/
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wavethumb
* Domain Path: /languages
*/
/**
* Register a REST field for the waveform attachment.
*/
add_action( 'rest_api_init', function() {
register_rest_field( 'attachment', 'waveform', array(
'get_callback' => function( $post ) {
return get_post_meta( $post['id'], '_waveform_id', true );
},
'update_callback' => function( $value, $post ) {
update_post_meta( $post->ID, '_waveform_id', $value );
},
'schema' => array(
'description' => esc_html__( 'The id of the waveform for the audio file.', 'wavethumb' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
) );
} );
/**
* Enqueue admin assets.
*/
add_action( 'admin_enqueue_scripts', function() {
if ( 'attachment' !== get_current_screen()->id || 'audio/mpeg' !== get_post_mime_type() ) {
return;
}
wp_enqueue_style(
'wavethumb-edit-attachment',
plugins_url( '/admin/assets/css/admin.css', __FILE__ )
);
wp_enqueue_script(
'wavethumb-edit-attachment',
plugins_url( 'admin/assets/js/edit-attachment.bundle.js', __FILE__ ),
array( 'wp-api-request' ),
'1.0.0',
true
);
$attachment_id = get_post()->ID;
$file = get_attached_file( $attachment_id );
$waveform_filename = str_replace( '.', '-', basename( $file ) ) . '-waveform.png';
$waveform_filename = apply_filters( 'wavethumb_filename', $waveform_filename, $attachment_id );
$waveform_id = get_post_meta( $attachment_id, '_waveform_id', true );
$waveform_url = '';
if ( $waveform_id ) {
$waveform_url = wp_get_attachment_image_url( $waveform_id, 'full' );
}
wp_localize_script( 'wavethumb-edit-attachment', '_waveThumbSettings', array(
'attachmentId' => $attachment_id,
'attachmentUrl' => esc_url_raw( wp_get_attachment_url( $attachment_id ) ),
'waveformColor' => apply_filters( 'wavethumb_color', 'black' ),
'waveformFilename' => $waveform_filename,
'waveformUrl' => esc_url_raw( $waveform_url ),
) );
} );