feat: Virtual page redirect, breadcrumb & menu items
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m5s

This commit is contained in:
2025-09-25 21:27:41 -04:00
parent 3f0d4dbb4e
commit e9c92840fc
12 changed files with 155 additions and 134 deletions

View File

@@ -0,0 +1,18 @@
<?php
// Helper: Determine if page is a virtual page
function ccat_is_virtual_page( $page_id ) {
return get_post_meta( $page_id, '_wp_page_template', true ) === 'virtual.php';
}
// Replace menu item path with '#' for virtual pages
add_filter( 'graphql_resolve_field', 'ccat_virtual_page_menu_item_graphql_value', 10, 5 );
function ccat_virtual_page_menu_item_graphql_value( $result, $source, $args, $context, $info ) {
if ( $source instanceof \WPGraphQL\Model\MenuItem && $info->fieldName === 'path' ) {
$menu_item = wp_setup_nav_menu_item( get_post( $source->databaseId ) );
if ( $menu_item->object === 'page' && ccat_is_virtual_page( $menu_item->object_id ) ) {
return '#';
}
}
return $result;
}

View File

@@ -49,102 +49,77 @@ function ccat_register_breadcrumb_graphql_fields() {
// 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();
if ( ! $node ) {
$is_front_page = get_option( 'show_on_front' ) === 'page' && (int) get_option( 'page_on_front' ) === $post_id;
if ( $is_front_page ) {
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 );
$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' => 'Accueil',
'to' => '/',
'label' => get_the_title( $ancestor_id ),
'to' => ccat_is_virtual_page( $ancestor_id ) ? null : str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
);
}
$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;
}
$breadcrumbs[] = array(
'label' => get_the_title( $post_id ),
'to' => null,
);
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 ) ),
);
}
}
// 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;
}