Files
cultureat-bak/wp-content/themes/ccat/includes/graphql/breadcrumbs.php
Pascal Martineau 1817772c50
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 56s
fix: breadcrumbs ancestors
2025-09-24 16:20:32 -04:00

151 lines
4.2 KiB
PHP

<?php
// Register GraphQL types and fields for breadcrumbs
add_action( 'graphql_register_types', 'ccat_register_breadcrumb_graphql_types' );
function ccat_register_breadcrumb_graphql_types() {
register_graphql_object_type(
'BreadcrumbItem',
array(
'description' => 'A single breadcrumb item',
'fields' => array(
'label' => array(
'type' => array( 'non_null' => 'String' ),
'description' => 'The display label for the breadcrumb',
),
'to' => array(
'type' => 'String',
'description' => 'The to for the breadcrumb link (null for current page)',
),
),
)
);
}
// Add 'breadcrumbs' field to all Node types
add_action( 'graphql_register_types', 'ccat_register_breadcrumb_graphql_fields' );
function ccat_register_breadcrumb_graphql_fields() {
register_graphql_field(
'Node',
'breadcrumbs',
array(
'type' => array(
'list_of' => array( 'non_null' => 'BreadcrumbItem' ),
),
'description' => 'Breadcrumb navigation items for this content',
'resolve' => function ( $node ) {
return array_map(
function ( $item ) {
return array(
'label' => html_entity_decode( $item['label'], ENT_QUOTES, 'UTF-8' ),
'to' => $item['to'] ?? null,
);
},
ccat_get_breadcrumbs( $node )
);
},
)
);
}
// Helper: Get breadcrumbs for a given node
function ccat_get_breadcrumbs( $node ) {
$breadcrumbs = array();
if ( ! $node ) {
return $breadcrumbs;
}
if ( $node instanceof \WPGraphQL\Model\Post ) {
$post = get_post( $node->databaseId );
if ( $post ) {
$breadcrumbs = ccat_get_post_breadcrumbs( $post );
$breadcrumbs[] = array(
'label' => get_the_title( $post->ID ),
'to' => null,
);
}
} elseif ( $node instanceof \WPGraphQL\Model\Term ) {
$term = get_term( $node->databaseId );
if ( $term && ! is_wp_error( $term ) ) {
$breadcrumbs = ccat_get_term_breadcrumbs( $term );
$breadcrumbs[] = array(
'label' => $term->name,
'to' => null,
);
}
}
return $breadcrumbs;
}
// Helper: Get breadcrumbs for a given post
function ccat_get_post_breadcrumbs( WP_Post $post ) {
$breadcrumbs = array();
$is_front_page = get_option( 'show_on_front' ) === 'page' && (int) get_option( 'page_on_front' ) === $post->ID;
if ( $is_front_page ) {
return $breadcrumbs; // No breadcrumbs for the front page
} else {
$breadcrumbs[] = array(
'label' => 'Accueil',
'to' => '/',
);
}
$post_type = get_post_type( $post->ID );
switch ( $post_type ) {
case 'page':
$ancestors = get_post_ancestors( $post->ID );
if ( ! empty( $ancestors ) ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor_id ) {
$breadcrumbs[] = array(
'label' => get_the_title( $ancestor_id ),
'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
);
}
}
break;
default:
if ( ! empty( $post_id = get_option( "page_for_$post_type" ) ) ) {
// Get ancestors of the archive page
$ancestors = get_post_ancestors( $post_id );
if ( ! empty( $ancestors ) ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor_id ) {
$breadcrumbs[] = array(
'label' => get_the_title( $ancestor_id ),
'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
);
}
}
// Add the archive page itself
$breadcrumbs[] = array(
'label' => get_the_title( $post_id ),
'to' => str_replace( home_url(), '', get_permalink( $post_id ) ),
);
}
break;
}
return $breadcrumbs;
}
// Helper: Get breadcrumbs for a given term
function ccat_get_term_breadcrumbs( WP_Term $term ) {
$breadcrumbs = array();
$taxonomy = $term->taxonomy;
if ( $term->parent ) {
$ancestors = get_ancestors( $term->term_id, $taxonomy );
if ( ! empty( $ancestors ) ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor_id ) {
$ancestor_term = get_term( $ancestor_id, $taxonomy );
if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) {
$breadcrumbs[] = array(
'label' => $ancestor_term->name,
'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
);
}
}
}
}
return $breadcrumbs;
}