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 all commits
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
30 changes: 28 additions & 2 deletions src/classes/jsonld/class-jsonld-article-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
32 changes: 30 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,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
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
);

// 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 ) {
Expand Down
53 changes: 53 additions & 0 deletions src/modules/jsonld-author-filter/load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 array An array with the updated JSON-LD and references.
*
* @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 ) && function_exists( 'get_coauthors' ) ) {

$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 array(
'author' => $author,
'references' => $references,
);
}

// 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
Loading