Esempio n. 1
1
function do_embed_lazy($html = '')
{
    if (is_exit() || empty($html)) {
        return $html;
    }
    $patern_ifr = '/<\\s*iframe\\s+(?<attr>[^>]*)/i';
    if (preg_match($patern_ifr, $html, $ifr)) {
        $attr_arr = parse_attr($ifr['attr']);
        if (!$attr_arr || empty($attr_arr['src']) || LAZY_PLACEHOLDER === $attr_arr['src'] || !empty($attr_arr['data-src'])) {
            return $html;
        }
        if (array_key_exists('class', $attr_arr)) {
            $attr_arr['class'] = do_class($attr_arr['class']);
        } else {
            $attr_arr['class'] = LAZY_CLASS;
        }
        $attr_arr['data-src'] = $attr_arr['src'];
        $attr_arr['src'] = LAZY_PLACEHOLDER;
        $html = str_replace($ifr['attr'], trim(attributes_to_string($attr_arr)), $html);
        get_assets();
    }
    return $html;
}
Esempio n. 2
0
function get_picture_tag($image_id, $default_size, array $responsive_sizes = null, array $options = [])
{
    // short-cirtcuit for animated gifs
    if (!empty($options['animated-gifs'])) {
        if (\get_post_mime_type($image_id) === 'image/gif') {
            $default_size = 'full';
            $responsive_sizes = null;
        }
    }
    $default_size = (array) $default_size;
    // get image url
    $default_url = get_image_url($image_id, $default_size[0]);
    // exit early
    if (!$default_url) {
        return '';
    }
    // data string
    $data_string = '';
    $data = [];
    if (!empty($options['data_size'])) {
        $img_src = get_image_source($image_id, $default_size[0]);
        if (!empty($img_src[0]) && !empty($img_src[1]) && !empty($img_src[2])) {
            $data['width'] = $img_src[1];
            $data['height'] = $img_src[2];
        }
    }
    $data_string = attributes_to_string($data, 'data-');
    // form the picture tag
    $result = '<picture';
    if (!empty($options['class'])) {
        $result .= ' class="' . implode(' ', (array) $options['class']) . '"';
    }
    $result .= $data_string;
    $result .= '>';
    if (!empty($responsive_sizes)) {
        foreach ($responsive_sizes as $r) {
            if (empty($r['image'])) {
                $r['image'] = $image_id;
            }
            $size = (array) $r['size'];
            $result .= '<source srcset="' . get_image_url($r['image'], $size[0]);
            if (count($size) > 1) {
                $result .= ', ' . get_image_url($r['image'], $size[1]) . ' 2x"';
            }
            $result .= ' media="(' . trim(trim($r['rule'], ')'), '(') . ')">';
        }
    }
    // default image
    if (count($default_size) > 1) {
        $result .= '<img srcset="' . $default_url . ', ' . get_image_url($image_id, $default_size[1]) . ' 2x"';
    } else {
        $result .= '<img src="' . $default_url . '"';
    }
    //the alt
    if (empty($options['no_alt'])) {
        $result .= ' alt="' . esc_attr(get_image_alt($image_id, true)) . '"';
    }
    $result .= '>';
    // close picture tag
    $result .= '</picture>';
    return $result;
}