<?php
/*
Plugin Name: My Recent Posts widget
Description: Adds a sidebar widget to display posts from a specified category.  Modified by <a class="url fn" href="http://greengaloshes.cc/" title="Justin Henry">Justin Henry</a>.
Author: Mike Jolley, jolley_small@tesco.net
Version: 1.1
Author URI: http://blue-anvil.com
*/

function widget_myRecentPosts_init() {

    if ( !
function_exists('register_sidebar_widget') )
        return;

        function 
widget_myRecentPosts($args) {
        
            
// "$args is an array of strings that help widgets to conform to
            // the active theme: before_widget, before_title, after_widget,
            // and after_title are the array keys." - These are set up by the theme
            
extract($args);

            
// These are our own options
            
$options get_option('widget_myRecentPosts');
            
$title $options['title'];  // Title in sidebar for widget
            
$show $options['show'];  // # of Posts we are showing
            
$excerpt $options['excerpt'];  // Showing the excerpt or not
            
$exclude $options['exclude'];  // Categories to exclude
            
$category $options['category'];  // Categories to exclude
            //if ($show<1) $show = 1;
            
if ($exclude==""$exclude "0"
        

        
// Output
        
echo $before_widget $before_title $title $after_title;

        
// GET POSTS

            /*global $wpdb;
            $sql = 'select DISTINCT * from '.$wpdb->posts.' 
            INNER JOIN (select * from '.$wpdb->post2cat.' 
            INNER JOIN '.$wpdb->categories .' ON '.$wpdb->post2cat.'.category_id = '.$wpdb->categories .'.cat_ID) 
            as A ON '.$wpdb->posts.'.ID = A.post_ID 
            WHERE (A.cat_ID NOT IN ('.$exclude.')) 
            AND '.$wpdb->posts.'.post_status="publish" 
            AND '.$wpdb->posts.'.post_type="post"
            GROUP BY ID 
            ORDER BY '.$wpdb->posts.'.post_date 
            DESC LIMIT 0,'.$show.';';
                        
            $posts = $wpdb->get_results($sql);
            */
            
global $post;
             
$posts get_posts('numberposts='.$show.'&category=' $category '&exclude=' $exclude);
            
// start list
            
echo '<ul class="my-recent-posts">';
                
// were there any posts found?
                
if (!empty($posts)) {
                    
// posts were found, loop through them
                     
foreach ($posts as $post) {
                     
                            
// format a date for the posts
                            //$post->post_date = date("F j, Y",strtotime($post->post_date));
                             
                            // if we want to display an excerpt, get it/generate it if no excerpt found
                            
if ($excerpt) {
                                 if (empty(
$post->post_excerpt)) {
                                     
$post->post_excerpt explode(" ",strrev(substr(strip_tags($post->post_content), 0100)),2);
                                     
$post->post_excerpt strrev($post->post_excerpt[1]);
                                     
$post->post_excerpt.= " [...]";
                                 }
                            }
                            
                            
//output to screen
                            
echo '<li>
                            <a class="post" rel="bookmark" href="'
.get_permalink($post->ID).'"><span class="inner">
                            <span class="title lifestyle">'
.$post->post_title.'</span>';
                            
                            if (
$excerpt) echo '<br />'.strip_tags($post->post_excerpt);
                            
                            echo 
'</span></a></li>';
                     }
                } 
//else echo "<li>No recent Posts</li>";
        // end list
        
echo '</ul>';
        
        
// echo widget closing tag
        
echo $after_widget;
    }


    
// Settings form
    
function widget_myRecentPosts_control() {

        
// Get options
        
$options get_option('widget_myRecentPosts');
        
// options exist? if not set defaults
        
if ( !is_array($options) )
            
$options = array('title'=>'Recent Posts''show'=>'5''excerpt'=>'1','exclude'=>'''category'=>'');
        
        
// form posted?
        
if ( $_POST['myRecentPosts-submit'] ) {

            
// Remember to sanitize and format use input appropriately.
            
$options['title'] = strip_tags(stripslashes($_POST['myRecentPosts-title']));
            
$options['show'] = strip_tags(stripslashes($_POST['myRecentPosts-show']));
            
$options['excerpt'] = strip_tags(stripslashes($_POST['myRecentPosts-excerpt']));
            
$options['exclude'] = strip_tags(stripslashes($_POST['myRecentPosts-exclude']));
            
$options['category'] = strip_tags(stripslashes($_POST['myRecentPosts-category']));
            
update_option('widget_myRecentPosts'$options);
        }

        
// Get options for form fields to show
        
$title htmlspecialchars($options['title'], ENT_QUOTES);
        
$show htmlspecialchars($options['show'], ENT_QUOTES);
        
$excerpt htmlspecialchars($options['excerpt'], ENT_QUOTES);
        
$exclude htmlspecialchars($options['exclude'], ENT_QUOTES);
        
$category htmlspecialchars($options['category'], ENT_QUOTES);
        
        
// The form fields
        
echo '<p style="text-align:right;">
                <label for="myRecentPosts-title">' 
__('Title:') . 
                <input style="width: 200px;" id="myRecentPosts-title" name="myRecentPosts-title" type="text" value="'
.$title.'" />
                </label></p>'
;
        echo 
'<p style="text-align:right;">
                <label for="myRecentPosts-show">' 
__('Number of posts to show:') . 
                <input style="width: 200px;" id="myRecentPosts-show" name="myRecentPosts-show" type="text" value="'
.$show.'" />
                </label></p>'
;
        echo 
'<p style="text-align:right;">
                <label for="myRecentPosts-excerpt">' 
__('Show excerpts?:') . 
                <input style="width: 200px;" id="myRecentPosts-excerpt" name="myRecentPosts-excerpt" type="text" value="'
.$excerpt.'" />
                </label></p>'
;
        echo 
'<p>Enter the categories to exclude/show below, as a comma seperated list of category id\'s.</p>';    
        echo 
'<p style="text-align:right;">
                <label for="myRecentPosts-exclude">' 
__('Exclude category:') . 
                <input style="width: 200px;" id="myRecentPosts-exclude" name="myRecentPosts-exclude" type="text" value="'
.$exclude.'" />
                </label></p>'
;
            echo 
'<p style="text-align:right;">
                    <label for="myRecentPosts-category">' 
__('Show only this category:') . 
                    <input style="width: 200px;" id="myRecentPosts-category" name="myRecentPosts-category" type="text" value="'
.$category.'" />
                    </label></p>'
;        
        echo 
'<input type="hidden" id="myRecentPosts-submit" name="myRecentPosts-submit" value="1" />';
    }
    
    
// Register widget for use
    
register_sidebar_widget(array('My Recent Posts''widgets'), 'widget_myRecentPosts');

    
// Register settings for use, 300x100 pixel form
    
register_widget_control(array('My Recent Posts''widgets'), 'widget_myRecentPosts_control'300350);
}

// Run code and init
add_action('widgets_init''widget_myRecentPosts_init');

?>