Ejemplo n.º 1
0
/**
 * Function to get the post thumbnail.
 *
 * @since 1.7
 *
 * @param 	array|string 	$args	Array / Query string with arguments post thumbnails
 * @return 	string 					Output with the post thumbnail
 */
function crp_get_the_post_thumbnail($args = array())
{
    global $crp_url, $crp_settings;
    $defaults = array('postid' => '', 'thumb_height' => '150', 'thumb_width' => '150', 'thumb_meta' => 'post-image', 'thumb_html' => 'html', 'thumb_default' => '', 'thumb_default_show' => true, 'scan_images' => false, 'class' => 'crp_thumb');
    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args($args, $defaults);
    // Issue notice for deprecated arguments
    if (isset($args['thumb_timthumb'])) {
        _deprecated_argument(__FUNCTION__, '2.1', __('thumb_timthumb argument has been deprecated', CRP_LOCAL_NAME));
    }
    if (isset($args['thumb_timthumb_q'])) {
        _deprecated_argument(__FUNCTION__, '2.1', __('thumb_timthumb_q argument has been deprecated', CRP_LOCAL_NAME));
    }
    if (isset($args['filter'])) {
        _deprecated_argument(__FUNCTION__, '2.1', __('filter argument has been deprecated', CRP_LOCAL_NAME));
    }
    $result = get_post($args['postid']);
    $post_title = get_the_title($args['postid']);
    $output = '';
    $postimage = '';
    // Let's start fetching the thumbnail. First place to look is in the post meta defined in the Settings page
    if (!$postimage) {
        $postimage = get_post_meta($result->ID, $args['thumb_meta'], true);
        // Check the post meta first
        $pick = 'meta';
    }
    // If there is no thumbnail found, check the post thumbnail
    if (!$postimage) {
        if (false != get_post_thumbnail_id($result->ID)) {
            $postthumb = wp_get_attachment_image_src(get_post_thumbnail_id($result->ID), $crp_settings['thumb_size']);
            $postimage = $postthumb[0];
        }
        $pick = 'featured';
    }
    // If there is no thumbnail found, fetch the first image in the post, if enabled
    if (!$postimage && $args['scan_images']) {
        preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $result->post_content, $matches);
        if (isset($matches[1][0]) && $matches[1][0]) {
            // any image there?
            $postimage = $matches[1][0];
            // we need the first one only!
        }
        if ($postimage) {
            $postimage_id = crp_get_attachment_id_from_url($postimage);
            if (false != wp_get_attachment_image_src($postimage_id, $crp_settings['thumb_size'])) {
                $postthumb = wp_get_attachment_image_src($postimage_id, $crp_settings['thumb_size']);
                $postimage = $postthumb[0];
            }
            $pick = 'correct';
        }
        $pick .= 'first';
    }
    // If there is no thumbnail found, fetch the first child image
    if (!$postimage) {
        $postimage = crp_get_first_image($result->ID);
        // Get the first image
        $pick = 'firstchild';
    }
    // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
    if (!$postimage) {
        $postimage = get_post_meta($result->ID, '_video_thumbnail', true);
        $pick = 'video';
    }
    // If no thumb found and settings permit, use default thumb
    if (!$postimage && $args['thumb_default_show']) {
        $postimage = $args['thumb_default'];
        $pick = 'default';
    }
    // Hopefully, we've found a thumbnail by now. If so, run it through the custom filter, check for SSL and create the image tag
    if ($postimage) {
        /**
         * Filters the thumbnail image URL.
         *
         * Use this filter to modify the thumbnail URL that is automatically created
         * Before v2.1 this was used for cropping the post image using timthumb
         *
         * @since	2.1.0
         *
         * @param	string	$postimage		URL of the thumbnail image
         * @param	int		$thumb_width	Thumbnail width
         * @param	int		$thumb_height	Thumbnail height
         * @param	object	$result			Post Object
         */
        $postimage = apply_filters('crp_thumb_url', $postimage, $args['thumb_width'], $args['thumb_height'], $result);
        /* Backward compatibility */
        $thumb_timthumb = false;
        $thumb_timthumb_q = 75;
        /**
         * Filters the thumbnail image URL.
         *
         * @since	1.8.10
         * @deprecated	2.1	Use crp_thumb_url instead.
         *
         * @param	string	$postimage		URL of the thumbnail image
         * @param	int		$thumb_width	Thumbnail width
         * @param	int		$thumb_height	Thumbnail height
         * @param	boolean	$thumb_timthumb	Enable timthumb?
         * @param	int		$thumb_timthumb_q	Quality of timthumb thumbnail.
         * @param	object	$result			Post Object
         */
        $postimage = apply_filters('crp_postimage', $postimage, $args['thumb_width'], $args['thumb_height'], $thumb_timthumb, $thumb_timthumb_q, $result);
        if (is_ssl()) {
            $postimage = preg_replace('~http://~', 'https://', $postimage);
        }
        if ('css' == $args['thumb_html']) {
            $thumb_html = 'style="max-width:' . $args['thumb_width'] . 'px;max-height:' . $args['thumb_height'] . 'px;"';
        } else {
            if ('html' == $args['thumb_html']) {
                $thumb_html = 'width="' . $args['thumb_width'] . '" height="' . $args['thumb_height'] . '"';
            } else {
                $thumb_html = '';
            }
        }
        /**
         * Filters the thumbnail HTML and allows a filter function to add any more HTML if needed.
         *
         * @since	2.2.0
         *
         * @param	string	$thumb_html	Thumbnail HTML
         */
        $thumb_html = apply_filters('crp_thumb_html', $thumb_html);
        $class = $args['class'] . ' crp_' . $pick;
        $output .= '<img src="' . $postimage . '" alt="' . $post_title . '" title="' . $post_title . '" ' . $thumb_html . ' class="' . $args['class'] . '" />';
    }
    /**
     * Filters post thumbnail created for CRP.
     *
     * @since	1.9
     *
     * @param	array	$output	Formatted output
     * @param	array	$args	Argument list
     */
    return apply_filters('crp_get_the_post_thumbnail', $output, $args);
}
/**
 * Function to get the post thumbnail.
 *
 * @since 1.7
 *
 * @param 	array|string 	$args	Array / Query string with arguments post thumbnails
 * @return 	string 					Output with the post thumbnail
 */
