PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.3.8
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.3.8
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
← All changes | includes/youtube-api.php +216 -87 2.0.02.3.8 View file →
@@ -44,15 +44,15 @@
44 44 'videos.list' => 'https://www.googleapis.com/youtube/v3/videos'
45 45 );
46 46
47 47 /**
48 - * Is API cache enabled?
48 + * Is development mode enabled?
49 49 *
50 - * @since 2.0.0
50 + * @since 2.3.0
51 51 * @access protected
52 52 * @var bool
53 53 */
54 - protected $cache_api_results = true;
54 + protected $is_development_mode = false;
55 55
56 56 /**
57 57 * Get videos.
58 58 *
@@ -69,11 +69,11 @@
69 69 }
70 70
71 71 $this->api_key = $general_settings['api_key'];
72 72
73 - // Is API cache enabled?
73 + // Is development mode enabled?
74 74 if ( isset( $general_settings['development_mode'] ) && ! empty( $general_settings['development_mode'] ) ) {
75 - $this->cache_api_results = false;
75 + $this->is_development_mode = true;
76 76 }
77 77
78 78 // Process output
79 79 $response = array();
@@ -97,16 +97,22 @@
97 97 if ( empty( $params['id'] ) ) {
98 98 return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) );
99 99 }
100 100
101 - // Find playlistId from the channel
102 - $response = $this->request_api_channels( $params );
101 + // Get playlist id from the channel
102 + $playlist_id = $this->get_playlist_id( $params );
103 103
104 - // Get videos using the playlistId
105 - if ( ! isset( $response->error ) ) {
106 - $params['src'] = $response->playlistId;
107 - $response = $this->request_api_playlist_items( $params );
104 + if ( is_object( $playlist_id ) && isset( $playlist_id->error ) ) {
105 + return $playlist_id;
108 106 }
107 +
108 + if ( empty( $playlist_id ) ) {
109 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
110 + }
111 +
112 + // Get videos using the playlist id
113 + $params['src'] = $playlist_id;
114 + $response = $this->request_api_playlist_items( $params );
109 115 break;
110 116
111 117 case 'username':
112 118 if ( empty( $params['src'] ) ) {
@@ -112,22 +118,28 @@
112 118 if ( empty( $params['src'] ) ) {
113 119 return $this->get_error( __( 'YouTube Account Username is required.', 'automatic-youtube-gallery' ) );
114 120 }
115 121
116 - // Find playlistId from the channel
122 + // Get playlist id from the channel
117 123 $params['forUsername'] = $this->parse_youtube_id_from_url( $params['src'], 'username' );
118 - $response = $this->request_api_channels( $params );
124 + $playlist_id = $this->get_playlist_id( $params );
119 125
120 - // Get videos using the playlistId
121 - if ( ! isset( $response->error ) ) {
122 - $params['src'] = $response->playlistId;
123 - $response = $this->request_api_playlist_items( $params );
126 + if ( is_object( $playlist_id ) && isset( $playlist_id->error ) ) {
127 + return $playlist_id;
124 128 }
129 +
130 + if ( empty( $playlist_id ) ) {
131 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
132 + }
133 +
134 + // Get videos using the playlist id
135 + $params['src'] = $playlist_id;
136 + $response = $this->request_api_playlist_items( $params );
125 137 break;
126 138
127 139 case 'search':
128 140 if ( empty( $params['src'] ) ) {
129 - return $this->get_error( __( 'Cannot search an empty string. A search term is required.', 'automatic-youtube-gallery' ) );
141 + return $this->get_error( __( 'Cannot search an empty string. A search keyword is required.', 'automatic-youtube-gallery' ) );
130 142 }
131 143
132 144 $response = $this->request_api_search( $params );
133 145 break;
@@ -144,14 +156,16 @@
144 156 if ( empty( $params['src'] ) ) {
145 157 return $this->get_error( __( 'YouTube Channel ID (or) or a YouTube Video URL from the Channel is required.', 'automatic-youtube-gallery' ) );
146 158 }
147 159
148 - $response = new stdClass();
149 - $response->id = $this->get_channel_id( $params );
160 + $params['channelId'] = $this->get_channel_id( $params );
150 161
151 - if ( empty( $response->id ) ) {
162 + if ( empty( $params['channelId'] ) ) {
152 163 return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) );
153 164 }
165 +
166 + // Get live video using the channel id
167 + $response = $this->request_api_live_video( $params );
154 168 break;
155 169
156 170 default: // video
157 171 if ( empty( $params['src'] ) ) {
@@ -242,16 +256,55 @@
242 256 * @param array $params Array of query params.
243 257 * @return string
244 258 */
245 259 private function get_channel_id( $params = array() ) {
260 + // Parse channel ID from URL: https://www.youtube.com/channel/XXXXXXXXXX
246 261 $id = $this->parse_youtube_id_from_url( $params['src'], 'channel' );
247 262
248 263 if ( empty( $id ) ) {
249 - $response = $this->request_api_video( $params );
264 + // Get channel ID from a Video URL: https://www.youtube.com/watch?v=XXXXXXXXXX
265 + $video_id = $this->parse_youtube_id_from_url( $params['src'], 'video' );
250 266
251 - if ( isset( $response->videos ) ) {
252 - $id = $response->videos[0]->channel_id;
267 + // Request from cache
268 + $channel_ids = get_option( 'ayg_channel_ids', array() );
269 + if ( ! is_array( $channel_ids ) ) {
270 + $channel_ids = (array) $channel_ids;
253 271 }
272 +
273 + if ( isset( $channel_ids[ $video_id ] ) && ! empty( $channel_ids[ $video_id ] ) ) {
274 + return $channel_ids[ $video_id ];
275 + }
276 +
277 + // Request from API
278 + $api_url = $this->get_api_url( 'videos.list' );
279 +
280 + $params['id'] = $video_id;
281 +
282 + $api_params = $this->safe_merge_params(
283 + array(
284 + 'id' => '',
285 + 'part' => 'id,snippet,contentDetails,status',
286 + 'cache' => 0
287 + ),
288 + $params
289 + );
290 +
291 + $api_response = $this->request_api( $api_url, $api_params, 'channel_id' );
292 + if ( isset( $api_response->error ) ) {
293 + return $id;
294 + }
295 +
296 + $videos = $this->parse_videos( $api_response );
297 + if ( isset( $videos->error ) ) {
298 + return $id;
299 + }
300 +
301 + // Process output
302 + if ( $id = $videos[0]->channel_id ) {
303 + // Store in cache
304 + $channel_ids[ $video_id ] = $id;
305 + update_option( 'ayg_channel_ids', $channel_ids );
306 + }
254 307 }
255 308
256 309 return $id;
257 310 }
@@ -256,8 +309,78 @@
256 309 return $id;
257 310 }
258 311
259 312 /**
313 + * Get playlist id using channels API.
314 + *
315 + * @since 1.0.0
316 + * @access private
317 + * @param array $params Array of query params.
318 + * @return mixed
319 + */
320 + private function get_playlist_id( $params = array() ) {
321 + // Request from cache
322 + $playlist_ids = get_option( 'ayg_playlist_ids', array() );
323 + if ( ! is_array( $playlist_ids ) ) {
324 + $playlist_ids = (array) $playlist_ids;
325 + }
326 +
327 + $key = '';
328 +
329 + if ( isset( $params['forUsername'] ) && ! empty( $params['forUsername'] ) ) {
330 + $key = $params['forUsername'];
331 + }
332 +
333 + if ( isset( $params['id'] ) && ! empty( $params['id'] ) ) {
334 + unset( $params['forUsername'] );
335 + $key = $params['id'];
336 + }
337 +
338 + if ( isset( $playlist_ids[ $key ] ) && ! empty( $playlist_ids[ $key ] ) ) {
339 + return $playlist_ids[ $key ];
340 + }
341 +
342 + // Request from API
343 + $api_url = $this->get_api_url( 'channels.list' );
344 +
345 + $api_params = $this->safe_merge_params(
346 + array(
347 + 'id' => '',
348 + 'forUsername' => '',
349 + 'part' => 'contentDetails',
350 + 'cache' => 0
351 + ),
352 + $params
353 + );
354 +
355 + $api_response = $this->request_api( $api_url, $api_params, 'playlist_id' );
356 + if ( isset( $api_response->error ) ) {
357 + return $api_response;
358 + }
359 +
360 + if ( ! isset( $api_response->items ) ) {
361 + return false;
362 + }
363 +
364 + $items = $api_response->items;
365 + if ( ! is_array( $items ) || count( $items ) == 0 ) {
366 + return false;
367 + }
368 +
369 + // Process output
370 + if ( $id = $items[0]->contentDetails->relatedPlaylists->uploads ) {
371 + // Store in cache
372 + $playlist_ids[ $key ] = $id;
373 + update_option( 'ayg_playlist_ids', $playlist_ids );
374 +
375 + // Return
376 + return $id;
377 + }
378 +
379 + return false;
380 + }
381 +
382 + /**
260 383 * Get videos using playlistItems API.
261 384 *
262 385 * @since 1.0.0
263 386 * @access private
@@ -295,53 +418,11 @@
295 418 $response->page_info = $this->parse_page_info( $api_response );
296 419 $response->videos = $videos;
297 420
298 421 return $response;
299 - }
422 + }
300 423
301 424 /**
302 - * Find playlistId using channels API.
303 - *
304 - * @since 1.0.0
305 - * @access private
306 - * @param array $params Array of query params.
307 - * @return mixed
308 - */
309 - private function request_api_channels( $params = array() ) {
310 - $api_url = $this->get_api_url( 'channels.list' );
311 -
312 - $api_params = $this->safe_merge_params(
313 - array(
314 - 'id' => '',
315 - 'forUsername' => '',
316 - 'part' => 'contentDetails',
317 - 'cache' => 0
318 - ),
319 - $params
320 - );
321 -
322 - $api_response = $this->request_api( $api_url, $api_params );
323 - if ( isset( $api_response->error ) ) {
324 - return $api_response;
325 - }
326 -
327 - if ( ! isset( $api_response->items ) ) {
328 - return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
329 - }
330 -
331 - $items = $api_response->items;
332 - if ( ! is_array( $items ) || count( $items ) == 0 ) {
333 - return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
334 - }
335 -
336 - // Process output
337 - $response = new stdClass();
338 - $response->playlistId = $items[0]->contentDetails->relatedPlaylists->uploads;
339 -
340 - return $response;
341 - }
342 -
343 - /**
344 425 * Get videos using search API.
345 426 *
346 427 * @since 1.0.0
347 428 * @access private
@@ -392,8 +473,50 @@
392 473 return $response;
393 474 }
394 475
395 476 /**
477 + * Get live video using search API.
478 + *
479 + * @since 2.3.7
480 + * @access private
481 + * @param array $params Array of query params.
482 + * @return mixed
483 + */
484 + private function request_api_live_video( $params = array() ) {
485 + $api_url = $this->get_api_url( 'search.list' );
486 +
487 + $params['type'] = 'video'; // Overrides user defined type value 'livestream'
488 +
489 + $api_params = $this->safe_merge_params(
490 + array(
491 + 'type' => 'video',
492 + 'eventType' => 'live',
493 + 'part' => 'snippet',
494 + 'channelId' => '',
495 + 'cache' => 0
496 + ),
497 + $params
498 + );
499 +
500 + $api_response = $this->request_api( $api_url, $api_params );
501 + if ( isset( $api_response->error ) ) {
502 + return $api_response;
503 + }
504 +
505 + $videos = $this->parse_videos( $api_response );
506 + if ( isset( $videos->error ) ) {
507 + $livestream_settings = get_option( 'ayg_livestream_settings' );
508 + return $this->get_error( '<div class="ayg-livestream-fallback-message">' . $livestream_settings['fallback_message'] . '</div>' );
509 + }
510 +
511 + // Process output
512 + $response = new stdClass();
513 + $response->videos = $videos;
514 +
515 + return $response;
516 + }
517 +
518 + /**
396 519 * Get details of the given video ID.
397 520 *
398 521 * @since 1.0.0
399 522 * @access private
@@ -520,17 +643,18 @@
520 643 * Request data from the API server.
521 644 *
522 645 * @since 1.0.0
523 646 * @access private
524 - * @param string $url YouTube API URL.
525 - * @param array $params Array of query params.
647 + * @param string $url YouTube API URL.
648 + * @param array $params Array of query params.
649 + * @param string $context "channel_id", "playlist_id", or "videos"
526 650 * @return mixed
527 651 */
528 - private function request_api( $url, $params ) {
652 + private function request_api( $url, $params, $context = 'videos' ) {
529 653 $params['key'] = $this->api_key;
530 654
531 - // Request data from cache
532 - if ( $this->cache_api_results ) {
655 + // Request from cache
656 + if ( ! $this->is_development_mode ) {
533 657 $cache_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
534 658 $cache_key = 'ayg_' . md5( $cache_url );
535 659 $cache_data = get_transient( $cache_key );
536 660
@@ -538,9 +662,9 @@
538 662 return $cache_data;
539 663 }
540 664 }
541 665
542 - // Request data from API server
666 + // Request from API
543 667 $cache_duration = 0;
544 668 if ( isset( $params['cache'] ) ) {
545 669 $cache_duration = (int) $params['cache'];
546 670 unset( $params['cache'] );
@@ -578,27 +702,32 @@
578 702
579 703 return $this->get_error( $message );
580 704 }
581 705
582 - // Store data in cache (transients)
583 - if ( $this->cache_api_results && $cache_duration > 0 ) {
584 - set_transient( $cache_key, $data, $cache_duration );
706 + // Store in cache (transients)
707 + if ( ! $this->is_development_mode && 'videos' == $context && $cache_duration > 0 ) {
708 + if ( isset( $data->items ) && is_array( $data->items ) && count( $data->items ) > 0 ) {
709 + set_transient( $cache_key, $data, $cache_duration );
585 710
586 - // Get the current list of transients
587 - $cache_keys = get_option( 'ayg_transient_keys', array() );
588 - if ( ! is_array( $cache_keys ) ) {
589 - $cache_keys = (array) $cache_keys;
590 - }
711 + // Get the current list of transients
712 + $cache_keys = get_option( 'ayg_transient_keys', array() );
713 + if ( ! is_array( $cache_keys ) ) {
714 + $cache_keys = (array) $cache_keys;
715 + }
591 716
592 - // Append our new one
593 - if ( ! in_array( $cache_key, $cache_keys ) ) {
594 - $cache_keys[] = $cache_key;
717 + // Append our new one
718 + if ( ! in_array( $cache_key, $cache_keys ) ) {
719 + $cache_keys[] = $cache_key;
720 + }
721 +
722 + // Save it to the DB
723 + update_option( 'ayg_transient_keys', $cache_keys );
595 724 }
596 -
597 - // Save it to the DB
598 - update_option( 'ayg_transient_keys', $cache_keys );
599 725 }
600 726
727 + // Store videos in our custom database table "{$wpdb->prefix}ayg_videos"
728 + ayg_db_store_videos( $data );
729 +
601 730 // Finally return the data
602 731 return $data;
603 732 }
604 733