PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1.7
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1.7
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 6.1.7, at modules/api-backups/classes/class-api-backups-handler.php

300 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 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * MainWP API Backups Handler
22 */
23 class Api_Backups_Handler {
24
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;
31
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 }
47
48
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 }
57
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 }
65
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 }
78
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
88
89 if ( empty( $website_id ) ) {
90 wp_send_json( array( 'error' => esc_html__( 'Error: empty site ID.', 'mainwp' ) ) );
91 }
92
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' ) );
95
96 if ( ! is_array( $site_options ) ) {
97 $site_options = array();
98 }
99
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;
102
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 }
106
107 $return = true;
108
109 $data = array();
110 $data['provider'] = $backup_api;
111
112 $result = null;
113
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 }
171
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'] );
193
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 );
201
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 );
216
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 );
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 }
251
252 if ( ! empty( $result ) ) {
253 $data['result'] = $result;
254 }
255
256 return $data;
257 }
258
259
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 }
299 }
300