*/ public static function defaults(): array { return array( 'enabled' => true, 'home_label' => '', 'separator' => '›', 'show_current' => true, 'prefix' => '', ); } public function register( Container $container ): void {} public function boot( Container $container ): void { add_action( 'rest_api_init', array( $this, 'routes' ) ); add_shortcode( 'airanksuite_breadcrumbs', array( $this, 'shortcode' ) ); } /** * Build the trail for the current request. * * @return array */ public function trail(): array { $settings = $this->settings(); $home = '' !== trim( (string) $settings['home_label'] ) ? (string) $settings['home_label'] : __( 'Home', 'ai-rank-suite' ); $items = array( array( 'label' => $home, 'url' => home_url( '/' ), ), ); if ( is_singular() ) { $post = get_queried_object(); if ( $post instanceof \WP_Post ) { if ( 'post' === $post->post_type ) { $primary = $this->primary_term( $post ); if ( $primary ) { foreach ( array_reverse( (array) get_ancestors( $primary->term_id, $primary->taxonomy ) ) as $ancestor_id ) { $ancestor = get_term( $ancestor_id, $primary->taxonomy ); if ( $ancestor instanceof \WP_Term ) { $items[] = array( 'label' => $ancestor->name, 'url' => (string) get_term_link( $ancestor ), ); } } $items[] = array( 'label' => $primary->name, 'url' => (string) get_term_link( $primary ), ); } } else { foreach ( array_reverse( get_post_ancestors( $post ) ) as $ancestor_id ) { $ancestor = get_post( $ancestor_id ); if ( ! $ancestor instanceof \WP_Post ) { continue; } $items[] = array( 'label' => \AiRankSuite\Support\PostSeo::breadcrumb_title( $ancestor ), 'url' => (string) get_permalink( $ancestor_id ), ); } } if ( ! empty( $settings['show_current'] ) ) { $items[] = array( 'label' => wp_strip_all_tags( \AiRankSuite\Support\PostSeo::breadcrumb_title( $post ) ), 'url' => (string) get_permalink( $post ), ); } } } elseif ( is_category() || is_tag() || is_tax() ) { $term = get_queried_object(); if ( $term instanceof \WP_Term ) { $items[] = array( 'label' => $term->name, 'url' => (string) get_term_link( $term ), ); } } elseif ( is_search() ) { $items[] = array( /* translators: %s: search query */ 'label' => sprintf( __( 'Search: %s', 'ai-rank-suite' ), get_search_query() ), 'url' => '', ); } elseif ( is_404() ) { $items[] = array( 'label' => __( 'Page not found', 'ai-rank-suite' ), 'url' => '', ); } elseif ( is_author() ) { $author = get_queried_object(); if ( $author instanceof \WP_User ) { $items[] = array( 'label' => $author->display_name, 'url' => (string) get_author_posts_url( $author->ID ), ); } } elseif ( is_post_type_archive() ) { $obj = get_queried_object(); if ( $obj instanceof \WP_Post_Type ) { $items[] = array( 'label' => $obj->labels->name, 'url' => (string) get_post_type_archive_link( $obj->name ), ); } } /** * Filter the breadcrumb trail. * * @param array $items Trail items. */ return (array) apply_filters( 'airanksuite/breadcrumbs/trail', $items ); } /** * Primary category: the one flagged by us, else the first assigned. */ private function primary_term( \WP_Post $post ): ?\WP_Term { $primary_id = (int) get_post_meta( $post->ID, '_airanksuite_primary_term', true ); if ( $primary_id > 0 ) { $term = get_term( $primary_id, 'category' ); if ( $term instanceof \WP_Term ) { return $term; } } $terms = get_the_terms( $post, 'category' ); return is_array( $terms ) && ! empty( $terms ) ? $terms[0] : null; } /** * Rendered HTML trail. */ public function render(): string { $settings = $this->settings(); if ( empty( $settings['enabled'] ) ) { return ''; } $items = $this->trail(); if ( count( $items ) < 2 ) { return ''; } $sep = (string) $settings['separator']; $html = ''; } /** * @param array|string $atts Shortcode attributes. */ public function shortcode( $atts = array() ): string { return $this->render(); } public function routes(): void { register_rest_route( self::NAMESPACE, '/settings/breadcrumbs', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'enabled' => array( 'type' => 'boolean' ), 'home_label' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'separator' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'show_current' => array( 'type' => 'boolean' ), 'prefix' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); } public function get_settings(): WP_REST_Response { return new WP_REST_Response( array( 'settings' => $this->settings(), 'preview' => $this->preview(), ) ); } public function update_settings( WP_REST_Request $request ): WP_REST_Response { $defaults = self::defaults(); $values = array(); foreach ( array_keys( $defaults ) as $key ) { if ( $request->has_param( $key ) ) { $values[ $key ] = $request->get_param( $key ); } } \AiRankSuite\Kernel::service( Options::class )->set( 'breadcrumbs', $values, $defaults ); return $this->get_settings(); } /** * Example trail for the settings screen. * * @return array */ private function preview(): array { $settings = $this->settings(); $home = '' !== trim( (string) $settings['home_label'] ) ? (string) $settings['home_label'] : __( 'Home', 'ai-rank-suite' ); $items = array( $home, __( 'Category', 'ai-rank-suite' ) ); if ( ! empty( $settings['show_current'] ) ) { $items[] = __( 'Your page title', 'ai-rank-suite' ); } return $items; } /** @return array */ private function settings(): array { return \AiRankSuite\Kernel::service( Options::class )->get( 'breadcrumbs', self::defaults() ); } }