-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-embed-tidal.php
256 lines (223 loc) · 6.62 KB
/
class-embed-tidal.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* Embed Tidal plugin.
*
* @package EmbedTidal
*/
/**
* The Embed_Tidal class.
*/
class Embed_Tidal {
/**
* Load the Tidal JavaScript only when a post needs it.
*
* @var bool
*/
public static $load_script = false;
/**
* Key => value array of the different supported embed types.
*
* @var array
*/
public static $embed_type = [];
/**
* Setup hooks. Register handlers etc.
*/
public static function hooks() {
self::$embed_type = [
'a' => __( 'Album', 'embed-tidal' ),
'p' => __( 'Playlist', 'embed-tidal' ),
't' => _x( 'Track', 'embed type', 'embed-tidal' ),
'v' => __( 'Video', 'embed-tidal' ),
];
add_shortcode( 'tidal', [ __CLASS__, 'shortcode_handler' ] );
wp_embed_register_handler( 'tidal', '/^https?:\/\/(listen\.)?tidal.com\/(playlist|track|album|video)\/(.*)$/', [ __CLASS__, 'embed_handler' ] );
add_action( 'wp_footer', [ __CLASS__, 'enqueue_scripts' ] );
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_scripts' ] );
add_action(
'plugins_loaded', function () {
load_plugin_textdomain( 'embed-tidal', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
);
/*
* Include a script for working with the shortcode UI
*/
add_action(
'enqueue_shortcode_ui', function() {
wp_enqueue_script( 'tidal-embed-shortcode-ui', plugin_dir_url( __FILE__ ) . 'tidal-embed-shortcode-ui.js', [], EMBED_TIDAL_VERSION, false );
}
);
add_action( 'register_shortcode_ui', [ __CLASS__, 'embed_tidal_shortcode_ui' ] );
}
/**
* Builds the content
*
* @since 0.1.2
*
* @param array $attr Provided attributes.
* @param string $content The shortcode content.
* @param string $shortcode_tag The shortcode tag.
* @return string The rendered HTML.
*/
public static function get_content( $attr, $content = null, $shortcode_tag = null ) {
if ( is_admin() ) {
return 'Tidal ' . esc_html( self::$embed_type[ $attr['type'] ] ) . ' Embed (#' . esc_html( $attr['id'] ) . ')';
}
$result = sprintf( '<div class="tidal-embed" data-type="%s" data-id="%s"', esc_attr( $attr['type'] ), esc_attr( $attr['id'] ) );
if ( 'v' === $attr['type'] && isset( $attr['related_id'] ) && $attr['related_id'] ) {
$result .= sprintf( ' data-related-id="%s"', esc_attr( $attr['related_id'] ) );
}
$result .= '></div>';
return $result;
}
/**
* Callback for the tidal shortcode.
*
* @since 0.0.1
*
* @param array $attr Provided attributes.
* @param string $content The shortcode content.
* @param string $shortcode_tag The shortcode tag.
* @return string The rendered HTML.
*/
public static function shortcode_handler( $attr, $content, $shortcode_tag ) {
self::$load_script = true;
$attr = shortcode_atts(
[
'type' => 'a',
'id' => null,
'related_id' => null,
], $attr, $shortcode_tag
);
/**
* Filters the tidal shortcode output.
*
* @since 0.0.1
*
* @param string $html Tidal shortcode HTML output.
* @param array $attr Array of tidal shortcode attributes.
*/
return apply_filters( 'tidal_shortcode', self::get_content( $attr, $content, $shortcode_tag ), $attr );
}
/**
* The custom embed handler for Tidal URLs
*
* @since 0.0.2
*
* @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
* @param array $attr Embed attributes.
* @param string $url The original URL that was matched by the regex.
* @param array $rawattr The original unmodified attributes.
* @return string The embed HTML (or the original URL if we can’t handle it after all).
*/
public static function embed_handler( $matches, $attr, $url, $rawattr ) {
self::$load_script = true;
$attr = [ 'id' => $matches[3] ];
switch ( $matches[2] ) {
case 'track':
$attr['type'] = 't';
break;
case 'playlist':
$attr['type'] = 'p';
break;
case 'video':
$attr['type'] = 'v';
break;
case 'album':
$attr['type'] = 'a';
break;
default:
return $url;
}
/**
* Filters the Tidal URL embed output.
*
* @since 0.0.2
*
* @param string $embed Embed HTML output.
* @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler().
* @param array $attr Embed attributes.
* @param string $url The original URL that was matched by the regex.
* @param array $rawattr The original unmodified attributes.
* @return string The embed HTML.
*/
return apply_filters( 'embed_tidal', self::get_content( $attr ), $matches, $attr, $url, $rawattr );
}
/**
* Enqueue the Tidal JavaScript
*
* @since 0.1.2
*/
public static function enqueue_scripts() {
if ( ! self::$load_script ) {
return;
}
wp_enqueue_script( 'embed-tidal', 'https://embed.tidal.com/tidal-embed.js', [], EMBED_TIDAL_VERSION, true );
}
/**
* Enqueue the Tidal JavaScript on admin pages
*
* @param string $hook The admin page hook.
*/
public static function admin_enqueue_scripts( $hook ) {
if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) {
return;
}
wp_enqueue_script( 'embed-tidal', 'https://embed.tidal.com/tidal-embed.js', [], EMBED_TIDAL_VERSION );
}
/**
* Shortcode UI setup for the tidal shortcode
*
* @since 0.0.1
*/
public static function embed_tidal_shortcode_ui() {
/*
* Define the UI for attributes of the shortcode.
*/
$fields = [
[
/* translators: Type of embed. I.e. album, playlist, track or video */
'label' => esc_html_x( 'Type', 'noun', 'embed-tidal' ),
'attr' => 'type',
'type' => 'select',
'options' => [
'a' => esc_html__( 'Album', 'embed-tidal' ),
'p' => esc_html__( 'Playlist', 'embed-tidal' ),
't' => esc_html_x( 'Track', 'embed type', 'embed-tidal' ),
'v' => esc_html__( 'Video', 'embed-tidal' ),
],
],
[
'label' => esc_html__( 'Tidal ID', 'embed-tidal' ),
'attr' => 'id',
'type' => 'text',
],
[
'label' => esc_html__( 'Tidal ID for related album', 'embed-tidal' ),
'attr' => 'related_id',
'type' => 'text',
],
];
/*
* Define the Shortcode UI arguments.
*/
$shortcode_ui_args = [
/*
* How the shortcode should be labeled in the UI.
*/
'label' => esc_html__( 'Embed from Tidal', 'embed-tidal' ),
/*
* Include an icon with your shortcode.
* Use a dashicon, or full URL to image.
*/
'listItemImage' => 'dashicons-format-audio',
/*
* Define the UI for attributes of the shortcode.
*
* See above, to where the the assignment to the $fields variable was made.
*/
'attrs' => $fields,
];
shortcode_ui_register_for_shortcode( 'tidal', $shortcode_ui_args );
}
}