PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / integrations / third-party / coauthors-plus.php

coauthors-plus.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at src/integrations/third-party/coauthors-plus.php

152 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Third_Party;
4
5 use Yoast\WP\SEO\Config\Schema_Types;
6 use Yoast\WP\SEO\Conditionals\Third_Party\CoAuthors_Plus_Activated_Conditional;
7 use Yoast\WP\SEO\Conditionals\Third_Party\CoAuthors_Plus_Flag_Conditional;
8 use Yoast\WP\SEO\Context\Meta_Tags_Context;
9 use Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece;
10 use Yoast\WP\SEO\Generators\Schema\Third_Party\CoAuthor;
11 use Yoast\WP\SEO\Integrations\Integration_Interface;
12 use Yoast\WP\SEO\Surfaces\Helpers_Surface;
13
14 /**
15 * Integrates the multiple authors capability from CoAuthors Plus into Yoast SEO schema.
16 */
17 class CoAuthors_Plus implements Integration_Interface {
18
19 /**
20 * The helpers surface.
21 *
22 * @var Helpers_Surface
23 */
24 protected $helpers;
25
26 /**
27 * Initializes the integration.
28 *
29 * @return void
30 */
31 public function register_hooks() {
32 \add_filter( 'wpseo_schema_graph', [ $this, 'filter_graph' ], 11, 2 );
33 \add_filter( 'wpseo_schema_author', [ $this, 'filter_author_graph' ], 11, 4 );
34 }
35
36 /**
37 * Returns the conditionals based in which this loadable should be active.
38 *
39 * @return array
40 */
41 public static function get_conditionals() {
42 return [
43 CoAuthors_Plus_Activated_Conditional::class,
44 CoAuthors_Plus_Flag_Conditional::class,
45 ];
46 }
47
48 /**
49 * CoAuthors_Plus constructor.
50 *
51 * @codeCoverageIgnore It only sets dependencies.
52 *
53 * @param Helpers_Surface $helpers The helper surface.
54 */
55 public function __construct( Helpers_Surface $helpers ) {
56 $this->helpers = $helpers;
57 }
58
59 /**
60 * Filters the graph output to add authors.
61 *
62 * @param array $data The schema graph.
63 * @param Meta_Tags_Context $context The context object.
64 * @param Abstract_Schema_Piece $graph_piece_generator The graph piece generator.
65 * @param Abstract_Schema_Piece[] $graph_piece_generators The graph piece generators.
66 *
67 * @return array The (potentially altered) schema graph.
68 */
69 public function filter_author_graph( $data, $context, $graph_piece_generator, $graph_piece_generators ) {
70 if ( ! isset( $data['image']['url'] ) ) {
71 return $data;
72 }
73
74 if ( isset( $data['image']['@id'] ) ) {
75 $data['image']['@id'] .= \md5( $data['image']['url'] );
76 }
77
78 if ( isset( $data['logo']['@id'] ) ) {
79 $data['logo']['@id'] .= \md5( $data['image']['url'] );
80 }
81
82 return $data;
83 }
84
85 /**
86 * Filters the graph output to add authors.
87 *
88 * @param array $data The schema graph.
89 * @param Meta_Tags_Context $context Context object.
90 *
91 * @return array The (potentially altered) schema graph.
92 */
93 public function filter_graph( $data, $context ) {
94 if ( ! \is_singular() ) {
95 return $data;
96 }
97
98 if ( ! \function_exists( '\get_coauthors' ) ) {
99 return $data;
100 }
101
102 /**
103 * Contains the authors from the CoAuthors Plus plugin.
104 *
105 * @var \WP_User[] $author_objects
106 */
107 $author_objects = \get_coauthors( $context->post->ID );
108 if ( \count( $author_objects ) <= 1 ) {
109 return $data;
110 }
111
112 $ids = [];
113
114 // Add the authors to the schema.
115 foreach ( $author_objects as $author ) {
116 if ( $author->ID === (int) $context->post->post_author ) {
117 continue;
118 }
119 $author_generator = new CoAuthor();
120 $author_generator->context = $context;
121 $author_generator->helpers = $this->helpers;
122 $author_data = $author_generator->generate_from_user_id( $author->ID );
123 if ( ! empty( $author_data ) ) {
124 $ids[] = [ '@id' => $author_data['@id'] ];
125 }
126 }
127
128 $schema_types = new Schema_Types();
129 $article_types = $schema_types->get_article_type_options_values();
130
131 // Change the author reference to reference our multiple authors.
132 $add_to_graph = false;
133 foreach ( $data as $key => $piece ) {
134 if ( \in_array( $piece['@type'], $article_types, true ) ) {
135 $data[ $key ]['author'] = \array_merge( [ $piece['author'] ], $ids );
136 $add_to_graph = true;
137 break;
138 }
139 }
140
141 if ( $add_to_graph ) {
142 if ( ! empty( $author_data ) ) {
143 if ( $context->site_represents !== 'person' || $author->ID !== $context->site_user_id ) {
144 $data[] = $author_data;
145 }
146 }
147 }
148
149 return $data;
150 }
151 }
152