Skip to content

Commit

Permalink
See #1719: Added Co-Author JSON-LD support. (#1720)
Browse files Browse the repository at this point in the history
* See #1719: Added Co-Author JSON-LD support.

* See #1719: Code improvements.
  • Loading branch information
mauanga authored Jan 3, 2024
1 parent 2591eff commit d29aab8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
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
),
$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

0 comments on commit d29aab8

Please sign in to comment.