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

1,001 lines 34.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 $args An array of gallery options.
22 * @return mixed
23 */
24 function ayg_build_gallery( $args ) {
25 global $post;
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, $args );
38
39 $attributes['post_id'] = 0;
40 if ( isset( $post->ID ) ) {
41 $attributes['post_id'] = (int) $post->ID;
42 }
43
44 $attributes['columns'] = min( 12, (int) $attributes['columns'] );
45 if ( empty( $attributes['columns'] ) ) {
46 $attributes['columns'] = 3;
47 }
48
49 $attributes['limit'] = min( 500, (int) $attributes['limit'] );
50 if ( empty( $attributes['limit'] ) ) {
51 $attributes['limit'] = 500;
52 }
53
54 $attributes['per_page'] = min( 50, (int) $attributes['per_page'] );
55 if ( empty( $attributes['per_page'] ) ) {
56 $attributes['per_page'] = 50;
57 }
58
59 $source_type = sanitize_text_field( $attributes['type'] );
60 if ( 'livestream' == $source_type ) {
61 $attributes['livestream'] = $attributes['channel'];
62 }
63
64 if ( isset( $args['uid'] ) && ! empty( $args['uid'] ) ) {
65 $attributes['uid'] = $args['uid'];
66 } else {
67 $attributes['uid'] = md5( $source_type . sanitize_text_field( $attributes[ $source_type ] ) . sanitize_text_field( $attributes['theme'] ) );
68 }
69
70 // Get Videos
71 $api_params = array(
72 'type' => $source_type,
73 'src' => sanitize_text_field( $attributes[ $source_type ] ),
74 'order' => sanitize_text_field( $attributes['order'] ), // applicable only when type=search
75 'maxResults' => $attributes['per_page'],
76 'cache' => (int) $attributes['cache']
77 );
78
79 $api_params = apply_filters( 'ayg_youtube_api_request_params', $api_params, $args );
80
81 $youtube_api = new AYG_YouTube_API();
82 $response = $youtube_api->query( $api_params );
83
84 // Process output
85 if ( ! isset( $response->error ) ) {
86 // Store Gallery ID
87 if ( $attributes['post_id'] > 0 && isset( $attributes['deeplinking'] ) && 1 == $attributes['deeplinking'] ) {
88 $pages = get_option( 'ayg_gallery_page_ids', array() );
89 $page_id = $attributes['post_id'];
90
91 if ( ! in_array( $page_id, $pages ) ) {
92 $pages[] = $page_id;
93 update_option( 'ayg_gallery_page_ids', $pages );
94 }
95 }
96
97 // Enqueue dependencies
98 wp_enqueue_style( AYG_SLUG . '-public' );
99 wp_enqueue_script( AYG_SLUG . '-public' );
100
101 // Gallery
102 $videos = array();
103
104 $gallery_id_from_url = get_query_var( 'ayg_gallery_id' );
105 $video_id_from_url = get_query_var( 'ayg_video_id' );
106
107 if ( $attributes['uid'] == $gallery_id_from_url ) {
108 $video = ayg_db_get_video( $video_id_from_url );
109
110 if ( $video ) {
111 $videos[] = $video;
112 } else {
113 $video_id_from_url = '';
114 }
115 }
116
117 if ( isset( $response->videos ) ) {
118 if ( ! empty( $video_id_from_url ) ) {
119 foreach ( $response->videos as $video ) {
120 if ( $video->id != $video_id_from_url ) {
121 $videos[] = $video;
122 }
123 }
124 } else {
125 $videos = $response->videos;
126 }
127 }
128
129 // Pagination
130 if ( isset( $response->page_info ) ) {
131 $attributes = array_merge( $attributes, $api_params, $response->page_info );
132 }
133
134 // Theme
135 $theme = 'classic';
136
137 if ( 'video' == $source_type ) {
138 $theme = 'single';
139 } elseif ( 'livestream' == $source_type ) {
140 $theme = 'livestream';
141 } else {
142 if ( 1 == count( $videos ) ) {
143 $theme = 'single';
144 }
145 }
146
147 // Output
148 ob_start();
149 include ayg_get_template( AYG_DIR . "public/templates/theme-{$theme}.php", $attributes['theme'] );
150 return ob_get_clean();
151 } else {
152 return '<p class="ayg-error">' . $response->error_message . '</p>';
153 }
154 }
155
156 /**
157 * Create a custom database table "{$wpdb->prefix}ayg_videos"
158 *
159 * @since 2.1.0
160 */
161 function ayg_db_create_videos_table() {
162 global $wpdb;
163
164 $charset_collate = $wpdb->get_charset_collate();
165
166 $sql = "CREATE TABLE `{$wpdb->prefix}ayg_videos` (
167 NUM bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
168 id varchar(100) NOT NULL,
169 title text NOT NULL,
170 description text NOT NULL,
171 thumbnails text NOT NULL,
172 duration varchar(25) NOT NULL,
173 status varchar(100) NOT NULL,
174 published_at varchar(100) NOT NULL,
175 PRIMARY KEY (NUM)
176 ) $charset_collate;";
177
178 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
179 dbDelta( $sql );
180 }
181
182 /**
183 * Store videos in our custom database table "{$wpdb->prefix}ayg_videos"
184 *
185 * @since 2.1.0
186 * @param object $data YouTube API response object.
187 */
188 function ayg_db_store_videos( $data ) {
189 if ( AYG_VERSION !== get_option( 'ayg_version' ) ) {
190 return false;
191 }
192
193 global $wpdb, $post;
194
195 $table_name = $wpdb->prefix . 'ayg_videos';
196
197 if ( isset( $data->kind ) && 'youtube#channelListResponse' == $data->kind ) {
198 return false;
199 }
200
201 if ( ! isset( $data->items ) ) {
202 return false;
203 }
204
205 $items = $data->items;
206 if ( ! is_array( $items ) || 0 == count( $items ) ) {
207 return false;
208 }
209
210 // Store Videos
211 foreach ( $items as $item ) {
212 $row = array();
213
214 // Video ID
215 $row['id'] = '';
216
217 if ( isset( $item->snippet->resourceId ) && isset( $item->snippet->resourceId->videoId ) ) {
218 $row['id'] = $item->snippet->resourceId->videoId;
219 } elseif ( isset( $item->contentDetails ) && isset( $item->contentDetails->videoId ) ) {
220 $row['id'] = $item->contentDetails->videoId;
221 } elseif ( isset( $item->id ) && isset( $item->id->videoId ) ) {
222 $row['id'] = $item->id->videoId;
223 } elseif ( isset( $item->id ) ) {
224 $row['id'] = $item->id;
225 }
226
227 if ( empty( $row['id'] ) ) {
228 continue;
229 }
230
231 // Video title
232 $row['title'] = $item->snippet->title;
233
234 // Video description
235 $row['description'] = $item->snippet->description;
236
237 // Video thumbnails
238 $row['thumbnails'] = '';
239 if ( isset( $item->snippet->thumbnails ) ) {
240 $row['thumbnails'] = serialize( $item->snippet->thumbnails );
241 }
242
243 // Video duration
244 $row['duration'] = '';
245 if ( isset( $item->contentDetails ) && isset( $item->contentDetails->duration ) ) {
246 $row['duration'] = $item->contentDetails->duration;
247 }
248
249 // Video status
250 $row['status'] = 'private';
251
252 if ( isset( $item->status ) && ( 'public' == $item->status->privacyStatus || 'unlisted' == $item->status->privacyStatus ) ) {
253 $row['status'] = 'public';
254 }
255
256 if ( isset( $item->snippet->status ) && ( 'public' == $item->snippet->status->privacyStatus || 'unlisted' == $item->snippet->status->privacyStatus ) ) {
257 $row['status'] = 'public';
258 }
259
260 if ( 'youtube#searchResult' == $item->kind ) {
261 $row['status'] = 'public';
262 }
263
264 // Video publish date
265 $row['published_at'] = $item->snippet->publishedAt;
266
267 // Store
268 $query = $wpdb->prepare( "SELECT NUM FROM $table_name WHERE id = %s", $row['id'] );
269 $insert_id = $wpdb->get_var( $query );
270
271 if ( empty( $insert_id ) ) {
272 $wpdb->insert(
273 $table_name,
274 $row,
275 array( '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
276 );
277 } else {
278 $wpdb->update(
279 $table_name,
280 $row,
281 array( 'NUM' => $insert_id ),
282 array( '%s', '%s', '%s', '%s', '%s', '%s', '%s' ),
283 array( '%d' )
284 );
285 }
286 }
287 }
288
289 /**
290 * Get a single video record from our custom database table "{$wpdb->prefix}ayg_videos"
291 *
292 * @since 2.1.0
293 * @param string $video_id YouTube Video ID.
294 * @return mixed
295 */
296 function ayg_db_get_video( $video_id ) {
297 global $wpdb;
298
299 $cache_key = 'ayg_'. $video_id;
300 $row = wp_cache_get( $cache_key );
301
302 if ( false === $row ) {
303 $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ayg_videos WHERE id = %s", $video_id );
304 $row = $wpdb->get_row( $query );
305
306 if ( $row ) {
307 if ( ! empty( $row->thumbnails ) ) {
308 $row->thumbnails = unserialize( $row->thumbnails );
309 }
310
311 wp_cache_set( $cache_key, $row );
312 }
313 }
314
315 return $row;
316 }
317
318 /**
319 * Dump our plugin transients.
320 *
321 * @since 2.1.0
322 */
323 function ayg_delete_cache() {
324 delete_option( 'ayg_gallery_page_ids' );
325
326 // Get the current list of transients
327 $transient_keys = get_option( 'ayg_transient_keys', array() );
328
329 // For each key, delete that transient
330 foreach ( $transient_keys as $key ) {
331 delete_transient( $key );
332 }
333
334 // Reset our DB value
335 update_option( 'ayg_transient_keys', array() );
336 }
337
338 /**
339 * Get current address bar URL.
340 *
341 * @since 2.1.0
342 * @return string Current Page URL.
343 */
344 function ayg_get_current_url() {
345 global $wp;
346 $current_url = home_url( add_query_arg( array(), $wp->request ) );
347
348 return $current_url;
349 }
350
351 /**
352 * Get default plugin settings.
353 *
354 * @since 1.6.4
355 * @return array $defaults Array of plugin settings.
356 */
357 function ayg_get_default_settings() {
358 $defaults = array(
359 'ayg_general_settings' => array(
360 'api_key' => '',
361 'development_mode' => 1
362 ),
363 'ayg_gallery_settings' => array(
364 'theme' => 'classic',
365 'columns' => 3,
366 'per_page' => 12,
367 'thumb_ratio' => 56.25,
368 'thumb_title' => 1,
369 'thumb_title_length' => 0,
370 'thumb_excerpt' => 1,
371 'thumb_excerpt_length' => 75,
372 'pagination' => 1,
373 'pagination_type' => 'more',
374 'more_button_label' => __( 'Load More', 'automatic-youtube-gallery' ),
375 'previous_button_label' => __( 'Previous', 'automatic-youtube-gallery' ),
376 'next_button_label' => __( 'Next', 'automatic-youtube-gallery' )
377 ),
378 'ayg_player_settings' => array(
379 'player_ratio' => 56.25,
380 'player_title' => 1,
381 'player_description' => 1,
382 'autoplay' => 0,
383 'autoadvance' => 1,
384 'loop' => 0,
385 'muted' => 0,
386 'controls' => 1,
387 'modestbranding' => 1,
388 'cc_load_policy' => 0,
389 'iv_load_policy' => 0,
390 'hl' => '',
391 'cc_lang_pref' => '',
392 'privacy_enhanced_mode' => 0,
393 'origin' => 0
394 ),
395 'ayg_livestream_settings' => array(
396 'fallback_message' => __( 'Sorry, but the channel is not currently streaming live content. Please check back later.', 'automatic-youtube-gallery' )
397 ),
398 'ayg_privacy_settings' => array(
399 'cookie_consent' => 0,
400 'consent_message' => __( 'Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.', 'automatic-youtube-gallery' ),
401 'button_label' => __( 'Accept', 'automatic-youtube-gallery' )
402 )
403 );
404
405 return $defaults;
406 }
407
408 /**
409 * Get editor fields.
410 *
411 * @since 1.0.0
412 * @return array Array of fields.
413 */
414 function ayg_get_editor_fields() {
415 $fields = array(
416 'source' => array(
417 'label' => __( 'General', 'automatic-youtube-gallery' ),
418 'fields' => array(
419 array(
420 'name' => 'type',
421 'label' => __( 'Source Type', 'automatic-youtube-gallery' ),
422 'description' => '',
423 'type' => 'select',
424 'options' => array(
425 'playlist' => __( 'Playlist', 'automatic-youtube-gallery' ),
426 'channel' => __( 'Channel', 'automatic-youtube-gallery' ),
427 'username' => __( 'Username', 'automatic-youtube-gallery' ),
428 'search' => __( 'Search Keywords', 'automatic-youtube-gallery' ),
429 'video' => __( 'Single Video', 'automatic-youtube-gallery' ),
430 'livestream' => __( 'Livestream', 'automatic-youtube-gallery' ),
431 'videos' => __( 'Custom Videos List', 'automatic-youtube-gallery' )
432 ),
433 'value' => 'playlist',
434 'sanitize_callback' => 'sanitize_key'
435 ),
436 array(
437 'name' => 'playlist',
438 'label' => __( 'YouTube Playlist ID (or) URL', 'automatic-youtube-gallery' ),
439 'description' => sprintf( '%s: https://www.youtube.com/playlist?list=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
440 'type' => 'url',
441 'value' => '',
442 'sanitize_callback' => 'sanitize_text_field'
443 ),
444 array(
445 'name' => 'channel',
446 'label' => __( 'YouTube Channel ID (or) a YouTube Video URL from the Channel', 'automatic-youtube-gallery' ),
447 'description' => sprintf( '%s: https://www.youtube.com/channel/XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
448 'type' => 'url',
449 'value' => '',
450 'sanitize_callback' => 'sanitize_text_field'
451 ),
452 array(
453 'name' => 'username',
454 'label' => __( 'YouTube Account Username', 'automatic-youtube-gallery' ),
455 'description' => sprintf( '%s: SanRosh', __( 'Example', 'automatic-youtube-gallery' ) ),
456 'type' => 'text',
457 'value' => '',
458 'sanitize_callback' => 'sanitize_text_field'
459 ),
460 array(
461 'name' => 'search',
462 'label' => __( 'Search Keywords', 'automatic-youtube-gallery' ),
463 'description' => sprintf( '%s: Cartoon (space:AND , -:NOT , |:OR)', __( 'Example', 'automatic-youtube-gallery' ) ),
464 'type' => 'text',
465 'value' => '',
466 'sanitize_callback' => 'sanitize_text_field'
467 ),
468 array(
469 'name' => 'video',
470 'label' => __( 'YouTube Video ID (or) URL', 'automatic-youtube-gallery' ),
471 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
472 'type' => 'url',
473 'value' => '',
474 'sanitize_callback' => 'sanitize_text_field'
475 ),
476 array(
477 'name' => 'videos',
478 'label' => __( 'YouTube Video IDs (or) URLs', 'automatic-youtube-gallery' ),
479 'description' => sprintf( '%s: https://www.youtube.com/watch?v=XXXXXXXXXX', __( 'Example', 'automatic-youtube-gallery' ) ),
480 'type' => 'textarea',
481 'placeholder' => __( 'Enter one video per line', 'automatic-youtube-gallery' ),
482 'value' => '',
483 'sanitize_callback' => 'sanitize_text_field'
484 ),
485 array(
486 'name' => 'order',
487 'label' => __( 'Order Videos by', 'automatic-youtube-gallery' ),
488 'description' => '',
489 'type' => 'select',
490 'options' => array(
491 'date' => __( 'Date', 'automatic-youtube-gallery' ),
492 'rating' => __( 'Rating', 'automatic-youtube-gallery' ),
493 'relevance' => __( 'Relevance', 'automatic-youtube-gallery' ),
494 'title' => __( 'Title', 'automatic-youtube-gallery' ),
495 'viewCount' => __( 'View Count', 'automatic-youtube-gallery' )
496 ),
497 'value' => 'relevance',
498 'sanitize_callback' => 'sanitize_key'
499 ),
500 array(
501 'name' => 'limit',
502 'label' => __( 'Number of Videos', 'automatic-youtube-gallery' ),
503 'description' => __( 'Specifies the maximum number of videos that will appear in this gallery. Set to 0 for the maximum amount (500).', 'automatic-youtube-gallery' ),
504 'type' => 'number',
505 'min' => 0,
506 'max' => 500,
507 'value' => 0,
508 'sanitize_callback' => 'intval'
509 ),
510 array(
511 'name' => 'cache',
512 'label' => __( 'Cache Duration', 'automatic-youtube-gallery' ),
513 'description' => __( 'Specifies how frequently we should check your YouTube source for new videos/updates.', 'automatic-youtube-gallery' ),
514 'type' => 'select',
515 'options' => array(
516 '900' => __( '15 Minutes', 'automatic-youtube-gallery' ),
517 '1800' => __( '30 Minutes', 'automatic-youtube-gallery' ),
518 '3600' => __( '1 Hour', 'automatic-youtube-gallery' ),
519 '86400' => __( '1 Day', 'automatic-youtube-gallery' ),
520 '604800' => __( '1 Week', 'automatic-youtube-gallery' ),
521 '2419200' => __( '1 Month', 'automatic-youtube-gallery' )
522 ),
523 'value' => 86400,
524 'sanitize_callback' => 'intval'
525 )
526 )
527 ),
528 'gallery' => array(
529 'label' => __( 'Gallery (optional)', 'automatic-youtube-gallery' ),
530 'fields' => ayg_get_gallery_settings_fields()
531 ),
532 'player' => array(
533 'label' => __( 'Player (optional)', 'automatic-youtube-gallery' ),
534 'fields' => ayg_get_player_settings_fields()
535 )
536 );
537
538 return apply_filters( 'ayg_editor_fields', $fields );
539 }
540
541 /**
542 * Get gallery settings fields.
543 *
544 * @since 1.0.0
545 * @return array $fields Array of fields.
546 */
547 function ayg_get_gallery_settings_fields() {
548 $gallery_settings = get_option( 'ayg_gallery_settings' );
549
550 $fields = array(
551 array(
552 'name' => 'theme',
553 'label' => __( 'Select Theme (Layout)', 'automatic-youtube-gallery' ),
554 'description' => ( ayg_fs()->is_not_paying() ? sprintf( __( '<a href="%s">Upgrade Pro</a> for more themes (Popup, Inline, Slider, Playlist).', 'automatic-youtube-gallery' ), esc_url( ayg_fs()->get_upgrade_url() ) ) : '' ),
555 'type' => 'select',
556 'options' => array(
557 'classic' => __( 'Classic', 'automatic-youtube-gallery' )
558 ),
559 'value' => $gallery_settings['theme'],
560 'sanitize_callback' => 'sanitize_key'
561 ),
562 array(
563 'name' => 'columns',
564 'label' => __( 'Columns', 'automatic-youtube-gallery' ),
565 'description' => __( 'Enter the number of columns you like to have in the gallery. Maximum of 12.', 'automatic-youtube-gallery' ),
566 'type' => 'number',
567 'min' => 0,
568 'max' => 12,
569 'value' => $gallery_settings['columns'],
570 'sanitize_callback' => 'intval'
571 ),
572 array(
573 'name' => 'per_page',
574 'label' => __( 'Videos per Page', 'automatic-youtube-gallery' ),
575 'description' => __( 'Enter the number of videos to show per page. Maximum of 50.', 'automatic-youtube-gallery' ),
576 'type' => 'number',
577 'min' => 0,
578 'max' => 50,
579 'value' => $gallery_settings['per_page'],
580 'sanitize_callback' => 'intval'
581 ),
582 array(
583 'name' => 'thumb_ratio',
584 'label' => __( 'Image Height (Ratio)', 'automatic-youtube-gallery' ),
585 'description' => __( 'Select the ratio value used to calculate the image height in the gallery thumbnails.', 'automatic-youtube-gallery' ),
586 'type' => 'radio',
587 'options' => array(
588 '56.25' => '16:9',
589 '75' => '4:3'
590 ),
591 'value' => $gallery_settings['thumb_ratio'],
592 'sanitize_callback' => 'floatval'
593 ),
594 array(
595 'name' => 'thumb_title',
596 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
597 'description' => __( 'Check this option to show the video title in each gallery item.', 'automatic-youtube-gallery' ),
598 'type' => 'checkbox',
599 'value' => $gallery_settings['thumb_title'],
600 'sanitize_callback' => 'intval'
601 ),
602 array(
603 'name' => 'thumb_title_length',
604 'label' => __( 'Video Title Length', 'automatic-youtube-gallery' ),
605 'description' => __( 'Enter the number of characters you like to show in the title. Set 0 to show the whole title.', 'automatic-youtube-gallery' ),
606 'type' => 'number',
607 'min' => 0,
608 'max' => 500,
609 'value' => $gallery_settings['thumb_title_length'],
610 'sanitize_callback' => 'intval'
611 ),
612 array(
613 'name' => 'thumb_excerpt',
614 'label' => __( 'Show Video Excerpt (Short Description)', 'automatic-youtube-gallery' ),
615 'description' => __( 'Check this option to show the short description of a video in each gallery item.', 'automatic-youtube-gallery' ),
616 'type' => 'checkbox',
617 'value' => $gallery_settings['thumb_excerpt'],
618 'sanitize_callback' => 'intval'
619 ),
620 array(
621 'name' => 'thumb_excerpt_length',
622 'label' => __( 'Video Excerpt Length', 'automatic-youtube-gallery' ),
623 'description' => __( 'Enter the number of characters you like to have in the video excerpt. Set 0 to show the whole description.', 'automatic-youtube-gallery' ),
624 'type' => 'number',
625 'min' => 0,
626 'max' => 500,
627 'value' => $gallery_settings['thumb_excerpt_length'],
628 'sanitize_callback' => 'intval'
629 ),
630 array(
631 'name' => 'pagination',
632 'label' => __( 'Pagination', 'automatic-youtube-gallery' ),
633 'description' => __( 'Check this option to show the pagination.', 'automatic-youtube-gallery' ),
634 'type' => 'checkbox',
635 'value' => $gallery_settings['pagination'],
636 'sanitize_callback' => 'intval'
637 ),
638 array(
639 'name' => 'pagination_type',
640 'label' => __( 'Pagination Type', 'automatic-youtube-gallery' ),
641 'type' => 'select',
642 'options' => array(
643 'more' => __( 'More Button', 'automatic-youtube-gallery' ),
644 'pager' => __( 'Pager', 'automatic-youtube-gallery' )
645
646 ),
647 'value' => $gallery_settings['pagination_type'],
648 'sanitize_callback' => 'sanitize_key'
649 ),
650 array(
651 'name' => 'more_button_label',
652 'label' => __( 'More Button Label', 'automatic-youtube-gallery' ),
653 'description' => __( 'Enter the more button label text.', 'automatic-youtube-gallery' ),
654 'type' => 'text',
655 'value' => isset( $gallery_settings['more_button_label'] ) ? $gallery_settings['more_button_label'] : __( 'Load More', 'automatic-youtube-gallery' ),
656 'sanitize_callback' => 'sanitize_text_field'
657 ),
658 array(
659 'name' => 'previous_button_label',
660 'label' => __( 'Previous Button Label', 'automatic-youtube-gallery' ),
661 'description' => __( 'Enter the previous button label text.', 'automatic-youtube-gallery' ),
662 'type' => 'text',
663 'value' => isset( $gallery_settings['previous_button_label'] ) ? $gallery_settings['previous_button_label'] : __( 'Previous', 'automatic-youtube-gallery' ),
664 'sanitize_callback' => 'sanitize_text_field'
665 ),
666 array(
667 'name' => 'next_button_label',
668 'label' => __( 'Next Button Label', 'automatic-youtube-gallery' ),
669 'description' => __( 'Enter the next button label text.', 'automatic-youtube-gallery' ),
670 'type' => 'text',
671 'value' => isset( $gallery_settings['next_button_label'] ) ? $gallery_settings['next_button_label'] : __( 'Next', 'automatic-youtube-gallery' ),
672 'sanitize_callback' => 'sanitize_text_field'
673 )
674 );
675
676 return apply_filters( 'ayg_gallery_settings_fields', $fields );
677 }
678
679 /**
680 * Get video description to show on top of the player.
681 *
682 * @since 1.0.0
683 * @param stdClass $video YouTube video object.
684 * @param int $words_count Number of words to show by default.
685 * @return string Video description.
686 */
687 function ayg_get_player_description( $video, $words_count = 30 ) {
688 $description = $video->description;
689
690 $words_array = explode( ' ', strip_tags( $description ) );
691 if ( count( $words_array ) > $words_count ) {
692 $words_array[ $words_count ] = '<span class="ayg-player-description-dots">...</span></span><span class="ayg-player-description-more">' . $words_array[ $words_count ];
693
694 $description = '<span class="ayg-player-description-less">' . implode( ' ', $words_array ) . '</span>';
695 $description .= '<a href="#" class="ayg-player-description-toggle-btn">[+] ' . __( 'Show More', 'automatic-youtube-gallery' ) . '</a>';
696 }
697
698 $description = nl2br( $description );
699 $description = make_clickable( $description );
700
701 return apply_filters( 'ayg_player_description', $description, $video, $words_count );
702 }
703
704 /**
705 * Get player settings fields.
706 *
707 * @since 1.0.0
708 * @return array $fields Array of fields.
709 */
710 function ayg_get_player_settings_fields() {
711 $player_settings = get_option( 'ayg_player_settings' );
712
713 $fields = array(
714 array(
715 'name' => 'player_ratio',
716 'label' => __( 'Player Height (Ratio)', 'automatic-youtube-gallery' ),
717 'description' => __( 'Select the ratio value used to calculate the player height.', 'automatic-youtube-gallery' ),
718 'type' => 'radio',
719 'options' => array(
720 '56.25' => '16:9',
721 '75' => '4:3'
722 ),
723 'value' => $player_settings['player_ratio'],
724 'sanitize_callback' => 'floatval'
725 ),
726 array(
727 'name' => 'player_title',
728 'label' => __( 'Show Video Title', 'automatic-youtube-gallery' ),
729 'description' => __( 'Check this option to show the current playing video title on the bottom of the player.', 'automatic-youtube-gallery' ),
730 'type' => 'checkbox',
731 'value' => $player_settings['player_title'],
732 'sanitize_callback' => 'intval'
733 ),
734 array(
735 'name' => 'player_description',
736 'label' => __( 'Show Video Description', 'automatic-youtube-gallery' ),
737 'description' => __( 'Check this option to show the current playing video description on the bottom of the player.', 'automatic-youtube-gallery' ),
738 'type' => 'checkbox',
739 'value' => $player_settings['player_description'],
740 'sanitize_callback' => 'intval'
741 ),
742 array(
743 'name' => 'autoplay',
744 'label' => __( 'Autoplay', 'automatic-youtube-gallery' ),
745 'description' => __( 'Specifies whether the initial video will automatically start to play when the player loads.', 'automatic-youtube-gallery' ),
746 'type' => 'checkbox',
747 'value' => $player_settings['autoplay'],
748 'sanitize_callback' => 'intval'
749 ),
750 array(
751 'name' => 'autoadvance',
752 'label' => __( 'Autoplay Next Video', 'automatic-youtube-gallery' ),
753 'description' => __( 'Specifies whether to play the next video in the list automatically after previous one end.', 'automatic-youtube-gallery' ),
754 'type' => 'checkbox',
755 'value' => $player_settings['autoadvance'],
756 'sanitize_callback' => 'intval'
757 ),
758 array(
759 'name' => 'loop',
760 'label' => __( 'Loop', 'automatic-youtube-gallery' ),
761 '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' ),
762 'type' => 'checkbox',
763 'value' => $player_settings['loop'],
764 'sanitize_callback' => 'intval'
765 ),
766 array(
767 'name' => 'muted',
768 'label' => __( 'Muted', 'automatic-youtube-gallery' ),
769 'description' => __( 'Check this option to turn OFF the audio output of the video by default.', 'automatic-youtube-gallery' ),
770 'type' => 'checkbox',
771 'value' => isset( $player_settings['muted'] ) ? $player_settings['muted'] : 0,
772 'sanitize_callback' => 'intval'
773 ),
774 array(
775 'name' => 'controls',
776 'label' => __( 'Show Player Controls', 'automatic-youtube-gallery' ),
777 'description' => __( 'Uncheck this option to hide the video player controls.', 'automatic-youtube-gallery' ),
778 'type' => 'checkbox',
779 'value' => $player_settings['controls'],
780 'sanitize_callback' => 'intval'
781 ),
782 array(
783 'name' => 'modestbranding',
784 'label' => __( 'Hide YouTube Logo', 'automatic-youtube-gallery' ),
785 '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' ),
786 'type' => 'checkbox',
787 'value' => $player_settings['modestbranding'],
788 'sanitize_callback' => 'intval'
789 ),
790 array(
791 'name' => 'cc_load_policy',
792 'label' => __( 'Force Closed Captions', 'automatic-youtube-gallery' ),
793 'description' => __( 'Show captions by default, even if the user has turned captions off. The default behavior is based on user preference.', 'automatic-youtube-gallery' ),
794 'type' => 'checkbox',
795 'value' => $player_settings['cc_load_policy'],
796 'sanitize_callback' => 'intval'
797 ),
798 array(
799 'name' => 'iv_load_policy',
800 'label' => __( 'Show Annotations', 'automatic-youtube-gallery' ),
801 'description' => __( 'Choose whether to show annotations or not.', 'automatic-youtube-gallery' ),
802 'type' => 'checkbox',
803 'value' => $player_settings['iv_load_policy'],
804 'sanitize_callback' => 'intval'
805 ),
806 array(
807 'name' => 'hl',
808 'label' => __( 'Player Language', 'automatic-youtube-gallery' ),
809 'description' => sprintf(
810 __( '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' ),
811 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
812 ),
813 'type' => 'text',
814 'value' => $player_settings['hl'],
815 'sanitize_callback' => 'sanitize_text_field'
816 ),
817 array(
818 'name' => 'cc_lang_pref',
819 'label' => __( 'Default Captions Language', 'automatic-youtube-gallery' ),
820 'description' => sprintf(
821 __( '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' ),
822 'http://www.loc.gov/standards/iso639-2/php/code_list.php'
823 ),
824 'type' => 'text',
825 'value' => $player_settings['cc_lang_pref'],
826 'sanitize_callback' => 'sanitize_text_field'
827 ),
828 array(
829 'name' => 'privacy_enhanced_mode',
830 'label' => __( 'Privacy Enhanced Mode', 'automatic-youtube-gallery' ),
831 'description' => __( "Prevent YouTube from leaving tracking cookies on your visitor's browsers unless they actually play the videos. Please uncheck this option if you see errors while testing your playlist embeds or watching your videos on mobile.", 'automatic-youtube-gallery' ),
832 'type' => 'checkbox',
833 'value' => isset( $player_settings['privacy_enhanced_mode'] ) ? $player_settings['privacy_enhanced_mode'] : 0,
834 'sanitize_callback' => 'intval'
835 ),
836 array(
837 'name' => 'origin',
838 'label' => __( 'Extra Player Security', 'automatic-youtube-gallery' ),
839 'description' => __( 'Add site origin information with each embed code as an extra security measure. In YouTube\'s own words, checking this option "protects against malicious third-party JavaScript being injected into your page and hijacking control of your YouTube player."', 'automatic-youtube-gallery' ),
840 'type' => 'checkbox',
841 'value' => isset( $player_settings['origin'] ) ? $player_settings['origin'] : 0,
842 'sanitize_callback' => 'intval'
843 ),
844 );
845
846 return $fields;
847 }
848
849
850 /**
851 * Get a single video page URL.
852 *
853 * @since 2.1.0
854 * @param stdClass $video YouTube video object.
855 * @param array $args An array of gallery options.
856 * @return string Single video URL.
857 */
858 function ayg_get_single_video_url( $video, $attributes ) {
859 return apply_filters( 'ayg_single_video_url', '', $video, $attributes );
860 }
861
862 /**
863 * Get filtered php template file path.
864 *
865 * @since 1.0.0
866 * @param array $template PHP file path.
867 * @param string $theme Automatic YouTube Gallery Theme.
868 * @return string Filtered file path.
869 */
870 function ayg_get_template( $template, $theme = '' ) {
871 return apply_filters( 'ayg_load_template', $template, $theme );
872 }
873
874 /**
875 * Get unique ID.
876 *
877 * @since 1.0.0
878 * @return string Unique ID.
879 */
880 function ayg_get_uniqid() {
881 global $ayg_uniqid;
882
883 if ( ! $ayg_uniqid ) {
884 $ayg_uniqid = 0;
885 }
886
887 return uniqid() . ++$ayg_uniqid;
888 }
889
890 /**
891 * Get YouTube domain.
892 *
893 * @since 2.3.0
894 * @return string YouTube embed domain.
895 */
896 function ayg_get_youtube_domain() {
897 $player_settings = get_option( 'ayg_player_settings' );
898
899 $domain = 'https://www.youtube.com';
900 if ( isset( $player_settings['privacy_enhanced_mode'] ) && ! empty( $player_settings['privacy_enhanced_mode'] ) ) {
901 $domain = 'https://www.youtube-nocookie.com';
902 }
903
904 return $domain;
905 }
906
907 /**
908 * Inserts a new associative array after another associative array key.
909 *
910 * @since 2.1.0
911 * @param string $key The associative array key to insert after.
912 * @param array $array An array to insert in to.
913 * @param array $new_array An array to insert.
914 * @return array Updated array.
915 */
916 function ayg_insert_array_after( $key, $array, $new_array ) {
917 if ( array_key_exists( $key, $array ) ) {
918 $new = array();
919
920 foreach ( $array as $k => $value ) {
921 $new[ $k ] = $value;
922
923 if ( $k === $key ) {
924 foreach ( $new_array as $new_key => $new_value ) {
925 $new[ $new_key ] = $new_value;
926 }
927 }
928 }
929
930 return $new;
931 }
932
933 return $array;
934 }
935
936 /**
937 * Sanitize the integer inputs, accepts empty values.
938 *
939 * @since 1.0.0
940 * @param string|int $value Input value.
941 * @return string|int Sanitized value.
942 */
943 function ayg_sanitize_int( $value ) {
944 $value = intval( $value );
945 return ( 0 == $value ) ? '' : $value;
946 }
947
948 /**
949 * Trims text to a certain number of characters.
950 *
951 * @since 2.0.0
952 * @param string $text Text to trim.
953 * @param int $num_characters Number of characters.
954 * @param string $append String to append to the end of the excerpt.
955 * @return string Trimmed text.
956 */
957 function ayg_trim_words( $text, $num_characters, $append = '...' ) {
958 $num_characters++;
959
960 $original_text = $text;
961 $text = ( $num_characters > 1 ) ? wp_strip_all_tags( $original_text, true ) : nl2br( $original_text );
962
963 if ( $num_characters > 1 && mb_strlen( $text ) > $num_characters ) {
964 $subex = mb_substr( $text, 0, $num_characters - 5 );
965 $exwords = explode( ' ', $subex );
966 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
967
968 if ( $excut < 0 ) {
969 $text = mb_substr( $subex, 0, $excut );
970 } else {
971 $text = $subex;
972 }
973
974 $text .= $append;
975 }
976
977 return apply_filters( 'ayg_trim_words', $text, $original_text, $num_characters, $append );
978 }
979
980 /**
981 * Gallery HTML output.
982 *
983 * @since 1.0.0
984 * @param array $video YouTube video object.
985 * @param array $attributes Array of user attributes.
986 */
987 function the_ayg_gallery_thumbnail( $video, $attributes ) {
988 include ayg_get_template( AYG_DIR . 'public/templates/thumbnail.php' );
989 }
990
991 /**
992 * Pagination HTML output.
993 *
994 * @since 1.0.0
995 * @param array $attributes Array of user attributes.
996 */
997 function the_ayg_pagination( $attributes ) {
998 if ( ! empty( $attributes['pagination'] ) ) {
999 include ayg_get_template( AYG_DIR . 'public/templates/pagination.php' );
1000 }
1001 }