WordPress.org

Ready to get started?Download WordPress

Forums

Top 10 - Popular posts plugin for WordPress
[Plugin: Top 10] Patch: Fix max width & height, support for Video Thumbnails, option: no thumb (3 posts)

  1. Daedalon
    Member
    Posted 2 years ago #

    Ajay, please incorporate this patch into the next version of Top 10 :)

    I've been very happy with Top 10 since I patched it to support Video Thumbnails and to make it respect the setting for max width and max height. Top 10 1.7.6 doesn't recognize thumbnails generated by Video Thumbnails and it stretches the images so that they are shown at the specified max width and height. For example with max width 100 and max height 100 a thumbnail of 480 x 360 is stretched to 100 x 100 violating the image dimensions, when it should be shown as 100 x 75 respecting the image dimensions.

    Please jump to the second post from here, that patch incorporates the one included here and adds an option for displaying no image instead of the default when no thumb is found, improves performance and cleans the code slightly.

    Only in case you don't agree with the second post, here's the original one:

    Here's how: in top10.php, find lines 180 and 181:

    if (!$postimage) $postimage = $tptn_settings[thumb_default];
    						$output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" width="'.$tptn_settings[thumb_width].'" height="'.$tptn_settings[thumb_height].'" border="0" class="tptn_thumb" />';

    and replace them with

    if (!$postimage) $postimage = get_post_meta($result->postnumber, '_video_thumbnail', true); // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
    						if (!$postimage) $postimage = $tptn_settings[thumb_default]; // If no thumb found, use default
    						$output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" style="max-width:'.$tptn_settings[thumb_width].'px;max-height:'.$tptn_settings[thumb_height].'px;" border="0" class="tptn_thumb" />';

    After this it's good to also update the comments. Previously from line 317, after the above from 317, find:

    'thumb_height' => '100',			// Height of thumbnails
    						'thumb_width' => '100',			// Width of thumbnails

    and replace it with

    'thumb_height' => '100',			// Max height of thumbnails
    						'thumb_width' => '100',			// Max width of thumbnails

    This patch may need similar changes in some of the .js.php files. At least top-10-daily.js.php replicates the thumbnail function in a way that also requires replicating the first of the above patches.

    http://wordpress.org/extend/plugins/top-10/

  2. Daedalon
    Member
    Posted 2 years ago #

    Here's the more complete patch to 1.7.6. Start with admin.inc.php and after the line 40, which is

    $tptn_settings[scan_images] = (($_POST['scan_images']) ? true : false);

    add the line

    $tptn_settings[thumb_default_show] = (($_POST['thumb_default_show']) ? true : false);

    and find line 273, which used to be 272 and is

    <p><?php _e('The plugin will first check if the post contains a thumbnail. If it doesn\'t then it will check the meta field. If this is not available, then it will show the default image as specified below:',TPTN_LOCAL_NAME); ?>

    and replace it with

    <p><label><input type="checkbox" name="thumb_default_show" id="thumb_default_show" <?php if ($tptn_settings[thumb_default_show]) echo 'checked="checked"' ?> /> <?php _e('If checked, when no thumbnail is found, show a default one from the URL below. If not checked and no thumbnail is found, no image will be shown.',TPTN_LOCAL_NAME); ?></label><br />

    In top10.php, find starting from line 317:

    'thumb_height' => '100',			// Height of thumbnails
    						'thumb_width' => '100',			// Width of thumbnails
    						'thumb_meta' => 'post-image',		// Meta field that is used to store the location of default thumbnail image
    						'thumb_default' => $thumb_default,	// Default thumbnail image
    						'scan_images' => false,			// Scan post for images

    and replace it with

    'thumb_height' => '100',			// Max height of thumbnails
    						'thumb_width' => '100',			// Max width of thumbnails
    						'thumb_meta' => 'post-image',		// Meta field that is used to store the location of default thumbnail image
    						'thumb_default' => $thumb_default,	// Default thumbnail image
    						'scan_images' => false,			// Scan post for images
    						'thumb_default_show' => true, // Show default thumb if none found (if false, don't show thumb at all)

    and find from line 167:

    if (($tptn_settings['post_thumb_op']=='inline')||($tptn_settings['post_thumb_op']=='thumbs_only')) {
    					$output .= '<a href="'.get_permalink($result->postnumber).'" rel="bookmark">';
    					if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail($result->postnumber))) {
    						$output .= get_the_post_thumbnail( $result->postnumber, array($tptn_settings[thumb_width],$tptn_settings[thumb_height]), array('title' => $title,'alt' => $title, 'class' => 'tptn_thumb', 'border' => '0'));
    					} else {
    						$postimage = get_post_meta($result->postnumber, $tptn_settings[thumb_meta], true);	// Check
    						if ((!$postimage)&&($tptn_settings['scan_images'])) {
    							preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $result->post_content, $matches );
    							// any image there?
    							if( isset( $matches ) && $matches[1][0] ) {
    								$postimage = $matches[1][0]; // we need the first one only!
    							}
    						}
    						if (!$postimage) $postimage = $tptn_settings[thumb_default];
    						$output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" width="'.$tptn_settings[thumb_width].'" height="'.$tptn_settings[thumb_height].'" border="0" class="tptn_thumb" />';
    					}
    					$output .= '</a> ';
    				}
    				if (($tptn_settings['post_thumb_op']=='inline')||($tptn_settings['post_thumb_op']=='text_only')) {
    					$output .= '<a href="'.get_permalink($result->postnumber).'" rel="bookmark">'.$title.'</a>';
    				}
    				if ($tptn_settings['show_excerpt']) {
    					$output .= '<span class="tptn_excerpt"> '.tptn_excerpt($result->post_content,$tptn_settings['excerpt_length']).'</span>';
    				}
    				if ($tptn_settings['disp_list_count']) $output .= ' ('.$result->sumCount.')';

    and replace it with

    $output .= '<a href="'.get_permalink($result->postnumber).'" rel="bookmark">'; // Add beginning of link
    				if ($tptn_settings['post_thumb_op']=='inline' || $tptn_settings['post_thumb_op']=='thumbs_only') {
    					if (function_exists('has_post_thumbnail') && has_post_thumbnail($result->postnumber)) {
    						$output .= get_the_post_thumbnail($result->postnumber, array($tptn_settings[thumb_width],$tptn_settings[thumb_height]), array('title' => $title,'alt' => $title, 'class' => 'tptn_thumb', 'border' => '0'));
    					} else {
    						$postimage = get_post_meta($result->postnumber, $tptn_settings[thumb_meta], true);	// Check
    						if (!$postimage && $tptn_settings['scan_images']) {
    							preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $result->post_content, $matches );
    							// any image there?
    							if (isset($matches) && $matches[1][0]) {
    								$postimage = $matches[1][0]; // we need the first one only!
    							}
    						}
    						if (!$postimage) $postimage = get_post_meta($result->postnumber, '_video_thumbnail', true); // If no other thumbnail set, try to get the custom video thumbnail set by the Video Thumbnails plugin
    						if ($tptn_settings['thumb_default_show'] && !$postimage) $postimage = $tptn_settings[thumb_default]; // If no thumb found and settings permit, use default thumb
                if ($postimage) {
                  $output .= '<img src="'.$postimage.'" alt="'.$title.'" title="'.$title.'" style="max-width:'.$tptn_settings[thumb_width].'px;max-height:'.$tptn_settings[thumb_height].'px;" border="0" class="tptn_thumb" />';
                }
    					}
    				}
    				if ($tptn_settings['post_thumb_op']=='inline' || $tptn_settings['post_thumb_op']=='text_only') {
    					$output .= $title; // Add title when required by settings
    				}
    				$output .= '</a>'; // Close the link
    				if ($tptn_settings['show_excerpt']) {
    					$output .= '<span class="tptn_excerpt"> '.tptn_excerpt($result->post_content,$tptn_settings['excerpt_length']).'</span>';
    				}
    				if ($tptn_settings['disp_list_count']) {
              $output .= ' ('.$result->sumCount.')';
            }

    The performance increase is very slight and comes from removing redundant ifs and duplicate HTML output. Now regardless of thumb and title settings, both will be wrapped in one <a>. Also all the ifs in this part of code are now formatted in the same way and redundant brackets have been removed for better readability.

    Functional improvements, as mentioned in the first post, are: fix max width and max height; support Video Thumbnails; add option for displaying no thumbnail when none is found. This patch may need to be replicated to the .js.php files which replicate code, or better yet, the replicated code should be put to its own function.

  3. Ajay
    Member
    Plugin Author

    Posted 2 years ago #

    Thanks for this patch. I am implementing this in v1.8 to be released shortly

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic