Beispiel #1
0
 public function get_data($url = '', $args = '')
 {
     if ($url) {
         $this->url = $url;
     }
     if (!$this->url) {
         return false;
     }
     //if it was already fetched
     if ($this->data) {
         return $this->data;
     }
     $provider = self::$oembed->get_provider($this->url, $args);
     if (!$provider) {
         return false;
     }
     $data = self::$oembed->fetch($provider, $this->url, $args);
     if (!$data) {
         return false;
     }
     $this->data = $data;
     return $this->data;
 }
/**
 * Filter the oembed results to see if we should do some extra handling
 *
 * @since 0.1
 * @param string $html     The original HTML returned from the external oembed provider.
 * @param string $url      The URL found in the content.
 * @param mixed  $attr     An array with extra attributes.
 * @param int    $post_id  The post ID.
 * @return string The potentially filtered HTML.
 */
function instant_articles_embed_oembed_html($html, $url, $attr, $post_id)
{
    if (!class_exists('WP_oEmbed')) {
        include_once ABSPATH . WPINC . '/class-oembed.php';
    }
    // Instead of checking all possible URL variants, use the provider list from WP_oEmbed.
    $wp_oembed = new WP_oEmbed();
    $provider_url = $wp_oembed->get_provider($url);
    $provider_name = false;
    if (false !== strpos($provider_url, 'instagram.com')) {
        $provider_name = 'instagram';
    } elseif (false !== strpos($provider_url, 'twitter.com')) {
        $provider_name = 'twitter';
    } elseif (false !== strpos($provider_url, 'youtube.com')) {
        $provider_name = 'youtube';
    } elseif (false !== strpos($provider_url, 'vine.co')) {
        $provider_name = 'vine';
    }
    $provider_name = apply_filters('instant_articles_social_embed_type', $provider_name, $url);
    if ($provider_name) {
        $html = instant_articles_embed_get_html($provider_name, $html, $url, $attr, $post_id);
    }
    return $html;
}
function uncode_admin_get_oembed()
{
    $code = $mime = '';
    $width = 1;
    $height = 1;
    $urlEnterd = isset($_REQUEST['urlOembed']) ? urldecode($_REQUEST['urlOembed']) : die;
    $onlycode = isset($_REQUEST['onlycode']) ? $_REQUEST['onlycode'] : false;
    $urlEnterd = str_replace('https://instagram.com', 'http://instagram.com', $urlEnterd);
    $WP_oembed = new WP_oEmbed();
    $raw_provider = parse_url($WP_oembed->get_provider($urlEnterd));
    if (isset($raw_provider['host'])) {
        $host = $raw_provider['host'];
        $strip = array("www.", "api.", "embed.");
        $bare_host = str_replace($strip, "", $host);
        $bare_host = explode('.', $bare_host);
        $mime = 'oembed/' . $bare_host[0];
        $code = wp_oembed_get($urlEnterd);
        preg_match_all('/(width|height)=("[^"]*")/i', $code, $img_attr);
        if (isset($img_attr[2][0])) {
            $width = preg_replace('/\\D/', '', $img_attr[2][0]);
        }
        if (isset($img_attr[2][1])) {
            $height = preg_replace('/\\D/', '', $img_attr[2][1]);
        }
        if ($bare_host[0] === 'youtube') {
            $parts = parse_url($urlEnterd);
            if (isset($parts['query'])) {
                parse_str($parts['query'], $query);
                $idvideo = $query['v'];
            } else {
                $idvideo = $parts['path'];
                $idvideo = str_replace('/', '', $idvideo);
            }
            $data = wp_remote_fopen("https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails&id=" . $idvideo . "&key=AIzaSyA9PEtdNGSwzuM8QtaDbZvkcSlkh_UG2HI");
            $json = json_decode($data);
            $code = '<img src="' . $json->items[0]->snippet->thumbnails->default->url . '" />';
        } else {
            if ($bare_host[0] === 'vimeo') {
                $urlEnterd = preg_replace('/#.*/', '', $urlEnterd);
                $vimeo = unserialize(wp_remote_fopen("http://vimeo.com/api/v2/video/" . basename(strtok($urlEnterd, '?')) . ".php"));
                $code = '<img src="' . $vimeo[0]['thumbnail_large'] . '" />';
            } else {
                if ($bare_host[0] === 'flickr') {
                    $code = preg_replace('/<\\/?a[^>]*>/', '', $code);
                }
            }
        }
    } else {
        if (preg_match('/(\\.jpg|\\.jpeg|\\.png|\\.bmp)$/i', $urlEnterd) || preg_match('/(\\.jpg?|\\.jpeg?|\\.png?|\\.bmp?)/i', $urlEnterd) || strpos($urlEnterd, 'imgix') !== false) {
            $code = '<img src="' . $urlEnterd . '" />';
            $mime = 'image/url';
            if ($onlycode == 'false') {
                if ($getsize = @getimagesize($urlEnterd)) {
                    if (isset($getsize[0])) {
                        $width = $getsize[0];
                    }
                    if (isset($getsize[1])) {
                        $height = $getsize[1];
                    }
                } else {
                    $width = 'indefinit';
                    $height = 'indefinit';
                }
            }
        } else {
            if (strpos(strtolower($urlEnterd), '<iframe') !== false) {
                $mime = 'oembed/iframe';
                preg_match_all('/(width|height)=("[^"]*")/i', $urlEnterd, $iframe_size);
                if (isset($iframe_size[2][0])) {
                    preg_match("|\\d+|", $iframe_size[2][0], $width);
                    $width = $width[0];
                }
                if (isset($iframe_size[2][1])) {
                    preg_match("|\\d+|", $iframe_size[2][1], $height);
                    $height = $height[0];
                }
            } else {
                if (strpos(strtolower($urlEnterd), '<svg') !== false) {
                    $mime = 'oembed/svg';
                    preg_match_all('/(width|height)=("[^"]*")/i', $urlEnterd, $svg_size);
                    if (isset($svg_size[2][0])) {
                        preg_match("|\\d+|", $svg_size[2][0], $width);
                        $width = $width[0];
                    }
                    if (isset($svg_size[2][1])) {
                        preg_match("|\\d+|", $svg_size[2][1], $height);
                        $height = $height[0];
                    }
                } else {
                    $mime = 'oembed/html';
                }
            }
            $code = esc_html($urlEnterd);
        }
    }
    if ($code == '' && $urlEnterd != '') {
        $code = 'null';
    }
    echo json_encode(array('code' => $code, 'mime' => $mime, 'width' => $width, 'height' => $height));
    die;
}
Beispiel #4
0
/**
 * Import a video thumbnail from an oEmbed endpoint into the media library.
 *
 * @todo Considering doing video URL comparison rather than oembed thumbnail
 *       comparison?
 *
 * @since 1.8.0
 *
 * @param int $post_id Video post ID.
 * @param string $url Video URL.
 */
