Skip to content

Commit 4a603ac

Browse files
author
Jacob Middag
committed
Refactor XML-RPC logic
1 parent 49c2b56 commit 4a603ac

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/class-tiny-settings.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ class Tiny_Settings extends Tiny_WP_Base {
3030
public function __construct() {
3131
parent::__construct();
3232
$this->notices = new Tiny_Notices();
33+
}
34+
35+
private function init_compressor() {
36+
$this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback'));
37+
}
3338

34-
if(defined('XMLRPC_REQUEST') && XMLRPC_REQUEST && $this->get_api_key()) {
35-
$this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback'));
39+
public function xmlrpc_init() {
40+
try {
41+
$this->init_compressor();
42+
} catch (Tiny_Exception $e) {
3643
}
3744
}
3845

@@ -42,8 +49,8 @@ public function admin_init() {
4249
self::translate_escape('Please fill in an API key to start compressing images'));
4350
$this->notices->show('setting', $link, 'error', false);
4451
}
45-
try {
46-
$this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback'));
52+
try {
53+
$this->init_compressor();
4754
} catch (Tiny_Exception $e) {
4855
$this->notices->show('compressor_exception', self::translate_escape($e->getMessage()), 'error', false);
4956
}

src/class-tiny-wp-base.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public static function check_wp_version($version) {
4242
return floatval(self::wp_version()) >= $version;
4343
}
4444

45+
protected function is_xmlrpc_request() {
46+
return defined('XMLRPC_REQUEST') && XMLRPC_REQUEST;
47+
}
48+
4549
public static function plugin_version() {
4650
if (is_null(self::$plugin_version)) {
4751
$plugin_data = get_plugin_data(dirname(__FILE__) . '/../tiny-compress-images.php');
@@ -68,7 +72,9 @@ protected static function translate_escape($phrase) {
6872

6973
public function __construct() {
7074
add_action('init', $this->get_method('init'));
71-
if (is_admin()) {
75+
if (self::is_xmlrpc_request()) {
76+
add_action('init', $this->get_method('xmlrpc_init'));
77+
} elseif (is_admin()) {
7278
add_action('admin_init', $this->get_method('admin_init'));
7379
}
7480
}

0 commit comments

Comments
 (0)