PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.1.0
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.1.0
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / includes / functions.php

functions.php in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 2.1.0, at includes/functions.php

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