Skip to content
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

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/classes/jsonld/class-jsonld-article-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Copy link
Member

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:

$primary_author = $this->get_primary_author($article_jsonld['author']);

if ( $author_jsonld && ! $this->is_author_entity_present_in_graph( $jsonld, $primary_author['@id'] ) ) {

$jsonld[] = $author_jsonld;
}

Expand All @@ -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'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add a couple more checks here, like if ( is_array( $author) && !empty( $author ) && ! isset( $author['@id'] ) ) { ... otherwise you risk trying to access index 0 of something which is not an array or has no index 0.

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;
Expand Down
28 changes: 26 additions & 2 deletions src/includes/class-wordlift-post-to-jsonld-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think $references would work as an output in this case.

Check src/includes/class-wordlift-entity-post-to-jsonld-converter.php:L123 to see how we do it in a similar case:

		/**
		 * Call the `wl_post_jsonld_array` filter. This filter allows 3rd parties to also modify the references.
		 *
		 * @param array $value {
		 *
		 * @type array $jsonld The JSON-LD structure.
		 * @type int[] $references An array of post IDs.
		 * }
		 * @since 3.25.0
		 *
		 * @see https://www.geeklab.info/2010/04/wordpress-pass-variables-by-reference-with-apply_filter/
		 *
		 * @api
		 */
		$ret_val    = apply_filters(
			'wl_entity_jsonld_array',
			array(
				'jsonld'     => $jsonld,
				'references' => $references,
			),
			$post_id
		);
		$jsonld     = $ret_val['jsonld'];
		$references = $ret_val['references'];

),
$post_id
);

// Return the JSON-LD if filters are disabled by the client.
if ( $this->disable_convert_filters ) {
Expand Down
50 changes: 50 additions & 0 deletions src/modules/jsonld-author-filter/load.php
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 ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be on the safe side also add && function_exists( 'get_coauthors' ), we don't want to find ourselves breaking a website because this function changed or is renamed.


$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;
Copy link
Member

Choose a reason for hiding this comment

The 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);
1 change: 1 addition & 0 deletions src/wordlift.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down