@@ -11,70 +11,70 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Youtube_Client extends Singleton implements Client { |
| 13 | 13 | |
| 14 | - const YOUTUBE_REGEX = '/(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.|m\.)?youtube\.com\/(?:watch|v|embed)(?:\.php)?(?:\?.*v=|\/))([a-zA-Z0-9\_-]+)/m'; |
|
| 14 | + const YOUTUBE_REGEX = '/(?:https?:\/\/)?(?:youtu\.be\/|(?:www\.|m\.)?youtube\.com\/(?:watch|v|embed)(?:\.php)?(?:\?.*v=|\/))([a-zA-Z0-9\_-]+)/m'; |
|
| 15 | 15 | |
| 16 | - static $requests_sent = 0; |
|
| 16 | + static $requests_sent = 0; |
|
| 17 | 17 | |
| 18 | - public static function get_api_key_option_name() { |
|
| 19 | - return "__wl_video_object_youtube_api_key"; |
|
| 20 | - } |
|
| 18 | + public static function get_api_key_option_name() { |
|
| 19 | + return "__wl_video_object_youtube_api_key"; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public static function get_api_key() { |
|
| 23 | - return get_option( self::get_api_key_option_name(), false ); |
|
| 24 | - } |
|
| 22 | + public static function get_api_key() { |
|
| 23 | + return get_option( self::get_api_key_option_name(), false ); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function get_api_url() { |
|
| 27 | - return 'https://www.googleapis.com/youtube/v3/videos'; |
|
| 28 | - } |
|
| 26 | + public function get_api_url() { |
|
| 27 | + return 'https://www.googleapis.com/youtube/v3/videos'; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function get_data( $video_urls ) { |
|
| 32 | - $video_ids = $this->get_video_ids_as_string( $video_urls ); |
|
| 33 | - $url = add_query_arg( array( |
|
| 34 | - 'part' => 'snippet,contentDetails,statistics,liveStreamingDetails', |
|
| 35 | - 'id' => $video_ids, |
|
| 36 | - 'key' => $this->get_api_key() |
|
| 37 | - ), $this->get_api_url() ); |
|
| 31 | + public function get_data( $video_urls ) { |
|
| 32 | + $video_ids = $this->get_video_ids_as_string( $video_urls ); |
|
| 33 | + $url = add_query_arg( array( |
|
| 34 | + 'part' => 'snippet,contentDetails,statistics,liveStreamingDetails', |
|
| 35 | + 'id' => $video_ids, |
|
| 36 | + 'key' => $this->get_api_key() |
|
| 37 | + ), $this->get_api_url() ); |
|
| 38 | 38 | |
| 39 | - $response = wp_remote_get( $url ); |
|
| 39 | + $response = wp_remote_get( $url ); |
|
| 40 | 40 | |
| 41 | - self::$requests_sent += 1; |
|
| 41 | + self::$requests_sent += 1; |
|
| 42 | 42 | |
| 43 | - return wp_remote_retrieve_body( $response ); |
|
| 44 | - } |
|
| 43 | + return wp_remote_retrieve_body( $response ); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | |
| 47 | - private function get_video_ids_as_string( $video_urls ) { |
|
| 48 | - // validate the urls. |
|
| 49 | - $video_urls = array_filter( $video_urls, function ( $url ) { |
|
| 50 | - return filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 51 | - } ); |
|
| 47 | + private function get_video_ids_as_string( $video_urls ) { |
|
| 48 | + // validate the urls. |
|
| 49 | + $video_urls = array_filter( $video_urls, function ( $url ) { |
|
| 50 | + return filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 51 | + } ); |
|
| 52 | 52 | |
| 53 | - // extract the video ids. |
|
| 54 | - return join( ",", self::get_video_ids( $video_urls ) ); |
|
| 55 | - } |
|
| 53 | + // extract the video ids. |
|
| 54 | + return join( ",", self::get_video_ids( $video_urls ) ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - public static function get_video_ids( $video_urls ) { |
|
| 58 | + public static function get_video_ids( $video_urls ) { |
|
| 59 | 59 | |
| 60 | - $regex = self::YOUTUBE_REGEX; |
|
| 60 | + $regex = self::YOUTUBE_REGEX; |
|
| 61 | 61 | |
| 62 | - $video_ids = array_map( function ( $url ) use ( $regex ) { |
|
| 63 | - $matches = array(); |
|
| 64 | - preg_match( $regex, $url, $matches ); |
|
| 62 | + $video_ids = array_map( function ( $url ) use ( $regex ) { |
|
| 63 | + $matches = array(); |
|
| 64 | + preg_match( $regex, $url, $matches ); |
|
| 65 | 65 | |
| 66 | - // Return video id or return false. |
|
| 67 | - if ( isset( $matches[1] ) && is_string( $matches[1] ) ) { |
|
| 68 | - return $matches[1]; |
|
| 69 | - } |
|
| 66 | + // Return video id or return false. |
|
| 67 | + if ( isset( $matches[1] ) && is_string( $matches[1] ) ) { |
|
| 68 | + return $matches[1]; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return false; |
|
| 71 | + return false; |
|
| 72 | 72 | |
| 73 | - }, $video_urls ); |
|
| 73 | + }, $video_urls ); |
|
| 74 | 74 | |
| 75 | - return array_values( array_filter( $video_ids ) ); |
|
| 75 | + return array_values( array_filter( $video_ids ) ); |
|
| 76 | 76 | |
| 77 | - } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | 80 | } |
| 81 | 81 | \ No newline at end of file |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public static function get_api_key() { |
| 23 | - return get_option( self::get_api_key_option_name(), false ); |
|
| 23 | + return get_option(self::get_api_key_option_name(), false); |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | public function get_api_url() { |
@@ -28,51 +28,51 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function get_data( $video_urls ) { |
|
| 32 | - $video_ids = $this->get_video_ids_as_string( $video_urls ); |
|
| 33 | - $url = add_query_arg( array( |
|
| 31 | + public function get_data($video_urls) { |
|
| 32 | + $video_ids = $this->get_video_ids_as_string($video_urls); |
|
| 33 | + $url = add_query_arg(array( |
|
| 34 | 34 | 'part' => 'snippet,contentDetails,statistics,liveStreamingDetails', |
| 35 | 35 | 'id' => $video_ids, |
| 36 | 36 | 'key' => $this->get_api_key() |
| 37 | - ), $this->get_api_url() ); |
|
| 37 | + ), $this->get_api_url()); |
|
| 38 | 38 | |
| 39 | - $response = wp_remote_get( $url ); |
|
| 39 | + $response = wp_remote_get($url); |
|
| 40 | 40 | |
| 41 | 41 | self::$requests_sent += 1; |
| 42 | 42 | |
| 43 | - return wp_remote_retrieve_body( $response ); |
|
| 43 | + return wp_remote_retrieve_body($response); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | |
| 47 | - private function get_video_ids_as_string( $video_urls ) { |
|
| 47 | + private function get_video_ids_as_string($video_urls) { |
|
| 48 | 48 | // validate the urls. |
| 49 | - $video_urls = array_filter( $video_urls, function ( $url ) { |
|
| 50 | - return filter_var( $url, FILTER_VALIDATE_URL ); |
|
| 49 | + $video_urls = array_filter($video_urls, function($url) { |
|
| 50 | + return filter_var($url, FILTER_VALIDATE_URL); |
|
| 51 | 51 | } ); |
| 52 | 52 | |
| 53 | 53 | // extract the video ids. |
| 54 | - return join( ",", self::get_video_ids( $video_urls ) ); |
|
| 54 | + return join(",", self::get_video_ids($video_urls)); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | |
| 58 | - public static function get_video_ids( $video_urls ) { |
|
| 58 | + public static function get_video_ids($video_urls) { |
|
| 59 | 59 | |
| 60 | 60 | $regex = self::YOUTUBE_REGEX; |
| 61 | 61 | |
| 62 | - $video_ids = array_map( function ( $url ) use ( $regex ) { |
|
| 62 | + $video_ids = array_map(function($url) use ($regex) { |
|
| 63 | 63 | $matches = array(); |
| 64 | - preg_match( $regex, $url, $matches ); |
|
| 64 | + preg_match($regex, $url, $matches); |
|
| 65 | 65 | |
| 66 | 66 | // Return video id or return false. |
| 67 | - if ( isset( $matches[1] ) && is_string( $matches[1] ) ) { |
|
| 67 | + if (isset($matches[1]) && is_string($matches[1])) { |
|
| 68 | 68 | return $matches[1]; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return false; |
| 72 | 72 | |
| 73 | - }, $video_urls ); |
|
| 73 | + }, $video_urls); |
|
| 74 | 74 | |
| 75 | - return array_values( array_filter( $video_ids ) ); |
|
| 75 | + return array_values(array_filter($video_ids)); |
|
| 76 | 76 | |
| 77 | 77 | } |
| 78 | 78 | |