Beispiel #1
0
/**
 * Check the content blob for an <audio>, <video> <object>, <embed>, or <iframe>
 *
 * @since 3.6.0
 *
 * @param string $content A string which might contain media data.
 * @param array $types array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'
 * @return array A list of found HTML media embeds
 */
function get_media_embedded_in_content($content, $types = null)
{
    $html = array();
    $allowed_media_types = array('audio', 'video', 'object', 'embed', 'iframe');
    if (!empty($types)) {
        if (!is_array($types)) {
            $types = array($types);
        }
        $allowed_media_types = array_intersect($allowed_media_types, $types);
    }
    foreach ($allowed_media_types as $tag) {
        if (preg_match('#' . get_tag_regex($tag) . '#', $content, $matches)) {
            $html[] = $matches[0];
        }
    }
    return $html;
}
 /**
  * Extract and return the first image from passed content.
  *
  * @since 3.1.0
  * @link https://core.trac.wordpress.org/browser/trunk/wp-includes/media.php?rev=24240#L2223
  *
  * @param string  $content A string which might contain a URL.
  * @return string The found URL.
  */
 public function get_image_in_content($content)
 {
     $image = '';
     if (!empty($content) && preg_match('#' . get_tag_regex('img') . '#i', $content, $matches) && !empty($matches)) {
         // @todo Sanitize this.
         $image = $matches[0];
     }
     return $image;
 }
/**
 * Retrieve the attachment post id from HTML containing an image.
 *
 * @since 3.6.0
 *
 * @param string $html The html, possibly with an image
 * @param string $matched_html Passed by reference, will be set to to the matched img string
 * @return int The attachment id if found, or 0.
 */
function img_html_to_post_id($html, &$matched_html = null)
{
    $attachment_id = 0;
    // Look for an <img /> tag
    if (!preg_match('#' . get_tag_regex('img') . '#i', $html, $matches) || empty($matches)) {
        return $attachment_id;
    }
    $matched_html = $matches[0];
    // Look for attributes.
    if (!preg_match_all('#(src|class)=([\'"])(.+?)\\2#is', $matched_html, $matches) || empty($matches)) {
        return $attachment_id;
    }
    $attr = array();
    foreach ($matches[1] as $key => $attribute_name) {
        $attr[$attribute_name] = $matches[3][$key];
    }
    if (!empty($attr['class']) && false !== strpos($attr['class'], 'wp-image-')) {
        if (preg_match('#wp-image-([0-9]+)#i', $attr['class'], $matches)) {
            $attachment_id = absint($matches[1]);
        }
    }
    if (!$attachment_id && !empty($attr['src'])) {
        $attachment_id = attachment_url_to_postid($attr['src']);
    }
    return $attachment_id;
}
Beispiel #4
0
/**
 * Store any wanted media for later use and process $content to strip them out of it
 * Argument $types receives an array of wanted media types: 'audio', 'video', 'object', 'embed', or 'iframe'.
 * Function based on get_media_embedded_in_content() from media.php (in Core @since 3.6.0).
 */
function quadro_print_media($content, $types = null, $amount = 999, $before, $after)
{
    $allowed_media_types = array('audio', 'video', 'object', 'embed', 'iframe');
    $media = '';
    $i = 0;
    if (!empty($types)) {
        if (!is_array($types)) {
            $types = array($types);
        }
        $allowed_media_types = array_intersect($allowed_media_types, $types);
    }
    // Filter through the_content filter
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    foreach ($allowed_media_types as $tag) {
        if (preg_match_all('#' . get_tag_regex($tag) . '#', $content, $elements)) {
            foreach ($elements[0] as $element) {
                $media .= $before . $element . $after;
                // Remove element from $content
                $content = str_replace($element, '', $content);
                // Exit this if we reached the requested amount.
                $i++;
                if ($amount == $i) {
                    break 2;
                }
            }
        }
    }
    // Return the results, content and requested media apart
    return array('content' => $content, 'media' => $media);
}