Make WordPress Core

Ticket #3130: quickcategories-002.diff

File quickcategories-002.diff, 8.6 KB (added by markjaquith, 19 years ago)

Patch for /trunk/

  • wp-admin/admin-ajax.php

     
    106106        }
    107107        die('0');
    108108        break;
     109case 'add-quickcategory' : // On the Fly
     110        if ( !current_user_can( 'manage_categories' ) )
     111                die('-1');
     112        $names = explode(',', $_POST['newcat']);
     113        $x = new WP_Ajax_Response();
     114        foreach ( $names as $hierarchical_cat_name ) {
     115                $cat_names = explode('>', $hierarchical_cat_name);
     116                $parents = array();
     117                $depth = count($cat_names) - 1;
     118                foreach ( $cat_names as $cat_name ) {
     119                        $cat_name = trim($cat_name);
     120                        if ( !$category_nicename = sanitize_title($cat_name) )
     121                                die('0');
     122                        if ( $parents ) {
     123                                $n = count($parents);
     124                                $category_parent = $parents[count($parents) - 1]['cat_ID'];
     125                        } else {
     126                                $n = 0;
     127                                $category_parent = 0;
     128                        }
     129                        if ( !$cat_ID = category_exists( $cat_name ) ) {
     130                                if ( current_user_can( 'manage_categories' ) ) {
     131                                        $cat_ID = wp_create_category($cat_name, $category_parent);
     132                                } else {
     133                                        die('-1');
     134                                }
     135                        }
     136                        $cat_name = wp_specialchars(stripslashes($cat_name));
     137                        $category = compact('cat_name', 'cat_ID');
     138                        if ( $depth == $n ) {
     139                                $li = get_quickadd_category($category, $parents, true);
     140                                $x->add( array(
     141                                        'what' => 'quickcategory',
     142                                        'id' => $cat_ID,
     143                                        'data' => $li
     144                                ) );
     145                        } else {
     146                                $parents[] = $category;
     147                        }
     148                }
     149        }
     150        $x->send();
     151        break;
     152case 'delete-quickcategory' :
     153        if ( !current_user_can( 'manage_categories') )
     154                die('-1');
     155        break;
    109156case 'add-category' : // On the Fly
    110157        if ( !current_user_can( 'manage_categories' ) )
    111158                die('-1');
  • wp-admin/wp-admin.css

     
    10731073        background-position: 0 -28px;
    10741074}
    10751075
     1076#moremeta h4 {
     1077        margin: 0;
     1078        background: #eee;
     1079        position: relative;
     1080        left: -5px;
     1081        width: 182px;
     1082        text-indent: 10px;
     1083        color: #444;
     1084display: none;
     1085}
     1086
    10761087#categorychecklist {
    10771088        margin-right: 6px;
    10781089}
    10791090
     1091#categorylist {
     1092        font-size: 10px;
     1093        color: #333;
     1094        list-style: none;
     1095}
     1096
     1097#categorylist a {
     1098        color: #f00;
     1099        border-bottom: 0;
     1100}
     1101
     1102#categorylist b {
     1103        color: #000;
     1104}
     1105
     1106#categorylist span {
     1107        display: block;
     1108        position: absolute;
     1109        left: -15px;
     1110        color: #888;
     1111}
     1112
     1113#categorylist li {
     1114        left: -20px;
     1115        position: relative;
     1116        width: 155px;
     1117}
     1118
    10801119/* additional clone styles */
    10811120.dbx-clone {
    10821121        opacity: 0.8;
  • wp-admin/admin-functions.php

     
    712712        write_nested_categories(get_nested_link_categories($default));
    713713}
    714714
     715function quickadd_categories($default = 0) {
     716                write_nested_quickadd_categories(get_nested_categories($default));
     717}
     718
     719// $categories is the array to expand
     720// $parents is an array of the parents of $categories, with the first element in the array being the top level parent, and so forth
     721function write_nested_quickadd_categories($categories, $parents = array()) {
     722        foreach ( (array) $categories as $category ) {
     723                if ( $category['checked'] )
     724                        write_quickadd_category($category, $parents);
     725                if ( $category['children'] ) {
     726                        $newparents = $parents;
     727                        $newparents[] = array('cat_name' => $category['cat_name']);
     728                        write_nested_quickadd_categories($category['children'], $newparents);
     729                }
     730        }
     731}
     732
     733function write_quickadd_category($category, $parents = array()) {
     734        echo get_quickadd_category($category, $parents);
     735}
     736
     737function get_quickadd_category($category, $parents = array(), $with_input = false) {
     738        $return = '';
     739        $return .= "<li id='quickcategory-{$category['cat_ID']}'><span>[<a href='#' onclick='quickCatRemove({$category['cat_ID']}); return false;'>x</a>]</span> ";
     740        foreach ( (array) $parents as $parent )
     741                $return .= $parent['cat_name'] . ' &raquo; ';
     742        $return .= "<b>{$category['cat_name']}</b>";
     743        if ( $with_input )
     744                $return .= "<input value='{$category['cat_ID']}' type='hidden' id='quickcat-hidden-{$category['cat_ID']}' name='post_category[]' />";
     745        $return .= "</li>\n\n";
     746        return $return;
     747}
     748
    715749// Dandy new recursive multiple category stuff.
    716750function cat_rows($parent = 0, $level = 0, $categories = 0) {
    717751        if (!$categories)
  • wp-admin/admin-db.php

     
    217217        return 1;
    218218}
    219219
    220 function wp_create_category($cat_name) {
    221         $cat_array = compact('cat_name');
     220function wp_create_category($cat_name, $category_parent = 0) {
     221        $cat_array = compact('cat_name', 'category_parent');
    222222        return wp_insert_category($cat_array);
    223223}
    224224
  • wp-admin/edit-form-advanced.php

     
    7171<div id="moremeta">
    7272<div id="grabit" class="dbx-group">
    7373
    74 <fieldset id="categorydiv" class="dbx-box">
    75 <h3 class="dbx-handle"><?php _e('Categories') ?></h3>
     74<fieldset id="quickcategorydiv" class="dbx-box" style="display: none;">
     75<h3 class="dbx-handle"><?php _e('Quick Categories') ?></h3>
    7676<div class="dbx-content">
    7777<p id="jaxcat"></p>
    78 <ul id="categorychecklist"><?php dropdown_categories(get_option('default_category')); ?></ul></div>
     78<h4>Assigned categories</h4>
     79<ul id="categorylist">
     80<?php quickadd_categories(get_option('default_category')); ?>
     81</ul>
     82<!--
     83        <li id="quickcategory-1"><span>[<a href="#">x</a>]</span> <b>Category name</b></li>
     84        <li id="quickcategory-2"><span>[<a href="#">x</a>]</span> Parent &raquo; <b>Child</b></li>
     85        <li id="quickcategory-3"><span>[<a href="#">x</a>]</span> Another Category &raquo; With &raquo; <b>a grandchild</b></li>
     86-->
     87</div>
    7988</fieldset>
     89<script type="text/javascript">
     90<!--
     91document.getElementByID('quickcategorydiv').style.display = 'auto';
     92//-->
     93</script>
    8094
     95<fieldset id="categorydiv" class="dbx-box">
     96<h3 class="dbx-handle"><?php _e('Category Tree') ?></h3>
     97<div class="dbx-content">
     98<ul id="categorychecklist"><?php dropdown_categories(get_option('default_category')); ?></ul>
     99</div>
     100</fieldset>
     101
    81102<fieldset id="commentstatusdiv" class="dbx-box">
    82103<h3 class="dbx-handle"><?php _e('Discussion') ?></h3>
    83104<div class="dbx-content">
  • wp-admin/cat-js.php

     
    22require_once('../wp-config.php');
    33cache_javascript_headers();
    44?>
    5 addLoadEvent(function(){catList=new listMan('categorychecklist');catList.ajaxRespEl='jaxcat';catList.topAdder=1;catList.alt=0;catList.showLink=0;});
     5addLoadEvent(function(){quickCatList=new listMan('categorylist');quickCatList.ajaxRespEl='jaxcat';quickCatList.topAdder=1;quickCatList.alt=0;quickCatList.showLink=0;});
     6addLoadEvent(function(){catList=new listMan('categorychecklist');catList.ajaxRespEl='jaxcat';catList.topAdder=0;catList.alt=0;catList.showLink=0;});
    67addLoadEvent(newCatAddIn);
    78function newCatAddIn() {
    89        var jaxcat = $('jaxcat');
    910        if ( !jaxcat )
    1011                return false;
    1112        jaxcat.update('<span id="ajaxcat"><input type="text" name="newcat" id="newcat" size="16" autocomplete="off"/><input type="button" name="Button" id="catadd" value="<?php _e('Add'); ?>"/><span id="howto"><?php _e('Separate multiple categories with commas.'); ?></span></span>');
    12         $('newcat').onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','jaxcat');", e); };
    13         $('catadd').onclick = function() { catList.ajaxAdder('category', 'jaxcat'); };
     13
     14/*
     15killSubmit("catList.ajaxAdder('category','jaxcat');", e);
     16catList.ajaxAdder('category', 'jaxcat');
     17
     18I'm having problems sending both ajaxAdder() calls.  The first one works, but the second has an error.
     19I think it is because the first one is "taking" the input field and blanking it and the second one gets
     20blank data.  No idea how to fix it.
     21*/
     22        $('newcat').onkeypress = function(e) { return killSubmit("quickCatList.ajaxAdder('quickcategory','jaxcat');", e); };
     23        $('catadd').onclick = function() { quickCatList.ajaxAdder('quickcategory', 'jaxcat'); };
    1424}
     25
     26function quickCatAdd(id) {
     27        // Later
     28}
     29
     30function quickCatRemove(id) {
     31        if ( document.getElementById('quickcat-hidden-' + id) )
     32                document.getElementById('quickcat-hidden-' + id).value = '';
     33        quickCatList.ajaxDelete('quickcategory', id);
     34        if ( document.getElementById('in-category-' + id) )
     35                document.getElementById('in-category-' + id).checked = false;
     36}
     37 No newline at end of file