| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined('ABSPATH') ) |
| 4 |
die(); |
| 5 |
|
| 6 |
abstract class IWP_MMB_UploadModule { |
| 7 |
|
| 8 |
private $_options; |
| 9 |
|
| 10 |
private $_instance_id; |
| 11 |
|
| 12 |
private $_storage; |
| 13 |
|
| 14 |
/** |
| 15 |
* Store options (within this class) for this remote storage module. There is also a parameter for saving to the permanent storage (i.e. database). |
| 16 |
* |
| 17 |
* @param array $options array of options to store |
| 18 |
* @param boolean $save whether or not to also save the options to the database |
| 19 |
* @param null|String $instance_id optionally set the instance ID for this instance at the same time. This is required if you have not already set an instance ID with set_instance_id() |
| 20 |
* @return void|Boolean If saving to DB, then the result of the DB save operation is returned. |
| 21 |
*/ |
| 22 |
public function set_options($options, $save = false, $instance_id = null) { |
| 23 |
|
| 24 |
$this->_options = $options; |
| 25 |
|
| 26 |
if (null !== $instance_id) $this->set_instance_id($instance_id); |
| 27 |
|
| 28 |
if (!empty($this->_storage)) unset($this->_storage); |
| 29 |
|
| 30 |
if ($save) return $this->save_options(); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Saves the current options to the database. This is a private function; external callers should use set_options(). |
| 36 |
* |
| 37 |
* @throws Exception if trying to save options without indicating an instance_id, or if the remote storage module does not have the multi-option capability |
| 38 |
*/ |
| 39 |
private function save_options() { |
| 40 |
|
| 41 |
if (!$this->supports_feature('multi_options')) { |
| 42 |
throw new Exception('set_options() can only be called on a storage method which supports multi_options (this module, '.$this->get_id().', does not)'); |
| 43 |
} |
| 44 |
|
| 45 |
// if (!$this->_instance_id) { |
| 46 |
// throw new Exception('set_options() requires an instance ID, but was called without setting one (either directly or via set_instance_id())'); |
| 47 |
// } |
| 48 |
|
| 49 |
global $iwp_backup_core; |
| 50 |
|
| 51 |
$current_db_options = $iwp_backup_core->update_remote_storage_options_format($this->get_id()); |
| 52 |
|
| 53 |
if (is_wp_error($current_db_options)) { |
| 54 |
throw new Exception('set_options(): options fetch/update failed ('.$current_db_options->get_error_code().': '.$current_db_options->get_error_message().')'); |
| 55 |
} |
| 56 |
|
| 57 |
$current_db_options = $this->_options; |
| 58 |
|
| 59 |
return IWP_MMB_Backup_Options::update_iwp_backup_option('IWP_'.$this->get_id(), $current_db_options); |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Retrieve default options for this remote storage module. |
| 65 |
* This method would normally be over-ridden by the child. |
| 66 |
* |
| 67 |
* @return Array - an array of options |
| 68 |
*/ |
| 69 |
public function get_default_options() { |
| 70 |
return array(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Retrieve a list of supported features for this storage method |
| 75 |
* This method should be over-ridden by methods supporting new |
| 76 |
* features. |
| 77 |
* |
| 78 |
* Keys are strings, and values are booleans. |
| 79 |
* |
| 80 |
* Currently known features: |
| 81 |
* |
| 82 |
* - multi_options : indicates that the remote storage module |
| 83 |
* can handle its options being in the Feb-2017 multi-options |
| 84 |
* format. N.B. This only indicates options handling, not any |
| 85 |
* other multi-destination options. |
| 86 |
* |
| 87 |
* - multi_servers : not implemented yet: indicates that the |
| 88 |
* remote storage module can handle multiple servers at backup |
| 89 |
* time. This should not be specified without multi_options. |
| 90 |
* multi_options without multi_servers is fine - it will just |
| 91 |
* cause only the first entry in the options array to be used. |
| 92 |
* |
| 93 |
* @return Array - an array of supported features (any features not |
| 94 |
* mentioned are assumed to not be supported) |
| 95 |
*/ |
| 96 |
public function get_supported_features() { |
| 97 |
return array(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Over-ride this to allow methods to not use the hidden version field, if they do not output any settings (to prevent their saved settings being over-written by just this hidden field |
| 102 |
* |
| 103 |
* @return [boolean] - return true to output the version field or false to not output the field |
| 104 |
*/ |
| 105 |
public function print_shared_settings_fields() { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Prints out the configuration section for a particular module |
| 111 |
*/ |
| 112 |
abstract function config_print(); |
| 113 |
|
| 114 |
/** |
| 115 |
* Supplies the list of keys for options to be saved in the backup job. |
| 116 |
*/ |
| 117 |
public function get_credentials() { |
| 118 |
$keys = array('IWP_ssl_disableverify', 'IWP_ssl_nossl', 'IWP_ssl_useservercerts'); |
| 119 |
if (!$this->supports_feature('multi_servers')) $keys[] = 'IWP_'.$this->get_id(); |
| 120 |
return $keys; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* Returns a space-separated list of CSS classes suitable for rows in the configuration section |
| 127 |
* |
| 128 |
* @returns String - the list of CSS classes |
| 129 |
*/ |
| 130 |
public function get_css_classes() { |
| 131 |
$classes = 'IWPmethod '.$this->get_id(); |
| 132 |
if ('' != $this->_instance_id) $classes .= ' '.$this->get_id().'-'.$this->_instance_id; |
| 133 |
return $classes; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get the backup method identifier for this class |
| 138 |
* |
| 139 |
* @return String - the identifier |
| 140 |
*/ |
| 141 |
public function get_id() { |
| 142 |
$class = get_class($this); |
| 143 |
// IWP_MMB_UploadModule_ |
| 144 |
return substr($class, 21); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Sets the instance ID - for supporting multi_options |
| 149 |
* |
| 150 |
* @param String $instance_id - the instance ID |
| 151 |
*/ |
| 152 |
public function set_instance_id($instance_id) { |
| 153 |
$this->_instance_id = $instance_id; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Sets the instance ID - for supporting multi_options |
| 158 |
* |
| 159 |
* @returns String the instance ID |
| 160 |
*/ |
| 161 |
public function get_instance_id() { |
| 162 |
return $this->_instance_id; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Check whether this storage module supports a mentioned feature |
| 167 |
* |
| 168 |
* @param String $feature - the feature concerned |
| 169 |
* |
| 170 |
* @returns Boolean |
| 171 |
*/ |
| 172 |
public function supports_feature($feature) { |
| 173 |
return in_array($feature, $this->get_supported_features()); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Retrieve options for this remote storage module |
| 178 |
* |
| 179 |
* @uses get_default_options |
| 180 |
* |
| 181 |
* @return Array - array of options. This will include default values for any options not set. |
| 182 |
*/ |
| 183 |
public function get_options() { |
| 184 |
|
| 185 |
global $iwp_backup_core; |
| 186 |
|
| 187 |
$supports_multi_options = $this->supports_feature('multi_options'); |
| 188 |
|
| 189 |
if (is_array($this->_options)) { |
| 190 |
|
| 191 |
// First, prioritise any options that were explicitly set. This is the eventual goal for all storage modules. |
| 192 |
$options = $this->_options; |
| 193 |
|
| 194 |
} elseif (is_callable(array($this, 'get_opts'))) { |
| 195 |
|
| 196 |
// Next, get any options available via a legacy / over-ride method. |
| 197 |
|
| 198 |
if ($supports_multi_options) { |
| 199 |
// This is forbidden, because get_opts() is legacy and is for methods that do not support multi-options. Supporting multi-options leads to the array format being updated, which will then break get_opts(). |
| 200 |
die('Fatal error: method '.$this->get_id().' both supports multi_options and provides a get_opts method'); |
| 201 |
} |
| 202 |
|
| 203 |
$options = $this->get_opts(); |
| 204 |
|
| 205 |
} else { |
| 206 |
|
| 207 |
// Next, look for job options (which in turn, falls back to saved settings if no job options were set) |
| 208 |
|
| 209 |
$options = $iwp_backup_core->get_job_option('IWP_'.$this->get_id()); |
| 210 |
if (!is_array($options)) $options = array(); |
| 211 |
|
| 212 |
if ($supports_multi_options) { |
| 213 |
|
| 214 |
if (!isset($options['version'])) { |
| 215 |
$options_full = $iwp_backup_core->update_remote_storage_options_format($this->get_id()); |
| 216 |
|
| 217 |
if (is_wp_error($options_full)) { |
| 218 |
$iwp_backup_core->log("Options retrieval failure: ".$options_full->get_error_code().": ".$options_full->get_error_message()." (".json_encode($options_full->get_error_data()).")"); |
| 219 |
return array(); |
| 220 |
} |
| 221 |
|
| 222 |
} else { |
| 223 |
$options_full = $options; |
| 224 |
} |
| 225 |
|
| 226 |
// IWP_MMB_UploadModule::get_options() is for getting the current instance's options. So, this branch (going via the job option) is a legacy route, and hence we just give back the first one. The non-legacy route is to call the set_options() method externally. |
| 227 |
|
| 228 |
if (!empty($options_full['settings'])) { |
| 229 |
$options = reset($options_full['settings']); |
| 230 |
}else{ |
| 231 |
$options = $options_full; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
if (false === $options) { |
| 236 |
$iwp_backup_core->log("Options retrieval failure (no options set)"); |
| 237 |
return array(); |
| 238 |
} |
| 239 |
if (!empty($options_full['settings'])) { |
| 240 |
$instance_id = key($options_full['settings']); |
| 241 |
}else{ |
| 242 |
$instance_id = null; |
| 243 |
|
| 244 |
} |
| 245 |
$this->set_options($options, false, $instance_id); |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
$options = apply_filters( |
| 252 |
'IWP_backupmodule_get_options', |
| 253 |
wp_parse_args($options, $this->get_default_options()), |
| 254 |
$this |
| 255 |
); |
| 256 |
|
| 257 |
return $options; |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
public function jobdata_set($key, $value, $legacy_key = null) { |
| 262 |
|
| 263 |
$instance_key = $this->get_id().'-'.($this->_instance_id ? $this->_instance_id : 'no_instance'); |
| 264 |
|
| 265 |
global $iwp_backup_core; |
| 266 |
|
| 267 |
$instance_data = $iwp_backup_core->jobdata_get($instance_key); |
| 268 |
|
| 269 |
if (!is_array($instance_data)) $instance_data = array(); |
| 270 |
|
| 271 |
$instance_data[$key] = $value; |
| 272 |
|
| 273 |
$iwp_backup_core->jobdata_set($instance_key, $instance_data); |
| 274 |
|
| 275 |
if (is_string($legacy_key)) $iwp_backup_core->jobdata_set($legacy_key, $value); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
public function jobdata_get($key, $default = null, $legacy_key = null) { |
| 280 |
|
| 281 |
$instance_key = $this->get_id().'-'.($this->_instance_id ? $this->_instance_id : 'no_instance'); |
| 282 |
|
| 283 |
global $iwp_backup_core; |
| 284 |
|
| 285 |
$instance_data = $iwp_backup_core->jobdata_get($instance_key); |
| 286 |
|
| 287 |
if (is_array($instance_data) && isset($instance_data[$key])) return $instance_data[$key]; |
| 288 |
|
| 289 |
return is_string($legacy_key) ? $iwp_backup_core->jobdata_get($legacy_key, $default) : $default; |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
public function jobdata_delete($key, $legacy_key = null) { |
| 294 |
|
| 295 |
$instance_key = $this->get_id().'-'.($this->_instance_id ? $this->_instance_id : 'no_instance'); |
| 296 |
|
| 297 |
global $iwp_backup_core; |
| 298 |
|
| 299 |
$instance_data = $iwp_backup_core->jobdata_get($instance_key); |
| 300 |
|
| 301 |
if (is_array($instance_data) && isset($instance_data[$key])) { |
| 302 |
unset($instance_data[$key]); |
| 303 |
$iwp_backup_core->jobdata_set($instance_key, $instance_data); |
| 304 |
} |
| 305 |
|
| 306 |
if (is_string($legacy_key)) $iwp_backup_core->jobdata_delete($legacy_key); |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
/** |
| 312 |
* This method will set the stored storage object to that indicated |
| 313 |
* |
| 314 |
* @param Object $storage - the storage client |
| 315 |
*/ |
| 316 |
public function set_storage($storage) { |
| 317 |
$this->_storage = $storage; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* This method will return the stored storage client |
| 322 |
* |
| 323 |
* @return Object - the stored remote storage client |
| 324 |
*/ |
| 325 |
public function get_storage() { |
| 326 |
if (!empty($this->_storage)) return $this->_storage; |
| 327 |
} |
| 328 |
} |
| 329 |
|