| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Copyright 2016 Freemius, Inc. |
| 4 | 4 | * |
| @@ -163,8 +163,17 @@ | ||
| 163 | 163 | public static function SetHttp() { |
| 164 | 164 | self::$_protocol = 'http'; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | + /** | |
| 168 | + * Sets API connection protocol to HTTPS. | |
| 169 | + * | |
| 170 | + * @since 2.5.4 | |
| 171 | + */ | |
| 172 | + public static function SetHttps() { | |
| 173 | + self::$_protocol = 'https'; | |
| 174 | + } | |
| 175 | + | |
| 167 | 176 | /** |
| 168 | 177 | * @since 1.0.4 |
| 169 | 178 | * |
| 170 | 179 | * @return bool |
| @@ -305,9 +314,9 @@ | ||
| 305 | 314 | $bt = debug_backtrace(); |
| 306 | 315 | |
| 307 | 316 | $start = microtime( true ); |
| 308 | 317 | |
| 309 | - $response = wp_remote_request( $pUrl, $pWPRemoteArgs ); | |
| 318 | + $response = self::RemoteRequest( $pUrl, $pWPRemoteArgs ); | |
| 310 | 319 | |
| 311 | 320 | if ( FS_API__LOGGER_ON ) { |
| 312 | 321 | $end = microtime( true ); |
| 313 | 322 | |
| @@ -332,8 +341,33 @@ | ||
| 332 | 341 | |
| 333 | 342 | return $response; |
| 334 | 343 | } |
| 335 | 344 | |
| 345 | + /** | |
| 346 | + * @author Leo Fajardo (@leorw) | |
| 347 | + * | |
| 348 | + * @param string $pUrl | |
| 349 | + * @param array $pWPRemoteArgs | |
| 350 | + * | |
| 351 | + * @return array|WP_Error The response array or a WP_Error on failure. | |
| 352 | + */ | |
| 353 | + static function RemoteRequest( $pUrl, $pWPRemoteArgs ) { | |
| 354 | + $response = wp_remote_request( $pUrl, $pWPRemoteArgs ); | |
| 355 | + | |
| 356 | + if ( | |
| 357 | + is_array( $response ) && | |
| 358 | + ( | |
| 359 | + empty( $response['headers'] ) || | |
| 360 | + empty( $response['headers']['x-api-server'] ) | |
| 361 | + ) | |
| 362 | + ) { | |
| 363 | + // API is considered blocked if the response doesn't include the `x-api-server` header. When there's no error but this header doesn't exist, the response is usually not in the expected form (e.g., cannot be JSON-decoded). | |
| 364 | + $response = new WP_Error( 'api_blocked', htmlentities( $response['body'] ) ); | |
| 365 | + } | |
| 366 | + | |
| 367 | + return $response; | |
| 368 | + } | |
| 369 | + | |
| 336 | 370 | /** |
| 337 | 371 | * @return array |
| 338 | 372 | */ |
| 339 | 373 | static function GetLogger() { |
| @@ -373,9 +407,9 @@ | ||
| 373 | 407 | |
| 374 | 408 | $pWPRemoteArgs = array( |
| 375 | 409 | 'method' => strtoupper( $pMethod ), |
| 376 | 410 | 'connect_timeout' => 10, |
| 377 | - 'timeout' => 60, | |
| 411 | + 'timeout' => WP_FS__API_TIMEOUT, | |
| 378 | 412 | 'follow_redirects' => true, |
| 379 | 413 | 'redirection' => 5, |
| 380 | 414 | 'user-agent' => $user_agent, |
| 381 | 415 | 'blocking' => true, |
| @@ -438,10 +472,14 @@ | ||
| 438 | 472 | * @author Vova Feldman (@svovaf) |
| 439 | 473 | */ |
| 440 | 474 | if ( filter_var( $matches[1], FILTER_VALIDATE_IP ) ) { |
| 441 | 475 | if ( strlen( inet_pton( $matches[1] ) ) === 16 ) { |
| 442 | -// error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.'); | |
| 443 | - // Hook to an action triggered just before cURL is executed to resolve the IP version to v4. | |
| 476 | + /** | |
| 477 | + * error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.'); | |
| 478 | + * Hook to an action triggered just before cURL is executed to resolve the IP version to v4. | |
| 479 | + * | |
| 480 | + * @phpstan-ignore-next-line | |
| 481 | + */ | |
| 444 | 482 | add_action( 'http_api_curl', 'Freemius_Api_WordPress::CurlResolveToIPv4', 10, 1 ); |
| 445 | 483 | |
| 446 | 484 | // Re-run request. |
| 447 | 485 | $result = self::ExecuteRequest( $request_url, $pWPRemoteArgs ); |
| @@ -542,31 +580,23 @@ | ||
| 542 | 580 | #---------------------------------------------------------------------------------- |
| 543 | 581 | #region Connectivity Test |
| 544 | 582 | #---------------------------------------------------------------------------------- |
| 545 | 583 | |
| 546 | - /** | |
| 547 | - * If successful connectivity to the API endpoint using ping.json endpoint. | |
| 548 | - * | |
| 549 | - * - OR - | |
| 550 | - * | |
| 551 | - * Validate if ping result object is valid. | |
| 552 | - * | |
| 553 | - * @param mixed $pPong | |
| 554 | - * | |
| 555 | - * @return bool | |
| 556 | - */ | |
| 557 | - public static function Test( $pPong = null ) { | |
| 558 | - $pong = is_null( $pPong ) ? | |
| 559 | - self::Ping() : | |
| 560 | - $pPong; | |
| 584 | + /** | |
| 585 | + * This method exists only for backward compatibility to prevent a fatal error from happening when called from an outdated piece of code. | |
| 586 | + * | |
| 587 | + * @param mixed $pPong | |
| 588 | + * | |
| 589 | + * @return bool | |
| 590 | + */ | |
| 591 | + public static function Test( $pPong = null ) { | |
| 592 | + return ( | |
| 593 | + is_object( $pPong ) && | |
| 594 | + isset( $pPong->api ) && | |
| 595 | + 'pong' === $pPong->api | |
| 596 | + ); | |
| 597 | + } | |
| 561 | 598 | |
| 562 | - return ( | |
| 563 | - is_object( $pong ) && | |
| 564 | - isset( $pong->api ) && | |
| 565 | - 'pong' === $pong->api | |
| 566 | - ); | |
| 567 | - } | |
| 568 | - | |
| 569 | 599 | /** |
| 570 | 600 | * Ping API to test connectivity. |
| 571 | 601 | * |
| 572 | 602 | * @return object |
| @@ -711,5 +741,5 @@ | ||
| 711 | 741 | } |
| 712 | 742 | |
| 713 | 743 | #endregion |
| 714 | 744 | } |
| 715 | - } | |
| 745 | + } | |