function audiotheme_video_sideload_thumbnail($post_id, $url)
{
    require_once ABSPATH . WPINC . '/class-oembed.php';
    $oembed = new \WP_oEmbed();
    $provider = $oembed->get_provider($url);
    if (!$provider || false === ($data = $oembed->fetch($provider, $url)) || !isset($data->thumbnail_url)) {
        return;
    }
    $current_thumb_id = get_post_thumbnail_id($post_id);
    $oembed_thumb_id = get_post_meta($post_id, '_audiotheme_oembed_thumbnail_id', true);
    $oembed_thumb_url = get_post_meta($post_id, '_audiotheme_oembed_thumbnail_url', true);
    // Re-use the existing oEmbed data instead of making another copy of the thumbnail.
    if ($data->thumbnail_url == $oembed_thumb_url && (!$current_thumb_id || $current_thumb_id != $oembed_thumb_id)) {
        set_post_thumbnail($post_id, $oembed_thumb_id);
    } elseif (!$current_thumb_id || $data->thumbnail_url != $oembed_thumb_url) {
        $attachment_id = audiotheme_video_sideload_image($data->thumbnail_url, $post_id);
        if (!empty($attachment_id) && !is_wp_error($attachment_id)) {
            set_post_thumbnail($post_id, $attachment_id);
            // Store the oEmbed thumb data so the same image isn't copied on repeated requests.
            update_post_meta($post_id, '_audiotheme_oembed_thumbnail_id', $attachment_id);
            update_post_meta($post_id, '_audiotheme_oembed_thumbnail_url', $data->thumbnail_url);
        }
    }
}