function crp_get_the_post_thumbnail($args = array())
{
    $defaults = array('postid' => '', 'thumb_height' => '50', 'thumb_width' => '50', 'thumb_meta' => 'post-image', 'thumb_html' => 'html', 'thumb_default' => '', 'thumb_default_show' => true, 'thumb_timthumb' => true, 'thumb_timthumb_q' => '75', 'scan_images' => false, 'class' => 'crp_thumb', 'filter' => 'crp_postimage');
    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args($args, $defaults);
    // Declare each item in $args as its own variable i.e. $type, $before.
    extract($args, EXTR_SKIP);
    $result = get_post($postid);
    $title = get_the_title($postid);
    $output = '';
    $thumb_html = 'css' == $thumb_html ? 'style="max-width:' . $thumb_width . 'px;max-height:' . $thumb_height . 'px;"' : 'width="' . $thumb_width . '" height="' . $thumb_height . '"';
    // Let's start fetching the thumbnail. First place to look is in the post meta defined in the Settings page
    $postimage = get_post_meta($result->ID, $thumb_meta, true);
    // Check the post meta first
    // If there is no thumbnail found, check the post thumbnail
    if (!$postimage) {
        if (function_exists('has_post_thumbnail') && ('' != wp_get_attachment_image_src(get_post_thumbnail_id($result->ID)) || false != wp_get_attachment_image_src(get_post_thumbnail_id($result->ID)))) {
            $postthumb = wp_get_attachment_image_src(get_post_thumbnail_id($result->ID), 'crp_thumbnail');
            $postimage = $postthumb[0];
        }
    }
    // If there is no thumbnail found, fetch the first image in the post, if enabled
    if (!$postimage && $scan_images) {
        preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $result->post_content, $matches);
        if (isset($matches[1][0]) && $matches[1][0]) {
            // any image there?
            $postimage = $matches[1][0];
            // we need the first one only!
        }
    }
    // If there is no thumbnail found, fetch the first child image
    if (!$postimage) {
        $postimage = crp_get_first_image($result->ID);
        // Get the first image
    }
    // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
    if (!$postimage) {
        $postimage = get_post_meta($result->ID, '_video_thumbnail', true);
    }
    // If no thumb found and settings permit, use default thumb
    if ($thumb_default_show && !$postimage) {
        $postimage = $thumb_default;
    }
    // Hopefully, we've found a thumbnail by now. If so, run it through the custom filter, check for SSL and create the image tag
    if ($postimage) {
        /**
         * Get the first image in the post.
         *
         * @since 1.8.10
         *
         * @param mixed $postID	Post ID
         */
        $postimage = apply_filters($filter, $postimage, $thumb_width, $thumb_height, $thumb_timthumb, $thumb_timthumb_q, $result);
        if (is_ssl()) {
            $postimage = preg_replace('~http://~', 'https://', $postimage);
        }
        $output .= '<img src="' . $postimage . '" alt="' . $title . '" title="' . $title . '" ' . $thumb_html . ' class="' . $class . '" />';
    }
    /**
     * Filters post thumbnail created for CRP.
     *
     * @since	1.9
     *
     * @param	array	$output	Formatted output
     */
    return apply_filters('crp_get_the_post_thumbnail', $output);
}