wp_category_checklist() and wp_link_category_checklist() to replace the nested cats code. see #2621

git-svn-id: https://develop.svn.wordpress.org/trunk@7737 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-04-18 17:41:10 +00:00
parent 844fb9d2a4
commit 7f228f1cd6
3 changed files with 58 additions and 77 deletions

View File

@ -241,7 +241,7 @@ endif; ?>
<div id="categories-all" class="ui-tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
<?php dropdown_categories( 0, 0, $popular_ids ); ?>
<?php wp_category_checklist($post->ID) ?>
</ul>
</div>

View File

@ -118,7 +118,7 @@ if ( ( 'edit' == $action) && current_user_can('manage_links') )
<div id="categories-all" class="ui-tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
<?php dropdown_link_categories(); ?>
<?php wp_link_category_checklist($link_id); ?>
</ul>
</div>

View File

@ -113,84 +113,60 @@ function selected( $selected, $current) {
}
//
// Nasty Category Stuff
// Category Checklists
//
function sort_cats( $cat1, $cat2 ) {
if ( $cat1['checked'] || $cat2['checked'] )
return ( $cat1['checked'] && !$cat2['checked'] ) ? -1 : 1;
else
return strcasecmp( $cat1['cat_name'], $cat2['cat_name'] );
}
function wp_set_checked_post_categories( $default = 0 ) {
global $post_ID, $checked_categories;
if ( empty($checked_categories) ) {
if ( $post_ID ) {
$checked_categories = wp_get_post_categories($post_ID);
if ( count( $checked_categories ) == 0 ) {
// No selected categories, strange
$checked_categories[] = $default;
}
} else {
$checked_categories[] = $default;
}
}
}
function get_nested_categories( $default = 0, $parent = 0 ) {
global $checked_categories;
wp_set_checked_post_categories( $default = 0 );
if ( is_object($parent) ) { // Hack: if passed a category object, will return nested cats with parent as root
$root = array(
'children' => get_nested_categories( $default, $parent->term_id ),
'cat_ID' => $parent->term_id,
'checked' => in_array( $parent->term_id, $checked_categories ),
'cat_name' => get_the_category_by_ID( $parent->term_id )
);
$result = array( $parent->term_id => $root );
} else {
$parent = (int) $parent;
$cats = get_categories("parent=$parent&hide_empty=0&fields=ids");
$result = array();
if ( is_array( $cats ) ) {
foreach ( $cats as $cat ) {
$result[$cat]['children'] = get_nested_categories( $default, $cat );
$result[$cat]['cat_ID'] = $cat;
$result[$cat]['checked'] = in_array( $cat, $checked_categories );
$result[$cat]['cat_name'] = get_the_category_by_ID( $cat );
}
}
}
$result = apply_filters('get_nested_categories', $result);
usort( $result, 'sort_cats' );
return $result;
}
function write_nested_categories( $categories, $popular_ids = array() ) {
foreach ( $categories as $category ) {
$class = in_array( $category['cat_ID'], $popular_ids ) ? ' class="popular-category"' : '';
echo "\n", "<li id='category-$category[cat_ID]'$class>", '<label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : "" ), '/> ', wp_specialchars( apply_filters('the_category', $category['cat_name'] )), '</label>';
if ( $category['children'] ) {
echo "\n<ul>";
write_nested_categories( $category['children'] );
echo "\n</ul>";
}
echo '</li>';
}
}
// Deprecated. Use wp_link_category_checklist
function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
write_nested_categories( get_nested_categories( $default, $parent ), $popular_ids );
global $post_ID;
wp_category_checklist($post_ID);
}
class Walker_Category_Checklist extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_lvl($output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
return $output;
}
function end_lvl($output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
return $output;
}
function start_el($output, $category, $depth, $args) {
extract($args);
$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
$output .= "\n<li id='category-$category->term_id'$class>" . '<label for="in-category-' . $category->term_id . '" class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="post_category[]" id="in-category-' . $category->term_id . '"' . (in_array( $category->term_id, $selected_cats ) ? ' checked="checked"' : "" ) . '/> ' . wp_specialchars( apply_filters('the_category', $category->name )) . '</label>';
return $output;
}
function end_el($output, $category, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
$output .= "</li>\n";
return $output;
}
}
function wp_category_checklist( $post_id ) {
$walker = new Walker_Category_Checklist;
$args = array();
$args['selected_cats'] = wp_get_post_categories($post_id);
$args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10 ) );
$categories = get_categories('get=all');
$args = array($categories, 0, $args);
$output = call_user_func_array(array(&$walker, 'walk'), $args);
echo $output;
}
function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) {
@ -214,9 +190,14 @@ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) {
return $popular_ids;
}
// Deprecated. Use wp_link_category_checklist
function dropdown_link_categories( $default = 0 ) {
global $link_id;
wp_link_category_checklist($link_id);
}
function wp_link_category_checklist( $link_id = 0 ) {
if ( $link_id ) {
$checked_categories = wp_get_link_cats($link_id);