webpage.php
4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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();
}
}