ds_cache( $post ) { $zero_oembed_ttl = function () { return 0; }; add_filter( 'oembed_ttl', $zero_oembed_ttl ); global $wp_embed; $wp_embed->cache_oembed( $post->ID ); remove_filter( 'oembed_ttl', $zero_oembed_ttl ); } /** * @return string[] */ private function extract_supported_urls( $post_content ) { $urls = wp_extract_urls( $post_content ); return array_filter( $urls, array( $this, 'is_supported_media' ) ); } /** * @return string */ private function get_item_data_key( $url ) { return md5( trim( $url ) ); } /** * @return array|mixed */ public function get_cache( $post_id ) { $cache = get_post_meta( $post_id, self::META_KEY, true ); return empty( $cache ) ? array() : $cache; } /** * @return bool|int */ private function set_cache( $post_id, $value ) { return update_post_meta( $post_id, self::META_KEY, $value ); } /** * @return bool */ private function delete_cache( $post_id ) { return delete_post_meta( $post_id, self::META_KEY ); } /** * @return array */ private function fetch_oembed_data( $url ) { $url = trim( $url ); // Remove white spaces if any. $autoembed = $this->get_oembed(); $provider = $autoembed->get_provider( $url ); if ( filter_var( $provider, FILTER_VALIDATE_URL ) === false ) { return array(); } return $this->prepare_oembed_data( $url, $autoembed->fetch( $provider, $url ) ); } /** * @return array */ private function prepare_oembed_data( $url, $data ) { if ( ! $data ) { return array(); } $data->url = $url; return (array) $data; } /** * @return \WP_oEmbed */ private function get_oembed() { if ( ! class_exists( '\WP_oEmbed' ) ) { require_once ABSPATH . WPINC . '/class-oembed.php'; } return new \WP_oEmbed(); } /** * @return bool */ private function url_has_domain( $url, $domains ) { $domains = join( '|', array_map( 'preg_quote', $domains ) ); return (bool) preg_match( "~http(s)?://([^.]*.)?($domains).*~", $url ); } /** * @return bool */ private function is_youtube_url( $url ) { return $this->url_has_domain( $url, array( 'youtube.com', 'youtu.be' ) ); } /** * @return bool */ private function is_supported_media( $url ) { return $this->url_has_domain( $url, array_merge( $this->get_supported_video_domains(), $this->get_supported_audio_domains() ) ); } /** * @return bool */ private function is_supported_video( $url ) { return $this->url_has_domain( $url, $this->get_supported_video_domains() ); } /** * @return bool */ private function is_supported_audio( $url ) { return $this->url_has_domain( $url, $this->get_supported_audio_domains() ); } /** * @return string[] */ private function get_supported_audio_domains() { return array( 'soundcloud.com', 'mixcloud.com', 'spotify.com', ); } /** * @return string[] */ private function get_supported_video_domains() { return array( 'ted.com', 'vimeo.com', 'dailymotion.com', 'videopress.com', 'vine.com', 'youtube.com', 'youtu.be', ); } }