| 1 |
<?php |
| 2 |
namespace UninstallerForm\Support; |
| 3 |
|
| 4 |
/** |
| 5 |
* GoogleSheetClient class for the uninstaller form. |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package UNINSTALLER_FORM |
| 10 |
*/ |
| 11 |
class GoogleSheetClient { |
| 12 |
protected $credentials; |
| 13 |
protected $spreadsheetId; |
| 14 |
protected $sheetName; |
| 15 |
protected $accessToken; |
| 16 |
|
| 17 |
/** |
| 18 |
* GoogleSheetClient Constructor. |
| 19 |
* |
| 20 |
* @param string $credentialsPath The path to the credentials file. |
| 21 |
* @param string $spreadsheetId The ID of the Google Sheet. |
| 22 |
* @param string $sheetName The name of the sheet to write to. Defaults to 'Sheet1'. |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
public function __construct( $credentialsPath, $spreadsheetId, $sheetName = 'Sheet1' ) { |
| 27 |
$this->credentials = json_decode( file_get_contents( $credentialsPath ), true ); |
| 28 |
$this->spreadsheetId = $spreadsheetId; |
| 29 |
$this->sheetName = $sheetName; |
| 30 |
$this->accessToken = $this->generateAccessToken(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Generate an access token for the Google Sheets API. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
protected function generateAccessToken() { |
| 39 |
$header = [ |
| 40 |
"alg" => "RS256", |
| 41 |
"typ" => "JWT", |
| 42 |
]; |
| 43 |
|
| 44 |
$now = time(); |
| 45 |
$claimSet = [ |
| 46 |
"iss" => $this->credentials['client_email'], |
| 47 |
"scope" => "https://www.googleapis.com/auth/spreadsheets", |
| 48 |
"aud" => "https://oauth2.googleapis.com/token", |
| 49 |
"iat" => $now, |
| 50 |
"exp" => $now + 3600, |
| 51 |
]; |
| 52 |
|
| 53 |
$jwtHeader = $this->base64UrlEncode( json_encode( $header ) ); |
| 54 |
$jwtClaims = $this->base64UrlEncode( json_encode( $claimSet ) ); |
| 55 |
$signatureInput = "$jwtHeader.$jwtClaims"; |
| 56 |
|
| 57 |
// Sign the JWT |
| 58 |
openssl_sign( $signatureInput, $signature, $this->credentials['private_key'], 'sha256WithRSAEncryption' ); |
| 59 |
$jwtSignature = $this->base64UrlEncode( $signature ); |
| 60 |
$jwt = "$signatureInput.$jwtSignature"; |
| 61 |
|
| 62 |
// Exchange JWT for access token |
| 63 |
$response = wp_remote_post( 'https://oauth2.googleapis.com/token', [ |
| 64 |
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'], |
| 65 |
'body' => [ |
| 66 |
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
| 67 |
'assertion' => $jwt, |
| 68 |
], |
| 69 |
] ); |
| 70 |
|
| 71 |
$body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 72 |
|
| 73 |
return $body['access_token'] ?? null; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Base64 URL encode a string. |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* |
| 81 |
* @param string $data The string to encode. |
| 82 |
* |
| 83 |
* @return string The base64 URL encoded string. |
| 84 |
* */ |
| 85 |
protected function base64UrlEncode( $data ) { |
| 86 |
return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Append a row to the Google Sheet. |
| 91 |
* |
| 92 |
* @since 1.0.0 |
| 93 |
* |
| 94 |
* @param array $values The values to append to the sheet. |
| 95 |
* |
| 96 |
* @return array The response from the Google Sheets API. |
| 97 |
**/ |
| 98 |
public function appendRow( array $values ) { |
| 99 |
$url = "https://sheets.googleapis.com/v4/spreadsheets/{$this->spreadsheetId}/values/{$this->sheetName}!A1:append?valueInputOption=RAW"; |
| 100 |
|
| 101 |
$body = [ |
| 102 |
'values' => [$values], |
| 103 |
]; |
| 104 |
|
| 105 |
$response = wp_remote_post( $url, [ |
| 106 |
'headers' => [ |
| 107 |
'Authorization' => 'Bearer ' . $this->accessToken, |
| 108 |
'Content-Type' => 'application/json', |
| 109 |
], |
| 110 |
'body' => json_encode( $body ), |
| 111 |
] ); |
| 112 |
|
| 113 |
if ( is_wp_error( $response ) ) { |
| 114 |
throw new \Exception( 'Google Sheets API request failed: ' . $response->get_error_message() ); |
| 115 |
} |
| 116 |
|
| 117 |
return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 118 |
} |
| 119 |
} |