first commit

This commit is contained in:
Rachit Bhargava
2023-07-21 17:12:10 -04:00
parent d0fe47dde4
commit 5d0f0734d8
14003 changed files with 2829464 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Context\Meta_Tags_Context;
use Yoast\WP\SEO\Surfaces\Helpers_Surface;
/**
* Class Abstract_Schema_Piece.
*/
abstract class Abstract_Schema_Piece {
/**
* The meta tags context.
*
* @var Meta_Tags_Context
*/
public $context;
/**
* The helpers surface
*
* @var Helpers_Surface
*/
public $helpers;
/**
* Optional identifier for this schema piece.
*
* Used in the `Schema_Generator::filter_graph_pieces_to_generate()` method.
*
* @var string
*/
public $identifier;
/**
* Generates the schema piece.
*
* @return mixed
*/
abstract public function generate();
/**
* Determines whether the schema piece is needed.
*
* @return bool
*/
abstract public function is_needed();
}

View File

@@ -0,0 +1,236 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema Article data.
*/
class Article extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
if ( $this->context->indexable->object_type !== 'post' ) {
return false;
}
// If we cannot output a publisher, we shouldn't output an Article.
if ( $this->context->site_represents === false ) {
return false;
}
// If we cannot output an author, we shouldn't output an Article.
if ( ! $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type ) ) {
return false;
}
if ( $this->context->schema_article_type !== 'None' ) {
$this->context->has_article = true;
return true;
}
return false;
}
/**
* Returns Article data.
*
* @return array Article data.
*/
public function generate() {
$author = \get_userdata( $this->context->post->post_author );
$data = [
'@type' => $this->context->schema_article_type,
'@id' => $this->context->canonical . Schema_IDs::ARTICLE_HASH,
'isPartOf' => [ '@id' => $this->context->main_schema_id ],
'author' => [
'name' => ( $author instanceof \WP_User ) ? $this->helpers->schema->html->smart_strip_tags( $author->display_name ) : '',
'@id' => $this->helpers->schema->id->get_user_schema_id( $this->context->post->post_author, $this->context ),
],
'headline' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
'datePublished' => $this->helpers->date->format( $this->context->post->post_date_gmt ),
'dateModified' => $this->helpers->date->format( $this->context->post->post_modified_gmt ),
'mainEntityOfPage' => [ '@id' => $this->context->main_schema_id ],
'wordCount' => $this->word_count( $this->context->post->post_content, $this->context->post->post_title ),
];
if ( $this->context->post->comment_status === 'open' ) {
$data['commentCount'] = \intval( $this->context->post->comment_count, 10 );
}
if ( $this->context->site_represents_reference ) {
$data['publisher'] = $this->context->site_represents_reference;
}
$data = $this->add_image( $data );
$data = $this->add_keywords( $data );
$data = $this->add_sections( $data );
$data = $this->helpers->schema->language->add_piece_language( $data );
if ( \post_type_supports( $this->context->post->post_type, 'comments' ) && $this->context->post->comment_status === 'open' ) {
$data = $this->add_potential_action( $data );
}
return $data;
}
/**
* Adds tags as keywords, if tags are assigned.
*
* @param array $data Article data.
*
* @return array Article data.
*/
private function add_keywords( $data ) {
/**
* Filter: 'wpseo_schema_article_keywords_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
*
* @api string $taxonomy The chosen taxonomy.
*/
$taxonomy = \apply_filters( 'wpseo_schema_article_keywords_taxonomy', 'post_tag' );
return $this->add_terms( $data, 'keywords', $taxonomy );
}
/**
* Adds categories as sections, if categories are assigned.
*
* @param array $data Article data.
*
* @return array Article data.
*/
private function add_sections( $data ) {
/**
* Filter: 'wpseo_schema_article_sections_taxonomy' - Allow changing the taxonomy used to assign keywords to a post type Article data.
*
* @api string $taxonomy The chosen taxonomy.
*/
$taxonomy = \apply_filters( 'wpseo_schema_article_sections_taxonomy', 'category' );
return $this->add_terms( $data, 'articleSection', $taxonomy );
}
/**
* Adds a term or multiple terms, comma separated, to a field.
*
* @param array $data Article data.
* @param string $key The key in data to save the terms in.
* @param string $taxonomy The taxonomy to retrieve the terms from.
*
* @return mixed Article data.
*/
protected function add_terms( $data, $key, $taxonomy ) {
$terms = \get_the_terms( $this->context->id, $taxonomy );
if ( ! \is_array( $terms ) ) {
return $data;
}
$callback = static function( $term ) {
// We are using the WordPress internal translation.
return $term->name !== \__( 'Uncategorized', 'default' );
};
$terms = \array_filter( $terms, $callback );
if ( empty( $terms ) ) {
return $data;
}
$data[ $key ] = \wp_list_pluck( $terms, 'name' );
return $data;
}
/**
* Adds an image node if the post has a featured image.
*
* @param array $data The Article data.
*
* @return array The Article data.
*/
private function add_image( $data ) {
if ( $this->context->main_image_url !== null ) {
$data['image'] = [
'@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH,
];
$data['thumbnailUrl'] = $this->context->main_image_url;
}
return $data;
}
/**
* Adds the potential action property to the Article Schema piece.
*
* @param array $data The Article data.
*
* @return array The Article data with the potential action added.
*/
private function add_potential_action( $data ) {
/**
* Filter: 'wpseo_schema_article_potential_action_target' - Allows filtering of the schema Article potentialAction target.
*
* @api array $targets The URLs for the Article potentialAction target.
*/
$targets = \apply_filters( 'wpseo_schema_article_potential_action_target', [ $this->context->canonical . '#respond' ] );
$data['potentialAction'][] = [
'@type' => 'CommentAction',
'name' => 'Comment',
'target' => $targets,
];
return $data;
}
/**
* Does a simple word count but tries to be relatively smart about it.
*
* @param string $post_content The post content.
* @param string $post_title The post title.
*
* @return int The number of words in the content.
*/
private function word_count( $post_content, $post_title = '' ) {
// Add the title to our word count.
$post_content = $post_title . ' ' . $post_content;
// Strip pre/code blocks and their content.
$post_content = \preg_replace( '@<(pre|code)[^>]*?>.*?</\\1>@si', '', $post_content );
// Add space between tags that don't have it.
$post_content = \preg_replace( '@><@', '> <', $post_content );
// Strips all other tags.
$post_content = \wp_strip_all_tags( $post_content );
$characters = '';
if ( \preg_match( '@[а-я]@ui', $post_content ) ) {
// Correct counting of the number of words in the Russian and Ukrainian languages.
$alphabet = [
'ru' => 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя',
'ua' => 'абвгґдеєжзиіїйклмнопрстуфхцчшщьюя',
];
$characters = \implode( '', $alphabet );
$characters = \preg_split( '//u', $characters, -1, \PREG_SPLIT_NO_EMPTY );
$characters = \array_unique( $characters );
$characters = \implode( '', $characters );
$characters .= \mb_strtoupper( $characters );
}
// Remove characters from HTML entities.
$post_content = \preg_replace( '@&[a-z0-9]+;@i', ' ', \htmlentities( $post_content ) );
return \str_word_count( $post_content, 0, $characters );
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
/**
* Returns schema Author data.
*/
class Author extends Person {
/**
* Determine whether we should return Person schema.
*
* @return bool
*/
public function is_needed() {
if ( $this->context->indexable->object_type === 'user' ) {
return true;
}
if (
$this->context->indexable->object_type === 'post'
&& $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
&& $this->context->schema_article_type !== 'None'
) {
return true;
}
return false;
}
/**
* Returns Person Schema data.
*
* @return bool|array Person data on success, false on failure.
*/
public function generate() {
$user_id = $this->determine_user_id();
if ( ! $user_id ) {
return false;
}
$data = $this->build_person_data( $user_id );
if ( $this->site_represents_current_author() === false ) {
$data['@type'] = [ 'Person' ];
unset( $data['logo'] );
}
// If this is an author page, the Person object is the main object, so we set it as such here.
if ( $this->context->indexable->object_type === 'user' ) {
$data['mainEntityOfPage'] = [
'@id' => $this->context->main_schema_id,
];
}
// If this is a post and the author archives are enabled, set the author archive url as the author url.
if ( $this->context->indexable->object_type === 'post' ) {
if ( $this->helpers->options->get( 'disable-author' ) !== true ) {
$data['url'] = $this->helpers->user->get_the_author_posts_url( $user_id );
}
}
return $data;
}
/**
* Determines a User ID for the Person data.
*
* @return bool|int User ID or false upon return.
*/
protected function determine_user_id() {
$user_id = 0;
if ( $this->context->indexable->object_type === 'post' ) {
$user_id = (int) $this->context->post->post_author;
}
if ( $this->context->indexable->object_type === 'user' ) {
$user_id = $this->context->indexable->object_id;
}
/**
* Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
*
* @api int|bool $user_id The user ID currently determined.
*/
$user_id = \apply_filters( 'wpseo_schema_person_user_id', $user_id );
if ( \is_int( $user_id ) && $user_id > 0 ) {
return $user_id;
}
return false;
}
/**
* An author should not have an image from options, this only applies to persons.
*
* @param array $data The Person schema.
* @param string $schema_id The string used in the `@id` for the schema.
* @param bool $add_hash Whether or not the person's image url hash should be added to the image id.
* @param WP_User $user_data User data.
*
* @return array The Person schema.
*/
protected function set_image_from_options( $data, $schema_id, $add_hash = false, $user_data = null ) {
if ( $this->site_represents_current_author( $user_data ) ) {
return parent::set_image_from_options( $data, $schema_id, $add_hash, $user_data );
}
return $data;
}
}

View File

@@ -0,0 +1,186 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema Breadcrumb data.
*/
class Breadcrumb extends Abstract_Schema_Piece {
/**
* Determine if we should add a breadcrumb attribute.
*
* @return bool
*/
public function is_needed() {
if ( $this->context->indexable->object_type === 'unknown' ) {
return false;
}
if ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ) {
return false;
}
return true;
}
/**
* Returns Schema breadcrumb data to allow recognition of page's position in the site hierarchy.
*
* @link https://developers.google.com/search/docs/data-types/breadcrumb
*
* @return bool|array Array on success, false on failure.
*/
public function generate() {
$breadcrumbs = $this->context->presentation->breadcrumbs;
$list_elements = [];
// In case of pagination, replace the last breadcrumb, because it only contains "Page [number]" and has no URL.
if (
(
$this->helpers->current_page->is_paged()
|| $this->context->indexable->number_of_pages > 1
) && (
// Do not replace the last breadcrumb on static post pages.
! $this->helpers->current_page->is_static_posts_page()
// Do not remove the last breadcrumb if only one exists (bugfix for custom paginated frontpages).
&& \count( $breadcrumbs ) > 1
)
) {
\array_pop( $breadcrumbs );
}
// Only output breadcrumbs that are not hidden.
$breadcrumbs = \array_filter( $breadcrumbs, [ $this, 'not_hidden' ] );
\reset( $breadcrumbs );
/*
* Check whether at least one of the breadcrumbs is broken.
* If so, do not output anything.
*/
foreach ( $breadcrumbs as $breadcrumb ) {
if ( $this->is_broken( $breadcrumb ) ) {
return false;
}
}
// Create the last breadcrumb.
$last_breadcrumb = \array_pop( $breadcrumbs );
$breadcrumbs[] = $this->format_last_breadcrumb( $last_breadcrumb );
// If this is a static front page, prevent nested pages from creating a trail.
if ( $this->helpers->current_page->is_home_static_page() ) {
// Check if we're dealing with a nested page.
if ( \count( $breadcrumbs ) > 1 ) {
// Store the breadcrumbs home variable before dropping the parent page from the Schema.
$breadcrumbs_home = $breadcrumbs[0]['text'];
$breadcrumbs = [ \array_pop( $breadcrumbs ) ];
// Make the child page show the breadcrumbs home variable rather than its own title.
$breadcrumbs[0]['text'] = $breadcrumbs_home;
}
}
$breadcrumbs = \array_filter( $breadcrumbs, [ $this, 'not_empty_text' ] );
$breadcrumbs = \array_values( $breadcrumbs );
// Create intermediate breadcrumbs.
foreach ( $breadcrumbs as $index => $breadcrumb ) {
$list_elements[] = $this->create_breadcrumb( $index, $breadcrumb );
}
return [
'@type' => 'BreadcrumbList',
'@id' => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH,
'itemListElement' => $list_elements,
];
}
/**
* Returns a breadcrumb array.
*
* @param int $index The position in the list.
* @param array $breadcrumb The position in the list.
*
* @return array A breadcrumb listItem.
*/
private function create_breadcrumb( $index, $breadcrumb ) {
$crumb = [
'@type' => 'ListItem',
'position' => ( $index + 1 ),
'name' => $this->helpers->schema->html->smart_strip_tags( $breadcrumb['text'] ),
];
if ( ! empty( $breadcrumb['url'] ) ) {
$crumb['item'] = $breadcrumb['url'];
}
return $crumb;
}
/**
* Creates the last breadcrumb in the breadcrumb list, omitting the URL per Google's spec.
*
* @link https://developers.google.com/search/docs/data-types/breadcrumb
*
* @param array $breadcrumb The position in the list.
*
* @return array The last of the breadcrumbs.
*/
private function format_last_breadcrumb( $breadcrumb ) {
unset( $breadcrumb['url'] );
return $breadcrumb;
}
/**
* Tests if the breadcrumb is broken.
* A breadcrumb is considered broken:
* - when it is not an array.
* - when it has no URL or text.
*
* @param array $breadcrumb The breadcrumb to test.
*
* @return bool `true` if the breadcrumb is broken.
*/
private function is_broken( $breadcrumb ) {
// A breadcrumb is broken if it is not an array.
if ( ! \is_array( $breadcrumb ) ) {
return true;
}
// A breadcrumb is broken if it does not contain a URL or text.
if ( ! \array_key_exists( 'url', $breadcrumb ) || ! \array_key_exists( 'text', $breadcrumb ) ) {
return true;
}
return false;
}
/**
* Checks whether the breadcrumb is not set to be hidden.
*
* @param array $breadcrumb The breadcrumb array.
*
* @return bool If the breadcrumb should not be hidden.
*/
private function not_hidden( $breadcrumb ) {
return empty( $breadcrumb['hide_in_schema'] );
}
/**
* Checks whether the breadcrumb has a not empty text.
*
* @param array $breadcrumb The breadcrumb array.
*
* @return bool If the breadcrumb has a not empty text.
*/
private function not_empty_text( $breadcrumb ) {
return ! empty( $breadcrumb['text'] );
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
/**
* Returns schema FAQ data.
*/
class FAQ extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
if ( empty( $this->context->blocks['yoast/faq-block'] ) ) {
return false;
}
if ( ! \is_array( $this->context->schema_page_type ) ) {
$this->context->schema_page_type = [ $this->context->schema_page_type ];
}
$this->context->schema_page_type[] = 'FAQPage';
$this->context->main_entity_of_page = $this->generate_ids();
return true;
}
/**
* Generate the IDs so we can link to them in the main entity.
*
* @return array
*/
private function generate_ids() {
$ids = [];
foreach ( $this->context->blocks['yoast/faq-block'] as $block ) {
foreach ( $block['attrs']['questions'] as $index => $question ) {
if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
continue;
}
$ids[] = [ '@id' => $this->context->canonical . '#' . \esc_attr( $question['id'] ) ];
}
}
return $ids;
}
/**
* Render a list of questions, referencing them by ID.
*
* @return array Our Schema graph.
*/
public function generate() {
$graph = [];
$questions = [];
foreach ( $this->context->blocks['yoast/faq-block'] as $index => $block ) {
$questions = \array_merge( $questions, $block['attrs']['questions'] );
}
foreach ( $questions as $index => $question ) {
if ( ! isset( $question['jsonAnswer'] ) || empty( $question['jsonAnswer'] ) ) {
continue;
}
$graph[] = $this->generate_question_block( $question, ( $index + 1 ) );
}
return $graph;
}
/**
* Generate a Question piece.
*
* @param array $question The question to generate schema for.
* @param int $position The position of the question.
*
* @return array Schema.org Question piece.
*/
protected function generate_question_block( $question, $position ) {
$url = $this->context->canonical . '#' . \esc_attr( $question['id'] );
$data = [
'@type' => 'Question',
'@id' => $url,
'position' => $position,
'url' => $url,
'name' => $this->helpers->schema->html->smart_strip_tags( $question['jsonQuestion'] ),
'answerCount' => 1,
'acceptedAnswer' => $this->add_accepted_answer_property( $question ),
];
$data = $this->helpers->schema->language->add_piece_language( $data );
return $data;
}
/**
* Adds the Questions `acceptedAnswer` property.
*
* @param array $question The question to add the acceptedAnswer to.
*
* @return array Schema.org Question piece.
*/
protected function add_accepted_answer_property( $question ) {
$data = [
'@type' => 'Answer',
'text' => $this->helpers->schema->html->sanitize( $question['jsonAnswer'] ),
];
$data = $this->helpers->schema->language->add_piece_language( $data );
return $data;
}
}

View File

@@ -0,0 +1,186 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema HowTo data.
*/
class HowTo extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
return ! empty( $this->context->blocks['yoast/how-to-block'] );
}
/**
* Renders a list of questions, referencing them by ID.
*
* @return array Our Schema graph.
*/
public function generate() {
$graph = [];
foreach ( $this->context->blocks['yoast/how-to-block'] as $index => $block ) {
$this->add_how_to( $graph, $block, $index );
}
return $graph;
}
/**
* Adds the duration of the task to the Schema.
*
* @param array $data Our How-To schema data.
* @param array $attributes The block data attributes.
*/
private function add_duration( &$data, $attributes ) {
if ( empty( $attributes['hasDuration'] ) ) {
return;
}
$days = empty( $attributes['days'] ) ? 0 : $attributes['days'];
$hours = empty( $attributes['hours'] ) ? 0 : $attributes['hours'];
$minutes = empty( $attributes['minutes'] ) ? 0 : $attributes['minutes'];
if ( ( $days + $hours + $minutes ) > 0 ) {
$data['totalTime'] = \esc_attr( 'P' . $days . 'DT' . $hours . 'H' . $minutes . 'M' );
}
}
/**
* Adds the steps to our How-To output.
*
* @param array $data Our How-To schema data.
* @param array $steps Our How-To block's steps.
*/
private function add_steps( &$data, $steps ) {
foreach ( $steps as $step ) {
$schema_id = $this->context->canonical . '#' . \esc_attr( $step['id'] );
$schema_step = [
'@type' => 'HowToStep',
'url' => $schema_id,
];
if ( isset( $step['jsonText'] ) ) {
$json_text = $this->helpers->schema->html->sanitize( $step['jsonText'] );
}
if ( isset( $step['jsonName'] ) ) {
$json_name = $this->helpers->schema->html->smart_strip_tags( $step['jsonName'] );
}
if ( empty( $json_name ) ) {
if ( empty( $step['text'] ) ) {
continue;
}
$schema_step['text'] = '';
$this->add_step_image( $schema_step, $step );
// If there is no text and no image, don't output the step.
if ( empty( $json_text ) && empty( $schema_step['image'] ) ) {
continue;
}
if ( ! empty( $json_text ) ) {
$schema_step['text'] = $json_text;
}
}
elseif ( empty( $json_text ) ) {
$schema_step['text'] = $json_name;
}
else {
$schema_step['name'] = $json_name;
$this->add_step_description( $schema_step, $json_text );
$this->add_step_image( $schema_step, $step );
}
$data['step'][] = $schema_step;
}
}
/**
* Checks if we have a step description, if we do, add it.
*
* @param array $schema_step Our Schema output for the Step.
* @param string $json_text The step text.
*/
private function add_step_description( &$schema_step, $json_text ) {
$schema_step['itemListElement'] = [
[
'@type' => 'HowToDirection',
'text' => $json_text,
],
];
}
/**
* Checks if we have a step image, if we do, add it.
*
* @param array $schema_step Our Schema output for the Step.
* @param array $step The step block data.
*/
private function add_step_image( &$schema_step, $step ) {
if ( isset( $step['text'] ) && is_array( $step['text'] ) ) {
foreach ( $step['text'] as $line ) {
if ( \is_array( $line ) && isset( $line['type'] ) && $line['type'] === 'img' ) {
$schema_step['image'] = $this->get_image_schema( \esc_url( $line['props']['src'] ) );
}
}
}
}
/**
* Generates the HowTo schema for a block.
*
* @param array $graph Our Schema data.
* @param array $block The How-To block content.
* @param int $index The index of the current block.
*/
protected function add_how_to( &$graph, $block, $index ) {
$data = [
'@type' => 'HowTo',
'@id' => $this->context->canonical . '#howto-' . ( $index + 1 ),
'name' => $this->helpers->schema->html->smart_strip_tags( $this->helpers->post->get_post_title_with_fallback( $this->context->id ) ),
'mainEntityOfPage' => [ '@id' => $this->context->main_schema_id ],
'description' => '',
];
if ( $this->context->has_article ) {
$data['mainEntityOfPage'] = [ '@id' => $this->context->main_schema_id . Schema_IDs::ARTICLE_HASH ];
}
if ( isset( $block['attrs']['jsonDescription'] ) ) {
$data['description'] = $this->helpers->schema->html->sanitize( $block['attrs']['jsonDescription'] );
}
$this->add_duration( $data, $block['attrs'] );
$this->add_steps( $data, $block['attrs']['steps'] );
$data = $this->helpers->schema->language->add_piece_language( $data );
$graph[] = $data;
}
/**
* Generates the image schema from the attachment $url.
*
* @param string $url Attachment url.
*
* @return array Image schema.
*/
protected function get_image_schema( $url ) {
$schema_id = $this->context->canonical . '#schema-image-' . \md5( $url );
return $this->helpers->schema->image->generate_from_url( $schema_id, $url );
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns ImageObject schema data.
*/
class Main_Image extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
return true;
}
/**
* Adds a main image for the current URL to the schema if there is one.
*
* This can be either the featured image or the first image in the content of the page.
*
* @return false|array Image Schema.
*/
public function generate() {
$image_id = $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH;
// The featured image.
if ( $this->context->main_image_id ) {
$generated_schema = $this->helpers->schema->image->generate_from_attachment_id( $image_id, $this->context->main_image_id );
$this->context->main_image_url = $generated_schema['url'];
return $generated_schema;
}
// The first image in the content.
if ( $this->context->main_image_url ) {
return $this->helpers->schema->image->generate_from_url( $image_id, $this->context->main_image_url );
}
return false;
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema Organization data.
*/
class Organization extends Abstract_Schema_Piece {
/**
* Determines whether an Organization graph piece should be added.
*
* @return bool
*/
public function is_needed() {
return $this->context->site_represents === 'company';
}
/**
* Returns the Organization Schema data.
*
* @return array The Organization schema.
*/
public function generate() {
$logo_schema_id = $this->context->site_url . Schema_IDs::ORGANIZATION_LOGO_HASH;
if ( $this->context->company_logo_meta ) {
$logo = $this->helpers->schema->image->generate_from_attachment_meta( $logo_schema_id, $this->context->company_logo_meta, $this->context->company_name );
}
else {
$logo = $this->helpers->schema->image->generate_from_attachment_id( $logo_schema_id, $this->context->company_logo_id, $this->context->company_name );
}
$organization = [
'@type' => 'Organization',
'@id' => $this->context->site_url . Schema_IDs::ORGANIZATION_HASH,
'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->company_name ),
];
if ( ! empty( $this->context->company_alternate_name ) ) {
$organization['alternateName'] = $this->context->company_alternate_name;
}
$organization['url'] = $this->context->site_url;
$organization['logo'] = $logo;
$organization['image'] = [ '@id' => $logo['@id'] ];
$same_as = \array_values( \array_unique( \array_filter( $this->fetch_social_profiles() ) ) );
if ( ! empty( $same_as ) ) {
$organization['sameAs'] = $same_as;
}
return $organization;
}
/**
* Retrieve the social profiles to display in the organization schema.
*
* @return array An array of social profiles.
*/
private function fetch_social_profiles() {
$profiles = $this->helpers->social_profiles->get_organization_social_profiles();
if ( isset( $profiles['other_social_urls'] ) ) {
$other_social_urls = $profiles['other_social_urls'];
unset( $profiles['other_social_urls'] );
$profiles = \array_merge( $profiles, $other_social_urls );
}
/**
* Filter: 'wpseo_schema_organization_social_profiles' - Allows filtering social profiles for the
* represented organization.
*
* @api string[] $profiles
*/
$profiles = \apply_filters( 'wpseo_schema_organization_social_profiles', $profiles );
return $profiles;
}
}

View File

@@ -0,0 +1,312 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use WP_User;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema Person data.
*/
class Person extends Abstract_Schema_Piece {
/**
* Array of the social profiles we display for a Person.
*
* @var string[]
*/
private $social_profiles = [
'facebook',
'instagram',
'linkedin',
'pinterest',
'twitter',
'myspace',
'youtube',
'soundcloud',
'tumblr',
'wikipedia',
];
/**
* The Schema type we use for this class.
*
* @var string[]
*/
protected $type = [ 'Person', 'Organization' ];
/**
* Determine whether we should return Person schema.
*
* @return bool
*/
public function is_needed() {
// Using an author piece instead.
if ( $this->site_represents_current_author() ) {
return false;
}
return $this->context->site_represents === 'person' || $this->context->indexable->object_type === 'user';
}
/**
* Returns Person Schema data.
*
* @return bool|array Person data on success, false on failure.
*/
public function generate() {
$user_id = $this->determine_user_id();
if ( ! $user_id ) {
return false;
}
return $this->build_person_data( $user_id );
}
/**
* Determines a User ID for the Person data.
*
* @return bool|int User ID or false upon return.
*/
protected function determine_user_id() {
/**
* Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
*
* @api int|bool $user_id The user ID currently determined.
*/
$user_id = \apply_filters( 'wpseo_schema_person_user_id', $this->context->site_user_id );
// It should to be an integer higher than 0.
if ( \is_int( $user_id ) && $user_id > 0 ) {
return $user_id;
}
return false;
}
/**
* Retrieve a list of social profile URLs for Person.
*
* @param array $same_as_urls Array of SameAs URLs.
* @param int $user_id User ID.
*
* @return string[] A list of SameAs URLs.
*/
protected function get_social_profiles( $same_as_urls, $user_id ) {
/**
* Filter: 'wpseo_schema_person_social_profiles' - Allows filtering of social profiles per user.
*
* @param int $user_id The current user we're grabbing social profiles for.
*
* @api string[] $social_profiles The array of social profiles to retrieve. Each should be a user meta field
* key. As they are retrieved using the WordPress function `get_the_author_meta`.
*/
$social_profiles = \apply_filters( 'wpseo_schema_person_social_profiles', $this->social_profiles, $user_id );
// We can only handle an array.
if ( ! \is_array( $social_profiles ) ) {
return $same_as_urls;
}
foreach ( $social_profiles as $profile ) {
// Skip non-string values.
if ( ! \is_string( $profile ) ) {
continue;
}
$social_url = $this->url_social_site( $profile, $user_id );
if ( $social_url ) {
$same_as_urls[] = $social_url;
}
}
return $same_as_urls;
}
/**
* Builds our array of Schema Person data for a given user ID.
*
* @param int $user_id The user ID to use.
* @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
*
* @return array An array of Schema Person data.
*/
protected function build_person_data( $user_id, $add_hash = false ) {
$user_data = \get_userdata( $user_id );
$data = [
'@type' => $this->type,
'@id' => $this->helpers->schema->id->get_user_schema_id( $user_id, $this->context ),
];
// Safety check for the `get_userdata` WP function, which could return false.
if ( $user_data === false ) {
return $data;
}
$data['name'] = $this->helpers->schema->html->smart_strip_tags( $user_data->display_name );
$data = $this->add_image( $data, $user_data, $add_hash );
if ( ! empty( $user_data->description ) ) {
$data['description'] = $this->helpers->schema->html->smart_strip_tags( $user_data->description );
}
$data = $this->add_same_as_urls( $data, $user_data, $user_id );
/**
* Filter: 'wpseo_schema_person_data' - Allows filtering of schema data per user.
*
* @param array $data The schema data we have for this person.
* @param int $user_id The current user we're collecting schema data for.
*/
$data = \apply_filters( 'wpseo_schema_person_data', $data, $user_id );
return $data;
}
/**
* Returns an ImageObject for the persons avatar.
*
* @param array $data The Person schema.
* @param WP_User $user_data User data.
* @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
*
* @return array The Person schema.
*/
protected function add_image( $data, $user_data, $add_hash = false ) {
$schema_id = $this->context->site_url . Schema_IDs::PERSON_LOGO_HASH;
$data = $this->set_image_from_options( $data, $schema_id, $add_hash, $user_data );
if ( ! isset( $data['image'] ) ) {
$data = $this->set_image_from_avatar( $data, $user_data, $schema_id, $add_hash );
}
if ( \is_array( $this->type ) && \in_array( 'Organization', $this->type, true ) ) {
$data_logo = isset( $data['image']['@id'] ) ? $data['image']['@id'] : $schema_id;
$data['logo'] = [ '@id' => $data_logo ];
}
return $data;
}
/**
* Generate the person image from our settings.
*
* @param array $data The Person schema.
* @param string $schema_id The string used in the `@id` for the schema.
* @param bool $add_hash Whether or not the person's image url hash should be added to the image id.
* @param WP_User $user_data User data.
*
* @return array The Person schema.
*/
protected function set_image_from_options( $data, $schema_id, $add_hash = false, $user_data = null ) {
if ( $this->context->site_represents !== 'person' ) {
return $data;
}
if ( \is_array( $this->context->person_logo_meta ) ) {
$data['image'] = $this->helpers->schema->image->generate_from_attachment_meta( $schema_id, $this->context->person_logo_meta, $data['name'], $add_hash );
}
return $data;
}
/**
* Generate the person logo from gravatar.
*
* @param array $data The Person schema.
* @param WP_User $user_data User data.
* @param string $schema_id The string used in the `@id` for the schema.
* @param bool $add_hash Wether or not the person's image url hash should be added to the image id.
*
* @return array The Person schema.
*/
protected function set_image_from_avatar( $data, $user_data, $schema_id, $add_hash = false ) {
// If we don't have an image in our settings, fall back to an avatar, if we're allowed to.
$show_avatars = \get_option( 'show_avatars' );
if ( ! $show_avatars ) {
return $data;
}
$url = \get_avatar_url( $user_data->user_email );
if ( empty( $url ) ) {
return $data;
}
$data['image'] = $this->helpers->schema->image->simple_image_object( $schema_id, $url, $user_data->display_name, $add_hash );
return $data;
}
/**
* Returns an author's social site URL.
*
* @param string $social_site The social site to retrieve the URL for.
* @param mixed $user_id The user ID to use function outside of the loop.
*
* @return string
*/
protected function url_social_site( $social_site, $user_id = false ) {
$url = \get_the_author_meta( $social_site, $user_id );
if ( ! empty( $url ) && $social_site === 'twitter' ) {
$url = 'https://twitter.com/' . $url;
}
return $url;
}
/**
* Checks the site is represented by the same person as this indexable.
*
* @param WP_User $user_data User data.
*
* @return bool True when the site is represented by the same person as this indexable.
*/
protected function site_represents_current_author( $user_data = null ) {
// Can only be the case when the site represents a user.
if ( $this->context->site_represents !== 'person' ) {
return false;
}
// Article post from the same user as the site represents.
if (
$this->context->indexable->object_type === 'post'
&& $this->helpers->schema->article->is_author_supported( $this->context->indexable->object_sub_type )
&& $this->context->schema_article_type !== 'None'
) {
$user_id = ( ( ! \is_null( $user_data ) ) && ( isset( $user_data->ID ) ) ) ? $user_data->ID : $this->context->indexable->author_id;
return $this->context->site_user_id === $user_id;
}
// Author archive from the same user as the site represents.
return $this->context->indexable->object_type === 'user' && $this->context->site_user_id === $this->context->indexable->object_id;
}
/**
* Builds our SameAs array.
*
* @param array $data The Person schema data.
* @param WP_User $user_data The user data object.
* @param int $user_id The user ID to use.
*
* @return array The Person schema data.
*/
protected function add_same_as_urls( $data, $user_data, $user_id ) {
$same_as_urls = [];
// Add the "Website" field from WordPress' contact info.
if ( ! empty( $user_data->user_url ) ) {
$same_as_urls[] = $user_data->user_url;
}
// Add the social profiles.
$same_as_urls = $this->get_social_profiles( $same_as_urls, $user_id );
if ( ! empty( $same_as_urls ) ) {
$same_as_urls = \array_values( \array_unique( $same_as_urls ) );
$data['sameAs'] = $same_as_urls;
}
return $data;
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use WP_Post;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema WebPage data.
*/
class WebPage extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
if ( $this->context->indexable->object_type === 'unknown' ) {
return false;
}
return ! ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' );
}
/**
* Returns WebPage schema data.
*
* @return array WebPage schema data.
*/
public function generate() {
$data = [
'@type' => $this->context->schema_page_type,
'@id' => $this->context->main_schema_id,
'url' => $this->context->canonical,
'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->title ),
'isPartOf' => [
'@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH,
],
];
if ( empty( $this->context->canonical ) && \is_search() ) {
$data['url'] = $this->build_search_url();
}
if ( $this->helpers->current_page->is_front_page() ) {
if ( $this->context->site_represents_reference ) {
$data['about'] = $this->context->site_represents_reference;
}
}
$data = $this->add_image( $data );
if ( $this->context->indexable->object_type === 'post' ) {
$data['datePublished'] = $this->helpers->date->format( $this->context->post->post_date_gmt );
$data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt );
if ( $this->context->indexable->object_sub_type === 'post' ) {
$data = $this->add_author( $data, $this->context->post );
}
}
if ( ! empty( $this->context->description ) ) {
$data['description'] = $this->helpers->schema->html->smart_strip_tags( $this->context->description );
}
if ( $this->add_breadcrumbs() ) {
$data['breadcrumb'] = [
'@id' => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH,
];
}
if ( ! empty( $this->context->main_entity_of_page ) ) {
$data['mainEntity'] = $this->context->main_entity_of_page;
}
$data = $this->helpers->schema->language->add_piece_language( $data );
$data = $this->add_potential_action( $data );
return $data;
}
/**
* Adds an author property to the $data if the WebPage is not represented.
*
* @param array $data The WebPage schema.
* @param WP_Post $post The post the context is representing.
*
* @return array The WebPage schema.
*/
public function add_author( $data, $post ) {
if ( $this->context->site_represents === false ) {
$data['author'] = [ '@id' => $this->helpers->schema->id->get_user_schema_id( $post->post_author, $this->context ) ];
}
return $data;
}
/**
* If we have an image, make it the primary image of the page.
*
* @param array $data WebPage schema data.
*/
public function add_image( $data ) {
if ( $this->context->has_image ) {
$data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
$data['image'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
$data['thumbnailUrl'] = $this->context->main_image_url;
}
return $data;
}
/**
* Determine if we should add a breadcrumb attribute.
*
* @return bool
*/
private function add_breadcrumbs() {
if ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ) {
return false;
}
return true;
}
/**
* Adds the potential action property to the WebPage Schema piece.
*
* @param array $data The WebPage data.
*
* @return array The WebPage data with the potential action added.
*/
private function add_potential_action( $data ) {
$url = $this->context->canonical;
if ( $data['@type'] === 'CollectionPage' || ( \is_array( $data['@type'] ) && \in_array( 'CollectionPage', $data['@type'], true ) ) ) {
return $data;
}
/**
* Filter: 'wpseo_schema_webpage_potential_action_target' - Allows filtering of the schema WebPage potentialAction target.
*
* @api array $targets The URLs for the WebPage potentialAction target.
*/
$targets = \apply_filters( 'wpseo_schema_webpage_potential_action_target', [ $url ] );
$data['potentialAction'][] = [
'@type' => 'ReadAction',
'target' => $targets,
];
return $data;
}
/**
* Creates the search URL for use when if there is no canonical.
*
* @return string Search URL.
*/
private function build_search_url() {
return $this->context->site_url . '?s=' . \get_search_query();
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Yoast\WP\SEO\Generators\Schema;
use Yoast\WP\SEO\Config\Schema_IDs;
/**
* Returns schema Website data.
*/
class Website extends Abstract_Schema_Piece {
/**
* Determines whether or not a piece should be added to the graph.
*
* @return bool
*/
public function is_needed() {
return true;
}
/**
* Outputs code to allow recognition of the internal search engine.
*
* @return array Website data blob.
*/
public function generate() {
$data = [
'@type' => 'WebSite',
'@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH,
'url' => $this->context->site_url,
'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->site_name ),
'description' => \get_bloginfo( 'description' ),
];
if ( $this->context->site_represents_reference ) {
$data['publisher'] = $this->context->site_represents_reference;
}
$data = $this->add_alternate_name( $data );
$data = $this->internal_search_section( $data );
$data = $this->helpers->schema->language->add_piece_language( $data );
return $data;
}
/**
* Returns an alternate name if one was specified in the Yoast SEO settings.
*
* @param array $data The website data array.
*
* @return array
*/
private function add_alternate_name( $data ) {
if ( $this->context->alternate_site_name !== '' ) {
$data['alternateName'] = $this->helpers->schema->html->smart_strip_tags( $this->context->alternate_site_name );
}
return $data;
}
/**
* Adds the internal search JSON LD code to the homepage if it's not disabled.
*
* @link https://developers.google.com/search/docs/data-types/sitelinks-searchbox
*
* @param array $data The website data array.
*
* @return array
*/
private function internal_search_section( $data ) {
/**
* Filter: 'disable_wpseo_json_ld_search' - Allow disabling of the json+ld output.
*
* @api bool $display_search Whether or not to display json+ld search on the frontend.
*/
if ( \apply_filters( 'disable_wpseo_json_ld_search', false ) ) {
return $data;
}
/**
* Filter: 'wpseo_json_ld_search_url' - Allows filtering of the search URL for Yoast SEO.
*
* @api string $search_url The search URL for this site with a `{search_term_string}` variable.
*/
$search_url = \apply_filters( 'wpseo_json_ld_search_url', $this->context->site_url . '?s={search_term_string}' );
$data['potentialAction'][] = [
'@type' => 'SearchAction',
'target' => [
'@type' => 'EntryPoint',
'urlTemplate' => $search_url,
],
'query-input' => 'required name=search_term_string',
];
return $data;
}
}