| @@ -84,12 +84,9 @@ | ||
| 84 | 84 | if ( ! defined( 'FS_API__SANDBOX_ADDRESS' ) ) { |
| 85 | 85 | define( 'FS_API__SANDBOX_ADDRESS', '://sandbox-api.freemius.com' ); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - if ( class_exists( 'Freemius_Api_WordPress' ) ) { | |
| 89 | - return; | |
| 90 | - } | |
| 91 | - | |
| 88 | + if ( ! class_exists( 'Freemius_Api_WordPress' ) ) { | |
| 92 | 89 | class Freemius_Api_WordPress extends Freemius_Api_Base { |
| 93 | 90 | private static $_logger = array(); |
| 94 | 91 | |
| 95 | 92 | /** |
| @@ -166,8 +163,17 @@ | ||
| 166 | 163 | public static function SetHttp() { |
| 167 | 164 | self::$_protocol = 'http'; |
| 168 | 165 | } |
| 169 | 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 | + | |
| 170 | 176 | /** |
| 171 | 177 | * @since 1.0.4 |
| 172 | 178 | * |
| 173 | 179 | * @return bool |
| @@ -304,11 +310,13 @@ | ||
| 304 | 310 | * |
| 305 | 311 | * @return mixed |
| 306 | 312 | */ |
| 307 | 313 | private static function ExecuteRequest( $pUrl, &$pWPRemoteArgs ) { |
| 314 | + $bt = debug_backtrace(); | |
| 315 | + | |
| 308 | 316 | $start = microtime( true ); |
| 309 | 317 | |
| 310 | - $response = wp_remote_request( $pUrl, $pWPRemoteArgs ); | |
| 318 | + $response = self::RemoteRequest( $pUrl, $pWPRemoteArgs ); | |
| 311 | 319 | |
| 312 | 320 | if ( FS_API__LOGGER_ON ) { |
| 313 | 321 | $end = microtime( true ); |
| 314 | 322 | |
| @@ -326,9 +334,9 @@ | ||
| 326 | 334 | 'result' => ! $is_http_error ? |
| 327 | 335 | $response['body'] : |
| 328 | 336 | json_encode( $response->get_error_messages() ), |
| 329 | 337 | 'code' => ! $is_http_error ? $response['response']['code'] : null, |
| 330 | - 'backtrace' => debug_backtrace(), | |
| 338 | + 'backtrace' => $bt, | |
| 331 | 339 | ); |
| 332 | 340 | } |
| 333 | 341 | |
| 334 | 342 | return $response; |
| @@ -333,8 +341,33 @@ | ||
| 333 | 341 | |
| 334 | 342 | return $response; |
| 335 | 343 | } |
| 336 | 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 | + | |
| 337 | 370 | /** |
| 338 | 371 | * @return array |
| 339 | 372 | */ |
| 340 | 373 | static function GetLogger() { |
| @@ -374,9 +407,9 @@ | ||
| 374 | 407 | |
| 375 | 408 | $pWPRemoteArgs = array( |
| 376 | 409 | 'method' => strtoupper( $pMethod ), |
| 377 | 410 | 'connect_timeout' => 10, |
| 378 | - 'timeout' => 60, | |
| 411 | + 'timeout' => WP_FS__API_TIMEOUT, | |
| 379 | 412 | 'follow_redirects' => true, |
| 380 | 413 | 'redirection' => 5, |
| 381 | 414 | 'user-agent' => $user_agent, |
| 382 | 415 | 'blocking' => true, |
| @@ -439,10 +472,14 @@ | ||
| 439 | 472 | * @author Vova Feldman (@svovaf) |
| 440 | 473 | */ |
| 441 | 474 | if ( filter_var( $matches[1], FILTER_VALIDATE_IP ) ) { |
| 442 | 475 | if ( strlen( inet_pton( $matches[1] ) ) === 16 ) { |
| 443 | -// error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.'); | |
| 444 | - // 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 | + */ | |
| 445 | 482 | add_action( 'http_api_curl', 'Freemius_Api_WordPress::CurlResolveToIPv4', 10, 1 ); |
| 446 | 483 | |
| 447 | 484 | // Re-run request. |
| 448 | 485 | $result = self::ExecuteRequest( $request_url, $pWPRemoteArgs ); |
| @@ -543,31 +580,23 @@ | ||
| 543 | 580 | #---------------------------------------------------------------------------------- |
| 544 | 581 | #region Connectivity Test |
| 545 | 582 | #---------------------------------------------------------------------------------- |
| 546 | 583 | |
| 547 | - /** | |
| 548 | - * If successful connectivity to the API endpoint using ping.json endpoint. | |
| 549 | - * | |
| 550 | - * - OR - | |
| 551 | - * | |
| 552 | - * Validate if ping result object is valid. | |
| 553 | - * | |
| 554 | - * @param mixed $pPong | |
| 555 | - * | |
| 556 | - * @return bool | |
| 557 | - */ | |
| 558 | - public static function Test( $pPong = null ) { | |
| 559 | - $pong = is_null( $pPong ) ? | |
| 560 | - self::Ping() : | |
| 561 | - $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 | + } | |
| 562 | 598 | |
| 563 | - return ( | |
| 564 | - is_object( $pong ) && | |
| 565 | - isset( $pong->api ) && | |
| 566 | - 'pong' === $pong->api | |
| 567 | - ); | |
| 568 | - } | |
| 569 | - | |
| 570 | 599 | /** |
| 571 | 600 | * Ping API to test connectivity. |
| 572 | 601 | * |
| 573 | 602 | * @return object |
| @@ -712,4 +741,5 @@ | ||
| 712 | 741 | } |
| 713 | 742 | |
| 714 | 743 | #endregion |
| 715 | 744 | } |
| 745 | + } | |