| 1 |
<?php |
| 2 |
/** |
| 3 |
* ======================================= |
| 4 |
* MainWP API Backups Handler |
| 5 |
* ======================================= |
| 6 |
* |
| 7 |
* @package MainWP\Dashboard |
| 8 |
* @version 5.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace MainWP\Dashboard\Module\ApiBackups; |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
/** |
| 16 |
* MainWP API Backups Handler |
| 17 |
*/ |
| 18 |
class Api_Backups_Handler { |
| 19 |
|
| 20 |
/** |
| 21 |
* Public static variable to hold the single instance of the class. |
| 22 |
* |
| 23 |
* @var mixed Default null |
| 24 |
*/ |
| 25 |
private static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get Instance |
| 29 |
* |
| 30 |
* Creates public static instance. |
| 31 |
* |
| 32 |
* @static |
| 33 |
* |
| 34 |
* @return instance. |
| 35 |
*/ |
| 36 |
public static function get_instance() { |
| 37 |
if ( null === static::$instance ) { |
| 38 |
static::$instance = new self(); |
| 39 |
} |
| 40 |
return static::$instance; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor. |
| 46 |
* |
| 47 |
* Run each time the class is called. |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
add_action( 'admin_init', array( &$this, 'admin_init' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Init ajax actions. |
| 55 |
*/ |
| 56 |
public function admin_init() { |
| 57 |
// Cloudways Ajax. |
| 58 |
do_action( 'mainwp_ajax_add_action', 'mainwp_api_backups_selected_websites', array( &$this, 'ajax_backups_selected_websites' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Bulk Action: action backup. |
| 63 |
* |
| 64 |
* Perform a backup on the selected Child Site. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function ajax_backups_selected_websites() { |
| 69 |
Api_Backups_Helper::security_nonce( 'mainwp_api_backups_selected_websites' ); |
| 70 |
static::backups_selected_site( true ); |
| 71 |
wp_die( esc_html__( 'Error: backup', 'mainwp' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Backups selected site. |
| 76 |
* |
| 77 |
* @param bool $die_output Die to output. |
| 78 |
* |
| 79 |
* @return mixed |
| 80 |
*/ |
| 81 |
public static function backups_selected_site( $die_output = true ) { //phpcs:ignore -- NOSONAR - complex method. |
| 82 |
$website_id = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 83 |
|
| 84 |
if ( empty( $website_id ) ) { |
| 85 |
wp_send_json( array( 'error' => esc_html__( 'Error: empty site ID.', 'mainwp' ) ) ); |
| 86 |
} |
| 87 |
|
| 88 |
// Grab this from websites. |
| 89 |
$site_options = Api_Backups_Helper::get_website_options( $website_id, array( 'mainwp_3rd_party_instance_id', 'mainwp_3rd_party_app_id', 'mainwp_3rd_party_api' ) ); |
| 90 |
|
| 91 |
if ( ! is_array( $site_options ) ) { |
| 92 |
$site_options = array(); |
| 93 |
} |
| 94 |
|
| 95 |
$server_id = isset( $site_options['mainwp_3rd_party_instance_id'] ) ? $site_options['mainwp_3rd_party_instance_id'] : null; |
| 96 |
$backup_api = isset( $site_options['mainwp_3rd_party_api'] ) ? strtolower( $site_options['mainwp_3rd_party_api'] ) : null; |
| 97 |
|
| 98 |
if ( 'cpanel' !== $backup_api && 'plesk' !== $backup_api && ( empty( $server_id ) || empty( $backup_api ) ) ) { |
| 99 |
wp_send_json( array( 'error' => esc_html__( 'Error: Check Backup API settings for the website. Server Id & or Backup API provider not set.', 'mainwp' ) ) ); |
| 100 |
} |
| 101 |
|
| 102 |
$return = true; |
| 103 |
|
| 104 |
$data = array(); |
| 105 |
$data['provider'] = $backup_api; |
| 106 |
|
| 107 |
$result = null; |
| 108 |
|
| 109 |
switch ( $backup_api ) { |
| 110 |
case 'vultr': |
| 111 |
$result = Api_Backups_3rd_Party::vultr_action_create_snapshot( $website_id, $backup_api, $return ); |
| 112 |
if ( $die_output ) { |
| 113 |
$snapshot_response = $result; |
| 114 |
// Store Last Backup timestamp. |
| 115 |
if ( empty( $snapshot_response ) ) { |
| 116 |
$error = new WP_Error( '400', __( 'There was an issue with creating your backup.', 'mainwp' ) ); |
| 117 |
static::send_backups_response( false, $error ); |
| 118 |
} else { |
| 119 |
static::send_backups_response(); |
| 120 |
} |
| 121 |
} |
| 122 |
break; |
| 123 |
case 'digitalocean': |
| 124 |
$result = Api_Backups_3rd_Party::digitalocean_action_create_backup( $website_id, $return ); |
| 125 |
if ( $die_output ) { |
| 126 |
$api_response = $result; |
| 127 |
if ( is_array( $api_response ) ) { |
| 128 |
if ( 'true' === $api_response['status'] ) { |
| 129 |
static::send_backups_response(); |
| 130 |
} else { |
| 131 |
static::send_backups_response( false ); |
| 132 |
} |
| 133 |
} |
| 134 |
static::send_bulk_backups_error( false, esc_html__( 'Error: DigitalOcean Backup', 'mainwp' ) ); |
| 135 |
} |
| 136 |
break; |
| 137 |
case 'linode': |
| 138 |
$result = Api_Backups_3rd_Party::linode_action_create_backup( $website_id, $return ); |
| 139 |
if ( $die_output ) { |
| 140 |
$linode_response = $result; |
| 141 |
if ( is_object( $linode_response ) ) { |
| 142 |
// Handle response. |
| 143 |
if ( isset( $linode_response->errors ) ) { |
| 144 |
$error = new WP_Error( '400', __( $linode_response->errors['0']->reason, 'Some information' ) ); |
| 145 |
static::send_backups_response( false, $error ); |
| 146 |
} else { |
| 147 |
static::send_backups_response(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
static::send_bulk_backups_error( false, esc_html__( 'Error: Linode Backup', 'mainwp' ) ); |
| 152 |
break; |
| 153 |
case 'gridpane': |
| 154 |
$result = Api_Backups_3rd_Party::gridpane_action_create_backup( $website_id, $return ); |
| 155 |
if ( $die_output ) { |
| 156 |
$backup_status = (array) $result; |
| 157 |
if ( array_key_exists( 'error', $backup_status ) ) { |
| 158 |
$error = new WP_Error( $backup_status['error']->code, __( $backup_status['error']->message, 'mainwp' ) ); |
| 159 |
if ( is_wp_error( $error ) ) { |
| 160 |
static::send_backups_response( false, $error->get_error_message() ); |
| 161 |
} |
| 162 |
} |
| 163 |
static::send_backups_response(); |
| 164 |
static::send_bulk_backups_error( false, esc_html__( 'Error: GridPane Backup', 'mainwp' ) ); |
| 165 |
} |
| 166 |
|
| 167 |
break; |
| 168 |
case 'cloudways': |
| 169 |
$result = Api_Backups_3rd_Party::cloudways_action_create_backup( $website_id, $return ); |
| 170 |
if ( $die_output ) { |
| 171 |
$api_response = $result; |
| 172 |
if ( is_array( $api_response ) ) { |
| 173 |
if ( $api_response['status'] ) { |
| 174 |
static::send_backups_response( false ); |
| 175 |
} else { |
| 176 |
// Return success. |
| 177 |
static::send_backups_response(); |
| 178 |
} |
| 179 |
} |
| 180 |
static::send_bulk_backups_error( false, esc_html__( 'Error: Cloudways Backup', 'mainwp' ) ); |
| 181 |
} |
| 182 |
break; |
| 183 |
case 'cpanel': |
| 184 |
$result = Api_Backups_3rd_Party::cpanel_action_create_manual_backup( $return, $website_id ); |
| 185 |
if ( $die_output ) { |
| 186 |
$api_response = $result; |
| 187 |
$response_decoded = json_decode( $api_response['response'] ); |
| 188 |
|
| 189 |
if ( is_array( $api_response ) ) { |
| 190 |
// Handle response. |
| 191 |
if ( isset( $response_decoded->errors ) ) { |
| 192 |
$error = $response_decoded->errors['0']; |
| 193 |
static::send_backups_response( false, $error ); |
| 194 |
} else { |
| 195 |
$database_backup_response = Api_Backups_3rd_Party::ajax_cpanel_action_create_database_backup( true, $website_id ); |
| 196 |
|
| 197 |
// If no errors, return true. |
| 198 |
if ( 'GOOD' === $database_backup_response['result'] ) { |
| 199 |
static::send_backups_response(); |
| 200 |
} else { |
| 201 |
$error = 'There was an issue while creating the Database backup. Please check logs and try again.' . $database_backup_response['output']; |
| 202 |
static::send_backups_response( false, $error ); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
static::send_bulk_backups_error( false, esc_html__( 'Error: cPanel Backup', 'mainwp' ) ); |
| 207 |
} |
| 208 |
break; |
| 209 |
case 'plesk': |
| 210 |
$result = Api_Backups_3rd_Party::plesk_action_create_backup( $return, $website_id ); |
| 211 |
|
| 212 |
if ( $die_output ) { |
| 213 |
$api_response = $result; |
| 214 |
if ( is_array( $api_response ) ) { |
| 215 |
if ( 'true' !== $api_response['status'] ) { |
| 216 |
$response_decoded = is_array( $api_response ) && ! empty( $api_response['response'] ) ? json_decode( $api_response['response'] ) : ''; |
| 217 |
$errors = ! empty( $response_decoded ) && ! empty( $response_decoded->task->errors ) ? $response_decoded->task->errors : ''; |
| 218 |
static::send_backups_response( false, $errors ); |
| 219 |
} else { |
| 220 |
// Return success. |
| 221 |
static::send_backups_response(); |
| 222 |
} |
| 223 |
} |
| 224 |
static::send_bulk_backups_error( false, esc_html__( 'Error: Plesk Backup', 'mainwp' ) ); |
| 225 |
} |
| 226 |
break; |
| 227 |
default: |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
if ( ! empty( $result ) ) { |
| 232 |
$data['result'] = $result; |
| 233 |
} |
| 234 |
|
| 235 |
return $data; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Send backups response. |
| 241 |
* |
| 242 |
* @param bool $success Suceess or not. |
| 243 |
* @param mixed $error error. |
| 244 |
* @return void |
| 245 |
*/ |
| 246 |
public static function send_backups_response( $success = true, $error = '' ) { |
| 247 |
if ( ! $success ) { |
| 248 |
if ( $error instanceof WP_Error ) { |
| 249 |
$error = $error->get_error_message(); |
| 250 |
} |
| 251 |
wp_send_json( array( 'error' => ! empty( $error ) ? esc_html( $error ) : esc_html__( 'Send Backups request failed.', 'mainwp' ) ) ); |
| 252 |
} else { |
| 253 |
wp_send_json( array( 'success' => true ) ); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Send bulk backups error. |
| 259 |
* |
| 260 |
* @param bool $success Suceess or not. |
| 261 |
* @param mixed $error error. |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
public static function send_bulk_backups_error( $success = true, $error = '' ) { |
| 265 |
if ( isset( $_POST['bulk_backups'] ) && ! empty( $_POST['bulk_backups'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 266 |
if ( ! $success ) { |
| 267 |
if ( $error instanceof WP_Error ) { |
| 268 |
$error = $error->get_error_message(); |
| 269 |
} |
| 270 |
wp_send_json( array( 'error' => ! empty( $error ) ? esc_html( $error ) : esc_html__( 'Send Backups request failed.', 'mainwp' ) ) ); |
| 271 |
} else { |
| 272 |
wp_send_json( array( 'success' => true ) ); |
| 273 |
} |
| 274 |
} else { // to compatible with previous error messages. |
| 275 |
wp_die( esc_html( $error ) ); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|