Convert Walker classes to pass as reference. Props mdawaffe. fixes #6796 for 2.5

git-svn-id: http://svn.automattic.com/wordpress/branches/2.5@7770 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-04-22 21:06:00 +00:00
parent 5e7617335c
commit 5ea09d46ed
5 changed files with 95 additions and 134 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,56 @@ 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";
}
function end_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
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>';
}
function end_el(&$output, $category, $depth, $args) {
$output .= "</li>\n";
}
}
function wp_category_checklist( $post_id = 0 ) {
$walker = new Walker_Category_Checklist;
$args = array();
if ( $post_id )
$args['selected_cats'] = wp_get_post_categories($post_id);
else
$args['selected_cats'] = array();
$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 +186,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);

View File

@ -35,6 +35,7 @@ function redirect_post($post_ID = '') {
$location = $location[0] . '#postcustom';
} elseif (!empty($referredby) && $referredby != $referer) {
$location = $_POST['referredby'];
$location = remove_query_arg('_wp_original_http_referer', $location);
if ( $_POST['referredby'] == 'redo' )
$location = get_permalink( $post_ID );
elseif ( false !== strpos($location, 'edit.php') )

View File

@ -395,26 +395,26 @@ class Walker {
var $db_fields;
//abstract callbacks
function start_lvl($output) { return $output; }
function end_lvl($output) { return $output; }
function start_el($output) { return $output; }
function end_el($output) { return $output; }
function start_lvl(&$output) {}
function end_lvl(&$output) {}
function start_el(&$output) {}
function end_el(&$output) {}
/*
* display one element if the element doesn't have any children
* otherwise, display the element and its children
*/
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, $output ) {
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
if ( !$element)
return $output;
if ( !$element )
return;
$id_field = $this->db_fields['id'];
$parent_field = $this->db_fields['parent'];
//display this element
$cb_args = array_merge( array($output, $element, $depth), $args);
$output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
if ( $max_depth == 0 ||
($max_depth != 0 && $max_depth > $depth+1 )) { //whether to descend
@ -427,12 +427,12 @@ class Walker {
if ( !isset($newlevel) ) {
$newlevel = true;
//start the child delimiter
$cb_args = array_merge( array($output, $depth), $args);
$output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
array_splice( $children_elements, $i, 1 );
$output = $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
$i = -1;
}
}
@ -440,15 +440,13 @@ class Walker {
if ( isset($newlevel) && $newlevel ){
//end the child delimiter
$cb_args = array_merge( array($output, $depth), $args);
$output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge( array($output, $element, $depth), $args);
$output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
return $output;
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}
/*
@ -476,7 +474,7 @@ class Walker {
if ( -1 == $max_depth ) {
$empty_array = array();
foreach ( $elements as $e )
$output = $this->display_element( $e, $empty_array, 1, 0, $args, $output );
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
return $output;
}
@ -512,7 +510,7 @@ class Walker {
}
foreach ( $top_level_elements as $e )
$output = $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
/*
* if we are displaying all levels, and remaining children_elements is not empty,
@ -521,7 +519,7 @@ class Walker {
if ( ( $max_depth == 0 ) && sizeof( $children_elements ) > 0 ) {
$empty_array = array();
foreach ( $children_elements as $orphan_e )
$output = $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
$this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output );
}
return $output;
}
@ -531,19 +529,17 @@ class Walker_Page extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
function start_lvl($output, $depth) {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul>\n";
return $output;
}
function end_lvl($output, $depth) {
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
return $output;
}
function start_el($output, $page, $depth, $current_page, $args) {
function start_el(&$output, $page, $depth, $current_page, $args) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
@ -571,14 +567,10 @@ class Walker_Page extends Walker {
$output .= " " . mysql2date($date_format, $time);
}
return $output;
}
function end_el($output, $page, $depth) {
function end_el(&$output, $page, $depth) {
$output .= "</li>\n";
return $output;
}
}
@ -587,18 +579,16 @@ class Walker_PageDropdown extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
function start_el($output, $page, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
function start_el(&$output, $page, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$output .= "\t<option value=\"$page->ID\"";
if ( $page->ID == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$title = wp_specialchars($page->post_title);
$output .= "$pad$title";
$output .= "</option>\n";
return $output;
$output .= "\t<option value=\"$page->ID\"";
if ( $page->ID == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$title = wp_specialchars($page->post_title);
$output .= "$pad$title";
$output .= "</option>\n";
}
}
@ -606,25 +596,23 @@ class Walker_Category extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_lvl($output, $depth, $args) {
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
return $output;
}
function end_lvl($output, $depth, $args) {
function end_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
return $output;
}
function start_el($output, $category, $depth, $args) {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = attribute_escape( $category->name);
@ -687,16 +675,13 @@ class Walker_Category extends Walker {
} else {
$output .= "\t$link<br />\n";
}
return $output;
}
function end_el($output, $page, $depth, $args) {
function end_el(&$output, $page, $depth, $args) {
if ( 'list' != $args['style'] )
return $output;
return;
$output .= "</li>\n";
return $output;
}
}
@ -705,7 +690,7 @@ class Walker_CategoryDropdown extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
function start_el($output, $category, $depth, $args) {
function start_el(&$output, $category, $depth, $args) {
$pad = str_repeat('&nbsp;', $depth * 3);
$cat_name = apply_filters('list_cats', $category->name, $category);
@ -721,8 +706,6 @@ class Walker_CategoryDropdown extends Walker {
$output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
}
$output .= "</option>\n";
return $output;
}
}