Skip to content

Commit

Permalink
See #1728: Add check for false @id in Author JSON-LD. (#1729)
Browse files Browse the repository at this point in the history
* See #1728: Add check for false @id in Author JSON-LD.

* See #1728: Refactored get_author method to return empty array if unable to find author entity or user.

* See #1728: Formatting.
  • Loading branch information
mauanga authored Feb 13, 2024
1 parent 2def24f commit 067dd40
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/includes/class-wordlift-post-to-jsonld-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,18 @@ public function convert( $post_id, &$references = array(), &$references_infos =
$post_id
);

// Set the values returned by the filter.
$jsonld['author'] = $ret_val['author'];
$references = $ret_val['references'];
// Set the values returned by the author filter.
/*
* Do not add the author JSON-LD if an invalid author was referenced in a post.
*
* @see https://github.com/insideout10/wordlift-plugin/issues/1728
*
* @since 3.53.2
*/
if ( ! empty( $ret_val['author'] ) ) {
$jsonld['author'] = $ret_val['author'];
$references = $ret_val['references'];
}

// Return the JSON-LD if filters are disabled by the client.
if ( $this->disable_convert_filters ) {
Expand Down Expand Up @@ -264,9 +273,19 @@ public function get_author( $author_id, &$references ) {
// Get the entity bound to this user.
$entity_id = $this->user_service->get_entity( $author_id );

// If there's no entity bound return a simple author structure.
if ( empty( $entity_id ) || 'publish' !== get_post_status( $entity_id ) ) {
if ( ! empty( $entity_id ) && 'publish' === get_post_status( $entity_id ) ) {
// Add the author to the references.
$author_uri = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
$references[] = $entity_id;

// Return the JSON-LD for the referenced entity.
return array(
'@id' => $author_uri,
);
}

// If there's no entity bound return a simple author structure.
if ( false !== get_userdata( $author_id ) ) {
$author = get_the_author_meta( 'display_name', $author_id );
$author_first_name = get_the_author_meta( 'first_name', $author_id );
$author_last_name = get_the_author_meta( 'last_name', $author_id );
Expand All @@ -282,14 +301,8 @@ public function get_author( $author_id, &$references ) {
);
}

// Add the author to the references.
$author_uri = Wordlift_Entity_Service::get_instance()->get_uri( $entity_id );
$references[] = $entity_id;

// Return the JSON-LD for the referenced entity.
return array(
'@id' => $author_uri,
);
// No valid entity or author so return empty array
return array();
}

/**
Expand Down

0 comments on commit 067dd40

Please sign in to comment.