Prevent null object warnings in schemas

No idea how, but apparently the post can be null under yet to be determined circumstances.
This commit is contained in:
Tetrakern 2024-04-25 02:11:42 +02:00
parent 0e5de4f90a
commit fe1ca73b1c
8 changed files with 48 additions and 8 deletions

View File

@ -62,6 +62,7 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
}
// Setup
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$image_data = fictioneer_get_schema_primary_image( $post_id );
@ -69,6 +70,10 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
$page_description = fictioneer_get_seo_description( $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
if ( ! $post ) {
return '';
}
// Website node
$schema['@graph'][] = fictioneer_get_schema_node_website();
@ -84,7 +89,7 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
// Article node
$article_node = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data, true
'Article', $page_description, $post, $image_data, true
);
if ( $word_count > 0 ) {

View File

@ -85,9 +85,14 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
// Setup
$list = get_posts( $query_args );
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
if ( ! $post ) {
return '';
}
// Filter out invalid chapters (faster than meta query)
$list = array_filter( $list, function ( $post ) {
// Chapter hidden?
@ -131,7 +136,7 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
// Article node
$schema['@graph'][] = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data
'Article', $page_description, $post, $image_data
);
// List node

View File

@ -85,9 +85,14 @@ if ( ! function_exists( 'fictioneer_build_collections_schema' ) ) {
// Setup
$list = get_posts( $query_args );
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
if ( ! $post ) {
return '';
}
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
_x( 'All collections on %s.', 'SEO default description for Collections template.', 'fictioneer' ),
@ -116,7 +121,7 @@ if ( ! function_exists( 'fictioneer_build_collections_schema' ) ) {
// Article node
$schema['@graph'][] = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data
'Article', $page_description, $post, $image_data
);
// List node

View File

@ -54,11 +54,16 @@ if ( ! function_exists( 'fictioneer_build_post_schema' ) ) {
}
// Setup
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
$page_description = fictioneer_get_seo_description( $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
if ( ! $post ) {
return '';
}
// Website node
$schema['@graph'][] = fictioneer_get_schema_node_website();
@ -74,7 +79,7 @@ if ( ! function_exists( 'fictioneer_build_post_schema' ) ) {
// Article node
$schema['@graph'][] = fictioneer_get_schema_node_article(
['Article', 'BlogPosting'], $page_description, get_post( $post_id ), $image_data, true
['Article', 'BlogPosting'], $page_description, $post, $image_data, true
);
// Prepare and cache for next time

View File

@ -54,11 +54,16 @@ if ( ! function_exists( 'fictioneer_build_recommendation_schema' ) ) {
}
// Setup
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
$default_description = get_post_meta( $post_id, 'fictioneer_recommendation_one_sentence', true );
if ( ! $post ) {
return '';
}
$page_description = fictioneer_get_seo_description(
$post_id,
array( 'default' => $default_description, 'skip_cache' => true )
@ -79,7 +84,7 @@ if ( ! function_exists( 'fictioneer_build_recommendation_schema' ) ) {
// Article node
$article_node = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data, true
'Article', $page_description, $post, $image_data, true
);
// Override author node

View File

@ -84,10 +84,15 @@ if ( ! function_exists( 'fictioneer_build_recommendations_schema' ) ) {
);
// Setup
$post = get_post( $post_id );
$list = get_posts( $query_args );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
if ( ! $post ) {
return '';
}
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
_x( 'All recommendations on %s.', 'SEO default description for Recommendations template.', 'fictioneer' ),
@ -116,7 +121,7 @@ if ( ! function_exists( 'fictioneer_build_recommendations_schema' ) ) {
// Article node
$schema['@graph'][] = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data
'Article', $page_description, $post, $image_data
);
// List node

View File

@ -85,9 +85,14 @@ if ( ! function_exists( 'fictioneer_build_stories_schema' ) ) {
// Setup
$list = get_posts( $query_args );
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
if ( ! $post ) {
return '';
}
// Filter out invalid chapters (faster than meta query)
$list = array_filter( $list, function ( $post ) {
// Chapter hidden?
@ -125,7 +130,7 @@ if ( ! function_exists( 'fictioneer_build_stories_schema' ) ) {
// Article node
$schema['@graph'][] = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data
'Article', $page_description, $post, $image_data
);
// List node

View File

@ -64,6 +64,7 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
}
// Setup
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
$rating = get_post_meta( $post_id, 'fictioneer_story_rating', true );
@ -73,6 +74,10 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
$chapter_count = 0;
$chapter_list = [];
if ( ! $post ) {
return '';
}
$page_description = fictioneer_get_seo_description(
$post_id,
array( 'default' => $default_description, 'skip_cache' => true )
@ -111,7 +116,7 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
// Article node
$article_block = fictioneer_get_schema_node_article(
'Article', $page_description, get_post( $post_id ), $image_data, true
'Article', $page_description, $post, $image_data, true
);
if ( ! empty( $rating ) ) {