← All changes
|
jetpack_vendor/automattic/jetpack-forms/src/service/class-google-drive.php
+6
-42
13.0.2
→
16.3-beta
View file →
| @@ -8,9 +8,11 @@ | ||
| 8 | 8 | namespace Automattic\Jetpack\Forms\Service; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Connection\Client; |
| 11 | 11 | use Automattic\Jetpack\Connection\Manager; |
| 12 | +use Automattic\Jetpack\External_Connections; | |
| 12 | 13 | use Automattic\Jetpack\Status\Visitor; |
| 14 | +use WP_Error; | |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * Class Google_Drive |
| 16 | 18 | */ |
| @@ -17,50 +19,12 @@ | ||
| 17 | 19 | class Google_Drive { |
| 18 | 20 | /** |
| 19 | 21 | * Checks if the user has a valid connection to Google Drive |
| 20 | 22 | * |
| 21 | - * @param int $user_id The user ID. | |
| 22 | - * @return array Array with single 'valid' (bool) entry. | |
| 23 | + * @return boolean Whether the connection is valid. | |
| 23 | 24 | */ |
| 24 | - public static function has_valid_connection( $user_id ) { | |
| 25 | - $site_id = Manager::get_site_id(); | |
| 26 | - if ( is_wp_error( $site_id ) ) { | |
| 27 | - return false; | |
| 28 | - } | |
| 29 | - | |
| 30 | - if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 31 | - // check for gdrive helper class, call synchronously on .com | |
| 32 | - require_lib( 'google-sheets-helper' ); | |
| 33 | - $user_id = (int) get_current_user_id(); | |
| 34 | - $token = \WPCOM_Google_Sheets_helper::get_google_drive_token_for_user_id( $user_id ); | |
| 35 | - | |
| 36 | - return ! is_wp_error( $token ) && ! $token->is_expired(); | |
| 37 | - } | |
| 38 | - | |
| 39 | - $request_path = sprintf( '/sites/%d/google-drive/connection', $site_id ); | |
| 40 | - $wpcom_request = Client::wpcom_json_api_request_as_user( | |
| 41 | - $request_path, | |
| 42 | - '2', | |
| 43 | - array( | |
| 44 | - 'method' => 'GET', | |
| 45 | - 'headers' => array( | |
| 46 | - 'content-type' => 'application/json', | |
| 47 | - 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), | |
| 48 | - ), | |
| 49 | - ) | |
| 50 | - ); | |
| 51 | - | |
| 52 | - $response_code = wp_remote_retrieve_response_code( $wpcom_request ); | |
| 53 | - if ( 200 !== $response_code ) { | |
| 54 | - return new \WP_Error( | |
| 55 | - 'failed_to_fetch_data', | |
| 56 | - esc_html__( 'Unable to fetch the requested data.', 'jetpack-forms' ), | |
| 57 | - array( 'status' => $response_code ) | |
| 58 | - ); | |
| 59 | - } | |
| 60 | - $result = json_decode( wp_remote_retrieve_body( $wpcom_request ), true ); | |
| 61 | - | |
| 62 | - return ! empty( $result ) && ! empty( $result['valid'] ); | |
| 25 | + public static function has_valid_connection() { | |
| 26 | + return External_Connections::get_connection_data( 'google-drive' )['is_connected']; | |
| 63 | 27 | } |
| 64 | 28 | |
| 65 | 29 | /** |
| 66 | 30 | * Creates a Google Spreadsheet and returns some of its meta |
| @@ -67,9 +31,9 @@ | ||
| 67 | 31 | * |
| 68 | 32 | * @param int $user_id The user ID. |
| 69 | 33 | * @param string $title The spreadsheet title. |
| 70 | 34 | * @param array $rows Array of arrays with values. |
| 71 | - * @return array | |
| 35 | + * @return array|WP_Error | |
| 72 | 36 | */ |
| 73 | 37 | public static function create_sheet( $user_id, $title, $rows = array() ) { |
| 74 | 38 | $site_id = Manager::get_site_id(); |
| 75 | 39 | if ( is_wp_error( $site_id ) ) { |