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

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