PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 6.12
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v6.12
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 / Google / class-wpdbbackupgoogle.php

class-wpdbbackupgoogle.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 6.12, at includes/admin/Destination/Google/class-wpdbbackupgoogle.php

100 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Destination form.
4 *
5 * @package wpdbbkp
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 add_action( 'wp_db_backup_completed', array( 'WPDBBackupGoogle', 'wp_db_backup_completed' ) );
13
14 /**
15 * WPDBBackupGoogle Class.
16 *
17 * @class WPDBBackupGoogle
18 */
19 class WPDBBackupGoogle {
20
21 /**
22 * Process the database backup and upload to Google Drive.
23 *
24 * @param array $args Arguments for the backup process.
25 */
26 public static function wp_db_backup_completed( &$args ) {
27 global $wp_filesystem;
28
29 // Initialize the WordPress filesystem if it hasn't been initialized yet.
30 if ( ! function_exists( 'WP_Filesystem' ) ) {
31 require_once ABSPATH . 'wp-admin/includes/file.php';
32 }
33
34 WP_Filesystem();
35
36 $auth_code = get_option( 'wpdb_dest_google_authCode' );
37 $client_id = get_option( 'wpdb_dest_google_client_key' );
38 $client_secret = get_option( 'wpdb_dest_google_secret_key' );
39
40 if ( ! empty( $auth_code ) && ! empty( $client_id ) && ! empty( $client_secret ) ) {
41 update_option( 'wpdbbkp_backupcron_current', 'Processing Google Backup', false );
42 set_time_limit( 0 );
43
44 // Initialize the Google API client
45 require_once 'google-api-php-client/src/Google_Client.php';
46 require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
47
48 $client = new Google_Client();
49 $client->setClientId( $client_id );
50 $client->setClientSecret( $client_secret );
51 $client->setRedirectUri( site_url() . '/wp-admin/admin.php?page=wp-database-backup&action=auth' );
52 $client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
53
54 $service = new Google_DriveService( $client );
55
56 // Exchange authorization code for access token
57 if ( ! get_option( 'wpdb_google_drive_token' ) ) {
58 $access_token = $client->authenticate( $auth_code );
59 update_option( 'wpdb_google_drive_token', $access_token, false );
60 } else {
61 $access_token = get_option( 'wpdb_google_drive_token' );
62 }
63 $client->setAccessToken( $access_token );
64
65 // Upload file to Google Drive
66 $file = new Google_DriveFile();
67 $file->setTitle( $args[0] );
68 $file->setDescription( 'WP Database Backup : DB backup file - ' . site_url() );
69 $file->setMimeType( 'application/gzip' );
70
71 // Uploading chunked file so CPU and memory usage doesn't go 100%
72 $chunk_size_bytes = 1 * 1024 * 1024;
73 $media = new Google_MediaFileUpload( 'application/gzip', null, true, $chunk_size_bytes );
74 $media->setFileSize( $wp_filesystem->size( $args[1] ) );
75
76 $created_file = $service->files->insert(
77 $file,
78 array( 'mediaUpload' => $media )
79 );
80
81 $status = false;
82 $file_handle = $wp_filesystem->get_contents( $args[1] );
83
84 if ( $file_handle !== false ) {
85 $offset = 0;
86 while ( ! $status && $offset < strlen( $file_handle ) ) {
87 $chunk = substr( $file_handle, $offset, $chunk_size_bytes );
88 $status = $media->nextChunk( $created_file, $chunk );
89 $offset += $chunk_size_bytes;
90 }
91 }
92
93 $args[2] .= '<br>' . esc_html__( 'Upload Database Backup on Google Drive', 'wpdbbkp' );
94 $args[4] .= 'Drive, ';
95 }
96 }
97
98
99 }
100