| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Generators\Schema; |
| 4 |
|
| 5 |
use WP_Post; |
| 6 |
use Yoast\WP\SEO\Config\Schema_IDs; |
| 7 |
|
| 8 |
/** |
| 9 |
* Returns schema WebPage data. |
| 10 |
*/ |
| 11 |
class WebPage 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 === 'unknown' ) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
return ! ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns WebPage schema data. |
| 27 |
* |
| 28 |
* @return array<string|array<string>> WebPage schema data. |
| 29 |
*/ |
| 30 |
public function generate() { |
| 31 |
$data = [ |
| 32 |
'@type' => $this->context->schema_page_type, |
| 33 |
'@id' => $this->context->main_schema_id, |
| 34 |
'url' => $this->context->canonical, |
| 35 |
'name' => $this->helpers->schema->html->smart_strip_tags( $this->context->title ), |
| 36 |
'isPartOf' => [ |
| 37 |
'@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH, |
| 38 |
], |
| 39 |
]; |
| 40 |
|
| 41 |
if ( empty( $this->context->canonical ) && \is_search() ) { |
| 42 |
$data['url'] = $this->build_search_url(); |
| 43 |
} |
| 44 |
|
| 45 |
if ( $this->helpers->current_page->is_front_page() ) { |
| 46 |
if ( $this->context->site_represents_reference ) { |
| 47 |
$data['about'] = $this->context->site_represents_reference; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$data = $this->add_image( $data ); |
| 52 |
|
| 53 |
if ( $this->context->indexable->object_type === 'post' ) { |
| 54 |
$data['datePublished'] = $this->helpers->date->format( $this->context->post->post_date_gmt ); |
| 55 |
|
| 56 |
if ( \strtotime( $this->context->post->post_modified_gmt ) > \strtotime( $this->context->post->post_date_gmt ) ) { |
| 57 |
$data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( $this->context->indexable->object_sub_type === 'post' ) { |
| 61 |
$data = $this->add_author( $data, $this->context->post ); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! empty( $this->context->description ) ) { |
| 66 |
$data['description'] = $this->helpers->schema->html->smart_strip_tags( $this->context->description ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->add_breadcrumbs() ) { |
| 70 |
$data['breadcrumb'] = [ |
| 71 |
'@id' => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! empty( $this->context->main_entity_of_page ) ) { |
| 76 |
$data['mainEntity'] = $this->context->main_entity_of_page; |
| 77 |
} |
| 78 |
|
| 79 |
$data = $this->helpers->schema->language->add_piece_language( $data ); |
| 80 |
$data = $this->add_potential_action( $data ); |
| 81 |
|
| 82 |
return $data; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Adds an author property to the $data if the WebPage is not represented. |
| 87 |
* |
| 88 |
* @param array<string|array<string>> $data The WebPage schema. |
| 89 |
* @param WP_Post $post The post the context is representing. |
| 90 |
* |
| 91 |
* @return array<string|array<string>> The WebPage schema. |
| 92 |
*/ |
| 93 |
public function add_author( $data, $post ) { |
| 94 |
if ( $this->context->site_represents === false ) { |
| 95 |
$data['author'] = [ '@id' => $this->helpers->schema->id->get_user_schema_id( $post->post_author, $this->context ) ]; |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* If we have an image, make it the primary image of the page. |
| 103 |
* |
| 104 |
* @param array<string|array<string>> $data WebPage schema data. |
| 105 |
* |
| 106 |
* @return array<string|array<string>> |
| 107 |
*/ |
| 108 |
public function add_image( $data ) { |
| 109 |
if ( $this->context->has_image ) { |
| 110 |
$data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ]; |
| 111 |
$data['image'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ]; |
| 112 |
$data['thumbnailUrl'] = $this->context->main_image_url; |
| 113 |
} |
| 114 |
return $data; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determine if we should add a breadcrumb attribute. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
private function add_breadcrumbs() { |
| 123 |
if ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Adds the potential action property to the WebPage Schema piece. |
| 132 |
* |
| 133 |
* @param array<string|array<string>> $data The WebPage data. |
| 134 |
* |
| 135 |
* @return array<string|array<string>> The WebPage data with the potential action added. |
| 136 |
*/ |
| 137 |
private function add_potential_action( $data ) { |
| 138 |
$url = $this->context->canonical; |
| 139 |
if ( $data['@type'] === 'CollectionPage' || ( \is_array( $data['@type'] ) && \in_array( 'CollectionPage', $data['@type'], true ) ) ) { |
| 140 |
return $data; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Filter: 'wpseo_schema_webpage_potential_action_target' - Allows filtering of the schema WebPage potentialAction target. |
| 145 |
* |
| 146 |
* @param array<string> $targets The URLs for the WebPage potentialAction target. |
| 147 |
*/ |
| 148 |
$targets = \apply_filters( 'wpseo_schema_webpage_potential_action_target', [ $url ] ); |
| 149 |
|
| 150 |
$data['potentialAction'][] = [ |
| 151 |
'@type' => 'ReadAction', |
| 152 |
'target' => $targets, |
| 153 |
]; |
| 154 |
|
| 155 |
return $data; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Creates the search URL for use when if there is no canonical. |
| 160 |
* |
| 161 |
* @return string Search URL. |
| 162 |
*/ |
| 163 |
private function build_search_url() { |
| 164 |
return $this->context->site_url . '?s=' . \rawurlencode( \get_search_query() ); |
| 165 |
} |
| 166 |
} |
| 167 |
|