_Post $post An ID or a WP_Post object. * @param string $type The crumb type. * @return array An array of the post parent crumbs. */ public function getPostParentCrumbs( $post, $type = 'single' ) { $crumbs = []; if ( ! is_post_type_hierarchical( get_post_type( $post ) ) ) { return $crumbs; } $postHierarchy = $this->getPostHierarchy( $post ); if ( ! empty( $postHierarchy ) ) { foreach ( $postHierarchy as $parentID ) { // Do not include the Home Page. if ( aioseo()->helpers->getHomePageId() === $parentID ) { continue; } $crumbs[] = $this->getPostCrumb( get_post( $parentID ), $type, 'parent' ); } } return $crumbs; } /** * Function to extend on pro for extra functionality. * * @since 4.1.1 * * @param string $type The type of breadcrumb. * @param mixed $reference The breadcrumb reference. * @return bool Show current item. */ public function showCurrentItem( $type = null, $reference = null ) { return apply_filters( 'aioseo_breadcrumbs_show_current_item', aioseo()->options->breadcrumbs->showCurrentItem, $type, $reference ); } /** * Gets a home page crumb. * * @since 4.1.1 * * @param string $type The type of breadcrumb. * @param mixed $reference The breadcrumb reference. * @return array|void The home crumb. */ public function maybeGetHomePageCrumb( $type = null, $reference = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable if ( aioseo()->options->breadcrumbs->homepageLink ) { return $this->getHomePageCrumb(); } } /** * Gets a home page crumb. * * @since 4.1.1 * * @return array The home crumb. */ public function getHomePageCrumb() { $homePageId = aioseo()->helpers->getHomePageId(); $label = ''; if ( $homePageId ) { $label = get_the_title( $homePageId ); } if ( 0 < strlen( aioseo()->options->breadcrumbs->homepageLabel ) ) { $label = aioseo()->options->breadcrumbs->homepageLabel; } // Label fallback. if ( empty( $label ) ) { $label = __( 'Home', 'all-in-one-seo-pack' ); } return $this->makeCrumb( $label, get_home_url(), 'homePage', aioseo()->helpers->getHomePage() ); } /** * Gets the blog crumb. * * @since 4.1.1 * * @return array The blog crumb. */ public function getBlogCrumb() { $crumb = []; $blogPage = aioseo()->helpers->getBlogPage(); if ( null !== $blogPage ) { $crumb = $this->makeCrumb( $blogPage->post_title, get_permalink( $blogPage ), 'blog', $blogPage ); } return $crumb; } /** * Maybe add the shop crumb to products and product categories. * * @since 4.5.5 * * @return array The shop crumb. */ public function maybeGetWooCommerceShopCrumb() { $crumb = []; if ( aioseo()->helpers->isWooCommerceShopPage() || aioseo()->helpers->isWooCommerceProductPage() || aioseo()->helpers->isWooCommerceTaxonomyPage() ) { $crumb = $this->getWooCommerceShopCrumb(); } return $crumb; } /** * Gets the shop crumb. * @see WC_Breadcrumb::prepend_shop_page() * * @since 4.5.5 * * @return array The shop crumb. */ public function getWooCommerceShopCrumb() { $crumb = []; if ( ! function_exists( 'wc_get_page_id' ) ) { return $crumb; } $shopPageId = wc_get_page_id( 'shop' ); $shopPage = get_post( $shopPageId ); // WC checks if the permalink contains the shop page in the URI, but we prefer to // always show the shop page as the first crumb if it exists and it's not the home page. if ( $shopPageId && $shopPage && aioseo()->helpers->getHomePageId() !== $shopPageId ) { $crumb = $this->makeCrumb( get_the_title( $shopPage ), get_permalink( $shopPage ), 'wcShop' ); } return $crumb; } /** * Gets a post's term hierarchy for a list of taxonomies selecting the one that has a lengthier hierarchy. * * @since 4.1.1 * * @param int|\WP_Post $post 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 ); } } }