Make WordPress Core

Changeset 11087


Ignore:
Timestamp:
04/25/2009 12:43:51 PM (16 years ago)
Author:
azaozz
Message:

Move recent posts widget to WP_Widget. see #8441

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r11081 r11087  
    488488
    489489/**
    490  * Display recent entries widget.
    491  *
    492  * @since 2.2.0
    493  *
    494  * @param array $args Widget arguments.
    495  * @return int Displayed cache.
    496  */
    497 function wp_widget_recent_entries($args) {
    498     if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
     490 * Recent_Posts widget class
     491 *
     492 * @since 2.8.0
     493 */
     494class WP_Widget_Recent_Posts extends WP_Widget {
     495
     496    function WP_Widget_Recent_Posts() {
     497        $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
     498        $this->WP_Widget('recent_posts', __('Recent Posts'), $widget_ops);
     499
     500        add_action( 'save_post', array(&$this, 'flush_widget_cache') );
     501        add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
     502        add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
     503    }
     504
     505    function widget($args, $instance) {
    499506        if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
    500507            return print($output);
     508
    501509        ob_start();
    502     }
    503 
    504     extract($args);
    505     $options = get_option('widget_recent_entries');
    506     $title = empty($options['title']) ? __('Recent Posts') : apply_filters('widget_title', $options['title']);
    507     if ( !$number = (int) $options['number'] )
    508         $number = 10;
    509     else if ( $number < 1 )
    510         $number = 1;
    511     else if ( $number > 15 )
    512         $number = 15;
    513 
    514     $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
    515     if ($r->have_posts()) :
    516 ?>
    517         <?php echo $before_widget; ?>
     510   
     511        extract($args);
     512
     513        $title = empty($instance['title']) ? __('Recent Posts') : apply_filters('widget_title', $instance['title']);
     514        if ( !$number = (int) $instance['number'] )
     515            $number = 10;
     516        else if ( $number < 1 )
     517            $number = 1;
     518        else if ( $number > 15 )
     519            $number = 15;
     520   
     521        $r = new WP_Query(array('showposts' => $number, 'what_to_show' => 'posts', 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1));
     522        if ($r->have_posts()) :
     523?>
     524            <?php echo $before_widget; ?>
    518525            <?php echo $before_title . $title . $after_title; ?>
    519526            <ul>
     
    522529            <?php endwhile; ?>
    523530            </ul>
    524         <?php echo $after_widget; ?>
    525 <?php
    526         wp_reset_query();  // Restore global post data stomped by the_post().
    527     endif;
    528 
    529     if ( '%BEG_OF_TITLE%' != $args['before_title'] )
     531            <?php echo $after_widget; ?>
     532<?php
     533            wp_reset_query();  // Restore global post data stomped by the_post().
     534        endif;
     535   
    530536        wp_cache_add('widget_recent_entries', ob_get_flush(), 'widget');
    531 }
    532 
    533 /**
    534  * Remove recent entries widget items cache.
    535  *
    536  * @since 2.2.0
    537  */
    538 function wp_flush_widget_recent_entries() {
    539     wp_cache_delete('widget_recent_entries', 'widget');
    540 }
    541 
    542 add_action('save_post', 'wp_flush_widget_recent_entries');
    543 add_action('deleted_post', 'wp_flush_widget_recent_entries');
    544 add_action('switch_theme', 'wp_flush_widget_recent_entries');
    545 
    546 /**
    547  * Display and process recent entries widget options form.
    548  *
    549  * @since 2.2.0
    550  */
    551 function wp_widget_recent_entries_control() {
    552     $options = $newoptions = get_option('widget_recent_entries');
    553     if ( isset($_POST["recent-entries-submit"]) ) {
    554         $newoptions['title'] = strip_tags(stripslashes($_POST["recent-entries-title"]));
    555         $newoptions['number'] = (int) $_POST["recent-entries-number"];
    556     }
    557     if ( $options != $newoptions ) {
    558         $options = $newoptions;
    559         update_option('widget_recent_entries', $options);
    560         wp_flush_widget_recent_entries();
    561     }
    562     $title = attribute_escape($options['title']);
    563     if ( !$number = (int) $options['number'] )
    564         $number = 5;
    565 ?>
    566 
    567             <p><label for="recent-entries-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-entries-title" name="recent-entries-title" type="text" value="<?php echo $title; ?>" /></label></p>
    568             <p>
    569                 <label for="recent-entries-number"><?php _e('Number of posts to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-entries-number" name="recent-entries-number" type="text" value="<?php echo $number; ?>" /></label>
    570                 <br />
    571                 <small><?php _e('(at most 15)'); ?></small>
    572             </p>
    573             <input type="hidden" id="recent-entries-submit" name="recent-entries-submit" value="1" />
    574 <?php
     537    }
     538
     539    function update( $new_instance, $old_instance ) {
     540        $instance = $old_instance;
     541        $instance['title'] = strip_tags($new_instance['title']);
     542        $instance['number'] = (int) $new_instance['number'];
     543        $this->flush_widget_cache();
     544
     545        return $instance;
     546    }
     547
     548    function flush_widget_cache() {
     549        wp_cache_delete('widget_recent_entries', 'widget');
     550    }
     551
     552    function form( $instance ) {
     553        $title = attribute_escape($instance['title']);
     554        if ( !$number = (int) $instance['number'] )
     555            $number = 5;
     556?>
     557        <p>
     558            <label for="<?php echo $this->get_field_id('title'); ?>">
     559                <?php _e('Title:'); ?>
     560                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
     561            </label>
     562        </p>
     563        <p>
     564            <label for="<?php echo $this->get_field_id('number'); ?>">
     565                <?php _e('Number of posts to show:'); ?>
     566                <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
     567            </label><br />
     568            <small><?php _e('(at most 15)'); ?></small>
     569        </p>
     570<?php
     571    }
    575572}
    576573
     
    11371134    register_widget('WP_Widget_Categories');
    11381135
    1139     $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
    1140     wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
    1141     wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control' );
     1136    register_widget('WP_Widget_Recent_Posts');
    11421137
    11431138    $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
  • trunk/wp-includes/widgets.php

    r11074 r11087  
    8888     */
    8989    function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) {
    90         $this->id_base = $id_base === false ? str_replace( 'wp_widget_', '', strtolower(get_class($this)) ) : $id_base;
     90        $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : $id_base;
    9191        $this->name = $name;
    92         $this->option_name = 'widget_' . $id_base;
     92        $this->option_name = 'widget_' . $this->id_base;
    9393        $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
    9494        $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
Note: See TracChangeset for help on using the changeset viewer.