Clean up schemas a bit more

This commit is contained in:
Tetrakern 2023-09-04 23:57:19 +02:00
parent 58c865c41e
commit 3f736dbade
7 changed files with 56 additions and 64 deletions

View File

@ -30,9 +30,13 @@ function fictioneer_refresh_chapter_schema( $post_id, $post ) {
// Setup
$story_id = fictioneer_get_field( 'fictioneer_chapter_story', $post_id );
// Rebuild schema(s)
// Rebuild schema
fictioneer_build_chapter_schema( $post_id );
if ( ! empty( $story_id ) ) fictioneer_build_story_schema( $story_id );
// Also rebuild story schema (if any)
if ( ! empty( $story_id ) ) {
fictioneer_build_story_schema( $story_id );
}
}
add_action( 'save_post', 'fictioneer_refresh_chapter_schema', 20, 2 );
@ -83,7 +87,9 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
'Article', $page_description, get_post( $post_id ), $image_data, true
);
$article_node['wordCount'] = $word_count;
if ( $word_count > 0 ) {
$article_node['wordCount'] = $word_count;
}
if ( ! empty( $story_id ) ) {
$article_node['contentRating'] = fictioneer_get_field( 'fictioneer_story_rating', $story_id );

View File

@ -40,11 +40,9 @@ function fictioneer_refresh_chapters_schema( $post_id, $post ) {
)
);
// Rebuild schemas
if ( $pages ) {
foreach ( $pages as $page ) {
fictioneer_build_chapters_schema( $page->ID );
}
// Rebuild schemas (empty array if nothing found)
foreach ( $pages as $page ) {
fictioneer_build_chapters_schema( $page->ID );
}
}
add_action( 'save_post', 'fictioneer_refresh_chapters_schema', 20, 2 );
@ -72,8 +70,8 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
// Prepare query arguments
$query_args = array (
'post_type' => array( 'fcn_chapter' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_chapter',
'post_status' => 'publish',
'meta_query' => array(
array( 'key' => 'fictioneer_chapter_hidden', 'compare' => '=', 'value' => 0, 'type' => 'numeric' ),
array( 'key' => 'fictioneer_chapter_no_chapter', 'compare' => '=', 'value' => 0, 'type' => 'numeric' )
@ -94,14 +92,14 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
__( 'All chapters on %s.', 'fictioneer' ),
_x( 'All chapters on %s.', 'SEO default description for Chapters template.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'skip_cache' => true
));
$page_title = fictioneer_get_seo_title( $post_id, array(
'default' => _x( 'Chapters', 'SEO fallback title for Chapters template.', 'fictioneer' ),
'default' => _x( 'Chapters', 'SEO default title for Chapters template.', 'fictioneer' ),
'skip_cache' => true
));
@ -126,9 +124,9 @@ if ( ! function_exists( 'fictioneer_build_chapters_schema' ) ) {
// List node
$schema['@graph'][] = fictioneer_get_schema_node_list(
$list,
__( 'Chapters', 'fictioneer' ),
_x( 'Chapters', 'SEO schema chapter list node name.', 'fictioneer' ),
sprintf(
__( 'List of chapters on %s.', 'fictioneer' ),
_x( 'List of chapters on %s.', 'SEO schema chapter list node description.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'#article'

View File

@ -40,11 +40,9 @@ function fictioneer_refresh_collections_schema( $post_id, $post ) {
)
);
// Rebuild schemas
if ( $pages ) {
foreach ( $pages as $page ) {
fictioneer_build_collections_schema( $page->ID );
}
// Rebuild schemas (empty array if nothing found)
foreach ( $pages as $page ) {
fictioneer_build_collections_schema( $page->ID );
}
}
add_action( 'save_post', 'fictioneer_refresh_collections_schema', 20, 2 );
@ -72,8 +70,8 @@ if ( ! function_exists( 'fictioneer_build_collections_schema' ) ) {
// Prepare query arguments
$query_args = array (
'post_type' => array( 'fcn_collection' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_collection',
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 20,
@ -90,14 +88,14 @@ if ( ! function_exists( 'fictioneer_build_collections_schema' ) ) {
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
__( 'All collections on %s.', 'fictioneer' ),
_x( 'All collections on %s.', 'SEO default description for Collections template.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'skip_cache' => true
));
$page_title = fictioneer_get_seo_title( $post_id, array(
'default' => _x( 'Collections', 'SEO fallback title for Collections template.', 'fictioneer' ),
'default' => _x( 'Collections', 'SEO default title for Collections template.', 'fictioneer' ),
'skip_cache' => true
));
@ -122,9 +120,9 @@ if ( ! function_exists( 'fictioneer_build_collections_schema' ) ) {
// List node
$schema['@graph'][] = fictioneer_get_schema_node_list(
$list,
__( 'Collections', 'fictioneer' ),
_x( 'Collections', 'SEO schema collection list node name.', 'fictioneer' ),
sprintf(
__( 'List of collections on %s.', 'fictioneer' ),
_x( 'List of collections on %s.', 'SEO schema collection list node description.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'#article'

View File

@ -57,13 +57,11 @@ if ( ! function_exists( 'fictioneer_build_recommendation_schema' ) ) {
$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 = fictioneer_get_field( 'fictioneer_recommendation_one_sentence', $post_id );
$page_description = fictioneer_get_seo_description(
$post_id,
array(
'default' => fictioneer_get_field( 'fictioneer_recommendation_one_sentence', $post_id ),
'skip_cache' => true
)
array( 'default' => $default_description, 'skip_cache' => true )
);
// Website node

View File

@ -40,11 +40,9 @@ function fictioneer_refresh_recommendations_schema( $post_id, $post ) {
)
);
// Rebuild schemas
if ( $pages ) {
foreach ( $pages as $page ) {
fictioneer_build_recommendations_schema( $page->ID );
}
// Rebuild schemas (empty array if nothing found)
foreach ( $pages as $page ) {
fictioneer_build_recommendations_schema( $page->ID );
}
}
add_action( 'save_post', 'fictioneer_refresh_recommendations_schema', 20, 2 );
@ -72,8 +70,8 @@ if ( ! function_exists( 'fictioneer_build_recommendations_schema' ) ) {
// Prepare query arguments
$query_args = array (
'post_type' => array( 'fcn_recommendation' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_recommendation',
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 20,
@ -90,14 +88,14 @@ if ( ! function_exists( 'fictioneer_build_recommendations_schema' ) ) {
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
__( 'All recommendations on %s.', 'fictioneer' ),
_x( 'All recommendations on %s.', 'SEO default description for Recommendations template.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'skip_cache' => true
));
$page_title = fictioneer_get_seo_title( $post_id, array(
'default' => _x( 'Recommendations', 'SEO fallback title for Recommendations template.', 'fictioneer' ),
'default' => _x( 'Recommendations', 'SEO default title for Recommendations template.', 'fictioneer' ),
'skip_cache' => true
));
@ -122,9 +120,9 @@ if ( ! function_exists( 'fictioneer_build_recommendations_schema' ) ) {
// List node
$schema['@graph'][] = fictioneer_get_schema_node_list(
$list,
__( 'Recommendations', 'fictioneer' ),
_x( 'Recommendations', 'SEO schema recommendation list node name.', 'fictioneer' ),
sprintf(
__( 'List of recommendations on %s.', 'fictioneer' ),
_x( 'List of recommendations on %s.', 'SEO schema recommendation list node description.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'#article'

View File

@ -40,11 +40,9 @@ function fictioneer_refresh_stories_schema( $post_id, $post ) {
)
);
// Rebuild schemas
if ( $pages ) {
foreach ( $pages as $page ) {
fictioneer_build_stories_schema( $page->ID );
}
// Rebuild schemas (empty array if nothing found)
foreach ( $pages as $page ) {
fictioneer_build_stories_schema( $page->ID );
}
}
add_action( 'save_post', 'fictioneer_refresh_stories_schema', 20, 2 );
@ -72,8 +70,8 @@ if ( ! function_exists( 'fictioneer_build_stories_schema' ) ) {
// Prepare query arguments
$query_args = array (
'post_type' => array( 'fcn_story', 'fcn_collection' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_story',
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 20,
@ -90,14 +88,14 @@ if ( ! function_exists( 'fictioneer_build_stories_schema' ) ) {
$page_description = fictioneer_get_seo_description( $post_id, array(
'default' => sprintf(
__( 'All stories on %s.', 'fictioneer' ),
_x( 'All stories on %s.', 'SEO default description for Stories template.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'skip_cache' => true
));
$page_title = fictioneer_get_seo_title( $post_id, array(
'default' => _x( 'Stories', 'SEO fallback title for Stories template.', 'fictioneer' ),
'default' => _x( 'Stories', 'SEO default title for Stories template.', 'fictioneer' ),
'skip_cache' => true
));
@ -122,9 +120,9 @@ if ( ! function_exists( 'fictioneer_build_stories_schema' ) ) {
// List node
$schema['@graph'][] = fictioneer_get_schema_node_list(
$list,
__( 'Stories', 'fictioneer' ),
_x( 'Stories', 'SEO schema story list node name.', 'fictioneer' ),
sprintf(
__( 'List of stories on %s.', 'fictioneer' ),
_x( 'List of stories on %s.', 'SEO schema story list node description.', 'fictioneer' ),
FICTIONEER_SITE_NAME
),
'#article'

View File

@ -27,14 +27,14 @@ function fictioneer_refresh_story_schema( $post_id, $post ) {
return;
}
// Rebuild schema(s)
// Rebuild schema
fictioneer_build_story_schema( $post_id );
// Get chapters of story
$chapters = fictioneer_get_field( 'fictioneer_story_chapters', $post_id );
// Rebuild chapter schemas (if any)
if ( ! empty( $chapters ) ) {
if ( is_array( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
fictioneer_build_chapter_schema( $chapter_id );
}
@ -68,22 +68,18 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
$image_data = fictioneer_get_schema_primary_image( $post_id );
$rating = fictioneer_get_field( 'fictioneer_story_rating', $post_id );
$chapters = fictioneer_get_field( 'fictioneer_story_chapters', $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
$default_description = fictioneer_first_paragraph_as_excerpt( fictioneer_get_content_field( 'fictioneer_story_short_description', $post_id ) );
$chapter_count = 0;
$chapter_list = [];
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
$page_description = fictioneer_first_paragraph_as_excerpt( fictioneer_get_content_field( 'fictioneer_story_short_description', $post_id ) );
$page_description = fictioneer_get_seo_description(
$post_id,
array(
'default' => $page_description,
'skip_cache' => true
)
array( 'default' => $default_description, 'skip_cache' => true )
);
// Collect visible chapters
if ( ! empty( $chapters ) ) {
if ( is_array( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
if (
! fictioneer_get_field( 'fictioneer_chapter_no_chapter', $chapter_id ) &&
@ -128,16 +124,16 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
if ( ! empty( $chapter_list ) ) {
$list_node = array(
'@type' => 'ItemList',
'name' => __( 'Chapters', 'fictioneer' ),
'name' => _x( 'Chapters', 'SEO schema story chapters list node name.', 'fictioneer' ),
'description' => sprintf(
__( 'Chapters of %s.', 'fictioneer' ),
_x( 'Chapters of %s.', 'SEO schema story chapters list node description.', 'fictioneer' ),
fictioneer_get_safe_title( $post_id )
),
'mainEntityOfPage' => ['@id' => '#article'],
'itemListElement' => []
);
foreach( $chapter_list as $chapter ) {
foreach ( $chapter_list as $chapter ) {
$list_node['itemListElement'][] = array(
'@type' => 'ListItem',
'position' => $chapter['position'],