PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
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 / webpage.php

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

167 lines 4.9 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 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