Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Copperblade: that’s right – multiple instance just means that you can add it more than once in the sidebar, rather than only being able to use it once. I just updated it so it works using the newer widgets API – pretty much all new widget do the multiple-instance thing by default.
    Hope it’s helpful!

    This plugin is great, I was recently asked to make it into a multiple-instance widget and adapted it to the new API.
    It was worked on by Myself and user Davemac.

    I don’t know if you want to do a version update or if you’d like to make a new widget, but the code for multiple-instance is below:

    function wpms_recent_posts_mu($how_many=10, $how_long=0, $titleOnly=true, $begin_wrap="\n<li>", $end_wrap="</li>") {
    	global $wpdb;
    	global $table_prefix;
    	$counter = 0;
    
    		// get a list of blogs in order of most recent update. show only public and nonarchived/spam/mature/deleted
    	if ($how_long > 0) {
    		$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    			public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
    			AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
    			ORDER BY last_updated DESC");
    	} else {
    		$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    			public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
    			ORDER BY last_updated DESC");
    	}
    
    	if ($blogs) {
    		// Should we make <ul> optional since this is a widget now?
    		echo "<ul>";
    		foreach ($blogs as $blog) {
    			// we need _posts and _options tables for this to work
    			$blogOptionsTable = $wpdb->base_prefix.$blog."_options";
    		    	$blogPostsTable = $wpdb->base_prefix.$blog."_posts";
    			$options = $wpdb->get_results("SELECT option_value FROM
    				$blogOptionsTable WHERE option_name IN ('siteurl','blogname')
    				ORDER BY option_name DESC");
    		        // we fetch the title and ID for the latest post
    			if ($how_long > 0) {
    				$thispost = $wpdb->get_results("SELECT ID, post_title
    					FROM $blogPostsTable WHERE post_status = 'publish'
    					AND ID > 1
    					AND post_type = 'post'
    					AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
    					ORDER BY id DESC LIMIT 0,1");
    			} else {
    				$thispost = $wpdb->get_results("SELECT ID, post_title
    					FROM $blogPostsTable WHERE post_status = 'publish'
    					AND ID > 1
    					AND post_type = 'post'
    					ORDER BY id DESC LIMIT 0,1");
    			}
    			// if it is found put it to the output
    			if($thispost) {
    				// get permalink by ID.  check wp-includes/wpmu-functions.php
    				$thispermalink = get_blog_permalink($blog, $thispost[0]->ID);
    				if ($titleOnly == false) {
    					echo $begin_wrap.'<a href="'
    					.$thispermalink.'">'.$thispost[0]->post_title.'</a> <br/> by <a href="'
    					.$options[0]->option_value.'">'
    					.$options[1]->option_value.'</a>'.$end_wrap;
    					$counter++;
    					} else {
    						echo $begin_wrap.'<a href="'.$thispermalink
    						.'">'.$thispost[0]->post_title.'</a>' . $end_wrap;
    						$counter++;
    					}
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    		echo "</ul>";
    
    	}
    }
    
    class wpms_recent_posts extends WP_Widget {
    
    	// The widget construct.
    	function wpms_recent_posts() {
    		$widget_ops = array( 'classname' => 'widget_WPMS_Recent_Posts', 'description' => __( "Display Recent Posts from all WPMS sites Multiwidget" ) );
    		$this->WP_Widget('recentPosts', __('WPMS Recent Posts Multiwidget'), $widget_ops);
    	} // End function wpms_recent_posts
    
    	// This code displays the widget on the screen.
    	function widget($args, $instance) {
    		extract($args);
    
    		$options = get_option("wpms_recent_posts_widget");
    
    		if (!is_array( $options )) {
    			$options = array(
    				'title' => 'Last Posts',
    				'number' => '10',
    				'days' => '-1'
    			);
    		}
    
    		echo $before_widget;
    
    		if(!empty($instance['title'])) {
    			echo $before_title . $instance['title'] . $after_title;
    		}
    		wpms_recent_posts_mu($instance['number'],$instance['days'],true,"\n<li>","</li>");
    		echo $after_widget;
    	} // End function widget.
    
    	// Updates the settings.
    	function update($new_instance, $old_instance) {
    		return $new_instance;
    	} // End function update
    
    	// The admin form.
    	function form($instance) {		
    
    		echo '<div id="wpms_RecentPosts-admin-panel">';
    		echo '<label for="' . $this->get_field_id("title") .'">WPMS Recent Posts Title:</label>';
    		echo '<input type="text" class="widefat" ';
    		echo 'name="' . $this->get_field_name("title") . '" ';
    		echo 'id="' . $this->get_field_id("title") . '" ';
    		if(empty($instance['title'])) {
    			echo 'value="Recent Posts" />';
    		}
    		else {
    		echo 'value="' . $instance["title"] . '" />';
    		}
    		echo '<label for="' . $this->get_field_id("number") .'">Number of posts to show:</label>';
    		echo '<input type="text" size="3" ';
    		echo 'name="' . $this->get_field_name("number") . '" ';
    		echo 'id="' . $this->get_field_id("number") . '" ';
    		if(empty($instance['number'])) {
    			echo 'value="10" />';
    		}
    		else {
    		echo 'value="' . $instance["number"] . '" />';
    		}
    		echo '<label for="' . $this->get_field_id("days") .'">Number of days to limit:</label>';
    		echo '<input type="text" size="3" ';
    		echo 'name="' . $this->get_field_name("days") . '" ';
    		echo 'id="' . $this->get_field_id("days") . '" ';
    		if(empty($instance['days'])) {
    			echo 'value="-1" />';
    		}
    		else {
    		echo 'value="' . $instance["days"] . '" />';
    		}
    
    		echo '<input type="hidden" ';
    		echo 'name="wpms_recent_posts_submit" ';
    		echo 'id="wpms_recent_posts_submit" ';
    		echo 'value="1" />';
    
    		echo '</div>';
    
    	$options = get_option('wpms_recent_posts_widget');
    
    	if (!is_array( $options )) {
    		$options = array(
    			'title' => 'Last Posts',
    			'number' => '10',
    			'days' => '-1'
    		);
    	}
    
    	if ($_POST['wpms_recent_posts_submit']) {
    		$options['title'] = htmlspecialchars($_POST['wpms_recent_posts_title']);
    		$options['number'] = intval($_POST['wpms_recent_posts_number']);
    		$options['days'] = intval($_POST['wpms_recent_posts_days']);
    		update_option("wpms_recent_posts_widget", $options);
    	}
    
    	} // end function form
    
    } // end class wpms_recent_posts
    
    // Register the widget.
    add_action('widgets_init', create_function('', 'return register_widget("wpms_recent_posts");'));
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)