Ejemplo n.º 1
0
/**
 * The main image function for displaying an image.  It supports several arguments that allow developers to
 * customize how the script outputs the image.
 *
 * The image check order is important to note here.  If an image is found by any specific check, the script
 * will no longer look for images.  The check order is 'meta_key', 'the_post_thumbnail', 'attachment', 
 * 'image_scan', 'callback', and 'default_image'.
 *
 * @since 0.1.0
 * @access public
 * @global $post The current post's database object.
 * @param array $args Arguments for how to load and display the image.
 * @return string|array The HTML for the image. | Image attributes in an array.
 */
function get_the_image($args = array())
{
    /* Set the default arguments. */
    $defaults = array('meta_key' => array('Thumbnail', 'thumbnail'), 'post_id' => get_the_ID(), 'attachment' => true, 'the_post_thumbnail' => true, 'size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'meta_key_save' => false, 'thumbnail_id_save' => false, 'callback' => null, 'cache' => true, 'before' => '', 'after' => '', 'echo' => true, 'custom_key' => null, 'default_size' => null);
    /* Allow plugins/themes to filter the arguments. */
    $args = apply_filters('get_the_image_args', $args);
    /* Merge the input arguments and the defaults. */
    $args = wp_parse_args($args, $defaults);
    /* If $default_size is given, overwrite $size. */
    if (!is_null($args['default_size'])) {
        $args['size'] = $args['default_size'];
    }
    // Deprecated 0.5 in favor of $size
    /* If $custom_key is set, overwrite $meta_key. */
    if (!is_null($args['custom_key'])) {
        $args['meta_key'] = $args['custom_key'];
    }
    // Deprecated 0.6 in favor of $meta_key
    /* If $format is set to 'array', don't link to the post. */
    if ('array' == $args['format']) {
        $args['link_to_post'] = false;
    }
    /* Extract the array to allow easy use of variables. */
    extract($args);
    /* Get cache key based on $args. */
    $key = md5(serialize(compact(array_keys($args))));
    /* Check for a cached image. */
    $image_cache = wp_cache_get($post_id, 'get_the_image');
    if (!is_array($image_cache)) {
        $image_cache = array();
    }
    /* Set up a default, empty $image_html variable. */
    $image_html = '';
    /* If there is no cached image, let's see if one exists. */
    if (!isset($image_cache[$key]) || empty($cache)) {
        /* If a custom field key (array) is defined, check for images by custom field. */
        if (!empty($meta_key)) {
            $image = get_the_image_by_meta_key($args);
        }
        /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
        if (empty($image) && !empty($the_post_thumbnail)) {
            $image = get_the_image_by_post_thumbnail($args);
        }
        /* If no image found and $attachment is set to true, check for an image by attachment. */
        if (empty($image) && !empty($attachment)) {
            $image = get_the_image_by_attachment($args);
        }
        /* If no image found and $image_scan is set to true, scan the post for images. */
        if (empty($image) && !empty($image_scan)) {
            $image = get_the_image_by_scan($args);
        }
        /* If no image found and a callback function was given. Callback function must pass back array of <img> attributes. */
        if (empty($image) && !is_null($callback) && function_exists($callback)) {
            $image = call_user_func($callback, $args);
        }
        /* If no image found and a $default_image is set, get the default image. */
        if (empty($image) && !empty($default_image)) {
            $image = get_the_image_by_default($args);
        }
        /* If an image was found. */
        if (!empty($image)) {
            /* If $meta_key_save was set, save the image to a custom field. */
            if (!empty($meta_key_save)) {
                get_the_image_meta_key_save($args, $image);
            }
            /* Format the image HTML. */
            $image_html = get_the_image_format($args, $image);
            /* Set the image cache for the specific post. */
            $image_cache[$key] = $image_html;
            wp_cache_set($post_id, $image_cache, 'get_the_image');
        }
    } else {
        $image_html = $image_cache[$key];
    }
    /* Allow plugins/theme to override the final output. */
    $image_html = apply_filters('get_the_image', $image_html);
    /* If $format is set to 'array', return an array of image attributes. */
    if ('array' == $format) {
        /* Set up a default empty array. */
        $out = array();
        /* Get the image attributes. */
        $atts = wp_kses_hair($image_html, array('http'));
        /* Loop through the image attributes and add them in key/value pairs for the return array. */
        foreach ($atts as $att) {
            $out[$att['name']] = $att['value'];
        }
        $out['url'] = $out['src'];
        // @deprecated 0.5 Use 'src' instead of 'url'.
        /* Return the array of attributes. */
        return $out;
    } elseif (false === $echo) {
        return !empty($image_html) ? $args['before'] . $image_html . $args['after'] : $image_html;
    }
    /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
    if (isset($image['post_thumbnail_id'])) {
        do_action('begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size);
    }
    /* Display the image if we get to this point. */
    echo !empty($image_html) ? $args['before'] . $image_html . $args['after'] : $image_html;
    /* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
    if (isset($image['post_thumbnail_id'])) {
        do_action('end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size);
    }
}
Ejemplo n.º 2
0
/**
 * The main image function for displaying an image.  It supports several arguments that allow developers to
 * customize how the script outputs the image.
 *
 * The image check order is important to note here.  If an image is found by any specific check, the script
 * will no longer look for images.  The check order is 'meta_key', 'the_post_thumbnail', 'attachment', 
 * 'image_scan', 'callback', and 'default_image'.
 *
 * @since 0.1.0
 * @global $post The current post's DB object.
 * @param array $args Arguments for how to load and display the image.
 * @return string|array The HTML for the image. | Image attributes in an array.
 */
function get_the_image($args = array())
{
    global $post;
    /* Set the default arguments. */
    $defaults = array('meta_key' => array('Thumbnail', 'thumbnail'), 'post_id' => $post->ID, 'attachment' => true, 'the_post_thumbnail' => true, 'size' => 'thumbnail', 'default_image' => false, 'order_of_image' => 1, 'link_to_post' => true, 'image_class' => false, 'image_scan' => false, 'width' => false, 'height' => false, 'format' => 'img', 'meta_key_save' => false, 'callback' => null, 'cache' => false, 'echo' => true, 'custom_key' => null, 'default_size' => null);
    /* Allow plugins/themes to filter the arguments. */
    $args = apply_filters('get_the_image_args', $args);
    /* Merge the input arguments and the defaults. */
    $args = wp_parse_args($args, $defaults);
    /* If $default_size is given, overwrite $size. */
    if (!is_null($args['default_size'])) {
        $args['size'] = $args['default_size'];
    }
    // Deprecated 0.5 in favor of $size
    /* If $custom_key is set, overwrite $meta_key. */
    if (!is_null($args['custom_key'])) {
        $args['meta_key'] = $args['custom_key'];
    }
    // Deprecated 0.6 in favor of $meta_key
    /* If $format is set to 'array', don't link to the post. */
    if ('array' == $args['format']) {
        $args['link_to_post'] = false;
    }
    /* Extract the array to allow easy use of variables. */
    extract($args);
    /* Check for a cached image. */
    $image_cache = wp_cache_get('get_the_image');
    if (!is_array($image_cache)) {
        $image_cache = array();
    }
    /* If there is no cached image, let's see if one exists. */
    if (!isset($image_cache[$post_id][$size]) || empty($cache)) {
        /* If a custom field key (array) is defined, check for images by custom field. */
        if (!empty($meta_key)) {
            $image = image_by_custom_field($args);
        }
        /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
        if (empty($image) && !empty($the_post_thumbnail)) {
            $image = image_by_the_post_thumbnail($args);
        }
        /* If no image found and $attachment is set to true, check for an image by attachment. */
        if (empty($image) && !empty($attachment)) {
            $image = image_by_attachment($args);
        }
        /* If no image found and $image_scan is set to true, scan the post for images. */
        if (empty($image) && !empty($image_scan)) {
            $image = image_by_scan($args);
        }
        /* If no image found and a callback function was given. */
        if (empty($image) && !is_null($callback) && function_exists($callback)) {
            $image = call_user_func($callback, $args);
        }
        /* If no image found and a $default_image is set, get the default image. */
        if (empty($image) && !empty($default_image) || !empty($default_image)) {
            $image = image_by_default($args);
        }
        /* If $meta_key_save was set, save the image to a custom field. */
        if (!empty($image) && !empty($meta_key_save)) {
            get_the_image_meta_key_save($args, $image);
        }
        /* If an image is returned, run it through the display function. */
        if (!empty($image)) {
            $image = display_the_image($args, $image);
        }
        $image_cache[$post_id][$size] = $image;
        wp_cache_set('get_the_image', $image_cache);
    } else {
        $image = $image_cache[$post_id][$size];
    }
    /* Allow plugins/theme to override the final output. */
    $image = apply_filters('get_the_image', $image);
    /* Display the image if $echo is set to true and the $format isn't an array. Else, return the image. */
    if ('array' == $format) {
        $atts = wp_kses_hair($image, array('http'));
        foreach ($atts as $att) {
            $out[$att['name']] = $att['value'];
        }
        $out['url'] = $out['src'];
        // @deprecated 0.5 Use 'src' instead of 'url'.
        return $out;
    } elseif (!empty($echo)) {
        echo $image;
    } else {
        return $image;
    }
}