I upgraded WordPress to 4.5.1 from 4.2.2 and it has broken the functionality I have extending the Walker class. The Walker extension:
class ReturnWalker extends Walker{
public $db_fields = array( 'parent' => 'parent',
'id' => 'term_id' );
public $show_all = FALSE;
/*
*
* @param array
* @param object
* @param int
* @param
* @return NULL
*/
public function start_el( &$output, $category, $depth = 0, $args = array(), $current_object_id = 0) {
if( !is_array($output) )
$output = array();
$id = $this->db_fields['id'];
$parent = $this->db_fields['parent'];
if( $depth == 0 || (!$category->$parent) ){
$output[$category->term_id] = $category;
} else if( $depth > 0){
$this->search( $output, $category->$parent, $category );
}
}
/*
*
* @param array
* @param int
* @param object
*/
private function search( &$output, $key, $category){
$id = $this->db_fields['id'];
$parent = $this->db_fields['parent'];
if( count($output) ){
foreach( $output as $k => &$object){
if( $k == $key ){
$object->children[$category->term_id] = $category;
return;
} else if( isset($object->children[$key]) ){
$this->search( $object->children, $category->$parent, $category );
}
}
} else if( $this->show_all){
$output[$category->$id] = $category;
}
}
/*
* sets the field names used for id and parent id
* @param array(
* 'id' => string
* 'parent' => string
* )
* @return NULL
*/
public function setDbFields( array $db_fields){
$this->db_fields = array_merge( $this->db_fields, $db_fields );
}
}
takes categories retrieved by using “get_the_terms()” and passes it in to the walk method:
$breadcrumbs = new ReturnWalker;
$category = get_the_terms( $post->ID, 'category' );
$struct = $breadcrumbs->walk( $category, 0 );
After some careful observation I noticed that $category is now an array of WP_Term objects and that the WP_Term class is final. This breaks the extension because it depended on the ability to add the property “children”, but because the class is non-mutable, is no longer possible.
The end product is that the functionality would build out URLs and breadcrumbs based off of the custom taxonomy which the post lived under. Does anyone have any good solutions to this type of issue?
Update –
Creating a class that mimics WP_Terms and copying over the objects returned by get_the_terms() to my *_Terms class worked. Passing in the returned objects when instantiating my class hits the constructor and builds out a copy of the properties that I have control over without worrying about core code changes:
$categoryTerms = get_the_terms( $post->ID, 'category' );
foreach( $categoryTerms as $categoryTerm ){
$category[] = new MyTerm( $categoryTerm );
}
$breadcrumbs = new MyWalker;
$struct = $breadcrumbs->walk( $category, 0, array());