• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 3b5e7bce9c5da12af4359302c0da5c793b07091e

04 Feb 2025 08:41AM UTC coverage: 54.606% (+0.01%) from 54.595%
3b5e7bce9c5da12af4359302c0da5c793b07091e

push

github

YoastBot
Bump version to 24.4 on free

7710 of 13750 branches covered (56.07%)

Branch coverage included in aggregate %.

30086 of 55466 relevant lines covered (54.24%)

41481.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/src/generators/schema/webpage.php
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() {
6✔
19
                if ( $this->context->indexable->object_type === 'unknown' ) {
6✔
20
                        return false;
×
21
                }
22
                return ! ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' );
6✔
23
        }
24

25
        /**
26
         * Returns WebPage schema data.
27
         *
28
         * @return array<string|array<string>> WebPage schema data.
29
         */
30
        public function generate() {
28✔
31
                $data = [
14✔
32
                        '@type'      => $this->context->schema_page_type,
28✔
33
                        '@id'        => $this->context->main_schema_id,
28✔
34
                        'url'        => $this->context->canonical,
28✔
35
                        'name'       => $this->helpers->schema->html->smart_strip_tags( $this->context->title ),
28✔
36
                        'isPartOf'   => [
14✔
37
                                '@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH,
28✔
38
                        ],
14✔
39
                ];
14✔
40

41
                if ( empty( $this->context->canonical ) && \is_search() ) {
28✔
42
                        $data['url'] = $this->build_search_url();
8✔
43
                }
44

45
                if ( $this->helpers->current_page->is_front_page() ) {
28✔
46
                        if ( $this->context->site_represents_reference ) {
4✔
47
                                $data['about'] = $this->context->site_represents_reference;
2✔
48
                        }
49
                }
50

51
                $data = $this->add_image( $data );
28✔
52

53
                if ( $this->context->indexable->object_type === 'post' ) {
28✔
54
                        $data['datePublished'] = $this->helpers->date->format( $this->context->post->post_date_gmt );
18✔
55

56
                        if ( \strtotime( $this->context->post->post_modified_gmt ) > \strtotime( $this->context->post->post_date_gmt ) ) {
18✔
57
                                $data['dateModified'] = $this->helpers->date->format( $this->context->post->post_modified_gmt );
18✔
58
                        }
59

60
                        if ( $this->context->indexable->object_sub_type === 'post' ) {
18✔
61
                                $data = $this->add_author( $data, $this->context->post );
4✔
62
                        }
63
                }
64

65
                if ( ! empty( $this->context->description ) ) {
28✔
66
                        $data['description'] = $this->helpers->schema->html->smart_strip_tags( $this->context->description );
2✔
67
                }
68

69
                if ( $this->add_breadcrumbs() ) {
28✔
70
                        $data['breadcrumb'] = [
28✔
71
                                '@id' => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH,
28✔
72
                        ];
14✔
73
                }
74

75
                if ( ! empty( $this->context->main_entity_of_page ) ) {
28✔
76
                        $data['mainEntity'] = $this->context->main_entity_of_page;
×
77
                }
78

79
                $data = $this->helpers->schema->language->add_piece_language( $data );
28✔
80
                $data = $this->add_potential_action( $data );
28✔
81

82
                return $data;
28✔
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 ) {
4✔
94
                if ( $this->context->site_represents === false ) {
4✔
95
                        $data['author'] = [ '@id' => $this->helpers->schema->id->get_user_schema_id( $post->post_author, $this->context ) ];
2✔
96
                }
97

98
                return $data;
4✔
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 ) {
6✔
109
                if ( $this->context->has_image ) {
6✔
110
                        $data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
111
                        $data['image']              = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
112
                        $data['thumbnailUrl']       = $this->context->main_image_url;
2✔
113
                }
114
                return $data;
6✔
115
        }
116

117
        /**
118
         * Determine if we should add a breadcrumb attribute.
119
         *
120
         * @return bool
121
         */
122
        private function add_breadcrumbs() {
20✔
123
                if ( $this->context->indexable->object_type === 'system-page' && $this->context->indexable->object_sub_type === '404' ) {
20✔
124
                        return false;
×
125
                }
126

127
                return true;
20✔
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 ) {
20✔
138
                $url = $this->context->canonical;
20✔
139
                if ( $data['@type'] === 'CollectionPage' || ( \is_array( $data['@type'] ) && \in_array( 'CollectionPage', $data['@type'], true ) ) ) {
20✔
140
                        return $data;
4✔
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 ] );
16✔
149

150
                $data['potentialAction'][] = [
16✔
151
                        '@type'  => 'ReadAction',
16✔
152
                        'target' => $targets,
16✔
153
                ];
8✔
154

155
                return $data;
16✔
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
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc