| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Generators\Schema; |
| 4 |
|
| 5 |
use WP_User; |
| 6 |
use Yoast\WP\SEO\Config\Schema_IDs; |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns schema Article data. |
| 10 |
*/ |
| 11 |
class Article extends Abstract_Schema_Piece { |
| 12 |
|
| 13 |
/** |
| 14 |
* Determines whether or not a piece should be added to the graph. |
| 15 |
* |
| 16 |
* @return bool |
| 17 |
*/ |
| 18 |
public function is_needed() { |
| 19 |
if ( $this->context->indexable->object_type !== 'post' ) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
// If we cannot output an author, we shouldn't output an Article. |
| 24 |
if ( ! $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type ) ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
if ( $this->context->schema_article_type !== 'None' ) { |
| 29 |
$this->context->has_article = true; |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns Article data. |
| 38 |
* |
| 39 |
* @return array Article data. |
| 40 |
*/ |
| 41 |
public function generate() { |
| 42 |
$author = \get_userdata( $this->context->post->post_author ); |
| 43 |
$data = [ |
| 44 |
'@type' => $this->context->schema_article_type, |
| 45 |
'@id' => $this->context->canonical . Schema_IDs::ARTICLE_HASH, |
| 46 |
'isPartOf' => [ '@id' => $this->context->main_schema_id ], |
| 47 |
'author' => [ |
| 48 |
'name' => ( $author instanceof WP_User ) ? $this->helpers->schema->html->smart_strip_tags( $author->display_name ) : '', |
| 49 |
'@id' => $this->helpers->schema->id->get_user_schema_id( $this->context->post->post_author, $this->context ), |
| 50 |
], |
| 51 |
'headline' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ), |
| 52 |
'datePublished' => $this->helpers->date->format( $this->context->post->post_date_gmt ), |
| 53 |
]; |
| 54 |
|
| 55 |
if ( \strtotime( $this->context->post->post_modified_gmt ) > \strtotime( $this->context->post->post_date_gmt ) ) { |
| 56 |
$data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt ); |
| 57 |
} |
| 58 |
|
| 59 |
$data['mainEntityOfPage'] = [ '@id' => $this->context->main_schema_id ]; |
| 60 |
$data['wordCount'] = $this->word_count( $this->context->post->post_content, $this->context->post->post_title ); |
| 61 |
|
| 62 |
if ( $this->context->post->comment_status === 'open' ) { |
| 63 |
$data['commentCount'] = \intval( $this->context->post->comment_count, 10 ); |
| 64 |
} |
| 65 |
|
| 66 |
if ( $this->context->site_represents_reference ) { |
| 67 |
$data['publisher'] = $this->context->site_represents_reference; |
| 68 |
} |
| 69 |
|
| 70 |
$data = $this->add_image( $data ); |
| 71 |
$data = $this->add_keywords( $data ); |
| 72 |
$data = $this->add_sections( $data ); |
| 73 |
$data = $this->helpers->schema->language->add_piece_language( $data ); |
| 74 |
|
| 75 |
if ( \post_type_supports( $this->context->post->post_type, 'comments' ) && $this->context->post->comment_status === 'open' ) { |
| 76 |
$data = $this->add_potential_action( $data ); |
| 77 |
} |
| 78 |
|
| 79 |
return $data; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Adds tags as keywords, if tags are assigned. |
| 84 |
* |
| 85 |
* @param array $data Article data. |
| 86 |
* |
| 87 |
* @return array Article data. |
| 88 |
*/ |
| 89 |
private function add_keywords( $data ) { |
| 90 |
/** |
| 91 |
* Filter: 'wpseo_schema_article_keywords_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data. |
| 92 |
* |
| 93 |
* @param string $taxonomy The chosen taxonomy. |
| 94 |
*/ |
| 95 |
$taxonomy = \apply_filters( 'wpseo_schema_article_keywords_taxonomy', 'post_tag' ); |
| 96 |
|
| 97 |
return $this->add_terms( $data, 'keywords', $taxonomy ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Adds categories as sections, if categories are assigned. |
| 102 |
* |
| 103 |
* @param array $data Article data. |
| 104 |
* |
| 105 |
* @return array Article data. |
| 106 |
*/ |
| 107 |
private function add_sections( $data ) { |
| 108 |
/** |
| 109 |
* Filter: 'wpseo_schema_article_sections_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data. |
| 110 |
* |
| 111 |
* @param string $taxonomy The chosen taxonomy. |
| 112 |
*/ |
| 113 |
$taxonomy = \apply_filters( 'wpseo_schema_article_sections_taxonomy', 'category' ); |
| 114 |
|
| 115 |
return $this->add_terms( $data, 'articleSection', $taxonomy ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Adds a term or multiple terms, comma separated, to a field. |
| 120 |
* |
| 121 |
* @param array $data Article data. |
| 122 |
* @param string $key The key in data to save the terms in. |
| 123 |
* @param string $taxonomy The taxonomy to retrieve the terms from. |
| 124 |
* |
| 125 |
* @return mixed Article data. |
| 126 |
*/ |
| 127 |
protected function add_terms( $data, $key, $taxonomy ) { |
| 128 |
$terms = \get_the_terms( $this->context->id, $taxonomy ); |
| 129 |
|
| 130 |
if ( ! \is_array( $terms ) ) { |
| 131 |
return $data; |
| 132 |
} |
| 133 |
|
| 134 |
$callback = static function ( $term ) { |
| 135 |
// We are using the WordPress internal translation. |
| 136 |
return $term->name !== \__( 'Uncategorized', 'default' ); |
| 137 |
}; |
| 138 |
$terms = \array_filter( $terms, $callback ); |
| 139 |
|
| 140 |
if ( empty( $terms ) ) { |
| 141 |
return $data; |
| 142 |
} |
| 143 |
|
| 144 |
$data[ $key ] = \wp_list_pluck( $terms, 'name' ); |
| 145 |
|
| 146 |
return $data; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds an image node if the post has a featured image. |
| 151 |
* |
| 152 |
* @param array $data The Article data. |
| 153 |
* |
| 154 |
* @return array The Article data. |
| 155 |
*/ |
| 156 |
private function add_image( $data ) { |
| 157 |
if ( $this->context->main_image_url !== null ) { |
| 158 |
$data['image'] = [ |
| 159 |
'@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH, |
| 160 |
]; |
| 161 |
$data['thumbnailUrl'] = $this->context->main_image_url; |
| 162 |
} |
| 163 |
|
| 164 |
return $data; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Adds the potential action property to the Article Schema piece. |
| 169 |
* |
| 170 |
* @param array $data The Article data. |
| 171 |
* |
| 172 |
* @return array The Article data with the potential action added. |
| 173 |
*/ |
| 174 |
private function add_potential_action( $data ) { |
| 175 |
/** |
| 176 |
* Filter: 'wpseo_schema_article_potential_action_target' - Allows filtering of the schema Article potentialAction target. |
| 177 |
* |
| 178 |
* @param array $targets The URLs for the Article potentialAction target. |
| 179 |
*/ |
| 180 |
$targets = \apply_filters( 'wpseo_schema_article_potential_action_target', [ $this->context->canonical . '#respond' ] ); |
| 181 |
|
| 182 |
$data['potentialAction'][] = [ |
| 183 |
'@type' => 'CommentAction', |
| 184 |
'name' => 'Comment', |
| 185 |
'target' => $targets, |
| 186 |
]; |
| 187 |
|
| 188 |
return $data; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Does a simple word count but tries to be relatively smart about it. |
| 193 |
* |
| 194 |
* @param string $post_content The post content. |
| 195 |
* @param string $post_title The post title. |
| 196 |
* |
| 197 |
* @return int The number of words in the content. |
| 198 |
*/ |
| 199 |
private function word_count( $post_content, $post_title = '' ) { |
| 200 |
// Add the title to our word count. |
| 201 |
$post_content = $post_title . ' ' . $post_content; |
| 202 |
|
| 203 |
// Strip pre/code blocks and their content. |
| 204 |
$post_content = \preg_replace( '@<(pre|code)[^>]*?>.*?</\\1>@si', '', $post_content ); |
| 205 |
|
| 206 |
// Add space between tags that don't have it. |
| 207 |
$post_content = \preg_replace( '@><@', '> <', $post_content ); |
| 208 |
|
| 209 |
// Strips all other tags. |
| 210 |
$post_content = \wp_strip_all_tags( $post_content ); |
| 211 |
|
| 212 |
$characters = ''; |
| 213 |
|
| 214 |
if ( \preg_match( '@[а-я]@ui', $post_content ) ) { |
| 215 |
// Correct counting of the number of words in the Russian and Ukrainian languages. |
| 216 |
$alphabet = [ |
| 217 |
'ru' => 'абвгдеёжзийклмнопрстуф� |
| 218 |
цчшщъыьэюя', |
| 219 |
'ua' => 'абвгґдеєжзиіїйклмнопрстуф� |
| 220 |
цчшщьюя', |
| 221 |
]; |
| 222 |
|
| 223 |
$characters = \implode( '', $alphabet ); |
| 224 |
$characters = \preg_split( '//u', $characters, -1, \PREG_SPLIT_NO_EMPTY ); |
| 225 |
$characters = \array_unique( $characters ); |
| 226 |
$characters = \implode( '', $characters ); |
| 227 |
$characters .= \mb_strtoupper( $characters ); |
| 228 |
} |
| 229 |
|
| 230 |
// Remove characters from HTML entities. |
| 231 |
$post_content = \preg_replace( '@&[a-z0-9]+;@i', ' ', \htmlentities( $post_content ) ); |
| 232 |
|
| 233 |
return \str_word_count( $post_content, 0, $characters ); |
| 234 |
} |
| 235 |
} |
| 236 |
|