get_bloginfo( 'name' ), 'url' => $home ) ); if ( post_type_exists( $object_type ) ) { $post = get_post( $object_id ); if ( ! $post instanceof WP_Post ) { return $items; } if ( 'product' === $object_type && taxonomy_exists( 'product_cat' ) ) { $leaf = self::primary_leaf( $object_id, 'product_cat' ); if ( $leaf instanceof WP_Term ) { foreach ( self::term_trail( $leaf ) as $crumb ) { if ( '' !== (string) $crumb['url'] ) { $items[] = $crumb; } } } } $items[] = array( 'name' => get_the_title( $post ), 'url' => (string) get_permalink( $post ), 'current' => true ); return $items; } if ( taxonomy_exists( $object_type ) ) { $term = get_term( $object_id, $object_type ); if ( ! $term instanceof WP_Term ) { return $items; } foreach ( self::term_trail( $term ) as $i => $crumb ) { $items[] = $crumb; } $items[ count( $items ) - 1 ]['current'] = true; return $items; } return $items; } public static function current_object() { if ( is_singular() ) { $id = (int) get_queried_object_id(); return $id > 0 ? array( (string) get_post_type( $id ), $id ) : null; } if ( is_category() || is_tag() || is_tax() ) { $t = get_queried_object(); if ( $t instanceof WP_Term ) { return array( (string) $t->taxonomy, (int) $t->term_id ); } } return null; } public static function object_from( $post_id, $taxonomy = '', $term_id = 0 ) { $post_id = (int) $post_id; if ( $post_id > 0 ) { $post = get_post( $post_id ); return ( $post instanceof WP_Post && 'publish' === $post->post_status && is_post_type_viewable( $post->post_type ) ) ? array( (string) $post->post_type, $post_id ) : false; } $term_id = (int) $term_id; if ( $term_id > 0 ) { $taxonomy = sanitize_key( (string) $taxonomy ); $term = get_term( $term_id, ( '' !== $taxonomy && taxonomy_exists( $taxonomy ) ) ? $taxonomy : '' ); return ( $term instanceof WP_Term && is_taxonomy_viewable( $term->taxonomy ) ) ? array( (string) $term->taxonomy, (int) $term->term_id ) : false; } return null; } public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'separator' => '›', 'post_id' => 0, 'taxonomy' => '', 'term_id' => 0 ), is_array( $atts ) ? $atts : array(), 'aieo_breadcrumb' ); return self::render_current( array( 'separator' => (string) $atts['separator'], 'object' => self::object_from( $atts['post_id'], $atts['taxonomy'], $atts['term_id'] ), ) ); } public static function render_current( $args = array() ) { $named = isset( $args['object'] ) ? $args['object'] : null; if ( false === $named ) { return ''; } $resolved = is_array( $named ) ? array_values( $named ) : self::current_object(); if ( ! $resolved ) { return ''; } $items = self::items( $resolved[0], $resolved[1] ); if ( count( $items ) < 2 ) { return ''; } $sep = isset( $args['separator'] ) && '' !== $args['separator'] ? (string) $args['separator'] : '›'; $class = isset( $args['class'] ) ? (string) $args['class'] : 'aieo-breadcrumb'; $last = count( $items ) - 1; $out = ''; return $out; } private static function primary_leaf( $object_id, $taxonomy ) { $cats = get_the_terms( $object_id, $taxonomy ); if ( empty( $cats ) || is_wp_error( $cats ) ) { return null; } $leaf = $cats[0]; $pinned = ( class_exists( 'AIEO_SEO_Primary_Term' ) && method_exists( 'AIEO_SEO_Primary_Term', 'get_primary_cat_id' ) ) ? (int) AIEO_SEO_Primary_Term::get_primary_cat_id( 'product', (int) $object_id ) : 0; if ( $pinned > 0 ) { foreach ( $cats as $c ) { if ( (int) $c->term_id === $pinned ) { $leaf = $c; break; } } } return $leaf; } private static function term_trail( $leaf ) { $out = array(); $ancestors = array_reverse( (array) get_ancestors( $leaf->term_id, $leaf->taxonomy, 'taxonomy' ) ); foreach ( $ancestors as $aid ) { $at = get_term( (int) $aid, $leaf->taxonomy ); if ( $at instanceof WP_Term ) { $l = get_term_link( $at ); if ( ! is_wp_error( $l ) ) { $out[] = array( 'name' => $at->name, 'url' => (string) $l ); } } } $ll = get_term_link( $leaf ); $out[] = array( 'name' => $leaf->name, 'url' => is_wp_error( $ll ) ? '' : (string) $ll ); return $out; } } }