PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | jetpack_vendor/automattic/jetpack-forms/src/service/class-google-drive.php +92 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,92 @@
1 +<?php
2 +/**
3 + * Google Drive helper.
4 + *
5 + * @package automattic/jetpack
6 + */
7 +
8 +namespace Automattic\Jetpack\Forms\Service;
9 +
10 +use Automattic\Jetpack\Connection\Client;
11 +use Automattic\Jetpack\Connection\Manager;
12 +use Automattic\Jetpack\External_Connections;
13 +use Automattic\Jetpack\Status\Visitor;
14 +use WP_Error;
15 +
16 +/**
17 + * Class Google_Drive
18 + */
19 +class Google_Drive {
20 + /**
21 + * Checks if the user has a valid connection to Google Drive
22 + *
23 + * @return boolean Whether the connection is valid.
24 + */
25 + public static function has_valid_connection() {
26 + return External_Connections::get_connection_data( 'google-drive' )['is_connected'];
27 + }
28 +
29 + /**
30 + * Creates a Google Spreadsheet and returns some of its meta
31 + *
32 + * @param int $user_id The user ID.
33 + * @param string $title The spreadsheet title.
34 + * @param array $rows Array of arrays with values.
35 + * @return array|WP_Error
36 + */
37 + public static function create_sheet( $user_id, $title, $rows = array() ) {
38 + $site_id = Manager::get_site_id();
39 + if ( is_wp_error( $site_id ) ) {
40 + return false;
41 + }
42 +
43 + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
44 + // check for gdrive helper class, call synchronously on .com
45 + require_lib( 'google-sheets-helper' );
46 + $helper = \WPCOM_Google_Sheets_helper::create_for_user( $user_id );
47 +
48 + if ( is_wp_error( $helper ) ) {
49 + return $helper;
50 + }
51 +
52 + $spreadsheet = $helper->create_spreadsheet( $title, $rows );
53 +
54 + if ( is_wp_error( $spreadsheet ) ) {
55 + return $spreadsheet;
56 + }
57 +
58 + return array(
59 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- as is on google client
60 + 'sheet_link' => $spreadsheet->spreadsheetUrl,
61 + // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- as is on google client
62 + 'sheet_id' => $spreadsheet->spreadsheetId,
63 + );
64 + }
65 +
66 + $request_path = sprintf( '/sites/%d/google-drive/sheets', $site_id );
67 + $wpcom_request = Client::wpcom_json_api_request_as_user(
68 + $request_path,
69 + '2',
70 + array(
71 + 'method' => 'POST',
72 + 'headers' => array(
73 + 'content-type' => 'application/json',
74 + 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ),
75 + ),
76 + ),
77 + array(
78 + 'title' => $title,
79 + 'rows' => $rows,
80 + )
81 + );
82 + $response_code = wp_remote_retrieve_response_code( $wpcom_request );
83 + if ( 200 !== $response_code ) {
84 + return new \WP_Error(
85 + 'failed_to_fetch_data',
86 + esc_html__( 'Unable to fetch the requested data.', 'jetpack-forms' ),
87 + array( 'status' => $response_code )
88 + );
89 + }
90 + return json_decode( wp_remote_retrieve_body( $wpcom_request ), true );
91 + }
92 +}