Edit File by line
/home/barbar84/www/wp-inclu...
File: class-wp-walker.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class for displaying various tree-like structures.
[2] Fix | Delete
*
[3] Fix | Delete
* Extend the Walker class to use it, see examples below. Child classes
[4] Fix | Delete
* do not need to implement all of the abstract methods in the class. The child
[5] Fix | Delete
* only needs to implement the methods that are needed.
[6] Fix | Delete
*
[7] Fix | Delete
* @since 2.1.0
[8] Fix | Delete
*
[9] Fix | Delete
* @package WordPress
[10] Fix | Delete
* @abstract
[11] Fix | Delete
*/
[12] Fix | Delete
class Walker {
[13] Fix | Delete
/**
[14] Fix | Delete
* What the class handles.
[15] Fix | Delete
*
[16] Fix | Delete
* @since 2.1.0
[17] Fix | Delete
* @var string
[18] Fix | Delete
*/
[19] Fix | Delete
public $tree_type;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* DB fields to use.
[23] Fix | Delete
*
[24] Fix | Delete
* @since 2.1.0
[25] Fix | Delete
* @var array
[26] Fix | Delete
*/
[27] Fix | Delete
public $db_fields;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Max number of pages walked by the paged walker
[31] Fix | Delete
*
[32] Fix | Delete
* @since 2.7.0
[33] Fix | Delete
* @var int
[34] Fix | Delete
*/
[35] Fix | Delete
public $max_pages = 1;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Whether the current element has children or not.
[39] Fix | Delete
*
[40] Fix | Delete
* To be used in start_el().
[41] Fix | Delete
*
[42] Fix | Delete
* @since 4.0.0
[43] Fix | Delete
* @var bool
[44] Fix | Delete
*/
[45] Fix | Delete
public $has_children;
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* Starts the list before the elements are added.
[49] Fix | Delete
*
[50] Fix | Delete
* The $args parameter holds additional values that may be used with the child
[51] Fix | Delete
* class methods. This method is called at the start of the output list.
[52] Fix | Delete
*
[53] Fix | Delete
* @since 2.1.0
[54] Fix | Delete
* @abstract
[55] Fix | Delete
*
[56] Fix | Delete
* @param string $output Used to append additional content (passed by reference).
[57] Fix | Delete
* @param int $depth Depth of the item.
[58] Fix | Delete
* @param array $args An array of additional arguments.
[59] Fix | Delete
*/
[60] Fix | Delete
public function start_lvl( &$output, $depth = 0, $args = array() ) {}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Ends the list of after the elements are added.
[64] Fix | Delete
*
[65] Fix | Delete
* The $args parameter holds additional values that may be used with the child
[66] Fix | Delete
* class methods. This method finishes the list at the end of output of the elements.
[67] Fix | Delete
*
[68] Fix | Delete
* @since 2.1.0
[69] Fix | Delete
* @abstract
[70] Fix | Delete
*
[71] Fix | Delete
* @param string $output Used to append additional content (passed by reference).
[72] Fix | Delete
* @param int $depth Depth of the item.
[73] Fix | Delete
* @param array $args An array of additional arguments.
[74] Fix | Delete
*/
[75] Fix | Delete
public function end_lvl( &$output, $depth = 0, $args = array() ) {}
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Start the element output.
[79] Fix | Delete
*
[80] Fix | Delete
* The $args parameter holds additional values that may be used with the child
[81] Fix | Delete
* class methods. Includes the element output also.
[82] Fix | Delete
*
[83] Fix | Delete
* @since 2.1.0
[84] Fix | Delete
* @abstract
[85] Fix | Delete
*
[86] Fix | Delete
* @param string $output Used to append additional content (passed by reference).
[87] Fix | Delete
* @param object $object The data object.
[88] Fix | Delete
* @param int $depth Depth of the item.
[89] Fix | Delete
* @param array $args An array of additional arguments.
[90] Fix | Delete
* @param int $current_object_id ID of the current item.
[91] Fix | Delete
*/
[92] Fix | Delete
public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Ends the element output, if needed.
[96] Fix | Delete
*
[97] Fix | Delete
* The $args parameter holds additional values that may be used with the child class methods.
[98] Fix | Delete
*
[99] Fix | Delete
* @since 2.1.0
[100] Fix | Delete
* @abstract
[101] Fix | Delete
*
[102] Fix | Delete
* @param string $output Used to append additional content (passed by reference).
[103] Fix | Delete
* @param object $object The data object.
[104] Fix | Delete
* @param int $depth Depth of the item.
[105] Fix | Delete
* @param array $args An array of additional arguments.
[106] Fix | Delete
*/
[107] Fix | Delete
public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Traverse elements to create list from elements.
[111] Fix | Delete
*
[112] Fix | Delete
* Display one element if the element doesn't have any children otherwise,
[113] Fix | Delete
* display the element and its children. Will only traverse up to the max
[114] Fix | Delete
* depth and no ignore elements under that depth. It is possible to set the
[115] Fix | Delete
* max depth to include all depths, see walk() method.
[116] Fix | Delete
*
[117] Fix | Delete
* This method should not be called directly, use the walk() method instead.
[118] Fix | Delete
*
[119] Fix | Delete
* @since 2.5.0
[120] Fix | Delete
*
[121] Fix | Delete
* @param object $element Data object.
[122] Fix | Delete
* @param array $children_elements List of elements to continue traversing (passed by reference).
[123] Fix | Delete
* @param int $max_depth Max depth to traverse.
[124] Fix | Delete
* @param int $depth Depth of current element.
[125] Fix | Delete
* @param array $args An array of arguments.
[126] Fix | Delete
* @param string $output Used to append additional content (passed by reference).
[127] Fix | Delete
*/
[128] Fix | Delete
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
[129] Fix | Delete
if ( ! $element ) {
[130] Fix | Delete
return;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
$id_field = $this->db_fields['id'];
[134] Fix | Delete
$id = $element->$id_field;
[135] Fix | Delete
[136] Fix | Delete
// Display this element.
[137] Fix | Delete
$this->has_children = ! empty( $children_elements[ $id ] );
[138] Fix | Delete
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
[139] Fix | Delete
$args[0]['has_children'] = $this->has_children; // Back-compat.
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
$this->start_el( $output, $element, $depth, ...array_values( $args ) );
[143] Fix | Delete
[144] Fix | Delete
// Descend only when the depth is right and there are children for this element.
[145] Fix | Delete
if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
[146] Fix | Delete
[147] Fix | Delete
foreach ( $children_elements[ $id ] as $child ) {
[148] Fix | Delete
[149] Fix | Delete
if ( ! isset( $newlevel ) ) {
[150] Fix | Delete
$newlevel = true;
[151] Fix | Delete
// Start the child delimiter.
[152] Fix | Delete
$this->start_lvl( $output, $depth, ...array_values( $args ) );
[153] Fix | Delete
}
[154] Fix | Delete
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
[155] Fix | Delete
}
[156] Fix | Delete
unset( $children_elements[ $id ] );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
if ( isset( $newlevel ) && $newlevel ) {
[160] Fix | Delete
// End the child delimiter.
[161] Fix | Delete
$this->end_lvl( $output, $depth, ...array_values( $args ) );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
// End this element.
[165] Fix | Delete
$this->end_el( $output, $element, $depth, ...array_values( $args ) );
[166] Fix | Delete
}
[167] Fix | Delete
[168] Fix | Delete
/**
[169] Fix | Delete
* Display array of elements hierarchically.
[170] Fix | Delete
*
[171] Fix | Delete
* Does not assume any existing order of elements.
[172] Fix | Delete
*
[173] Fix | Delete
* $max_depth = -1 means flatly display every element.
[174] Fix | Delete
* $max_depth = 0 means display all levels.
[175] Fix | Delete
* $max_depth > 0 specifies the number of display levels.
[176] Fix | Delete
*
[177] Fix | Delete
* @since 2.1.0
[178] Fix | Delete
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
[179] Fix | Delete
* to the function signature.
[180] Fix | Delete
*
[181] Fix | Delete
* @param array $elements An array of elements.
[182] Fix | Delete
* @param int $max_depth The maximum hierarchical depth.
[183] Fix | Delete
* @param mixed ...$args Optional additional arguments.
[184] Fix | Delete
* @return string The hierarchical item output.
[185] Fix | Delete
*/
[186] Fix | Delete
public function walk( $elements, $max_depth, ...$args ) {
[187] Fix | Delete
$output = '';
[188] Fix | Delete
[189] Fix | Delete
// Invalid parameter or nothing to walk.
[190] Fix | Delete
if ( $max_depth < -1 || empty( $elements ) ) {
[191] Fix | Delete
return $output;
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
$parent_field = $this->db_fields['parent'];
[195] Fix | Delete
[196] Fix | Delete
// Flat display.
[197] Fix | Delete
if ( -1 == $max_depth ) {
[198] Fix | Delete
$empty_array = array();
[199] Fix | Delete
foreach ( $elements as $e ) {
[200] Fix | Delete
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
[201] Fix | Delete
}
[202] Fix | Delete
return $output;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/*
[206] Fix | Delete
* Need to display in hierarchical order.
[207] Fix | Delete
* Separate elements into two buckets: top level and children elements.
[208] Fix | Delete
* Children_elements is two dimensional array, eg.
[209] Fix | Delete
* Children_elements[10][] contains all sub-elements whose parent is 10.
[210] Fix | Delete
*/
[211] Fix | Delete
$top_level_elements = array();
[212] Fix | Delete
$children_elements = array();
[213] Fix | Delete
foreach ( $elements as $e ) {
[214] Fix | Delete
if ( empty( $e->$parent_field ) ) {
[215] Fix | Delete
$top_level_elements[] = $e;
[216] Fix | Delete
} else {
[217] Fix | Delete
$children_elements[ $e->$parent_field ][] = $e;
[218] Fix | Delete
}
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
/*
[222] Fix | Delete
* When none of the elements is top level.
[223] Fix | Delete
* Assume the first one must be root of the sub elements.
[224] Fix | Delete
*/
[225] Fix | Delete
if ( empty( $top_level_elements ) ) {
[226] Fix | Delete
[227] Fix | Delete
$first = array_slice( $elements, 0, 1 );
[228] Fix | Delete
$root = $first[0];
[229] Fix | Delete
[230] Fix | Delete
$top_level_elements = array();
[231] Fix | Delete
$children_elements = array();
[232] Fix | Delete
foreach ( $elements as $e ) {
[233] Fix | Delete
if ( $root->$parent_field == $e->$parent_field ) {
[234] Fix | Delete
$top_level_elements[] = $e;
[235] Fix | Delete
} else {
[236] Fix | Delete
$children_elements[ $e->$parent_field ][] = $e;
[237] Fix | Delete
}
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
foreach ( $top_level_elements as $e ) {
[242] Fix | Delete
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/*
[246] Fix | Delete
* If we are displaying all levels, and remaining children_elements is not empty,
[247] Fix | Delete
* then we got orphans, which should be displayed regardless.
[248] Fix | Delete
*/
[249] Fix | Delete
if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
[250] Fix | Delete
$empty_array = array();
[251] Fix | Delete
foreach ( $children_elements as $orphans ) {
[252] Fix | Delete
foreach ( $orphans as $op ) {
[253] Fix | Delete
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
[254] Fix | Delete
}
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
return $output;
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
/**
[262] Fix | Delete
* paged_walk() - produce a page of nested elements
[263] Fix | Delete
*
[264] Fix | Delete
* Given an array of hierarchical elements, the maximum depth, a specific page number,
[265] Fix | Delete
* and number of elements per page, this function first determines all top level root elements
[266] Fix | Delete
* belonging to that page, then lists them and all of their children in hierarchical order.
[267] Fix | Delete
*
[268] Fix | Delete
* $max_depth = 0 means display all levels.
[269] Fix | Delete
* $max_depth > 0 specifies the number of display levels.
[270] Fix | Delete
*
[271] Fix | Delete
* @since 2.7.0
[272] Fix | Delete
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
[273] Fix | Delete
* to the function signature.
[274] Fix | Delete
*
[275] Fix | Delete
* @param array $elements
[276] Fix | Delete
* @param int $max_depth The maximum hierarchical depth.
[277] Fix | Delete
* @param int $page_num The specific page number, beginning with 1.
[278] Fix | Delete
* @param int $per_page
[279] Fix | Delete
* @param mixed ...$args Optional additional arguments.
[280] Fix | Delete
* @return string XHTML of the specified page of elements
[281] Fix | Delete
*/
[282] Fix | Delete
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
[283] Fix | Delete
if ( empty( $elements ) || $max_depth < -1 ) {
[284] Fix | Delete
return '';
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
$output = '';
[288] Fix | Delete
[289] Fix | Delete
$parent_field = $this->db_fields['parent'];
[290] Fix | Delete
[291] Fix | Delete
$count = -1;
[292] Fix | Delete
if ( -1 == $max_depth ) {
[293] Fix | Delete
$total_top = count( $elements );
[294] Fix | Delete
}
[295] Fix | Delete
if ( $page_num < 1 || $per_page < 0 ) {
[296] Fix | Delete
// No paging.
[297] Fix | Delete
$paging = false;
[298] Fix | Delete
$start = 0;
[299] Fix | Delete
if ( -1 == $max_depth ) {
[300] Fix | Delete
$end = $total_top;
[301] Fix | Delete
}
[302] Fix | Delete
$this->max_pages = 1;
[303] Fix | Delete
} else {
[304] Fix | Delete
$paging = true;
[305] Fix | Delete
$start = ( (int) $page_num - 1 ) * (int) $per_page;
[306] Fix | Delete
$end = $start + $per_page;
[307] Fix | Delete
if ( -1 == $max_depth ) {
[308] Fix | Delete
$this->max_pages = ceil( $total_top / $per_page );
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
// Flat display.
[313] Fix | Delete
if ( -1 == $max_depth ) {
[314] Fix | Delete
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
[315] Fix | Delete
$elements = array_reverse( $elements );
[316] Fix | Delete
$oldstart = $start;
[317] Fix | Delete
$start = $total_top - $end;
[318] Fix | Delete
$end = $total_top - $oldstart;
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
$empty_array = array();
[322] Fix | Delete
foreach ( $elements as $e ) {
[323] Fix | Delete
$count++;
[324] Fix | Delete
if ( $count < $start ) {
[325] Fix | Delete
continue;
[326] Fix | Delete
}
[327] Fix | Delete
if ( $count >= $end ) {
[328] Fix | Delete
break;
[329] Fix | Delete
}
[330] Fix | Delete
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
[331] Fix | Delete
}
[332] Fix | Delete
return $output;
[333] Fix | Delete
}
[334] Fix | Delete
[335] Fix | Delete
/*
[336] Fix | Delete
* Separate elements into two buckets: top level and children elements.
[337] Fix | Delete
* Children_elements is two dimensional array, e.g.
[338] Fix | Delete
* $children_elements[10][] contains all sub-elements whose parent is 10.
[339] Fix | Delete
*/
[340] Fix | Delete
$top_level_elements = array();
[341] Fix | Delete
$children_elements = array();
[342] Fix | Delete
foreach ( $elements as $e ) {
[343] Fix | Delete
if ( 0 == $e->$parent_field ) {
[344] Fix | Delete
$top_level_elements[] = $e;
[345] Fix | Delete
} else {
[346] Fix | Delete
$children_elements[ $e->$parent_field ][] = $e;
[347] Fix | Delete
}
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
$total_top = count( $top_level_elements );
[351] Fix | Delete
if ( $paging ) {
[352] Fix | Delete
$this->max_pages = ceil( $total_top / $per_page );
[353] Fix | Delete
} else {
[354] Fix | Delete
$end = $total_top;
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
[358] Fix | Delete
$top_level_elements = array_reverse( $top_level_elements );
[359] Fix | Delete
$oldstart = $start;
[360] Fix | Delete
$start = $total_top - $end;
[361] Fix | Delete
$end = $total_top - $oldstart;
[362] Fix | Delete
}
[363] Fix | Delete
if ( ! empty( $args[0]['reverse_children'] ) ) {
[364] Fix | Delete
foreach ( $children_elements as $parent => $children ) {
[365] Fix | Delete
$children_elements[ $parent ] = array_reverse( $children );
[366] Fix | Delete
}
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
foreach ( $top_level_elements as $e ) {
[370] Fix | Delete
$count++;
[371] Fix | Delete
[372] Fix | Delete
// For the last page, need to unset earlier children in order to keep track of orphans.
[373] Fix | Delete
if ( $end >= $total_top && $count < $start ) {
[374] Fix | Delete
$this->unset_children( $e, $children_elements );
[375] Fix | Delete
}
[376] Fix | Delete
[377] Fix | Delete
if ( $count < $start ) {
[378] Fix | Delete
continue;
[379] Fix | Delete
}
[380] Fix | Delete
[381] Fix | Delete
if ( $count >= $end ) {
[382] Fix | Delete
break;
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
if ( $end >= $total_top && count( $children_elements ) > 0 ) {
[389] Fix | Delete
$empty_array = array();
[390] Fix | Delete
foreach ( $children_elements as $orphans ) {
[391] Fix | Delete
foreach ( $orphans as $op ) {
[392] Fix | Delete
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
[393] Fix | Delete
}
[394] Fix | Delete
}
[395] Fix | Delete
}
[396] Fix | Delete
[397] Fix | Delete
return $output;
[398] Fix | Delete
}
[399] Fix | Delete
[400] Fix | Delete
/**
[401] Fix | Delete
* Calculates the total number of root elements.
[402] Fix | Delete
*
[403] Fix | Delete
* @since 2.7.0
[404] Fix | Delete
*
[405] Fix | Delete
* @param array $elements Elements to list.
[406] Fix | Delete
* @return int Number of root elements.
[407] Fix | Delete
*/
[408] Fix | Delete
public function get_number_of_root_elements( $elements ) {
[409] Fix | Delete
$num = 0;
[410] Fix | Delete
$parent_field = $this->db_fields['parent'];
[411] Fix | Delete
[412] Fix | Delete
foreach ( $elements as $e ) {
[413] Fix | Delete
if ( 0 == $e->$parent_field ) {
[414] Fix | Delete
$num++;
[415] Fix | Delete
}
[416] Fix | Delete
}
[417] Fix | Delete
return $num;
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
/**
[421] Fix | Delete
* Unset all the children for a given top level element.
[422] Fix | Delete
*
[423] Fix | Delete
* @since 2.7.0
[424] Fix | Delete
*
[425] Fix | Delete
* @param object $e
[426] Fix | Delete
* @param array $children_elements
[427] Fix | Delete
*/
[428] Fix | Delete
public function unset_children( $e, &$children_elements ) {
[429] Fix | Delete
if ( ! $e || ! $children_elements ) {
[430] Fix | Delete
return;
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
$id_field = $this->db_fields['id'];
[434] Fix | Delete
$id = $e->$id_field;
[435] Fix | Delete
[436] Fix | Delete
if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
[437] Fix | Delete
foreach ( (array) $children_elements[ $id ] as $child ) {
[438] Fix | Delete
$this->unset_children( $child, $children_elements );
[439] Fix | Delete
}
[440] Fix | Delete
}
[441] Fix | Delete
[442] Fix | Delete
unset( $children_elements[ $id ] );
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
}
[446] Fix | Delete
[447] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function