@@ -14,211 +14,211 @@ |
||
| 14 | 14 | |
| 15 | 15 | class Video_Processor { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var array<Client> |
|
| 19 | - */ |
|
| 20 | - private $api_clients; |
|
| 21 | - |
|
| 22 | - public function __construct() { |
|
| 23 | - $this->api_clients = array( |
|
| 24 | - Client_Factory::get_client( Client_Factory::YOUTUBE ), |
|
| 25 | - Client_Factory::get_client( Client_Factory::VIMEO ) |
|
| 26 | - ); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - private function get_data_for_videos( $embedded_videos ) { |
|
| 30 | - |
|
| 31 | - $youtube_videos = $this->get_youtube_videos( $embedded_videos ); |
|
| 32 | - |
|
| 33 | - $youtube_provider = Provider_Factory::get_provider( Provider_Factory::YOUTUBE ); |
|
| 34 | - $youtube_videos = $youtube_provider->get_videos_data( $youtube_videos ); |
|
| 35 | - |
|
| 36 | - $vimeo_videos = $this->get_vimeo_videos( $embedded_videos ); |
|
| 37 | - |
|
| 38 | - $vimeo_provider = Provider_Factory::get_provider( Provider_Factory::VIMEO ); |
|
| 39 | - $vimeo_videos = $vimeo_provider->get_videos_data( $vimeo_videos ); |
|
| 40 | - |
|
| 41 | - return array_merge( $youtube_videos, $vimeo_videos ); |
|
| 42 | - |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - private function get_video_ids( $video_urls ) { |
|
| 47 | - $clients = $this->api_clients; |
|
| 48 | - $video_ids = array(); |
|
| 49 | - foreach ( $clients as $client ) { |
|
| 50 | - $ids = $client->get_video_ids( $video_urls ); |
|
| 51 | - if ( $ids ) { |
|
| 52 | - $video_ids = array_merge( $video_ids, $ids ); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return array_unique( $video_ids ); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param $storage Storage |
|
| 62 | - * @param $post_id int |
|
| 63 | - * @param $embedded_videos array<Embedded_Video> |
|
| 64 | - */ |
|
| 65 | - private function remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ) { |
|
| 66 | - |
|
| 67 | - $videos_to_be_deleted = $this->get_videos_to_be_deleted( $storage, $post_id, $embedded_videos ); |
|
| 68 | - $storage->remove_videos( $videos_to_be_deleted, $post_id ); |
|
| 69 | - |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param Storage $storage |
|
| 74 | - * @param $post_id |
|
| 75 | - * @param array $embedded_videos |
|
| 76 | - * |
|
| 77 | - * @return array An array of videos which exist on storage, not on post content. |
|
| 78 | - */ |
|
| 79 | - private function get_videos_to_be_deleted( Storage $storage, $post_id, array $embedded_videos ) { |
|
| 80 | - $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 81 | - $embedded_video_urls = array_map( function ( $embedded_video ) { |
|
| 82 | - /** |
|
| 83 | - * @var $embedded_video Embedded_Video |
|
| 84 | - */ |
|
| 85 | - return $embedded_video->get_url(); |
|
| 86 | - }, $embedded_videos ); |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Previously we are checking if the captured url is content_url to delete it from the storage |
|
| 90 | - * but this might not work well if we want to support multiple URL formats, we extract the video |
|
| 91 | - * ids for embedded URLs. |
|
| 92 | - */ |
|
| 93 | - $embedded_video_ids = $this->get_video_ids( $embedded_video_urls ); |
|
| 94 | - |
|
| 95 | - $that = $this; |
|
| 96 | - |
|
| 97 | - return array_filter( $videos_in_store, function ( $video ) use ( $embedded_video_ids, $embedded_video_urls, $that ) { |
|
| 98 | - /** |
|
| 99 | - * If the video id doesnt exist on the content then we need to return it |
|
| 100 | - * in order to delete that video. |
|
| 101 | - */ |
|
| 102 | - return count( array_intersect( |
|
| 103 | - $that->get_video_ids( array( $video->id ) ), |
|
| 104 | - $embedded_video_ids |
|
| 105 | - ) ) === 0; |
|
| 106 | - |
|
| 107 | - } ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param \WP_Post $post |
|
| 112 | - * @param $post_id |
|
| 113 | - */ |
|
| 114 | - public function process_video_urls( \WP_Post $post, $post_id ) { |
|
| 115 | - |
|
| 116 | - $parser = Parser_Factory::get_parser_from_content( $post->post_content ); |
|
| 117 | - |
|
| 118 | - $embedded_videos = $parser->get_videos( $post_id ); |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Filters the embedded videos on post contet, custom plugins can add their video urls |
|
| 122 | - * by constructing \Default_Embedded_Video or implement Embedded_Video class |
|
| 123 | - * |
|
| 124 | - * @param $embedded_videos array<Embedded_Video> |
|
| 125 | - * @param $post_id int The post id for the videoobject is processed. |
|
| 126 | - * |
|
| 127 | - * @return array<Embedded_Video> |
|
| 128 | - * @since 3.31.4 |
|
| 129 | - * Filter name : wl_videoobject_embedded_videos |
|
| 130 | - */ |
|
| 131 | - $embedded_videos = apply_filters( 'wl_videoobject_embedded_videos', $embedded_videos, $post_id ); |
|
| 132 | - |
|
| 133 | - $storage = Video_Storage_Factory::get_storage(); |
|
| 134 | - |
|
| 135 | - // Before sending api requests we need to check if there are any videos in |
|
| 136 | - // store which is not present on post content, remove them if there are |
|
| 137 | - // any |
|
| 138 | - $this->remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ); |
|
| 139 | - |
|
| 140 | - $embedded_videos = $this->get_videos_without_existing_data( $storage, $post_id, $embedded_videos ); |
|
| 141 | - |
|
| 142 | - // Return early after removing all the videos. |
|
| 143 | - if ( ! $embedded_videos ) { |
|
| 144 | - return; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $videos = $this->get_data_for_videos( $embedded_videos ); |
|
| 148 | - |
|
| 149 | - if ( ! $videos ) { |
|
| 150 | - return; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - foreach ( $videos as $video ) { |
|
| 154 | - $storage->add_video( $post_id, $video ); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @param $storage Storage |
|
| 160 | - * @param $post_id int |
|
| 161 | - * @param $embedded_videos array<Embedded_Video> |
|
| 162 | - * |
|
| 163 | - * @return array<Embedded_Video> Return array of embedded videos which are not in store. |
|
| 164 | - */ |
|
| 165 | - private function get_videos_without_existing_data( $storage, $post_id, $embedded_videos ) { |
|
| 166 | - $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 167 | - $video_urls_in_store = array_map( function ( $video ) { |
|
| 168 | - return $video->id; |
|
| 169 | - }, $videos_in_store ); |
|
| 170 | - |
|
| 171 | - $video_ids_in_store = $this->get_video_ids( $video_urls_in_store ); |
|
| 172 | - |
|
| 173 | - $that = $this; |
|
| 174 | - |
|
| 175 | - return array_filter( $embedded_videos, function ( $embedded_video ) use ( $video_ids_in_store, $that ) { |
|
| 176 | - /** |
|
| 177 | - * If the video id exist on content, not on storage then |
|
| 178 | - * we need to fetch the data. |
|
| 179 | - */ |
|
| 180 | - return count( array_intersect( |
|
| 181 | - $that->get_video_ids( array( $embedded_video->get_url() ) ), |
|
| 182 | - $video_ids_in_store |
|
| 183 | - ) ) === 0; |
|
| 184 | - } ); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @param $embedded_videos |
|
| 189 | - * |
|
| 190 | - * @return array |
|
| 191 | - */ |
|
| 192 | - private function get_youtube_videos( $embedded_videos ) { |
|
| 193 | - return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 194 | - /** |
|
| 195 | - * it should have youtube.com or youtu.be in the url |
|
| 196 | - * |
|
| 197 | - * @param $embedded_video Embedded_Video |
|
| 198 | - */ |
|
| 199 | - $video_url = $embedded_video->get_url(); |
|
| 200 | - |
|
| 201 | - return strpos( $video_url, "youtube.com" ) !== false || |
|
| 202 | - strpos( $video_url, "youtu.be" ) !== false; |
|
| 203 | - } ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param $embedded_videos |
|
| 208 | - * |
|
| 209 | - * @return array |
|
| 210 | - */ |
|
| 211 | - private function get_vimeo_videos( $embedded_videos ) { |
|
| 212 | - return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 213 | - /** |
|
| 214 | - * it should have vimeo.com in the url |
|
| 215 | - * |
|
| 216 | - * @param $embedded_video Embedded_Video |
|
| 217 | - */ |
|
| 218 | - $video_url = $embedded_video->get_url(); |
|
| 219 | - |
|
| 220 | - return strpos( $video_url, "vimeo.com" ) !== false; |
|
| 221 | - } ); |
|
| 222 | - } |
|
| 17 | + /** |
|
| 18 | + * @var array<Client> |
|
| 19 | + */ |
|
| 20 | + private $api_clients; |
|
| 21 | + |
|
| 22 | + public function __construct() { |
|
| 23 | + $this->api_clients = array( |
|
| 24 | + Client_Factory::get_client( Client_Factory::YOUTUBE ), |
|
| 25 | + Client_Factory::get_client( Client_Factory::VIMEO ) |
|
| 26 | + ); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + private function get_data_for_videos( $embedded_videos ) { |
|
| 30 | + |
|
| 31 | + $youtube_videos = $this->get_youtube_videos( $embedded_videos ); |
|
| 32 | + |
|
| 33 | + $youtube_provider = Provider_Factory::get_provider( Provider_Factory::YOUTUBE ); |
|
| 34 | + $youtube_videos = $youtube_provider->get_videos_data( $youtube_videos ); |
|
| 35 | + |
|
| 36 | + $vimeo_videos = $this->get_vimeo_videos( $embedded_videos ); |
|
| 37 | + |
|
| 38 | + $vimeo_provider = Provider_Factory::get_provider( Provider_Factory::VIMEO ); |
|
| 39 | + $vimeo_videos = $vimeo_provider->get_videos_data( $vimeo_videos ); |
|
| 40 | + |
|
| 41 | + return array_merge( $youtube_videos, $vimeo_videos ); |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + private function get_video_ids( $video_urls ) { |
|
| 47 | + $clients = $this->api_clients; |
|
| 48 | + $video_ids = array(); |
|
| 49 | + foreach ( $clients as $client ) { |
|
| 50 | + $ids = $client->get_video_ids( $video_urls ); |
|
| 51 | + if ( $ids ) { |
|
| 52 | + $video_ids = array_merge( $video_ids, $ids ); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return array_unique( $video_ids ); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param $storage Storage |
|
| 62 | + * @param $post_id int |
|
| 63 | + * @param $embedded_videos array<Embedded_Video> |
|
| 64 | + */ |
|
| 65 | + private function remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ) { |
|
| 66 | + |
|
| 67 | + $videos_to_be_deleted = $this->get_videos_to_be_deleted( $storage, $post_id, $embedded_videos ); |
|
| 68 | + $storage->remove_videos( $videos_to_be_deleted, $post_id ); |
|
| 69 | + |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param Storage $storage |
|
| 74 | + * @param $post_id |
|
| 75 | + * @param array $embedded_videos |
|
| 76 | + * |
|
| 77 | + * @return array An array of videos which exist on storage, not on post content. |
|
| 78 | + */ |
|
| 79 | + private function get_videos_to_be_deleted( Storage $storage, $post_id, array $embedded_videos ) { |
|
| 80 | + $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 81 | + $embedded_video_urls = array_map( function ( $embedded_video ) { |
|
| 82 | + /** |
|
| 83 | + * @var $embedded_video Embedded_Video |
|
| 84 | + */ |
|
| 85 | + return $embedded_video->get_url(); |
|
| 86 | + }, $embedded_videos ); |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Previously we are checking if the captured url is content_url to delete it from the storage |
|
| 90 | + * but this might not work well if we want to support multiple URL formats, we extract the video |
|
| 91 | + * ids for embedded URLs. |
|
| 92 | + */ |
|
| 93 | + $embedded_video_ids = $this->get_video_ids( $embedded_video_urls ); |
|
| 94 | + |
|
| 95 | + $that = $this; |
|
| 96 | + |
|
| 97 | + return array_filter( $videos_in_store, function ( $video ) use ( $embedded_video_ids, $embedded_video_urls, $that ) { |
|
| 98 | + /** |
|
| 99 | + * If the video id doesnt exist on the content then we need to return it |
|
| 100 | + * in order to delete that video. |
|
| 101 | + */ |
|
| 102 | + return count( array_intersect( |
|
| 103 | + $that->get_video_ids( array( $video->id ) ), |
|
| 104 | + $embedded_video_ids |
|
| 105 | + ) ) === 0; |
|
| 106 | + |
|
| 107 | + } ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param \WP_Post $post |
|
| 112 | + * @param $post_id |
|
| 113 | + */ |
|
| 114 | + public function process_video_urls( \WP_Post $post, $post_id ) { |
|
| 115 | + |
|
| 116 | + $parser = Parser_Factory::get_parser_from_content( $post->post_content ); |
|
| 117 | + |
|
| 118 | + $embedded_videos = $parser->get_videos( $post_id ); |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Filters the embedded videos on post contet, custom plugins can add their video urls |
|
| 122 | + * by constructing \Default_Embedded_Video or implement Embedded_Video class |
|
| 123 | + * |
|
| 124 | + * @param $embedded_videos array<Embedded_Video> |
|
| 125 | + * @param $post_id int The post id for the videoobject is processed. |
|
| 126 | + * |
|
| 127 | + * @return array<Embedded_Video> |
|
| 128 | + * @since 3.31.4 |
|
| 129 | + * Filter name : wl_videoobject_embedded_videos |
|
| 130 | + */ |
|
| 131 | + $embedded_videos = apply_filters( 'wl_videoobject_embedded_videos', $embedded_videos, $post_id ); |
|
| 132 | + |
|
| 133 | + $storage = Video_Storage_Factory::get_storage(); |
|
| 134 | + |
|
| 135 | + // Before sending api requests we need to check if there are any videos in |
|
| 136 | + // store which is not present on post content, remove them if there are |
|
| 137 | + // any |
|
| 138 | + $this->remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ); |
|
| 139 | + |
|
| 140 | + $embedded_videos = $this->get_videos_without_existing_data( $storage, $post_id, $embedded_videos ); |
|
| 141 | + |
|
| 142 | + // Return early after removing all the videos. |
|
| 143 | + if ( ! $embedded_videos ) { |
|
| 144 | + return; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $videos = $this->get_data_for_videos( $embedded_videos ); |
|
| 148 | + |
|
| 149 | + if ( ! $videos ) { |
|
| 150 | + return; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + foreach ( $videos as $video ) { |
|
| 154 | + $storage->add_video( $post_id, $video ); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @param $storage Storage |
|
| 160 | + * @param $post_id int |
|
| 161 | + * @param $embedded_videos array<Embedded_Video> |
|
| 162 | + * |
|
| 163 | + * @return array<Embedded_Video> Return array of embedded videos which are not in store. |
|
| 164 | + */ |
|
| 165 | + private function get_videos_without_existing_data( $storage, $post_id, $embedded_videos ) { |
|
| 166 | + $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 167 | + $video_urls_in_store = array_map( function ( $video ) { |
|
| 168 | + return $video->id; |
|
| 169 | + }, $videos_in_store ); |
|
| 170 | + |
|
| 171 | + $video_ids_in_store = $this->get_video_ids( $video_urls_in_store ); |
|
| 172 | + |
|
| 173 | + $that = $this; |
|
| 174 | + |
|
| 175 | + return array_filter( $embedded_videos, function ( $embedded_video ) use ( $video_ids_in_store, $that ) { |
|
| 176 | + /** |
|
| 177 | + * If the video id exist on content, not on storage then |
|
| 178 | + * we need to fetch the data. |
|
| 179 | + */ |
|
| 180 | + return count( array_intersect( |
|
| 181 | + $that->get_video_ids( array( $embedded_video->get_url() ) ), |
|
| 182 | + $video_ids_in_store |
|
| 183 | + ) ) === 0; |
|
| 184 | + } ); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @param $embedded_videos |
|
| 189 | + * |
|
| 190 | + * @return array |
|
| 191 | + */ |
|
| 192 | + private function get_youtube_videos( $embedded_videos ) { |
|
| 193 | + return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 194 | + /** |
|
| 195 | + * it should have youtube.com or youtu.be in the url |
|
| 196 | + * |
|
| 197 | + * @param $embedded_video Embedded_Video |
|
| 198 | + */ |
|
| 199 | + $video_url = $embedded_video->get_url(); |
|
| 200 | + |
|
| 201 | + return strpos( $video_url, "youtube.com" ) !== false || |
|
| 202 | + strpos( $video_url, "youtu.be" ) !== false; |
|
| 203 | + } ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param $embedded_videos |
|
| 208 | + * |
|
| 209 | + * @return array |
|
| 210 | + */ |
|
| 211 | + private function get_vimeo_videos( $embedded_videos ) { |
|
| 212 | + return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 213 | + /** |
|
| 214 | + * it should have vimeo.com in the url |
|
| 215 | + * |
|
| 216 | + * @param $embedded_video Embedded_Video |
|
| 217 | + */ |
|
| 218 | + $video_url = $embedded_video->get_url(); |
|
| 219 | + |
|
| 220 | + return strpos( $video_url, "vimeo.com" ) !== false; |
|
| 221 | + } ); |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | 224 | } |
| 225 | 225 | \ No newline at end of file |
@@ -21,39 +21,39 @@ discard block |
||
| 21 | 21 | |
| 22 | 22 | public function __construct() { |
| 23 | 23 | $this->api_clients = array( |
| 24 | - Client_Factory::get_client( Client_Factory::YOUTUBE ), |
|
| 25 | - Client_Factory::get_client( Client_Factory::VIMEO ) |
|
| 24 | + Client_Factory::get_client(Client_Factory::YOUTUBE), |
|
| 25 | + Client_Factory::get_client(Client_Factory::VIMEO) |
|
| 26 | 26 | ); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - private function get_data_for_videos( $embedded_videos ) { |
|
| 29 | + private function get_data_for_videos($embedded_videos) { |
|
| 30 | 30 | |
| 31 | - $youtube_videos = $this->get_youtube_videos( $embedded_videos ); |
|
| 31 | + $youtube_videos = $this->get_youtube_videos($embedded_videos); |
|
| 32 | 32 | |
| 33 | - $youtube_provider = Provider_Factory::get_provider( Provider_Factory::YOUTUBE ); |
|
| 34 | - $youtube_videos = $youtube_provider->get_videos_data( $youtube_videos ); |
|
| 33 | + $youtube_provider = Provider_Factory::get_provider(Provider_Factory::YOUTUBE); |
|
| 34 | + $youtube_videos = $youtube_provider->get_videos_data($youtube_videos); |
|
| 35 | 35 | |
| 36 | - $vimeo_videos = $this->get_vimeo_videos( $embedded_videos ); |
|
| 36 | + $vimeo_videos = $this->get_vimeo_videos($embedded_videos); |
|
| 37 | 37 | |
| 38 | - $vimeo_provider = Provider_Factory::get_provider( Provider_Factory::VIMEO ); |
|
| 39 | - $vimeo_videos = $vimeo_provider->get_videos_data( $vimeo_videos ); |
|
| 38 | + $vimeo_provider = Provider_Factory::get_provider(Provider_Factory::VIMEO); |
|
| 39 | + $vimeo_videos = $vimeo_provider->get_videos_data($vimeo_videos); |
|
| 40 | 40 | |
| 41 | - return array_merge( $youtube_videos, $vimeo_videos ); |
|
| 41 | + return array_merge($youtube_videos, $vimeo_videos); |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | |
| 46 | - private function get_video_ids( $video_urls ) { |
|
| 46 | + private function get_video_ids($video_urls) { |
|
| 47 | 47 | $clients = $this->api_clients; |
| 48 | 48 | $video_ids = array(); |
| 49 | - foreach ( $clients as $client ) { |
|
| 50 | - $ids = $client->get_video_ids( $video_urls ); |
|
| 51 | - if ( $ids ) { |
|
| 52 | - $video_ids = array_merge( $video_ids, $ids ); |
|
| 49 | + foreach ($clients as $client) { |
|
| 50 | + $ids = $client->get_video_ids($video_urls); |
|
| 51 | + if ($ids) { |
|
| 52 | + $video_ids = array_merge($video_ids, $ids); |
|
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - return array_unique( $video_ids ); |
|
| 56 | + return array_unique($video_ids); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | |
@@ -62,10 +62,10 @@ discard block |
||
| 62 | 62 | * @param $post_id int |
| 63 | 63 | * @param $embedded_videos array<Embedded_Video> |
| 64 | 64 | */ |
| 65 | - private function remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ) { |
|
| 65 | + private function remove_videos_from_store_if_not_present_in_content($storage, $post_id, $embedded_videos) { |
|
| 66 | 66 | |
| 67 | - $videos_to_be_deleted = $this->get_videos_to_be_deleted( $storage, $post_id, $embedded_videos ); |
|
| 68 | - $storage->remove_videos( $videos_to_be_deleted, $post_id ); |
|
| 67 | + $videos_to_be_deleted = $this->get_videos_to_be_deleted($storage, $post_id, $embedded_videos); |
|
| 68 | + $storage->remove_videos($videos_to_be_deleted, $post_id); |
|
| 69 | 69 | |
| 70 | 70 | } |
| 71 | 71 | |
@@ -76,33 +76,33 @@ discard block |
||
| 76 | 76 | * |
| 77 | 77 | * @return array An array of videos which exist on storage, not on post content. |
| 78 | 78 | */ |
| 79 | - private function get_videos_to_be_deleted( Storage $storage, $post_id, array $embedded_videos ) { |
|
| 80 | - $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 81 | - $embedded_video_urls = array_map( function ( $embedded_video ) { |
|
| 79 | + private function get_videos_to_be_deleted(Storage $storage, $post_id, array $embedded_videos) { |
|
| 80 | + $videos_in_store = $storage->get_all_videos($post_id); |
|
| 81 | + $embedded_video_urls = array_map(function($embedded_video) { |
|
| 82 | 82 | /** |
| 83 | 83 | * @var $embedded_video Embedded_Video |
| 84 | 84 | */ |
| 85 | 85 | return $embedded_video->get_url(); |
| 86 | - }, $embedded_videos ); |
|
| 86 | + }, $embedded_videos); |
|
| 87 | 87 | |
| 88 | 88 | /** |
| 89 | 89 | * Previously we are checking if the captured url is content_url to delete it from the storage |
| 90 | 90 | * but this might not work well if we want to support multiple URL formats, we extract the video |
| 91 | 91 | * ids for embedded URLs. |
| 92 | 92 | */ |
| 93 | - $embedded_video_ids = $this->get_video_ids( $embedded_video_urls ); |
|
| 93 | + $embedded_video_ids = $this->get_video_ids($embedded_video_urls); |
|
| 94 | 94 | |
| 95 | 95 | $that = $this; |
| 96 | 96 | |
| 97 | - return array_filter( $videos_in_store, function ( $video ) use ( $embedded_video_ids, $embedded_video_urls, $that ) { |
|
| 97 | + return array_filter($videos_in_store, function($video) use ($embedded_video_ids, $embedded_video_urls, $that) { |
|
| 98 | 98 | /** |
| 99 | 99 | * If the video id doesnt exist on the content then we need to return it |
| 100 | 100 | * in order to delete that video. |
| 101 | 101 | */ |
| 102 | - return count( array_intersect( |
|
| 103 | - $that->get_video_ids( array( $video->id ) ), |
|
| 102 | + return count(array_intersect( |
|
| 103 | + $that->get_video_ids(array($video->id)), |
|
| 104 | 104 | $embedded_video_ids |
| 105 | - ) ) === 0; |
|
| 105 | + )) === 0; |
|
| 106 | 106 | |
| 107 | 107 | } ); |
| 108 | 108 | } |
@@ -111,11 +111,11 @@ discard block |
||
| 111 | 111 | * @param \WP_Post $post |
| 112 | 112 | * @param $post_id |
| 113 | 113 | */ |
| 114 | - public function process_video_urls( \WP_Post $post, $post_id ) { |
|
| 114 | + public function process_video_urls(\WP_Post $post, $post_id) { |
|
| 115 | 115 | |
| 116 | - $parser = Parser_Factory::get_parser_from_content( $post->post_content ); |
|
| 116 | + $parser = Parser_Factory::get_parser_from_content($post->post_content); |
|
| 117 | 117 | |
| 118 | - $embedded_videos = $parser->get_videos( $post_id ); |
|
| 118 | + $embedded_videos = $parser->get_videos($post_id); |
|
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * Filters the embedded videos on post contet, custom plugins can add their video urls |
@@ -128,30 +128,30 @@ discard block |
||
| 128 | 128 | * @since 3.31.4 |
| 129 | 129 | * Filter name : wl_videoobject_embedded_videos |
| 130 | 130 | */ |
| 131 | - $embedded_videos = apply_filters( 'wl_videoobject_embedded_videos', $embedded_videos, $post_id ); |
|
| 131 | + $embedded_videos = apply_filters('wl_videoobject_embedded_videos', $embedded_videos, $post_id); |
|
| 132 | 132 | |
| 133 | 133 | $storage = Video_Storage_Factory::get_storage(); |
| 134 | 134 | |
| 135 | 135 | // Before sending api requests we need to check if there are any videos in |
| 136 | 136 | // store which is not present on post content, remove them if there are |
| 137 | 137 | // any |
| 138 | - $this->remove_videos_from_store_if_not_present_in_content( $storage, $post_id, $embedded_videos ); |
|
| 138 | + $this->remove_videos_from_store_if_not_present_in_content($storage, $post_id, $embedded_videos); |
|
| 139 | 139 | |
| 140 | - $embedded_videos = $this->get_videos_without_existing_data( $storage, $post_id, $embedded_videos ); |
|
| 140 | + $embedded_videos = $this->get_videos_without_existing_data($storage, $post_id, $embedded_videos); |
|
| 141 | 141 | |
| 142 | 142 | // Return early after removing all the videos. |
| 143 | - if ( ! $embedded_videos ) { |
|
| 143 | + if ( ! $embedded_videos) { |
|
| 144 | 144 | return; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - $videos = $this->get_data_for_videos( $embedded_videos ); |
|
| 147 | + $videos = $this->get_data_for_videos($embedded_videos); |
|
| 148 | 148 | |
| 149 | - if ( ! $videos ) { |
|
| 149 | + if ( ! $videos) { |
|
| 150 | 150 | return; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - foreach ( $videos as $video ) { |
|
| 154 | - $storage->add_video( $post_id, $video ); |
|
| 153 | + foreach ($videos as $video) { |
|
| 154 | + $storage->add_video($post_id, $video); |
|
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
@@ -162,25 +162,25 @@ discard block |
||
| 162 | 162 | * |
| 163 | 163 | * @return array<Embedded_Video> Return array of embedded videos which are not in store. |
| 164 | 164 | */ |
| 165 | - private function get_videos_without_existing_data( $storage, $post_id, $embedded_videos ) { |
|
| 166 | - $videos_in_store = $storage->get_all_videos( $post_id ); |
|
| 167 | - $video_urls_in_store = array_map( function ( $video ) { |
|
| 165 | + private function get_videos_without_existing_data($storage, $post_id, $embedded_videos) { |
|
| 166 | + $videos_in_store = $storage->get_all_videos($post_id); |
|
| 167 | + $video_urls_in_store = array_map(function($video) { |
|
| 168 | 168 | return $video->id; |
| 169 | - }, $videos_in_store ); |
|
| 169 | + }, $videos_in_store); |
|
| 170 | 170 | |
| 171 | - $video_ids_in_store = $this->get_video_ids( $video_urls_in_store ); |
|
| 171 | + $video_ids_in_store = $this->get_video_ids($video_urls_in_store); |
|
| 172 | 172 | |
| 173 | 173 | $that = $this; |
| 174 | 174 | |
| 175 | - return array_filter( $embedded_videos, function ( $embedded_video ) use ( $video_ids_in_store, $that ) { |
|
| 175 | + return array_filter($embedded_videos, function($embedded_video) use ($video_ids_in_store, $that) { |
|
| 176 | 176 | /** |
| 177 | 177 | * If the video id exist on content, not on storage then |
| 178 | 178 | * we need to fetch the data. |
| 179 | 179 | */ |
| 180 | - return count( array_intersect( |
|
| 181 | - $that->get_video_ids( array( $embedded_video->get_url() ) ), |
|
| 180 | + return count(array_intersect( |
|
| 181 | + $that->get_video_ids(array($embedded_video->get_url())), |
|
| 182 | 182 | $video_ids_in_store |
| 183 | - ) ) === 0; |
|
| 183 | + )) === 0; |
|
| 184 | 184 | } ); |
| 185 | 185 | } |
| 186 | 186 | |
@@ -189,8 +189,8 @@ discard block |
||
| 189 | 189 | * |
| 190 | 190 | * @return array |
| 191 | 191 | */ |
| 192 | - private function get_youtube_videos( $embedded_videos ) { |
|
| 193 | - return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 192 | + private function get_youtube_videos($embedded_videos) { |
|
| 193 | + return array_filter($embedded_videos, function($embedded_video) { |
|
| 194 | 194 | /** |
| 195 | 195 | * it should have youtube.com or youtu.be in the url |
| 196 | 196 | * |
@@ -198,8 +198,8 @@ discard block |
||
| 198 | 198 | */ |
| 199 | 199 | $video_url = $embedded_video->get_url(); |
| 200 | 200 | |
| 201 | - return strpos( $video_url, "youtube.com" ) !== false || |
|
| 202 | - strpos( $video_url, "youtu.be" ) !== false; |
|
| 201 | + return strpos($video_url, "youtube.com") !== false || |
|
| 202 | + strpos($video_url, "youtu.be") !== false; |
|
| 203 | 203 | } ); |
| 204 | 204 | } |
| 205 | 205 | |
@@ -208,8 +208,8 @@ discard block |
||
| 208 | 208 | * |
| 209 | 209 | * @return array |
| 210 | 210 | */ |
| 211 | - private function get_vimeo_videos( $embedded_videos ) { |
|
| 212 | - return array_filter( $embedded_videos, function ( $embedded_video ) { |
|
| 211 | + private function get_vimeo_videos($embedded_videos) { |
|
| 212 | + return array_filter($embedded_videos, function($embedded_video) { |
|
| 213 | 213 | /** |
| 214 | 214 | * it should have vimeo.com in the url |
| 215 | 215 | * |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | */ |
| 218 | 218 | $video_url = $embedded_video->get_url(); |
| 219 | 219 | |
| 220 | - return strpos( $video_url, "vimeo.com" ) !== false; |
|
| 220 | + return strpos($video_url, "vimeo.com") !== false; |
|
| 221 | 221 | } ); |
| 222 | 222 | } |
| 223 | 223 | |
@@ -11,71 +11,71 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Vimeo_Client extends Singleton implements Client { |
| 13 | 13 | |
| 14 | - static $requests_sent = 0; |
|
| 14 | + static $requests_sent = 0; |
|
| 15 | 15 | |
| 16 | - const VIMEO_URL_REGEX = '/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/'; |
|
| 16 | + const VIMEO_URL_REGEX = '/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/'; |
|
| 17 | 17 | |
| 18 | 18 | |
| 19 | - public function get_data( $video_urls ) { |
|
| 19 | + public function get_data( $video_urls ) { |
|
| 20 | 20 | |
| 21 | - $vimeo_ids = $this->get_video_ids($video_urls); |
|
| 21 | + $vimeo_ids = $this->get_video_ids($video_urls); |
|
| 22 | 22 | |
| 23 | - $vimeo_api_ids = array_map( function ( $item ) { |
|
| 24 | - return '/videos/' . $item; |
|
| 25 | - }, $vimeo_ids ); |
|
| 23 | + $vimeo_api_ids = array_map( function ( $item ) { |
|
| 24 | + return '/videos/' . $item; |
|
| 25 | + }, $vimeo_ids ); |
|
| 26 | 26 | |
| 27 | - $ids = join( ",", $vimeo_api_ids ); |
|
| 27 | + $ids = join( ",", $vimeo_api_ids ); |
|
| 28 | 28 | |
| 29 | - if ( ! $ids ) { |
|
| 30 | - return array(); |
|
| 31 | - } |
|
| 29 | + if ( ! $ids ) { |
|
| 30 | + return array(); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - $api_url = $this->get_api_url() . "/videos/"; |
|
| 34 | - $api_url = add_query_arg( array( |
|
| 35 | - 'uris' => $ids, |
|
| 36 | - 'fields' => 'name,description,link,uri,duration,release_time,pictures,stats' |
|
| 37 | - ), $api_url ); |
|
| 33 | + $api_url = $this->get_api_url() . "/videos/"; |
|
| 34 | + $api_url = add_query_arg( array( |
|
| 35 | + 'uris' => $ids, |
|
| 36 | + 'fields' => 'name,description,link,uri,duration,release_time,pictures,stats' |
|
| 37 | + ), $api_url ); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - $response = wp_remote_get( $api_url, array( |
|
| 41 | - 'headers' => array( |
|
| 42 | - 'Authorization' => 'bearer ' . $this->get_api_key() |
|
| 43 | - ) |
|
| 44 | - ) ); |
|
| 40 | + $response = wp_remote_get( $api_url, array( |
|
| 41 | + 'headers' => array( |
|
| 42 | + 'Authorization' => 'bearer ' . $this->get_api_key() |
|
| 43 | + ) |
|
| 44 | + ) ); |
|
| 45 | 45 | |
| 46 | - self::$requests_sent += 1; |
|
| 46 | + self::$requests_sent += 1; |
|
| 47 | 47 | |
| 48 | - return wp_remote_retrieve_body( $response ); |
|
| 49 | - } |
|
| 48 | + return wp_remote_retrieve_body( $response ); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public static function get_api_key() { |
|
| 52 | - return get_option( self::get_api_key_option_name(), false ); |
|
| 53 | - } |
|
| 51 | + public static function get_api_key() { |
|
| 52 | + return get_option( self::get_api_key_option_name(), false ); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - public static function get_api_key_option_name() { |
|
| 56 | - return '_wl_videoobject_vimeo_api_key'; |
|
| 57 | - } |
|
| 55 | + public static function get_api_key_option_name() { |
|
| 56 | + return '_wl_videoobject_vimeo_api_key'; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function get_api_url() { |
|
| 60 | - return 'https://api.vimeo.com'; |
|
| 61 | - } |
|
| 59 | + public function get_api_url() { |
|
| 60 | + return 'https://api.vimeo.com'; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function get_video_ids( $video_urls ) { |
|
| 63 | + public function get_video_ids( $video_urls ) { |
|
| 64 | 64 | |
| 65 | - $that = $this; |
|
| 65 | + $that = $this; |
|
| 66 | 66 | |
| 67 | - return array_filter( array_map( function ( $video_url ) use ( $that ) { |
|
| 68 | - if ( ! $video_url ) { |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - preg_match( $that::VIMEO_URL_REGEX, $video_url, $matches ); |
|
| 67 | + return array_filter( array_map( function ( $video_url ) use ( $that ) { |
|
| 68 | + if ( ! $video_url ) { |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + preg_match( $that::VIMEO_URL_REGEX, $video_url, $matches ); |
|
| 72 | 72 | |
| 73 | - if ( ! array_key_exists( 3, $matches ) ) { |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 73 | + if ( ! array_key_exists( 3, $matches ) ) { |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - return $matches[3]; |
|
| 77 | + return $matches[3]; |
|
| 78 | 78 | |
| 79 | - }, $video_urls ) ); |
|
| 80 | - } |
|
| 79 | + }, $video_urls ) ); |
|
| 80 | + } |
|
| 81 | 81 | } |
| 82 | 82 | \ No newline at end of file |
@@ -16,40 +16,40 @@ discard block |
||
| 16 | 16 | const VIMEO_URL_REGEX = '/https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)/'; |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - public function get_data( $video_urls ) { |
|
| 19 | + public function get_data($video_urls) { |
|
| 20 | 20 | |
| 21 | 21 | $vimeo_ids = $this->get_video_ids($video_urls); |
| 22 | 22 | |
| 23 | - $vimeo_api_ids = array_map( function ( $item ) { |
|
| 24 | - return '/videos/' . $item; |
|
| 25 | - }, $vimeo_ids ); |
|
| 23 | + $vimeo_api_ids = array_map(function($item) { |
|
| 24 | + return '/videos/'.$item; |
|
| 25 | + }, $vimeo_ids); |
|
| 26 | 26 | |
| 27 | - $ids = join( ",", $vimeo_api_ids ); |
|
| 27 | + $ids = join(",", $vimeo_api_ids); |
|
| 28 | 28 | |
| 29 | - if ( ! $ids ) { |
|
| 29 | + if ( ! $ids) { |
|
| 30 | 30 | return array(); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - $api_url = $this->get_api_url() . "/videos/"; |
|
| 34 | - $api_url = add_query_arg( array( |
|
| 33 | + $api_url = $this->get_api_url()."/videos/"; |
|
| 34 | + $api_url = add_query_arg(array( |
|
| 35 | 35 | 'uris' => $ids, |
| 36 | 36 | 'fields' => 'name,description,link,uri,duration,release_time,pictures,stats' |
| 37 | - ), $api_url ); |
|
| 37 | + ), $api_url); |
|
| 38 | 38 | |
| 39 | 39 | |
| 40 | - $response = wp_remote_get( $api_url, array( |
|
| 40 | + $response = wp_remote_get($api_url, array( |
|
| 41 | 41 | 'headers' => array( |
| 42 | - 'Authorization' => 'bearer ' . $this->get_api_key() |
|
| 42 | + 'Authorization' => 'bearer '.$this->get_api_key() |
|
| 43 | 43 | ) |
| 44 | - ) ); |
|
| 44 | + )); |
|
| 45 | 45 | |
| 46 | 46 | self::$requests_sent += 1; |
| 47 | 47 | |
| 48 | - return wp_remote_retrieve_body( $response ); |
|
| 48 | + return wp_remote_retrieve_body($response); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public static function get_api_key() { |
| 52 | - return get_option( self::get_api_key_option_name(), false ); |
|
| 52 | + return get_option(self::get_api_key_option_name(), false); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public static function get_api_key_option_name() { |
@@ -60,22 +60,22 @@ discard block |
||
| 60 | 60 | return 'https://api.vimeo.com'; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - public function get_video_ids( $video_urls ) { |
|
| 63 | + public function get_video_ids($video_urls) { |
|
| 64 | 64 | |
| 65 | 65 | $that = $this; |
| 66 | 66 | |
| 67 | - return array_filter( array_map( function ( $video_url ) use ( $that ) { |
|
| 68 | - if ( ! $video_url ) { |
|
| 67 | + return array_filter(array_map(function($video_url) use ($that) { |
|
| 68 | + if ( ! $video_url) { |
|
| 69 | 69 | return false; |
| 70 | 70 | } |
| 71 | - preg_match( $that::VIMEO_URL_REGEX, $video_url, $matches ); |
|
| 71 | + preg_match($that::VIMEO_URL_REGEX, $video_url, $matches); |
|
| 72 | 72 | |
| 73 | - if ( ! array_key_exists( 3, $matches ) ) { |
|
| 73 | + if ( ! array_key_exists(3, $matches)) { |
|
| 74 | 74 | return false; |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | return $matches[3]; |
| 78 | 78 | |
| 79 | - }, $video_urls ) ); |
|
| 79 | + }, $video_urls)); |
|
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | 82 | \ No newline at end of file |
@@ -6,37 +6,37 @@ |
||
| 6 | 6 | * This acts as interface for api clients |
| 7 | 7 | */ |
| 8 | 8 | interface Client { |
| 9 | - /** |
|
| 10 | - * |
|
| 11 | - * @param $video_urls array<string> Array of urls. |
|
| 12 | - * |
|
| 13 | - * Response body or false if the request cant be made or failed. |
|
| 14 | - * @return string | false |
|
| 15 | - */ |
|
| 16 | - public function get_data( $video_urls ); |
|
| 9 | + /** |
|
| 10 | + * |
|
| 11 | + * @param $video_urls array<string> Array of urls. |
|
| 12 | + * |
|
| 13 | + * Response body or false if the request cant be made or failed. |
|
| 14 | + * @return string | false |
|
| 15 | + */ |
|
| 16 | + public function get_data( $video_urls ); |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public static function get_api_key(); |
|
| 22 | - /** |
|
| 23 | - * Returns the option where the api key is stored. |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public static function get_api_key_option_name(); |
|
| 18 | + /** |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public static function get_api_key(); |
|
| 22 | + /** |
|
| 23 | + * Returns the option where the api key is stored. |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public static function get_api_key_option_name(); |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The api base url. |
|
| 30 | - * @return string |
|
| 31 | - */ |
|
| 32 | - public function get_api_url(); |
|
| 28 | + /** |
|
| 29 | + * The api base url. |
|
| 30 | + * @return string |
|
| 31 | + */ |
|
| 32 | + public function get_api_url(); |
|
| 33 | 33 | |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Get video ids from URLs. |
|
| 37 | - * @param $video_urls array<string> |
|
| 38 | - * @return array<string> |
|
| 39 | - */ |
|
| 40 | - public function get_video_ids( $video_urls ); |
|
| 35 | + /** |
|
| 36 | + * Get video ids from URLs. |
|
| 37 | + * @param $video_urls array<string> |
|
| 38 | + * @return array<string> |
|
| 39 | + */ |
|
| 40 | + public function get_video_ids( $video_urls ); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | * Response body or false if the request cant be made or failed. |
| 14 | 14 | * @return string | false |
| 15 | 15 | */ |
| 16 | - public function get_data( $video_urls ); |
|
| 16 | + public function get_data($video_urls); |
|
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | 19 | * @return string |
@@ -37,6 +37,6 @@ discard block |
||
| 37 | 37 | * @param $video_urls array<string> |
| 38 | 38 | * @return array<string> |
| 39 | 39 | */ |
| 40 | - public function get_video_ids( $video_urls ); |
|
| 40 | + public function get_video_ids($video_urls); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -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( ",", $this->get_video_ids( $video_urls ) ); |
|
| 55 | - } |
|
| 53 | + // extract the video ids. |
|
| 54 | + return join( ",", $this->get_video_ids( $video_urls ) ); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | |
| 58 | - public function get_video_ids( $video_urls ) { |
|
| 58 | + public 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( ",", $this->get_video_ids( $video_urls ) ); |
|
| 54 | + return join(",", $this->get_video_ids($video_urls)); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | |
| 58 | - public function get_video_ids( $video_urls ) { |
|
| 58 | + public 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 | |