Make WordPress Core

Changeset 10802


Ignore:
Timestamp:
03/17/2009 03:39:36 PM (16 years ago)
Author:
ryan
Message:

Move text widget to WP_Widget. see #8441

File:
1 edited

Legend:

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

    r10801 r10802  
    283283}
    284284
    285 
    286285/**
    287286 * Calendar widget class
     
    324323
    325324/**
    326  * Display the Text widget, depending on the widget number.
    327  *
    328  * Supports multiple text widgets and keeps track of the widget number by using
    329  * the $widget_args parameter. The option 'widget_text' is used to store the
    330  * content for the widgets. The content and title are passed through the
    331  * 'widget_text' and 'widget_title' filters respectively.
    332  *
    333  * @since 2.2.0
    334  *
    335  * @param array $args Widget arguments.
    336  * @param int $number Widget number.
    337  */
    338 function wp_widget_text($args, $widget_args = 1) {
    339     extract( $args, EXTR_SKIP );
    340     if ( is_numeric($widget_args) )
    341         $widget_args = array( 'number' => $widget_args );
    342     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    343     extract( $widget_args, EXTR_SKIP );
    344 
    345     $options = get_option('widget_text');
    346     if ( !isset($options[$number]) )
    347         return;
    348 
    349     $title = apply_filters('widget_title', $options[$number]['title']);
    350     $text = apply_filters( 'widget_text', $options[$number]['text'] );
    351 ?>
    352         <?php echo $before_widget; ?>
    353             <?php if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
     325 * Text widget class
     326 *
     327 * @since 2.8.0
     328 */
     329class WP_Widget_Text extends WP_Widget {
     330
     331    function WP_Widget_Text() {
     332        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
     333        $control_ops = array('width' => 400, 'height' => 350);
     334        $this->WP_Widget('text', __('Text'), $widget_ops, $control_ops);
     335    }
     336
     337    function widget( $args, $instance ) {
     338        extract($args);
     339        $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
     340        $text = apply_filters( 'widget_text', $instance['text'] );
     341        echo $before_widget;
     342        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
    354343            <div class="textwidget"><?php echo $text; ?></div>
    355         <?php echo $after_widget; ?>
    356 <?php
    357 }
    358 
    359 /**
    360  * Display and process text widget options form.
    361  *
    362  * @since 2.2.0
    363  *
    364  * @param int $widget_args Widget number.
    365  */
    366 function wp_widget_text_control($widget_args) {
    367     global $wp_registered_widgets;
    368     static $updated = false;
    369 
    370     if ( is_numeric($widget_args) )
    371         $widget_args = array( 'number' => $widget_args );
    372     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    373     extract( $widget_args, EXTR_SKIP );
    374 
    375     $options = get_option('widget_text');
    376     if ( !is_array($options) )
    377         $options = array();
    378 
    379     if ( !$updated && !empty($_POST['sidebar']) ) {
    380         $sidebar = (string) $_POST['sidebar'];
    381 
    382         $sidebars_widgets = wp_get_sidebars_widgets();
    383         if ( isset($sidebars_widgets[$sidebar]) )
    384             $this_sidebar =& $sidebars_widgets[$sidebar];
     344        <?php
     345        echo $after_widget;
     346    }
     347
     348    function update( $new_instance, $old_instance ) {
     349        $instance = $old_instance;
     350        $instance['title'] = strip_tags($new_instance['title']);
     351        if ( current_user_can('unfiltered_html') )
     352            $instance['text'] =  $new_instance['text'];
    385353        else
    386             $this_sidebar = array();
    387 
    388         foreach ( (array) $this_sidebar as $_widget_id ) {
    389             if ( 'wp_widget_text' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    390                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    391                 if ( !in_array( "text-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
    392                     unset($options[$widget_number]);
    393             }
    394         }
    395 
    396         foreach ( (array) $_POST['widget-text'] as $widget_number => $widget_text ) {
    397             if ( !isset($widget_text['text']) && isset($options[$widget_number]) ) // user clicked cancel
    398                 continue;
    399             $title = strip_tags(stripslashes($widget_text['title']));
    400             if ( current_user_can('unfiltered_html') )
    401                 $text = stripslashes( $widget_text['text'] );
    402             else
    403                 $text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
    404             $options[$widget_number] = compact( 'title', 'text' );
    405         }
    406 
    407         update_option('widget_text', $options);
    408         $updated = true;
    409     }
    410 
    411     if ( -1 == $number ) {
    412         $title = '';
    413         $text = '';
    414         $number = '%i%';
    415     } else {
    416         $title = attribute_escape($options[$number]['title']);
    417         $text = format_to_edit($options[$number]['text']);
    418     }
    419 ?>
    420         <p>
    421             <input class="widefat" id="text-title-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
    422             <textarea class="widefat" rows="16" cols="20" id="text-text-<?php echo $number; ?>" name="widget-text[<?php echo $number; ?>][text]"><?php echo $text; ?></textarea>
    423             <input type="hidden" name="widget-text[<?php echo $number; ?>][submit]" value="1" />
    424         </p>
    425 <?php
    426 }
    427 
    428 /**
    429  * Register text widget on startup.
    430  *
    431  * @since 2.2.0
    432  */
    433 function wp_widget_text_register() {
    434     $options = get_option('widget_text');
    435     if ( !is_array($options) )
    436         $options = array();
    437 
    438     $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
    439     $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'text');
    440     $name = __('Text');
    441 
    442     $id = false;
    443     foreach ( (array) array_keys($options) as $o ) {
    444         // Old widgets can have null values for some reason
    445         if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
    446             continue;
    447         $id = "text-$o"; // Never never never translate an id
    448         wp_register_sidebar_widget($id, $name, 'wp_widget_text', $widget_ops, array( 'number' => $o ));
    449         wp_register_widget_control($id, $name, 'wp_widget_text_control', $control_ops, array( 'number' => $o ));
    450     }
    451 
    452     // If there are none, we register the widget's existance with a generic template
    453     if ( !$id ) {
    454         wp_register_sidebar_widget( 'text-1', $name, 'wp_widget_text', $widget_ops, array( 'number' => -1 ) );
    455         wp_register_widget_control( 'text-1', $name, 'wp_widget_text_control', $control_ops, array( 'number' => -1 ) );
     354            $instance['text'] = wp_filter_post_kses( $new_instance['text'] );
     355        return $instance;
     356    }
     357
     358    function form( $instance ) {
     359        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
     360        $title = strip_tags($instance['title']);
     361        $text = format_to_edit($instance['text']);
     362?>
     363            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
     364            <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
     365            <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
     366<?php
    456367    }
    457368}
     
    13271238    new WP_Widget_Search();
    13281239
     1240    new WP_Widget_Text();
     1241
    13291242    $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
    13301243    wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
     
    13361249
    13371250    wp_widget_categories_register();
    1338     wp_widget_text_register();
    13391251    wp_widget_rss_register();
    13401252    wp_widget_recent_comments_register();
Note: See TracChangeset for help on using the changeset viewer.