| @@ -13,11 +13,8 @@ | ||
| 13 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | 14 | * License for the specific language governing permissions and limitations |
| 15 | 15 | * under the License. |
| 16 | 16 | */ |
| 17 | - if ( ! defined( 'ABSPATH' ) ) { | |
| 18 | - exit; | |
| 19 | - } | |
| 20 | 17 | |
| 21 | 18 | require_once dirname( __FILE__ ) . '/FreemiusBase.php'; |
| 22 | 19 | |
| 23 | 20 | if ( ! defined( 'FS_SDK__USER_AGENT' ) ) { |
| @@ -61,12 +58,8 @@ | ||
| 61 | 58 | define( 'FS_SDK__HAS_CURL', $has_curl ); |
| 62 | 59 | } |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | - if ( ! defined( 'FS_SDK__SSLVERIFY' ) ) { | |
| 66 | - define( 'FS_SDK__SSLVERIFY', false ); | |
| 67 | - } | |
| 68 | - | |
| 69 | 62 | $curl_version = FS_SDK__HAS_CURL ? |
| 70 | 63 | curl_version() : |
| 71 | 64 | array( 'version' => '7.37' ); |
| 72 | 65 | |
| @@ -84,9 +77,12 @@ | ||
| 84 | 77 | if ( ! defined( 'FS_API__SANDBOX_ADDRESS' ) ) { |
| 85 | 78 | define( 'FS_API__SANDBOX_ADDRESS', '://sandbox-api.freemius.com' ); |
| 86 | 79 | } |
| 87 | 80 | |
| 88 | - if ( ! class_exists( 'Freemius_Api_WordPress' ) ) { | |
| 81 | + if ( class_exists( 'Freemius_Api_WordPress' ) ) { | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + | |
| 89 | 85 | class Freemius_Api_WordPress extends Freemius_Api_Base { |
| 90 | 86 | private static $_logger = array(); |
| 91 | 87 | |
| 92 | 88 | /** |
| @@ -163,17 +159,8 @@ | ||
| 163 | 159 | public static function SetHttp() { |
| 164 | 160 | self::$_protocol = 'http'; |
| 165 | 161 | } |
| 166 | 162 | |
| 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 | - | |
| 176 | 163 | /** |
| 177 | 164 | * @since 1.0.4 |
| 178 | 165 | * |
| 179 | 166 | * @return bool |
| @@ -240,16 +227,13 @@ | ||
| 240 | 227 | $content_type = ''; |
| 241 | 228 | $now = ( time() - self::$_clock_diff ); |
| 242 | 229 | $date = date( 'r', $now ); |
| 243 | 230 | |
| 244 | - if ( in_array( $pMethod, array( 'POST', 'PUT' ) ) ) { | |
| 245 | - $content_type = 'application/json'; | |
| 231 | + if ( in_array( $pMethod, array( 'POST', 'PUT' ) ) && ! empty( $pPostParams ) ) { | |
| 232 | + $content_md5 = md5( $pPostParams ); | |
| 233 | + $content_type = 'application/json'; | |
| 234 | + } | |
| 246 | 235 | |
| 247 | - if ( ! empty( $pPostParams ) ) { | |
| 248 | - $content_md5 = md5( $pPostParams ); | |
| 249 | - } | |
| 250 | - } | |
| 251 | - | |
| 252 | 236 | $string_to_sign = implode( $eol, array( |
| 253 | 237 | $pMethod, |
| 254 | 238 | $content_md5, |
| 255 | 239 | $content_type, |
| @@ -310,13 +294,11 @@ | ||
| 310 | 294 | * |
| 311 | 295 | * @return mixed |
| 312 | 296 | */ |
| 313 | 297 | private static function ExecuteRequest( $pUrl, &$pWPRemoteArgs ) { |
| 314 | - $bt = debug_backtrace(); | |
| 315 | - | |
| 316 | 298 | $start = microtime( true ); |
| 317 | 299 | |
| 318 | - $response = self::RemoteRequest( $pUrl, $pWPRemoteArgs ); | |
| 300 | + $response = wp_remote_request( $pUrl, $pWPRemoteArgs ); | |
| 319 | 301 | |
| 320 | 302 | if ( FS_API__LOGGER_ON ) { |
| 321 | 303 | $end = microtime( true ); |
| 322 | 304 | |
| @@ -334,9 +316,9 @@ | ||
| 334 | 316 | 'result' => ! $is_http_error ? |
| 335 | 317 | $response['body'] : |
| 336 | 318 | json_encode( $response->get_error_messages() ), |
| 337 | 319 | 'code' => ! $is_http_error ? $response['response']['code'] : null, |
| 338 | - 'backtrace' => $bt, | |
| 320 | + 'backtrace' => debug_backtrace(), | |
| 339 | 321 | ); |
| 340 | 322 | } |
| 341 | 323 | |
| 342 | 324 | return $response; |
| @@ -341,33 +323,8 @@ | ||
| 341 | 323 | |
| 342 | 324 | return $response; |
| 343 | 325 | } |
| 344 | 326 | |
| 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 | - | |
| 370 | 327 | /** |
| 371 | 328 | * @return array |
| 372 | 329 | */ |
| 373 | 330 | static function GetLogger() { |
| @@ -407,9 +364,9 @@ | ||
| 407 | 364 | |
| 408 | 365 | $pWPRemoteArgs = array( |
| 409 | 366 | 'method' => strtoupper( $pMethod ), |
| 410 | 367 | 'connect_timeout' => 10, |
| 411 | - 'timeout' => WP_FS__API_TIMEOUT, | |
| 368 | + 'timeout' => 60, | |
| 412 | 369 | 'follow_redirects' => true, |
| 413 | 370 | 'redirection' => 5, |
| 414 | 371 | 'user-agent' => $user_agent, |
| 415 | 372 | 'blocking' => true, |
| @@ -422,13 +379,12 @@ | ||
| 422 | 379 | $pWPRemoteArgs['headers'] = array(); |
| 423 | 380 | } |
| 424 | 381 | |
| 425 | 382 | if ( in_array( $pMethod, array( 'POST', 'PUT' ) ) ) { |
| 426 | - $pWPRemoteArgs['headers']['Content-type'] = 'application/json'; | |
| 427 | - | |
| 428 | - if ( is_array( $pParams ) && 0 < count( $pParams ) ) { | |
| 429 | - $pWPRemoteArgs['body'] = json_encode( $pParams ); | |
| 430 | - } | |
| 383 | + if ( is_array( $pParams ) && 0 < count( $pParams ) ) { | |
| 384 | + $pWPRemoteArgs['headers']['Content-type'] = 'application/json'; | |
| 385 | + $pWPRemoteArgs['body'] = json_encode( $pParams ); | |
| 386 | + } | |
| 431 | 387 | } |
| 432 | 388 | |
| 433 | 389 | $request_url = self::GetUrl( $pCanonizedPath, $pIsSandbox ); |
| 434 | 390 | |
| @@ -440,9 +396,9 @@ | ||
| 440 | 396 | $pWPRemoteArgs['headers']['Expect'] = ''; |
| 441 | 397 | } |
| 442 | 398 | |
| 443 | 399 | if ( 'https' === substr( strtolower( $request_url ), 0, 5 ) ) { |
| 444 | - $pWPRemoteArgs['sslverify'] = FS_SDK__SSLVERIFY; | |
| 400 | + $pWPRemoteArgs['sslverify'] = false; | |
| 445 | 401 | } |
| 446 | 402 | |
| 447 | 403 | if ( false !== $pBeforeExecutionFunction && |
| 448 | 404 | is_callable( $pBeforeExecutionFunction ) |
| @@ -472,14 +428,10 @@ | ||
| 472 | 428 | * @author Vova Feldman (@svovaf) |
| 473 | 429 | */ |
| 474 | 430 | if ( filter_var( $matches[1], FILTER_VALIDATE_IP ) ) { |
| 475 | 431 | if ( strlen( inet_pton( $matches[1] ) ) === 16 ) { |
| 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 | - */ | |
| 432 | +// error_log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.'); | |
| 433 | + // Hook to an action triggered just before cURL is executed to resolve the IP version to v4. | |
| 482 | 434 | add_action( 'http_api_curl', 'Freemius_Api_WordPress::CurlResolveToIPv4', 10, 1 ); |
| 483 | 435 | |
| 484 | 436 | // Re-run request. |
| 485 | 437 | $result = self::ExecuteRequest( $request_url, $pWPRemoteArgs ); |
| @@ -580,23 +532,31 @@ | ||
| 580 | 532 | #---------------------------------------------------------------------------------- |
| 581 | 533 | #region Connectivity Test |
| 582 | 534 | #---------------------------------------------------------------------------------- |
| 583 | 535 | |
| 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 | - } | |
| 536 | + /** | |
| 537 | + * If successful connectivity to the API endpoint using ping.json endpoint. | |
| 538 | + * | |
| 539 | + * - OR - | |
| 540 | + * | |
| 541 | + * Validate if ping result object is valid. | |
| 542 | + * | |
| 543 | + * @param mixed $pPong | |
| 544 | + * | |
| 545 | + * @return bool | |
| 546 | + */ | |
| 547 | + public static function Test( $pPong = null ) { | |
| 548 | + $pong = is_null( $pPong ) ? | |
| 549 | + self::Ping() : | |
| 550 | + $pPong; | |
| 598 | 551 | |
| 552 | + return ( | |
| 553 | + is_object( $pong ) && | |
| 554 | + isset( $pong->api ) && | |
| 555 | + 'pong' === $pong->api | |
| 556 | + ); | |
| 557 | + } | |
| 558 | + | |
| 599 | 559 | /** |
| 600 | 560 | * Ping API to test connectivity. |
| 601 | 561 | * |
| 602 | 562 | * @return object |
| @@ -609,9 +569,9 @@ | ||
| 609 | 569 | $result = (object) $e->getResult(); |
| 610 | 570 | } catch ( Exception $e ) { |
| 611 | 571 | // Map to error object. |
| 612 | 572 | $result = (object) array( |
| 613 | - 'error' => (object) array( | |
| 573 | + 'error' => array( | |
| 614 | 574 | 'type' => 'Unknown', |
| 615 | 575 | 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')', |
| 616 | 576 | 'code' => 'unknown', |
| 617 | 577 | 'http' => 402 |
| @@ -688,9 +648,9 @@ | ||
| 688 | 648 | $code = ( 0 < count( $parts ) ) ? $parts[0] : 'http_request_failed'; |
| 689 | 649 | $message = ( 1 < count( $parts ) ) ? $parts[1] : $message; |
| 690 | 650 | |
| 691 | 651 | $e = new Freemius_Exception( array( |
| 692 | - 'error' => (object) array( | |
| 652 | + 'error' => array( | |
| 693 | 653 | 'code' => $code, |
| 694 | 654 | 'message' => $message, |
| 695 | 655 | 'type' => 'CurlException', |
| 696 | 656 | ), |
| @@ -696,9 +656,9 @@ | ||
| 696 | 656 | ), |
| 697 | 657 | ) ); |
| 698 | 658 | } else { |
| 699 | 659 | $e = new Freemius_Exception( array( |
| 700 | - 'error' => (object) array( | |
| 660 | + 'error' => array( | |
| 701 | 661 | 'code' => $pError->get_error_code(), |
| 702 | 662 | 'message' => $pError->get_error_message(), |
| 703 | 663 | 'type' => 'WPRemoteException', |
| 704 | 664 | ), |
| @@ -740,6 +700,5 @@ | ||
| 740 | 700 | ) ); |
| 741 | 701 | } |
| 742 | 702 | |
| 743 | 703 | #endregion |
| 744 | - } | |
| 745 | - } | |
| 704 | + } | |