= $this->changeYoutubeUrlForYoutuDotBe( $iframe['src'] ); $youtube_url = $this->cleanYoutubeUrl( $iframe['src'] ); /** * Filter the LazyLoad HTML output on Youtube iframes * * @since 2.11 * * @param array $html Output that will be printed. */ $youtube_lazyload = apply_filters( 'rocket_lazyload_youtube_html', '
' ); $youtube_lazyload .= ''; return $youtube_lazyload; } /** * Gets the Youtube ID from the URL provided * * @param string $url URL to search. * @return bool|string */ public function getYoutubeIDFromURL( $url ) { $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; $result = preg_match( $pattern, $url, $matches ); if ( ! $result ) { return false; } // exclude playlist. if ( 'videoseries' === $matches[1] ) { return false; } return $matches[1]; } /** * Changes URL youtu.be/ID to youtube.com/embed/ID * * @param string $url URL to replace. * @return string Unchanged URL or modified URL. */ public function changeYoutubeUrlForYoutuDotBe( $url ) { $pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU'; $result = preg_match( $pattern, $url, $matches ); if ( ! $result ) { return $url; } return 'https://www.youtube.com/embed/' . $matches[1]; } /** * Cleans Youtube URL. Keeps only scheme, host and path. * * @param string $url URL to be cleaned. * @return string Cleaned URL */ public function cleanYoutubeUrl( $url ) { $parsed_url = wp_parse_url( $url, -1 ); $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : '//'; $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; return $scheme . $host . $path; } }