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 +376 -147 1.2.02.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,25 +59,30 @@
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'] ) ) {
70 - return $this->get_error( __( 'YouTube playlist URL is required.', 'automatic-youtube-gallery' ) );
83 + if ( empty( $params['src'] ) ) {
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 );
74 88 break;
@@ -73,47 +87,58 @@
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 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'] ) ) {
96 - return $this->get_error( __( 'YouTube account username is required.', 'automatic-youtube-gallery' ) );
118 + if ( empty( $params['src'] ) ) {
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'] ) ) {
140 + if ( empty( $params['src'] ) ) {
116 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 );
@@ -119,18 +144,33 @@
119 144 $response = $this->request_api_search( $params );
120 145 break;
121 146
122 147 case 'videos':
123 - if ( empty( $params['id'] ) ) {
124 - return $this->get_error( __( 'Atleast one YouTube video URL is required.', 'automatic-youtube-gallery' ) );
148 + if ( empty( $params['src'] ) ) {
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 );
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'] ) ) {
132 - return $this->get_error( __( 'YouTube video URL is required.', 'automatic-youtube-gallery' ) );
171 + if ( empty( $params['src'] ) ) {
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 );
136 176 break;
@@ -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,8 +248,139 @@
203 248 return $id;
204 249 }
205 250
206 251 /**
252 + * Get the channel ID.
253 + *
254 + * @since 2.0.0
255 + * @access private
256 + * @param array $params Array of query params.
257 + * @return string
258 + */
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' );
262 +
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' );
266 +
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 + }
307 + }
308 +
309 + return $id;
310 + }
311 +
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 + /**
207 383 * Get videos using playlistItems API.
208 384 *
209 385 * @since 1.0.0
210 386 * @access private
@@ -213,9 +389,9 @@
213 389 */
214 390 private function request_api_playlist_items( $params = array() ) {
215 391 $api_url = $this->get_api_url( 'playlistItems.list' );
216 392
217 - $params['playlistId'] = $this->parse_youtube_id_from_url( $params['id'], 'playlist' );
393 + $params['playlistId'] = $this->parse_youtube_id_from_url( $params['src'], 'playlist' );
218 394
219 395 $api_params = $this->safe_merge_params(
220 396 array(
221 397 'playlistId' => '',
@@ -236,33 +412,88 @@
236 412 if ( isset( $videos->error ) ) {
237 413 return $videos;
238 414 }
239 415
240 - // Process result
416 + // Process output
241 417 $response = new stdClass();
242 418 $response->page_info = $this->parse_page_info( $api_response );
243 419 $response->videos = $videos;
244 420
245 421 return $response;
246 - }
422 + }
247 423
248 424 /**
249 - * Find playlistId using channels API.
425 + * Get videos using search API.
250 426 *
251 427 * @since 1.0.0
252 428 * @access private
429 + * @param array $params Array of query params.
430 + * @return stdClass
431 + */
432 + private function request_api_search( $params = array() ) {
433 + $api_url = $this->get_api_url( 'search.list' );
434 +
435 + $params['q'] = $params['src'];
436 +
437 + if ( ! empty( $params['q'] ) ) {
438 + $params['q'] = str_replace( '|', '%7C', $params['q'] );
439 + }
440 +
441 + $params['type'] = 'video'; // Overrides user defined type value 'search'
442 +
443 + $api_params = $this->safe_merge_params(
444 + array(
445 + 'q' => '',
446 + 'channelId' => '',
447 + 'type' => 'video',
448 + 'videoEmbeddable' => true,
449 + 'part' => 'id,snippet',
450 + 'order' => 'date',
451 + 'maxResults' => 50,
452 + 'pageToken' => '',
453 + 'cache' => 0
454 + ),
455 + $params
456 + );
457 +
458 + $api_response = $this->request_api( $api_url, $api_params );
459 + if ( isset( $api_response->error ) ) {
460 + return $api_response;
461 + }
462 +
463 + $videos = $this->parse_videos( $api_response );
464 + if ( isset( $videos->error ) ) {
465 + return $videos;
466 + }
467 +
468 + // Process output
469 + $response = new stdClass();
470 + $response->page_info = $this->parse_page_info( $api_response );
471 + $response->videos = $videos;
472 +
473 + return $response;
474 + }
475 +
476 + /**
477 + * Get live video using search API.
478 + *
479 + * @since 2.3.7
480 + * @access private
253 481 * @param array $params Array of query params.
254 482 * @return mixed
255 483 */
256 - private function request_api_channels( $params = array() ) {
257 - $api_url = $this->get_api_url( 'channels.list' );
484 + private function request_api_live_video( $params = array() ) {
485 + $api_url = $this->get_api_url( 'search.list' );
258 486
487 + $params['type'] = 'video'; // Overrides user defined type value 'livestream'
488 +
259 489 $api_params = $this->safe_merge_params(
260 490 array(
261 - 'id' => '',
262 - 'forUsername' => '',
263 - 'part' => 'contentDetails',
264 - 'cache' => 0
491 + 'type' => 'video',
492 + 'eventType' => 'live',
493 + 'part' => 'snippet',
494 + 'channelId' => '',
495 + 'cache' => 0
265 496 ),
266 497 $params
267 498 );
268 499
@@ -270,22 +501,23 @@
270 501 if ( isset( $api_response->error ) ) {
271 502 return $api_response;
272 503 }
273 504
274 - $items = $api_response->items;
275 - if ( ! is_array( $items ) || count( $items ) == 0 ) {
276 - return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
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>' );
277 509 }
278 510
279 - // Process result
511 + // Process output
280 512 $response = new stdClass();
281 - $response->playlistId = $items[0]->contentDetails->relatedPlaylists->uploads;
513 + $response->videos = $videos;
282 514
283 - return $response;
515 + return $response;
284 516 }
285 517
286 518 /**
287 - * Get videos using search API.
519 + * Get details of the given video ID.
288 520 *
289 521 * @since 1.0.0
290 522 * @access private
291 523 * @param array $params Array of query params.
@@ -290,33 +522,22 @@
290 522 * @access private
291 523 * @param array $params Array of query params.
292 524 * @return stdClass
293 525 */
294 - private function request_api_search( $params = array() ) {
295 - $api_url = $this->get_api_url( 'search.list' );
296 -
297 - $params['q'] = $params['id'];
298 -
299 - if ( ! empty( $params['q'] ) ) {
300 - $params['q'] = str_replace( '|', '%7C', $params['q'] );
301 - }
302 -
303 - $params['type'] = 'video'; // Overrides user defined type value 'search'
304 -
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 +
305 531 $api_params = $this->safe_merge_params(
306 532 array(
307 - 'q' => '',
308 - 'channelId' => '',
309 - 'type' => 'video',
310 - 'part' => 'id,snippet',
311 - 'order' => 'date',
312 - 'maxResults' => 50,
313 - 'pageToken' => '',
314 - 'cache' => 0
315 - ),
533 + 'id' => '',
534 + 'part' => 'id,snippet,contentDetails,status',
535 + 'cache' => 0
536 + ),
316 537 $params
317 - );
318 -
538 + );
539 +
319 540 $api_response = $this->request_api( $api_url, $api_params );
320 541 if ( isset( $api_response->error ) ) {
321 542 return $api_response;
322 543 }
@@ -325,15 +546,14 @@
325 546 if ( isset( $videos->error ) ) {
326 547 return $videos;
327 548 }
328 549
329 - // Process result
550 + // Process output
330 551 $response = new stdClass();
331 - $response->page_info = $this->parse_page_info( $api_response );
332 552 $response->videos = $videos;
333 553
334 554 return $response;
335 - }
555 + }
336 556
337 557 /**
338 558 * Get details of the given video IDs.
339 559 *
@@ -344,10 +564,10 @@
344 564 */
345 565 private function request_api_videos( $params = array() ) {
346 566 $api_url = $this->get_api_url( 'videos.list' );
347 567
348 - $urls = str_replace( "\n\r", ',', $params['id'] );
349 - $urls = str_replace( ' ', ',', $params['id'] );
568 + $urls = str_replace( "\n\r", ',', $params['src'] );
569 + $urls = str_replace( ' ', ',', $urls );
350 570 $urls = explode( ',', $urls );
351 571 $urls = array_filter( $urls );
352 572
353 573 $all_ids = array();
@@ -371,9 +591,9 @@
371 591
372 592 $api_params = $this->safe_merge_params(
373 593 array(
374 594 'id' => '',
375 - 'part' => 'id,snippet,contentDetails,statistics,status',
595 + 'part' => 'id,snippet,contentDetails,status',
376 596 'cache' => 0
377 597 ),
378 598 $params
379 599 );
@@ -383,10 +603,13 @@
383 603 return $api_response;
384 604 }
385 605
386 606 $videos = $this->parse_videos( $api_response );
607 + if ( isset( $videos->error ) ) {
608 + return $videos;
609 + }
387 610
388 - // Process result
611 + // Process output
389 612 $response = new stdClass();
390 613 $response->videos = $videos;
391 614
392 615 $response->page_info = array(
@@ -402,44 +625,8 @@
402 625 }
403 626
404 627 return $response;
405 628 }
406 -
407 - /**
408 - * Get details of the given video ID.
409 - *
410 - * @since 1.0.0
411 - * @access private
412 - * @param array $params Array of query params.
413 - * @return stdClass
414 - */
415 - private function request_api_video( $params = array() ) {
416 - $api_url = $this->get_api_url( 'videos.list' );
417 -
418 - $params['id'] = $this->parse_youtube_id_from_url( $params['id'], 'video' );
419 -
420 - $api_params = $this->safe_merge_params(
421 - array(
422 - 'id' => '',
423 - 'part' => 'id,snippet,contentDetails,statistics,status',
424 - 'cache' => 0
425 - ),
426 - $params
427 - );
428 -
429 - $api_response = $this->request_api( $api_url, $api_params );
430 - if ( isset( $api_response->error ) ) {
431 - return $api_response;
432 - }
433 -
434 - $videos = $this->parse_videos( $api_response );
435 -
436 - // Process result
437 - $response = new stdClass();
438 - $response->videos = $videos;
439 -
440 - return $response;
441 - }
442 629
443 630 /**
444 631 * Get API URL by request.
445 632 *
@@ -456,35 +643,48 @@
456 643 * Request data from the API server.
457 644 *
458 645 * @since 1.0.0
459 646 * @access private
460 - * @param string $url YouTube API URL.
461 - * @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"
462 650 * @return mixed
463 651 */
464 - private function request_api( $url, $params ) {
652 + private function request_api( $url, $params, $context = 'videos' ) {
465 653 $params['key'] = $this->api_key;
466 654
467 - // Request data from transients
468 - $transient_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
469 - $transient_key = 'ayg_' . md5( $transient_url );
470 - $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 );
471 660
472 - if ( ! empty( $transient_data ) ) {
473 - return $transient_data;
661 + if ( ! empty( $cache_data ) ) {
662 + return $cache_data;
663 + }
474 664 }
475 665
476 - $transient_expiration = 0;
477 -
666 + // Request from API
667 + $cache_duration = 0;
478 668 if ( isset( $params['cache'] ) ) {
479 - $transient_expiration = (int) $params['cache'];
669 + $cache_duration = (int) $params['cache'];
480 670 unset( $params['cache'] );
481 671 }
482 672
483 - // Request data from API server
673 + $q = '';
674 + if ( isset( $params['q'] ) ) {
675 + $q = $params['q'];
676 + unset( $params['q'] );
677 + }
678 +
484 679 $url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params );
680 + if ( ! empty( $q ) ) {
681 + $url .= '&q=' . $q;
682 + }
485 683
486 - $request = wp_remote_get( $url );
684 + $request = wp_remote_get( $url, array(
685 + 'headers' => [ 'referer' => home_url() ],
686 + ) );
487 687
488 688 if ( is_wp_error( $request ) ) {
489 689 return $this->get_error( $request->get_error_message() );
490 690 }
@@ -502,24 +702,32 @@
502 702
503 703 return $this->get_error( $message );
504 704 }
505 705
506 - // Store data in transients
507 - if ( $transient_expiration > 0 ) {
508 - 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 );
509 710
510 - // Get the current list of transients
511 - $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 + }
512 716
513 - // Append our new one
514 - if ( ! in_array( $transient_key, $transient_keys ) ) {
515 - $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 );
516 724 }
517 -
518 - // Save it to the DB
519 - update_option( 'ayg_transient_keys', $transient_keys );
520 725 }
521 726
727 + // Store videos in our custom database table "{$wpdb->prefix}ayg_videos"
728 + ayg_db_store_videos( $data );
729 +
522 730 // Finally return the data
523 731 return $data;
524 732 }
525 733
@@ -531,10 +739,13 @@
531 739 * @param object $data YouTube API response object.
532 740 * @return mixed
533 741 */
534 742 private function parse_videos( $data ) {
535 - $items = $data->items;
743 + if ( ! isset( $data->items ) ) {
744 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
745 + }
536 746
747 + $items = $data->items;
537 748 if ( ! is_array( $items ) || 0 == count( $items ) ) {
538 749 return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
539 750 }
540 751
@@ -553,10 +764,17 @@
553 764 } elseif ( isset( $item->id ) && isset( $item->id->videoId ) ) {
554 765 $video->id = $item->id->videoId;
555 766 } elseif ( isset( $item->id ) ) {
556 767 $video->id = $item->id;
557 - }
768 + }
558 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 +
559 777 // Video title
560 778 $video->title = $item->snippet->title;
561 779
562 780 // Video description
@@ -569,19 +787,30 @@
569 787
570 788 // Video publish date
571 789 $video->published_at = $item->snippet->publishedAt;
572 790
573 - // Is video private?
574 - $video->is_private = 0;
791 + // Push resulting object to the main array
792 + $status = 'private';
793 +
794 + if ( isset( $item->status ) && ( 'public' == $item->status->privacyStatus || 'unlisted' == $item->status->privacyStatus ) ) {
795 + $status = 'public';
796 + }
575 797
576 - if ( ! isset( $item->snippet->thumbnails ) || ( isset( $item->snippet->status ) && 'private' == $item->snippet->status->privacyStatus ) ) {
577 - $video->is_private = 1;
798 + if ( isset( $item->snippet->status ) && ( 'public' == $item->snippet->status->privacyStatus || 'unlisted' == $item->snippet->status->privacyStatus ) ) {
799 + $status = 'public';
578 800 }
579 801
580 - // Push resulting object to the main array
581 - if ( ! $video->is_private ) {
802 + if ( 'youtube#searchResult' == $item->kind ) {
803 + $status = 'public';
804 + }
805 +
806 + if ( 'public' == $status ) {
582 807 $videos[] = $video;
583 808 }
809 + }
810 +
811 + if ( 0 == count( $videos ) ) {
812 + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) );
584 813 }
585 814
586 815 return $videos;
587 816 }