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

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