| @@ -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,51 @@ | ||
| 73 | 87 | $response = $this->request_api_playlist_items( $params ); |
| 74 | 88 | break; |
| 75 | 89 | |
| 76 | 90 | case 'channel': |
| 77 | - if ( empty( $params['id'] ) ) { | |
| 78 | - return $this->get_error( __( 'YouTube Channel ID (or) URL is required.', 'automatic-youtube-gallery' ) ); | |
| 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' ) ); | |
| 79 | 93 | } |
| 80 | 94 | |
| 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 ); | |
| 95 | + $params['id'] = $this->get_channel_id( $params ); | |
| 84 | 96 | |
| 85 | - // Get videos using the playlistId | |
| 86 | - if ( ! isset( $response->error ) ) { | |
| 97 | + if ( empty( $params['id'] ) ) { | |
| 98 | + return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) ); | |
| 99 | + } | |
| 87 | 100 | |
| 88 | - $params['id'] = $response->playlistId; | |
| 89 | - $response = $this->request_api_playlist_items( $params ); | |
| 101 | + // Get playlist id from the channel | |
| 102 | + $playlist_id = $this->get_playlist_id( $params ); | |
| 90 | 103 | |
| 104 | + if ( empty( $playlist_id ) ) { | |
| 105 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 91 | 106 | } |
| 107 | + | |
| 108 | + // Get videos using the playlist id | |
| 109 | + $params['src'] = $playlist_id; | |
| 110 | + $response = $this->request_api_playlist_items( $params ); | |
| 92 | 111 | break; |
| 93 | 112 | |
| 94 | 113 | case 'username': |
| 95 | - if ( empty( $params['id'] ) ) { | |
| 114 | + if ( empty( $params['src'] ) ) { | |
| 96 | 115 | return $this->get_error( __( 'YouTube Account Username is required.', 'automatic-youtube-gallery' ) ); |
| 97 | 116 | } |
| 98 | 117 | |
| 99 | - // Find playlistId from the channel | |
| 100 | - $params['forUsername'] = $this->parse_youtube_id_from_url( $params['id'], 'username' ); | |
| 101 | - unset( $params['id'] ); | |
| 118 | + // Get playlist id from the channel | |
| 119 | + $params['forUsername'] = $this->parse_youtube_id_from_url( $params['src'], 'username' ); | |
| 120 | + $playlist_id = $this->get_playlist_id( $params ); | |
| 102 | 121 | |
| 103 | - $response = $this->request_api_channels( $params ); | |
| 122 | + if ( empty( $playlist_id ) ) { | |
| 123 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 124 | + } | |
| 104 | 125 | |
| 105 | - // Get videos using the playlistId | |
| 106 | - if ( ! isset( $response->error ) ) { | |
| 107 | - | |
| 108 | - $params['id'] = $response->playlistId; | |
| 109 | - $response = $this->request_api_playlist_items( $params ); | |
| 110 | - | |
| 111 | - } | |
| 126 | + // Get videos using the playlist id | |
| 127 | + $params['src'] = $playlist_id; | |
| 128 | + $response = $this->request_api_playlist_items( $params ); | |
| 112 | 129 | break; |
| 113 | 130 | |
| 114 | 131 | 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' ) ); | |
| 132 | + if ( empty( $params['src'] ) ) { | |
| 133 | + return $this->get_error( __( 'Cannot search an empty string. A search keyword is required.', 'automatic-youtube-gallery' ) ); | |
| 117 | 134 | } |
| 118 | 135 | |
| 119 | 136 | $response = $this->request_api_search( $params ); |
| 120 | 137 | break; |
| @@ -119,9 +136,9 @@ | ||
| 119 | 136 | $response = $this->request_api_search( $params ); |
| 120 | 137 | break; |
| 121 | 138 | |
| 122 | 139 | case 'videos': |
| 123 | - if ( empty( $params['id'] ) ) { | |
| 140 | + if ( empty( $params['src'] ) ) { | |
| 124 | 141 | return $this->get_error( __( 'Atleast one YouTube Video ID (or) URL is required.', 'automatic-youtube-gallery' ) ); |
| 125 | 142 | } |
| 126 | 143 | |
| 127 | 144 | $response = $this->request_api_videos( $params ); |
| @@ -126,10 +143,23 @@ | ||
| 126 | 143 | |
| 127 | 144 | $response = $this->request_api_videos( $params ); |
| 128 | 145 | break; |
| 129 | 146 | |
| 147 | + case 'livestream': | |
| 148 | + if ( empty( $params['src'] ) ) { | |
| 149 | + return $this->get_error( __( 'YouTube Channel ID (or) or a YouTube Video URL from the Channel is required.', 'automatic-youtube-gallery' ) ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + $response = new stdClass(); | |
| 153 | + $response->id = $this->get_channel_id( $params ); | |
| 154 | + | |
| 155 | + if ( empty( $response->id ) ) { | |
| 156 | + return $this->get_error( __( 'Invalid YouTube Channel ID.', 'automatic-youtube-gallery' ) ); | |
| 157 | + } | |
| 158 | + break; | |
| 159 | + | |
| 130 | 160 | default: // video |
| 131 | - if ( empty( $params['id'] ) ) { | |
| 161 | + if ( empty( $params['src'] ) ) { | |
| 132 | 162 | return $this->get_error( __( 'YouTube Video ID (or) URL is required.', 'automatic-youtube-gallery' ) ); |
| 133 | 163 | } |
| 134 | 164 | |
| 135 | 165 | $response = $this->request_api_video( $params ); |
| @@ -148,9 +178,10 @@ | ||
| 148 | 178 | * @param string $type Type of the URL (playlist|channel|video). |
| 149 | 179 | * @return mixed |
| 150 | 180 | */ |
| 151 | 181 | private function parse_youtube_id_from_url( $url, $type = 'video' ) { |
| 152 | - $id = $url; | |
| 182 | + $url = trim( $url ); | |
| 183 | + $id = $url; | |
| 153 | 184 | |
| 154 | 185 | switch ( $type ) { |
| 155 | 186 | case 'playlist': |
| 156 | 187 | if ( preg_match( '/list=(.*)&?\/?/', $url, $matches ) ) { |
| @@ -158,8 +189,12 @@ | ||
| 158 | 189 | } |
| 159 | 190 | break; |
| 160 | 191 | |
| 161 | 192 | case 'channel': |
| 193 | + if ( wp_http_validate_url( $id ) ) { | |
| 194 | + $id = ''; | |
| 195 | + } | |
| 196 | + | |
| 162 | 197 | $url = parse_url( rtrim( $url, '/' ) ); |
| 163 | 198 | |
| 164 | 199 | if ( isset( $url['path'] ) && preg_match( '/^\/channel\/(([^\/])+?)$/', $url['path'], $matches ) ) { |
| 165 | 200 | $id = $matches[1]; |
| @@ -203,51 +238,70 @@ | ||
| 203 | 238 | return $id; |
| 204 | 239 | } |
| 205 | 240 | |
| 206 | 241 | /** |
| 207 | - * Get videos using playlistItems API. | |
| 242 | + * Get the channel ID. | |
| 208 | 243 | * |
| 209 | - * @since 1.0.0 | |
| 244 | + * @since 2.0.0 | |
| 210 | 245 | * @access private |
| 211 | - * @param array $params Array of query params. | |
| 212 | - * @return stdClass | |
| 246 | + * @param array $params Array of query params. | |
| 247 | + * @return string | |
| 213 | 248 | */ |
| 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' ); | |
| 249 | + private function get_channel_id( $params = array() ) { | |
| 250 | + // Parse channel ID from URL: https://www.youtube.com/channel/XXXXXXXXXX | |
| 251 | + $id = $this->parse_youtube_id_from_url( $params['src'], 'channel' ); | |
| 218 | 252 | |
| 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 | - } | |
| 253 | + if ( empty( $id ) ) { | |
| 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' ); | |
| 234 | 256 | |
| 235 | - $videos = $this->parse_videos( $api_response ); | |
| 236 | - if ( isset( $videos->error ) ) { | |
| 237 | - return $videos; | |
| 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; | |
| 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 | + } | |
| 238 | 297 | } |
| 239 | 298 | |
| 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; | |
| 299 | + return $id; | |
| 246 | 300 | } |
| 247 | 301 | |
| 248 | 302 | /** |
| 249 | - * Find playlistId using channels API. | |
| 303 | + * Get playlist id using channels API. | |
| 250 | 304 | * |
| 251 | 305 | * @since 1.0.0 |
| 252 | 306 | * @access private |
| 253 | 307 | * @param array $params Array of query params. |
| @@ -252,9 +306,31 @@ | ||
| 252 | 306 | * @access private |
| 253 | 307 | * @param array $params Array of query params. |
| 254 | 308 | * @return mixed |
| 255 | 309 | */ |
| 256 | - private function request_api_channels( $params = array() ) { | |
| 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 | |
| 257 | 333 | $api_url = $this->get_api_url( 'channels.list' ); |
| 258 | 334 | |
| 259 | 335 | $api_params = $this->safe_merge_params( |
| 260 | 336 | array( |
| @@ -265,24 +341,76 @@ | ||
| 265 | 341 | ), |
| 266 | 342 | $params |
| 267 | 343 | ); |
| 268 | 344 | |
| 269 | - $api_response = $this->request_api( $api_url, $api_params ); | |
| 345 | + $api_response = $this->request_api( $api_url, $api_params, 'playlist_id' ); | |
| 270 | 346 | if ( isset( $api_response->error ) ) { |
| 271 | 347 | return $api_response; |
| 272 | 348 | } |
| 273 | 349 | |
| 350 | + if ( ! isset( $api_response->items ) ) { | |
| 351 | + return false; | |
| 352 | + } | |
| 353 | + | |
| 274 | 354 | $items = $api_response->items; |
| 275 | 355 | if ( ! is_array( $items ) || count( $items ) == 0 ) { |
| 276 | - return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 356 | + return false; | |
| 277 | 357 | } |
| 278 | 358 | |
| 279 | - // Process result | |
| 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 | + /** | |
| 373 | + * Get videos using playlistItems API. | |
| 374 | + * | |
| 375 | + * @since 1.0.0 | |
| 376 | + * @access private | |
| 377 | + * @param array $params Array of query params. | |
| 378 | + * @return stdClass | |
| 379 | + */ | |
| 380 | + private function request_api_playlist_items( $params = array() ) { | |
| 381 | + $api_url = $this->get_api_url( 'playlistItems.list' ); | |
| 382 | + | |
| 383 | + $params['playlistId'] = $this->parse_youtube_id_from_url( $params['src'], 'playlist' ); | |
| 384 | + | |
| 385 | + $api_params = $this->safe_merge_params( | |
| 386 | + array( | |
| 387 | + 'playlistId' => '', | |
| 388 | + 'part' => 'id,snippet,contentDetails,status', | |
| 389 | + 'maxResults' => 50, | |
| 390 | + 'pageToken' => '', | |
| 391 | + 'cache' => 0 | |
| 392 | + ), | |
| 393 | + $params | |
| 394 | + ); | |
| 395 | + | |
| 396 | + $api_response = $this->request_api( $api_url, $api_params ); | |
| 397 | + if ( isset( $api_response->error ) ) { | |
| 398 | + return $api_response; | |
| 399 | + } | |
| 400 | + | |
| 401 | + $videos = $this->parse_videos( $api_response ); | |
| 402 | + if ( isset( $videos->error ) ) { | |
| 403 | + return $videos; | |
| 404 | + } | |
| 405 | + | |
| 406 | + // Process output | |
| 280 | 407 | $response = new stdClass(); |
| 281 | - $response->playlistId = $items[0]->contentDetails->relatedPlaylists->uploads; | |
| 408 | + $response->page_info = $this->parse_page_info( $api_response ); | |
| 409 | + $response->videos = $videos; | |
| 282 | 410 | |
| 283 | - return $response; | |
| 284 | - } | |
| 411 | + return $response; | |
| 412 | + } | |
| 285 | 413 | |
| 286 | 414 | /** |
| 287 | 415 | * Get videos using search API. |
| 288 | 416 | * |
| @@ -293,9 +421,9 @@ | ||
| 293 | 421 | */ |
| 294 | 422 | private function request_api_search( $params = array() ) { |
| 295 | 423 | $api_url = $this->get_api_url( 'search.list' ); |
| 296 | 424 | |
| 297 | - $params['q'] = $params['id']; | |
| 425 | + $params['q'] = $params['src']; | |
| 298 | 426 | |
| 299 | 427 | if ( ! empty( $params['q'] ) ) { |
| 300 | 428 | $params['q'] = str_replace( '|', '%7C', $params['q'] ); |
| 301 | 429 | } |
| @@ -326,17 +454,56 @@ | ||
| 326 | 454 | if ( isset( $videos->error ) ) { |
| 327 | 455 | return $videos; |
| 328 | 456 | } |
| 329 | 457 | |
| 330 | - // Process result | |
| 458 | + // Process output | |
| 331 | 459 | $response = new stdClass(); |
| 332 | 460 | $response->page_info = $this->parse_page_info( $api_response ); |
| 333 | 461 | $response->videos = $videos; |
| 334 | 462 | |
| 335 | 463 | return $response; |
| 336 | - } | |
| 464 | + } | |
| 337 | 465 | |
| 338 | 466 | /** |
| 467 | + * Get details of the given video ID. | |
| 468 | + * | |
| 469 | + * @since 1.0.0 | |
| 470 | + * @access private | |
| 471 | + * @param array $params Array of query params. | |
| 472 | + * @return stdClass | |
| 473 | + */ | |
| 474 | + private function request_api_video( $params = array() ) { | |
| 475 | + $api_url = $this->get_api_url( 'videos.list' ); | |
| 476 | + | |
| 477 | + $params['id'] = $this->parse_youtube_id_from_url( $params['src'], 'video' ); | |
| 478 | + | |
| 479 | + $api_params = $this->safe_merge_params( | |
| 480 | + array( | |
| 481 | + 'id' => '', | |
| 482 | + 'part' => 'id,snippet,contentDetails,status', | |
| 483 | + 'cache' => 0 | |
| 484 | + ), | |
| 485 | + $params | |
| 486 | + ); | |
| 487 | + | |
| 488 | + $api_response = $this->request_api( $api_url, $api_params ); | |
| 489 | + if ( isset( $api_response->error ) ) { | |
| 490 | + return $api_response; | |
| 491 | + } | |
| 492 | + | |
| 493 | + $videos = $this->parse_videos( $api_response ); | |
| 494 | + if ( isset( $videos->error ) ) { | |
| 495 | + return $videos; | |
| 496 | + } | |
| 497 | + | |
| 498 | + // Process output | |
| 499 | + $response = new stdClass(); | |
| 500 | + $response->videos = $videos; | |
| 501 | + | |
| 502 | + return $response; | |
| 503 | + } | |
| 504 | + | |
| 505 | + /** | |
| 339 | 506 | * Get details of the given video IDs. |
| 340 | 507 | * |
| 341 | 508 | * @since 1.0.0 |
| 342 | 509 | * @access private |
| @@ -345,10 +512,10 @@ | ||
| 345 | 512 | */ |
| 346 | 513 | private function request_api_videos( $params = array() ) { |
| 347 | 514 | $api_url = $this->get_api_url( 'videos.list' ); |
| 348 | 515 | |
| 349 | - $urls = str_replace( "\n\r", ',', $params['id'] ); | |
| 350 | - $urls = str_replace( ' ', ',', $params['id'] ); | |
| 516 | + $urls = str_replace( "\n\r", ',', $params['src'] ); | |
| 517 | + $urls = str_replace( ' ', ',', $urls ); | |
| 351 | 518 | $urls = explode( ',', $urls ); |
| 352 | 519 | $urls = array_filter( $urls ); |
| 353 | 520 | |
| 354 | 521 | $all_ids = array(); |
| @@ -372,9 +539,9 @@ | ||
| 372 | 539 | |
| 373 | 540 | $api_params = $this->safe_merge_params( |
| 374 | 541 | array( |
| 375 | 542 | 'id' => '', |
| 376 | - 'part' => 'id,snippet,contentDetails,statistics,status', | |
| 543 | + 'part' => 'id,snippet,contentDetails,status', | |
| 377 | 544 | 'cache' => 0 |
| 378 | 545 | ), |
| 379 | 546 | $params |
| 380 | 547 | ); |
| @@ -384,10 +551,13 @@ | ||
| 384 | 551 | return $api_response; |
| 385 | 552 | } |
| 386 | 553 | |
| 387 | 554 | $videos = $this->parse_videos( $api_response ); |
| 555 | + if ( isset( $videos->error ) ) { | |
| 556 | + return $videos; | |
| 557 | + } | |
| 388 | 558 | |
| 389 | - // Process result | |
| 559 | + // Process output | |
| 390 | 560 | $response = new stdClass(); |
| 391 | 561 | $response->videos = $videos; |
| 392 | 562 | |
| 393 | 563 | $response->page_info = array( |
| @@ -403,44 +573,8 @@ | ||
| 403 | 573 | } |
| 404 | 574 | |
| 405 | 575 | return $response; |
| 406 | 576 | } |
| 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 | 577 | |
| 444 | 578 | /** |
| 445 | 579 | * Get API URL by request. |
| 446 | 580 | * |
| @@ -457,33 +591,44 @@ | ||
| 457 | 591 | * Request data from the API server. |
| 458 | 592 | * |
| 459 | 593 | * @since 1.0.0 |
| 460 | 594 | * @access private |
| 461 | - * @param string $url YouTube API URL. | |
| 462 | - * @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" | |
| 463 | 598 | * @return mixed |
| 464 | 599 | */ |
| 465 | - private function request_api( $url, $params ) { | |
| 600 | + private function request_api( $url, $params, $context = 'videos' ) { | |
| 466 | 601 | $params['key'] = $this->api_key; |
| 467 | 602 | |
| 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 ); | |
| 603 | + // Request from cache | |
| 604 | + if ( ! $this->is_development_mode ) { | |
| 605 | + $cache_url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params ); | |
| 606 | + $cache_key = 'ayg_' . md5( $cache_url ); | |
| 607 | + $cache_data = get_transient( $cache_key ); | |
| 472 | 608 | |
| 473 | - if ( ! empty( $transient_data ) ) { | |
| 474 | - return $transient_data; | |
| 609 | + if ( ! empty( $cache_data ) ) { | |
| 610 | + return $cache_data; | |
| 611 | + } | |
| 475 | 612 | } |
| 476 | 613 | |
| 477 | - $transient_expiration = 0; | |
| 478 | - | |
| 614 | + // Request from API | |
| 615 | + $cache_duration = 0; | |
| 479 | 616 | if ( isset( $params['cache'] ) ) { |
| 480 | - $transient_expiration = (int) $params['cache']; | |
| 617 | + $cache_duration = (int) $params['cache']; | |
| 481 | 618 | unset( $params['cache'] ); |
| 482 | 619 | } |
| 483 | 620 | |
| 484 | - // Request data from API server | |
| 621 | + $q = ''; | |
| 622 | + if ( isset( $params['q'] ) ) { | |
| 623 | + $q = $params['q']; | |
| 624 | + unset( $params['q'] ); | |
| 625 | + } | |
| 626 | + | |
| 485 | 627 | $url = $url . ( strpos( $url, '?' ) === false ? '?' : '' ) . http_build_query( $params ); |
| 628 | + if ( ! empty( $q ) ) { | |
| 629 | + $url .= '&q=' . $q; | |
| 630 | + } | |
| 486 | 631 | |
| 487 | 632 | $request = wp_remote_get( $url, array( |
| 488 | 633 | 'headers' => [ 'referer' => home_url() ], |
| 489 | 634 | ) ); |
| @@ -505,24 +650,32 @@ | ||
| 505 | 650 | |
| 506 | 651 | return $this->get_error( $message ); |
| 507 | 652 | } |
| 508 | 653 | |
| 509 | - // Store data in transients | |
| 510 | - if ( $transient_expiration > 0 ) { | |
| 511 | - set_transient( $transient_key, $data, $transient_expiration ); | |
| 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 ); | |
| 512 | 658 | |
| 513 | - // Get the current list of transients | |
| 514 | - $transient_keys = get_option( 'ayg_transient_keys', array() ); | |
| 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 | + } | |
| 515 | 664 | |
| 516 | - // Append our new one | |
| 517 | - if ( ! in_array( $transient_key, $transient_keys ) ) { | |
| 518 | - $transient_keys[] = $transient_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 ); | |
| 519 | 672 | } |
| 520 | - | |
| 521 | - // Save it to the DB | |
| 522 | - update_option( 'ayg_transient_keys', $transient_keys ); | |
| 523 | 673 | } |
| 524 | 674 | |
| 675 | + // Store videos in our custom database table "{$wpdb->prefix}ayg_videos" | |
| 676 | + ayg_db_store_videos( $data ); | |
| 677 | + | |
| 525 | 678 | // Finally return the data |
| 526 | 679 | return $data; |
| 527 | 680 | } |
| 528 | 681 | |
| @@ -534,10 +687,13 @@ | ||
| 534 | 687 | * @param object $data YouTube API response object. |
| 535 | 688 | * @return mixed |
| 536 | 689 | */ |
| 537 | 690 | private function parse_videos( $data ) { |
| 538 | - $items = $data->items; | |
| 691 | + if ( ! isset( $data->items ) ) { | |
| 692 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 693 | + } | |
| 539 | 694 | |
| 695 | + $items = $data->items; | |
| 540 | 696 | if ( ! is_array( $items ) || 0 == count( $items ) ) { |
| 541 | 697 | return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); |
| 542 | 698 | } |
| 543 | 699 | |
| @@ -556,10 +712,17 @@ | ||
| 556 | 712 | } elseif ( isset( $item->id ) && isset( $item->id->videoId ) ) { |
| 557 | 713 | $video->id = $item->id->videoId; |
| 558 | 714 | } elseif ( isset( $item->id ) ) { |
| 559 | 715 | $video->id = $item->id; |
| 560 | - } | |
| 716 | + } | |
| 561 | 717 | |
| 718 | + // Video channel ID | |
| 719 | + $video->channel_id = ''; | |
| 720 | + | |
| 721 | + if ( isset( $item->snippet->channelId ) ) { | |
| 722 | + $video->channel_id = $item->snippet->channelId; | |
| 723 | + } | |
| 724 | + | |
| 562 | 725 | // Video title |
| 563 | 726 | $video->title = $item->snippet->title; |
| 564 | 727 | |
| 565 | 728 | // Video description |
| @@ -573,11 +736,29 @@ | ||
| 573 | 736 | // Video publish date |
| 574 | 737 | $video->published_at = $item->snippet->publishedAt; |
| 575 | 738 | |
| 576 | 739 | // 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 ) { | |
| 740 | + $status = 'private'; | |
| 741 | + | |
| 742 | + if ( isset( $item->status ) && ( 'public' == $item->status->privacyStatus || 'unlisted' == $item->status->privacyStatus ) ) { | |
| 743 | + $status = 'public'; | |
| 744 | + } | |
| 745 | + | |
| 746 | + if ( isset( $item->snippet->status ) && ( 'public' == $item->snippet->status->privacyStatus || 'unlisted' == $item->snippet->status->privacyStatus ) ) { | |
| 747 | + $status = 'public'; | |
| 748 | + } | |
| 749 | + | |
| 750 | + if ( 'youtube#searchResult' == $item->kind ) { | |
| 751 | + $status = 'public'; | |
| 752 | + } | |
| 753 | + | |
| 754 | + if ( 'public' == $status ) { | |
| 578 | 755 | $videos[] = $video; |
| 579 | 756 | } |
| 757 | + } | |
| 758 | + | |
| 759 | + if ( 0 == count( $videos ) ) { | |
| 760 | + return $this->get_error( __( 'No videos found matching your query.', 'automatic-youtube-gallery' ) ); | |
| 580 | 761 | } |
| 581 | 762 | |
| 582 | 763 | return $videos; |
| 583 | 764 | } |