Skip to content

Conversation

@mattwatsoncodes
Copy link

When a post type is excluded from the Yoast SEO index using the wpseo_indexable_excluded_post_types filter, the post may not have a proper indexable object or ID when passed into Yoast’s filter_graph hook (using the $context argument). Co-Authors Plus currently assumes that a valid post object within the $context and therefore the ID will be available within this filter, which can trigger warnings.

The problematic function is here:

/**
* Filters the graph output to add authors.
*
* @param array $data The schema graph.
* @param Meta_Tags_Context $context Context object.
*
* @return array The (potentially altered) schema graph.
*/
public static function filter_graph( $data, $context ): array {
if ( ! is_singular() ) {
return $data;
}
if ( ! function_exists( 'get_coauthors' ) ) {
return $data;
}
/**
* Contains the authors from the Co-Authors Plus plugin.
*
* @var WP_User[] $author_objects
*/
$author_objects = get_coauthors( $context->post->ID );
$ids = [];
$authors = [];
// Add the authors to the schema.
foreach ( $author_objects as $author ) {
$author_generator = new CoAuthor();
$author_generator->context = $context;
$author_generator->helpers = YoastSEO()->helpers;
if ( $author instanceof WP_User ) {
$author_data = $author_generator->generate_from_user_id( $author->ID );
} elseif ( ! empty( $author->type ) && $author->type === 'guest-author' ) {
$author_data = $author_generator->generate_from_guest_author( $author );
}
if ( ! empty( $author_data ) ) {
$ids[] = [ '@id' => $author_data['@id'] ];
$authors[] = $author_data;
}
}
$schema_types = new Schema_Types();
$article_types = array_column( $schema_types->get_article_type_options(), 'value' );
// Change the author reference to reference our multiple authors.
$add_to_graph = false;
foreach ( $data as $key => $piece ) {
if ( in_array( $piece['@type'], $article_types, true ) ) {
$data[ $key ]['author'] = $ids;
$add_to_graph = true;
break;
}
}
if ( $add_to_graph ) {
// Clean all Persons from the schema, as the user stored as post owner might be incorrectly added if the post has only guest authors as authors.
$data = array_filter(
$data,
function( $piece ) {
return empty( $piece['@type'] ) || $piece['@type'] !== 'Person';
}
);
if ( ! empty( $author_data ) ) {
if ( $context->site_represents !== 'person' || $author->ID !== $context->site_user_id ) {
$data = array_merge( $data, $authors );
}
}
}
return $data;
}

This issue was observed with a custom post type that is excluded via:

add_filter( 'wpseo_indexable_excluded_post_types', function( $post_types ) {
    $post_types[] = 'example-post-type';
    return $post_types;
});

The filter_graph filter still runs for this post type, even though it is not Yoast-indexable. However, Co-Authors tries to access get_coauthors( $context->post->ID ), which can result in an error if $context->post is missing due to the post not being indexed.

Proposed Solution

I propose adding a check to ensure the post and the post->ID exist within the $context that is passed to the filter, before trying to attach co-authors to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant