WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
Pulling the last image from the post (8 posts)

  1. jd2media
    Member
    Posted 3 years ago #

    Hi,

    I have a weird phenomenon with this plugin. It seems to be pulling the last image in the post instead of the first. I thought this was probably because I am using the Faster Image Insert plugin (http://wordpress.org/extend/plugins/faster-image-insert/), which requires reordering of the images in order to insert multiple images, however, I'm also using Yet Another Related Posts Plugin (http://wordpress.org/extend/plugins/yet-another-related-posts-plugin/) and calling the first image from timthumb and that is working fine.
    So I'm not really sure why it is calling the last image.

    You can see it here:
    http://design-milk.com/hamlet-diagram-poster/

    As you can see in the right column under "This Week's Popular Posts" there are 5 posts with images. If you click on any of those posts you will notice that the image on the sidebar is the last image featured in the post.

    However, if you scroll down and look at the images being shown under "Related Posts" you will see that all of those are pulling the correct images -- the first ones from the post. All of the thumbs are being called from the same place, the timthumb script located in the WordPress Popular Posts plugin file. Why are the ones in the sidebar calling the last images while the other ones are calling the first?

    Thanks for your help!

    Jaime

  2. jd2media
    Member
    Posted 3 years ago #

    FYI here is the code from the YARPP plugin file that is correctly pulling the first image:

    <?php $id =$post->ID;
    					$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
    					$pattern = '!<img.*?src="(.*?)"!';
    					preg_match_all($pattern, $the_content, $matches);
    					$firstimage_src = $matches['1'][0]; ?> 
    
    				<?php if($firstimage_src != '')  : ?>  
    
    					<img src="http://design-milk.com/wp-content/plugins/wordpress-popular-posts/scripts/timthumb.php?src=<?php echo $firstimage_src; ?>&h=100&w=100&zc=1" alt="<?php the_title(); ?>" />
    
    				<?php else : ?>
  3. jd2media
    Member
    Posted 3 years ago #

    Is there a way to edit the WordPress Popular Posts plugin code to pull the same way that the YARPP code file is doing... i.e., $firstimage_src?

  4. jd2media
    Member
    Posted 3 years ago #

    I was investigating this further last night and realized that the plugin is calling the first UPLOADED image not the first image that APPEARS in the post.

    For example:

    http://design-milk.com/a-modern-hen-house-in-portland/ (last image called) - uploaded using Faster Image Insert which changes image order reversed (3,2,1, etc.)

    http://design-milk.com/hamlet-diagram-poster/ (THIRD to last image called) - uploaded using Faster Image Insert which changes image order reversed (3,2,1) then uploaded 2 more images. That's why #3 is being called.

    http://design-milk.com/mickael-de-santos/ (last image called) - uploaded using Faster Image Insert which changes image order reversed (3,2,1)

    http://design-milk.com/with-house-in-japan-by-studioloop-architects/ (FIRST image called) - uploaded using Faster Image Insert which changes image order reversed except THIS TIME the images were ordered backwards so that the first image was first.

    http://design-milk.com/studio801/ (last image called) - uploaded using Faster Image Insert which changes image order reversed (3,2,1, etc.)

    I think that the WordPress Popular Posts plugin should consider calling the image in a different manner so that it always calls the first image that APPEARS in the post, not the first image that gets UPLOADED for the post.

    Thanks!

  5. lindsayanng
    Member
    Posted 3 years ago #

    Can you post the code from the Popular Posts plugin?

  6. lindsayanng
    Member
    Posted 3 years ago #

    Can you post the code from the Popular Posts plugin?

    I just checked and now the sidebar posts plugin is showing "no thumbnail"

  7. jd2media
    Member
    Posted 3 years ago #

    Here is the WPP code. If you clear your cache it should be OK now. We were messing with it.

    <?php
    /*
    Plugin Name: WordPress Popular Posts
    Plugin URI: http://wordpress.org/extend/plugins/wordpress-popular-posts
    Description: Showcases your most popular posts to your visitors on your blog's sidebar. Use WordPress Popular Posts as a widget or place it anywhere on your theme using  <strong><?php wpp_get_mostpopular(); ?></strong>
    Version: 2.1.4
    Author: H&eacute;ctor Cabrera
    Author URI: http://wordpress.org/extend/plugins/wordpress-popular-posts
    License: GPL2
    */
    
    if (basename($_SERVER['SCRIPT_NAME']) == basename(__FILE__)) exit('Please do not load this page directly');
    
    /**
     * Load WordPress Popular Posts to widgets_init.
     * @since 2.0
     */
    add_action('widgets_init', 'load_wpp');
    
    function load_wpp() {
    	register_widget('WordPressPopularPosts');
    }
    
    /**
     * WordPress Popular Posts class.
     */
    
    if ( !class_exists('WordPressPopularPosts') ) {
    	class WordPressPopularPosts extends WP_Widget {
    		// plugin global variables
    		var $version = "2.1.4";
    		var $qTrans = false;
    		var $postRating = false;
    		var $thumb = false;
    		var $pluginDir = "";
    		var $charset = "UTF-8";
    		var $magicquotes = false;
    
    		// constructor
    		function WordPressPopularPosts() {
    			global $wp_version;
    
    			// widget settings
    			$widget_ops = array( 'classname' => 'popular-posts', 'description' => 'The most Popular Posts on your blog.' );
    
    			// widget control settings
    			$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'wpp' );
    
    			// create the widget
    			$this->WP_Widget( 'wpp', 'WordPress Popular Posts', $widget_ops, $control_ops );
    
    			// set plugin path
    			if (empty($this->pluginDir)) $this->pluginDir = WP_PLUGIN_URL . '/wordpress-popular-posts';
    
    			// set charset
    			$this->charset = get_bloginfo('charset');
    
    			// detect PHP magic quotes
    			$this->magicquotes = get_magic_quotes_gpc();
    
    			// add ajax update to wp_ajax_ hook
    			add_action('wp_ajax_nopriv_wpp_update', array(&$this, 'wpp_ajax_update'));
    			add_action('wp_head', array(&$this, 'wpp_print_ajax'));
    
    			// add ajax table truncation to wp_ajax_ hook
    			add_action('wp_ajax_wpp_clear_cache', array(&$this, 'wpp_clear_data'));
    			add_action('wp_ajax_wpp_clear_all', array(&$this, 'wpp_clear_data'));
    
    			// print stylesheet
    			add_action('wp_head', array(&$this, 'wpp_print_stylesheet'));
    
    			// activate textdomain for translations
    			add_action('init', array(&$this, 'wpp_textdomain'));
    
    			// activate maintenance page
    			add_action('admin_menu', array(&$this, 'add_wpp_maintenance_page'));
    
    			// database creation
    			register_activation_hook(__FILE__, $this->wpp_install());
    
    			// cache maintenance schedule
    			register_deactivation_hook(__FILE__, array(&$this, 'wpp_deactivation'));
    			add_action('wpp_cache_event', array(&$this, 'wpp_cache_maintenance'));
    			if (!wp_next_scheduled('wpp_cache_event')) {
    				$tomorrow = time() + 86400;
    				$midnight  = mktime(0, 0, 0,
    					date("m", $tomorrow),
    					date("d", $tomorrow),
    					date("Y", $tomorrow));
    				wp_schedule_event( $midnight, 'daily', 'wpp_cache_event' );
    			}
    
    			// WordPress version check
    			if (version_compare($wp_version, '2.8.0', '<')) add_action('admin_notices', array(&$this, 'wpp_update_warning'));
    
    			// qTrans plugin support
    			if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) $this->qTrans = true;
    
    			// WP-Post Ratings plugin support
    			if (function_exists('the_ratings_results')) $this->postRating = true;
    
    			// Can we create thumbnails?
    			if (extension_loaded('gd') && function_exists('gd_info') && version_compare(phpversion(), '4.3.0', '>=')) $this->thumb = true;
    
    			// shortcode
    			if( function_exists('add_shortcode') ){
    				add_shortcode('wpp', array(&$this, 'wpp_shortcode'));
    				add_shortcode('WPP', array(&$this, 'wpp_shortcode'));
    			}
    
    			// set version
    			$wpp_ver = get_option('wpp_ver');
    			if (!$wp_ver) {
    				add_option('wpp_ver', $this->version);
    			} else if (version_compare($wpp_ver, $this->version, '<')) {
    				update_option('wpp_ver', $this->version);
    			}
    
    			// add stats page
    			add_action('admin_menu', array(&$this, 'wpp_stats'));
    		}
    
    		// builds WordPress Popular Posts' widgets
    		function widget($args, $instance) {
    			extract($args);
    			echo "<!-- WordPress Popular Posts Plugin v". $this->version ." [W] [".$instance['range']."]". (($instance['markup']['custom_html']) ? ' [custom]' : ' [regular]') ." -->"."\n";
    			echo $before_widget . "\n";
    
    			// has user set a title?
    			if ($instance['title'] != '') {
    				if ($instance['markup']['custom_html'] && $instance['markup']['title-start'] != "" && $instance['markup']['title-end'] != "" ) {
    					echo htmlspecialchars_decode($instance['markup']['title-start'], ENT_QUOTES) . $instance['title'], ENT_QUOTES . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES);
    				} else {
    					echo $before_title . $instance['title'] . $after_title;
    				}
    			}
    
    			echo $this->get_popular_posts($instance, false);
    			echo $after_widget . "\n";
    			echo "<!-- End WordPress Popular Posts Plugin v". $this->version ." -->"."\n";
    		}
    
    		// updates each widget instance when user clicks the "save" button
    		function update($new_instance, $old_instance) {
    
    			$instance = $old_instance;
    
    			//$instance['title'] = htmlspecialchars( stripslashes(strip_tags( $new_instance['title'] )), ENT_QUOTES, 'UTF-8', FALSE );
    			$instance['title'] = ($this->magicquotes) ? htmlspecialchars( stripslashes(strip_tags( $new_instance['title'] )), ENT_QUOTES ) : htmlspecialchars( strip_tags( $new_instance['title'] ), ENT_QUOTES );
    			$instance['limit'] = is_numeric($new_instance['limit']) ? $new_instance['limit'] : 10;
    			$instance['range'] = $new_instance['range'];
    			$instance['order_by'] = $new_instance['order_by'];
    			$instance['pages'] = $new_instance['pages'];
    			$instance['shorten_title']['active'] = $new_instance['shorten_title-active'];
    			$instance['shorten_title']['length'] = is_numeric($new_instance['shorten_title-length']) ? $new_instance['shorten_title-length'] : 25;
    			$instance['post-excerpt']['active'] = $new_instance['post-excerpt-active'];
    			$instance['post-excerpt']['length'] = is_numeric($new_instance['post-excerpt-length']) ? $new_instance['post-excerpt-length'] : 55;
    			$instance['post-excerpt']['keep_format'] = $new_instance['post-excerpt-format'];
    			$instance['exclude-cats']['active'] = $new_instance['exclude-cats'];
    			$instance['exclude-cats']['cats'] = empty($new_instance['excluded']) ? '' : (ctype_digit(str_replace(",", "", $new_instance['excluded']))) ? $new_instance['excluded'] : '';
    			if ($this->thumb) { // can create thumbnails
    				$instance['thumbnail']['active'] = $new_instance['thumbnail-active'];
    				$instance['thumbnail']['thumb_selection'] = empty($new_instance['thumb_selection']) ? "wppgenerated" : $new_instance['thumb_selection'];
    				$instance['thumbnail']['width'] = is_numeric($new_instance['thumbnail-width']) ? $new_instance['thumbnail-width'] : 15;
    				$instance['thumbnail']['height'] = is_numeric($new_instance['thumbnail-height']) ? $new_instance['thumbnail-height'] : 15;
    			} else { // cannot create thumbnails
    				$instance['thumbnail']['active'] = false;
    				$instance['thumbnail']['thumb_selection'] = "wppgenerated";
    				$instance['thumbnail']['width'] = 15;
    				$instance['thumbnail']['height'] = 15;
    			}
    
    			$instance['rating'] = $new_instance['rating'];
    			$instance['stats_tag']['comment_count'] = $new_instance['comment_count'];
    			$instance['stats_tag']['views'] = $new_instance['views'];
    			$instance['stats_tag']['author'] = $new_instance['author'];
    			$instance['stats_tag']['date']['active'] = $new_instance['date'];
    			$instance['stats_tag']['date']['format'] = empty($new_instance['date_format']) ? 'F j, Y' : $new_instance['date_format'];
    			$instance['markup']['custom_html'] = $new_instance['custom_html'];
    			$instance['markup']['wpp-start'] = empty($new_instance['wpp-start']) ? '<ul>' : htmlspecialchars( $new_instance['wpp-start'], ENT_QUOTES );
    			$instance['markup']['wpp-end'] = empty($new_instance['wpp-end']) ? '</ul>' : htmlspecialchars( $new_instance['wpp-end'], ENT_QUOTES );
    			$instance['markup']['post-start'] = empty ($new_instance['post-start']) ? '<li>' : htmlspecialchars( $new_instance['post-start'], ENT_QUOTES );
    			$instance['markup']['post-end'] = empty ($new_instance['post-end']) ? '</li>' : htmlspecialchars( $new_instance['post-end'], ENT_QUOTES );
    			$instance['markup']['title-start'] = empty($new_instance['title-start']) ? '' : htmlspecialchars( $new_instance['title-start'], ENT_QUOTES );
    			$instance['markup']['title-end'] = empty($new_instance['title-end']) ? '' : htmlspecialchars( $new_instance['title-end'], ENT_QUOTES );
    			$instance['markup']['pattern']['active'] = $new_instance['pattern_active'];
    			$instance['markup']['pattern']['form'] = empty($new_instance['pattern_form']) ? '{image} {title}: {summary} {stats}' : strip_tags( $new_instance['pattern_form'] );
    
    			return $instance;
    		}
    
    		// widget's form
    		function form($instance) {
    			// set default values
    			$defaults = array(
    				'title' => __('Popular Posts', 'wordpress-popular-posts'),
    				'limit' => 10,
    				'range' => 'daily',
    				'order_by' => 'comments',
    				'pages' => true,
    				'shorten_title' => array(
    					'active' => false,
    					'length' => 25,
    					'keep_format' => false
    				),
    				'post-excerpt' => array(
    					'active' => false,
    					'length' => 55
    				),
    				'exclude-cats' => array(
    					'active' => false,
    					'cats' => ''
    				),
    				'thumbnail' => array(
    					'active' => false,
    					'width' => 15,
    					'height' => 15
    				),
    				'rating' => false,
    				'stats_tag' => array(
    					'comment_count' => true,
    					'views' => false,
    					'author' => false,
    					'date' => array(
    						'active' => false,
    						'format' => 'F j, Y'
    					)
    				),
    				'markup' => array(
    					'custom_html' => false,
    					'wpp-start' => '<ul>',
    					'wpp-end' => '</ul>',
    					'post-start' => '<li>',
    					'post-end' => '</li>',
    					'title-start' => '<h2>',
    					'title-end' => '</h2>',
    					'pattern' => array(
    						'active' => false,
    						'form' => '{image} {title}: {summary} {stats}'
    					)
    				)
    			);
    
    			// update instance's default options
    			$instance = wp_parse_args( (array) $instance, $defaults );
    
    			// form
    			?>
                <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wordpress-popular-posts'); ?></label>
                <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" /></p>
                <p><label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e('Show up to:', 'wordpress-popular-posts'); ?></label><br />
                <input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" value="<?php echo $instance['limit']; ?>"  class="widefat" style="width:50px!important" /> <?php _e('posts', 'wordpress-popular-posts'); ?></p>
                <p><label for="<?php echo $this->get_field_id( 'range' ); ?>"><?php _e('Time Range:', 'wordpress-popular-posts'); ?></label>
                <select id="<?php echo $this->get_field_id( 'range' ); ?>" name="<?php echo $this->get_field_name( 'range' ); ?>" class="widefat">
                	<option value="daily" <?php if ( 'daily' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Today', 'wordpress-popular-posts'); ?></option>
                    <option value="weekly" <?php if ( 'weekly' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Last 7 days', 'wordpress-popular-posts'); ?></option>
                    <option value="monthly" <?php if ( 'monthly' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('Last 30 days', 'wordpress-popular-posts'); ?></option>
                    <option value="all" <?php if ( 'all' == $instance['range'] ) echo 'selected="selected"'; ?>><?php _e('All-time', 'wordpress-popular-posts'); ?></option>
                </select>
                </p>
                <p><label for="<?php echo $this->get_field_id( 'order_by' ); ?>"><?php _e('Sort posts by:', 'wordpress-popular-posts'); ?></label>
                <select id="<?php echo $this->get_field_id( 'order_by' ); ?>" name="<?php echo $this->get_field_name( 'order_by' ); ?>" class="widefat">
                	<option value="comments" <?php if ( 'comments' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Comments', 'wordpress-popular-posts'); ?></option>
                    <option value="views" <?php if ( 'views' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Total views', 'wordpress-popular-posts'); ?></option>
                    <option value="avg" <?php if ( 'avg' == $instance['order_by'] ) echo 'selected="selected"'; ?>><?php _e('Avg. daily views', 'wordpress-popular-posts'); ?></option>
                </select>
                </p>
                <input type="checkbox" class="checkbox" <?php echo ($instance['pages']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'pages' ); ?>" name="<?php echo $this->get_field_name( 'pages' ); ?>" /> <label for="<?php echo $this->get_field_id( 'pages' ); ?>"><?php _e('Include pages', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                <?php if ($this->postRating) : ?>
                <input type="checkbox" class="checkbox" <?php echo ($instance['rating']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'rating' ); ?>" name="<?php echo $this->get_field_name( 'rating' ); ?>" /> <label for="<?php echo $this->get_field_id( 'rating' ); ?>"><?php _e('Display post rating', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                <?php endif; ?>
                <input type="checkbox" class="checkbox" <?php echo ($instance['shorten_title']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'shorten_title-active' ); ?>" name="<?php echo $this->get_field_name( 'shorten_title-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'shorten_title-active' ); ?>"><?php _e('Shorten title output', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                <?php if ($instance['shorten_title']['active']) : ?>
                <label for="<?php echo $this->get_field_id( 'shorten_title-length' ); ?>"><?php _e('Shorten title to', 'wordpress-popular-posts'); ?> <input id="<?php echo $this->get_field_id( 'shorten_title-length' ); ?>" name="<?php echo $this->get_field_name( 'shorten_title-length' ); ?>" value="<?php echo $instance['shorten_title']['length']; ?>" class="widefat" style="width:50px!important" /> <?php _e('characters', 'wordpress-popular-posts'); ?></label><br /><br />
    			<?php endif; ?>
                <input type="checkbox" class="checkbox" <?php echo ($instance['post-excerpt']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'post-excerpt-active' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'post-excerpt-active' ); ?>"><?php _e('Display post excerpt', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                <?php if ($instance['post-excerpt']['active']) : ?>
                <fieldset class="widefat">
                    <legend><?php _e('Excerpt Properties', 'wordpress-popular-posts'); ?></legend>
                	&nbsp;&nbsp;<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'post-excerpt-format' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-format' ); ?>" <?php echo ($instance['post-excerpt']['keep_format']) ? 'checked="checked"' : ''; ?> /> <label for="<?php echo $this->get_field_id( 'post-excerpt-format' ); ?>"><?php _e('Keep text format and links', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                	&nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'post-excerpt-length' ); ?>"><?php _e('Excerpt length:', 'wordpress-popular-posts'); ?> <input id="<?php echo $this->get_field_id( 'post-excerpt-length' ); ?>" name="<?php echo $this->get_field_name( 'post-excerpt-length' ); ?>" value="<?php echo $instance['post-excerpt']['length']; ?>" class="widefat" style="width:30px!important" /> <?php _e('characters', 'wordpress-popular-posts'); ?></label>
    			</fieldset>
    			<br />
                <?php endif; ?>
                <input type="checkbox" class="checkbox" <?php echo ($instance['exclude-cats']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'exclude-cats' ); ?>" name="<?php echo $this->get_field_name( 'exclude-cats' ); ?>" /> <label for="<?php echo $this->get_field_id( 'exclude-cats' ); ?>"><?php _e('Exclude categories', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                <?php if ($instance['exclude-cats']['active']) : ?>
                <fieldset class="widefat">
                    <legend><?php _e('Categories to exclude', 'wordpress-popular-posts'); ?></legend>
                    &nbsp;&nbsp;<label for="<?php echo $this->get_field_id( 'excluded' ); ?>"><?php _e('ID(s) (comma separated, no spaces):', 'wordpress-popular-posts'); ?></label><br />&nbsp;&nbsp;<input id="<?php echo $this->get_field_id( 'excluded' ); ?>" name="<?php echo $this->get_field_name( 'excluded' ); ?>" value="<?php echo $instance['exclude-cats']['cats']; ?>" class="widefat" style="width:150px" /><br /><br />
                </fieldset>
                <?php endif; ?>
                <br />
    
    			<fieldset style="width:214px; padding:5px;"  class="widefat">
                    <legend><?php _e('Thumbnail settings', 'wordpress-popular-posts'); ?></legend>
    				<input type="checkbox" class="checkbox" <?php echo ($instance['thumbnail']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'thumbnail-active' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'thumbnail-active' ); ?>"><?php _e('Display post thumbnail', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
    				<?php if($instance['thumbnail']['active']) : ?>
    
    				<input type="radio" name="<?php echo $this->get_field_name( 'thumb_selection' ); ?>" value="wppgenerated" <?php if ( 'wppgenerated' == $instance['thumbnail']['thumb_selection'] ) echo 'checked="checked"'; ?>> <label for="<?php echo $this->get_field_id( 'thumb_selection' ); ?>"><?php _e('Generate all thumbnails for me', 'wordpress-popular-posts'); ?></label><br />
    				<input type="radio" name="<?php echo $this->get_field_name( 'thumb_selection' ); ?>" value="usergenerated" <?php if ( 'usergenerated' == $instance['thumbnail']['thumb_selection']) { echo 'checked="checked"'; } if (!function_exists('get_the_post_thumbnail')) { echo 'disabled="disabled"'; } ?>>  <label for="<?php echo $this->get_field_id( 'thumb_selection' ); ?>"><?php _e('Use thumbnails selected by me', 'wordpress-popular-posts'); ?></label>				
    
    				<label for="<?php echo $this->get_field_id( 'thumbnail-width' ); ?>"><?php _e('Width:', 'wordpress-popular-posts'); ?></label>
    				<input id="<?php echo $this->get_field_id( 'thumbnail-width' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-width' ); ?>" value="<?php echo $instance['thumbnail']['width']; ?>"  class="widefat" style="width:30px!important" <?php echo ($this->thumb) ? '' : 'disabled="disabled"' ?> /> <?php _e('px', 'wordpress-popular-posts'); ?> <br />
    				<label for="<?php echo $this->get_field_id( 'thumbnail-height' ); ?>"><?php _e('Height:', 'wordpress-popular-posts'); ?></label>
    				<input id="<?php echo $this->get_field_id( 'thumbnail-height' ); ?>" name="<?php echo $this->get_field_name( 'thumbnail-height' ); ?>" value="<?php echo $instance['thumbnail']['height']; ?>"  class="widefat" style="width:30px!important" <?php echo ($this->thumb) ? '' : 'disabled="disabled"' ?> /> <?php _e('px', 'wordpress-popular-posts'); ?><br />
    
    				<?php endif; ?>
    			</fieldset>
    
                <br />
                <fieldset style="width:214px; padding:5px;"  class="widefat">
                    <legend><?php _e('Stats Tag settings', 'wordpress-popular-posts'); ?></legend>
                    <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['comment_count']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'comment_count' ); ?>" name="<?php echo $this->get_field_name( 'comment_count' ); ?>" /> <label for="<?php echo $this->get_field_id( 'comment_count' ); ?>"><?php _e('Display comment count', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                    <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['views']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'views' ); ?>" name="<?php echo $this->get_field_name( 'views' ); ?>" /> <label for="<?php echo $this->get_field_id( 'views' ); ?>"><?php _e('Display views', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                    <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['author']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" /> <label for="<?php echo $this->get_field_id( 'author' ); ?>"><?php _e('Display author', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                    <input type="checkbox" class="checkbox" <?php echo ($instance['stats_tag']['date']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" /> <label for="<?php echo $this->get_field_id( 'date' ); ?>"><?php _e('Display date', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small>
    				<?php if ($instance['stats_tag']['date']['active']) : ?>
                    	<fieldset class="widefat">
                        	<legend><?php _e('Date Format', 'wordpress-popular-posts'); ?></legend>
                            <label title='F j, Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='F j, Y' <?php echo ($instance['stats_tag']['date']['format'] == 'F j, Y') ? 'checked="checked"' : ''; ?> /><?php echo date('F j, Y', time()); ?></label><br />
                            <label title='Y/m/d'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='Y/m/d' <?php echo ($instance['stats_tag']['date']['format'] == 'Y/m/d') ? 'checked="checked"' : ''; ?> /><?php echo date('Y/m/d', time()); ?></label><br />
                            <label title='m/d/Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='m/d/Y' <?php echo ($instance['stats_tag']['date']['format'] == 'm/d/Y') ? 'checked="checked"' : ''; ?> /><?php echo date('m/d/Y', time()); ?></label><br />
                            <label title='d/m/Y'><input type='radio' name='<?php echo $this->get_field_name( 'date_format' ); ?>' value='d/m/Y' <?php echo ($instance['stats_tag']['date']['format'] == 'd/m/Y') ? 'checked="checked"' : ''; ?> /><?php echo date('d/m/Y', time()); ?></label><br />
                        </fieldset>
                    <?php endif; ?>
                </fieldset>
                <br />
    
                <fieldset style="width:214px; padding:5px;"  class="widefat">
                    <legend><?php _e('HTML Markup settings', 'wordpress-popular-posts'); ?></legend>
                    <input type="checkbox" class="checkbox" <?php echo ($instance['markup']['custom_html']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'custom_html' ); ?>" name="<?php echo $this->get_field_name( 'custom_html' ); ?>" /> <label for="<?php echo $this->get_field_id( 'custom_html' ); ?>"><?php _e('Use custom HTML Markup', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                    <?php if ($instance['markup']['custom_html']) : ?>
                    <br />
                    <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'title-start' ); ?>"><?php _e('Before / after title:', 'wordpress-popular-posts'); ?></label> <br />
                    <input type="text" id="<?php echo $this->get_field_id( 'title-start' ); ?>" name="<?php echo $this->get_field_name( 'title-start' ); ?>" value="<?php echo $instance['markup']['title-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'title-end' ); ?>" name="<?php echo $this->get_field_name( 'title-end' ); ?>" value="<?php echo $instance['markup']['title-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
                    <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'wpp_start' ); ?>"><?php _e('Before / after Popular Posts:', 'wordpress-popular-posts'); ?></label> <br />
                    <input type="text" id="<?php echo $this->get_field_id( 'wpp-start' ); ?>" name="<?php echo $this->get_field_name( 'wpp-start' ); ?>" value="<?php echo $instance['markup']['wpp-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'wpp-end' ); ?>" name="<?php echo $this->get_field_name( 'wpp-end' ); ?>" value="<?php echo $instance['markup']['wpp-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
                    <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'post-start' ); ?>"><?php _e('Before / after each post:', 'wordpress-popular-posts'); ?></label> <br />
                    <input type="text" id="<?php echo $this->get_field_id( 'post-start' ); ?>" name="<?php echo $this->get_field_name( 'post-start' ); ?>" value="<?php echo $instance['markup']['post-start']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /> <input type="text" id="<?php echo $this->get_field_id( 'post-end' ); ?>" name="<?php echo $this->get_field_name( 'post-end' ); ?>" value="<?php echo $instance['markup']['post-end']; ?>" class="widefat" style="width:80px!important" <?php echo ($instance['markup']['custom_html']) ? '' : 'disabled="disabled"' ?> /></p>
                    <hr />
                    <?php endif; ?>
                    <input type="checkbox" class="checkbox" <?php echo ($instance['markup']['pattern']['active']) ? 'checked="checked"' : ''; ?> id="<?php echo $this->get_field_id( 'pattern_active' ); ?>" name="<?php echo $this->get_field_name( 'pattern_active' ); ?>" /> <label for="<?php echo $this->get_field_id( 'pattern_active' ); ?>"><?php _e('Use content formatting tags', 'wordpress-popular-posts'); ?></label> <small>[<a href="<?php echo bloginfo('url'); ?>/wp-admin/options-general.php?page=wordpress-popular-posts/wordpress-popular-posts.php">?</a>]</small><br />
                    <?php if ($instance['markup']['pattern']['active']) : ?>
                    <br />
                    <p style="font-size:11px"><label for="<?php echo $this->get_field_id( 'pattern_form' ); ?>"><?php _e('Content format:', 'wordpress-popular-posts'); ?></label>
                    <input type="text" id="<?php echo $this->get_field_id( 'pattern_form' ); ?>" name="<?php echo $this->get_field_name( 'pattern_form' ); ?>" value="<?php echo $instance['markup']['pattern']['form']; ?>" style="width:204px" <?php echo ($instance['markup']['pattern']['active']) ? '' : 'disabled="disabled"' ?> /></p>
                    <?php endif; ?>
                </fieldset>
                <?php // end form
    		}
    
    		// updates popular posts data table
    		function wpp_ajax_update() {
    			$nonce = $_POST['token'];
    
    			// is this a valid request?
    			if (! wp_verify_nonce($nonce, 'wpp-token') ) die("Oops!");
    
    			if (is_numeric($_POST['id']) && (intval($_POST['id']) == floatval($_POST['id'])) && ($_POST['id'] != '')) {
    				$id = $_POST['id'];
    			} else {
    				die("Invalid ID");
    			}
    
    			// if we got an ID, let's update the data table
    
    			global $wpdb;
    
    			$wpdb->show_errors();
    
    			$table = $wpdb->prefix . 'popularpostsdata';
    
    			// update popularpostsdata table
    			$exists = $wpdb->get_results("SELECT postid FROM $table WHERE postid = '$id'");
    			if ($exists) {
    				$result = $wpdb->query("UPDATE $table SET last_viewed = NOW(), pageviews = pageviews + 1 WHERE postid = '$id'");
    			} else {
    				$result = $wpdb->query("INSERT INTO $table (postid, day, last_viewed) VALUES ('".$id."', NOW(), NOW())");
    			}
    
    			// update popularpostsdatacache table
    			$isincache = $wpdb->get_results("SELECT id FROM ".$table."cache WHERE id = '".$id."' AND day = CURDATE()");
    			if ($isincache) {
    				$result2 = $wpdb->query("UPDATE ".$table."cache SET pageviews = pageviews + 1 WHERE id = '".$id."' AND day = CURDATE()");
    			} else {
    				$result2 = $wpdb->query("INSERT INTO ".$table."cache (id, day) VALUES ('".$id."', CURDATE())");
    			}
    
    			if (($result == 1) && ($result2 == 1)) {
    				die("OK");
    			} else {
    				die($wpdb->print_error);
    			}		
    
    		}
    
    		// clears WordPress Popular Posts' data
    		function wpp_clear_data() {
    			$token = $_POST['token'];
    			$clear = isset($_POST['clear']) ? $_POST['clear'] : '';
    			$key = get_option("wpp_rand");
    
    			if (current_user_can('manage_options') && ($token === $key) && !empty($clear)) {
    				global $wpdb;
    				// set table name
    				$table = $wpdb->prefix . "popularpostsdata";
    				$cache = $wpdb->prefix . "popularpostsdatacache";
    
    				if ($clear == 'cache') {
    					if ( $wpdb->get_var("SHOW TABLES LIKE '$cache'") == $cache ) {
    						$wpdb->query("TRUNCATE TABLE $cache;");
    						_e('Success! The cache table has been cleared!', 'wordpress-popular-posts');
    					} else {
    						_e('Error: cache table does not exist.', 'wordpress-popular-posts');
    					}
    				} else if ($clear == 'all') {
    					if ( $wpdb->get_var("SHOW TABLES LIKE '$table'") == $table && $wpdb->get_var("SHOW TABLES LIKE '$cache'") == $cache ) {
    						$wpdb->query("TRUNCATE TABLE $table;");
    						$wpdb->query("TRUNCATE TABLE $cache;");
    						_e('Success! All data have been cleared!', 'wordpress-popular-posts');
    					} else {
    						_e('Error: one or both data tables are missing.', 'wordpress-popular-posts');
    					}
    				} else {
    					_e('Invalid action.', 'wordpress-popular-posts');
    				}
    			} else {
    				_e('Sorry, you do not have enough permissions to do this. Please contact the site administrator for support.', 'wordpress-popular-posts');
    			}
    
    			die();
    		}
    
    		// database install
    		function wpp_install() {
    			global $wpdb;
    
    			// set table name
    			$table = $wpdb->prefix . "popularpostsdata";
    
    			// does popularpostsdata table exists?
    			if ( $wpdb->get_var("SHOW TABLES LIKE '$table'") != $table ) { // fresh setup
    				// create tables popularpostsdata and popularpostsdatacache
    				$sql = "CREATE TABLE " . $table . " ( UNIQUE KEY id (postid), postid int(10) NOT NULL, day datetime NOT NULL default '0000-00-00 00:00:00', last_viewed datetime NOT NULL default '0000-00-00 00:00:00', pageviews int(10) default 1 ); CREATE TABLE " . $table ."cache ( UNIQUE KEY id (id, day), id int(10) NOT NULL, day date NOT NULL, pageviews int(10) default 1 );";
    
    				require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    				dbDelta($sql);
    			} else {
    				$cache = $table . "cache";
    				if ( $wpdb->get_var("SHOW TABLES LIKE '$cache'") != $cache ) {
    					// someone is upgrading from version 1.5.x
    					$sql = "CREATE TABLE " . $table ."cache ( UNIQUE KEY id (id, day), id int(10) NOT NULL, day date NOT NULL, pageviews int(10) default 1 );";
    
    					require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    					dbDelta($sql);
    				}
    			}
    		}
    
    		// prints ajax script to theme's header
    		function wpp_print_ajax() {
    			// let's add jQuery
    			wp_print_scripts('jquery');
    
    			// create security token
    			$nonce = wp_create_nonce('wpp-token');
    
    			// get current post's ID
    			global $wp_query;
    			wp_reset_query();
    
    			// if we're on a page or post, load the script
    			if ( (is_single() || is_page()) && !is_user_logged_in() ) {
    				$id = $wp_query->post->ID;
    			?>
    <!-- WordPress Popular Posts v<?php echo $this->version; ?> -->
    <script type="text/javascript" charset="utf-8">
        /* <![CDATA[ */
    	jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', {action: 'wpp_update', token: '<?php echo $nonce; ?>', id: <?php echo $id; ?>});
        /* ]]> */
    </script>
    <!-- End WordPress Popular Posts v<?php echo $this->version; ?> -->
                <?php
    			}
    		}		
    
    		// prints popular posts
    		function get_popular_posts($instance, $echo = true) {		
    
    			global $wpdb;
    			$table = $wpdb->prefix . "popularpostsdata";
    
    			if ( $instance['pages'] ) {
    				$nopages = '';
    			} else {
    				$nopages = "AND $wpdb->posts.post_type = 'post'";
    			}
    
    			switch( $instance['range'] ) {
    				case 'all':
    					$range = "post_date_gmt < '".gmdate("Y-m-d H:i:s")."'";
    					break;
    				case 'yesterday':
    					$range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 1 DAY";
    					break;
    				case 'daily':
    					$range = $table."cache.day = CURDATE()";
    					break;
    				case 'weekly':
    					$range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 7 DAY";
    					break;
    				case 'monthly':
    					$range = $table."cache.day >= '".gmdate("Y-m-d")."' - INTERVAL 30 DAY";
    					break;
    				default:
    					$range = "post_date_gmt < '".gmdate("Y-m-d H:i:s")."'";
    					break;
    			}
    
    			// sorting options
    			switch( $instance['order_by'] ) {
    				case 'comments':
    					$sortby = 'comment_count';
    					break;
    				case 'views':
    					$sortby = 'pageviews';
    					break;
    				case 'avg':
    					$sortby = 'avg_views';
    					break;
    				default:
    					$sortby = 'comment_count';
    					break;
    			}
    
    			// dynamic query fields
    			$fields = ', ';
    			if ( $instance['stats_tag']['views'] || ($sortby != 'comment_count') ) {
    				if ( $instance['range'] == 'all') {
    					$fields .= "$table.pageviews AS 'pageviews' ";
    				} else {
    					if ( $sortby == 'avg_views' ) {
    						$fields .= "(SUM(".$table."cache.pageviews)/(IF ( DATEDIFF(CURDATE(), MIN(".$table."cache.day)) > 0, DATEDIFF(CURDATE(), MIN(".$table."cache.day)), 1) )) AS 'avg_views' ";
    					} else {
    						$fields .= "(SUM(".$table."cache.pageviews)) AS 'pageviews' ";
    					}
    				}
    			}
    
    			if ( $instance['stats_tag']['comment_count'] ) {
    				if ( $fields != ', ' ) {
    					$fields .= ", $wpdb->posts.comment_count AS 'comment_count' ";
    				} else {
    					$fields .= "$wpdb->posts.comment_count AS 'comment_count' ";
    				}
    			}
    
    			if ( $instance['stats_tag']['author'] ) {
    				if ( $fields != ', ' ) {
    					$fields .= ", (SELECT $wpdb->users.display_name FROM $wpdb->users WHERE $wpdb->users.ID = $wpdb->posts.post_author ) AS 'display_name'";
    				} else {
    					$fields .= "(SELECT $wpdb->users.display_name FROM $wpdb->users WHERE $wpdb->users.ID = $wpdb->posts.post_author ) AS 'display_name'";
    				}
    			}
    			if ( $instance['stats_tag']['date']['active'] ) {
    				if ( $fields != ', ' ) {
    					$fields .= ", $wpdb->posts.post_date_gmt AS 'date_gmt'";
    				} else {
    					$fields .= "$wpdb->posts.post_date_gmt AS 'date_gmt'";
    				}
    			}			
    
    			if (strlen($fields) == 2) $fields = '';
    
    			if ( $instance['range'] == 'all') {
    				$join = "LEFT JOIN $table ON $wpdb->posts.ID = $table.postid";
    				$force_pv = "AND ".$table.".pageviews > 0 ";
    			} else {
    				$join = "RIGHT JOIN ".$table."cache ON $wpdb->posts.ID = ".$table."cache.id";
    				$force_pv = "";
    			}
    
    			// Category excluding snippet suggested by user raamdev at http://wordpress.org/support/topic/397885
    			// Thanks, raamdev!
    			if ( $instance['exclude-cats']['active'] && !empty($instance['exclude-cats']['cats']) ) {
    				$exclude = " AND $wpdb->posts.ID NOT IN (
    
    							SELECT  object_id
    							FROM    $wpdb->term_relationships AS r
    									JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
    									JOIN $wpdb->terms AS t ON t.term_id = x.term_id
    							WHERE   x.taxonomy = 'category'
    									AND object_id IN
    									   (
    										SELECT object_id
    										FROM $wpdb->term_relationships AS r
    										JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
    										JOIN $wpdb->terms AS t ON t.term_id = x.term_id
    										WHERE   x.taxonomy = 'category'
    										AND t.term_id IN  (".$instance['exclude-cats']['cats']."))) ";
    			} else {
    				$exclude = "";
    			}
    
    			$mostpopular = $wpdb->get_results("SELECT $wpdb->posts.ID, $wpdb->posts.post_title $fields FROM $wpdb->posts $join WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_password = '' AND $range $force_pv $nopages $exclude GROUP BY $wpdb->posts.ID ORDER BY $sortby DESC LIMIT " . $instance['limit'] . "");
    
    			$content = '';
    
    			if ( !is_array($mostpopular) || empty($mostpopular) ) {
    				$content .= "<p>".__('Sorry. No data so far.', 'wordpress-popular-posts')."</p>"."\n";
    			} else {
    
    				if ($instance['markup']['custom_html']) {
    					$content .= htmlspecialchars_decode($instance['markup']['wpp-start'], ENT_QUOTES) ."\n";
    				} else {
    					$content .= "<ul>" . "\n";
    				}
    
    				foreach ($mostpopular as $wppost) {					
    
    					$post_stats = "";
    					$stats = "";
    					$thumb = "";
    					$data = array();
    
    					// get post title
    					/* qTranslate integration check */
    					($this->qTrans) ? $tit = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($wppost->post_title) : $tit = $wppost->post_title;
    
    					$tit = ($this->magicquotes) ? stripslashes($tit) : $tit;
    					$title_attr = htmlentities($tit, ENT_QUOTES, $this->charset);
    
    					if ( $instance['shorten_title']['active'] && (strlen($tit) > $instance['shorten_title']['length'])) {
    						$tit = mb_substr($tit, 0, $instance['shorten_title']['length'], $this->charset) . "...";
    					}
    
    					$tit = htmlentities($tit, ENT_QUOTES, $this->charset);
    
    					// get post excerpt
    					if ( $instance['post-excerpt']['active'] ) {
    						if ($instance['markup']['pattern']['active']) {
    							$post_content = "<span class=\"wpp-excerpt\">" . $this->get_summary($wppost->ID, $instance) . "</span>";
    						} else {
    							$post_content = ": <span class=\"wpp-excerpt\">" . $this->get_summary($wppost->ID, $instance) . "...</span>";
    						}
    					} else {
    						$post_content = "";
    					}
    
    					// build stats tag
    					if ( $instance['stats_tag']['comment_count'] ) {
    						$comment_count = (int) $wppost->comment_count;
    						$post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment(s)', 'wordpress-popular-posts') . "</span>";
    					}
    					if ( $instance['stats_tag']['views'] ) {
    						$views_text = __(' view(s)', 'wordpress-popular-posts');
    						if ($instance['order_by'] == 'views') {
    							$pageviews = (int) $wppost->pageviews;
    						} else if ($instance['order_by'] == 'avg') {
    							$pageviews = ceil($wppost->avg_views);
    							if ($instance['range'] != 'daily') $views_text = __(' view(s) per day', 'wordpress-popular-posts');
    						} else {
    							$pageviews = (int) $wppost->pageviews;
    						}			
    
    						if ($post_stats != "") {
    							$post_stats .= " | <span class=\"wpp-views\">$pageviews $views_text</span>";
    						} else {
    							$post_stats .= "<span class=\"wpp-views\">$pageviews $views_text</span>";
    						}
    					}
    					if ( $instance['stats_tag']['author'] ) {
    						if ($post_stats != "") {
    							$post_stats .= " | ".__('by', 'wordpress-popular-posts')." <span class=\"wpp-author\">".$wppost->display_name."</span>";
    						} else {
    							$post_stats .= __('by', 'wordpress-popular-posts')." <span class=\"wpp-author\">".$wppost->display_name."</span>";
    						}
    					}
    					if ( $instance['stats_tag']['date']['active'] ) {
    						if ($post_stats != "") {
    							$post_stats .= " | <span class=\"wpp-date\">".__('posted on', 'wordpress-popular-posts')." ".date($instance['stats_tag']['date']['format'], strtotime($wppost->date_gmt))."</span>";
    						} else {
    							$post_stats .= "<span class=\"wpp-date\">".__('posted on', 'wordpress-popular-posts')." ".date($instance['stats_tag']['date']['format'], strtotime($wppost->date_gmt))."</span>";
    						}
    					}
    
    					if (!empty($post_stats)) {
    						$stats = ' <span class="post-stats">' . $post_stats . '</span> ';
    					}
    
    					// get thumbnail
    					if ($instance['thumbnail']['active'] && $this->thumb ) {
    						$tbWidth = $instance['thumbnail']['width'];
    						$tbHeight = $instance['thumbnail']['height'];
    
    						// default image
    						$thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppnothumb\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/no_thumb.jpg\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
    
    						// let's try to retrieve the post thumbnail!
    						if ($instance['thumbnail']['thumb_selection'] == "usergenerated") { // use thumbnail selected by user
    							if (function_exists('get_the_post_thumbnail') && has_post_thumbnail( $wppost->ID )) {
    								$thumb = "<a href=\"".get_permalink($wppost->ID)."\" title=\"". $title_attr ."\">" . get_the_post_thumbnail($wppost->ID, array($tbWidth), array('class' => 'wpp-thumbnail', 'alt' => $title_attr, 'title' => $title_attr) ) ."</a> <!-- $tbWidth $tbHeight-->";
    							}
    						} else if ($instance['thumbnail']['thumb_selection'] == "wppgenerated") { // WordPress Popular Posts should attempt to create a thumbnail by itself
    							$img = $this->get_img($wppost->ID);
    							if ( ($img && !empty($img)) ) {
    								$thumb = "<a href=\"".get_permalink($wppost->ID)."\" class=\"wppgen\" title=\"". $title_attr ."\"><img src=\"". $this->pluginDir . "/scripts/timthumb.php?src=". $img[1] ."&h=".$tbHeight."&w=".$tbWidth."&zc=1\" alt=\"".$title_attr."\" border=\"0\" class=\"wpp-thumbnail\" width=\"".$tbWidth."\" height=\"".$tbHeight."\" "."/></a>";
    							}
    						}
    					}
    
    					// get rating
    					if ($instance['rating'] && $this->postRating) {
    						$rating = '<span class="wpp-rating">'.the_ratings_results($wppost->ID).'</span>';
    					} else {
    						$rating = '';
    					}
    					$data = array(
    						'title' => '<a href="'.get_permalink($wppost->ID).'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>',
    						'summary' => $post_content,
    						'stats' => $stats,
    						'img' => $thumb,
    						'id' => $wppost->ID
    					);		
    
    					// build custom layout
    					if ($instance['markup']['custom_html']) {
    						if ($instance['markup']['pattern']['active']) {
    							$content .= htmlspecialchars_decode($instance['markup']['post-start'], ENT_QUOTES) . $this->format_content($instance['markup']['pattern']['form'], $data, $instance['rating']) . htmlspecialchars_decode($instance['markup']['post-end'], ENT_QUOTES) . "\n";
    						} else {
    							$content .= htmlspecialchars_decode($instance['markup']['post-start'], ENT_QUOTES) . $thumb . '<a href="'.get_permalink($wppost->ID).'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>'.$post_content.' '. $stats . $rating . htmlspecialchars_decode($instance['markup']['post-end'], ENT_QUOTES) . "\n";
    						}
    					} else {
    						$content .= '<li>'. $thumb .'<a href="'. get_permalink($wppost->ID) .'" title="'. $title_attr .'"><span class="wpp-post-title">'. $tit .'</span></a>'. $post_content .' '. $stats . $rating .'</li>' . "\n";
    					}
    				}			
    
    				if ($instance['markup']['custom_html']) {
    					$content .= htmlspecialchars_decode($instance['markup']['wpp-end'], ENT_QUOTES) ."\n";
    				} else {
    					$content .= "\n"."</ul>"."\n";
    				}
    
    			}
    
    			if ($echo) { echo "<noscript>" . $content . "</noscript>"; } else { return $content; }
    		}		
    
    		// builds posts' excerpt
    		function get_summary($id, $instance){
    			if (!is_numeric($id)) return false;
    			global $wpdb;
    			$excerpt = "";
    			$result = "";
    
    			$result = $wpdb->get_results("SELECT post_excerpt FROM $wpdb->posts WHERE ID = " . $id, ARRAY_A);
    
    			if (empty($result[0]['post_excerpt'])) {
    				// no custom excerpt defined, how lazy of you!
    				$result = $wpdb->get_results("SELECT post_content FROM $wpdb->posts WHERE ID = " . $id, ARRAY_A);
    				$excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $result[0]['post_content']);
    			} else {
    				// user has defined a custom excerpt, yay!
    				$excerpt = preg_replace("/\[caption.*\[\/caption\]/", "", $result[0]['post_excerpt']);
    			}
    
    			$excerpt = preg_replace("/<object[0-9 a-z_?*=\":\-\/\.#\,\\n\\r\\t]+/smi", "", $excerpt);
    
    			if ($instance['post-excerpt']['keep_format']) {
    				$excerpt = strip_tags($excerpt, '<a><b><i><strong><em>');
    			} else {
    				$excerpt = strip_tags($excerpt);
    			}
    
    			if (strlen($excerpt) > $instance['post-excerpt']['length']) {
    				$excerpt = $this->truncate($excerpt, $instance['post-excerpt']['length'], '', true, true);
    			}
    
    			return $excerpt;
    		}
    
    		// gets the first image of post / page
    		function get_img($id = "", $print = false) {
    			if ( empty($id) || !is_numeric($id) ) return false;
    
    			// get post attachments
    			$attachments = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
    
    			// no image has been found
    			if ( ! is_array($attachments) ) return false;
    
    			$count = count($attachments);
    			$first_attachment = array_shift($attachments);
    			$img = wp_get_attachment_image($first_attachment->ID);
    
    			preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'\s>]*)/i', $img, $imgm);
    
    			if ($print)
    					echo $imgm[1];
    				else
    					return $imgm;
    
    		}
    
    		// parses content structure defined by user
    		function format_content ($string, $data = array(), $rating) {
    			if (empty($string) || (empty($data) || !is_array($data))) return false;
    
    			$params = array();
    			$pattern = '/\{(summary|stats|title|image|rating)\}/i';
    			preg_match_all($pattern, $string, $matches);
    
    			for ($i=0; $i < count($matches[0]); $i++) {
    				if (strtolower($matches[0][$i]) == "{title}") {
    					$params[$matches[0][$i]] = $data['title']
  8. jd2media
    Member
    Posted 3 years ago #

    When you upload images using Faster Image Insert, they begin uploading 1, 2, 3, 4, but then when you click save all changes they reverse order and all of a sudden #4 is the top image. No matter how you reorder them it still pulls #4 as being the image to use...

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic