Make WordPress Core

Ticket #2593: 2593.diff

File 2593.diff, 19.1 KB (added by davidhouse, 20 years ago)
  • wp-includes/template-functions-category.php

     
    11<?php
    22
     3function walk_category_tree() {
     4        $walker = new Walker_Category;
     5        $args = func_get_args();
     6        return call_user_func_array(array(&$walker, 'walk'), $args);
     7}
     8
     9function walk_category_dropdown_tree() {
     10        $walker = new Walker_CategoryDropdown;
     11        $args = func_get_args();
     12        return call_user_func_array(array(&$walker, 'walk'), $args);
     13}
     14
    315function get_the_category($id = false) {
    416global $post, $category_cache;
    517
     
    185197                else
    186198                        $depth = -1; // Flat.
    187199
    188                 $output .= walk_category_tree($categories, $depth, '_category_dropdown_element', '', '', '', $selected, $r);
     200                $output .= walk_category_tree($categories, $depth, $selected, $r);
    189201                $output .= "</select>\n";
    190202        }
    191203
     
    197209        return $output;
    198210}
    199211
    200 function _category_dropdown_element($output, $category, $depth, $selected, $args) {
    201         $pad = str_repeat('&nbsp;', $depth * 3);
    202 
    203         $cat_name = apply_filters('list_cats', $category->cat_name, $category);
    204         $output .= "\t<option value=\"".$category->cat_ID."\"";
    205         if ( $category->cat_ID == $selected )
    206                 $output .= ' selected="selected"';
    207         $output .= '>';
    208         $output .= $cat_name;
    209         if ( $args['show_count'] )
    210                 $output .= '&nbsp;&nbsp;('. $category->category_count .')';
    211         if ( $args['show_last_update'] ) {
    212                 $format = 'Y-m-d';
    213                 $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
    214         }
    215         $output .= "</option>\n";
    216 
    217         return $output;
    218 }
    219 
    220212function wp_list_categories($args = '') {
    221213        if ( is_array($args) )
    222214                $r = &$args;
     
    251243                else
    252244                        $depth = -1; // Flat.
    253245
    254                 $output .= walk_category_tree($categories, $depth, '_category_list_element_start', '_category_list_element_end', '_category_list_level_start', '_category_list_level_end', $current_category, $r);
     246                $output .= walk_category_tree($categories, $depth, $current_category, $r);
    255247        }
    256248
    257249        if ( $title_li && $list )
     
    260252        echo apply_filters('list_cats', $output);
    261253}
    262254
    263 function _category_list_level_start($output, $depth, $cat, $args) {
    264         if ( 'list' != $args['style'] )
    265                 return $output;
    266 
    267         $indent = str_repeat("\t", $depth);
    268         $output .= "$indent<ul class='children'>\n";
    269         return $output;
    270 }
    271 
    272 function _category_list_level_end($output, $depth, $cat, $args) {
    273         if ( 'list' != $args['style'] )
    274                 return $output;
    275 
    276         $indent = str_repeat("\t", $depth);
    277         $output .= "$indent</ul>\n";
    278         return $output;
    279 }
    280 
    281 function _category_list_element_start($output, $category, $depth, $current_category, $args) {
    282         extract($args);
    283 
    284         $link = '<a href="' . get_category_link($category->cat_ID) . '" ';
    285         if ( $use_desc_for_title == 0 || empty($category->category_description) )
    286                 $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
    287         else
    288                 $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
    289         $link .= '>';
    290         $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
    291 
    292         if ( (! empty($feed_image)) || (! empty($feed)) ) {
    293                 $link .= ' ';
    294 
    295                 if ( empty($feed_image) )
    296                         $link .= '(';
    297 
    298                 $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
    299 
    300                 if ( !empty($feed) ) {
    301                         $title = ' title="' . $feed . '"';
    302                         $alt = ' alt="' . $feed . '"';
    303                         $name = $feed;
    304                         $link .= $title;
    305                 }
    306 
    307                 $link .= '>';
    308 
    309                 if ( !empty($feed_image) )
    310                         $link .= "<img src='$feed_image' $alt$title" . ' />';
    311                 else
    312                         $link .= $name;
    313                 $link .= '</a>';
    314                 if (empty($feed_image))
    315                         $link .= ')';
    316         }
    317 
    318         if ( $show_count )
    319                 $link .= ' ('.intval($category->category_count).')';
    320 
    321         if ( $show_date ) {
    322                 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
    323         }
    324 
    325         if ( 'list' == $args['style'] ) {
    326                 $output .= "\t<li";
    327                 if ( ($category->cat_ID == $current_category) && is_category() )
    328                         $output .=  ' class="current-cat"';
    329                 $output .= ">$link\n";
    330         } else {
    331                 $output .= "\t$link<br />\n";
    332         }
    333 
    334         return $output;
    335 }
    336 
    337 function _category_list_element_end($output, $category, $depth, $cat, $args) {
    338         if ( 'list' != $args['style'] )
    339                 return $output;
    340 
    341         $output .= "</li>\n";
    342         return $output;
    343 }
    344 
    345255function in_category($category) { // Check if the current post is in the given category
    346256        global $category_cache, $post;
    347257
  • wp-includes/template-functions-post.php

     
    278278Pages
    279279*/
    280280
     281function walk_page_tree() {
     282        $walker = new Walker_Page;
     283        $args = func_get_args();
     284        return call_user_func_array(array(&$walker, 'walk'), $args);
     285}
    281286
    282287function &get_page_children($page_id, $pages) {
    283288        global $page_cache;
     
    380385
    381386        if ( ! empty($pages) ) {
    382387                $output = "<select name='$name'>\n";
    383                 $output .= walk_page_tree($pages, $depth, '_page_dropdown_element', '', '', '', $selected);
     388                $output .= walk_page_tree($pages, $depth, $selected);
    384389                $output .= "</select>\n";
    385390        }
    386391
     
    427432
    428433                global $wp_query;
    429434                $current_page = $wp_query->get_queried_object_id();
    430                 $output .= walk_page_tree($pages, $depth, '_page_list_element_start', '_page_list_element_end', '_page_list_level_start', '_page_list_level_end', $current_page, $r['show_date'], $r['date_format']);
     435                $output .= walk_page_tree($pages, $depth, $current_page, $r['show_date'], $r['date_format']);
    431436
    432437                if ( $r['title_li'] )
    433438                        $output .= '</ul></li>';
  • wp-includes/walk-tree.php

     
     1<?php
     2
     3// A class for displaying various tree-like structures. Extend the Walker class to use it, see examples at the bottom
     4
     5class Walker { 
     6        var $tree_type;
     7        var $db_fields;
     8       
     9        //abstract callbacks
     10        function start_lvl() { }
     11        function end_lvl()   { }
     12        function start_el()  { }
     13        function end_el()    { }
     14       
     15        function walk($elements, $to_depth) {
     16                $args = array_slice(func_get_args(), 2); $parents = array(); $depth = 1; $previous_element = ''; $output = '';
     17       
     18                //padding at the end
     19                $last_element->post_parent = 0;
     20                $last_element->post_id = 0;
     21                $elements[] = $last_element;
     22       
     23                $id_field = $this->db_fields['id'];
     24                $parent_field = $this->db_fields['parent'];
     25       
     26                $flat = ($to_depth == -1) ? true : false;
     27       
     28                foreach ( $elements as $element ) {
     29                        // If flat, start and end the element and skip the level checks.
     30                        if ( $flat) {
     31                                // Start the element.
     32                                if ( $element->$id_field != 0 ) {
     33                                        $cb_args = array_merge( array($output, $element, $depth), $args);
     34                                        $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
     35                                }
     36       
     37                                // End the element.
     38                                if ( $element->$id_field != 0 ) {
     39                                        $cb_args = array_merge( array($output, $element, $depth), $args);
     40                                        $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
     41                                }
     42       
     43                                continue;       
     44                        }
     45       
     46                        // Walk the tree.
     47                        if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) {
     48                                // Previous element is my parent. Descend a level.
     49                                array_unshift($parents, $previous_element);
     50                                $depth++; //always do this so when we start the element further down, we know where we are
     51                                if ( !$to_depth || ($depth < $to_depth) ) { //only descend if we're below $to_depth
     52                                        $cb_args = array_merge( array($output, $depth), $args);
     53                                        $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
     54                                }
     55                        } else if ( $element->$parent_field == $previous_element->$parent_field) {
     56                                // On the same level as previous element.
     57                                if ( !$to_depth || ($depth <= $to_depth) ) {
     58                                        $cb_args = array_merge( array($output, $previous_element, $depth), $args);
     59                                        $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
     60                                }
     61                        } else if ( $depth > 1 ) {
     62                                // Ascend one or more levels.
     63                                if ( !$to_depth || ($depth <= $to_depth) ) {
     64                                        $cb_args = array_merge( array($output, $previous_element, $depth), $args);
     65                                        $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
     66                                }
     67       
     68                                while ( $parent = array_shift($parents) ) {
     69                                        $depth--;
     70                                        if ( !$to_depth || ($depth < $to_depth) ) {
     71                                                $cb_args = array_merge( array($output, $depth), $args);
     72                                                $output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
     73                                                $cb_args = array_merge( array($output, $parent, $depth), $args);
     74                                                $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
     75                                        }
     76                                        if ( $element->$parent_field == $parents[0]->$id_field ) {
     77                                                break;
     78                                        }
     79                                }
     80                        } else if ( !empty($previous_element) ) {
     81                                // Close off previous element.
     82                                if ( !$to_depth || ($depth <= $to_depth) ) {
     83                                        $cb_args = array_merge( array($output, $previous_element, $depth), $args);
     84                                        $output = call_user_func_array(array(&$this, 'end_el'), $cb_args);
     85                                }
     86                        }
     87       
     88                        // Start the element.
     89                        if ( !$to_depth || ($depth <= $to_depth) ) {
     90                                if ( $element->$id_field != 0 ) {
     91                                        $cb_args = array_merge( array($output, $element, $depth), $args);
     92                                        $output = call_user_func_array(array(&$this, 'start_el'), $cb_args);
     93                                }
     94                        }
     95       
     96                        $previous_element = $element;
     97                }
     98               
     99                return $output;
     100        }
     101}
     102
     103class Walker_Page extends Walker {
     104        var $tree_type = 'page';
     105        var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this
     106       
     107        function start_lvl($output, $depth) {
     108                $indent = str_repeat("\t", $depth);
     109                $output .= "$indent<ul>\n";
     110                return $output;
     111        }
     112       
     113        function end_lvl($output, $depth) {
     114                $indent = str_repeat("\t", $depth);
     115                $output .= "$indent</ul>\n";
     116                return $output;
     117        }
     118       
     119        function start_el($output, $page, $depth, $current_page, $show_date, $date_format) {
     120                if ( $depth )
     121                        $indent = str_repeat("\t", $depth);
     122
     123                $css_class = 'page_item';
     124                if ( $page->ID == $current_page )
     125                        $css_class .= ' current_page_item';
     126
     127                $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . wp_specialchars($page->post_title) . '">' . $page->post_title . '</a>';
     128       
     129                if ( !empty($show_date) ) {
     130                        if ( 'modified' == $show_date )
     131                                $time = $page->post_modified;
     132                        else
     133                                $time = $page->post_date;
     134       
     135                        $output .= " " . mysql2date($date_format, $time);
     136                }
     137
     138                return $output;
     139        }
     140       
     141        function end_el($output, $page, $depth) {
     142                $output .= "</li>\n";
     143
     144                return $output;
     145        }
     146
     147}
     148
     149class Walker_Category extends Walker {
     150        var $tree_type = 'category';
     151        var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
     152       
     153        function start_lvl($output, $depth, $args) {
     154                if ( 'list' != $args['style'] )
     155                        return $output;
     156       
     157                $indent = str_repeat("\t", $depth);
     158                $output .= "$indent<ul class='children'>\n";
     159                return $output;
     160        }
     161       
     162        function end_lvl($output, $depth, $args) {
     163                if ( 'list' != $args['style'] )
     164                        return $output;
     165       
     166                $indent = str_repeat("\t", $depth);
     167                $output .= "$indent</ul>\n";
     168                return $output;
     169        }
     170       
     171        function start_el($output, $category, $depth, $current_category, $args) {
     172                extract($args);
     173       
     174                $link = '<a href="' . get_category_link($category->cat_ID) . '" ';
     175                if ( $use_desc_for_title == 0 || empty($category->category_description) )
     176                        $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
     177                else
     178                        $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
     179                $link .= '>';
     180                $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
     181       
     182                if ( (! empty($feed_image)) || (! empty($feed)) ) {
     183                        $link .= ' ';
     184       
     185                        if ( empty($feed_image) )
     186                                $link .= '(';
     187       
     188                        $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
     189       
     190                        if ( !empty($feed) ) {
     191                                $title = ' title="' . $feed . '"';
     192                                $alt = ' alt="' . $feed . '"';
     193                                $name = $feed;
     194                                $link .= $title;
     195                        }
     196       
     197                        $link .= '>';
     198       
     199                        if ( !empty($feed_image) )
     200                                $link .= "<img src='$feed_image' $alt$title" . ' />';
     201                        else
     202                                $link .= $name;
     203                        $link .= '</a>';
     204                        if (empty($feed_image))
     205                                $link .= ')';
     206                }
     207       
     208                if ( $show_count )
     209                        $link .= ' ('.intval($category->category_count).')';
     210       
     211                if ( $show_date ) {
     212                        $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
     213                }
     214       
     215                if ( 'list' == $args['style'] ) {
     216                        $output .= "\t<li";
     217                        if ( ($category->cat_ID == $current_category) && is_category() )
     218                                $output .=  ' class="current-cat"';
     219                        $output .= ">$link\n";
     220                } else {
     221                        $output .= "\t$link<br />\n";
     222                }
     223       
     224                return $output;
     225        }
     226       
     227        function end_el($output, $page, $depth) {
     228                if ( 'list' != $args['style'] )
     229                        return $output;
     230       
     231                $output .= "</li>\n";
     232                return $output;
     233        }
     234
     235}
     236
     237class Walker_CategoryDropdown extends Walker {
     238        var $tree_type = 'category';
     239        var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
     240       
     241        function start_el($output, $category, $depth, $current_category, $args) {
     242                $pad = str_repeat('&nbsp;', $depth * 3);
     243               
     244                $cat_name = apply_filters('list_cats', $category->cat_name, $category);
     245                $output .= "\t<option value=\"".$category->cat_ID."\"";
     246                if ( $category->cat_ID == $selected )
     247                        $output .= ' selected="selected"';
     248                $output .= '>';
     249                $output .= $cat_name;
     250                if ( $args['show_count'] )
     251                        $output .= '&nbsp;&nbsp;('. $category->category_count .')';
     252                if ( $args['show_last_update'] ) {
     253                        $format = 'Y-m-d';
     254                        $output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
     255                }
     256                $output .= "</option>\n";
     257               
     258                return $output;
     259        }
     260}
     261
     262?>
     263 No newline at end of file
  • wp-includes/functions.php

     
    705705        }
    706706}
    707707
    708 function walk_tree($tree_type, $elements, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {
    709         $args = array_slice(func_get_args(), 7);
    710         $parents = array();
    711         $depth = 0;
    712         $previous_element = '';
    713         $output = '';
    714 
    715         $last_element->post_parent = 0;
    716         $last_element->post_id = 0;
    717         $elements[] = $last_element;
    718 
    719         if ( 'page' == $tree_type ) {
    720                 $parent_field = 'post_parent';
    721                 $id_field = 'ID';       
    722         } else {
    723                 $parent_field = 'category_parent';
    724                 $id_field = 'cat_ID';                   
    725         }
    726 
    727         $flat = false;
    728         if ( $to_depth == -1 )
    729                 $flat = true;
    730 
    731         foreach ( $elements as $element ) {
    732                 // If flat, start and end the element and skip the level checks.
    733                 if ( $flat) {
    734                         // Start the element.
    735                         if ( !empty($start_element_callback) && ($element->$id_field != 0) ) {
    736                                 $cb_args = array_merge( array($output, $element, $depth), $args);
    737                                 $output = call_user_func_array($start_element_callback, $cb_args);
    738                         }
    739 
    740                         // End the element.
    741                         if ( !empty($end_element_callback) && ($element->$id_field != 0) ) {
    742                                 $cb_args = array_merge( array($output, $element, $depth), $args);
    743                                 $output = call_user_func_array($end_element_callback, $cb_args);
    744                         }
    745 
    746                         continue;       
    747                 }
    748 
    749                 // Walk the tree.
    750                 if ( !empty($previous_element) && ($element->$parent_field == $previous_element->$id_field) ) {
    751                         // Previous element is my parent. Descend a level.
    752                         array_unshift($parents, $previous_element);
    753                         $depth++;
    754                         if ( !$to_depth || ($depth < $to_depth) )
    755                                 if ( !empty($start_level_callback) ) {
    756                                         $cb_args = array_merge( array($output, $depth), $args);
    757                                         $output = call_user_func_array($start_level_callback, $cb_args);
    758                                 }
    759                 } else if ( $depth && ($element->$parent_field == $previous_element->$parent_field) ) {
    760                         // On the same level as previous element.
    761                         if ( !$to_depth || ($depth < $to_depth) ) {
    762                                 if ( !empty($end_element_callback) ) {
    763                                         $cb_args = array_merge( array($output, $previous_element, $depth), $args);
    764                                         $output = call_user_func_array($end_element_callback, $cb_args);
    765                                 }
    766                         }
    767                 } else if ( $depth ) {
    768                         // Ascend one or more levels.
    769                         if ( !$to_depth || ($depth < $to_depth) ) {
    770                                 if ( !empty($end_element_callback) ) {
    771                                         $cb_args = array_merge( array($output, $previous_element, $depth), $args);
    772                                         $output = call_user_func_array($end_element_callback, $cb_args);
    773                                 }
    774                         }
    775 
    776                         while ( $parent = array_shift($parents) ) {
    777                                 $depth--;
    778                                 if ( !$to_depth || ($depth < $to_depth) ) {
    779                                         if ( !empty($end_level_callback) ) {
    780                                                 $cb_args = array_merge( array($output, $depth), $args);
    781                                                 $output = call_user_func_array($end_level_callback, $cb_args);
    782                                         }
    783                                         if ( !empty($end_element_callback) ) {
    784                                                 $cb_args = array_merge( array($output, $parent, $depth), $args);
    785                                                 $output = call_user_func_array($end_element_callback, $cb_args);
    786                                         }
    787                                 }
    788                                 if ( $element->$parent_field == $parents[0]->$id_field ) {
    789                                         break;
    790                                 }
    791                         }
    792                 } else if ( !empty($previous_element) ) {
    793                         // Close off previous element.
    794                         if ( !$to_depth || ($depth < $to_depth) ) {
    795                                 if ( !empty($end_element_callback) ) {
    796                                         $cb_args = array_merge( array($output, $previous_element, $depth), $args);
    797                                         $output = call_user_func_array($end_element_callback, $cb_args);
    798                                 }
    799                         }
    800                 }
    801 
    802                 // Start the element.
    803                 if ( !$to_depth || ($depth < $to_depth) ) {
    804                         if ( !empty($start_element_callback) && ($element->$id_field != 0) ) {
    805                                 $cb_args = array_merge( array($output, $element, $depth), $args);
    806                                 $output = call_user_func_array($start_element_callback, $cb_args);
    807                         }
    808                 }
    809 
    810                 $previous_element = $element;
    811         }
    812 
    813         return $output;
    814 }
    815 
    816 function walk_page_tree($pages, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {
    817         $args = array('page', $pages, $to_depth, $start_element_callback, $end_element_callback, $start_level_callback, $end_level_callback);
    818         $extra_args = array_slice(func_get_args(), 6);
    819 
    820         return call_user_func_array('walk_tree', array_merge($args, $extra_args));
    821 }
    822 
    823 function walk_category_tree($pages, $to_depth, $start_element_callback, $end_element_callback = '', $start_level_callback = '', $end_level_callback = '') {
    824         $args = array('category', $pages, $to_depth, $start_element_callback, $end_element_callback, $start_level_callback, $end_level_callback);
    825         $extra_args = array_slice(func_get_args(), 6);
    826         return call_user_func_array('walk_tree', array_merge($args, $extra_args));
    827 }
    828 
    829708function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
    830709        global $wpdb;
    831710        $category_path = rawurlencode(urldecode($category_path));
  • wp-settings.php

     
    129129$wpdb->show_errors();
    130130
    131131require (ABSPATH . WPINC . '/functions-formatting.php');
     132require (ABSPATH . WPINC . '/walk-tree.php');
    132133require (ABSPATH . WPINC . '/functions-post.php');
    133134require (ABSPATH . WPINC . '/capabilities.php');
    134135require (ABSPATH . WPINC . '/classes.php');