コード例 #1
0
/**
 * Return suitable HTML code for output based on the content related to the global $post
 * If found, remove the content from the @global $post's post_content field
 *
 * @since 3.6.0
 *
 * @param string $type Required. 'audio' or 'video'
 * @param WP_Post $post Optional. Used instead of global $post when passed.
 * @param int $limit Optional. The number of medias to remove if content is scanned.
 * @return string
 */
function get_the_post_format_media($type, &$post = null, $limit = 0)
{
    global $wp_embed;
    if (empty($post)) {
        $post =& get_post();
    }
    if (empty($post)) {
        return '';
    }
    $cache_key = "media:{$type}";
    if (isset($post->format_content[$cache_key])) {
        return $post->format_content[$cache_key];
    }
    if (!isset($post->format_content)) {
        $post->format_content = array();
    }
    $count = 1;
    if (has_post_format($type, $post)) {
        $meta = get_post_format_meta($post->ID);
        if (!empty($meta[$type . '_embed'])) {
            $value = $meta[$type . '_embed'];
            if (is_integer($value)) {
                $url = wp_get_attachment_url($value);
                $shortcode = sprintf('[%s src="%s"]', $type, $url);
            } elseif (preg_match('/' . get_shortcode_regex() . '/s', $value)) {
                $shortcode = $value;
            } elseif (preg_match('#<[^>]+>#', $value)) {
                $post->format_content[$cache_key] = $value;
                return $post->format_content[$cache_key];
            } elseif (0 === strpos($value, 'http')) {
                $post->split_content = str_replace($value, '', $post->post_content, $count);
                if (strstr($value, home_url())) {
                    $shortcode = sprintf('[%s src="%s"]', $type, $value);
                } else {
                    $post->format_content[$cache_key] = $wp_embed->autoembed($value);
                    return $post->format_content[$cache_key];
                }
            }
            if (!empty($shortcode)) {
                $post->format_content[$cache_key] = do_shortcode($shortcode);
                return $post->format_content[$cache_key];
            }
        }
    }
    // these functions expect a reference, so we should make a copy of post content to avoid changing it
    $content = $post->post_content;
    $htmls = get_content_media($type, $content, true, true, $limit);
    if (!empty($htmls)) {
        $html = reset($htmls);
        $post->split_content = $content;
        $post->format_content[$cache_key] = $html;
        return $post->format_content[$cache_key];
    }
    $embeds = get_embedded_media($type, $content, true, 1);
    if (!empty($embeds)) {
        $embed = reset($embeds);
        $post->split_content = $content;
        if (0 === strpos($embed, 'http')) {
            if (strstr($embed, home_url())) {
                $post->format_content[$cache_key] = do_shortcode(sprintf('[%s src="%s"]', $type, $embed));
            } else {
                $post->format_content[$cache_key] = $wp_embed->autoembed($embed);
            }
        } else {
            $post->format_content[$cache_key] = $embed;
        }
        return $post->format_content[$cache_key];
    }
    $medias = call_user_func('get_attached_' . $type, $post->ID);
    if (!empty($medias)) {
        $media = reset($medias);
        $url = wp_get_attachment_url($media->ID);
        $shortcode = sprintf('[%s src="%s"]', $type, $url);
        $post->format_content[$cache_key] = do_shortcode($shortcode);
        return $post->format_content[$cache_key];
    }
    return '';
}
コード例 #2
0
function sp_filter_display_media_embeds($content)
{
    global $wp_embed;
    $embeds = get_embedded_media('audio', $content, true, 1);
    if (!empty($embeds)) {
        $embed = reset($embeds);
        $content = $wp_embed->autoembed($embed) . $content;
        return $content;
    }
    $embeds = get_embedded_media('video', $content, true, 1);
    if (!empty($embeds)) {
        $embed = reset($embeds);
        $content = $wp_embed->autoembed($embed) . $content;
        return $content;
    }
    return $content;
}