PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 19.0, at src/generators/schema/webpage.php

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