PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 1.6.3
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v1.6.3
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.3, at includes/functions.php

809 lines 29.2 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 ( 'video' == $source_type ) {
56 $attributes['theme'] = 'classic';
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 $youtube_api = new AYG_YouTube_API();
68 $response = $youtube_api->get_videos( $api_params );
69
70 // Process output
71 if ( ! isset( $response->error ) ) {
72 // Enqueue dependencies
73 wp_enqueue_style( AYG_SLUG . '-public' );
74 wp_enqueue_script( AYG_SLUG . '-public' );
75
76 // Gallery
77 $videos = array();
78
79 if ( isset( $response->videos ) ) {
80 $videos = $response->videos;
81 }
82
83 // Pagination
84 if ( isset( $response->page_info ) ) {
85 $attributes = array_merge( $attributes, $response->page_info, $api_params );
86 }
87
88 // Output
89 ob_start();
90 include ayg_get_template( AYG_DIR . 'public/templates/theme-classic.php', $attributes['theme'] );
91 return ob_get_clean();
92 } else {
93 return '<p class="ayg-error">' . $response->error_message . '</p>';
94 }
95 }
96
97 /**
98 * Get editor fields.
99 *
100 * @since 1.0.0
101 * @return array Array of fields.
102 */
103 function ayg_get_editor_fields() {
104 $fields = array(
105 'source' => array(
106 'label' => __( 'General', 'automatic-youtube-gallery' ),
107 'fields' => array(
108 array(
109 'name' => 'type',
110 'label' => __( 'Source Type', 'automatic-youtube-gallery' ),
111 'description' => '',
112 'type' => 'select',
113 'options' => array(
114 'playlist' => __( 'Playlist', 'automatic-youtube-gallery' ),
115 'channel' => __( 'Channel', 'automatic-youtube-gallery' ),
116 'username' => __( 'Username', 'automatic-youtube-gallery' ),
117 'search' => __( 'Search Terms', 'automatic-youtube-gallery' ),
118 'videos' => __( 'Custom Videos List', 'automatic-youtube-gallery' ),
119 'video' => __( 'Single Video', 'automatic-youtube-gallery' )
120 ),
121 'value' => 'playlist',
122 'sanitize_callback' => 'sanitize_key'
123 ),
124 array(
125 'name' => 'playlist',
126 'label' => __( 'YouTube Playlist ID (or) URL', 'automatic-youtube-gallery' ),
127 'description' => sprintf( '%s: https://www.youtube.com/playlist?list=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
128 'type' => 'url',
129 'value' => '',
130 'sanitize_callback' => 'sanitize_text_field'
131 ),
132 array(
133 'name' => 'channel',
134 'label' => __( 'YouTube Channel ID (or) URL', 'automatic-youtube-gallery' ),
135 'description' => sprintf( '%s: https://www.youtube.com/channel/XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
136 'type' => 'url',
137 'value' => '',
138 'sanitize_callback' => 'sanitize_text_field'
139 ),
140 array(
141 'name' => 'username',
142 'label' => __( 'YouTube Account Username', 'automatic-youtube-gallery' ),
143 'description' => sprintf( '%s: SanRosh', __( 'Example', 'automatic-youtube-gallery' ) ),
144 'type' => 'text',
145 'value' => '',
146 'sanitize_callback' => 'sanitize_text_field'
147 ),
148 array(
149 'name' => 'search',
150 'label' => __( 'Search Terms', 'automatic-youtube-gallery' ),
151 'description' => sprintf( '%s: Cartoon (space:AND , -:NOT , |:OR)', __( 'Example', 'automatic-youtube-gallery' ) ),
152 'type' => 'text',
153 'value' => '',
154 'sanitize_callback' => 'sanitize_text_field'
155 ),
156 array(
157 'name' => 'videos',
158 'label' => __( 'YouTube Video IDs (or) URLs', 'automatic-youtube-gallery' ),
159 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
160 'type' => 'textarea',
161 'placeholder' => __( 'Enter one video per line', 'automatic-youtube-gallery' ),
162 'value' => '',
163 'sanitize_callback' => 'sanitize_text_field'
164 ),
165 array(
166 'name' => 'video',
167 'label' => __( 'YouTube Video ID (or) URL', 'automatic-youtube-gallery' ),
168 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
169 'type' => 'url',
170 'value' => '',
171 'sanitize_callback' => 'sanitize_text_field'
172 ),
173 array(
174 'name' => 'order',
175 'label' => __( 'Order Videos by', 'automatic-youtube-gallery' ),
176 'description' => '',
177 'type' => 'select',
178 'options' => array(
179 'date' => __( 'Date', 'automatic-youtube-gallery' ),
180 'rating' => __( 'Rating', 'automatic-youtube-gallery' ),
181 'relevance' => __( 'Relevance', 'automatic-youtube-gallery' ),
182 'title' => __( 'Title', 'automatic-youtube-gallery' ),
183 'viewCount' => __( 'View Count', 'automatic-youtube-gallery' )
184 ),
185 'value' => 'relevance',
186 'sanitize_callback' => 'sanitize_key'
187 ),
188 array(
189 'name' => 'limit',
190 'label' => __( 'Number of Videos', 'automatic-youtube-gallery' ),
191 'description' => __( 'Specifies the maximum number of videos that will appear in this gallery. Set to 0 for the maximum amount (500).', 'automatic-youtube-gallery' ),
192 'type' => 'number',
193 'min' => 0,
194 'max' => 500,
195 'value' => 0,
196 'sanitize_callback' => 'intval'
197 ),
198 array(
199 'name' => 'cache',
200 'label' => __( 'Refresh the data every (Cache time)', 'automatic-youtube-gallery' ),
201 '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' ),
202 'type' => 'select',
203 'options' => array(
204 '0' => __( 'Page load', 'automatic-youtube-gallery' ),
205 '900' => __( '15 minutes', 'automatic-youtube-gallery' ),
206 '1800' => __( '30 minutes', 'automatic-youtube-gallery' ),
207 '3600' => __( 'Hour', 'automatic-youtube-gallery' ),
208 '86400' => __( 'Day', 'automatic-youtube-gallery' ),
209 '604800' => __( 'Week', 'automatic-youtube-gallery' ),
210 '2419200' => __( 'Month', 'automatic-youtube-gallery' )
211 ),
212 'value' => 0,
213 'sanitize_callback' => 'intval'
214 )
215 )
216 ),
217 'gallery' => array(
218 'label' => __( 'Gallery (optional)', 'automatic-youtube-gallery' ),
219 'fields' => ayg_get_gallery_settings_fields()
220 ),
221 'player' => array(
222 'label' => __( 'Player (optional)', 'automatic-youtube-gallery' ),
223 'fields' => ayg_get_player_settings_fields()
224 )
225 );
226
227 return apply_filters( 'ayg_editor_fields', $fields );
228 }
229
230 /**
231 * Get gallery thumbnail video excerpt (short description).
232 *
233 * @since 1.0.0
234 * @param stdClass $video YouTube video object.
235 * @param array $attributes Array of user attributes.
236 * @return string Video excerpt.
237 */
238 function ayg_get_gallery_thumb_excerpt( $video, $attributes ) {
239 $char_length = (int) $attributes['thumb_excerpt_length'];
240 $char_length++;
241
242 $content = '';
243 if ( ! empty( $attributes['thumb_excerpt'] ) ) {
244 $content = ( $char_length > 1 ) ? wp_strip_all_tags( $video->description, true ) : nl2br( $video->description );
245 }
246
247 $excerpt = '';
248
249 if ( $char_length > 1 && mb_strlen( $content ) > $char_length ) {
250 $subex = mb_substr( $content, 0, $char_length - 5 );
251 $exwords = explode( ' ', $subex );
252 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
253 if ( $excut < 0 ) {
254 $excerpt = mb_substr( $subex, 0, $excut );
255 } else {
256 $excerpt = $subex;
257 }
258 $excerpt .= '[...]';
259 } else {
260 $excerpt = $content;
261 }
262
263 return apply_filters( 'ayg_gallery_thumb_excerpt', $excerpt, $video, $attributes );
264 }
265
266 /**
267 * Get gallery thumbnail image.
268 *
269 * @since 1.0.0
270 * @param stdClass $video YouTube video object.
271 * @param array $attributes Array of user attributes.
272 * @return string Video thumbnail image.
273 */
274 function ayg_get_gallery_thumb_image( $video, $attributes ) {
275 $image = '';
276
277 if ( isset( $video->thumbnails->default ) ) {
278 $image = $video->thumbnails->default->url;
279 }
280
281 // 4:3 ( default - 120x90, high - 480x360, standard - 640x480 )
282 if ( 75 == (int) $attributes['thumb_ratio'] ) {
283 if ( isset( $video->thumbnails->high ) ) {
284 $image = $video->thumbnails->high->url;
285 }
286 }
287
288 // 16:9 ( medium - 320x180, maxres - 1280x720 )
289 if ( 56.25 == (float) $attributes['thumb_ratio'] ) {
290 if ( isset( $video->thumbnails->medium ) ) {
291 $image = $video->thumbnails->medium->url;
292 }
293 }
294
295 return apply_filters( 'ayg_gallery_thumb_image', $image, $video, $attributes );
296 }
297
298 /**
299 * Get gallery thumbnail video title.
300 *
301 * @since 1.0.0
302 * @param stdClass $video YouTube video object.
303 * @param array $attributes Array of user attributes.
304 * @return string Video title.
305 */
306 function ayg_get_gallery_thumb_title( $video, $attributes ) {
307 $text = ! empty( $attributes['thumb_title'] ) ? $video->title : '';
308
309 $char_length = (int) $attributes['thumb_title_length'];
310 $char_length++;
311
312 $title = '';
313
314 if ( $char_length > 1 && mb_strlen( $text ) > $char_length ) {
315 $subex = mb_substr( $text, 0, $char_length - 5 );
316 $exwords = explode( ' ', $subex );
317 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
318 if ( $excut < 0 ) {
319 $title = mb_substr( $subex, 0, $excut );
320 } else {
321 $title = $subex;
322 }
323 $title .= '[...]';
324 } else {
325 $title = $text;
326 }
327
328 return apply_filters( 'ayg_gallery_thumb_title', $title, $video, $attributes );
329 }
330
331 /**
332 * Get gallery settings fields.
333 *
334 * @since 1.0.0
335 * @return array $fields Array of fields.
336 */
337 function ayg_get_gallery_settings_fields() {
338 $gallery_settings = get_option( 'ayg_gallery_settings' );
339
340 $fields = array(
341 array(
342 'name' => 'theme',
343 'label' => __( 'Select Theme', 'automatic-youtube-gallery' ),
344 '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() ) ) : '' ),
345 'type' => 'select',
346 'options' => array(
347 'classic' => __( 'Classic', 'automatic-youtube-gallery' )
348 ),
349 'value' => $gallery_settings['theme'],
350 'sanitize_callback' => 'sanitize_key'
351 ),
352 array(
353 'name' => 'columns',
354 'label' => __( 'Columns', 'automatic-youtube-gallery' ),
355 'description' => __( 'Enter the number of columns you like to have in the gallery. Maximum of 12.', 'automatic-youtube-gallery' ),
356 'type' => 'number',
357 'min' => 0,
358 'max' => 12,
359 'value' => $gallery_settings['columns'],
360 'sanitize_callback' => 'intval'
361 ),
362 array(
363 'name' => 'per_page',
364 'label' => __( 'Videos per page', 'automatic-youtube-gallery' ),
365 'description' => __( 'Enter the number of videos to show per page. Maximum of 50.', 'automatic-youtube-gallery' ),
366 'type' => 'number',
367 'min' => 0,
368 'max' => 50,
369 'value' => $gallery_settings['per_page'],
370 'sanitize_callback' => 'intval'
371 ),
372 array(
373 'name' => 'thumb_ratio',
374 'label' => __( 'Thumbnail Ratio', 'automatic-youtube-gallery' ),
375 'type' => 'radio',
376 'options' => array(
377 '56.25' => '16:9',
378 '75' => '4:3'
379 ),
380 'value' => $gallery_settings['thumb_ratio'],
381 'sanitize_callback' => 'floatval'
382 ),
383 array(
384 'name' => 'thumb_title',
385 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
386 'description' => __( 'Check this option to show the video title in each gallery item.', 'automatic-youtube-gallery' ),
387 'type' => 'checkbox',
388 'value' => $gallery_settings['thumb_title'],
389 'sanitize_callback' => 'intval'
390 ),
391 array(
392 'name' => 'thumb_title_length',
393 'label' => __( 'Video Title Length', 'automatic-youtube-gallery' ),
394 'description' => __( 'Enter the number of characters you like to show in the title. Set 0 to show the whole title.', 'automatic-youtube-gallery' ),
395 'type' => 'number',
396 'min' => 0,
397 'max' => 500,
398 'value' => $gallery_settings['thumb_title_length'],
399 'sanitize_callback' => 'intval'
400 ),
401 array(
402 'name' => 'thumb_excerpt',
403 'label' => __( 'Show Video Excerpt (Short Description)', 'automatic-youtube-gallery' ),
404 'description' => __( 'Check this option to show the short description of a video in each gallery item.', 'automatic-youtube-gallery' ),
405 'type' => 'checkbox',
406 'value' => $gallery_settings['thumb_excerpt'],
407 'sanitize_callback' => 'intval'
408 ),
409 array(
410 'name' => 'thumb_excerpt_length',
411 'label' => __( 'Video Excerpt Length', 'automatic-youtube-gallery' ),
412 'description' => __( 'Enter the number of characters you like to have in the video excerpt. Set 0 to show the whole description.', 'automatic-youtube-gallery' ),
413 'type' => 'number',
414 'min' => 0,
415 'max' => 500,
416 'value' => $gallery_settings['thumb_excerpt_length'],
417 'sanitize_callback' => 'intval'
418 ),
419 array(
420 'name' => 'pagination',
421 'label' => __( 'Pagination', 'automatic-youtube-gallery' ),
422 'description' => __( 'Check this option to show the pagination.', 'automatic-youtube-gallery' ),
423 'type' => 'checkbox',
424 'value' => $gallery_settings['pagination'],
425 'sanitize_callback' => 'intval'
426 ),
427 array(
428 'name' => 'pagination_type',
429 'label' => __( 'Pagination Type', 'automatic-youtube-gallery' ),
430 'type' => 'select',
431 'options' => array(
432 'pager' => __( 'Pager', 'automatic-youtube-gallery' ),
433 'load_more' => __( 'Load More', 'automatic-youtube-gallery' )
434 ),
435 'value' => $gallery_settings['pagination_type'],
436 'sanitize_callback' => 'sanitize_key'
437 )
438 );
439
440 return apply_filters( 'ayg_gallery_settings_fields', $fields );
441 }
442
443 /**
444 * Get video description to show on top of the player.
445 *
446 * @since 1.0.0
447 * @param stdClass $video YouTube video object.
448 * @param array $attributes Array of user attributes.
449 * @param int $words_count Number of words to show by default.
450 * @return string Video description.
451 */
452 function ayg_get_player_description( $video, $attributes, $words_count = 30 ) {
453 $description = ! empty( $attributes['player_description'] ) ? $video->description : '';
454
455 $words_array = explode( ' ', strip_tags( $description ) );
456
457 if ( count( $words_array ) > $words_count ) {
458 $words_array[ $words_count ] = '<span class="ayg-player-description-dots">...</span></span><span class="ayg-player-description-more">' . $words_array[ $words_count ];
459
460 $description = '<span class="ayg-player-description-less">' . implode( ' ', $words_array ) . '</span>';
461 $description .= '<button type="button" class="ayg-player-description-toggle-btn">' . __( 'Show More', 'automatic-youtube-gallery' ) . '</button>';
462 }
463
464 return apply_filters( 'ayg_player_description', nl2br( $description ), $video, $attributes, $words_count );
465 }
466
467 /**
468 * Get YouTube player embed URL.
469 *
470 * @since 1.0.0
471 * @param stdClass $video YouTube video object.
472 * @param array $attributes Array of user attributes.
473 * @return string YouTube video embed URL.
474 */
475 function ayg_get_player_embed_url( $video, $attributes ) {
476 $url = "https://www.youtube-nocookie.com/embed/{$video->id}";
477
478 if ( ! empty( $attributes['autoplay'] ) ) { // autoplay
479 $url = add_query_arg( 'autoplay', 1, $url );
480 }
481
482 if ( ! empty( $attributes['loop'] ) ) { // loop
483 $url = add_query_arg( 'loop', 1, $url );
484 }
485
486 if ( empty( $attributes['controls'] ) ) { // controls
487 $url = add_query_arg( 'controls', 0, $url );
488 }
489
490 if ( ! empty( $attributes['modestbranding'] ) ) { // modestbranding
491 $url = add_query_arg( 'modestbranding', 1, $url );
492 }
493
494 if ( ! empty( $attributes['cc_load_policy'] ) ) { // cc_load_policy
495 $url = add_query_arg( 'cc_load_policy', 1, $url );
496 }
497
498 if ( empty( $attributes['iv_load_policy'] ) ) { // iv_load_policy
499 $url = add_query_arg( 'iv_load_policy', 3, $url );
500 }
501
502 if ( ! empty( $attributes['hl'] ) ) { // hl
503 $url = add_query_arg( 'hl', $attributes['hl'], $url );
504 }
505
506 if ( ! empty( $attributes['cc_lang_pref'] ) ) { // cc_lang_pref
507 $url = add_query_arg( 'cc_lang_pref', $attributes['cc_lang_pref'], $url );
508 }
509
510 $url = add_query_arg( 'rel', 0, $url ); // rel
511 $url = add_query_arg( 'playsinline', 1, $url ); // playsinline
512 $url = add_query_arg( 'enablejsapi', 1, $url ); // enablejsapi
513
514 return apply_filters( 'ayg_player_embed_url', $url, $video, $attributes );
515 }
516
517 /**
518 * Get player ratio.
519 *
520 * @since 1.0.0
521 * @param array $attributes Array of user attributes.
522 * @return string Player ratio.
523 */
524 function ayg_get_player_ratio( $attributes ) {
525 $ratio = ! empty( $attributes['player_ratio'] ) ? $attributes['player_ratio'] . '%' : '56.25%';
526 return apply_filters( 'ayg_player_ratio', $ratio, $attributes );
527 }
528
529 /**
530 * Get player settings fields.
531 *
532 * @since 1.0.0
533 * @return array $fields Array of fields.
534 */
535 function ayg_get_player_settings_fields() {
536 $player_settings = get_option( 'ayg_player_settings' );
537
538 $fields = array(
539 array(
540 'name' => 'player_ratio',
541 'label' => __( 'Player Ratio', 'automatic-youtube-gallery' ),
542 'type' => 'radio',
543 'options' => array(
544 '56.25' => '16:9',
545 '75' => '4:3'
546 ),
547 'value' => $player_settings['player_ratio'],
548 'sanitize_callback' => 'floatval'
549 ),
550 array(
551 'name' => 'player_title',
552 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
553 'description' => __( 'Check this option to show the current playing video title on the bottom of the player.', 'automatic-youtube-gallery' ),
554 'type' => 'checkbox',
555 'value' => $player_settings['player_title'],
556 'sanitize_callback' => 'intval'
557 ),
558 array(
559 'name' => 'player_description',
560 'label' => __( 'Show Video Description', 'automatic-youtube-gallery' ),
561 'description' => __( 'Check this option to show the current playing video description on the bottom of the player.', 'automatic-youtube-gallery' ),
562 'type' => 'checkbox',
563 'value' => $player_settings['player_description'],
564 'sanitize_callback' => 'intval'
565 ),
566 array(
567 'name' => 'autoplay',
568 'label' => __( 'Autoplay', 'automatic-youtube-gallery' ),
569 'description' => __( 'Specifies whether the initial video will automatically start to play when the player loads.', 'automatic-youtube-gallery' ),
570 'type' => 'checkbox',
571 'value' => $player_settings['autoplay'],
572 'sanitize_callback' => 'intval'
573 ),
574 array(
575 'name' => 'autoadvance',
576 'label' => __( 'Autoplay Next Video', 'automatic-youtube-gallery' ),
577 'description' => __( 'Specifies whether to play the next video in the list automatically after previous one end.', 'automatic-youtube-gallery' ),
578 'type' => 'checkbox',
579 'value' => $player_settings['autoadvance'],
580 'sanitize_callback' => 'intval'
581 ),
582 array(
583 'name' => 'loop',
584 'label' => __( 'Loop', 'automatic-youtube-gallery' ),
585 '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' ),
586 'type' => 'checkbox',
587 'value' => $player_settings['loop'],
588 'sanitize_callback' => 'intval'
589 ),
590 array(
591 'name' => 'controls',
592 'label' => __( 'Show Player Controls', 'automatic-youtube-gallery' ),
593 'description' => __( 'Uncheck this option to hide the video player controls.', 'automatic-youtube-gallery' ),
594 'type' => 'checkbox',
595 'value' => $player_settings['controls'],
596 'sanitize_callback' => 'intval'
597 ),
598 array(
599 'name' => 'modestbranding',
600 'label' => __( 'Hide YouTube Logo', 'automatic-youtube-gallery' ),
601 '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' ),
602 'type' => 'checkbox',
603 'value' => $player_settings['modestbranding'],
604 'sanitize_callback' => 'intval'
605 ),
606 array(
607 'name' => 'cc_load_policy',
608 'label' => __( 'Force Closed Captions', 'automatic-youtube-gallery' ),
609 'description' => __( 'Show captions by default, even if the user has turned captions off. The default behavior is based on user preference.', 'automatic-youtube-gallery' ),
610 'type' => 'checkbox',
611 'value' => $player_settings['cc_load_policy'],
612 'sanitize_callback' => 'intval'
613 ),
614 array(
615 'name' => 'iv_load_policy',
616 'label' => __( 'Show Annotations', 'automatic-youtube-gallery' ),
617 'description' => __( 'Choose whether to show annotations or not.', 'automatic-youtube-gallery' ),
618 'type' => 'checkbox',
619 'value' => $player_settings['iv_load_policy'],
620 'sanitize_callback' => 'intval'
621 ),
622 array(
623 'name' => 'hl',
624 'label' => __( 'Player Language', 'automatic-youtube-gallery' ),
625 'description' => sprintf(
626 __( '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' ),
627 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
628 ),
629 'type' => 'text',
630 'value' => $player_settings['hl'],
631 'sanitize_callback' => 'sanitize_text_field'
632 ),
633 array(
634 'name' => 'cc_lang_pref',
635 'label' => __( 'Default Captions Language', 'automatic-youtube-gallery' ),
636 'description' => sprintf(
637 __( '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' ),
638 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
639 ),
640 'type' => 'text',
641 'value' => $player_settings['cc_lang_pref'],
642 'sanitize_callback' => 'sanitize_text_field'
643 )
644 );
645
646 return $fields;
647 }
648
649 /**
650 * Get video title to show on bottom of the player.
651 *
652 * @since 1.0.0
653 * @param stdClass $video YouTube video object.
654 * @param array $attributes Array of user attributes.
655 * @return string Video title.
656 */
657 function ayg_get_player_title( $video, $attributes ) {
658 $title = ! empty( $attributes['player_title'] ) ? $video->title : '';
659 return apply_filters( 'ayg_player_title', $title, $video, $attributes );
660 }
661
662 /**
663 * Get player width.
664 *
665 * @since 1.0.0
666 * @param array $attributes Array of user attributes.
667 * @return string Player width.
668 */
669 function ayg_get_player_width( $attributes ) {
670 $width = ! empty( $attributes['player_width'] ) ? $attributes['player_width'] . 'px' : '100%';
671 return apply_filters( 'ayg_player_width', $width, $attributes );
672 }
673
674 /**
675 * Get filtered php template file path.
676 *
677 * @since 1.0.0
678 * @param array $template PHP file path.
679 * @param string $theme Automatic YouTube Gallery Theme.
680 * @return string Filtered file path.
681 */
682 function ayg_get_template( $template, $theme = '' ) {
683 return apply_filters( 'ayg_load_template', $template, $theme );
684 }
685
686 /**
687 * Get unique ID.
688 *
689 * @since 1.0.0
690 * @return string Unique ID.
691 */
692 function ayg_get_uniqid() {
693 global $ayg_uniqid;
694
695 if ( ! $ayg_uniqid ) {
696 $ayg_uniqid = 0;
697 }
698
699 return uniqid() . ++$ayg_uniqid;
700 }
701
702 /**
703 * Sanitize the integer inputs, accepts empty values.
704 *
705 * @since 1.0.0
706 * @param string|int $value Input value.
707 * @return string|int Sanitized value.
708 */
709 function ayg_sanitize_int( $value ) {
710 $value = intval( $value );
711 return ( 0 == $value ) ? '' : $value;
712 }
713
714 /**
715 * Gallery HTML output.
716 *
717 * @since 1.0.0
718 * @param array $videos Array of YouTube video object.
719 * @param array $attributes Array of user attributes.
720 * @param int $active_id Current active/selected video ID in the gallery.
721 */
722 function the_ayg_gallery( $videos, $attributes, $active_id = 0 ) {
723 if ( 'video' != $attributes['type'] ) {
724 include ayg_get_template( AYG_DIR . 'public/templates/gallery.php', $attributes['theme'] );
725 }
726 }
727
728 /**
729 * Gallery HTML output.
730 *
731 * @since 1.0.0
732 * @param array $video YouTube video object.
733 * @param array $attributes Array of user attributes.
734 * @param boolean $is_active True if active video item, false if not.
735 */
736 function the_ayg_gallery_thumbnail( $video, $attributes, $is_active = false ) {
737 include ayg_get_template( AYG_DIR . 'public/templates/gallery-thumbnail.php', $attributes['theme'] );
738 }
739
740 /**
741 * Pagination HTML output.
742 *
743 * @since 1.0.0
744 * @param array $atts Array of user attributes.
745 */
746 function the_ayg_pagination( $atts ) {
747 if ( ! empty( $atts['pagination'] ) ) {
748 // Build attributes
749 $attributes = array();
750
751 $fields = ayg_get_editor_fields();
752
753 foreach ( $fields['gallery']['fields'] as $field ) {
754 $field_name = $field['name'];
755
756 if ( ! in_array( $field_name, array( 'per_page', 'pagination' ) ) ) {
757 $attributes[ $field_name ] = sanitize_text_field( $atts[ $field_name ] );
758 }
759 }
760
761 $source_type = sanitize_text_field( $atts['type'] );
762
763 $attributes = array_merge(
764 $attributes,
765 array(
766 'type' => $source_type,
767 'id' => sanitize_text_field( $atts[ $source_type ] ),
768 'order' => sanitize_text_field( $atts['order'] ), // works only when type=search
769 'per_page' => (int) $atts['per_page'],
770 'cache' => (int) $atts['cache'],
771 'player_description' => ! empty( $atts['player_description'] ) ? (int) $atts['player_description'] : 0,
772 'num_pages' => 1,
773 'paged' => ! empty( $atts['paged'] ) ? (int) $atts['paged'] : 1,
774 'next_page_token' => ! empty( $atts['next_page_token'] ) ? sanitize_text_field( $atts['next_page_token'] ) : '',
775 'prev_page_token' => ! empty( $atts['prev_page_token'] ) ? sanitize_text_field( $atts['prev_page_token'] ) : ''
776 )
777 );
778
779 // Find total number of pages
780 $videos_found = ! empty( $atts['videos_found'] ) ? (int) $atts['videos_found'] : 0;
781
782 if ( $videos_found > 0 ) {
783 if ( 'search' == $source_type ) {
784 $limit = min( (int) $atts['limit'], $videos_found );
785 $attributes['num_pages'] = ceil( $limit / $attributes['per_page'] );
786 } else {
787 $attributes['num_pages'] = ceil( $videos_found / $attributes['per_page'] );
788 }
789 }
790
791 // Process output
792 if ( $attributes['num_pages'] > 1 ) {
793 $pagination_type = ( 'pager' == $atts['pagination_type'] ) ? 'pager' : 'loadmore';
794 include ayg_get_template( AYG_DIR . 'public/templates/pagination-' . $pagination_type. '.php', $attributes['theme'] );
795 }
796 }
797 }
798
799 /**
800 * Player HTML output.
801 *
802 * @since 1.0.0
803 * @param stdClass $video YouTube video object.
804 * @param array $attributes Array of user attributes.
805 */
806 function the_ayg_player( $video, $attributes ) {
807 include ayg_get_template( AYG_DIR . 'public/templates/player.php', $attributes['theme'] );
808 }
809