PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 6.11
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v6.11
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.11, at includes/admin/Destination/Google/class-wpdbbackupgoogle.php

84 lines 2.8 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 * Run after complete backup.
23 *
24 * @param array $args - backup details.
25 */
26 public static function wp_db_backup_completed( &$args ) {
27
28 $auth_code = get_option( 'wpdb_dest_google_authCode' );
29 $client_id = get_option( 'wpdb_dest_google_client_key' );
30 $client_secret = get_option( 'wpdb_dest_google_secret_key' );
31 date_default_timezone_set('UTC');
32 if ( ! empty( $auth_code ) && ! empty( $client_id ) && ! empty( $client_secret ) ) {
33 update_option('wpdbbkp_backupcron_current','Processing Google Backup', false);
34 set_time_limit( 0 );
35 require_once 'google-api-php-client/src/Google_Client.php';
36 require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
37 require_once (ABSPATH . '/wp-admin/includes/file.php');
38 $client = new Google_Client();
39 // Get your credentials from the APIs Console.
40 $client->setClientId( $client_id );
41 $client->setClientSecret( $client_secret );
42 $client->setRedirectUri( site_url() . '/wp-admin/admin.php?page=wp-database-backup&action=auth' );
43 $client->setScopes( array( 'https://www.googleapis.com/auth/drive' ) );
44 $service = new Google_DriveService( $client );
45 // Exchange authorisation code for access token.
46 if ( ! get_option( 'wpdb_google_drive_token' ) ) {
47 // Save token for future use.
48 $access_token = $client->authenticate( $auth_code );
49 update_option( 'wpdb_google_drive_token', $access_token , false);
50 } else {
51 $access_token = get_option( 'wpdb_google_drive_token' );
52 }
53 $client->setAccessToken( $access_token );
54 // Upload file to Google Drive.
55 $file = new Google_DriveFile();
56 $file->setTitle( $args[0] );
57 $file->setDescription( 'WP Database Backup : DB backup file - ' . site_url() );
58 $file->setMimeType( 'application/gzip' );
59
60 // Uploading chunked file so CPU and memory usage doesn't go 100%
61 $chunkSizeBytes = 1 * 1024 * 1024;
62 $media = new Google_MediaFileUpload('application/gzip', null, true, $chunkSizeBytes);
63 $media->setFileSize(filesize($args[1]));
64
65 $created_file = $service->files->insert(
66 $file,
67 array('mediaUpload' => $media)
68 );
69 $status = false;
70 $handle = fopen($args[1], "rb");
71 while (!$status && !feof($handle)) {
72 $chunk = fread($handle, $chunkSizeBytes);
73 $uploadStatus = $media->nextChunk($created_file, $chunk);
74 }
75 fclose($handle);
76
77 $args[2] = $args[2] . '<br> Upload Database Backup on google drive';
78 $args[4] = $args[4] .= 'Drive, ';
79 // Process response here.
80 }
81 }
82
83 }
84