refactor: breadcrumbs stuff

This commit is contained in:
2025-09-16 13:51:03 -04:00
parent 4cff390f41
commit 3d1e311fef
6 changed files with 91 additions and 97 deletions

View File

@@ -12,9 +12,9 @@ function ccat_register_breadcrumb_graphql_types() {
'type' => array( 'non_null' => 'String' ),
'description' => 'The display label for the breadcrumb',
),
'uri' => array(
'to' => array(
'type' => 'String',
'description' => 'The URI for the breadcrumb link (null for current page)',
'description' => 'The to for the breadcrumb link (null for current page)',
),
),
)
@@ -28,10 +28,20 @@ function ccat_register_breadcrumb_graphql_fields() {
'Node',
'breadcrumbs',
array(
'type' => array( 'list_of' => 'BreadcrumbItem' ),
'type' => array(
'list_of' => array( 'non_null' => 'BreadcrumbItem' ),
),
'description' => 'Breadcrumb navigation items for this content',
'resolve' => function ( $node ) {
return ccat_get_breadcrumbs( $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 )
);
},
)
);
@@ -43,15 +53,13 @@ function ccat_get_breadcrumbs( $node ) {
if ( ! $node ) {
return $breadcrumbs;
}
// Handle WPGraphQL Model objects
if ( $node instanceof \WPGraphQL\Model\Post ) {
$post = get_post( $node->databaseId );
if ( $post ) {
$breadcrumbs = ccat_get_post_breadcrumbs( $post );
$breadcrumbs[] = array(
'label' => html_entity_decode( get_the_title( $post->ID ), ENT_QUOTES, 'UTF-8' ),
'uri' => null,
'label' => get_the_title( $post->ID ),
'to' => null,
);
}
} elseif ( $node instanceof \WPGraphQL\Model\Term ) {
@@ -59,8 +67,8 @@ function ccat_get_breadcrumbs( $node ) {
if ( $term && ! is_wp_error( $term ) ) {
$breadcrumbs = ccat_get_term_breadcrumbs( $term );
$breadcrumbs[] = array(
'label' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ),
'uri' => null,
'label' => $term->name,
'to' => null,
);
}
}
@@ -68,15 +76,15 @@ function ccat_get_breadcrumbs( $node ) {
}
// Helper: Get breadcrumbs for a given post
function ccat_get_post_breadcrumbs( $post ) {
function ccat_get_post_breadcrumbs( WP_Post $post ) {
$breadcrumbs = array();
$is_front_page = get_option( 'show_on_front' ) === 'page' && get_option( 'page_on_front' ) == $post->ID;
$is_front_page = get_option( 'show_on_front' ) === 'page' && get_option( 'page_on_front' ) === $post->ID;
if ( $is_front_page ) {
return $breadcrumbs; // No breadcrumbs for the front page
} else {
$breadcrumbs[] = array(
'label' => 'Accueil',
'uri' => '/',
'to' => '/',
);
}
$post_type = get_post_type( $post->ID );
@@ -87,8 +95,8 @@ function ccat_get_post_breadcrumbs( $post ) {
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor_id ) {
$breadcrumbs[] = array(
'label' => html_entity_decode( get_the_title( $ancestor_id ), ENT_QUOTES, 'UTF-8' ),
'uri' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
'label' => get_the_title( $ancestor_id ),
'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
);
}
}
@@ -97,42 +105,42 @@ function ccat_get_post_breadcrumbs( $post ) {
case 'post':
$breadcrumbs[] = array(
'label' => 'Actualités',
'uri' => '/actualites/',
'to' => '/actualites/',
);
break;
case 'event':
$breadcrumbs[] = array(
'label' => 'Événements',
'uri' => '/evenements/',
'to' => '/evenements/',
);
break;
case 'location':
$breadcrumbs[] = array(
'label' => 'Lieux',
'uri' => '/lieux/',
'to' => '/lieux/',
);
break;
case 'membership':
$breadcrumbs[] = array(
'label' => 'Membres',
'uri' => '/membres/',
'to' => '/membres/',
);
break;
case 'project':
$breadcrumbs[] = array(
'label' => 'Projets',
'uri' => '/projets/',
'to' => '/projets/',
);
break;
case 'resource':
$breadcrumbs[] = array(
'label' => 'Ressources',
'uri' => '/ressources/',
'to' => '/ressources/',
);
break;
}
@@ -141,7 +149,7 @@ function ccat_get_post_breadcrumbs( $post ) {
}
// Helper: Get breadcrumbs for a given term
function ccat_get_term_breadcrumbs( $term ) {
function ccat_get_term_breadcrumbs( WP_Term $term ) {
$breadcrumbs = array();
$taxonomy = $term->taxonomy;
if ( $term->parent ) {
@@ -152,8 +160,8 @@ function ccat_get_term_breadcrumbs( $term ) {
$ancestor_term = get_term( $ancestor_id, $taxonomy );
if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) {
$breadcrumbs[] = array(
'label' => html_entity_decode( $ancestor_term->name, ENT_QUOTES, 'UTF-8' ),
'uri' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
'label' => $ancestor_term->name,
'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
);
}
}
@@ -165,7 +173,7 @@ function ccat_get_term_breadcrumbs( $term ) {
array(
array(
'label' => 'Catégories',
'uri' => '/categories/',
'to' => '/categories/',
),
),
$breadcrumbs
@@ -177,7 +185,7 @@ function ccat_get_term_breadcrumbs( $term ) {
array(
array(
'label' => 'Disciplines',
'uri' => '/disciplines/',
'to' => '/disciplines/',
),
),
$breadcrumbs
@@ -189,7 +197,7 @@ function ccat_get_term_breadcrumbs( $term ) {
array(
array(
'label' => 'Catégories de projets',
'uri' => '/categories-projets/',
'to' => '/categories-projets/',
),
),
$breadcrumbs
@@ -201,7 +209,7 @@ function ccat_get_term_breadcrumbs( $term ) {
array(
array(
'label' => 'Catégories de ressources',
'uri' => '/categories-ressources/',
'to' => '/categories-ressources/',
),
),
$breadcrumbs