• 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

0.0
/inc/class-my-yoast-api-request.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Inc
6
 */
7

8
/**
9
 * Handles requests to MyYoast.
10
 */
11
class WPSEO_MyYoast_Api_Request {
12

13
        /**
14
         * The Request URL.
15
         *
16
         * @var string
17
         */
18
        protected $url;
19

20
        /**
21
         * The request parameters.
22
         *
23
         * @var array
24
         */
25
        protected $args = [
26
                'method'    => 'GET',
27
                'timeout'   => 5,
28
                'headers'   => [
29
                        'Accept-Encoding' => '*',
30
                        'Expect'          => '',
31
                ],
32
        ];
33

34
        /**
35
         * Contains the fetched response.
36
         *
37
         * @var stdClass
38
         */
39
        protected $response;
40

41
        /**
42
         * Contains the error message when request went wrong.
43
         *
44
         * @var string
45
         */
46
        protected $error_message = '';
47

48
        /**
49
         * Constructor.
50
         *
51
         * @codeCoverageIgnore
52
         *
53
         * @param string $url  The request url.
54
         * @param array  $args The request arguments.
55
         */
56
        public function __construct( $url, array $args = [] ) {
57
                $this->url  = 'https://my.yoast.com/api/' . $url;
58
                $this->args = wp_parse_args( $args, $this->args );
59
        }
60

61
        /**
62
         * Fires the request.
63
         *
64
         * @return bool True when request is successful.
65
         */
UNCOV
66
        public function fire() {
×
67
                try {
UNCOV
68
                        $response       = $this->do_request( $this->url, $this->args );
×
UNCOV
69
                        $this->response = $this->decode_response( $response );
×
70

UNCOV
71
                        return true;
×
72
                }
UNCOV
73
                catch ( WPSEO_MyYoast_Bad_Request_Exception $bad_request_exception ) {
×
UNCOV
74
                        $this->error_message = $bad_request_exception->getMessage();
×
75

UNCOV
76
                        return false;
×
77
                }
78
        }
79

80
        /**
81
         * Retrieves the error message.
82
         *
83
         * @return string The set error message.
84
         */
UNCOV
85
        public function get_error_message() {
×
UNCOV
86
                return $this->error_message;
×
87
        }
88

89
        /**
90
         * Retrieves the response.
91
         *
92
         * @return stdClass The response object.
93
         */
UNCOV
94
        public function get_response() {
×
UNCOV
95
                return $this->response;
×
96
        }
97

98
        /**
99
         * Performs the request using WordPress internals.
100
         *
101
         * @codeCoverageIgnore
102
         *
103
         * @param string $url               The request URL.
104
         * @param array  $request_arguments The request arguments.
105
         *
106
         * @return string                                 The retrieved body.
107
         * @throws WPSEO_MyYoast_Bad_Request_Exception    When request is invalid.
108
         */
109
        protected function do_request( $url, $request_arguments ) {
110
                $request_arguments = $this->enrich_request_arguments( $request_arguments );
111
                $response          = wp_remote_request( $url, $request_arguments );
112

113
                if ( is_wp_error( $response ) ) {
114
                        throw new WPSEO_MyYoast_Bad_Request_Exception( $response->get_error_message() );
115
                }
116

117
                $response_code    = wp_remote_retrieve_response_code( $response );
118
                $response_message = wp_remote_retrieve_response_message( $response );
119

120
                // Do nothing, response code is okay.
121
                if ( $response_code === 200 || strpos( $response_code, '200' ) !== false ) {
122
                        return wp_remote_retrieve_body( $response );
123
                }
124

125
                throw new WPSEO_MyYoast_Bad_Request_Exception( esc_html( $response_message ), (int) $response_code );
126
        }
127

128
        /**
129
         * Decodes the JSON encoded response.
130
         *
131
         * @param string $response The response to decode.
132
         *
133
         * @return stdClass                             The json decoded response.
134
         * @throws WPSEO_MyYoast_Invalid_JSON_Exception When decoded string is not a JSON object.
135
         */
UNCOV
136
        protected function decode_response( $response ) {
×
UNCOV
137
                $response = json_decode( $response );
×
138

UNCOV
139
                if ( ! is_object( $response ) ) {
×
UNCOV
140
                        throw new WPSEO_MyYoast_Invalid_JSON_Exception(
×
UNCOV
141
                                esc_html__( 'No JSON object was returned.', 'wordpress-seo' )
×
142
                        );
143
                }
144

UNCOV
145
                return $response;
×
146
        }
147

148
        /**
149
         * Checks if MyYoast tokens are allowed and adds the token to the request body.
150
         *
151
         * When tokens are disallowed it will add the url to the request body.
152
         *
153
         * @param array $request_arguments The arguments to enrich.
154
         *
155
         * @return array The enriched arguments.
156
         */
UNCOV
157
        protected function enrich_request_arguments( array $request_arguments ) {
×
UNCOV
158
                $request_arguments     = wp_parse_args( $request_arguments, [ 'headers' => [] ] );
×
UNCOV
159
                $addon_version_headers = $this->get_installed_addon_versions();
×
160

UNCOV
161
                foreach ( $addon_version_headers as $addon => $version ) {
×
UNCOV
162
                        $request_arguments['headers'][ $addon . '-version' ] = $version;
×
163
                }
164

UNCOV
165
                $request_body = $this->get_request_body();
×
UNCOV
166
                if ( $request_body !== [] ) {
×
UNCOV
167
                        $request_arguments['body'] = $request_body;
×
168
                }
169

UNCOV
170
                return $request_arguments;
×
171
        }
172

173
        /**
174
         * Retrieves the request body based on URL or access token support.
175
         *
176
         * @codeCoverageIgnore
177
         *
178
         * @return array The request body.
179
         */
180
        public function get_request_body() {
181
                return [ 'url' => WPSEO_Utils::get_home_url() ];
182
        }
183

184
        /**
185
         * Wraps the get current user id function.
186
         *
187
         * @codeCoverageIgnore
188
         *
189
         * @return int The user id.
190
         */
191
        protected function get_current_user_id() {
192
                return get_current_user_id();
193
        }
194

195
        /**
196
         * Retrieves the installed addons as http headers.
197
         *
198
         * @codeCoverageIgnore
199
         *
200
         * @return array The installed addon versions.
201
         */
202
        protected function get_installed_addon_versions() {
203
                $addon_manager = new WPSEO_Addon_Manager();
204

205
                return $addon_manager->get_installed_addons_versions();
206
        }
207
}
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