An ID or a WP_Post object. * @param array $taxonomies An array of taxonomy names. * @param false $skipUnselectedTerms Allow unselected terms to be filtered out from the crumbs. * @return array An array of the taxonomy name + a term hierarchy. */ public function getPostTaxTermHierarchy( $post, $taxonomies = [], $skipUnselectedTerms = false ) { // Get all taxonomies attached to the post. if ( empty( $taxonomies ) ) { $taxonomies = get_object_taxonomies( get_post_type( $post ), 'objects' ); $taxonomies = wp_filter_object_list( $taxonomies, [ 'public' => true ], 'and', 'name' ); } foreach ( $taxonomies as $taxonomy ) { $primaryTerm = aioseo()->standalone->primaryTerm->getPrimaryTerm( $post->ID, $taxonomy ); $terms = wp_get_object_terms( $post->ID, $taxonomy ); // Use the first taxonomy with terms. if ( empty( $terms ) || is_wp_error( $terms ) ) { continue; } // Determines the lengthier term hierarchy. $termHierarchy = []; foreach ( $terms as $term ) { // Gets our filtered ancestors. $ancestors = $this->getFilteredTermHierarchy( $term->term_id, $term->taxonomy, $skipUnselectedTerms ? $terms : [] ); // Merge the current term to be used in the breadcrumbs. $ancestors = array_merge( $ancestors, [ $term->term_id ] ); // If the current term is the primary term, use it. if ( is_a( $primaryTerm, 'WP_Term' ) && $primaryTerm->term_id === $term->term_id ) { $termHierarchy = $ancestors; break; } $termHierarchy = ( count( $termHierarchy ) < count( $ancestors ) ) ? $ancestors : $termHierarchy; } // Return a top to bottom hierarchy. return [ 'taxonomy' => $taxonomy, 'terms' => $termHierarchy ]; } return []; } /** * Filters a term's parent hierarchy against other terms. * * @since 4.1.1 * * @param int $termId A term id. * @param string $taxonomy The taxonomy name. * @param array $termsToFilterAgainst Terms to filter out of the hierarchy. * @return array The term's parent hierarchy. */ public function getFilteredTermHierarchy( $termId, $taxonomy, $termsToFilterAgainst = [] ) { $ancestors = $this->getTermHierarchy( $termId, $taxonomy ); // Keep only selected terms in the hierarchy. if ( ! empty( $termsToFilterAgainst ) ) { // If it's a WP_Term array make it a term_id array. if ( is_a( current( $termsToFilterAgainst ), 'WP_Term' ) ) { $termsToFilterAgainst = wp_list_pluck( $termsToFilterAgainst, 'term_id' ); } $ancestors = array_intersect( $ancestors, $termsToFilterAgainst ); } return $ancestors; } /** * Gets a term's parent hierarchy. * * @since 4.1.1 * * @param int $termId A term id. * @param string $taxonomy A taxonomy name. * @return array The term parent hierarchy. */ public function getTermHierarchy( $termId, $taxonomy ) { // Return a top to bottom hierarchy. return array_reverse( get_ancestors( $termId, $taxonomy, 'taxonomy' ) ); } /** * Gets a post's parent hierarchy. * * @since 4.1.1 * * @param int|\WP_Post $post An ID or a WP_Post object. * @return array The post parent hierarchy. */ public function getPostHierarchy( $post ) { $postId = ! empty( $post->ID ) ? $post->ID : $post; // Return a top to bottom hierarchy. return array_reverse( get_ancestors( $postId, '', 'post_type' ) ); } /** * Register our breadcrumb widget. * * @since 4.1.1 * * @return void */ public function registerWidget() { if ( aioseo()->helpers->canRegisterLegacyWidget( 'aioseo-breadcrumb-widget' ) ) { register_widget( 'AIOSEO\Plugin\Common\Breadcrumbs\Widget' ); } } } } namespace { // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } if ( ! function_exists( 'aioseo_breadcrumbs' ) ) { /** * Global function for breadcrumbs output. * * @since 4.1.1 * * @param boolean $echo Echo or return the output. * @return string|void The output. */ function aioseo_breadcrumbs( $echo = true ) { return aioseo()->breadcrumbs->frontend->display( $echo ); } } }