; $the_video['duration'] = gmdate( $time_format, $duration_sec ); } // Video data $the_video['title'] = self::remove_emoji( $video_info['snippet']['title'] ); $the_video['id'] = $video_id; $youtube_videos[ $video_id ] = $the_video; $youtube_updated = true; } } } // Vimeo elseif( $video_id = self::is_vimeo( $video ) ) { $videos_ids[] = array( 'id' => $video_id, 'type' => 'v', ); if( ! isset( $vimeo_videos[ $video_id ] ) ){ $video_info = self::get_vimeo_info( $video_id ); if( $video_info ){ $the_video = array(); // Prepare the Video duration if( ! empty( $video_info['duration'] ) ){ $duration_sec = $video_info['duration']; $time_format = ( $duration_sec >= 3600 ) ? 'H:i:s' : 'i:s'; $the_video['duration'] = gmdate( $time_format, $duration_sec ); } // Prepare the Video thumbnail if( ! empty( $video_info['thumbnail_small'] ) ){ $video_thumb = @parse_url( $video_info['thumbnail_small'] ); $video_thumb = str_replace( '/video/', '', $video_thumb['path'] ); $the_video['thumb'] = $video_thumb; } // Video data $the_video['title'] = self::remove_emoji( $video_info['title'] ); $the_video['id'] = $video_id; $vimeo_videos[ $video_id ] = $the_video; $vimeo_updated = true; } } } } if( $youtube_updated ){ update_option( 'tie_youtube_videos', $youtube_videos, false ); } if( $vimeo_updated ){ update_option( 'tie_vimeo_videos', $vimeo_videos, false ); } return $videos_ids; } /** * Check if the Video is YouTube */ private static function is_youtube( $video ) { preg_match( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video, $matches ); if( ! empty( $matches[0] ) ){ return TIELABS_HELPER::remove_spaces( $matches[0] ); } return false; } /** * Get YouTube Video data */ private static function get_youtube_info( $vid ){ if( ! tie_get_option( 'api_youtube' ) ){ return false; } // Build the Api request $params = array( 'part' => 'snippet,contentDetails', 'id' => $vid, 'key' => TIELABS_HELPER::remove_spaces( tie_get_option( 'api_youtube' ) ), ); $api_url = 'https://www.googleapis.com/youtube/v3/videos?' . http_build_query( $params ); $request = wp_remote_get( $api_url ); // Check if there are errors if( is_wp_error( $request ) ){ tie_debug_log( $request->get_error_message(), true ); return null; } // Prepare the data $result = json_decode( wp_remote_retrieve_body( $request ), true ); // Check Youtube API Errors if( ! empty( $result['error']['errors'][0]['message'] ) ){ update_option( 'tie_youtube_api_error', $result['error']['errors'][0]['message'], 'no' ); tie_debug_log( $result['error']['errors'][0]['message'], true ); return null; } // Check if the video title is exists if( empty( $result['items'][0]['snippet']['title'] ) ) { return null; } return $result['items'][0]; } /** * Check if the Video is Vimeo */ private static function is_vimeo( $video ) { preg_match( "/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/", $video, $matches ); if( ! empty( $matches[5] ) ){ return TIELABS_HELPER::remove_spaces( $matches[5] ); } return false; } /** * Get Vimeo Video data */ private static function get_vimeo_info( $vid ){ // Build the Api request $api_url = "https://vimeo.com/api/v2/video/$vid.json"; $request = wp_remote_get( $api_url ); // Check if there is no any errors if( is_wp_error( $request ) ){ tie_debug_log( $request->get_error_message(), true ); return null; } // Prepare the data $result = json_decode( wp_remote_retrieve_body( $request ), true ); // Check if the video title exists if( empty( $result[0]['title'] ) ){ return null; } return $result[0]; } /** * Remove emojis * * Causes issues https://core.trac.wordpress.org/ticket/21212 */ private static function remove_emoji( $string ) { // Match Emoticons $regex_emoticons = '/[\x{1F600}-\x{1F64F}]/u'; $clear_string = preg_replace($regex_emoticons, '', $string); // Match Miscellaneous Symbols and Pictographs $regex_symbols = '/[\x{1F300}-\x{1F5FF}]/u'; $clear_string = preg_replace($regex_symbols, '', $clear_string); // Match Transport And Map Symbols $regex_transport = '/[\x{1F680}-\x{1F6FF}]/u'; $clear_string = preg_replace($regex_transport, '', $clear_string); // Match Miscellaneous Symbols $regex_misc = '/[\x{2600}-\x{26FF}]/u'; $clear_string = preg_replace($regex_misc, '', $clear_string); // Match Dingbats $regex_dingbats = '/[\x{2700}-\x{27BF}]/u'; $clear_string = preg_replace($regex_dingbats, '', $clear_string); return $clear_string; } } // Instantiate the class new TIELABS_VIDEOS(); }