helpers = $helpers; } /** * Filters the graph output to add authors. * * @param array $data The schema graph. * @param Meta_Tags_Context $context The context object. * @param Abstract_Schema_Piece $graph_piece_generator The graph piece generator. * @param Abstract_Schema_Piece[] $graph_piece_generators The graph piece generators. * * @return array The (potentially altered) schema graph. */ public function filter_author_graph( $data, $context, $graph_piece_generator, $graph_piece_generators ) { if ( ! isset( $data['image']['url'] ) ) { return $data; } if ( isset( $data['image']['@id'] ) ) { $data['image']['@id'] .= \md5( $data['image']['url'] ); } if ( isset( $data['logo']['@id'] ) ) { $data['logo']['@id'] .= \md5( $data['image']['url'] ); } return $data; } /** * Filters the graph output to add authors. * * @param array $data The schema graph. * @param Meta_Tags_Context $context Context object. * * @return array The (potentially altered) schema graph. */ public function filter_graph( $data, $context ) { if ( ! \is_singular() ) { return $data; } if ( ! \function_exists( '\get_coauthors' ) ) { return $data; } /** * Contains the authors from the CoAuthors Plus plugin. * * @var \WP_User[] $author_objects */ $author_objects = \get_coauthors( $context->post->ID ); if ( \count( $author_objects ) <= 1 ) { return $data; } $ids = []; // Add the authors to the schema. foreach ( $author_objects as $author ) { if ( $author->ID === (int) $context->post->post_author ) { continue; } $author_generator = new CoAuthor(); $author_generator->context = $context; $author_generator->helpers = $this->helpers; $author_data = $author_generator->generate_from_user_id( $author->ID ); if ( ! empty( $author_data ) ) { $ids[] = [ '@id' => $author_data['@id'] ]; } } $schema_types = new Schema_Types(); $article_types = $schema_types->get_article_type_options_values(); // Change the author reference to reference our multiple authors. $add_to_graph = false; foreach ( $data as $key => $piece ) { if ( \in_array( $piece['@type'], $article_types, true ) ) { $data[ $key ]['author'] = \array_merge( [ $piece['author'] ], $ids ); $add_to_graph = true; break; } } if ( $add_to_graph ) { if ( ! empty( $author_data ) ) { if ( $context->site_represents !== 'person' || $author->ID !== $context->site_user_id ) { $data[] = $author_data; } } } return $data; } }