Files
Pascal Martineau e9c92840fc
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m5s
feat: Virtual page redirect, breadcrumb & menu items
2025-09-25 21:28:23 -04:00

126 lines
3.3 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 ) {
if ( $node instanceof \WPGraphQL\Model\Post ) {
return ccat_get_post_breadcrumbs( $node->databaseId );
}
if ( $node instanceof \WPGraphQL\Model\Term ) {
return ccat_get_term_breadcrumbs( $node->databaseId );
}
return array();
}
// Helper: Get breadcrumbs for a given post ID
function ccat_get_post_breadcrumbs( $post_id ) {
$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;
}
$breadcrumbs[] = array(
'label' => 'Accueil',
'to' => '/',
);
$post_type = get_post_type( $post_id );
$ancestor_ids = get_post_ancestors( $post_type === 'page' ? $post_id : get_option( "page_for_$post_type" ) );
$ancestor_ids = array_reverse( $ancestor_ids );
foreach ( $ancestor_ids as $ancestor_id ) {
$breadcrumbs[] = array(
'label' => get_the_title( $ancestor_id ),
'to' => ccat_is_virtual_page( $ancestor_id ) ? null : str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
);
}
$breadcrumbs[] = array(
'label' => get_the_title( $post_id ),
'to' => null,
);
return $breadcrumbs;
}
// Helper: Get breadcrumbs for a given term ID
function ccat_get_term_breadcrumbs( $term_id ) {
$breadcrumbs = array(
array(
'label' => 'Accueil',
'to' => '/',
),
);
$term = get_term( $term_id );
if ( is_wp_error( $term ) || ! $term ) {
return $breadcrumbs;
}
$ancestor_ids = get_ancestors( $term->term_id, $term->taxonomy );
$ancestor_ids = array_reverse( $ancestor_ids );
foreach ( $ancestor_ids as $ancestor_id ) {
$ancestor_term = get_term( $ancestor_id, $term->taxonomy );
if ( is_wp_error( $ancestor_term ) || ! $ancestor_term ) {
continue;
}
$breadcrumbs[] = array(
'label' => $ancestor_term->name,
'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
);
}
$breadcrumbs[] = array(
'label' => $term->name,
'to' => null,
);
return $breadcrumbs;
}