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

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 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 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✔
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   $data The WebPage schema.
86
         * @param WP_Post $post The post the context is representing.
87
         *
88
         * @return array 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 $data WebPage schema data.
102
         */
103
        public function add_image( $data ) {
6✔
104
                if ( $this->context->has_image ) {
6✔
105
                        $data['primaryImageOfPage'] = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
106
                        $data['image']              = [ '@id' => $this->context->canonical . Schema_IDs::PRIMARY_IMAGE_HASH ];
2✔
107
                        $data['thumbnailUrl']       = $this->context->main_image_url;
2✔
108
                }
109
                return $data;
6✔
110
        }
111

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

122
                return true;
20✔
123
        }
124

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

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

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

150
                return $data;
16✔
151
        }
152

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