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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

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

90.32
/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() {
20✔
31
                $data = [
10✔
32
                        '@type'      => $this->context->schema_page_type,
20✔
33
                        '@id'        => $this->context->main_schema_id,
20✔
34
                        'url'        => $this->context->canonical,
20✔
35
                        'name'       => $this->helpers->schema->html->smart_strip_tags( $this->context->title ),
20✔
36
                        'isPartOf'   => [
10✔
37
                                '@id' => $this->context->site_url . Schema_IDs::WEBSITE_HASH,
20✔
38
                        ],
10✔
39
                ];
10✔
40

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

45
                if ( $this->helpers->current_page->is_front_page() ) {
20✔
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 );
20✔
52

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

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

62
                if ( ! empty( $this->context->description ) ) {
20✔
63
                        $data['description'] = $this->helpers->schema->html->smart_strip_tags( $this->context->description );
2✔
64
                }
65

66
                if ( $this->add_breadcrumbs() ) {
20✔
67
                        $data['breadcrumb'] = [
20✔
68
                                '@id' => $this->context->canonical . Schema_IDs::BREADCRUMB_HASH,
20✔
69
                        ];
10✔
70
                }
71

72
                if ( ! empty( $this->context->main_entity_of_page ) ) {
20✔
73
                        $data['mainEntity'] = $this->context->main_entity_of_page;
×
74
                }
75

76
                $data = $this->helpers->schema->language->add_piece_language( $data );
20✔
77
                $data = $this->add_potential_action( $data );
20✔
78

79
                return $data;
20✔
80
        }
81

82
        /**
83
         * Adds an author property to the $data if the WebPage is not represented.
84
         *
85
         * @param array<string|array<string>> $data The WebPage schema.
86
         * @param WP_Post                     $post The post the context is representing.
87
         *
88
         * @return array<string|array<string>> The WebPage schema.
89
         */
90
        public function add_author( $data, $post ) {
4✔
91
                if ( $this->context->site_represents === false ) {
4✔
92
                        $data['author'] = [ '@id' => $this->helpers->schema->id->get_user_schema_id( $post->post_author, $this->context ) ];
2✔
93
                }
94

95
                return $data;
4✔
96
        }
97

98
        /**
99
         * If we have an image, make it the primary image of the page.
100
         *
101
         * @param array<string|array<string>> $data WebPage schema data.
102
         *
103
         * @return array<string|array<string>>
104
         */
105
        public function add_image( $data ) {
6✔
106
                if ( $this->context->has_image ) {
6✔
107
                        $data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
108
                        $data['image']              = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
109
                        $data['thumbnailUrl']       = $this->context->main_image_url;
2✔
110
                }
111
                return $data;
6✔
112
        }
113

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

124
                return true;
20✔
125
        }
126

127
        /**
128
         * Adds the potential action property to the WebPage Schema piece.
129
         *
130
         * @param array<string|array<string>> $data The WebPage data.
131
         *
132
         * @return array<string|array<string>> The WebPage data with the potential action added.
133
         */
134
        private function add_potential_action( $data ) {
20✔
135
                $url = $this->context->canonical;
20✔
136
                if ( $data['@type'] === 'CollectionPage' || ( \is_array( $data['@type'] ) && \in_array( 'CollectionPage', $data['@type'], true ) ) ) {
20✔
137
                        return $data;
4✔
138
                }
139

140
                /**
141
                 * Filter: 'wpseo_schema_webpage_potential_action_target' - Allows filtering of the schema WebPage potentialAction target.
142
                 *
143
                 * @param array<string> $targets The URLs for the WebPage potentialAction target.
144
                 */
145
                $targets = \apply_filters( 'wpseo_schema_webpage_potential_action_target', [ $url ] );
16✔
146

147
                $data['potentialAction'][] = [
16✔
148
                        '@type'  => 'ReadAction',
16✔
149
                        'target' => $targets,
16✔
150
                ];
8✔
151

152
                return $data;
16✔
153
        }
154

155
        /**
156
         * Creates the search URL for use when if there is no canonical.
157
         *
158
         * @return string Search URL.
159
         */
160
        private function build_search_url() {
×
161
                return $this->context->site_url . '?s=' . \rawurlencode( \get_search_query() );
×
162
        }
163
}
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

© 2025 Coveralls, Inc