f( '<%2$s>%1$s%2$s>', $label, $labelTag ) )
: '';
}
/**
* Generates the HTML for a non-hierarchical list of objects.
*
* @since 4.1.3
*
* @param array $objects The object.
* @return string The HTML code.
*/
private function generateList( $objects ) {
$list = '
';
foreach ( $objects as $object ) {
$list .= $this->generateListItem( $object ) . '';
}
return $list . '
';
}
/**
* Generates a list item for an object (without the closing tag).
* We cannot close it as the caller might need to generate a hierarchical structure inside the list item.
*
* @since 4.1.3
*
* @param array $object The object.
* @return string The HTML code.
*/
private function generateListItem( $object ) {
$li = '';
if ( ! empty( $object['title'] ) ) {
$li .= '';
// add nofollow to the link.
if ( filter_var( $this->attributes['nofollow_links'], FILTER_VALIDATE_BOOLEAN ) ) {
$li .= sprintf(
'',
esc_url( $object['loc'] ),
'rel="nofollow"',
$this->attributes['is_admin'] ? 'target="_blank"' : ''
);
} else {
$li .= sprintf(
'',
esc_url( $object['loc'] ),
$this->attributes['is_admin'] ? 'target="_blank"' : ''
);
}
$li .= sprintf( '%s', esc_attr( $object['title'] ) );
// add publication date on the list item.
if ( ! empty( $object['date'] ) && filter_var( $this->attributes['publication_date'], FILTER_VALIDATE_BOOLEAN ) ) {
$li .= sprintf( ' (%s)', esc_attr( $object['date'] ) );
}
$li .= '';
}
return $li;
}
/**
* Generates the HTML for a hierarchical list of objects.
*
* @since 4.1.3
*
* @param array $objects The objects.
* @return string The HTML of the hierarchical objects section.
*/
private function generateHierarchicalList( $objects ) {
if ( empty( $objects ) ) {
return '';
}
$objects = $this->buildHierarchicalTree( $objects );
$list = '';
foreach ( $objects as $object ) {
$list .= $this->generateListItem( $object );
if ( ! empty( $object['children'] ) ) {
$list .= $this->generateHierarchicalTree( $object );
}
$list .= '
';
}
$list .= '';
return $list;
}
/**
* Recursive helper function for generateHierarchicalList().
* Generates hierarchical structure for objects with child objects.
*
* @since 4.1.3
*
* @param array $object The object.
* @return string The HTML code of the hierarchical tree.
*/
private function generateHierarchicalTree( $object ) {
static $nestedLevel = 0;
$tree = '';
foreach ( $object['children'] as $child ) {
$nestedLevel++;
$tree .= $this->generateListItem( $child );
if ( ! empty( $child['children'] ) ) {
$tree .= $this->generateHierarchicalTree( $child );
}
$tree .= '';
}
$tree .= '
';
return $tree;
}
/**
* Builds the structure for hierarchical objects that have a parent.
*
* @since 4.1.3
* @version 4.2.8
*
* @param array $objects The list of hierarchical objects.
* @return array Multidimensional array with the hierarchical structure.
*/
private function buildHierarchicalTree( $objects ) {
$topLevelIds = [];
$objects = json_decode( wp_json_encode( $objects ) );
foreach ( $objects as $listItem ) {
// Create an array of top level IDs for later reference.
if ( empty( $listItem->parent ) ) {
array_push( $topLevelIds, $listItem->id );
}
// Create an array of children that belong to the current item.
$children = array_filter( $objects, function( $child ) use ( $listItem ) {
if ( ! empty( $child->parent ) ) {
return absint( $child->parent ) === absint( $listItem->id );
}
} );
if ( ! empty( $children ) ) {
$listItem->children = $children;
}
}
// Remove child objects from the root level since they've all been nested.
$objects = array_filter( $objects, function ( $item ) use ( $topLevelIds ) {
return in_array( $item->id, $topLevelIds, true );
} );
return array_values( json_decode( wp_json_encode( $objects ), true ) );
}
/**
* Returns the names of the included post types or taxonomies.
*
* @since 4.1.3
*
* @param array|string $objects The included post types/taxonomies.
* @param boolean $arePostTypes Whether the objects are post types.
* @return array The names of the included post types/taxonomies.
*/
private function getIncludedObjects( $objects, $arePostTypes = true ) {
if ( is_array( $objects ) ) {
return $objects;
}
if ( empty( $objects ) ) {
return [];
}
$exploded = explode( ',', $objects );
$objects = array_map( function( $object ) {
return trim( $object );
}, $exploded );
$publicObjects = $arePostTypes
? aioseo()->helpers->getPublicPostTypes( true )
: aioseo()->helpers->getPublicTaxonomies( true );
$objects = array_filter( $objects, function( $object ) use ( $publicObjects ) {
return in_array( $object, $publicObjects, true );
});
return $objects;
}
}