Make WordPress Core


Ignore:
Timestamp:
03/10/2009 07:42:22 PM (15 years ago)
Author:
azaozz
Message:

Add WP_Widget class, first run - only the Links widget is using it at the moment, see #8441

File:
1 edited

Legend:

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

    r10742 r10764  
    740740
    741741/**
    742  * Display links widget.
    743  *
    744  * @since 2.2.0
    745  *
    746  * @param array $args Widget arguments.
    747  */
    748 function wp_widget_links($args) {
    749     extract($args, EXTR_SKIP);
    750 
    751     $link_options = get_option('widget_links');
    752     $show_description = isset($link_options['description']) ? $link_options['description'] : false;
    753     $show_name = isset($link_options['name']) ? $link_options['name'] : false;
    754     $show_rating = isset($link_options['rating']) ? $link_options['rating'] : false;
    755     $show_images = isset($link_options['images']) ? $link_options['images'] : true;
    756 
    757     $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
    758     wp_list_bookmarks(apply_filters('widget_links_args', array(
    759         'title_before' => $before_title, 'title_after' => $after_title,
    760         'category_before' => $before_widget, 'category_after' => $after_widget,
    761         'show_images' => $show_images, 'show_description' => $show_description,
    762         'show_name' => $show_name, 'show_rating' => $show_rating,
    763         'class' => 'linkcat widget'
    764     )));
    765 }
    766 
    767 /**
    768  * Display and process links widget options form.
     742 * Links widget class
    769743 *
    770744 * @since 2.8.0
    771745 */
    772 function wp_widget_links_control() {
    773     $options = $newoptions = get_option('widget_links');
    774 
    775     //Defaults
    776     if ( ! $newoptions )
    777         $newoptions = array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false);
    778 
    779     if ( isset($_POST['links-submit']) ) {
    780         $newoptions['description'] = isset($_POST['links-description']);
    781         $newoptions['name'] = isset($_POST['links-name']);
    782         $newoptions['rating'] = isset($_POST['links-rating']);
    783         $newoptions['images'] = isset($_POST['links-images']);
    784     }
    785     if ( $options != $newoptions ) {
    786         $options = $newoptions;
    787         update_option('widget_links', $options);
    788     }
    789 ?>
    790             <p>
    791                 <label for="links-images"><input class="checkbox" type="checkbox" <?php checked($options['images'], true) ?> id="links-images" name="links-images" /> <?php _e('Show Link Image'); ?></label>
    792                 <br />
    793                 <label for="links-name"><input class="checkbox" type="checkbox" <?php checked($options['name'], true) ?> id="links-name" name="links-name" /> <?php _e('Show Link Name'); ?></label>
    794                 <br />
    795                 <label for="links-description"><input class="checkbox" type="checkbox" <?php checked($options['description'], true) ?> id="links-description" name="links-description" /> <?php _e('Show Link Description'); ?></label>
    796                 <br />
    797                 <label for="links-rating"><input class="checkbox" type="checkbox" <?php checked($options['rating'], true) ?> id="links-rating" name="links-rating" /> <?php _e('Show Link Rating'); ?></label>
    798             </p>
    799             <input type="hidden" id="links-submit" name="links-submit" value="1" />
    800 <?php
    801 }
    802 
     746class WP_Widget_Links extends WP_Widgets {
     747   
     748    function widget( $args, $instance ) {
     749        extract($args, EXTR_SKIP);
     750
     751        $show_description = isset($instance['description']) ? $instance['description'] : false;
     752        $show_name = isset($instance['name']) ? $instance['name'] : false;
     753        $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
     754        $show_images = isset($instance['images']) ? $instance['images'] : true;
     755   
     756        $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
     757        wp_list_bookmarks(apply_filters('widget_links_args', array(
     758            'title_before' => $before_title, 'title_after' => $after_title,
     759            'category_before' => $before_widget, 'category_after' => $after_widget,
     760            'show_images' => $show_images, 'show_description' => $show_description,
     761            'show_name' => $show_name, 'show_rating' => $show_rating,
     762            'class' => 'linkcat widget'
     763        )));
     764    }
     765   
     766    function update( $new_instance, $old_instance ) {
     767        if( !isset($new_instance['submit']) ) // user clicked cancel?
     768            return false;
     769
     770        return $new_instance;
     771    }
     772   
     773    function form( $instance ) {
     774
     775        //Defaults
     776        $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false) );
     777/*
     778        if ( isset($_POST['links-submit']) ) {
     779            $newoptions = array();
     780            $newoptions['description'] = isset($_POST['links-description']);
     781            $newoptions['name'] = isset($_POST['links-name']);
     782            $newoptions['rating'] = isset($_POST['links-rating']);
     783            $newoptions['images'] = isset($_POST['links-images']);
     784
     785            if ( $instance != $newoptions ) {
     786                $instance = $newoptions;
     787                update_option('widget_links', $instance);
     788            }
     789        }
     790*/
     791?>
     792        <p>
     793        <label for="<?php echo $this->get_field_id('images'); ?>">
     794        <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" /> <?php _e('Show Link Image'); ?></label><br />
     795        <label for="<?php echo $this->get_field_id('name'); ?>">
     796        <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" /> <?php _e('Show Link Name'); ?></label><br />
     797        <label for="<?php echo $this->get_field_id('description'); ?>">
     798        <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" /> <?php _e('Show Link Description'); ?></label><br />
     799        <label for="<?php echo $this->get_field_id('rating'); ?>">
     800        <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" /> <?php _e('Show Link Rating'); ?></label>
     801        <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
     802        </p>
     803<?php
     804    }
     805}
    803806
    804807/**
     
    19711974    wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
    19721975
    1973     $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
    1974     wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
    1975     wp_register_widget_control('links', __('Links'), 'wp_widget_links_control' );
     1976    $widget_ops = array('description' => __( "Your blogroll" ) );
     1977    $wp_widget_links = new WP_Widget_Links('links', __('Links'), $widget_ops);
     1978    $wp_widget_links->register();
    19761979
    19771980    $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
Note: See TracChangeset for help on using the changeset viewer.