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

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