From 7f228f1cd6056d33fb2b012b21546714b65647c7 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 18 Apr 2008 17:41:10 +0000 Subject: [PATCH] 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 --- wp-admin/edit-form-advanced.php | 2 +- wp-admin/edit-link-form.php | 2 +- wp-admin/includes/template.php | 131 ++++++++++++++------------------ 3 files changed, 58 insertions(+), 77 deletions(-) diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index b531b9c42e..00fc5f8f2c 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -241,7 +241,7 @@ endif; ?>
diff --git a/wp-admin/edit-link-form.php b/wp-admin/edit-link-form.php index a68c6da4ed..15c3fb5d2f 100644 --- a/wp-admin/edit-link-form.php +++ b/wp-admin/edit-link-form.php @@ -118,7 +118,7 @@ if ( ( 'edit' == $action) && current_user_can('manage_links') )
diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index 48ef7e6997..71b4ec2556 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -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", "
  • ", ''; - - if ( $category['children'] ) { - echo "\n"; - } - echo '
  • '; - } -} - +// 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\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
  • " . ''; + + return $output; + } + + function end_el($output, $category, $depth, $args) { + if ( 'list' != $args['style'] ) + return $output; + + $output .= "
  • \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);