-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.php
More file actions
410 lines (332 loc) · 15.7 KB
/
functions.php
File metadata and controls
410 lines (332 loc) · 15.7 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
define( 'SQUARECANDY_BYT_PATH', plugin_dir_path( __FILE__ ) );
define( 'SQUARECANDY_BYT_URL', plugin_dir_url( __FILE__ ) );
define( 'SQUARECANDY_BYT_VERSION', 'version-1.3.2' );
// Square Candy Common files
require_once SQUARECANDY_BYT_PATH . '/inc/sqcdy-common.php';
require_once SQUARECANDY_BYT_PATH . '/inc/sqcdy-plugin.php';
// Enqueue scripts required
if ( ! function_exists( 'squarecandy_video_scripts' ) ) :
function squarecandy_video_scripts() {
// apply fitvids
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'squarecandy-fitvids', SQUARECANDY_BYT_URL . 'dist/js/vendor/fitvids.min.js', array( 'jquery' ), SQUARECANDY_BYT_VERSION, true );
wp_enqueue_script( 'squarecandy-better-youtube', SQUARECANDY_BYT_URL . 'dist/js/better-youtube.min.js', array( 'jquery', 'squarecandy-fitvids' ), SQUARECANDY_BYT_VERSION, true );
wp_enqueue_script( 'squarecandy-magnific-popup', SQUARECANDY_BYT_URL . 'dist/js/vendor/jquery.magnific-popup.min.js', array( 'jquery' ), SQUARECANDY_BYT_VERSION, true );
wp_enqueue_style( 'squarecandy-magnific-popup-style', SQUARECANDY_BYT_URL . 'dist/css/vendor/magnific-popup.css', array(), SQUARECANDY_BYT_VERSION );
wp_enqueue_style( 'squarecandy-better-youtube-css', SQUARECANDY_BYT_URL . 'dist/css/better-youtube.min.css', array(), SQUARECANDY_BYT_VERSION );
$js_data = array(
'debug' => sqcdy_is_debug(),
);
// Pass the data as an inline script
wp_add_inline_script(
'squarecandy-better-youtube',
'window.betterYoutubeData = ' . wp_json_encode( $js_data ) . ';',
'before' // or 'after'
);
}
add_action( 'wp_enqueue_scripts', 'squarecandy_video_scripts' );
endif;
// Add styles to the TinyMCE editor window to make it look more like your site's front end
function squarecandy_better_youtube_tinymce_styles() {
add_editor_style( SQUARECANDY_BYT_URL . 'dist/css/better-youtube.min.css' );
}
add_action( 'admin_init', 'squarecandy_better_youtube_tinymce_styles' );
function squarecandy_video_admin_scripts() {
wp_enqueue_style( 'squarecandy-better-youtube-css', SQUARECANDY_BYT_URL . 'dist/css/better-youtube.min.css', array(), SQUARECANDY_BYT_VERSION );
}
add_action( 'admin_enqueue_scripts', 'squarecandy_video_admin_scripts' );
// override default oEmbed size
function squarecandy_own_embed_size() {
$width = (int) get_option( 'sqcdy_theme_colwidth', 620 ) - 20;
$height = 0.5625 * $width; // 16:9 aspect ratio
$settings = array(
'width' => $width,
'height' => $height,
);
return $settings;
}
add_filter( 'embed_defaults', 'squarecandy_own_embed_size' );
/*
* Uses regex turn a YT url into an embed url
* https://stackoverflow.com/questions/28894116/regex-youtube-url-for-embed-with-or-without-playlist
*/
function better_youtube_get_embed_url( $url ) {
$regex = '~^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((?:\w|-){11})(?:&list=(\S+))?$~';
preg_match( $regex, $url, $matches );
if ( $matches ) {
$new_url = 'https://www.youtube.com/embed/' . rawurlencode( $matches[1] );
return $new_url;
} else {
return false;
}
}
function better_youtube_get_youtube_iframe_from_embed( $embed ) {
// use preg_match to find iframe src
preg_match( '/src="(.+?)"/', $embed, $matches );
$src = isset( $matches[1] ) ? $matches[1] : null;
return $src;
}
function better_youtube_get_youtube_playlist_from_src( $src ) {
$playlist = $src ? explode( 'list=', $src ) : '';
if ( ! isset( $playlist[1] ) ) {
$playlist = false;
} else {
$playlist = explode( '&', $playlist[1] );
$playlist = $playlist[0];
}
return $playlist;
}
function better_youtube_get_large_youtube_thumbnail( $thumbnails ) {
if ( isset( $thumbnails->maxres->url ) ) {
$large_thumb = $thumbnails->maxres->url;
} elseif ( isset( $thumbnails->standard->url ) ) {
$large_thumb = $thumbnails->standard->url;
} elseif ( isset( $thumbnails->high->url ) ) {
$large_thumb = $thumbnails->high->url;
} elseif ( isset( $thumbnails->medium->url ) ) {
$large_thumb = $thumbnails->medium->url;
} else {
$large_thumb = false;
}
return $large_thumb;
}
/**
* Output embed url parameters as string or array
* @param $as_array bool opt default false
* @param $autoplay bool opt default true
*
* @return string|array
*/
function better_youtube_url_parameters( $as_array = false, $autoplay = true ) {
//previus parameters: '?feature=oembed&rel=0&controls=1&modestbranding=1&hd=1&autoplay=1'
$parameters = '?&mute=0&controls=1&playsinline=1&showinfo=0&rel=0&iv_load_policy=3&modestbranding=1';
$parameters .= '&enablejsapi=1'; // enable js interaction
if ( $autoplay ) {
$parameters .= '&autoplay=1';
} else {
$parameters .= '&autoplay=0';
}
if ( $as_array ) {
$output = array();
$parameters = trim( $parameters, '?' );
$parameters = explode( '&', $parameters );
foreach ( $parameters as $parameter ) {
$values = explode( '=', $parameter );
if ( isset( $values[0] ) && isset( $values[1] ) ) {
$output[ $values[0] ] = $values[1];
}
}
$parameters = $output;
}
return $parameters;
}
/**
* Construct a playlist element to be used with our YT js player
* @param string $input
*
* @return bool|string playlist html or false if error
*/
function better_youtube_api_playlist( $input ) {
$playlist = better_youtube_get_youtube_playlist_from_src( $input );
if ( ! $playlist || ! defined( 'YOUTUBE_API_KEY' ) ) {
return false;
}
// Google API for building custom YouTube Playlists
require_once SQUARECANDY_BYT_PATH . 'inc/vendor/autoload.php';
try {
$client = new Google_Client();
$client->setDeveloperKey( YOUTUBE_API_KEY );
$client->setScopes( 'https://www.googleapis.com/auth/youtube' );
$redirect = filter_var( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL );
$client->setRedirectUri( $redirect );
// Define an object that will be used to make all API requests.
$service = new Google_Service_YouTube( $client );
//get all items in the playlist via API
$params = array(
'maxResults' => 49,
'playlistId' => $playlist,
);
$params = array_filter( $params );
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$response = $service->playlistItems->listPlaylistItems( 'snippet', $params );
$large_thumb = better_youtube_get_large_youtube_thumbnail( $response->items[0]->snippet->thumbnails );
// data-post-id previously contained postid, but for embed in content that doesn't create unique data
// switching to playlist id here. will still get weird of they embed the same playlist twice...
$output = '<div id="playlist-' . $playlist . '" class="custom-api-playlist" data-playlist-id="' . $playlist . '" data-post-id="' . $playlist . '">';
$output .= '<div class="playlist-preview-first"><div id="player-' . $playlist . '" class="playlist-preview" data-video-index="0">
<div class="playlist-thumb" style="background-image:url(' .
$large_thumb . ')"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M549.655 124.083c-6.281-23.65-24.787-42.276-48.284-48.597C458.781 64 288 64 288 64S117.22 64 74.629 75.486c-23.497 6.322-42.003 24.947-48.284 48.597-11.412 42.867-11.412 132.305-11.412 132.305s0 89.438 11.412 132.305c6.281 23.65 24.787 41.5 48.284 47.821C117.22 448 288 448 288 448s170.78 0 213.371-11.486c23.497-6.321 42.003-24.171 48.284-47.821 11.412-42.867 11.412-132.305 11.412-132.305s0-89.438-11.412-132.305zm-317.51 213.508V175.185l142.739 81.205-142.739 81.201z"/></svg></div>
</div></div>';
$output .= '<div class="nav-buttons"><button class="nav-button previous mfp-arrow mfp-arrow-left mfp-prevent-close" data-video-index=""></button><button class="nav-button next mfp-arrow mfp-arrow-right mfp-prevent-close" data-video-index="1"></button></div>';
//set up the html for all items (small display)
$output .= '<div class="playlist-list"><ul>';
$i = 1;
foreach ( $response->items as $item ) {
$video_id = $item->snippet->resourceId->videoId;
if ( isset( $item->snippet->thumbnails ) ) {
if ( isset( $item->snippet->thumbnails->default->url ) ) {
$small_thumb = $item->snippet->thumbnails->default->url;
} else {
$small_thumb = better_youtube_get_large_youtube_thumbnail( $item->snippet->thumbnails );
}
$large_thumb = better_youtube_get_large_youtube_thumbnail( $item->snippet->thumbnails );
} else {
$small_thumb = false;
$large_thumb = false;
}
$index = $i - 1;
$output .= '<li class="playlist-preview" data-video-id="' . $video_id . '" data-video-index="' . $index . '" tabindex="0" role="button">' .
'<div class="playlist-small-thumb"><div class="playlist-thumb" style="background-image:url(' .
$small_thumb . ')"></div></div>' .
'<div class="playlist-num">' . $i . '</div>' .
'<div class="playlist-item-title">' . $item->snippet->title . '</div>' .
'</li>';
$i++;
}
$output .= '</ul></div>';
$output .= '</div>';
} catch ( Exception $e ) {
$error = json_decode( $e->getMessage() );
$output = '<div class="error">Error: <br>' . $error->error->message . '</div>';
}
return shortcode_unautop( $output );
}
/**
* Construct a playlist element that uses built in embed players
* @param string $src
*
* @return bool|string playlist html or false if error
*/
function better_youtube_legacy_playlist( $src ) {
$playlist = better_youtube_get_youtube_playlist_from_src( $src );
if ( $playlist && defined( 'YOUTUBE_API_KEY' ) ) {
// Google API for building custom YouTube Playlists
require_once SQUARECANDY_BYT_PATH . 'inc/vendor/autoload.php';
try {
$client = new Google_Client();
$client->setDeveloperKey( YOUTUBE_API_KEY );
$client->setScopes( 'https://www.googleapis.com/auth/youtube' );
$redirect = filter_var( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL );
$client->setRedirectUri( $redirect );
// Define an object that will be used to make all API requests.
$service = new Google_Service_YouTube( $client );
//get all items in the playlist via API
$params = array(
'maxResults' => 49,
'playlistId' => $playlist,
);
$params = array_filter( $params );
$response = $service->playlistItems->listPlaylistItems( 'snippet', $params );
$large_thumb = better_youtube_get_large_youtube_thumbnail( $response->items[0]->snippet->thumbnails );
//set up the html for the first item (large display)
$link = 'https://www.youtube.com/embed/' .
$response->items[0]->snippet->resourceId->videoId . better_youtube_url_parameters();
$output = '<div class="custom-playlist">';
$output .= '<div class="playlist-preview-first"><a href="' . $link . '">
<div class="playlist-thumb" style="background-image:url(' .
$large_thumb . ')"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M549.655 124.083c-6.281-23.65-24.787-42.276-48.284-48.597C458.781 64 288 64 288 64S117.22 64 74.629 75.486c-23.497 6.322-42.003 24.947-48.284 48.597-11.412 42.867-11.412 132.305-11.412 132.305s0 89.438 11.412 132.305c6.281 23.65 24.787 41.5 48.284 47.821C117.22 448 288 448 288 448s170.78 0 213.371-11.486c23.497-6.321 42.003-24.171 48.284-47.821 11.412-42.867 11.412-132.305 11.412-132.305s0-89.438-11.412-132.305zm-317.51 213.508V175.185l142.739 81.205-142.739 81.201z"/></svg></div>
</a></div>';
//set up the html for all items (small display)
$output .= '<div class="playlist-list"><ul>';
$i = 1;
foreach ( $response->items as $item ) {
$link = 'https://www.youtube.com/embed/' .
$item->snippet->resourceId->videoId . better_youtube_url_parameters();
if ( isset( $item->snippet->thumbnails ) ) {
$small_thumb = isset( $item->snippet->thumbnails->default->url ) ? $item->snippet->thumbnails->default->url : false;
$large_thumb = better_youtube_get_large_youtube_thumbnail( $item->snippet->thumbnails );
} else {
$small_thumb = false;
$large_thumb = false;
}
$output .= '<li><a href="' . $link . '" ' .
'rel="videogroup_' . $playlist . '" ' .
'title="' . esc_attr( $item->snippet->title ) . '" ' .
'data-large-thumb="' . $large_thumb . '">' .
'<div class="playlist-small-thumb"><div class="playlist-thumb" style="background-image:url(' .
$small_thumb . ')"></div></div>' .
'<div class="playlist-num">' . $i . '</div>' .
'<div class="playlist-item-title">' . $item->snippet->title . '</div>' .
'</a></li>';
$i++;
}
$output .= '</ul></div>';
$output .= '</div>';
} catch ( Exception $e ) {
$error = json_decode( $e->getMessage() );
$output = '<div class="error">Error: <br>' . $error->error->message . '</div>';
}
return shortcode_unautop( $output );
} else {
return false;
}
}
// use to wrap youtube iframes anywhere in the code for nicer output
if ( ! function_exists( 'better_youtube_iframe' ) ) :
function better_youtube_iframe( $iframe ) {
// something weird happened
if ( ! is_string( $iframe ) ) {
return;
}
//find iframe src
$src = better_youtube_get_youtube_iframe_from_embed( $iframe );
$use_api = apply_filters( 'better_youtube_use_playlist_api', true );
$playlist = $use_api ? better_youtube_api_playlist( $src ) : better_youtube_legacy_playlist( $src );
if ( $playlist ) {
return $playlist;
} else {
if ( $src ) {
// add extra params to iframe src
$params = better_youtube_url_parameters( true, false ); // should return an array
$new_src = add_query_arg( $params, $src ); // returns a string
$new_src = esc_url( $new_src );
$iframe = str_replace( $src, $new_src, $iframe );
}
$iframe = str_replace( 'allow="autoplay; encrypted-media"', '', $iframe );
$iframe = str_replace( 'frameborder="0"', '', $iframe );
if ( ! strpos( $iframe, 'loading=' ) ) {
$iframe = str_replace( '<iframe ', '<iframe loading="lazy" ', $iframe );
}
$iframe = shortcode_unautop( $iframe );
if ( ! strpos( $iframe, 'fitvids' ) ) {
$iframe = '<div class="fitvids">' . $iframe . '</div>';
}
return $iframe;
}
}
endif;
// apply the better_youtube_iframe improvements to the automatic WordPress oembed
if ( ! function_exists( 'squarecandy_custom_youtube_querystring' ) ) :
function squarecandy_custom_youtube_querystring( $html, $url, $args ) {
if ( strpos( $html, 'youtube' ) || strpos( $html, 'youtu.be' ) ) {
$html = better_youtube_iframe( $html );
} elseif ( strpos( $html, 'vimeo' ) && ! strpos( $html, 'fitvids' ) ) {
// apply fitvids container to vimeo
$html = '<div class="fitvids">' . $html . '</div>';
}
return $html;
}
add_filter( 'oembed_result', 'squarecandy_custom_youtube_querystring', 99, 3 );
add_filter( 'embed_oembed_html', 'squarecandy_custom_youtube_querystring', 99, 3 );
endif;
// Apply fitvids to the_excerpt
// Not sure why oembed_result is not being applied here already. Is there a better way to do this?
add_filter( 'get_the_excerpt', 'squarecandy_custom_youtube_excerpt', 999 );
function squarecandy_custom_youtube_excerpt( $content ) {
$content = str_replace( '<p><iframe', '<iframe', $content );
$content = str_replace( '</iframe></p>', '</iframe>', $content );
$content = preg_replace( '/(<iframe.*src=\S.*(?:youtu|vimeo).*\/iframe>)/', '<div class="fitvids">$1</div>', $content );
return $content;
}
add_filter( 'the_content', 'better_youtube_the_content', 110 );
function better_youtube_the_content( $content ) {
// shortcode_fix / fix wpautop crap
// https://wordpress.stackexchange.com/a/217304/41488
$content = str_replace( '<p></a></li>', '</a></li>', $content );
// wrap video iframes in fitvids div
// fetches iframes already wrapped in a <p> by WP - strips the containing <p> and adds our <div> instead
// this happens after the oembed filter but this ignore the embeds bc they aren't wrapped in <p>
$content = preg_replace( '/<p.*>(<iframe.*src=\S.*(?:youtu|vimeo).*\/iframe>)<\/p>/', '<div class="fitvids">$1</div>', $content );
return $content;
}