-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
See #1719: Added Co-Author JSON-LD support. #1720
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,7 @@ public function after_get_jsonld( $jsonld, $post_id, $context ) { | |
* 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, $this->get_primary_author($article_jsonld['author'])['@id'] ) ) { | ||
$jsonld[] = $author_jsonld; | ||
} | ||
|
||
|
@@ -128,12 +128,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 ( ! isset( $author['@id'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add a couple more checks here, like |
||
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,8 +167,32 @@ 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/ | ||
*/ | ||
$jsonld['author'] = apply_filters( | ||
'wl_jsonld_author', | ||
array( | ||
'author' => $this->get_author( $post->post_author, $references ), | ||
'references' => $references | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think Check src/includes/class-wordlift-entity-post-to-jsonld-converter.php:L123 to see how we do it in a similar case:
|
||
), | ||
$post_id | ||
); | ||
|
||
// Return the JSON-LD if filters are disabled by the client. | ||
if ( $this->disable_convert_filters ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* Filter the author object to add the co-authors. | ||
* | ||
* 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 $author The author JSON-LD structure. | ||
* @type int[] $references An array of post IDs. | ||
* } | ||
* @param int $post_id The post ID. | ||
* | ||
* @return string|array A JSON-LD structure. | ||
* | ||
* @since 3.51.4 | ||
* | ||
* @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/ | ||
*/ | ||
function _wl_jsonld_author__author_filter( $args_arr, $post_id ) { | ||
|
||
$author = $args_arr['author']; | ||
$references = $args_arr['references']; | ||
|
||
$coauthor_plugin_path = 'co-authors-plus/co-authors-plus.php'; | ||
|
||
// If the co-authors plugin is active. | ||
if ( is_plugin_active( $coauthor_plugin_path ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be on the safe side also add |
||
|
||
$coauthors = get_coauthors( $post_id ); | ||
|
||
// And we have multiple authors on a post. | ||
if ( count( $coauthors ) > 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 $author; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should send back both author and references, see the example filter I linked before. |
||
} | ||
|
||
// Add the filter | ||
add_filter('wl_jsonld_author', '_wl_jsonld_author__author_filter', 10, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to be careful about this syntax as I think older version of PHP do not support it.
Let's change it to: