| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 2 | +use Imagify\Traits\InstanceGetterTrait; | |
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | 5 | * Imagify.io API for WordPress. |
| 6 | 6 | */ |
| 7 | 7 | class Imagify { |
| 8 | - use \Imagify\Traits\InstanceGetterTrait; | |
| 8 | + use InstanceGetterTrait; | |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * The Imagify API endpoint. |
| 12 | 12 | * |
| @@ -46,9 +46,8 @@ | ||
| 46 | 46 | * Filesystem object. |
| 47 | 47 | * |
| 48 | 48 | * @var object Imagify_Filesystem |
| 49 | 49 | * @since 1.7.1 |
| 50 | - * @access protected | |
| 51 | 50 | * @author Grégory Viguier |
| 52 | 51 | */ |
| 53 | 52 | protected $filesystem; |
| 54 | 53 | |
| @@ -56,9 +55,8 @@ | ||
| 56 | 55 | * Use data fetched from the API. |
| 57 | 56 | * |
| 58 | 57 | * @var \stdClass|\WP_Error |
| 59 | 58 | * @since 1.9.9 |
| 60 | - * @access protected | |
| 61 | 59 | * @author Grégory Viguier |
| 62 | 60 | */ |
| 63 | 61 | protected static $user; |
| 64 | 62 | |
| @@ -82,14 +80,17 @@ | ||
| 82 | 80 | |
| 83 | 81 | /** |
| 84 | 82 | * Get your Imagify account infos. |
| 85 | 83 | * |
| 86 | - * @access public | |
| 87 | 84 | * @since 1.6.5 |
| 88 | 85 | * |
| 89 | 86 | * @return object |
| 90 | 87 | */ |
| 91 | 88 | public function get_user() { |
| 89 | + if ( empty( $this->api_key ) ) { | |
| 90 | + return new WP_Error( 'api_key_missing', __( 'API key required.', 'imagify' ) ); | |
| 91 | + } | |
| 92 | + | |
| 92 | 93 | global $wp_current_filter; |
| 93 | 94 | |
| 94 | 95 | if ( isset( static::$user ) ) { |
| 95 | 96 | return static::$user; |
| @@ -127,9 +128,8 @@ | ||
| 127 | 128 | |
| 128 | 129 | /** |
| 129 | 130 | * Create a user on your Imagify account. |
| 130 | 131 | * |
| 131 | - * @access public | |
| 132 | 132 | * @since 1.6.5 |
| 133 | 133 | * |
| 134 | 134 | * @param array $data All user data. |
| 135 | 135 | * @return object |
| @@ -135,9 +135,10 @@ | ||
| 135 | 135 | * @return object |
| 136 | 136 | */ |
| 137 | 137 | public function create_user( $data ) { |
| 138 | 138 | $this->headers = []; |
| 139 | - $data = array_merge( | |
| 139 | + | |
| 140 | + $data = array_merge( | |
| 140 | 141 | $data, |
| 141 | 142 | [ |
| 142 | 143 | 'from_plugin' => true, |
| 143 | 144 | 'partner' => imagify_get_partner(), |
| @@ -155,9 +156,9 @@ | ||
| 155 | 156 | 'post_data' => $data, |
| 156 | 157 | ] |
| 157 | 158 | ); |
| 158 | 159 | |
| 159 | - if ( ! is_wp_error( $response ) ) { | |
| 160 | + if ( ! is_wp_error( $response ) && isset( $data['partner'] ) ) { | |
| 160 | 161 | imagify_delete_partner(); |
| 161 | 162 | } |
| 162 | 163 | |
| 163 | 164 | return $response; |
| @@ -165,9 +166,8 @@ | ||
| 165 | 166 | |
| 166 | 167 | /** |
| 167 | 168 | * Update an existing user on your Imagify account. |
| 168 | 169 | * |
| 169 | - * @access public | |
| 170 | 170 | * @since 1.6.5 |
| 171 | 171 | * |
| 172 | 172 | * @param string $data All user data. |
| 173 | 173 | * @return object |
| @@ -187,9 +187,8 @@ | ||
| 187 | 187 | |
| 188 | 188 | /** |
| 189 | 189 | * Check your Imagify API key status. |
| 190 | 190 | * |
| 191 | - * @access public | |
| 192 | 191 | * @since 1.6.5 |
| 193 | 192 | * |
| 194 | 193 | * @param string $data The license key. |
| 195 | 194 | * @return object |
| @@ -219,9 +218,8 @@ | ||
| 219 | 218 | |
| 220 | 219 | /** |
| 221 | 220 | * Get the Imagify API version. |
| 222 | 221 | * |
| 223 | - * @access public | |
| 224 | 222 | * @since 1.6.5 |
| 225 | 223 | * |
| 226 | 224 | * @return object |
| 227 | 225 | */ |
| @@ -241,9 +239,8 @@ | ||
| 241 | 239 | |
| 242 | 240 | /** |
| 243 | 241 | * Get Public Info. |
| 244 | 242 | * |
| 245 | - * @access public | |
| 246 | 243 | * @since 1.6.5 |
| 247 | 244 | * |
| 248 | 245 | * @return object |
| 249 | 246 | */ |
| @@ -255,9 +252,8 @@ | ||
| 255 | 252 | |
| 256 | 253 | /** |
| 257 | 254 | * Optimize an image from its binary content. |
| 258 | 255 | * |
| 259 | - * @access public | |
| 260 | 256 | * @since 1.6.5 |
| 261 | 257 | * @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`. |
| 262 | 258 | * |
| 263 | 259 | * @param string $data All options. |
| @@ -279,9 +275,8 @@ | ||
| 279 | 275 | |
| 280 | 276 | /** |
| 281 | 277 | * Optimize an image from its URL. |
| 282 | 278 | * |
| 283 | - * @access public | |
| 284 | 279 | * @since 1.6.5 |
| 285 | 280 | * |
| 286 | 281 | * @param string $data All options. Details here: --. |
| 287 | 282 | * @return object |
| @@ -300,9 +295,8 @@ | ||
| 300 | 295 | |
| 301 | 296 | /** |
| 302 | 297 | * Get prices for plans. |
| 303 | 298 | * |
| 304 | - * @access public | |
| 305 | 299 | * @since 1.6.5 |
| 306 | 300 | * |
| 307 | 301 | * @return object |
| 308 | 302 | */ |
| @@ -312,29 +306,14 @@ | ||
| 312 | 306 | return $this->http_call( 'pricing/plan/' ); |
| 313 | 307 | } |
| 314 | 308 | |
| 315 | 309 | /** |
| 316 | - * Get prices for packs (One Time). | |
| 310 | + * Get all prices (Plans included). | |
| 317 | 311 | * |
| 318 | - * @access public | |
| 319 | 312 | * @since 1.6.5 |
| 320 | 313 | * |
| 321 | 314 | * @return object |
| 322 | 315 | */ |
| 323 | - public function get_packs_prices() { | |
| 324 | - $this->headers = $this->all_headers; | |
| 325 | - | |
| 326 | - return $this->http_call( 'pricing/pack/' ); | |
| 327 | - } | |
| 328 | - | |
| 329 | - /** | |
| 330 | - * Get all prices (packs & plans included). | |
| 331 | - * | |
| 332 | - * @access public | |
| 333 | - * @since 1.6.5 | |
| 334 | - * | |
| 335 | - * @return object | |
| 336 | - */ | |
| 337 | 316 | public function get_all_prices() { |
| 338 | 317 | $this->headers = $this->all_headers; |
| 339 | 318 | |
| 340 | 319 | return $this->http_call( 'pricing/all/' ); |
| @@ -340,11 +319,10 @@ | ||
| 340 | 319 | return $this->http_call( 'pricing/all/' ); |
| 341 | 320 | } |
| 342 | 321 | |
| 343 | 322 | /** |
| 344 | - * Get all prices (packs & plans included). | |
| 323 | + * Get coupon code data. | |
| 345 | 324 | * |
| 346 | - * @access public | |
| 347 | 325 | * @since 1.6.5 |
| 348 | 326 | * |
| 349 | 327 | * @param string $coupon A coupon code. |
| 350 | 328 | * @return object |
| @@ -357,9 +335,8 @@ | ||
| 357 | 335 | |
| 358 | 336 | /** |
| 359 | 337 | * Get information about current discount. |
| 360 | 338 | * |
| 361 | - * @access public | |
| 362 | 339 | * @since 1.6.5 |
| 363 | 340 | * |
| 364 | 341 | * @return object |
| 365 | 342 | */ |
| @@ -371,9 +348,8 @@ | ||
| 371 | 348 | |
| 372 | 349 | /** |
| 373 | 350 | * Make an HTTP call using curl. |
| 374 | 351 | * |
| 375 | - * @access private | |
| 376 | 352 | * @since 1.6.5 |
| 377 | 353 | * @since 1.6.7 Use `wp_remote_request()` when possible (when we don't need to send an image). |
| 378 | 354 | * |
| 379 | 355 | * @param string $url The URL to call. |
| @@ -450,9 +426,8 @@ | ||
| 450 | 426 | |
| 451 | 427 | /** |
| 452 | 428 | * Make an HTTP call using curl. |
| 453 | 429 | * |
| 454 | - * @access private | |
| 455 | 430 | * @since 1.6.7 |
| 456 | 431 | * @throws Exception When curl_init() fails. |
| 457 | 432 | * @author Grégory Viguier |
| 458 | 433 | * |
| @@ -465,11 +440,24 @@ | ||
| 465 | 440 | if ( ! Imagify_Requirements::supports_curl() ) { |
| 466 | 441 | return new WP_Error( 'curl', 'cURL isn\'t installed on the server.' ); |
| 467 | 442 | } |
| 468 | 443 | |
| 444 | + /** | |
| 445 | + * Allows to mock Imagify calls to the API. | |
| 446 | + * | |
| 447 | + * @param stdClass|null $response Response from the call. | |
| 448 | + * @param string $url URL from the call. | |
| 449 | + * @param array $args Arguments from the call. | |
| 450 | + */ | |
| 451 | + $response = apply_filters( 'pre_imagify_request', null, $url, $args ); | |
| 452 | + | |
| 453 | + if ( $response ) { | |
| 454 | + return $response; | |
| 455 | + } | |
| 456 | + | |
| 469 | 457 | try { |
| 470 | 458 | $url = self::API_ENDPOINT . $url; |
| 471 | - $ch = curl_init(); | |
| 459 | + $ch = curl_init(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init | |
| 472 | 460 | |
| 473 | 461 | if ( false === $ch ) { |
| 474 | 462 | throw new Exception( 'Could not initialize a new cURL handle' ); |
| 475 | 463 | } |
| @@ -474,9 +462,9 @@ | ||
| 474 | 462 | throw new Exception( 'Could not initialize a new cURL handle' ); |
| 475 | 463 | } |
| 476 | 464 | |
| 477 | 465 | if ( isset( $args['post_data']['image'] ) && is_string( $args['post_data']['image'] ) && $this->filesystem->exists( $args['post_data']['image'] ) ) { |
| 478 | - $args['post_data']['image'] = curl_file_create( $args['post_data']['image'] ); | |
| 466 | + $args['post_data']['image'] = curl_file_create( $args['post_data']['image'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_file_create | |
| 479 | 467 | } |
| 480 | 468 | |
| 481 | 469 | // Handle proxies. |
| 482 | 470 | $proxy = new WP_HTTP_Proxy(); |
| @@ -481,42 +469,42 @@ | ||
| 481 | 469 | // Handle proxies. |
| 482 | 470 | $proxy = new WP_HTTP_Proxy(); |
| 483 | 471 | |
| 484 | 472 | if ( $proxy->is_enabled() && $proxy->send_through_proxy( $url ) ) { |
| 485 | - curl_setopt( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); | |
| 486 | - curl_setopt( $ch, CURLOPT_PROXY, $proxy->host() ); | |
| 487 | - curl_setopt( $ch, CURLOPT_PROXYPORT, $proxy->port() ); | |
| 473 | + curl_setopt( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 474 | + curl_setopt( $ch, CURLOPT_PROXY, $proxy->host() ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 475 | + curl_setopt( $ch, CURLOPT_PROXYPORT, $proxy->port() ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 488 | 476 | |
| 489 | 477 | if ( $proxy->use_authentication() ) { |
| 490 | - curl_setopt( $ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); | |
| 491 | - curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $proxy->authentication() ); | |
| 478 | + curl_setopt( $ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 479 | + curl_setopt( $ch, CURLOPT_PROXYUSERPWD, $proxy->authentication() ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 492 | 480 | } |
| 493 | 481 | } |
| 494 | 482 | |
| 495 | 483 | if ( 'POST' === $args['method'] ) { |
| 496 | - curl_setopt( $ch, CURLOPT_POST, true ); | |
| 497 | - curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); | |
| 484 | + curl_setopt( $ch, CURLOPT_POST, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 485 | + curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 498 | 486 | } elseif ( 'PUT' === $args['method'] ) { |
| 499 | - curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' ); | |
| 500 | - curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); | |
| 487 | + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 488 | + curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 501 | 489 | } |
| 502 | 490 | |
| 503 | 491 | if ( defined( 'CURLOPT_PROTOCOLS' ) ) { |
| 504 | - curl_setopt( $ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS ); | |
| 492 | + curl_setopt( $ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 505 | 493 | } |
| 506 | 494 | |
| 507 | - $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ) ); | |
| 495 | + $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $url ); | |
| 508 | 496 | |
| 509 | - curl_setopt( $ch, CURLOPT_URL, $url ); | |
| 510 | - curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
| 511 | - curl_setopt( $ch, CURLOPT_HEADER, false ); | |
| 512 | - curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->headers ); | |
| 513 | - curl_setopt( $ch, CURLOPT_TIMEOUT, $args['timeout'] ); | |
| 514 | - curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $args['timeout'] ); | |
| 515 | - curl_setopt( $ch, CURLOPT_USERAGENT, $user_agent ); | |
| 516 | - @curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false ); | |
| 517 | - curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false ); | |
| 518 | - curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); | |
| 497 | + curl_setopt( $ch, CURLOPT_URL, $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 498 | + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 499 | + curl_setopt( $ch, CURLOPT_HEADER, false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 500 | + curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->headers ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 501 | + curl_setopt( $ch, CURLOPT_TIMEOUT, $args['timeout'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 502 | + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $args['timeout'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 503 | + curl_setopt( $ch, CURLOPT_USERAGENT, $user_agent ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 504 | + @curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 505 | + curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 506 | + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 519 | 507 | |
| 520 | 508 | /** |
| 521 | 509 | * Tell which http version to use with cURL during image optimization. |
| 522 | 510 | * |
| @@ -526,19 +514,19 @@ | ||
| 526 | 514 | * |
| 527 | 515 | * @param $use_version_1_0 bool True to use version 1.0. False for 1.1. Default is false. |
| 528 | 516 | */ |
| 529 | 517 | if ( apply_filters( 'imagify_curl_http_version_1_0', false ) ) { |
| 530 | - curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 ); | |
| 518 | + curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 531 | 519 | } else { |
| 532 | - curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); | |
| 520 | + curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
| 533 | 521 | } |
| 534 | 522 | |
| 535 | - $response = curl_exec( $ch ); | |
| 536 | - $error = curl_error( $ch ); | |
| 537 | - $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE ); | |
| 523 | + $response = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec | |
| 524 | + $error = curl_error( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_error | |
| 525 | + $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_getinfo | |
| 538 | 526 | |
| 539 | 527 | if ( is_resource( $ch ) ) { |
| 540 | - curl_close( $ch ); | |
| 528 | + curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close | |
| 541 | 529 | } else { |
| 542 | 530 | unset( $ch ); |
| 543 | 531 | } |
| 544 | 532 | } catch ( Exception $e ) { |
| @@ -555,9 +543,9 @@ | ||
| 555 | 543 | */ |
| 556 | 544 | do_action( 'imagify_curl_http_response', $url, $args, $e ); |
| 557 | 545 | |
| 558 | 546 | return new WP_Error( 'curl', 'An error occurred (' . $e->getMessage() . ')' ); |
| 559 | - } // End try(). | |
| 547 | + } | |
| 560 | 548 | |
| 561 | 549 | $args['headers'] = $this->headers; |
| 562 | 550 | |
| 563 | 551 | /** |
| @@ -579,9 +567,8 @@ | ||
| 579 | 567 | |
| 580 | 568 | /** |
| 581 | 569 | * Handle the request response and maybe trigger an error. |
| 582 | 570 | * |
| 583 | - * @access private | |
| 584 | 571 | * @since 1.6.7 |
| 585 | 572 | * @author Grégory Viguier |
| 586 | 573 | * |
| 587 | 574 | * @param string $response The request response. |
| @@ -628,9 +615,8 @@ | ||
| 628 | 615 | /** |
| 629 | 616 | * Generate a random key. |
| 630 | 617 | * Similar to wp_generate_password() but without filter. |
| 631 | 618 | * |
| 632 | - * @access private | |
| 633 | 619 | * @since 1.8.4 |
| 634 | 620 | * @see wp_generate_password() |
| 635 | 621 | * @author Grégory Viguier |
| 636 | 622 | * |
| @@ -650,9 +636,8 @@ | ||
| 650 | 636 | |
| 651 | 637 | /** |
| 652 | 638 | * Filter the arguments used in an HTTP request, to make sure our API key has not been overwritten by some other plugin. |
| 653 | 639 | * |
| 654 | - * @access public | |
| 655 | 640 | * @since 1.8.4 |
| 656 | 641 | * @author Grégory Viguier |
| 657 | 642 | * |
| 658 | 643 | * @param array $args An array of HTTP request arguments. |