How to display more than one instance for a widget?

I’m using a plugin called Recent category post widget. Unfortunately the plugin developer is no longer offering support. The way this plugin is coded, displays only one instance of the widget (meaning that I can use this widget only once). What must be changed in the code below so as to use multiple instances of the widget?

<?php
register_activation_hook    (   __FILE__,           array('single_category_posts_widget', 'activate')       );
register_deactivation_hook  (   __FILE__,           array('single_category_posts_widget', 'deactivate')     );
add_action                  (   "widgets_init",     array('single_category_posts_widget', 'register')       );

class single_category_posts_widget {

    function activate()
    {
        if( get_option( 'single_category_posts_widget_title' ) === FALSE ) {
            update_option( 'single_category_posts_widget_title', 'Recent Category Posts' );
        }
        if( get_option( 'single_category_posts_widget_category' ) === FALSE ) {
            update_option( 'single_category_posts_widget_category', 0 );
        }
        if( get_option( 'single_category_posts_widget_qty' ) === FALSE ) {
            update_option( 'single_category_posts_widget_qty', 5 );
        }
    }

    function deactivate()
    {
        delete_option( 'single_category_posts_widget_title' );
        delete_option( 'single_category_posts_widget_category' );
        delete_option( 'single_category_posts_widget_qty' );
    }

    function register()
    {
        wp_register_sidebar_widget( 'recent-category-posts', 'Recent Posts in Category', array('single_category_posts_widget', 'widget' ));
        wp_register_widget_control( 'recent-category-posts', 'Recent Posts in Category', array('single_category_posts_widget', 'control' ));
    }

    function control()
    {
        if (isset($_POST['single_category_posts_widget_title']))        update_option(  'single_category_posts_widget_title',       mysql_real_escape_string($_POST['single_category_posts_widget_title'])      );
        if (isset($_POST['single_category_posts_widget_category']))     update_option(  'single_category_posts_widget_category',    intval($_POST['single_category_posts_widget_category']) );
        if (isset($_POST['single_category_posts_widget_qty']))          update_option(  'single_category_posts_widget_qty',         intval($_POST['single_category_posts_widget_qty'])      );
        ?>
        <p><label>
            <strong>Widget Title:</strong><br />
            <input class="widefat" type="text" name="single_category_posts_widget_title" value="<?php echo get_option( 'single_category_posts_widget_title' ); ?>" /></label></p>
        <p><label>
            <strong>What Category:</strong><br />
            <?php wp_dropdown_categories( Array(
                        'orderby'            => 'ID', 
                        'order'              => 'ASC',
                        'show_count'         => 1,
                        'hide_empty'         => 0,
                        'hide_if_empty'      => false,
                        'echo'               => 1,
                        'selected'           => get_option( 'single_category_posts_widget_category' ),
                        'hierarchical'       => 1, 
                        'name'               => 'single_category_posts_widget_category',
                        'id'                 => 'single_category_posts_widget_category',
                        'class'              => 'widefat',
                        'taxonomy'           => 'category',
                    ) ); ?></label></p>
        <p><label>
            <strong>How Many Posts:</strong><br />
            <input class="widefat" type="text" name="single_category_posts_widget_qty" value="<?php echo get_option( 'single_category_posts_widget_qty' ); ?>" /></label></p>
        <?php
    }

    function widget( $args )
    {
        echo $args['before_widget'];
        echo $args['before_title'] . get_option( 'single_category_posts_widget_title', 'Recent Category Posts' ) . $args['after_title'];
        echo self::get_cat_posts( get_option( 'single_category_posts_widget_category', 0 ), get_option( 'single_category_posts_widget_qty', 5 ) );
        echo $args['after_widget'];
    }

    function get_cat_posts( $cat, $qty )
    {
        $posts = get_posts( Array(
            'cat'           =>  $cat,
            'orderby'       =>  'date',
            'order'         =>  'DESC',
            'numberposts'   =>  $qty
        ));
        $returnThis = '';
        if( count( $posts ) )
            $returnThis .= '<ul>'."rn";
        foreach( $posts as $post )
            $returnThis .= "t".'<li><a href="'.get_permalink( $post->ID ).'">'.$post->post_title.'</a></li>'."rn";
        if( count( $posts ) )
            $returnThis .= '</ul>'."rn";
        return $returnThis;
    }

}

Related posts

Leave a Reply

1 comment