Exemple #1
0
/**
 * Function to get the post thumbnail.
 *
 * @since	1.8
 * @param	array $args   Query string of options related to thumbnails
 * @return	string	Image tag
 */
function tptn_get_the_post_thumbnail($args = array())
{
    $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' => 'tptn_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', 'top-10'));
    }
    if (isset($args['thumb_timthumb_q'])) {
        _deprecated_argument(__FUNCTION__, '2.1', __('thumb_timthumb_q argument has been deprecated', 'top-10'));
    }
    if (isset($args['filter'])) {
        _deprecated_argument(__FUNCTION__, '2.1', __('filter argument has been deprecated', 'top-10'));
    }
    if (is_int($args['postid'])) {
        $result = get_post($args['postid']);
    } else {
        $result = $args['postid'];
    }
    $post_title = $result->post_title;
    /**
     * Filters the title and alt message for thumbnails.
     *
     * @since	2.3.0
     *
     * @param	string	$post_title		Post tile used as thumbnail alt and title
     * @param	object	$result			Post Object
     */
    $post_title = apply_filters('tptn_thumb_title', $post_title, $result);
    $output = '';
    $postimage = '';
    $pick = '';
    // 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 ($postimage) {
            $postimage_id = tptn_get_attachment_id_from_url($postimage);
            if (false != wp_get_attachment_image_src($postimage_id, array($args['thumb_width'], $args['thumb_height']))) {
                $postthumb = wp_get_attachment_image_src($postimage_id, array($args['thumb_width'], $args['thumb_height']));
                $postimage = $postthumb[0];
            }
            $pick .= 'correct';
        }
    }
    // 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), array($args['thumb_width'], $args['thumb_height']));
            $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!
        }
        $pick = 'first';
        if ($postimage) {
            $postimage_id = tptn_get_attachment_id_from_url($postimage);
            if (false != wp_get_attachment_image_src($postimage_id, array($args['thumb_width'], $args['thumb_height']))) {
                $postthumb = wp_get_attachment_image_src($postimage_id, array($args['thumb_width'], $args['thumb_height']));
                $postimage = $postthumb[0];
            }
            $pick .= 'correct';
        }
    }
    // If there is no thumbnail found, fetch the first child image
    if (!$postimage) {
        $postimage = tptn_get_first_image($result->ID, $args['thumb_width'], $args['thumb_height']);
        // 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_thumb';
    }
    // If no thumb found and settings permit, use default thumb
    if (!$postimage && $args['thumb_default_show']) {
        $postimage = $args['thumb_default'];
        $pick = 'default_thumb';
    }
    // 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('tptn_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.0	Use tptn_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('tptn_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('tptn_thumb_html', $thumb_html);
        $class = $args['class'] . ' tptn_' . $pick;
        /**
         * Filters the thumbnail classes and allows a filter function to add any more classes if needed.
         *
         * @since	2.2.0
         *
         * @param	string	$thumb_html	Thumbnail HTML
         */
        $class = apply_filters('tptn_thumb_class', $class);
        $output .= '<img src="' . $postimage . '" alt="' . $post_title . '" title="' . $post_title . '" ' . $thumb_html . ' class="' . $class . '" />';
    }
    /**
     * Filters post thumbnail created for Top 10.
     *
     * @since	1.9.10.1
     *
     * @param	array	$output	Formatted output
     * @param	array	$args	Argument list
     */
    return apply_filters('tptn_get_the_post_thumbnail', $output, $args);
}
Exemple #2
0
/**
 * Function to get the post thumbnail.
 *
 * @since	1.8
 * @param	array	$args	Query string of options related to thumbnails
 * @return	string	Image tag
 */
