| @@ -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,18 @@ | ||
| 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 ( empty( $playlist_id ) ) { | |
| 105 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 108 | 106 | } |
| 107 | + | |
| 108 | + // Get videos using the playlist id | |
| 109 | + $params['src'] = $playlist_id; | |
| 110 | + $response = $this->request_api_playlist_items( $params ); | |
| 109 | 111 | break; |
| 110 | 112 | |
| 111 | 113 | case 'username': |
| 112 | 114 | if ( empty( $params['src'] ) ) { |
| @@ -112,22 +114,24 @@ | ||
| 112 | 114 | if ( empty( $params['src'] ) ) { |
| 113 | 115 | return $this->get_error( __( 'YouTube Account Username is required.', 'automatic-youtube-gallery' ) ); |
| 114 | 116 | } |
| 115 | 117 | |
| 116 | - // Find playlistId from the channel | |
| 118 | + // Get playlist id from the channel | |
| 117 | 119 | $params['forUsername'] = $this->parse_youtube_id_from_url( $params['src'], 'username' ); |
| 118 | - $response = $this->request_api_channels( $params ); | |
| 120 | + $playlist_id = $this->get_playlist_id( $params ); | |
| 119 | 121 | |
| 120 | - // Get videos using the playlistId | |
| 121 | - if ( ! isset( $response->error ) ) { | |
| 122 | - $params['src'] = $response->playlistId; | |
| 123 | - $response = $this->request_api_playlist_items( $params ); | |
| 122 | + if ( empty( $playlist_id ) ) { | |
| 123 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 124 | 124 | } |
| 125 | + | |
| 126 | + // Get videos using the playlist id | |
| 127 | + $params['src'] = $playlist_id; | |
| 128 | + $response = $this->request_api_playlist_items( $params ); | |
| 125 | 129 | break; |
| 126 | 130 | |
| 127 | 131 | case 'search': |
| 128 | 132 | if ( empty( $params['src'] ) ) { |
| 129 | - return $this->get_error( __( 'Cannot search an empty string. A search term is required.', 'automatic-youtube-gallery' ) ); | |
| 133 | + return $this->get_error( __( 'Cannot search an empty string. A search keyword is required.', 'automatic-youtube-gallery' ) ); | |
| 130 | 134 | } |
| 131 | 135 | |
| 132 | 136 | $response = $this->request_api_search( $params ); |
| 133 | 137 | break; |
| @@ -242,16 +246,55 @@ | ||
| 242 | 246 | * @param array $params Array of query params. |
| 243 | 247 | * @return string |
| 244 | 248 | */ |
| 245 | 249 | private function get_channel_id( $params = array() ) { |
| 250 | + // Parse channel ID from URL: https://www.youtube.com/channel/XXXXXXXXXX | |
| 246 | 251 | $id = $this->parse_youtube_id_from_url( $params['src'], 'channel' ); |
| 247 | 252 | |
| 248 | 253 | if ( empty( $id ) ) { |
| 249 | - $response = $this->request_api_video( $params ); | |
| 254 | + // Get channel ID from a Video URL: https://www.youtube.com/watch?v=XXXXXXXXXX | |
| 255 | + $video_id = $this->parse_youtube_id_from_url( $params['src'], 'video' ); | |
| 250 | 256 | |
| 251 | - if ( isset( $response->videos ) ) { | |
| 252 | - $id = $response->videos[0]->channel_id; | |
| 257 | + // Request from cache | |
| 258 | + $channel_ids = get_option( 'ayg_channel_ids', array() ); | |
| 259 | + if ( ! is_array( $channel_ids ) ) { | |
| 260 | + $channel_ids = (array) $channel_ids; | |
| 253 | 261 | } |
| 262 | + | |
| 263 | + if ( isset( $channel_ids[ $video_id ] ) && ! empty( $channel_ids[ $video_id ] ) ) { | |
| 264 | + return $channel_ids[ $video_id ]; | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Request from API | |
| 268 | + $api_url = $this->get_api_url( 'videos.list' ); | |
| 269 | + | |
| 270 | + $params['id'] = $video_id; | |
| 271 | + | |
| 272 | + $api_params = $this->safe_merge_params( | |
| 273 | + array( | |
| 274 | + 'id' => '', | |
| 275 | + 'part' => 'id,snippet,contentDetails,status', | |
| 276 | + 'cache' => 0 | |
| 277 | + ), | |
| 278 | + $params | |
| 279 | + ); | |
| 280 | + | |
| 281 | + $api_response = $this->request_api( $api_url, $api_params, 'channel_id' ); | |
| 282 | + if ( isset( $api_response->error ) ) { | |
| 283 | + return $id; | |
| 284 | + } | |
| 285 | + | |
| 286 | + $videos = $this->parse_videos( $api_response ); | |
| 287 | + if ( isset( $videos->error ) ) { | |
| 288 | + return $id; | |
| 289 | + } | |
| 290 | + | |
| 291 | + // Process output | |
| 292 | + if ( $id = $videos[0]->channel_id ) { | |
| 293 | + // Store in cache | |
| 294 | + $channel_ids[ $video_id ] = $id; | |
| 295 | + update_option( 'ayg_channel_ids', $channel_ids ); | |
| 296 | + } | |
| 254 | 297 | } |
| 255 | 298 | |
| 256 | 299 | return $id; |
| 257 | 300 | } |
| @@ -256,8 +299,78 @@ | ||
| 256 | 299 | return $id; |
| 257 | 300 | } |
| 258 | 301 | |
| 259 | 302 | /** |
| 303 | + * Get playlist id using channels API. | |
| 304 | + * | |
| 305 | + * @since 1.0.0 | |
| 306 | + * @access private | |
| 307 | + * @param array $params Array of query params. | |
| 308 | + * @return mixed | |
| 309 | + */ | |
| 310 | + private function get_playlist_id( $params = array() ) { | |
| 311 | + // Request from cache | |
| 312 | + $playlist_ids = get_option( 'ayg_playlist_ids', array() ); | |
| 313 | + if ( ! is_array( $playlist_ids ) ) { | |
| 314 | + $playlist_ids = (array) $playlist_ids; | |
| 315 | + } | |
| 316 | + | |
| 317 | + $key = ''; | |
| 318 | + | |
| 319 | + if ( isset( $params['forUsername'] ) && ! empty( $params['forUsername'] ) ) { | |
| 320 | + $key = $params['forUsername']; | |
| 321 | + } | |
| 322 | + | |
| 323 | + if ( isset( $params['id'] ) && ! empty( $params['id'] ) ) { | |
| 324 | + unset( $params['forUsername'] ); | |
| 325 | + $key = $params['id']; | |
| 326 | + } | |
| 327 | + | |
| 328 | + if ( isset( $playlist_ids[ $key ] ) && ! empty( $playlist_ids[ $key ] ) ) { | |
| 329 | + return $playlist_ids[ $key ]; | |
| 330 | + } | |
| 331 | + | |
| 332 | + // Request from API | |
| 333 | + $api_url = $this->get_api_url( 'channels.list' ); | |
| 334 | + | |
| 335 | + $api_params = $this->safe_merge_params( | |
| 336 | + array( | |
| 337 | + 'id' => '', | |
| 338 | + 'forUsername' => '', | |
| 339 | + 'part' => 'contentDetails', | |
| 340 | + 'cache' => 0 | |
| 341 | + ), | |
| 342 | + $params | |
| 343 | + ); | |
| 344 | + | |
| 345 | + $api_response = $this->request_api( $api_url, $api_params, 'playlist_id' ); | |
| 346 | + if ( isset( $api_response->error ) ) { | |
| 347 | + return $api_response; | |
| 348 | + } | |
| 349 | + | |
| 350 | + if ( ! isset( $api_response->items ) ) { | |
| 351 | + return false; | |
| 352 | + } | |
| 353 | + | |
| 354 | + $items = $api_response->items; | |
| 355 | + if ( ! is_array( $items ) || count( $items ) == 0 ) { | |
| 356 | + return false; | |
| 357 | + } | |
| 358 | + | |
| 359 | + // Process output | |
| 360 | + if ( $id = $items[0]->contentDetails->relatedPlaylists->uploads ) { | |
| 361 | + // Store in cache | |
| 362 | + $playlist_ids[ $key ] = $id; | |
| 363 | + update_option( 'ayg_playlist_ids', $playlist_ids ); | |
| 364 | + | |
| 365 | + // Return | |
| 366 | + return $id; | |
| 367 | + } | |
| 368 | + | |
| 369 | + return false; | |
| 370 | + } | |
| 371 | + | |
| 372 | + /** | |
| 260 | 373 | * Get videos using playlistItems API. |
| 261 | 374 | * |
| 262 | 375 | * @since 1.0.0 |
| 263 | 376 | * @access private |
| @@ -295,53 +408,11 @@ | ||
| 295 | 408 | $response->page_info = $this->parse_page_info( $api_response ); |
| 296 | 409 | $response->videos = $videos; |
| 297 | 410 | |
| 298 | 411 | return $response; |
| 299 | - } | |
| 412 | + } | |
| 300 | 413 | |
| 301 | 414 | /** |
| 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 | 415 | * Get videos using search API. |
| 345 | 416 | * |
| 346 | 417 | * @since 1.0.0 |
| 347 | 418 | * @access private |
| @@ -520,17 +591,18 @@ | ||
| 520 | 591 | * Request data from the API server. |
| 521 | 592 | * |
| 522 | 593 | * @since 1.0.0 |
| 523 | 594 | * @access private |
| 524 | - * @param string $url YouTube API URL. | |
| 525 | - * @param array $params Array of query params. | |
| 595 | + * @param string $url YouTube API URL. | |
| 596 | + * @param array $params Array of query params. | |
| 597 | + * @param string $context "channel_id", "playlist_id", or "videos" | |
| 526 | 598 | * @return mixed |
| 527 | 599 | */ |
| 528 | - private function request_api( $url, $params ) { | |
| 600 | + private function request_api( $url, $params, $context = 'videos' ) { | |
| 529 | 601 | $params['key'] = $this->api_key; |
| 530 | 602 | |
| 531 | - // Request data from cache | |
| 532 | - if ( $this->cache_api_results ) { | |
| 603 | + // Request from cache | |
| 604 | + if ( ! $this->is_development_mode ) { | |
| 533 | 605 | $cache_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params ); |
| 534 | 606 | $cache_key = 'ayg_' . md5( $cache_url ); |
| 535 | 607 | $cache_data = get_transient( $cache_key ); |
| 536 | 608 | |
| @@ -538,9 +610,9 @@ | ||
| 538 | 610 | return $cache_data; |
| 539 | 611 | } |
| 540 | 612 | } |
| 541 | 613 | |
| 542 | - // Request data from API server | |
| 614 | + // Request from API | |
| 543 | 615 | $cache_duration = 0; |
| 544 | 616 | if ( isset( $params['cache'] ) ) { |
| 545 | 617 | $cache_duration = (int) $params['cache']; |
| 546 | 618 | unset( $params['cache'] ); |
| @@ -578,27 +650,32 @@ | ||
| 578 | 650 | |
| 579 | 651 | return $this->get_error( $message ); |
| 580 | 652 | } |
| 581 | 653 | |
| 582 | - // Store data in cache (transients) | |
| 583 | - if ( $this->cache_api_results && $cache_duration > 0 ) { | |
| 584 | - set_transient( $cache_key, $data, $cache_duration ); | |
| 654 | + // Store in cache (transients) | |
| 655 | + if ( ! $this->is_development_mode && 'videos' == $context && $cache_duration > 0 ) { | |
| 656 | + if ( isset( $data->items ) && is_array( $data->items ) && count( $data->items ) > 0 ) { | |
| 657 | + set_transient( $cache_key, $data, $cache_duration ); | |
| 585 | 658 | |
| 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 | - } | |
| 659 | + // Get the current list of transients | |
| 660 | + $cache_keys = get_option( 'ayg_transient_keys', array() ); | |
| 661 | + if ( ! is_array( $cache_keys ) ) { | |
| 662 | + $cache_keys = (array) $cache_keys; | |
| 663 | + } | |
| 591 | 664 | |
| 592 | - // Append our new one | |
| 593 | - if ( ! in_array( $cache_key, $cache_keys ) ) { | |
| 594 | - $cache_keys[] = $cache_key; | |
| 665 | + // Append our new one | |
| 666 | + if ( ! in_array( $cache_key, $cache_keys ) ) { | |
| 667 | + $cache_keys[] = $cache_key; | |
| 668 | + } | |
| 669 | + | |
| 670 | + // Save it to the DB | |
| 671 | + update_option( 'ayg_transient_keys', $cache_keys ); | |
| 595 | 672 | } |
| 596 | - | |
| 597 | - // Save it to the DB | |
| 598 | - update_option( 'ayg_transient_keys', $cache_keys ); | |
| 599 | 673 | } |
| 600 | 674 | |
| 675 | + // Store videos in our custom database table "{$wpdb->prefix}ayg_videos" | |
| 676 | + ayg_db_store_videos( $data ); | |
| 677 | + | |
| 601 | 678 | // Finally return the data |
| 602 | 679 | return $data; |
| 603 | 680 | } |
| 604 | 681 | |