PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / api-backups / classes / class-api-backups-handler.php

class-api-backups-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.1, at modules/api-backups/classes/class-api-backups-handler.php

295 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 && 'kinsta' !== $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 case 'kinsta':
228 $result = Api_Backups_3rd_Party::kinsta_action_create_backup( $return, $website_id );
229
230 if ( $die_output ) {
231 $api_response = $result;
232 if ( is_array( $api_response ) ) {
233 if ( true !== $api_response['status'] ) {
234 static::send_backups_response( false );
235 } else {
236 // Return success.
237 static::send_backups_response();
238 }
239 }
240 wp_die( esc_html__( 'Error: Kinsta Backup', 'mainwp' ) );
241 }
242 break;
243 default:
244 break;
245 }
246
247 if ( ! empty( $result ) ) {
248 $data['result'] = $result;
249 }
250
251 return $data;
252 }
253
254
255 /**
256 * Send backups response.
257 *
258 * @param bool $success Suceess or not.
259 * @param mixed $error error.
260 * @return void
261 */
262 public static function send_backups_response( $success = true, $error = '' ) {
263 if ( ! $success ) {
264 if ( $error instanceof WP_Error ) {
265 $error = $error->get_error_message();
266 }
267 wp_send_json( array( 'error' => ! empty( $error ) ? esc_html( $error ) : esc_html__( 'Send Backups request failed.', 'mainwp' ) ) );
268 } else {
269 wp_send_json( array( 'success' => true ) );
270 }
271 }
272
273 /**
274 * Send bulk backups error.
275 *
276 * @param bool $success Suceess or not.
277 * @param mixed $error error.
278 * @return void
279 */
280 public static function send_bulk_backups_error( $success = true, $error = '' ) {
281 if ( isset( $_POST['bulk_backups'] ) && ! empty( $_POST['bulk_backups'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
282 if ( ! $success ) {
283 if ( $error instanceof WP_Error ) {
284 $error = $error->get_error_message();
285 }
286 wp_send_json( array( 'error' => ! empty( $error ) ? esc_html( $error ) : esc_html__( 'Send Backups request failed.', 'mainwp' ) ) );
287 } else {
288 wp_send_json( array( 'success' => true ) );
289 }
290 } else { // to compatible with previous error messages.
291 wp_die( esc_html( $error ) );
292 }
293 }
294 }
295