PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 1.6.5
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v1.6.5
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / includes / functions.php

functions.php in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 1.6.5, at includes/functions.php

922 lines 32.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Helper Functions.
5 *
6 * @link https://plugins360.com
7 * @since 1.0.0
8 *
9 * @package Automatic_YouTube_Gallery
10 */
11
12 // Exit if accessed directly
13 if ( ! defined( 'WPINC' ) ) {
14 die;
15 }
16
17 /**
18 * Build gallery HTML output.
19 *
20 * @since 1.0.0
21 * @param array $atts An associative array of attributes.
22 * @return mixed
23 */
24 function ayg_build_gallery( $atts ) {
25 // Vars
26 $fields = ayg_get_editor_fields();
27 $defaults = array();
28
29 foreach ( $fields as $key => $value ) {
30 foreach ( $value['fields'] as $field ) {
31 $defaults[ $field['name'] ] = $field['value'];
32 }
33 }
34
35 $attributes = shortcode_atts( $defaults, $atts );
36
37 $attributes['columns'] = min( 12, (int) $attributes['columns'] );
38 if ( empty( $attributes['columns'] ) ) {
39 $attributes['columns'] = 3;
40 }
41
42 $attributes['limit'] = min( 500, (int) $attributes['limit'] );
43 if ( empty( $attributes['limit'] ) ) {
44 $attributes['limit'] = 500;
45 }
46
47 $attributes['per_page'] = min( 50, (int) $attributes['per_page'] );
48 if ( empty( $attributes['per_page'] ) ) {
49 $attributes['per_page'] = 50;
50 }
51
52 // Get Videos
53 $source_type = sanitize_text_field( $attributes['type'] );
54
55 if ( 'livestream' == $source_type ) {
56 $attributes['livestream'] = $attributes['channel'];
57 }
58
59 $api_params = array(
60 'type' => $source_type,
61 'id' => sanitize_text_field( $attributes[ $source_type ] ),
62 'order' => sanitize_text_field( $attributes['order'] ), // works only when type=search
63 'maxResults' => $attributes['per_page'],
64 'cache' => (int) $attributes['cache']
65 );
66
67 $api_params = apply_filters( 'ayg_youtube_api_request_params', $api_params, $atts );
68
69 $youtube_api = new AYG_YouTube_API();
70 $response = $youtube_api->get_videos( $api_params );
71
72 // Process output
73 if ( ! isset( $response->error ) ) {
74 // Enqueue dependencies
75 wp_enqueue_style( AYG_SLUG . '-public' );
76 wp_enqueue_script( AYG_SLUG . '-public' );
77
78 // Gallery
79 $videos = array();
80
81 if ( isset( $response->videos ) ) {
82 $videos = $response->videos;
83 }
84
85 // Pagination
86 if ( isset( $response->page_info ) ) {
87 $attributes = array_merge( $attributes, $response->page_info, $api_params );
88 }
89
90 // Theme
91 $theme = 'classic';
92
93 if ( 'video' == $source_type ) {
94 $theme = 'single';
95 }
96
97 if ( 'livestream' == $source_type ) {
98 $theme = 'livestream';
99 }
100
101 // Output
102 ob_start();
103 include ayg_get_template( AYG_DIR . "public/templates/theme-{$theme}.php", $attributes['theme'] );
104 return ob_get_clean();
105 } else {
106 return '<p class="ayg-error">' . $response->error_message . '</p>';
107 }
108 }
109
110 /**
111 * Get default plugin settings.
112 *
113 * @since 1.6.4
114 * @return array $defaults Array of plugin settings.
115 */
116 function ayg_get_default_settings() {
117 $defaults = array(
118 'ayg_general_settings' => array(
119 'api_key' => ''
120 ),
121 'ayg_gallery_settings' => array(
122 'theme' => 'classic',
123 'columns' => 3,
124 'per_page' => 12,
125 'thumb_ratio' => 56.25,
126 'thumb_title' => 1,
127 'thumb_title_length' => 0,
128 'thumb_excerpt' => 1,
129 'thumb_excerpt_length' => 75,
130 'pagination' => 1,
131 'pagination_type' => 'load_more'
132 ),
133 'ayg_player_settings' => array(
134 'player_ratio' => 56.25,
135 'player_title' => 1,
136 'player_description' => 1,
137 'autoplay' => 0,
138 'autoadvance' => 0,
139 'loop' => 0,
140 'controls' => 1,
141 'modestbranding' => 1,
142 'cc_load_policy' => 0,
143 'iv_load_policy' => 0,
144 'hl' => '',
145 'cc_lang_pref' => ''
146 ),
147 'ayg_livestream_settings' => array(
148 'fallback_message' => '<div style="padding: 15px; background: #eee; border-radius: 3px;"><strong>Sorry, but the channel is not currently streaming live content.</strong> Please check back later.<br /><br />Thank you!</div>'
149 )
150 );
151
152 return $defaults;
153 }
154
155 /**
156 * Get editor fields.
157 *
158 * @since 1.0.0
159 * @return array Array of fields.
160 */
161 function ayg_get_editor_fields() {
162 $fields = array(
163 'source' => array(
164 'label' => __( 'General', 'automatic-youtube-gallery' ),
165 'fields' => array(
166 array(
167 'name' => 'type',
168 'label' => __( 'Source Type', 'automatic-youtube-gallery' ),
169 'description' => '',
170 'type' => 'select',
171 'options' => array(
172 'playlist' => __( 'Playlist', 'automatic-youtube-gallery' ),
173 'channel' => __( 'Channel', 'automatic-youtube-gallery' ),
174 'username' => __( 'Username', 'automatic-youtube-gallery' ),
175 'search' => __( 'Search Terms', 'automatic-youtube-gallery' ),
176 'video' => __( 'Single Video', 'automatic-youtube-gallery' ),
177 'livestream' => __( 'Live Stream', 'automatic-youtube-gallery' ),
178 'videos' => __( 'Custom Videos List', 'automatic-youtube-gallery' )
179 ),
180 'value' => 'playlist',
181 'sanitize_callback' => 'sanitize_key'
182 ),
183 array(
184 'name' => 'playlist',
185 'label' => __( 'YouTube Playlist ID (or) URL', 'automatic-youtube-gallery' ),
186 'description' => sprintf( '%s: https://www.youtube.com/playlist?list=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
187 'type' => 'url',
188 'value' => '',
189 'sanitize_callback' => 'sanitize_text_field'
190 ),
191 array(
192 'name' => 'channel',
193 'label' => __( 'YouTube Channel ID (or) URL', 'automatic-youtube-gallery' ),
194 'description' => sprintf( '%s: https://www.youtube.com/channel/XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
195 'type' => 'url',
196 'value' => '',
197 'sanitize_callback' => 'sanitize_text_field'
198 ),
199 array(
200 'name' => 'username',
201 'label' => __( 'YouTube Account Username', 'automatic-youtube-gallery' ),
202 'description' => sprintf( '%s: SanRosh', __( 'Example', 'automatic-youtube-gallery' ) ),
203 'type' => 'text',
204 'value' => '',
205 'sanitize_callback' => 'sanitize_text_field'
206 ),
207 array(
208 'name' => 'search',
209 'label' => __( 'Search Terms', 'automatic-youtube-gallery' ),
210 'description' => sprintf( '%s: Cartoon (space:AND , -:NOT , |:OR)', __( 'Example', 'automatic-youtube-gallery' ) ),
211 'type' => 'text',
212 'value' => '',
213 'sanitize_callback' => 'sanitize_text_field'
214 ),
215 array(
216 'name' => 'video',
217 'label' => __( 'YouTube Video ID (or) URL', 'automatic-youtube-gallery' ),
218 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
219 'type' => 'url',
220 'value' => '',
221 'sanitize_callback' => 'sanitize_text_field'
222 ),
223 array(
224 'name' => 'videos',
225 'label' => __( 'YouTube Video IDs (or) URLs', 'automatic-youtube-gallery' ),
226 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
227 'type' => 'textarea',
228 'placeholder' => __( 'Enter one video per line', 'automatic-youtube-gallery' ),
229 'value' => '',
230 'sanitize_callback' => 'sanitize_text_field'
231 ),
232 array(
233 'name' => 'order',
234 'label' => __( 'Order Videos by', 'automatic-youtube-gallery' ),
235 'description' => '',
236 'type' => 'select',
237 'options' => array(
238 'date' => __( 'Date', 'automatic-youtube-gallery' ),
239 'rating' => __( 'Rating', 'automatic-youtube-gallery' ),
240 'relevance' => __( 'Relevance', 'automatic-youtube-gallery' ),
241 'title' => __( 'Title', 'automatic-youtube-gallery' ),
242 'viewCount' => __( 'View Count', 'automatic-youtube-gallery' )
243 ),
244 'value' => 'relevance',
245 'sanitize_callback' => 'sanitize_key'
246 ),
247 array(
248 'name' => 'limit',
249 'label' => __( 'Number of Videos', 'automatic-youtube-gallery' ),
250 'description' => __( 'Specifies the maximum number of videos that will appear in this gallery. Set to 0 for the maximum amount (500).', 'automatic-youtube-gallery' ),
251 'type' => 'number',
252 'min' => 0,
253 'max' => 500,
254 'value' => 0,
255 'sanitize_callback' => 'intval'
256 ),
257 array(
258 'name' => 'cache',
259 'label' => __( 'Refresh the data every (Cache time)', 'automatic-youtube-gallery' ),
260 'description' => __( 'IMPORTANT! Specifies how frequently the plugin should check your YouTube source for any new update. We recommend keeping this value as "Page load" when you test the gallery and strongly suggest to change this value as "Day" or to a greater value before pushing the gallery live.', 'automatic-youtube-gallery' ),
261 'type' => 'select',
262 'options' => array(
263 '0' => __( 'Page load', 'automatic-youtube-gallery' ),
264 '900' => __( '15 minutes', 'automatic-youtube-gallery' ),
265 '1800' => __( '30 minutes', 'automatic-youtube-gallery' ),
266 '3600' => __( 'Hour', 'automatic-youtube-gallery' ),
267 '86400' => __( 'Day', 'automatic-youtube-gallery' ),
268 '604800' => __( 'Week', 'automatic-youtube-gallery' ),
269 '2419200' => __( 'Month', 'automatic-youtube-gallery' )
270 ),
271 'value' => 0,
272 'sanitize_callback' => 'intval'
273 )
274 )
275 ),
276 'gallery' => array(
277 'label' => __( 'Gallery (optional)', 'automatic-youtube-gallery' ),
278 'fields' => ayg_get_gallery_settings_fields()
279 ),
280 'player' => array(
281 'label' => __( 'Player (optional)', 'automatic-youtube-gallery' ),
282 'fields' => ayg_get_player_settings_fields()
283 )
284 );
285
286 return apply_filters( 'ayg_editor_fields', $fields );
287 }
288
289 /**
290 * Get gallery thumbnail video excerpt (short description).
291 *
292 * @since 1.0.0
293 * @param stdClass $video YouTube video object.
294 * @param array $attributes Array of user attributes.
295 * @return string Video excerpt.
296 */
297 function ayg_get_gallery_thumb_excerpt( $video, $attributes ) {
298 $char_length = (int) $attributes['thumb_excerpt_length'];
299 $char_length++;
300
301 $content = '';
302 if ( ! empty( $attributes['thumb_excerpt'] ) ) {
303 $content = ( $char_length > 1 ) ? wp_strip_all_tags( $video->description, true ) : nl2br( $video->description );
304 }
305
306 $excerpt = '';
307
308 if ( $char_length > 1 && mb_strlen( $content ) > $char_length ) {
309 $subex = mb_substr( $content, 0, $char_length - 5 );
310 $exwords = explode( ' ', $subex );
311 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
312 if ( $excut < 0 ) {
313 $excerpt = mb_substr( $subex, 0, $excut );
314 } else {
315 $excerpt = $subex;
316 }
317 $excerpt .= '[...]';
318 } else {
319 $excerpt = $content;
320 }
321
322 return apply_filters( 'ayg_gallery_thumb_excerpt', $excerpt, $video, $attributes );
323 }
324
325 /**
326 * Get gallery thumbnail image.
327 *
328 * @since 1.0.0
329 * @param stdClass $video YouTube video object.
330 * @param array $attributes Array of user attributes.
331 * @return string Video thumbnail image.
332 */
333 function ayg_get_gallery_thumb_image( $video, $attributes ) {
334 $image = '';
335
336 if ( isset( $video->thumbnails->default ) ) {
337 $image = $video->thumbnails->default->url;
338 }
339
340 // 4:3 ( default - 120x90, high - 480x360, standard - 640x480 )
341 if ( 75 == (int) $attributes['thumb_ratio'] ) {
342 if ( isset( $video->thumbnails->high ) ) {
343 $image = $video->thumbnails->high->url;
344 }
345 }
346
347 // 16:9 ( medium - 320x180, maxres - 1280x720 )
348 if ( 56.25 == (float) $attributes['thumb_ratio'] ) {
349 if ( isset( $video->thumbnails->medium ) ) {
350 $image = $video->thumbnails->medium->url;
351 }
352 }
353
354 return apply_filters( 'ayg_gallery_thumb_image', $image, $video, $attributes );
355 }
356
357 /**
358 * Get gallery thumbnail video title.
359 *
360 * @since 1.0.0
361 * @param stdClass $video YouTube video object.
362 * @param array $attributes Array of user attributes.
363 * @return string Video title.
364 */
365 function ayg_get_gallery_thumb_title( $video, $attributes ) {
366 $text = ! empty( $attributes['thumb_title'] ) ? $video->title : '';
367
368 $char_length = (int) $attributes['thumb_title_length'];
369 $char_length++;
370
371 $title = '';
372
373 if ( $char_length > 1 && mb_strlen( $text ) > $char_length ) {
374 $subex = mb_substr( $text, 0, $char_length - 5 );
375 $exwords = explode( ' ', $subex );
376 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
377 if ( $excut < 0 ) {
378 $title = mb_substr( $subex, 0, $excut );
379 } else {
380 $title = $subex;
381 }
382 $title .= '[...]';
383 } else {
384 $title = $text;
385 }
386
387 return apply_filters( 'ayg_gallery_thumb_title', $title, $video, $attributes );
388 }
389
390 /**
391 * Get gallery settings fields.
392 *
393 * @since 1.0.0
394 * @return array $fields Array of fields.
395 */
396 function ayg_get_gallery_settings_fields() {
397 $gallery_settings = get_option( 'ayg_gallery_settings' );
398
399 $fields = array(
400 array(
401 'name' => 'theme',
402 'label' => __( 'Select Theme', 'automatic-youtube-gallery' ),
403 'description' => ( ayg_fs()->is_not_paying() ? sprintf( __( '<a href="%s">Upgrade Pro</a> for more themes (Popup, Slider, Playlister).', 'automatic-youtube-gallery' ), esc_url( ayg_fs()->get_upgrade_url() ) ) : '' ),
404 'type' => 'select',
405 'options' => array(
406 'classic' => __( 'Classic', 'automatic-youtube-gallery' )
407 ),
408 'value' => $gallery_settings['theme'],
409 'sanitize_callback' => 'sanitize_key'
410 ),
411 array(
412 'name' => 'columns',
413 'label' => __( 'Columns', 'automatic-youtube-gallery' ),
414 'description' => __( 'Enter the number of columns you like to have in the gallery. Maximum of 12.', 'automatic-youtube-gallery' ),
415 'type' => 'number',
416 'min' => 0,
417 'max' => 12,
418 'value' => $gallery_settings['columns'],
419 'sanitize_callback' => 'intval'
420 ),
421 array(
422 'name' => 'per_page',
423 'label' => __( 'Videos per page', 'automatic-youtube-gallery' ),
424 'description' => __( 'Enter the number of videos to show per page. Maximum of 50.', 'automatic-youtube-gallery' ),
425 'type' => 'number',
426 'min' => 0,
427 'max' => 50,
428 'value' => $gallery_settings['per_page'],
429 'sanitize_callback' => 'intval'
430 ),
431 array(
432 'name' => 'thumb_ratio',
433 'label' => __( 'Thumbnail Ratio', 'automatic-youtube-gallery' ),
434 'type' => 'radio',
435 'options' => array(
436 '56.25' => '16:9',
437 '75' => '4:3'
438 ),
439 'value' => $gallery_settings['thumb_ratio'],
440 'sanitize_callback' => 'floatval'
441 ),
442 array(
443 'name' => 'thumb_title',
444 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
445 'description' => __( 'Check this option to show the video title in each gallery item.', 'automatic-youtube-gallery' ),
446 'type' => 'checkbox',
447 'value' => $gallery_settings['thumb_title'],
448 'sanitize_callback' => 'intval'
449 ),
450 array(
451 'name' => 'thumb_title_length',
452 'label' => __( 'Video Title Length', 'automatic-youtube-gallery' ),
453 'description' => __( 'Enter the number of characters you like to show in the title. Set 0 to show the whole title.', 'automatic-youtube-gallery' ),
454 'type' => 'number',
455 'min' => 0,
456 'max' => 500,
457 'value' => $gallery_settings['thumb_title_length'],
458 'sanitize_callback' => 'intval'
459 ),
460 array(
461 'name' => 'thumb_excerpt',
462 'label' => __( 'Show Video Excerpt (Short Description)', 'automatic-youtube-gallery' ),
463 'description' => __( 'Check this option to show the short description of a video in each gallery item.', 'automatic-youtube-gallery' ),
464 'type' => 'checkbox',
465 'value' => $gallery_settings['thumb_excerpt'],
466 'sanitize_callback' => 'intval'
467 ),
468 array(
469 'name' => 'thumb_excerpt_length',
470 'label' => __( 'Video Excerpt Length', 'automatic-youtube-gallery' ),
471 'description' => __( 'Enter the number of characters you like to have in the video excerpt. Set 0 to show the whole description.', 'automatic-youtube-gallery' ),
472 'type' => 'number',
473 'min' => 0,
474 'max' => 500,
475 'value' => $gallery_settings['thumb_excerpt_length'],
476 'sanitize_callback' => 'intval'
477 ),
478 array(
479 'name' => 'pagination',
480 'label' => __( 'Pagination', 'automatic-youtube-gallery' ),
481 'description' => __( 'Check this option to show the pagination.', 'automatic-youtube-gallery' ),
482 'type' => 'checkbox',
483 'value' => $gallery_settings['pagination'],
484 'sanitize_callback' => 'intval'
485 ),
486 array(
487 'name' => 'pagination_type',
488 'label' => __( 'Pagination Type', 'automatic-youtube-gallery' ),
489 'type' => 'select',
490 'options' => array(
491 'pager' => __( 'Pager', 'automatic-youtube-gallery' ),
492 'load_more' => __( 'Load More', 'automatic-youtube-gallery' )
493 ),
494 'value' => $gallery_settings['pagination_type'],
495 'sanitize_callback' => 'sanitize_key'
496 )
497 );
498
499 return apply_filters( 'ayg_gallery_settings_fields', $fields );
500 }
501
502 /**
503 * Get video description to show on top of the player.
504 *
505 * @since 1.0.0
506 * @param stdClass $video YouTube video object.
507 * @param array $attributes Array of user attributes.
508 * @param int $words_count Number of words to show by default.
509 * @return string Video description.
510 */
511 function ayg_get_player_description( $video, $attributes, $words_count = 30 ) {
512 $description = ! empty( $attributes['player_description'] ) ? $video->description : '';
513
514 $words_array = explode( ' ', strip_tags( $description ) );
515
516 if ( count( $words_array ) > $words_count ) {
517 $words_array[ $words_count ] = '<span class="ayg-player-description-dots">...</span></span><span class="ayg-player-description-more">' . $words_array[ $words_count ];
518
519 $description = '<span class="ayg-player-description-less">' . implode( ' ', $words_array ) . '</span>';
520 $description .= '<a href="javascript:void(0);" class="ayg-player-description-toggle-btn">[+] ' . __( 'Show More', 'automatic-youtube-gallery' ) . '</a>';
521 }
522
523 return apply_filters( 'ayg_player_description', nl2br( $description ), $video, $attributes, $words_count );
524 }
525
526 /**
527 * Get YouTube player embed URL.
528 *
529 * @since 1.0.0
530 * @param stdClass $item YouTube source object.
531 * @param array $attributes Array of user attributes.
532 * @return string YouTube video embed URL.
533 */
534 function ayg_get_player_embed_url( $item, $attributes ) {
535 if ( 'livestream' == $attributes['type'] ) {
536 $url = "https://www.youtube-nocookie.com/embed/live_stream?channel={$item->id}";
537 } else {
538 $url = "https://www.youtube-nocookie.com/embed/{$item->id}";
539 }
540
541 if ( ! empty( $attributes['autoplay'] ) ) { // autoplay
542 $url = add_query_arg( 'autoplay', 1, $url );
543 }
544
545 if ( ! empty( $attributes['loop'] ) ) { // loop
546 $url = add_query_arg( 'loop', 1, $url );
547 }
548
549 if ( empty( $attributes['controls'] ) ) { // controls
550 $url = add_query_arg( 'controls', 0, $url );
551 }
552
553 if ( ! empty( $attributes['modestbranding'] ) ) { // modestbranding
554 $url = add_query_arg( 'modestbranding', 1, $url );
555 }
556
557 if ( ! empty( $attributes['cc_load_policy'] ) ) { // cc_load_policy
558 $url = add_query_arg( 'cc_load_policy', 1, $url );
559 }
560
561 if ( empty( $attributes['iv_load_policy'] ) ) { // iv_load_policy
562 $url = add_query_arg( 'iv_load_policy', 3, $url );
563 }
564
565 if ( ! empty( $attributes['hl'] ) ) { // hl
566 $url = add_query_arg( 'hl', $attributes['hl'], $url );
567 }
568
569 if ( ! empty( $attributes['cc_lang_pref'] ) ) { // cc_lang_pref
570 $url = add_query_arg( 'cc_lang_pref', $attributes['cc_lang_pref'], $url );
571 }
572
573 $url = add_query_arg( 'rel', 0, $url ); // rel
574 $url = add_query_arg( 'playsinline', 1, $url ); // playsinline
575 $url = add_query_arg( 'enablejsapi', 1, $url ); // enablejsapi
576
577 return apply_filters( 'ayg_player_embed_url', $url, $item, $attributes );
578 }
579
580 /**
581 * Get player ratio.
582 *
583 * @since 1.0.0
584 * @param array $attributes Array of user attributes.
585 * @return string Player ratio.
586 */
587 function ayg_get_player_ratio( $attributes ) {
588 $ratio = ! empty( $attributes['player_ratio'] ) ? $attributes['player_ratio'] . '%' : '56.25%';
589 return apply_filters( 'ayg_player_ratio', $ratio, $attributes );
590 }
591
592 /**
593 * Get player settings fields.
594 *
595 * @since 1.0.0
596 * @return array $fields Array of fields.
597 */
598 function ayg_get_player_settings_fields() {
599 $player_settings = get_option( 'ayg_player_settings' );
600
601 $fields = array(
602 array(
603 'name' => 'player_ratio',
604 'label' => __( 'Player Ratio', 'automatic-youtube-gallery' ),
605 'type' => 'radio',
606 'options' => array(
607 '56.25' => '16:9',
608 '75' => '4:3'
609 ),
610 'value' => $player_settings['player_ratio'],
611 'sanitize_callback' => 'floatval'
612 ),
613 array(
614 'name' => 'player_title',
615 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
616 'description' => __( 'Check this option to show the current playing video title on the bottom of the player.', 'automatic-youtube-gallery' ),
617 'type' => 'checkbox',
618 'value' => $player_settings['player_title'],
619 'sanitize_callback' => 'intval'
620 ),
621 array(
622 'name' => 'player_description',
623 'label' => __( 'Show Video Description', 'automatic-youtube-gallery' ),
624 'description' => __( 'Check this option to show the current playing video description on the bottom of the player.', 'automatic-youtube-gallery' ),
625 'type' => 'checkbox',
626 'value' => $player_settings['player_description'],
627 'sanitize_callback' => 'intval'
628 ),
629 array(
630 'name' => 'autoplay',
631 'label' => __( 'Autoplay', 'automatic-youtube-gallery' ),
632 'description' => __( 'Specifies whether the initial video will automatically start to play when the player loads.', 'automatic-youtube-gallery' ),
633 'type' => 'checkbox',
634 'value' => $player_settings['autoplay'],
635 'sanitize_callback' => 'intval'
636 ),
637 array(
638 'name' => 'autoadvance',
639 'label' => __( 'Autoplay Next Video', 'automatic-youtube-gallery' ),
640 'description' => __( 'Specifies whether to play the next video in the list automatically after previous one end.', 'automatic-youtube-gallery' ),
641 'type' => 'checkbox',
642 'value' => $player_settings['autoadvance'],
643 'sanitize_callback' => 'intval'
644 ),
645 array(
646 'name' => 'loop',
647 'label' => __( 'Loop', 'automatic-youtube-gallery' ),
648 'description' => __( 'In the case of a single video player, plays the initial video again and again. In the case of a gallery, plays the entire list in the gallery and then starts again at the first video.', 'automatic-youtube-gallery' ),
649 'type' => 'checkbox',
650 'value' => $player_settings['loop'],
651 'sanitize_callback' => 'intval'
652 ),
653 array(
654 'name' => 'controls',
655 'label' => __( 'Show Player Controls', 'automatic-youtube-gallery' ),
656 'description' => __( 'Uncheck this option to hide the video player controls.', 'automatic-youtube-gallery' ),
657 'type' => 'checkbox',
658 'value' => $player_settings['controls'],
659 'sanitize_callback' => 'intval'
660 ),
661 array(
662 'name' => 'modestbranding',
663 'label' => __( 'Hide YouTube Logo', 'automatic-youtube-gallery' ),
664 'description' => __( "Lets you prevent the YouTube logo from displaying in the control bar. Note that a small YouTube text label will still display in the upper-right corner of a paused video when the user's mouse pointer hovers over the player.", 'automatic-youtube-gallery' ),
665 'type' => 'checkbox',
666 'value' => $player_settings['modestbranding'],
667 'sanitize_callback' => 'intval'
668 ),
669 array(
670 'name' => 'cc_load_policy',
671 'label' => __( 'Force Closed Captions', 'automatic-youtube-gallery' ),
672 'description' => __( 'Show captions by default, even if the user has turned captions off. The default behavior is based on user preference.', 'automatic-youtube-gallery' ),
673 'type' => 'checkbox',
674 'value' => $player_settings['cc_load_policy'],
675 'sanitize_callback' => 'intval'
676 ),
677 array(
678 'name' => 'iv_load_policy',
679 'label' => __( 'Show Annotations', 'automatic-youtube-gallery' ),
680 'description' => __( 'Choose whether to show annotations or not.', 'automatic-youtube-gallery' ),
681 'type' => 'checkbox',
682 'value' => $player_settings['iv_load_policy'],
683 'sanitize_callback' => 'intval'
684 ),
685 array(
686 'name' => 'hl',
687 'label' => __( 'Player Language', 'automatic-youtube-gallery' ),
688 'description' => sprintf(
689 __( 'Specifies the player\'s interface language. Set the field\'s value to an <a href="%s" target="_blank">ISO 639-1 two-letter language code.</a>', 'automatic-youtube-gallery' ),
690 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
691 ),
692 'type' => 'text',
693 'value' => $player_settings['hl'],
694 'sanitize_callback' => 'sanitize_text_field'
695 ),
696 array(
697 'name' => 'cc_lang_pref',
698 'label' => __( 'Default Captions Language', 'automatic-youtube-gallery' ),
699 'description' => sprintf(
700 __( 'Specifies the default language that the player will use to display captions. Set the field\'s value to an <a href="%s" target="_blank">ISO 639-1 two-letter language code.</a>', 'automatic-youtube-gallery' ),
701 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
702 ),
703 'type' => 'text',
704 'value' => $player_settings['cc_lang_pref'],
705 'sanitize_callback' => 'sanitize_text_field'
706 )
707 );
708
709 return $fields;
710 }
711
712 /**
713 * Get video title to show on bottom of the player.
714 *
715 * @since 1.0.0
716 * @param stdClass $video YouTube video object.
717 * @param array $attributes Array of user attributes.
718 * @return string Video title.
719 */
720 function ayg_get_player_title( $video, $attributes ) {
721 $title = ! empty( $attributes['player_title'] ) ? $video->title : '';
722 return apply_filters( 'ayg_player_title', $title, $video, $attributes );
723 }
724
725 /**
726 * Get player width.
727 *
728 * @since 1.0.0
729 * @param array $attributes Array of user attributes.
730 * @return string Player width.
731 */
732 function ayg_get_player_width( $attributes ) {
733 $width = ! empty( $attributes['player_width'] ) ? $attributes['player_width'] . 'px' : '100%';
734 return apply_filters( 'ayg_player_width', $width, $attributes );
735 }
736
737 /**
738 * Get filtered php template file path.
739 *
740 * @since 1.0.0
741 * @param array $template PHP file path.
742 * @param string $theme Automatic YouTube Gallery Theme.
743 * @return string Filtered file path.
744 */
745 function ayg_get_template( $template, $theme = '' ) {
746 return apply_filters( 'ayg_load_template', $template, $theme );
747 }
748
749 /**
750 * Get unique ID.
751 *
752 * @since 1.0.0
753 * @return string Unique ID.
754 */
755 function ayg_get_uniqid() {
756 global $ayg_uniqid;
757
758 if ( ! $ayg_uniqid ) {
759 $ayg_uniqid = 0;
760 }
761
762 return uniqid() . ++$ayg_uniqid;
763 }
764
765 /**
766 * Sanitize the integer inputs, accepts empty values.
767 *
768 * @since 1.0.0
769 * @param string|int $value Input value.
770 * @return string|int Sanitized value.
771 */
772 function ayg_sanitize_int( $value ) {
773 $value = intval( $value );
774 return ( 0 == $value ) ? '' : $value;
775 }
776
777 /**
778 * Gallery HTML output.
779 *
780 * @since 1.0.0
781 * @param array $videos Array of YouTube video object.
782 * @param array $attributes Array of user attributes.
783 * @param int $active_id Current active/selected video ID in the gallery.
784 */
785 function the_ayg_gallery( $videos, $attributes, $active_id = 0 ) {
786 if ( 'video' != $attributes['type'] ) {
787 include ayg_get_template( AYG_DIR . 'public/templates/gallery.php', $attributes['theme'] );
788 }
789 }
790
791 /**
792 * Gallery HTML output.
793 *
794 * @since 1.0.0
795 * @param array $video YouTube video object.
796 * @param array $attributes Array of user attributes.
797 * @param boolean $is_active True if active video item, false if not.
798 */
799 function the_ayg_gallery_thumbnail( $video, $attributes, $is_active = false ) {
800 include ayg_get_template( AYG_DIR . 'public/templates/gallery-thumbnail.php', $attributes['theme'] );
801 }
802
803 /**
804 * Display gallery thumbnail image.
805 *
806 * @since 1.6.4
807 * @param stdClass $video YouTube video object.
808 * @param array $attributes Array of user attributes.
809 */
810 function the_ayg_gallery_thumbnail_image( $video, $attributes ) {
811 $image = '';
812 $src = '';
813 $width = '';
814 $height = '';
815
816 if ( isset( $video->thumbnails->default ) ) {
817 $src = $video->thumbnails->default->url;
818 $width = $video->thumbnails->default->width;
819 $height = $video->thumbnails->default->height;
820 }
821
822 // 4:3 ( default - 120x90, high - 480x360, standard - 640x480 )
823 if ( 75 == (int) $attributes['thumb_ratio'] ) {
824 if ( isset( $video->thumbnails->high ) ) {
825 $src = $video->thumbnails->high->url;
826 $width = $video->thumbnails->high->width;
827 $height = $video->thumbnails->high->height;
828 }
829 }
830
831 // 16:9 ( medium - 320x180, maxres - 1280x720 )
832 if ( 56.25 == (float) $attributes['thumb_ratio'] ) {
833 if ( isset( $video->thumbnails->medium ) ) {
834 $src = $video->thumbnails->medium->url;
835 $width = $video->thumbnails->medium->width;
836 $height = $video->thumbnails->medium->height;
837 }
838 }
839
840 if ( ! empty( $src ) ) {
841 $image = sprintf(
842 '<img src="%s" width="%s" height="%s" class="ayg-thumbnail-image" alt="%s" />',
843 esc_url( $src ),
844 esc_attr( $width ),
845 esc_attr( $height ),
846 esc_attr( $video->title )
847 );
848 }
849
850 echo apply_filters( 'ayg_gallery_thumbnail_image_html', $image, $video, $attributes );
851 }
852
853 /**
854 * Pagination HTML output.
855 *
856 * @since 1.0.0
857 * @param array $atts Array of user attributes.
858 */
859 function the_ayg_pagination( $atts ) {
860 if ( ! empty( $atts['pagination'] ) ) {
861 // Build attributes
862 $attributes = array();
863
864 $fields = ayg_get_editor_fields();
865
866 foreach ( $fields['gallery']['fields'] as $field ) {
867 $field_name = $field['name'];
868
869 if ( ! in_array( $field_name, array( 'per_page', 'pagination' ) ) ) {
870 $attributes[ $field_name ] = sanitize_text_field( $atts[ $field_name ] );
871 }
872 }
873
874 $source_type = sanitize_text_field( $atts['type'] );
875
876 $attributes = array_merge(
877 $attributes,
878 array(
879 'type' => $source_type,
880 'id' => sanitize_text_field( $atts[ $source_type ] ),
881 'order' => sanitize_text_field( $atts['order'] ), // works only when type=search
882 'per_page' => (int) $atts['per_page'],
883 'cache' => (int) $atts['cache'],
884 'player_description' => ! empty( $atts['player_description'] ) ? (int) $atts['player_description'] : 0,
885 'num_pages' => 1,
886 'paged' => ! empty( $atts['paged'] ) ? (int) $atts['paged'] : 1,
887 'next_page_token' => ! empty( $atts['next_page_token'] ) ? sanitize_text_field( $atts['next_page_token'] ) : '',
888 'prev_page_token' => ! empty( $atts['prev_page_token'] ) ? sanitize_text_field( $atts['prev_page_token'] ) : ''
889 )
890 );
891
892 // Find total number of pages
893 $videos_found = ! empty( $atts['videos_found'] ) ? (int) $atts['videos_found'] : 0;
894
895 if ( $videos_found > 0 ) {
896 if ( 'search' == $source_type ) {
897 $limit = min( (int) $atts['limit'], $videos_found );
898 $attributes['num_pages'] = ceil( $limit / $attributes['per_page'] );
899 } else {
900 $attributes['num_pages'] = ceil( $videos_found / $attributes['per_page'] );
901 }
902 }
903
904 // Process output
905 if ( $attributes['num_pages'] > 1 ) {
906 $pagination_type = ( 'pager' == $atts['pagination_type'] ) ? 'pager' : 'loadmore';
907 include ayg_get_template( AYG_DIR . 'public/templates/pagination-' . $pagination_type. '.php', $attributes['theme'] );
908 }
909 }
910 }
911
912 /**
913 * Player HTML output.
914 *
915 * @since 1.0.0
916 * @param stdClass $video YouTube video object.
917 * @param array $attributes Array of user attributes.
918 */
919 function the_ayg_player( $video, $attributes ) {
920 include ayg_get_template( AYG_DIR . 'public/templates/player.php', $attributes['theme'] );
921 }
922