From 029687a2dd24217da67a8097f655a8c791706146 Mon Sep 17 00:00:00 2001 From: realrellek <91732532+realrellek@users.noreply.github.com> Date: Thu, 4 Nov 2021 08:35:48 +0100 Subject: [PATCH 1/2] Update class-tiny-settings.php Making Tinify available outside wp-admin. For example if you sideload an image using an API or a frontend plugin (e.g. contact forms). No performance impact because compressor is only initialised if we actually have to process media, in which case we have to do quite heavy work anyway. --- src/class-tiny-settings.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/class-tiny-settings.php b/src/class-tiny-settings.php index f6e0ed4..152effb 100644 --- a/src/class-tiny-settings.php +++ b/src/class-tiny-settings.php @@ -24,6 +24,9 @@ class Tiny_Settings extends Tiny_WP_Base { private $tinify_sizes; private $compressor; private $notices; + + // Make Tinify available from outside wp-admin + private $compressor_init = false; protected static $offload_s3_plugin = 'amazon-s3-and-cloudfront/wordpress-s3.php'; @@ -37,6 +40,9 @@ private function init_compressor() { $this->get_api_key(), $this->get_method( 'after_compress_callback' ) ); + + // We assume, we now have a compressor. If not, something else went wrong. + $this->compressor_init = true; } public function get_absolute_url() { @@ -151,10 +157,25 @@ public function account_status() { } public function get_compressor() { + // We check if we should have a compressor available, and if not, we create one. + if ( $this->compressor_init === false ) { + try { + $this->init_compressor(); + } catch ( Tiny_Exception e ) { + // Did not work. Fail silently, like in XMLRPC + } + } + return $this->compressor; } public function set_compressor( $compressor ) { + // If $compressor is not null, we use this one. Otherwise we try to init our own when the time comes. + if ( ! is_null( $compressor ) ) { + $this->compressor_init = true; + } else { + $this->compressor_init = false; + } $this->compressor = $compressor; } From 72ce9e1c6c3259bb34c75f4d7348dbcad56d933f Mon Sep 17 00:00:00 2001 From: realrellek <91732532+realrellek@users.noreply.github.com> Date: Thu, 4 Nov 2021 17:35:26 +0100 Subject: [PATCH 2/2] Variables need a $ --- src/class-tiny-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class-tiny-settings.php b/src/class-tiny-settings.php index 152effb..df4c440 100644 --- a/src/class-tiny-settings.php +++ b/src/class-tiny-settings.php @@ -161,7 +161,7 @@ public function get_compressor() { if ( $this->compressor_init === false ) { try { $this->init_compressor(); - } catch ( Tiny_Exception e ) { + } catch ( Tiny_Exception $e ) { // Did not work. Fail silently, like in XMLRPC } }