WordPress.org

Make WordPress Core

Changeset 5717


Ignore:
Timestamp:
06/16/2007 02:03:25 AM (11 years ago)
Author:
ryan
Message:

Upgrade code for multiple category widgets. Props Andy. see #4285

File:
1 edited

Legend:

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

    r5708 r5717  
    610610}
    611611
    612 function wp_widget_categories($args) {
     612function wp_widget_categories($args, $number = 1) {
    613613    extract($args);
    614614    $options = get_option('widget_categories');
    615     $c = $options['count'] ? '1' : '0';
    616     $h = $options['hierarchical'] ? '1' : '0';
    617     $d = $options['dropdown'] ? '1' : '0';
    618     $title = empty($options['title']) ? __('Categories') : $options['title'];
     615   
     616    $c = $options[$number]['count'] ? '1' : '0';
     617    $h = $options[$number]['hierarchical'] ? '1' : '0';
     618    $d = $options[$number]['dropdown'] ? '1' : '0';
     619   
     620    $title = empty($options[$number]['title']) ? __('Categories') : $options[$number]['title'];
    619621
    620622    echo $before_widget;
     
    623625    $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";
    624626
    625     if($d) {
     627    if ( $d ) {
    626628        wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category'));
    627629?>
     
    649651}
    650652
    651 function wp_widget_categories_control() {
     653function wp_widget_categories_control( $number ) {
    652654    $options = $newoptions = get_option('widget_categories');
    653     if ( $_POST['categories-submit'] ) {
    654         $newoptions['count'] = isset($_POST['categories-count']);
    655         $newoptions['hierarchical'] = isset($_POST['categories-hierarchical']);
    656         $newoptions['dropdown'] = isset($_POST['categories-dropdown']);
    657         $newoptions['title'] = strip_tags(stripslashes($_POST['categories-title']));
    658     }
     655   
     656    if ( !is_array( $options ) ) {
     657        $options = $newoptions = get_option( 'widget_categories' );
     658    }
     659   
     660    if ( $_POST['categories-submit-' . $number] ) {
     661        $newoptions[$number]['count'] = isset($_POST['categories-count-' . $number]);
     662        $newoptions[$number]['hierarchical'] = isset($_POST['categories-hierarchical-' . $number]);
     663        $newoptions[$number]['dropdown'] = isset($_POST['categories-dropdown-' . $number]);
     664        $newoptions[$number]['title'] = strip_tags(stripslashes($_POST['categories-title-' . $number]));
     665    }
     666   
    659667    if ( $options != $newoptions ) {
    660668        $options = $newoptions;
    661669        update_option('widget_categories', $options);
    662670    }
    663     $count = $options['count'] ? 'checked="checked"' : '';
    664     $hierarchical = $options['hierarchical'] ? 'checked="checked"' : '';
    665     $dropdown = $options['dropdown'] ? 'checked="checked"' : '';
    666     $title = attribute_escape($options['title']);
    667 ?>
    668             <p><label for="categories-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="categories-title" name="categories-title" type="text" value="<?php echo $title; ?>" /></label></p>
    669             <p style="text-align:right;margin-right:40px;"><label for="categories-count"><?php _e('Show post counts'); ?> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="categories-count" name="categories-count" /></label></p>
    670             <p style="text-align:right;margin-right:40px;"><label for="categories-hierarchical" style="text-align:right;"><?php _e('Show hierarchy'); ?> <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="categories-hierarchical" name="categories-hierarchical" /></label></p>
    671             <p style="text-align:right;margin-right:40px;"><label for="categories-dropdown" style="text-align:right;"><?php _e('Display as a drop down'); ?> <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="categories-dropdown" name="categories-dropdown" /></label></p>
    672             <input type="hidden" id="categories-submit" name="categories-submit" value="1" />
    673 <?php
     671   
     672    $title = attribute_escape( $options[$number]['title'] );
     673?>
     674            <p><label for="categories-title-<?php echo $number; ?>">
     675                <?php _e( 'Title:' ); ?> <input style="width:300px" id="categories-title-<?php echo $number; ?>" name="categories-title-<?php echo $number; ?>" type="text" value="<?php echo $title; ?>" />
     676            </label></p>
     677           
     678            <p><label for="categories-dropdown-<?php echo $number; ?>">
     679                <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="categories-dropdown-<?php echo $number; ?>"<?php echo $options[$number]['dropdown'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show as dropdown' ); ?>
     680            </label></p>
     681           
     682            <p><label for="categories-count-<?php echo $number; ?>">
     683                <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="categories-count-<?php echo $number; ?>"<?php echo $options[$number]['count'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show post counts' ); ?>
     684            </label></p>
     685           
     686            <p><label for="categories-hierarchical-<?php echo $number; ?>">
     687                <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="categories-hierarchical-<?php echo $number; ?>"<?php echo $options[$number]['hierarchical'] ? ' checked="checked"' : ''; ?> /> <?php _e( 'Show hierarchy' ); ?>
     688            </label></p>
     689           
     690            <input type="hidden" id="categories-submit-<?php echo $number; ?>" name="categories-submit-<?php echo $number; ?>" value="1" />
     691<?php
     692}
     693
     694function wp_widget_categories_setup() {
     695    $options = $newoptions = get_option( 'widget_categories' );
     696   
     697    if ( isset( $_POST['categories-number-submit'] ) ) {
     698        $number = (int) $_POST['categories-number'];
     699       
     700        if ( $number > 9 ) {
     701            $number = 9;
     702        } elseif ( $number < 1 ) {
     703            $number = 1;
     704        }
     705       
     706        $newoptions['number'] = $number;
     707    }
     708   
     709    if ( $newoptions != $options ) {
     710        $options = $newoptions;
     711        update_option( 'widget_categories', $options );
     712        wp_widget_categories_register( $options['number'] );
     713    }
     714}
     715
     716function wp_widget_categories_page() {
     717    $options = get_option( 'widget_categories' );
     718?>
     719    <div class="wrap">
     720        <form method="post">
     721            <h2><?php _e( 'Categories Widgets' ); ?></h2>
     722            <p style="line-height: 30px;"><?php _e( 'How many categories widgets would you like?' ); ?>
     723                <select id="categories-number" name="categories-number" value="<?php echo attribute_escape( $options['number'] ); ?>">
     724                    <?php
     725                        for ( $i = 1; $i < 10; $i++ ) {
     726                            echo '<option value="' . $i . '"' . ( $i == $options['number'] ? ' selected="selected"' : '' ) . '>' . $i . "</option>\n";
     727                        }
     728                    ?>
     729                </select>
     730                <span class="submit">
     731                    <input type="submit" value="<?php echo attribute_escape( __( 'Save' ) ); ?>" id="categories-number-submit" name="categories-number-submit" />
     732                </span>
     733            </p>
     734        </form>
     735    </div>
     736<?php
     737}
     738
     739function wp_widget_categories_upgrade() {
     740    $options = get_option( 'widget_categories' );
     741
     742    $newoptions = array( 'number' => 1, 1 => $options );
     743
     744    update_option( 'widget_categories', $newoptions );
     745
     746    $sidebars_widgets = get_option( 'sidebars_widgets' );
     747    if ( is_array( $sidebars_widgets ) ) {
     748        foreach ( $sidebars_widgets as $sidebar => $widgets ) {
     749            if ( is_array( $widgets ) ) {
     750                foreach ( $widgets as $widget )
     751                    $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
     752            } else {
     753                $new_widgets[$sidebar] = $widgets;
     754            }
     755        }
     756        if ( $new_widgets != $sidebars_widgets )
     757            update_option( 'sidebars_widgets', $new_widgets );
     758    }
     759
     760    if ( isset( $_POST['categories-submit'] ) ) {
     761        $_POST['categories-submit-1'] = $_POST['categories-submit'];
     762        $_POST['categories-count-1'] = $_POST['categories-count'];
     763        $_POST['categories-hierarchical-1'] = $_POST['categories-hierarchical'];
     764        $_POST['categories-dropdown-1'] = $_POST['categories-dropdown'];
     765        $_POST['categories-title-1'] = $_POST['categories-title'];
     766        foreach ( $_POST as $k => $v )
     767            if ( substr($k, -5) == 'order' )
     768                $_POST[$k] = str_replace('categories', 'categories-1', $v);
     769    }
     770
     771    return $newoptions;
     772}
     773
     774function wp_widget_categories_register() {
     775    $options = get_option( 'widget_categories' );
     776    if ( !isset($options['number']) )
     777        $options = wp_widget_categories_upgrade();
     778    $number = (int) $options['number'];
     779   
     780    if ( $number > 9 ) {
     781        $number = 9;
     782    } elseif ( $number < 1 ) {
     783        $number = 1;
     784    }
     785   
     786    $dims = array( 'width' => 350, 'height' => 170 );
     787    $class = array( 'classname' => 'widget_catgories' );
     788   
     789    for ( $i = 1; $i <= 9; $i++ ) {
     790        $name = sprintf( __( 'Categories %d' ), $i );
     791        $id = 'categories-' . $i;
     792       
     793        $widget_callback = ( $i <= $number ) ? 'wp_widget_categories' : '';
     794        $control_callback = ( $i <= $number ) ? 'wp_widget_categories_control' : '';
     795       
     796        wp_register_sidebar_widget( $id, $name, $widget_callback, $class, $i );
     797        wp_register_widget_control( $id, $name, $control_callback, $dims, $i );
     798    }
     799   
     800    add_action( 'sidebar_admin_setup', 'wp_widget_categories_setup' );
     801    add_action( 'sidebar_admin_page', 'wp_widget_categories_page' );
    674802}
    675803
     
    9551083
    9561084function wp_widgets_init() {
    957     global $wp_register_widget_defaults;
    958 
    959     $wp_register_widget_defaults = true;
    960     $dims90 = array('height' => 90, 'width' => 300);
    961     $dims100 = array('height' => 100, 'width' => 300);
    962     $dims150 = array('height' => 150, 'width' => 300);
     1085    $GLOBALS['wp_register_widget_defaults'] = true;
     1086   
     1087    $dims90 = array( 'height' => 90, 'width' => 300 );
     1088    $dims100 = array( 'height' => 100, 'width' => 300 );
     1089    $dims150 = array( 'height' => 150, 'width' => 300 );
     1090   
    9631091    $class = array('classname' => 'widget_pages');
    9641092    wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $class);
    9651093    wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control', $dims150);
     1094   
    9661095    $class['classname'] = 'widget_calendar';
    9671096    wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $class);
    9681097    wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control', $dims90);
     1098   
    9691099    $class['classname'] = 'widget_archives';
    9701100    wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $class);
    9711101    wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control', $dims100);
     1102   
    9721103    $class['classname'] = 'widget_links';
    9731104    wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $class);
     1105   
    9741106    $class['classname'] = 'widget_meta';
    9751107    wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $class);
    9761108    wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control', $dims90);
     1109   
    9771110    $class['classname'] = 'widget_search';
    9781111    wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $class);
    979     $class['classname'] = 'widget_categories';
    980     wp_register_sidebar_widget('categories', __('Categories'), 'wp_widget_categories', $class);
    981     wp_register_widget_control('categories', __('Categories'), 'wp_widget_categories_control', $dims150);
     1112   
    9821113    $class['classname'] = 'widget_recent_entries';
    9831114    wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $class);
    9841115    wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control', $dims90);
     1116   
     1117    wp_widget_categories_register();
    9851118    wp_widget_text_register();
    9861119    wp_widget_rss_register();
    9871120    wp_widget_recent_comments_register();
    9881121
    989     $wp_register_widget_defaults = false;
     1122    $GLOBALS['wp_register_widget_defaults'] = false;
    9901123
    9911124    do_action('widgets_init');
Note: See TracChangeset for help on using the changeset viewer.