class-jetpack-google-drive-helper.php
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Google Drive helper.
*
* @package automattic/jetpack
*/
use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Status\Visitor;
/**
* Class Jetpack_Google_Drive_Helper
*/
class Jetpack_Google_Drive_Helper {
/**
* Checks if the user has a valid connection to Google Drive
*
* @param int $user_id The user ID.
* @return array Array with single 'valid' (bool) entry.
*/
public static function has_valid_connection( $user_id ) {
$site_id = Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
return false;
}
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// check for gdrive helper class, call synchronously on .com
require_lib( 'google-sheets-helper' );
$user_id = (int) get_current_user_id();
$token = WPCOM_Google_Sheets_helper::get_google_drive_token_for_user_id( $user_id );
return ! is_wp_error( $token ) && ! $token->is_expired();
}
$request_path = sprintf( '/sites/%d/google-drive/connection', $site_id );
$wpcom_request = Client::wpcom_json_api_request_as_user(
$request_path,
'2',
array(
'method' => 'GET',
'headers' => array(
'content-type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
),
)
);
$response_code = wp_remote_retrieve_response_code( $wpcom_request );
if ( 200 !== $response_code ) {
return new WP_Error(
'failed_to_fetch_data',
esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
array( 'status' => $response_code )
);
}
$result = json_decode( wp_remote_retrieve_body( $wpcom_request ), true );
return ! empty( $result ) && ! empty( $result['valid'] );
}
/**
* Creates a Google Spreadsheet and returns some of its meta
*
* @param int $user_id The user ID.
* @param string $title The spreadsheet title.
* @param array $rows Array of arrays with values.
* @return array
*/
public static function create_sheet( $user_id, $title, $rows = array() ) {
$site_id = Manager::get_site_id();
if ( is_wp_error( $site_id ) ) {
return false;
}
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// check for gdrive helper class, call synchronously on .com
require_lib( 'google-sheets-helper' );
$helper = WPCOM_Google_Sheets_helper::create_for_user( $user_id );
if ( is_wp_error( $helper ) ) {
return $helper;
}
$spreadsheet = $helper->create_spreadsheet( $title, $rows );
if ( is_wp_error( $spreadsheet ) ) {
return $spreadsheet;
}
return array(
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- as is on google client
'sheet_link' => $spreadsheet->spreadsheetUrl,
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- as is on google client
'sheet_id' => $spreadsheet->spreadsheetId,
);
}
$request_path = sprintf( '/sites/%d/google-drive/sheets', $site_id );
$wpcom_request = Client::wpcom_json_api_request_as_user(
$request_path,
'2',
array(
'method' => 'POST',
'headers' => array(
'content-type' => 'application/json',
'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
),
),
array(
'title' => $title,
'rows' => $rows,
)
);
$response_code = wp_remote_retrieve_response_code( $wpcom_request );
if ( 200 !== $response_code ) {
return new WP_Error(
'failed_to_fetch_data',
esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
array( 'status' => $response_code )
);
}
return json_decode( wp_remote_retrieve_body( $wpcom_request ), true );
}
}