function ale_get_embed_video($url)
{
    require_once ABSPATH . WPINC . '/class-oembed.php';
    $WP_oEmbed = new WP_oEmbed();
    $provider = $WP_oEmbed->discover($url);
    $data = $WP_oEmbed->fetch($provider, $url);
    return $data;
}
Beispiel #2
0
/**
 * Add Meta Keys for Fancybox Link & Embed for Video Posts 
 */
function orbitnews_fancy_video($post_ID)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (get_post_status($post_ID) != 'publish') {
        return;
    }
    /* Delete Existing Metakeys */
    delete_post_meta($post_ID, '_orn_oembed_body');
    delete_post_meta($post_ID, '_orn_embed_link');
    // Video Formats
    $video_link = trim(get_post_meta($post_ID, "orn_oembed_videos", true));
    $video_format = has_post_format('video', $post_ID);
    // Video Type Embed
    if (!empty($video_link) && $video_format) {
        // Get Video Data with wordpress oembed class
        require_once ABSPATH . WPINC . '/class-oembed.php';
        $oembed = new WP_oEmbed();
        $provider = $oembed->discover($video_link);
        $data = $oembed->fetch($provider, $video_link);
        if (isset($data) && $data != false) {
            // Setup variables
            $provider = $data->provider_name;
            // Video Provider Name
            $html_embed = $data->html;
            // Video Embed html
            $thumb_link = $data->thumbnail_url;
            // Video Post Thumbnail
            preg_match('/<iframe.*src=\\"(.*)\\".*><\\/iframe>/isU', $html_embed, $url_embed);
            $url_embed = $url_embed[1];
            // Embed Url
            $filename = sanitize_title($data->title);
            // Create filename from video title
            //Set meta key with feteched video embed html
            update_post_meta($post_ID, '_orn_oembed_body', $html_embed);
            //Video link and thumbnail for video posts
            switch ($provider) {
                case 'YouTube':
                    $url_embed .= '&amp;showinfo=0&amp;iv_load_policy=3&amp;modestbranding=0&amp;nologo=1&amp;vq=large&amp;autoplay=1&amp;ps=docs&amp;wmode=opaque&amp;rel=0';
                    break;
                case 'Vimeo':
                    $thumb_link = str_replace('_295.jpg', '_640.jpg', $thumb_link);
                    break;
                default:
                    // do nothing;
                    break;
            }
            //end switch
            // Add Meta key with embed link
            update_post_meta($post_ID, '_orn_embed_link', $url_embed);
            if (!has_post_thumbnail($post_ID)) {
                //Fetch and Store the Image
                $remote_file = wp_remote_get($thumb_link, array('sslverify' => false));
                $file_type = wp_remote_retrieve_header($remote_file, 'content-type');
                // Get image type
                $filename .= '-' . rawurldecode(basename($thumb_link));
                // add image file name to file
                $local_file_url = wp_upload_bits($filename, '', $remote_file['body']);
                // save image in local server
                //Attachment options
                $attachment = array('post_title' => $filename, 'post_mime_type' => $file_type, 'post_content' => '', 'post_status' => 'inherit', 'ping_status' => 'closed');
                // Add the image to your media library and set as featured image
                $attach_id = wp_insert_attachment($attachment, $local_file_url['file'], $post_ID);
                $attach_data = wp_generate_attachment_metadata($attach_id, $local_file_url['file']);
                wp_update_attachment_metadata($attach_id, $attach_data);
                set_post_thumbnail($post_ID, $attach_id);
            }
            // end !has_post_thumbnail()
        }
        //end isset( $data )
    }
    //end $video_link empty
}