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