compatibility
5 months ago
config
5 months ago
css
5 months ago
data
3 years ago
images
3 years ago
js
5 months ago
vendor
4 months ago
views
2 months ago
class-tiny-apache-rewrite.php
2 months ago
class-tiny-bulk-optimization.php
5 months ago
class-tiny-cli.php
5 months ago
class-tiny-compress-client.php
5 months ago
class-tiny-compress-fopen.php
5 months ago
class-tiny-compress.php
5 months ago
class-tiny-conversion.php
2 months ago
class-tiny-diagnostics.php
5 months ago
class-tiny-exception.php
5 months ago
class-tiny-helpers.php
5 months ago
class-tiny-image-size.php
5 months ago
class-tiny-image.php
5 months ago
class-tiny-logger.php
2 months ago
class-tiny-notices.php
2 months ago
class-tiny-php.php
5 months ago
class-tiny-picture.php
4 months ago
class-tiny-plugin.php
2 months ago
class-tiny-settings.php
2 months ago
class-tiny-source-base.php
2 months ago
class-tiny-source-image.php
5 months ago
class-tiny-source-picture.php
5 months ago
class-tiny-wp-base.php
5 months ago
class-tiny-compress-client.php
227 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2018 Tinify B.V. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | if ( ! defined( '\Tinify\VERSION' ) ) { |
| 22 | /* Load vendored client if it is not yet loaded. */ |
| 23 | require_once __DIR__ . '/vendor/tinify/Tinify/Exception.php'; |
| 24 | require_once __DIR__ . '/vendor/tinify/Tinify/ResultMeta.php'; |
| 25 | require_once __DIR__ . '/vendor/tinify/Tinify/Result.php'; |
| 26 | require_once __DIR__ . '/vendor/tinify/Tinify/Source.php'; |
| 27 | require_once __DIR__ . '/vendor/tinify/Tinify/Client.php'; |
| 28 | require_once __DIR__ . '/vendor/tinify/Tinify.php'; |
| 29 | } |
| 30 | |
| 31 | class Tiny_Compress_Client extends Tiny_Compress { |
| 32 | |
| 33 | /** |
| 34 | * API request timeout in seconds. |
| 35 | * 2026-01-14 showed 99% was < 120s |
| 36 | * |
| 37 | * @since 3.6.8 |
| 38 | * @var int |
| 39 | */ |
| 40 | const API_TIMEOUT = 120; |
| 41 | |
| 42 | /** |
| 43 | * Connection setup timeout |
| 44 | * |
| 45 | * @since 3.6.8 |
| 46 | * @var int |
| 47 | */ |
| 48 | const CONNECT_TIMEOUT = 8; |
| 49 | |
| 50 | private $last_error_code = 0; |
| 51 | private $last_message = ''; |
| 52 | private $proxy; |
| 53 | |
| 54 | protected function __construct( $api_key, $after_compress_callback ) { |
| 55 | parent::__construct( $after_compress_callback ); |
| 56 | |
| 57 | $this->proxy = new WP_HTTP_Proxy(); |
| 58 | |
| 59 | \Tinify\setAppIdentifier( self::identifier() ); |
| 60 | \Tinify\setKey( $api_key ); |
| 61 | } |
| 62 | |
| 63 | public function can_create_key() { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | public function get_compression_count() { |
| 68 | return \Tinify\getCompressionCount(); |
| 69 | } |
| 70 | |
| 71 | public function get_remaining_credits() { |
| 72 | return \Tinify\remainingCredits(); |
| 73 | } |
| 74 | |
| 75 | public function get_paying_state() { |
| 76 | return \Tinify\payingState(); |
| 77 | } |
| 78 | |
| 79 | public function get_email_address() { |
| 80 | return \Tinify\emailAddress(); |
| 81 | } |
| 82 | |
| 83 | public function get_key() { |
| 84 | return \Tinify\getKey(); |
| 85 | } |
| 86 | |
| 87 | protected function validate() { |
| 88 | try { |
| 89 | $this->last_error_code = 0; |
| 90 | $this->set_request_options( \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS ) ); |
| 91 | \Tinify\Tinify::getClient()->request( 'get', '/keys/' . $this->get_key() ); |
| 92 | return true; |
| 93 | } catch ( \Tinify\Exception $err ) { |
| 94 | $this->last_error_code = $err->status; |
| 95 | |
| 96 | if ( 429 == $err->status || 400 == $err->status ) { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | throw new Tiny_Exception( |
| 101 | $err->getMessage(), |
| 102 | get_class( $err ), |
| 103 | $err->status |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | protected function compress( $input, $resize_opts, $preserve_opts, $convert_to ) { |
| 109 | try { |
| 110 | $this->last_error_code = 0; |
| 111 | $this->set_request_options( \Tinify\Tinify::getClient() ); |
| 112 | $source = \Tinify\fromBuffer( $input ); |
| 113 | |
| 114 | if ( $resize_opts ) { |
| 115 | $source = $source->resize( $resize_opts ); |
| 116 | } |
| 117 | |
| 118 | if ( $preserve_opts ) { |
| 119 | $source = $source->preserve( $preserve_opts ); |
| 120 | } |
| 121 | |
| 122 | $compress_result = $source->result(); |
| 123 | $meta = array( |
| 124 | 'input' => array( |
| 125 | 'size' => strlen( $input ), |
| 126 | 'type' => Tiny_Helpers::get_mimetype( $input ), |
| 127 | ), |
| 128 | 'output' => array( |
| 129 | 'size' => $compress_result->size(), |
| 130 | 'type' => $compress_result->mediaType(), |
| 131 | 'width' => $compress_result->width(), |
| 132 | 'height' => $compress_result->height(), |
| 133 | 'ratio' => round( $compress_result->size() / strlen( $input ), 4 ), |
| 134 | ), |
| 135 | ); |
| 136 | |
| 137 | $buffer = $compress_result->toBuffer(); |
| 138 | $result = array( $buffer, $meta, null ); |
| 139 | |
| 140 | if ( count( $convert_to ) > 0 ) { |
| 141 | $convert_source = $source->convert( |
| 142 | array( |
| 143 | 'type' => $convert_to, |
| 144 | ) |
| 145 | ); |
| 146 | $convert_result = $convert_source->result(); |
| 147 | $meta['convert'] = array( |
| 148 | 'type' => $convert_result->mediaType(), |
| 149 | 'size' => $convert_result->size(), |
| 150 | ); |
| 151 | $convert_buffer = $convert_result->toBuffer(); |
| 152 | $result = array( $buffer, $meta, $convert_buffer ); |
| 153 | } |
| 154 | |
| 155 | return $result; |
| 156 | } catch ( \Tinify\Exception $err ) { |
| 157 | $this->last_error_code = $err->status; |
| 158 | |
| 159 | Tiny_Logger::error( |
| 160 | 'client compress error', |
| 161 | array( |
| 162 | 'error' => $err->getMessage(), |
| 163 | 'status' => $err->status, |
| 164 | ) |
| 165 | ); |
| 166 | |
| 167 | throw new Tiny_Exception( |
| 168 | $err->getMessage(), |
| 169 | get_class( $err ), |
| 170 | $err->status |
| 171 | ); |
| 172 | } // End try(). |
| 173 | } |
| 174 | |
| 175 | public function create_key( $email, $options ) { |
| 176 | try { |
| 177 | $this->last_error_code = 0; |
| 178 | $this->set_request_options( |
| 179 | \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS ) |
| 180 | ); |
| 181 | |
| 182 | \Tinify\createKey( $email, $options ); |
| 183 | } catch ( \Tinify\Exception $err ) { |
| 184 | $this->last_error_code = $err->status; |
| 185 | |
| 186 | throw new Tiny_Exception( |
| 187 | $err->getMessage(), |
| 188 | get_class( $err ), |
| 189 | $err->status |
| 190 | ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | private function set_request_options( $client ) { |
| 195 | /* The client does not let us override cURL properties yet, so we have |
| 196 | to use a reflection property. */ |
| 197 | $property = new ReflectionProperty( $client, 'options' ); |
| 198 | $property->setAccessible( true ); |
| 199 | $options = $property->getValue( $client ); |
| 200 | |
| 201 | // Set API request timeout to prevent indefinite hanging |
| 202 | $options[ CURLOPT_TIMEOUT ] = self::API_TIMEOUT; |
| 203 | $options[ CURLOPT_CONNECTTIMEOUT ] = self::CONNECT_TIMEOUT; |
| 204 | |
| 205 | if ( TINY_DEBUG ) { |
| 206 | $file = fopen( __DIR__ . '/curl.log', 'w' ); |
| 207 | if ( is_resource( $file ) ) { |
| 208 | $options[ CURLOPT_VERBOSE ] = true; |
| 209 | $options[ CURLOPT_STDERR ] = $file; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if ( $this->proxy->is_enabled() && $this->proxy->send_through_proxy( $url ) ) { |
| 214 | $options[ CURLOPT_PROXYTYPE ] = CURLPROXY_HTTP; |
| 215 | $options[ CURLOPT_PROXY ] = $this->proxy->host(); |
| 216 | $options[ CURLOPT_PROXYPORT ] = $this->proxy->port(); |
| 217 | |
| 218 | if ( $this->proxy->use_authentication() ) { |
| 219 | $options[ CURLOPT_PROXYAUTH ] = CURLAUTH_ANY; |
| 220 | $options[ CURLOPT_PROXYUSERPWD ] = $this->proxy->authentication(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | $property->setValue( $client, $options ); |
| 225 | } |
| 226 | } |
| 227 |