PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.1
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.1
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / Destination / CloudDrive / class-wpdatabasebackupcd.php

class-wpdatabasebackupcd.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.1, at includes/admin/Destination/CloudDrive/class-wpdatabasebackupcd.php

150 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Destination CloudDrive.
4 *
5 * @package wpdbbkp
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12
13
14
15 add_action( 'wp_db_backup_completed', array( 'WPDatabaseBackupCD', 'wp_db_backup_completed' ) );
16
17 /**
18 * WPDatabaseBackupCD Class.
19 *
20 * @class WPDatabaseBackupCD
21 */
22 class WPDatabaseBackupCD {
23
24 // Function to upload files to Backblaze B2
25 public static function upload_backup_to_clouddrive($file_path, $file_name) {
26
27 global $wp_filesystem;
28 if(!function_exists('WP_Filesystem')){
29 require_once ( ABSPATH . '/wp-admin/includes/file.php' );
30 }
31 WP_Filesystem();
32
33 $api_url ="https://app.backupforwp.com/public";
34
35 $token = get_option('wpdb_clouddrive_token') ? get_option('wpdb_clouddrive_token') : '';
36
37 $upload_auth_token = 'Bearer '.$token;
38 $upload_url = $api_url . '/api/v1/file/upload';
39
40
41 if (!$wp_filesystem) {
42 return array('success' => false, 'message' => esc_html__('Unable to initialize wp_filesystem : ' , 'wpdbbkp'). $file_path);
43 }
44
45 if (!$wp_filesystem->exists($file_path)) {
46 return array('success' => false, 'message' => esc_html__('File does not exist: ' , 'wpdbbkp'). $file_path);
47 }
48
49 $file_size = filesize($file_path);
50
51 $file_contents = $wp_filesystem->get_contents( $file_path );
52
53 if ($file_contents === false) {
54 return array('success' => false, 'message' => esc_html__('Failed to read file: ', 'wpdbbkp') . $file_path);
55 }
56
57 $root_path = str_replace('\\', '/', ABSPATH); // Normalize to forward slashes for consistency
58 $file_path = str_replace($root_path, '', $file_path);
59 $file_path = ltrim($file_path, '/'); // Ensure there is no leading slash
60
61
62 $file_data = $file_contents;
63
64 $boundary = wp_generate_password( 24 );
65
66 $headers = array(
67 'Authorization' => $upload_auth_token,
68 'domain'=> $_SERVER['HTTP_HOST'],
69 'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
70 );
71
72 $body = "--{$boundary}\r\n";
73 $body .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n";
74 $body .= "file_contents-Type: application/octet-stream\r\n\r\n";
75 $body .= $file_data . "\r\n";
76 $body .= "--{$boundary}\r\n";
77 $body .= 'Content-Disposition: form-data; name="filename"' . "\r\n\r\n";
78 $body .= $file_path . "\r\n";
79 $body .= "--{$boundary}\r\n";
80
81 $response = wp_remote_post( $upload_url, array(
82 'headers' => $headers,
83 'body' => $body,
84 'timeout' => 300,
85 ) );
86
87
88 if (is_wp_error($response)) {
89 return array('success' => false, 'message' => esc_html__('Upload request failed: ', 'wpdbbkp') . $response->get_error_message());
90 }
91
92 $response_code = wp_remote_retrieve_response_code($response);
93 if ($response_code != 200) {
94 $response_body = wp_remote_retrieve_body($response);
95 return array('success' => false, 'message' => esc_html__('Failed to upload ' , 'wpdbbkp'). $file_name . ' to Cloud Backup. Response: ' . $response_body);
96 }
97
98 return array('success' => true, 'message' => 'File ' . $file_name . esc_html__(' uploaded successfully to Cloud Backup.', 'wpdbbkp'));
99 }
100
101
102 /**
103 * Run after complete backup.
104 *
105 * @param array $args - backup details.
106 */
107
108 public static function wp_db_backup_completed( &$args ) {
109 $destination_cd = get_option( 'wpdb_clouddrive_token' , false);
110 if ( $destination_cd && !empty($destination_cd)) {
111
112 try {
113
114 $ret = WPDatabaseBackupCD::upload_backup_to_clouddrive($args[1], $args[1]);
115 $args[2] = $args[2] .$ret['message'];
116 if ($ret['success']) {
117 $args[4] .= 'CloudDrive, ';
118 }
119 return $ret;
120 } catch ( Exception $e ) {
121 $args[2] = $args[2] . "<br>".esc_html__("Failed to upload Database Backup on Cloud Backup", 'wpdbbkp');
122 return $e;
123 }
124 }
125 }
126
127 public // Function to handle the file upload
128 function wp_db_handle_file_upload($file_path) {
129 // Prepare file array for sideload
130 $file_array = array(
131 'name' => basename($file_path),
132 'tmp_name' => $file_path,
133 );
134
135 // Include WordPress file handling functions
136 require_once(ABSPATH . 'wp-admin/includes/file.php');
137 require_once(ABSPATH . 'wp-admin/includes/media.php');
138
139 // Handle the file upload
140 $uploaded_file = media_handle_sideload($file_array, 0);
141
142 if (is_wp_error($uploaded_file)) {
143 return $uploaded_file; // Return the WP_Error object
144 }
145
146 return get_attached_file($uploaded_file);
147 }
148
149 }
150