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