| @@ -65,13 +65,12 @@ | ||
| 65 | 65 | */ |
| 66 | 66 | public function fire() { |
| 67 | 67 | try { |
| 68 | 68 | $response = $this->do_request( $this->url, $this->args ); |
| 69 | - $this->response = $this->decode_response( $response ); | |
| 70 | - | |
| 69 | + $response = $this->decode_response( $response ); | |
| 70 | + $this->response = $this->validate_response( $response ); | |
| 71 | 71 | return true; |
| 72 | - } | |
| 73 | - catch ( WPSEO_MyYoast_Bad_Request_Exception $bad_request_exception ) { | |
| 72 | + } catch ( WPSEO_MyYoast_Bad_Request_Exception $bad_request_exception ) { | |
| 74 | 73 | $this->error_message = $bad_request_exception->getMessage(); |
| 75 | 74 | |
| 76 | 75 | return false; |
| 77 | 76 | } |
| @@ -117,9 +116,9 @@ | ||
| 117 | 116 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 118 | 117 | $response_message = wp_remote_retrieve_response_message( $response ); |
| 119 | 118 | |
| 120 | 119 | // Do nothing, response code is okay. |
| 121 | - if ( $response_code === 200 || strpos( $response_code, '200' ) !== false ) { | |
| 120 | + if ( $response_code === 200 ) { | |
| 122 | 121 | return wp_remote_retrieve_body( $response ); |
| 123 | 122 | } |
| 124 | 123 | |
| 125 | 124 | throw new WPSEO_MyYoast_Bad_Request_Exception( esc_html( $response_message ), (int) $response_code ); |
| @@ -137,13 +136,31 @@ | ||
| 137 | 136 | $response = json_decode( $response ); |
| 138 | 137 | |
| 139 | 138 | if ( ! is_object( $response ) ) { |
| 140 | 139 | throw new WPSEO_MyYoast_Invalid_JSON_Exception( |
| 141 | - esc_html__( 'No JSON object was returned.', 'wordpress-seo' ) | |
| 140 | + esc_html__( 'No JSON object was returned.', 'wordpress-seo' ), | |
| 142 | 141 | ); |
| 143 | 142 | } |
| 144 | 143 | |
| 145 | 144 | return $response; |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Validates that all the needed fields are in de decoded response. | |
| 149 | + * | |
| 150 | + * @param stdClass $response The response to validate. | |
| 151 | + * | |
| 152 | + * @return stdClass The json decoded response. | |
| 153 | + * @throws WPSEO_MyYoast_Invalid_JSON_Exception When not all needed fields are found. | |
| 154 | + */ | |
| 155 | + private function validate_response( $response ) { | |
| 156 | + if ( isset( $response->url, $response->subscriptions ) && is_array( $response->subscriptions ) ) { | |
| 157 | + return $response; | |
| 158 | + } | |
| 159 | + | |
| 160 | + throw new WPSEO_MyYoast_Invalid_JSON_Exception( | |
| 161 | + esc_html__( 'Not all needed fields are present.', 'wordpress-seo' ), | |
| 162 | + ); | |
| 146 | 163 | } |
| 147 | 164 | |
| 148 | 165 | /** |
| 149 | 166 | * Checks if MyYoast tokens are allowed and adds the token to the request body. |