Changeset 6588

Show
Ignore:
Timestamp:
01/10/08 09:39:35 (11 months ago)
Author:
matt
Message:

New experimental category interface, fixes #5618. Hat tip: mdawaffe.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/admin-ajax.php

    r6584 r6588  
    158158        die('-1'); 
    159159    $names = explode(',', $_POST['newcat']); 
     160    if ( 0 > $parent = (int) $_POST['newcat_parent'] ) 
     161        $parent = 0; 
     162 
     163    $checked_categories = array_map( 'absint', (array) $_POST['post_category'] ); 
     164 
    160165    $x = new WP_Ajax_Response(); 
    161166    foreach ( $names as $cat_name ) { 
     
    164169        if ( '' === $category_nicename ) 
    165170            continue; 
    166         $cat_id = wp_create_category( $cat_name ); 
    167         $cat_name = wp_specialchars(stripslashes($cat_name)); 
     171        $cat_id = wp_create_category( $cat_name, $parent ); 
     172        $checked_categories[] = $cat_id; 
     173        if ( $parent ) // Do these all at once in a second 
     174            continue; 
     175        $category = get_category( $cat_id ); 
     176        $category->_is_checked = true; 
     177        ob_start(); 
     178            dropdown_categories( 0, $category ); 
     179        $data = ob_get_contents(); 
     180        ob_end_clean(); 
    168181        $x->add( array( 
    169182            'what' => 'category', 
    170183            'id' => $cat_id, 
    171             'data' => "<li id='category-$cat_id'><label for='in-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='post_category[]' id='in-category-$cat_id'/> $cat_name</label></li>"
     184            'data' => $data
    172185            'position' => -1 
    173186        ) ); 
     187    } 
     188    if ( $parent ) { // Foncy - replace the parent and all its children 
     189        $parent = get_category( $parent ); 
     190        ob_start(); 
     191            dropdown_categories( 0, $parent ); 
     192        $data = ob_get_contents(); 
     193        ob_end_clean(); 
     194        $x->add( array( 
     195            'what' => 'category', 
     196            'id' => $parent->term_id, 
     197            'old_id' => $parent->term_id, 
     198            'data' => $data, 
     199            'position' => -1 
     200        ) ); 
     201 
    174202    } 
    175203    $x->send(); 
  • trunk/wp-admin/admin-header.php

    r6548 r6588  
    33if (!isset($_GET["page"])) require_once('admin.php'); 
    44if ( $editing ) { 
    5     if ( current_user_can('manage_categories') ) 
    6         wp_enqueue_script( 'ajaxcat' ); 
    75    if ( user_can_richedit() ) 
    86        wp_enqueue_script( 'wp_tiny_mce' ); 
  • trunk/wp-admin/edit-form-advanced.php

    r6584 r6588  
    122122<h3><?php _e('Categories') ?></h3> 
    123123<div class="inside"> 
    124 <p id="jaxcat"><?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?></p> 
    125 <ul id="categorychecklist" class="list:category"><?php dropdown_categories(); ?></ul> 
     124 
     125<div id="category-adder" class="wp-hidden-children"> 
     126    <h4><a id="category-add-toggle" href="#category-add"><?php _e( '+ Add New Category' ); ?></a></h4> 
     127    <p id="category-add" class="wp-hidden-child"> 
     128        <input type="text" name="newcat" id="newcat" class="form-required" value="<?php _e( 'New category name' ); ?>" /> 
     129        <?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category') ) ); ?> 
     130        <a id="category-add-sumbit" class="add:categorychecklist:categorydiv button" href="<?php echo wp_nonce_url( '', 'add-category' ); ?>"><?php _e( 'Add' ); ?></a> 
     131        <span id="category-ajax-response"></span> 
     132    </p> 
     133</div> 
     134 
     135<ul id="category-tabs"> 
     136    <li class="ui-tabs-selected"><a href="#categories-all"><?php _e( 'All Categories' ); ?></a></li> 
     137    <li><a href="#categories-pop"><?php _e( 'Most Used' ); ?></a></li> 
     138</ul> 
     139 
     140<div id="categories-all" class="ui-tabs-panel"> 
     141    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear"> 
     142        <?php dropdown_categories(); ?> 
     143    </ul> 
     144</div> 
     145 
     146<div id="categories-pop" class="ui-tabs-panel"> 
     147    <ul id="categorychecklist-pop" class="categorychecklist form-no-clear"> 
     148        <?php wp_popular_categories_checklist(); ?> 
     149    </ul> 
     150</div> 
     151 
    126152</div> 
    127153</div> 
  • trunk/wp-admin/includes/taxonomy.php

    r6363 r6588  
    1717} 
    1818 
    19 function wp_create_category($cat_name) { 
     19function wp_create_category( $cat_name, $parent = 0 ) { 
    2020    if ( $id = category_exists($cat_name) ) 
    2121        return $id; 
    2222 
    23     return wp_insert_category( array('cat_name' => $cat_name) ); 
     23    return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) ); 
    2424} 
    2525 
  • trunk/wp-admin/includes/template.php

    r6570 r6588  
    119119} 
    120120 
    121 function get_nested_categories( $default = 0, $parent = 0 ) { 
     121function wp_set_checked_post_categories( $default = 0 ) { 
    122122    global $post_ID, $checked_categories; 
    123123 
     
    135135    } 
    136136 
    137     $cats = get_categories("parent=$parent&hide_empty=0&fields=ids"); 
    138  
    139     $result = array (); 
    140     if ( is_array( $cats ) ) { 
    141         foreach ( $cats as $cat) { 
    142             $result[$cat]['children'] = get_nested_categories( $default, $cat); 
    143             $result[$cat]['cat_ID'] = $cat; 
    144             $result[$cat]['checked'] = in_array( $cat, $checked_categories ); 
    145             $result[$cat]['cat_name'] = get_the_category_by_ID( $cat); 
     137
     138function get_nested_categories( $default = 0, $parent = 0 ) { 
     139    global $checked_categories; 
     140 
     141    wp_set_checked_post_categories( $default = 0 ); 
     142 
     143    if ( is_object($parent) ) { // Hack: if passed a category object, will return nested cats with parent as root 
     144        $root = array( 
     145            'children' => get_nested_categories( $default, $parent->term_id ), 
     146            'cat_ID' => $parent->term_id, 
     147            'checked' => isset($parent->_is_checked) && $parent->_is_checked, 
     148            'cat_name' => get_the_category_by_ID( $parent->term_id ) 
     149        ); 
     150        $result = array( $parent->term_id => $root ); 
     151    } else { 
     152        $parent = (int) $parent; 
     153 
     154        $cats = get_categories("parent=$parent&hide_empty=0&fields=ids"); 
     155 
     156        $result = array(); 
     157        if ( is_array( $cats ) ) { 
     158            foreach ( $cats as $cat ) { 
     159                $result[$cat]['children'] = get_nested_categories( $default, $cat ); 
     160                $result[$cat]['cat_ID'] = $cat; 
     161                $result[$cat]['checked'] = in_array( $cat, $checked_categories ); 
     162                $result[$cat]['cat_name'] = get_the_category_by_ID( $cat ); 
     163            } 
    146164        } 
    147165    } 
     
    166184} 
    167185 
    168 function dropdown_categories( $default = 0 ) { 
    169     write_nested_categories( get_nested_categories( $default) ); 
     186function dropdown_categories( $default = 0, $parent = 0 ) { 
     187    write_nested_categories( get_nested_categories( $default, $parent ) ); 
     188
     189 
     190function wp_popular_categories_checklist( $default = 0, $number = 10 ) { 
     191    global $checked_categories; 
     192 
     193    wp_set_checked_post_categories( $default ); 
     194 
     195    $categories = get_categories( array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number ) ); 
     196 
     197    foreach ( (array) $categories as $category ) { 
     198        $id = "popular-category-$category->term_id"; 
     199        $checked = in_array( $category->term_id, $checked_categories ) ? ' checked="checked"' : '';      
     200        ?> 
     201 
     202        <li id="<?php echo $id; ?>"> 
     203            <label class="selectit" for="in-<?php echo $id; ?>"> 
     204                <input id="in-<?php echo $id; ?>" type="checkbox" name="post_category[]" value="<?php echo (int) $category->term_id; ?>"<?php echo $checked; ?> /> 
     205                <?php echo wp_specialchars( apply_filters( 'the_category', $category->name ) ); ?> 
     206            </label> 
     207        </li> 
     208 
     209        <?php 
     210    } 
    170211} 
    171212 
  • trunk/wp-admin/js/post.js

    r6584 r6588  
    9191    // postboxes 
    9292    add_postbox_toggles(); 
     93 
     94    // category tabs 
     95    var categoryTabs =jQuery('#category-tabs').tabs(); 
     96 
     97    // Ajax Cat 
     98    var newCat = jQuery('#newcat').one( 'focus', function() { jQuery(this).val( '' ) } ); 
     99    jQuery('#category-add-sumbit').click( function() { newCat.focus(); } ); 
     100    var catAddAfter = function( r, s ) { 
     101        jQuery(s.what + ' response_data', r).each( function() { 
     102            var t = jQuery(jQuery(this).text()); 
     103            var o = jQuery( '<option value="' +  parseInt( t.find(':input').val(), 10 ) + '"></option>' ); 
     104            o.text( jQuery.trim( t.text() ) ); 
     105            jQuery('#newcat_parent').prepend( o ); 
     106        } ); 
     107    }; 
     108    jQuery('#categorychecklist').wpList( { 
     109        alt: '', 
     110        response: 'category-ajax-response', 
     111        addAfter: catAddAfter 
     112    } ); 
     113    jQuery('#category-add-toggle').click( function() { 
     114        jQuery(this).parents('div:first').toggleClass( 'wp-hidden-children' ); 
     115        categoryTabs.tabsClick( 1 ); 
     116        return false; 
     117    } ); 
    93118}); 
  • trunk/wp-admin/wp-admin.css

    r6582 r6588  
    688688} 
    689689 
    690 #categorydiv ul { 
    691     list-style: none; 
    692     padding: 0; 
    693     margin-left: 10px; 
    694 } 
    695  
    696 #categorychecklist { 
    697     height: 12em; 
    698     overflow: auto; 
    699     margin-top: 8px; 
    700 } 
    701  
    702 #categorychecklist li { 
    703     margin: 0; 
    704     padding: 0; 
    705 } 
    706  
    707 #ajaxcat input { 
    708     border: 1px solid #ccc; 
    709 } 
    710  
    711690#your-profile #rich_editing { 
    712691    border: none; 
     
    738717} 
    739718 
    740  
    741  
    742 #newcat { 
    743     width: 120px; 
    744     margin-right: 5px; 
    745 } 
    746  
    747 input #catadd { 
    748     background: #a4a4a4; 
    749     border-bottom: 1px solid #898989; 
    750     border-left: 1px solid #bcbcbc; 
    751     border-right: 1px solid #898989; 
    752     border-top: 1px solid #bcbcbc; 
    753     color: #fff; 
    754     font-size: 10px; 
    755     padding: 0; 
    756     margin: 0; 
    757     font-weight: bold; 
    758     height: 20px; 
    759     margin-bottom: 2px; 
    760     text-align: center; 
    761     width: 37px; 
    762 } 
    763  
    764719#howto { 
    765720    font-size: 11px; 
    766721    margin: 0 5px; 
    767722    display: block; 
    768 } 
    769  
    770 #jaxcat { 
    771     margin: 0; 
    772     padding: 0; 
    773723} 
    774724 
     
    12801230    background-position: 4px 18px; 
    12811231} 
     1232 
     1233/* Categories */ 
     1234 
     1235#categorydiv #category-adder { 
     1236    margin-left: 120px; 
     1237    padding: 4px 0; 
     1238} 
     1239 
     1240#category-add input, #category-add select { 
     1241    width: 30%; 
     1242} 
     1243 
     1244#categorydiv ul#category-tabs { 
     1245    float: left; 
     1246    width: 120px; 
     1247    text-align: right; 
     1248    /* Negative margin for the sake of those without JS: all tabs display */ 
     1249    margin: 0 -120px 0 0; 
     1250    padding: 0; 
     1251} 
     1252 
     1253ul#category-tabs li { 
     1254    padding: 8px; 
     1255} 
     1256 
     1257ul#category-tabs li.ui-tabs-selected { 
     1258    background-color: #CEE1EF; 
     1259} 
     1260 
     1261div.ui-tabs-panel { 
     1262    margin: 0 0 0 120px; 
     1263    padding: .5em .9em; 
     1264    height: 12em; 
     1265    overflow: auto; 
     1266    border: 4px solid #CEE1EF; 
     1267} 
     1268 
     1269#categorydiv ul { 
     1270    list-style: none; 
     1271    padding: 0; 
     1272    margin: 0; 
     1273} 
     1274 
     1275#categorydiv ul.categorychecklist ul { 
     1276    margin-left: 18px; 
     1277} 
     1278 
     1279ul.categorychecklist li { 
     1280    margin: 0; 
     1281    padding: 0; 
     1282} 
     1283 
     1284/* Global classes */ 
     1285.wp-hidden-children .wp-hidden-child { display: none; } 
     1286.ui-tabs-hide { display: none; } 
  • trunk/wp-includes/js/wp-lists.js

    r6582 r6588  
    310310            var b = e.css( 'background-color' ); 
    311311            if ( b == 'transparent' ) { b = ''; } 
    312              
    313             $('#' + s.element).css('background-color', s.addColor).animate( { backgroundColor: '#fff' }, 300 ); 
     312            e.css('background-color', s.addColor).animate( { backgroundColor: '#fff' }, 300 ); 
    314313        } 
    315314        list.each( function() { this.wpList.process( e ); } ); 
     
    322321        if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; } 
    323322        e.find(':input').each( function() { 
     323            if ( $(this).parents('.form-no-clear').size() ) 
     324                return; 
    324325            var t = this.type.toLowerCase(); var tag = this.tagName.toLowerCase(); 
    325326            if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; } 
  • trunk/wp-includes/script-loader.php

    r6587 r6588  
    6161        ) ); 
    6262 
    63         $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('jquery'), '20071101' ); 
     63        $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('jquery'), '20080109' ); 
    6464        $this->localize( 'wp-lists', 'wpListL10n', array( 
    6565            'url' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php' 
     
    8585        $this->add( 'schedule', '/wp-includes/js/jquery/jquery.schedule.js', array('jquery'), '20'); 
    8686        $this->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.js', array('jquery'), '3.1'); 
     87 
     88        $this->add( 'jquery-ui-tabs', '/wp-includes/js/jquery/ui.tabs.js', array('jquery'), '3' ); 
    8789 
    8890        if ( is_admin() ) { 
     
    107109            $this->add( 'xfn', '/wp-admin/js/xfn.js', false, '3517' ); 
    108110            $this->add( 'upload', '/wp-admin/js/upload.js', array('jquery'), '20070518' ); 
    109             $this->add( 'post', '/wp-admin/js/post.js', array('suggest'), '20080102' ); 
     111            $this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists'), '20080109' ); 
    110112            $this->localize( 'post', 'postL10n', array( 
    111113                'tagsUsed' =>  __('Tags used on this post:'), 
     
    138140                'confirmText' => __("Are you sure you want to delete the file '%title%'?\nClick ok to delete or cancel to go back.") 
    139141            ) ); 
    140             $this->add( 'admin-widgets', '/wp-admin/js/widgets.js', array( 'interface' ), mt_rand() ); 
     142            $this->add( 'admin-widgets', '/wp-admin/js/widgets.js', array( 'interface' ), '20080109' ); 
    141143        } 
    142144    }