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

280 lines 8.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 === self::$instance ) {
38 self::$instance = new self();
39 }
40 return self::$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 self::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 -- 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 $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;
98
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 }
104
105 $return = true;
106
107 $data = array();
108 $data['provider'] = $backup_api;
109
110 $result = null;
111
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 }
169
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'] );
191
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 );
199
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 );
214
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 }
231
232 if ( ! empty( $result ) ) {
233 $data['result'] = $result;
234 }
235
236 return $data;
237 }
238
239
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
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 }
279 }
280