Skip to content

Commit 7d9ccec

Browse files
authored
fix: improve ContentTypeSeo handling and breadcrumb generation (#90)
1 parent 39d0725 commit 7d9ccec

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- feat: Add plugin dependencies header.
6+
- fix: Improve handling of `ContentTypeSeo` and prevent fatal error when generating breadcrumbs. H/t @MonPetitUd
67
- fix: Plugin versions in dependency check logic is now in sync with the version requirements.
78
- fix: Update the return type of the `type` field in the `Redirection` model to correctly return a `?string`.
89
- chore!: Add `WPGraphQL/RankMath` namespace to root-level files ( `activation.php`, `deactivation.php`, `wp-graphql-rank-math.php` ).

src/Model/ContentNodeSeo.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use GraphQL\Error\Error;
1313
use GraphQL\Error\UserError;
14+
use RankMath\Helper as RMHelper;
1415
use WPGraphQL;
1516

1617
/**
@@ -183,6 +184,25 @@ protected function init() {
183184
}
184185
}
185186

187+
/**
188+
* {@inheritDoc}
189+
*/
190+
protected function get_breadcrumbs(): ?array {
191+
$breadcrumbs = parent::get_breadcrumbs();
192+
193+
if ( empty( $breadcrumbs ) ) {
194+
return null;
195+
}
196+
197+
$remove_title = ( is_single( $this->database_id ) || is_page( $this->database_id ) ) && RMHelper::get_settings( 'general.breadcrumbs_remove_post_title' );
198+
199+
if ( $remove_title ) {
200+
array_pop( $breadcrumbs );
201+
}
202+
203+
return ! empty( $breadcrumbs ) ? $breadcrumbs : null;
204+
}
205+
186206
/**
187207
* {@inheritDoc}
188208
*/

src/Model/ContentTypeSeo.php

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use GraphQL\Error\Error;
1313
use GraphQL\Error\UserError;
14+
use RankMath\Helper as RMHelper;
1415
use WPGraphQL;
1516

1617
/**
@@ -53,11 +54,49 @@ public function __construct( string $post_type ) {
5354

5455
$allowed_fields = [ 'breadcrumbTitle' ];
5556

56-
global $wp_query;
57+
parent::__construct( $object, $capability, $allowed_fields );
58+
}
5759

58-
$wp_query->parse_query( [ 'post_type' => $post_type ] );
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
public function setup(): void {
64+
global $wp_query, $post;
5965

60-
parent::__construct( $object, $capability, $allowed_fields );
66+
// Store the global post before overriding.
67+
$this->global_post = $post;
68+
69+
if ( $this->data instanceof \WP_Post_Type ) {
70+
/**
71+
* Reset global post
72+
*/
73+
$GLOBALS['post'] = get_post( 0 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride
74+
}
75+
76+
// Store the global post before overriding.
77+
$this->global_post = $post;
78+
79+
/**
80+
* Parse the query to tell WordPress how to setup the global state.
81+
*/
82+
$wp_query->parse_query( [ 'post_type' => $this->data->name ] );
83+
84+
$wp_query->queried_object_id = $this->data->name;
85+
$wp_query->queried_object = $this->data;
86+
87+
parent::setup();
88+
}
89+
90+
/**
91+
* Reset global state after the model fields
92+
* have been generated
93+
*
94+
* @return void
95+
*/
96+
public function tear_down() {
97+
$GLOBALS['post'] = $this->global_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
98+
99+
wp_reset_postdata();
61100
}
62101

63102
/**
@@ -77,6 +116,45 @@ protected function init() {
77116
}
78117
}
79118

119+
/**
120+
* {@inheritDoc}
121+
*/
122+
protected function get_breadcrumbs(): ?array {
123+
$breadcrumbs = parent::get_breadcrumbs();
124+
125+
// For non posts, we return the breadcrumbs as-is.
126+
if ( empty( $breadcrumbs ) || 'post' !== $this->data->name ) {
127+
return $breadcrumbs;
128+
}
129+
130+
/**
131+
* @todo This is a workaround since WPGraphQL doesnt support an archive type.
132+
*/
133+
134+
$blog_id = get_option( 'page_for_posts' );
135+
136+
if ( ! $blog_id ) {
137+
return $breadcrumbs;
138+
}
139+
140+
$should_show_blog = RMHelper::get_settings( 'general.breadcrumbs_blog_page' );
141+
142+
if ( ! $should_show_blog || 'page' !== get_option( 'show_on_front' ) ) {
143+
return $breadcrumbs;
144+
}
145+
146+
$breadcrumb_title = RMHelper::get_post_meta( 'breadcrumb_title', $blog_id ) ?: get_the_title( $blog_id );
147+
$permalink = get_permalink( $blog_id );
148+
149+
$breadcrumbs[] = [
150+
'text' => $breadcrumb_title ?: null,
151+
'url' => $permalink ?: null,
152+
'isHidden' => false,
153+
];
154+
155+
return $breadcrumbs;
156+
}
157+
80158
/**
81159
* {@inheritDoc}
82160
*/

src/Model/Seo.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ public function __construct( $wp_object, $capability = '', $allowed_fields = []
9292
);
9393

9494
parent::__construct( $capability, $allowed_fields );
95-
96-
// Seat up RM Globals.
97-
$url = $this->get_object_url();
98-
99-
$this->setup_post_head( $url );
10095
}
10196

10297
/**
@@ -107,6 +102,11 @@ public function setup(): void {
107102
/** @var \RankMath\Paper\Paper $paper */
108103
$paper = Paper::get();
109104
$this->helper = $paper;
105+
106+
// Seat up RM Globals.
107+
$url = $this->get_object_url();
108+
109+
$this->setup_post_head( $url );
110110
}
111111

112112
/**
@@ -169,9 +169,18 @@ protected function init() {
169169
*/
170170
protected function get_breadcrumbs(): ?array {
171171
// Get the crumbs and shape them.
172-
$crumbs = RMBreadcrumbs::get()->get_crumbs();
172+
$crumbs = RMBreadcrumbs::get()->get_crumbs();
173+
174+
if ( empty( $crumbs ) ) {
175+
return null;
176+
}
177+
173178
$breadcrumbs = array_map(
174179
static function ( $crumb ) {
180+
if ( empty( $crumb[1] ) && empty( $crumb[0] ) ) {
181+
return null;
182+
}
183+
175184
return [
176185
'text' => $crumb[0] ?? null,
177186
'url' => $crumb[1] ?? null,
@@ -181,11 +190,7 @@ static function ( $crumb ) {
181190
$crumbs
182191
);
183192

184-
// Pop the current item's title.
185-
$remove_title = ( is_single( $this->database_id ) || is_page( $this->database_id ) ) && RMHelper::get_settings( 'general.breadcrumbs_remove_post_title' );
186-
if ( $remove_title ) {
187-
array_pop( $breadcrumbs );
188-
}
193+
$breadcrumbs = array_filter( $breadcrumbs );
189194

190195
return ! empty( $breadcrumbs ) ? $breadcrumbs : null;
191196
}

src/Type/WPInterface/NodeWithSeo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public static function get_fields(): array {
7979
return null;
8080
}
8181

82+
if ( empty( $source->uri ) ) {
83+
return null;
84+
}
85+
8286
$model = self::get_model_for_node( $source );
8387

8488
if ( empty( $model ) ) {

0 commit comments

Comments
 (0)