diff --git a/src/classes/jsonld/class-jsonld-article-wrapper.php b/src/classes/jsonld/class-jsonld-article-wrapper.php index 06f050b7d..e641c9e0d 100644 --- a/src/classes/jsonld/class-jsonld-article-wrapper.php +++ b/src/classes/jsonld/class-jsonld-article-wrapper.php @@ -109,12 +109,17 @@ public function after_get_jsonld( $jsonld, $post_id, $context ) { $author_jsonld = $this->get_author_linked_entity( $article_jsonld ); + + // Get primary author in case co-authors exist. + $primary_author = $this->get_primary_author($article_jsonld['author']); + /** * The author entities can be present in graph for some entity types * for Person and Organization, so check before we add it to graph. * reference : https://schema.org/author */ - if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $article_jsonld['author']['@id'] ) ) { + + if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $primary_author['@id'] ) ) { $jsonld[] = $author_jsonld; } @@ -128,12 +133,33 @@ private function is_article( $schema_types ) { return ! empty( $array_intersect ); } + /** + * Get primary author from author array. + * + * A helper function that checks the structure of the author array and returns the primary author. + * + * @param array|string $author The author array. + * + * @return array|string The primary author. + * + * @since 3.51.4 + */ + private function get_primary_author( $author ) { + + // Nested array of co-authors. Return the primary author. + if ( is_array( $author ) && ! empty( $author ) && ! isset( $author['@id'] ) ) { + return $author[0]; + } + + return $author; + } + private function get_author_linked_entity( $article_jsonld ) { if ( ! array_key_exists( 'author', $article_jsonld ) ) { return false; } - $author = $article_jsonld['author']; + $author = $this->get_primary_author( $article_jsonld['author'] ); if ( count( array_keys( $author ) ) !== 1 || ! array_key_exists( '@id', $author ) ) { return false; diff --git a/src/includes/class-wordlift-post-to-jsonld-converter.php b/src/includes/class-wordlift-post-to-jsonld-converter.php index bf0614495..baecad671 100644 --- a/src/includes/class-wordlift-post-to-jsonld-converter.php +++ b/src/includes/class-wordlift-post-to-jsonld-converter.php @@ -167,8 +167,36 @@ public function convert( $post_id, &$references = array(), &$references_infos = // Set the publisher. $this->set_publisher( $jsonld ); - // Finally set the author. - $jsonld['author'] = $this->get_author( $post->post_author, $references ); + /** + * Call the `wl_post_jsonld_author` filter. + * + * This filter checks if there are co-authors or a single author and + * returns a JSON-LD fragment for the author(s). + * + * @param array $value { + * + * @type array $jsonld The JSON-LD structure. + * @type int[] $references An array of post IDs. + * } + * + * @param int $post_id The {@link WP_Post} `id`. + * + * @since 3.51.4 + * + * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ + */ + $ret_val = apply_filters( + 'wl_jsonld_author', + array( + 'author' => $this->get_author( $post->post_author, $references ), + 'references' => $references + ), + $post_id + ); + + // Set the values returned by the filter. + $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 ) { diff --git a/src/modules/jsonld-author-filter/load.php b/src/modules/jsonld-author-filter/load.php new file mode 100644 index 000000000..18e8d23e1 --- /dev/null +++ b/src/modules/jsonld-author-filter/load.php @@ -0,0 +1,53 @@ + 1 ) { + + // Clear the existing author. + $author = array(); + + // Build array of authors. + foreach ( $coauthors as $coauthor ) { + $author[] = Wordlift_Post_To_Jsonld_Converter::get_instance()->get_author( $coauthor->ID, $references ); + } + } + } + + return array( + 'author' => $author, + 'references' => $references, + ); +} + +// Add the filter +add_filter('wl_jsonld_author', '_wl_jsonld_author__author_filter', 10, 2); \ No newline at end of file diff --git a/src/wordlift.php b/src/wordlift.php index 691bce148..a0f6dd247 100644 --- a/src/wordlift.php +++ b/src/wordlift.php @@ -320,6 +320,7 @@ function ( $value ) { require_once __DIR__ . '/modules/events/load.php'; require_once __DIR__ . '/modules/plugin-diagnostics/load.php'; require_once __DIR__ . '/modules/override-url/load.php'; +require_once __DIR__ . '/modules/jsonld-author-filter/load.php'; function _wl_update_plugins_raptive_domain( $update, $plugin_data, $plugin_file ) { // Bail out if it's not our plugin.