BossBey File Manager
PHP:
8.2.30
OS:
Linux
User:
imagivibe
Root
/
.
/
wp-content
/
plugins
/
robin-image-optimizer
/
includes
/
classes
/
processors
π€ Upload
π New File
π New Folder
Close
Editing: class-rio-server-premium.php
<?php // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) { exit; } /** * ΠΠ»Π°ΡΡ Π΄Π»Ρ ΠΎΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ ΡΠ΅ΡΠ΅Π· API ΡΠ΅ΡΠ²ΠΈΡΠ° Resmush. */ class WIO_Image_Processor_Premium extends WIO_Image_Processor_Abstract { /** * @var string */ protected $api_url; /** * @var string ΠΠΌΡ ΡΠ΅ΡΠ²Π΅ΡΠ° */ protected $server_name = 'server_5'; /** * ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ * * @return void */ public function __construct() { // ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ ΡΡΡΠ»ΠΊΡ Π½Π° ΡΠ΅ΡΠ²Π΅Ρ 5 $this->api_url = wrio_get_server_url( $this->server_name ); } public function howareyou() { return false; } /** * ΠΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ * * @param array $params Π²Ρ ΠΎΠ΄Π½ΡΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ ΠΎΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ * * @return array|WP_Error { * Π Π΅Π·ΡΠ»ΡΡΠ°ΡΡ ΠΎΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΠΈ * * {type} string $optimized_img_url Π£Π Π ΠΎΠΏΡΠΈΠΌΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π½Π° ΡΠ΅ΡΠ²Π΅ΡΠ΅ ΠΎΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΠΈ * {type} int $src_size ΡΠ°Π·ΠΌΠ΅Ρ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² Π±Π°ΠΉΡΠ°Ρ * {type} int $optimized_size ΡΠ°Π·ΠΌΠ΅Ρ ΠΎΠΏΡΠΈΠΌΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² Π±Π°ΠΉΡΠ°Ρ * {type} int $optimized_percent ΠΠ° ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΡΠ΅Π½ΡΠΎΠ² ΡΠΌΠ΅Π½ΡΡΠΈΠ»ΠΎΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ * } */ public function process( $settings ) { $settings = wp_parse_args( $settings, [ 'image_url' => '', 'quality' => 100, 'save_exif' => false, ] ); $query_args = [ 'quality' => $settings['quality'], 'progressive' => true, ]; if ( $settings['save_exif'] ) { $query_args['strip-exif'] = true; } if ( ! empty( $settings['image_url'] ) ) { $query_args['image_url'] = esc_url_raw( $settings['image_url'] ); } $file = wp_normalize_path( $settings['image_path'] ); if ( ! file_exists( $file ) ) { return new WP_Error( 'http_request_failed', sprintf( "File %s isn't exists.", $file ) ); } WRIO_Plugin::app()->logger->info( sprintf( 'Preparing to upload a file (%s) to a remote server (%s).', $settings['image_path'], $this->api_url ) ); $boundary = '--------------------------' . md5( microtime( true ) . wp_rand() ); $headers = [ 'Authorization' => 'Bearer ' . base64_encode( wrio_get_license_key() ), 'PluginId' => wrio_get_freemius_plugin_id(), 'X-License-Source' => wrio_get_license_source(), 'X-Site-Url' => home_url(), 'content-type' => 'multipart/form-data; boundary=' . $boundary, ]; $payload = ''; // First, add the standard POST fields: foreach ( $query_args as $name => $value ) { $payload .= '--' . $boundary; $payload .= "\r\n"; $payload .= 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n"; $payload .= $value; $payload .= "\r\n"; } // Upload the file if ( $file ) { $payload .= '--' . $boundary; $payload .= "\r\n"; $payload .= 'Content-Disposition: form-data; name="file"; filename="' . basename( $file ) . '"' . "\r\n"; // $payload .= 'Content-Type: image/jpeg' . "\r\n"; // If you know the mime-type $payload .= "\r\n"; $payload .= @file_get_contents( $file ); $payload .= "\r\n"; } $payload .= '--' . $boundary . '--'; $error_message = sprintf( 'Failed to get content of URL: %s as wp_remote_request()', $this->api_url ); wp_raise_memory_limit( 'image' ); $response = wp_remote_request( $this->api_url, [ 'method' => 'POST', 'headers' => $headers, 'body' => $payload, 'timeout' => 150, // it make take some time for large images and slow Internet connections ] ); if ( is_wp_error( $response ) ) { WRIO_Plugin::app()->logger->error( sprintf( '%s returned error (%s).', $error_message, $response->get_error_message() ) ); WRIO_Plugin::app()->logger->debug( var_export( $response, true ) ); return $response; } $response_code = wp_remote_retrieve_response_code( $response ); if ( $response_code !== 200 ) { WRIO_Plugin::app()->logger->error( sprintf( '%s, responded Http error (%s)', $error_message, $response_code ) ); return new WP_Error( 'http_request_failed', sprintf( 'Server responded an HTTP error %s', $response_code ) ); } $response_text = wp_remote_retrieve_body( $response ); $data = @json_decode( $response_text ); if ( ! isset( $data->status ) ) { WRIO_Plugin::app()->logger->error( sprintf( '%s responded an empty request body.', $error_message ) ); return new WP_Error( 'http_request_failed', 'Server responded an empty request body.' ); } if ( $data->status != 'ok' ) { WRIO_Plugin::app()->logger->error( sprintf( 'Pending status "ok", bot received "%s"', $data->status ) ); if ( isset( $data->error ) && is_string( $data->error ) ) { return new WP_Error( 'http_request_failed', $data->error ); } return new WP_Error( 'http_request_failed', sprintf( 'Server responded an %s status', $response_code ) ); } if ( ! empty( $data->response->quota ) ) { $this->set_quota_limit( $data->response->quota ); WRIO_Plugin::app()->updatePopulateOption( 'quota_fetched', true ); } return [ 'optimized_img_url' => $data->response->dest, 'src_size' => $data->response->src_size, 'optimized_size' => $data->response->dest_size, 'optimized_percent' => $data->response->percent, 'not_need_download' => false, ]; } /** * ΠΠ°ΡΠ΅ΡΡΠ²ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ * ΠΠ΅ΡΠΎΠ΄ ΠΊΠΎΠ½Π²Π΅ΡΡΠΈΡΡΠ΅Ρ ΠΊΠ°ΡΠ΅ΡΡΠ²ΠΎ ΠΈΠ· Π½Π°ΡΡΡΠΎΠ΅ΠΊ ΠΏΠ»Π°Π³ΠΈΠ½Π° Π² ΡΠΎΡΠΌΠ°Ρ ΡΠ΅ΡΠ²ΠΈΡΠ° resmush * * @param mixed $quality ΠΊΠ°ΡΠ΅ΡΡΠ²ΠΎ * * @return int */ public function quality( $quality = 100 ) { if ( is_numeric( $quality ) ) { if ( $quality >= 1 && $quality <= 100 ) { return $quality; } } switch ( $quality ) { case 'normal': return 90; case 'aggresive': return 75; case 'ultra': return 50; case 'googlepage': return 30; default: return 100; } } /** * ΠΡΠΎΠ²Π΅ΡΡΠ΅Ρ, ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ Π»ΠΈ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΠ΅ Π½Π° ΠΊΠ²ΠΎΡΡ. * * @return bool ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ true, Π΅ΡΠ»ΠΈ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΡ. */ public function has_quota_limit() { return true; } }
Save
Cancel