PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
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 / generators / schema / article.php

article.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at src/generators/schema/article.php

209 lines 6.2 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\Generators\Schema;
4
5 use Yoast\WP\SEO\Config\Schema_IDs;
6
7 /**
8 * Returns schema Article data.
9 */
10 class Article extends Abstract_Schema_Piece {
11
12 /**
13 * Determines whether or not a piece should be added to the graph.
14 *
15 * @return bool
16 */
17 public function is_needed() {
18 if ( $this->context->indexable->object_type !== 'post' ) {
19 return false;
20 }
21
22 // If we cannot output a publisher, we shouldn't output an Article.
23 if ( $this->context->site_represents === false ) {
24 return false;
25 }
26
27 // If we cannot output an author, we shouldn't output an Article.
28 if ( ! $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type ) ) {
29 return false;
30 }
31
32 if ( $this->context->schema_article_type !== 'None' ) {
33 $this->context->main_schema_id = $this->context->canonical . Schema_IDs::ARTICLE_HASH;
34
35 return true;
36 }
37
38 return false;
39 }
40
41 /**
42 * Returns Article data.
43 *
44 * @return array Article data.
45 */
46 public function generate() {
47 $data = [
48 '@type' => $this->context->schema_article_type,
49 '@id' => $this->context->canonical . Schema_IDs::ARTICLE_HASH,
50 'isPartOf' => [ '@id' => $this->context->canonical . Schema_IDs::WEBPAGE_HASH ],
51 'author' => [ '@id' => $this->helpers->schema->id->get_user_schema_id( $this->context->post->post_author, $this->context ) ],
52 'headline' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
53 'datePublished' => $this->helpers->date->format( $this->context->post->post_date_gmt ),
54 'dateModified' => $this->helpers->date->format( $this->context->post->post_modified_gmt ),
55 'mainEntityOfPage' => [ '@id' => $this->context->canonical . Schema_IDs::WEBPAGE_HASH ],
56 'wordCount' => $this->word_count( $this->context->post->post_content, $this->context->post->post_title ),
57 ];
58
59 if ( $this->context->post->comment_status === 'open' ) {
60 $data['commentCount'] = \intval( $this->context->post->comment_count, 10 );
61 }
62
63 if ( $this->context->site_represents_reference ) {
64 $data['publisher'] = $this->context->site_represents_reference;
65 }
66
67 $data = $this->add_image( $data );
68 $data = $this->add_keywords( $data );
69 $data = $this->add_sections( $data );
70 $data = $this->helpers->schema->language->add_piece_language( $data );
71
72 if ( \post_type_supports( $this->context->post->post_type, 'comments' ) && $this->context->post->comment_status === 'open' ) {
73 $data = $this->add_potential_action( $data );
74 }
75
76 return $data;
77 }
78
79 /**
80 * Adds tags as keywords, if tags are assigned.
81 *
82 * @param array $data Article data.
83 *
84 * @return array Article data.
85 */
86 private function add_keywords( $data ) {
87 /**
88 * Filter: 'wpseo_schema_article_keywords_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
89 *
90 * @api string $taxonomy The chosen taxonomy.
91 */
92 $taxonomy = \apply_filters( 'wpseo_schema_article_keywords_taxonomy', 'post_tag' );
93
94 return $this->add_terms( $data, 'keywords', $taxonomy );
95 }
96
97 /**
98 * Adds categories as sections, if categories are assigned.
99 *
100 * @param array $data Article data.
101 *
102 * @return array Article data.
103 */
104 private function add_sections( $data ) {
105 /**
106 * Filter: 'wpseo_schema_article_sections_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
107 *
108 * @api string $taxonomy The chosen taxonomy.
109 */
110 $taxonomy = \apply_filters( 'wpseo_schema_article_sections_taxonomy', 'category' );
111
112 return $this->add_terms( $data, 'articleSection', $taxonomy );
113 }
114
115 /**
116 * Adds a term or multiple terms, comma separated, to a field.
117 *
118 * @param array $data Article data.
119 * @param string $key The key in data to save the terms in.
120 * @param string $taxonomy The taxonomy to retrieve the terms from.
121 *
122 * @return mixed Article data.
123 */
124 protected function add_terms( $data, $key, $taxonomy ) {
125 $terms = \get_the_terms( $this->context->id, $taxonomy );
126
127 if ( ! \is_array( $terms ) ) {
128 return $data;
129 }
130
131 $callback = static function( $term ) {
132 // We are using the WordPress internal translation.
133 return $term->name !== \__( 'Uncategorized', 'default' );
134 };
135 $terms = \array_filter( $terms, $callback );
136
137 if ( empty( $terms ) ) {
138 return $data;
139 }
140
141 $data[ $key ] = \wp_list_pluck( $terms, 'name' );
142
143 return $data;
144 }
145
146 /**
147 * Adds an image node if the post has a featured image.
148 *
149 * @param array $data The Article data.
150 *
151 * @return array The Article data.
152 */
153 private function add_image( $data ) {
154 if ( $this->context->main_image_url !== null ) {
155 $data['image'] = [
156 '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH,
157 ];
158 $data['thumbnailUrl'] = $this->context->main_image_url;
159 }
160
161 return $data;
162 }
163
164 /**
165 * Adds the potential action property to the Article Schema piece.
166 *
167 * @param array $data The Article data.
168 *
169 * @return array The Article data with the potential action added.
170 */
171 private function add_potential_action( $data ) {
172 /**
173 * Filter: 'wpseo_schema_article_potential_action_target' - Allows filtering of the schema Article potentialAction target.
174 *
175 * @api array $targets The URLs for the Article potentialAction target.
176 */
177 $targets = \apply_filters( 'wpseo_schema_article_potential_action_target', [ $this->context->canonical . '#respond' ] );
178
179 $data['potentialAction'][] = [
180 '@type' => 'CommentAction',
181 'name' => 'Comment',
182 'target' => $targets,
183 ];
184
185 return $data;
186 }
187
188 /**
189 * Does a simple word count but tries to be relatively smart about it.
190 *
191 * @param string $post_content The post content.
192 * @param string $post_title The post title.
193 *
194 * @return int The number of words in the content.
195 */
196 private function word_count( $post_content, $post_title = '' ) {
197 // Add the title to our word count.
198 $post_content = $post_title . ' ' . $post_content;
199
200 // Strip pre/code blocks and their content.
201 $post_content = \preg_replace( '@<(pre|code)[^>]*?>.*?</\\1>@si', '', $post_content );
202
203 // Strips all other tags.
204 $post_content = \wp_strip_all_tags( $post_content );
205
206 return \str_word_count( $post_content, 0 );
207 }
208 }
209