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 +371 -138 1.6.22.3.8 View file →
@@ -44,8 +44,17 @@
44 44 'videos.list' => 'https://www.googleapis.com/youtube/v3/videos'
45 45 );
46 46
47 47 /**
48 + * Is development mode enabled?
49 + *
50 + * @since 2.3.0
51 + * @access protected
52 + * @var bool
53 + */
54 + protected $is_development_mode = false;
55 +
56 + /**
48 57 * Get videos.
49 58 *
50 59 * @since 1.0.0
51 60 * @param array $params Array of query params.
@@ -50,24 +59,29 @@
50 59 * @since 1.0.0
51 60 * @param array $params Array of query params.
52 61 * @return mixed
53 62 */
54 - public function get_videos( $params = array() ) {
63 + public function query( $params = array() ) {
55 64 // Get YouTube API Key
56 65 $general_settings = get_option( 'ayg_general_settings' );
57 66
58 67 if ( empty( $general_settings['api_key'] ) ) {
59 - return $this->get_error( __( 'YouTube API Key Missing.', 'automatic-youtube-gallery' ) . ' ' . sprintf( __( 'Follow <a href="%s" target="_blank">this guide</a> to get your own API key.', 'automatic-youtube-gallery' ), 'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/' ) );
68 + return $this->get_error( __( 'YouTube API key not found.', 'automatic-youtube-gallery' ) . ' ' . sprintf( __( 'Kindly follow this URL <a href="%s" target="_blank">this guide</a> to get your own API key.', 'automatic-youtube-gallery' ), 'https://plugins360.com/automatic-youtube-gallery/how-to-get-youtube-api-key/' ) );
60 69 }
61 70
62 71 $this->api_key = $general_settings['api_key'];
63 72
64 - // Process response
73 + // Is development mode enabled?
74 + if ( isset( $general_settings['development_mode'] ) && ! empty( $general_settings['development_mode'] ) ) {
75 + $this->is_development_mode = true;
76 + }
77 +
78 + // Process output
65 79 $response = array();
66 80
67 81 switch ( $params['type'] ) {
68 82 case 'playlist':
69 - if ( empty( $params['id'] ) ) {
83 + if ( empty( $params['src'] ) ) {
70 84 return $this->get_error( __( 'YouTube Playlist ID (or) URL is required.', 'automatic-youtube-gallery' ) );
71 85 }
72 86
73 87 $response = $this->request_api_playlist_items( $params );
@@ -73,48 +87,59 @@
73 87 $response = $this->request_api_playlist_items( $params );
74 88 break;
75 89
76 90 case 'channel':
91 + if ( empty( $params['src'] ) ) {
92 + return $this->get_error( __( 'YouTube Channel ID (or) or a YouTube Video URL from the Channel is required.', 'automatic-youtube-gallery' ) );
93 + }
94 +
95 + $params['id'] = $this->get_channel_id( $params );
96 +
77 97 if ( empty( $params['id'] ) ) {
78 - return $this->get_error( __( 'YouTube Channel ID (or) URL is required.', 'automatic-youtube-gallery' ) );
98 + return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) );
79 99 }
80 100
81 - // Find playlistId from the channel
82 - $params['id'] = $this->parse_youtube_id_from_url( $params['id'], 'channel' );
83 - $response = $this->request_api_channels( $params );
101 + // Get playlist id from the channel
102 + $playlist_id = $this->get_playlist_id( $params );
84 103
85 - // Get videos using the playlistId
86 - if ( ! isset( $response->error ) ) {
104 + if ( is_object( $playlist_id ) && isset( $playlist_id->error ) ) {
105 + return $playlist_id;
106 + }
87 107
88 - $params['id'] = $response->playlistId;
89 - $response = $this->request_api_playlist_items( $params );
108 + if ( empty( $playlist_id ) ) {
109 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
110 + }
90 111
91 - }
112 + // Get videos using the playlist id
113 + $params['src'] = $playlist_id;
114 + $response = $this->request_api_playlist_items( $params );
92 115 break;
93 116
94 117 case 'username':
95 - if ( empty( $params['id'] ) ) {
118 + if ( empty( $params['src'] ) ) {
96 119 return $this->get_error( __( 'YouTube Account Username is required.', 'automatic-youtube-gallery' ) );
97 120 }
98 121
99 - // Find playlistId from the channel
100 - $params['forUsername'] = $this->parse_youtube_id_from_url( $params['id'], 'username' );
101 - unset( $params['id'] );
122 + // Get playlist id from the channel
123 + $params['forUsername'] = $this->parse_youtube_id_from_url( $params['src'], 'username' );
124 + $playlist_id = $this->get_playlist_id( $params );
102 125
103 - $response = $this->request_api_channels( $params );
126 + if ( is_object( $playlist_id ) && isset( $playlist_id->error ) ) {
127 + return $playlist_id;
128 + }
104 129
105 - // Get videos using the playlistId
106 - if ( ! isset( $response->error ) ) {
130 + if ( empty( $playlist_id ) ) {
131 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
132 + }
107 133
108 - $params['id'] = $response->playlistId;
109 - $response = $this->request_api_playlist_items( $params );
110 -
111 - }
134 + // Get videos using the playlist id
135 + $params['src'] = $playlist_id;
136 + $response = $this->request_api_playlist_items( $params );
112 137 break;
113 138
114 139 case 'search':
115 - if ( empty( $params['id'] ) ) {
116 - return $this->get_error( __( 'Cannot search an empty string. A search term is required.', 'automatic-youtube-gallery' ) );
140 + if ( empty( $params['src'] ) ) {
141 + return $this->get_error( __( 'Cannot search an empty string. A search keyword is required.', 'automatic-youtube-gallery' ) );
117 142 }
118 143
119 144 $response = $this->request_api_search( $params );
120 145 break;
@@ -119,9 +144,9 @@
119 144 $response = $this->request_api_search( $params );
120 145 break;
121 146
122 147 case 'videos':
123 - if ( empty( $params['id'] ) ) {
148 + if ( empty( $params['src'] ) ) {
124 149 return $this->get_error( __( 'Atleast one YouTube Video ID (or) URL is required.', 'automatic-youtube-gallery' ) );
125 150 }
126 151
127 152 $response = $this->request_api_videos( $params );
@@ -126,10 +151,25 @@
126 151
127 152 $response = $this->request_api_videos( $params );
128 153 break;
129 154
155 + case 'livestream':
156 + if ( empty( $params['src'] ) ) {
157 + return $this->get_error( __( 'YouTube Channel ID (or) or a YouTube Video URL from the Channel is required.', 'automatic-youtube-gallery' ) );
158 + }
159 +
160 + $params['channelId'] = $this->get_channel_id( $params );
161 +
162 + if ( empty( $params['channelId'] ) ) {
163 + return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) );
164 + }
165 +
166 + // Get live video using the channel id
167 + $response = $this->request_api_live_video( $params );
168 + break;
169 +
130 170 default: // video
131 - if ( empty( $params['id'] ) ) {
171 + if ( empty( $params['src'] ) ) {
132 172 return $this->get_error( __( 'YouTube Video ID (or) URL is required.', 'automatic-youtube-gallery' ) );
133 173 }
134 174
135 175 $response = $this->request_api_video( $params );
@@ -148,9 +188,10 @@
148 188 * @param string $type Type of the URL (playlist|channel|video).
149 189 * @return mixed
150 190 */
151 191 private function parse_youtube_id_from_url( $url, $type = 'video' ) {
152 - $id = $url;
192 + $url = trim( $url );
193 + $id = $url;
153 194
154 195 switch ( $type ) {
155 196 case 'playlist':
156 197 if ( preg_match( '/list=(.*)&?\/?/', $url, $matches ) ) {
@@ -158,8 +199,12 @@
158 199 }
159 200 break;
160 201
161 202 case 'channel':
203 + if ( wp_http_validate_url( $id ) ) {
204 + $id = '';
205 + }
206 +
162 207 $url = parse_url( rtrim( $url, '/' ) );
163 208
164 209 if ( isset( $url['path'] ) && preg_match( '/^\/channel\/(([^\/])+?)$/', $url['path'], $matches ) ) {
165 210 $id = $matches[1];
@@ -203,51 +248,70 @@
203 248 return $id;
204 249 }
205 250
206 251 /**
207 - * Get videos using playlistItems API.
252 + * Get the channel ID.
208 253 *
209 - * @since 1.0.0
254 + * @since 2.0.0
210 255 * @access private
211 - * @param array $params Array of query params.
212 - * @return stdClass
256 + * @param array $params Array of query params.
257 + * @return string
213 258 */
214 - private function request_api_playlist_items( $params = array() ) {
215 - $api_url = $this->get_api_url( 'playlistItems.list' );
216 -
217 - $params['playlistId'] = $this->parse_youtube_id_from_url( $params['id'], 'playlist' );
259 + private function get_channel_id( $params = array() ) {
260 + // Parse channel ID from URL: https://www.youtube.com/channel/XXXXXXXXXX
261 + $id = $this->parse_youtube_id_from_url( $params['src'], 'channel' );
218 262
219 - $api_params = $this->safe_merge_params(
220 - array(
221 - 'playlistId' => '',
222 - 'part' => 'id,snippet,contentDetails,status',
223 - 'maxResults' => 50,
224 - 'pageToken' => '',
225 - 'cache' => 0
226 - ),
227 - $params
228 - );
229 -
230 - $api_response = $this->request_api( $api_url, $api_params );
231 - if ( isset( $api_response->error ) ) {
232 - return $api_response;
233 - }
263 + if ( empty( $id ) ) {
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' );
234 266
235 - $videos = $this->parse_videos( $api_response );
236 - if ( isset( $videos->error ) ) {
237 - return $videos;
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;
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 + }
238 307 }
239 308
240 - // Process result
241 - $response = new stdClass();
242 - $response->page_info = $this->parse_page_info( $api_response );
243 - $response->videos = $videos;
244 -
245 - return $response;
309 + return $id;
246 310 }
247 311
248 312 /**
249 - * Find playlistId using channels API.
313 + * Get playlist id using channels API.
250 314 *
251 315 * @since 1.0.0
252 316 * @access private
253 317 * @param array $params Array of query params.
@@ -252,9 +316,31 @@
252 316 * @access private
253 317 * @param array $params Array of query params.
254 318 * @return mixed
255 319 */
256 - private function request_api_channels( $params = array() ) {
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
257 343 $api_url = $this->get_api_url( 'channels.list' );
258 344
259 345 $api_params = $this->safe_merge_params(
260 346 array(
@@ -265,24 +351,76 @@
265 351 ),
266 352 $params
267 353 );
268 354
269 - $api_response = $this->request_api( $api_url, $api_params );
355 + $api_response = $this->request_api( $api_url, $api_params, 'playlist_id' );
270 356 if ( isset( $api_response->error ) ) {
271 357 return $api_response;
272 358 }
273 359
360 + if ( ! isset( $api_response->items ) ) {
361 + return false;
362 + }
363 +
274 364 $items = $api_response->items;
275 365 if ( ! is_array( $items ) || count( $items ) == 0 ) {
276 - return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
366 + return false;
277 367 }
278 368
279 - // Process result
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 + /**
383 + * Get videos using playlistItems API.
384 + *
385 + * @since 1.0.0
386 + * @access private
387 + * @param array $params Array of query params.
388 + * @return stdClass
389 + */
390 + private function request_api_playlist_items( $params = array() ) {
391 + $api_url = $this->get_api_url( 'playlistItems.list' );
392 +
393 + $params['playlistId'] = $this->parse_youtube_id_from_url( $params['src'], 'playlist' );
394 +
395 + $api_params = $this->safe_merge_params(
396 + array(
397 + 'playlistId' => '',
398 + 'part' => 'id,snippet,contentDetails,status',
399 + 'maxResults' => 50,
400 + 'pageToken' => '',
401 + 'cache' => 0
402 + ),
403 + $params
404 + );
405 +
406 + $api_response = $this->request_api( $api_url, $api_params );
407 + if ( isset( $api_response->error ) ) {
408 + return $api_response;
409 + }
410 +
411 + $videos = $this->parse_videos( $api_response );
412 + if ( isset( $videos->error ) ) {
413 + return $videos;
414 + }
415 +
416 + // Process output
280 417 $response = new stdClass();
281 - $response->playlistId = $items[0]->contentDetails->relatedPlaylists->uploads;
418 + $response->page_info = $this->parse_page_info( $api_response );
419 + $response->videos = $videos;
282 420
283 - return $response;
284 - }
421 + return $response;
422 + }
285 423
286 424 /**
287 425 * Get videos using search API.
288 426 *
@@ -293,9 +431,9 @@
293 431 */
294 432 private function request_api_search( $params = array() ) {
295 433 $api_url = $this->get_api_url( 'search.list' );
296 434
297 - $params['q'] = $params['id'];
435 + $params['q'] = $params['src'];
298 436
299 437 if ( ! empty( $params['q'] ) ) {
300 438 $params['q'] = str_replace( '|', '%7C', $params['q'] );
301 439 }
@@ -326,17 +464,98 @@
326 464 if ( isset( $videos->error ) ) {
327 465 return $videos;
328 466 }
329 467
330 - // Process result
468 + // Process output
331 469 $response = new stdClass();
332 470 $response->page_info = $this->parse_page_info( $api_response );
333 471 $response->videos = $videos;
334 472
335 473 return $response;
474 + }
475 +
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;
336 516 }
337 517
338 518 /**
519 + * Get details of the given video ID.
520 + *
521 + * @since 1.0.0
522 + * @access private
523 + * @param array $params Array of query params.
524 + * @return stdClass
525 + */
526 + private function request_api_video( $params = array() ) {
527 + $api_url = $this->get_api_url( 'videos.list' );
528 +
529 + $params['id'] = $this->parse_youtube_id_from_url( $params['src'], 'video' );
530 +
531 + $api_params = $this->safe_merge_params(
532 + array(
533 + 'id' => '',
534 + 'part' => 'id,snippet,contentDetails,status',
535 + 'cache' => 0
536 + ),
537 + $params
538 + );
539 +
540 + $api_response = $this->request_api( $api_url, $api_params );
541 + if ( isset( $api_response->error ) ) {
542 + return $api_response;
543 + }
544 +
545 + $videos = $this->parse_videos( $api_response );
546 + if ( isset( $videos->error ) ) {
547 + return $videos;
548 + }
549 +
550 + // Process output
551 + $response = new stdClass();
552 + $response->videos = $videos;
553 +
554 + return $response;
555 + }
556 +
557 + /**
339 558 * Get details of the given video IDs.
340 559 *
341 560 * @since 1.0.0
342 561 * @access private
@@ -345,10 +564,10 @@
345 564 */
346 565 private function request_api_videos( $params = array() ) {
347 566 $api_url = $this->get_api_url( 'videos.list' );
348 567
349 - $urls = str_replace( "\n\r", ',', $params['id'] );
350 - $urls = str_replace( ' ', ',', $params['id'] );
568 + $urls = str_replace( "\n\r", ',', $params['src'] );
569 + $urls = str_replace( ' ', ',', $urls );
351 570 $urls = explode( ',', $urls );
352 571 $urls = array_filter( $urls );
353 572
354 573 $all_ids = array();
@@ -372,9 +591,9 @@
372 591
373 592 $api_params = $this->safe_merge_params(
374 593 array(
375 594 'id' => '',
376 - 'part' => 'id,snippet,contentDetails,statistics,status',
595 + 'part' => 'id,snippet,contentDetails,status',
377 596 'cache' => 0
378 597 ),
379 598 $params
380 599 );
@@ -384,10 +603,13 @@
384 603 return $api_response;
385 604 }
386 605
387 606 $videos = $this->parse_videos( $api_response );
607 + if ( isset( $videos->error ) ) {
608 + return $videos;
609 + }
388 610
389 - // Process result
611 + // Process output
390 612 $response = new stdClass();
391 613 $response->videos = $videos;
392 614
393 615 $response->page_info = array(
@@ -403,44 +625,8 @@
403 625 }
404 626
405 627 return $response;
406 628 }
407 -
408 - /**
409 - * Get details of the given video ID.
410 - *
411 - * @since 1.0.0
412 - * @access private
413 - * @param array $params Array of query params.
414 - * @return stdClass
415 - */
416 - private function request_api_video( $params = array() ) {
417 - $api_url = $this->get_api_url( 'videos.list' );
418 -
419 - $params['id'] = $this->parse_youtube_id_from_url( $params['id'], 'video' );
420 -
421 - $api_params = $this->safe_merge_params(
422 - array(
423 - 'id' => '',
424 - 'part' => 'id,snippet,contentDetails,statistics,status',
425 - 'cache' => 0
426 - ),
427 - $params
428 - );
429 -
430 - $api_response = $this->request_api( $api_url, $api_params );
431 - if ( isset( $api_response->error ) ) {
432 - return $api_response;
433 - }
434 -
435 - $videos = $this->parse_videos( $api_response );
436 -
437 - // Process result
438 - $response = new stdClass();
439 - $response->videos = $videos;
440 -
441 - return $response;
442 - }
443 629
444 630 /**
445 631 * Get API URL by request.
446 632 *
@@ -457,33 +643,44 @@
457 643 * Request data from the API server.
458 644 *
459 645 * @since 1.0.0
460 646 * @access private
461 - * @param string $url YouTube API URL.
462 - * @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"
463 650 * @return mixed
464 651 */
465 - private function request_api( $url, $params ) {
652 + private function request_api( $url, $params, $context = 'videos' ) {
466 653 $params['key'] = $this->api_key;
467 654
468 - // Request data from transients
469 - $transient_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
470 - $transient_key = 'ayg_' . md5( $transient_url );
471 - $transient_data = get_transient( $transient_key );
655 + // Request from cache
656 + if ( ! $this->is_development_mode ) {
657 + $cache_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
658 + $cache_key = 'ayg_' . md5( $cache_url );
659 + $cache_data = get_transient( $cache_key );
472 660
473 - if ( ! empty( $transient_data ) ) {
474 - return $transient_data;
661 + if ( ! empty( $cache_data ) ) {
662 + return $cache_data;
663 + }
475 664 }
476 665
477 - $transient_expiration = 0;
478 -
666 + // Request from API
667 + $cache_duration = 0;
479 668 if ( isset( $params['cache'] ) ) {
480 - $transient_expiration = (int) $params['cache'];
669 + $cache_duration = (int) $params['cache'];
481 670 unset( $params['cache'] );
482 671 }
483 672
484 - // Request data from API server
673 + $q = '';
674 + if ( isset( $params['q'] ) ) {
675 + $q = $params['q'];
676 + unset( $params['q'] );
677 + }
678 +
485 679 $url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
680 + if ( ! empty( $q ) ) {
681 + $url .= '&q=' . $q;
682 + }
486 683
487 684 $request = wp_remote_get( $url, array(
488 685 'headers' => [ 'referer' => home_url() ],
489 686 ) );
@@ -505,24 +702,32 @@
505 702
506 703 return $this->get_error( $message );
507 704 }
508 705
509 - // Store data in transients
510 - if ( $transient_expiration > 0 ) {
511 - set_transient( $transient_key, $data, $transient_expiration );
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 );
512 710
513 - // Get the current list of transients
514 - $transient_keys = get_option( 'ayg_transient_keys', array() );
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 + }
515 716
516 - // Append our new one
517 - if ( ! in_array( $transient_key, $transient_keys ) ) {
518 - $transient_keys[] = $transient_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 );
519 724 }
520 -
521 - // Save it to the DB
522 - update_option( 'ayg_transient_keys', $transient_keys );
523 725 }
524 726
727 + // Store videos in our custom database table "{$wpdb->prefix}ayg_videos"
728 + ayg_db_store_videos( $data );
729 +
525 730 // Finally return the data
526 731 return $data;
527 732 }
528 733
@@ -534,10 +739,13 @@
534 739 * @param object $data YouTube API response object.
535 740 * @return mixed
536 741 */
537 742 private function parse_videos( $data ) {
538 - $items = $data->items;
743 + if ( ! isset( $data->items ) ) {
744 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
745 + }
539 746
747 + $items = $data->items;
540 748 if ( ! is_array( $items ) || 0 == count( $items ) ) {
541 749 return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
542 750 }
543 751
@@ -556,10 +764,17 @@
556 764 } elseif ( isset( $item->id ) && isset( $item->id->videoId ) ) {
557 765 $video->id = $item->id->videoId;
558 766 } elseif ( isset( $item->id ) ) {
559 767 $video->id = $item->id;
560 - }
768 + }
561 769
770 + // Video channel ID
771 + $video->channel_id = '';
772 +
773 + if ( isset( $item->snippet->channelId ) ) {
774 + $video->channel_id = $item->snippet->channelId;
775 + }
776 +
562 777 // Video title
563 778 $video->title = $item->snippet->title;
564 779
565 780 // Video description
@@ -573,11 +788,29 @@
573 788 // Video publish date
574 789 $video->published_at = $item->snippet->publishedAt;
575 790
576 791 // Push resulting object to the main array
577 - if ( ( isset( $item->status ) && 'public' == $item->status->privacyStatus ) || ( isset( $item->snippet->status ) && 'public' == $item->snippet->status->privacyStatus ) || 'youtube#searchResult' == $item->kind ) {
792 + $status = 'private';
793 +
794 + if ( isset( $item->status ) && ( 'public' == $item->status->privacyStatus || 'unlisted' == $item->status->privacyStatus ) ) {
795 + $status = 'public';
796 + }
797 +
798 + if ( isset( $item->snippet->status ) && ( 'public' == $item->snippet->status->privacyStatus || 'unlisted' == $item->snippet->status->privacyStatus ) ) {
799 + $status = 'public';
800 + }
801 +
802 + if ( 'youtube#searchResult' == $item->kind ) {
803 + $status = 'public';
804 + }
805 +
806 + if ( 'public' == $status ) {
578 807 $videos[] = $video;
579 808 }
809 + }
810 +
811 + if ( 0 == count( $videos ) ) {
812 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
580 813 }
581 814
582 815 return $videos;
583 816 }