From 067dd40f6ccd816063e1c064235452d1065f12fc Mon Sep 17 00:00:00 2001 From: mauanga Date: Tue, 13 Feb 2024 11:14:13 +0100 Subject: [PATCH] See #1728: Add check for false @id in Author JSON-LD. (#1729) * 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. --- ...lass-wordlift-post-to-jsonld-converter.php | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/includes/class-wordlift-post-to-jsonld-converter.php b/src/includes/class-wordlift-post-to-jsonld-converter.php index 701e5a189..8ec201150 100644 --- a/src/includes/class-wordlift-post-to-jsonld-converter.php +++ b/src/includes/class-wordlift-post-to-jsonld-converter.php @@ -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 ) { @@ -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 ); @@ -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(); } /**