function tptn_get_the_post_thumbnail($args = array())
{
    global $tptn_url, $tptn_settings;
    $defaults = array('postid' => '', 'thumb_height' => '150', 'thumb_width' => '150', 'thumb_meta' => 'post-image', 'thumb_html' => 'html', 'thumb_default' => '', 'thumb_default_show' => true, 'thumb_timthumb' => true, 'thumb_timthumb_q' => '75', 'scan_images' => false, 'class' => 'tptn_thumb', 'filter' => 'tptn_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);
    $post_title = get_the_title($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, $thumb_meta, true);
        // Check the post meta first
        $pick = 'meta';
    }
    // If there is no thumbnail found, check the post thumbnail
    if (!$postimage) {
        if (false != wp_get_attachment_image_src(get_post_thumbnail_id($result->ID))) {
            $postthumb = wp_get_attachment_image_src(get_post_thumbnail_id($result->ID), $tptn_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 && $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!
        }
        $pick = 'first';
    }
    // If there is no thumbnail found, fetch the first child image
    if (!$postimage) {
        $postimage = tptn_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);
    }
    // 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);
        }
        $thumb_html = 'css' == $thumb_html ? 'style="max-width:' . $thumb_width . 'px;max-height:' . $thumb_height . 'px;"' : 'width="' . $thumb_width . '" height="' . $thumb_height . '"';
        $class .= ' tptn_' . $pick;
        $output .= '<img src="' . $postimage . '" alt="' . $post_title . '" title="' . $post_title . '" ' . $thumb_html . ' class="' . $class . '" />';
    }
    /**
     * Filters post thumbnail created for Top 10.
     *
     * @since	1.9.10.1
     *
     * @param	array	$output	Formatted output
     * @param	array	$args	Argument list
     */
    return apply_filters('tptn_get_the_post_thumbnail', $output, $args);
}
Exemple #3
0
/**
 * Function to get the post thumbnail.
 *
 * @access public
 * @param array $args (default: array()) Query string of options related to thumbnails
 * @return string
 */
function tptn_get_the_post_thumbnail($args = array())
{
    global $tptn_url;
    $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, 'scan_images' => false, 'class' => 'tptn_thumb', 'filter' => 'tptn_postimage');
    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args($args, $defaults);
    // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before.
    extract($args, EXTR_SKIP);
    $result = get_post($postid);
    $output = '';
    $title = get_the_title($postid);
    $thumb_html = $thumb_html == 'css' ? 'style="max-width:' . $thumb_width . 'px;max-height:' . $thumb_height . 'px;"' : 'width="' . $thumb_width . '" height="' . $thumb_height . '"';
    if (function_exists('has_post_thumbnail') && (wp_get_attachment_image_src(get_post_thumbnail_id($result->ID)) != '' || wp_get_attachment_image_src(get_post_thumbnail_id($result->ID)) != false)) {
        $postimage = wp_get_attachment_image_src(get_post_thumbnail_id($result->ID));
        if ($postimage[1] < $thumb_width || $postimage[2] < $thumb_height) {
            $postimage = wp_get_attachment_image_src(get_post_thumbnail_id($result->ID), 'full');
        }
        $postimage = apply_filters($filter, $postimage[0], $thumb_width, $thumb_height, $thumb_timthumb);
        $output .= '<img src="' . $postimage . '" alt="' . $title . '" title="' . $title . '" ' . $thumb_html . ' border="0" class="' . $class . '" />';
    } else {
        $postimage = get_post_meta($result->ID, $thumb_meta, true);
        // Check
        if (!$postimage && $scan_images) {
            preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $result->post_content, $matches);
            // any image there?
            if (isset($matches[1][0]) && $matches[1][0]) {
                if (strpos($matches[1][0], parse_url(get_option('home'), PHP_URL_HOST)) !== false && strpos($matches[1][0], 'http://') !== false || strpos($matches[1][0], 'http://') === false) {
                    $postimage = preg_replace('/\\?.*/', '', $matches[1][0]);
                    // we need the first one only!
                }
            }
        }
        if (!$postimage) {
            $postimage = tptn_get_first_image($result->ID);
        }
        // Get the first image
        if (!$postimage) {
            $postimage = get_post_meta($result->ID, '_video_thumbnail', true);
        }
        // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
        if ($thumb_default_show && !$postimage) {
            $postimage = $thumb_default;
        }
        // If no thumb found and settings permit, use default thumb
        if ($postimage) {
            if ($thumb_timthumb) {
                $output .= '<img src="' . $tptn_url . '/timthumb/timthumb.php?src=' . urlencode($postimage) . '&amp;w=' . $thumb_width . '&amp;h=' . $thumb_height . '&amp;zc=1&amp;q=75" alt="' . $title . '" title="' . $title . '" ' . $thumb_html . ' border="0" class="' . $class . '" />';
            } else {
                $output .= '<img src="' . $postimage . '" alt="' . $title . '" title="' . $title . '" ' . $thumb_html . ' border="0" class="' . $class . '" />';
            }
        }
    }
    return $output;
}