| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed.'); |
| 4 |
|
| 5 |
if (!class_exists('UpdraftPlus_Login')) require_once('updraftplus-login.php'); |
| 6 |
|
| 7 |
class UpdraftPlus_Clone extends UpdraftPlus_Login { |
| 8 |
|
| 9 |
/** |
| 10 |
* Pulls the appropriate message for the given code and translate it before |
| 11 |
* returning it to the caller |
| 12 |
* |
| 13 |
* @internal |
| 14 |
* @param string $code The code of the message to pull |
| 15 |
* @return string - The translated message |
| 16 |
*/ |
| 17 |
protected function translate_message($code) { |
| 18 |
switch ($code) { |
| 19 |
case 'generic': |
| 20 |
default: |
| 21 |
return __('An error has occurred while processing your request.', 'updraftplus').' '.__('The server might be busy or you have lost your connection to the internet at the time of the request.', 'updraftplus').' '.__('Please try again later.', 'updraftplus'); |
| 22 |
break; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* This function will check the passed in response from the remote call and check for various errors and return the parsed response |
| 28 |
* |
| 29 |
* @param array $response - the response from the remote call |
| 30 |
* |
| 31 |
* @return array - the parsed response |
| 32 |
*/ |
| 33 |
private function parse_response($response) { |
| 34 |
|
| 35 |
if (is_wp_error($response)) { |
| 36 |
$response = array('status' => 'error', 'code' => $response->get_error_code(), 'message' => $response->get_error_message()); |
| 37 |
} else { |
| 38 |
if (isset($response['status'])) { |
| 39 |
if ('error' === $response['status']) { |
| 40 |
$response = array( |
| 41 |
'status' => 'error', |
| 42 |
'code' => isset($response['code']) ? $response['code'] : -1, |
| 43 |
'message' => isset($response['message']) ? $response['message'] : $this->translate_message('generic'), |
| 44 |
'response' => $response, |
| 45 |
); |
| 46 |
} |
| 47 |
} else { |
| 48 |
$response = array('status' => 'error', 'message' => $this->translate_message('generic')); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $response; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Executes login or registration process. Connects and sends request to the UpdraftClone |
| 57 |
* and returns the response coming from the server |
| 58 |
* |
| 59 |
* @internal |
| 60 |
* @param array $data The submitted form data |
| 61 |
* @param boolean $register Indicates whether the current call is for a registration process or not. Defaults to false. Currently will always be false. |
| 62 |
* @return array - The response from the request |
| 63 |
*/ |
| 64 |
protected function login_or_register($data, $register = false) { |
| 65 |
|
| 66 |
$action = ($register) ? 'updraftplus_clone_register' : 'updraftplus_clone_login'; |
| 67 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 68 |
|
| 69 |
$response = $this->send_remote_request($data, $action); |
| 70 |
|
| 71 |
return $this->parse_response($response); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* The ajax based request point of entry for the create clone process |
| 76 |
* |
| 77 |
* @param array $data - The submitted form data |
| 78 |
* |
| 79 |
* @return array - Response of the process |
| 80 |
*/ |
| 81 |
public function ajax_process_clone($data = array()) { |
| 82 |
try { |
| 83 |
if (isset($data['form_data']) && is_array($data['form_data'])) { |
| 84 |
$form_data = $data['form_data']; |
| 85 |
} |
| 86 |
|
| 87 |
$response = $this->create_clone($form_data); |
| 88 |
} catch (Exception $e) { |
| 89 |
$response = array('error' => true, 'message' => $e->getMessage()); |
| 90 |
} |
| 91 |
|
| 92 |
return $response; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Executes the create clone process. Connects and sends request to the UpdraftClone and returns the response coming from the server |
| 97 |
* |
| 98 |
* @internal |
| 99 |
* @param array $data - The submitted form data |
| 100 |
* @return array - The response from the request |
| 101 |
*/ |
| 102 |
public function create_clone($data) { |
| 103 |
global $updraftplus, $table_prefix; |
| 104 |
|
| 105 |
$action = 'updraftplus_clone_create'; |
| 106 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 107 |
// translators: %s: site URL with trailing slash |
| 108 |
if (empty($data['label'])) $data['label'] = sprintf(__('Clone of %s', 'updraftplus'), trailingslashit(network_site_url())); |
| 109 |
if (empty($data['install_info']['table_prefix'])) $data['install_info']['table_prefix'] = $table_prefix; |
| 110 |
$subdirectory = parse_url(network_site_url(), PHP_URL_PATH); |
| 111 |
if (empty($data['install_info']['package'])) $data['install_info']['package'] = 'starter'; |
| 112 |
if (empty($data['install_info']['subdirectory'])) $data['install_info']['subdirectory'] = !empty($subdirectory) ? $subdirectory : '/'; |
| 113 |
if (empty($data['install_info']['locale'])) $data['install_info']['locale'] = get_locale(); |
| 114 |
if (empty($data['install_info']['owner_id']) && empty($data['install_info']['owner_login'])) { |
| 115 |
$user = wp_get_current_user(); |
| 116 |
$data['install_info']['owner_id'] = $user->ID; |
| 117 |
$data['install_info']['owner_login'] = $user->user_login; |
| 118 |
} |
| 119 |
if (is_multisite()) { |
| 120 |
$data['install_info']['multisite'] = true; |
| 121 |
$data['install_info']['multisite_type'] = is_subdomain_install() ? 'subdomain' : 'subfolder'; |
| 122 |
} |
| 123 |
if (empty($data['install_info']['requested_by'])) $data['install_info']['requested_by'] = $updraftplus->version; |
| 124 |
|
| 125 |
$response = $this->send_remote_request($data, $action); |
| 126 |
|
| 127 |
return $this->parse_response($response); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Executes the clone status process. Connects and sends request to the UpdraftClone and returns the response coming from the server |
| 132 |
* |
| 133 |
* @internal |
| 134 |
* @param array $data - The submitted form data |
| 135 |
* @return array - The response from the request |
| 136 |
*/ |
| 137 |
public function clone_status($data) { |
| 138 |
|
| 139 |
$action = 'updraftplus_clone_status'; |
| 140 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 141 |
|
| 142 |
$response = $this->send_remote_request($data, $action); |
| 143 |
|
| 144 |
return $this->parse_response($response); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Executes the clone info poll. Connects and sends request to the UpdraftClone and returns the response coming from the server |
| 149 |
* |
| 150 |
* @internal |
| 151 |
* @param array $data - The submitted form data |
| 152 |
* @return array - The response from the request |
| 153 |
*/ |
| 154 |
public function clone_info_poll($data) { |
| 155 |
|
| 156 |
$action = 'updraftplus_clone_info_poll'; |
| 157 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 158 |
|
| 159 |
$response = $this->send_remote_request($data, $action); |
| 160 |
|
| 161 |
return $this->parse_response($response); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Executes the backup checkin. Connects and sends request to UpdraftPlus and returns the response coming from the server |
| 166 |
* |
| 167 |
* @internal |
| 168 |
* @param array $data - The submitted form data |
| 169 |
* @return array - The response from the request |
| 170 |
*/ |
| 171 |
public function backup_checkin($data) { |
| 172 |
$action = 'updraftplus_backup_checkin'; |
| 173 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 174 |
if (!empty($data['log_contents'])) { |
| 175 |
$data['log_contents'] = base64_encode(gzcompress($data['log_contents'])); |
| 176 |
$data['format'] = 'gzcompress'; |
| 177 |
} |
| 178 |
|
| 179 |
$response = $this->send_remote_request($data, $action); |
| 180 |
|
| 181 |
return $this->parse_response($response); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Executes the clone failed delete process. Connects and sends request to the UpdraftClone and returns the response coming from the server |
| 186 |
* |
| 187 |
* @internal |
| 188 |
* @param array $data - The submitted form data |
| 189 |
* @return array - The response from the request |
| 190 |
*/ |
| 191 |
public function clone_failed_delete($data) { |
| 192 |
|
| 193 |
$action = 'updraftplus_clone_failed_delete'; |
| 194 |
if (empty($data['site_url'])) $data['site_url'] = trailingslashit(network_site_url()); |
| 195 |
|
| 196 |
$response = $this->send_remote_request($data, $action); |
| 197 |
|
| 198 |
return $this->parse_response($response); |
| 199 |
} |
| 200 |
} |
| 201 |
|