Skip to content

Commit

Permalink
Merge pull request #1024 from skaut/private-videos
Browse files Browse the repository at this point in the history
Private videos
  • Loading branch information
marekdedic authored Jul 19, 2022
2 parents 8c9f872 + 62bb4f7 commit 0d11b3b
Show file tree
Hide file tree
Showing 11 changed files with 635 additions and 334 deletions.
6 changes: 3 additions & 3 deletions src/php/admin/class-oauth-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function grant_redirect() {
return;
}

$client = \Sgdg\API_Client::get_raw_client();
$client = \Sgdg\API_Client::get_unauthorized_raw_client();
$auth_url = $client->createAuthUrl();
header( 'Location: ' . esc_url_raw( $auth_url ) );
}
Expand All @@ -43,7 +43,7 @@ public static function grant_return() {
add_settings_error( 'general', 'oauth_failed', esc_html__( 'Google API hasn\'t returned an authentication code. Please try again.', 'skaut-google-drive-gallery' ), 'error' );
}
if ( count( get_settings_errors() ) === 0 && false === get_option( 'sgdg_access_token', false ) ) {
$client = \Sgdg\API_Client::get_raw_client();
$client = \Sgdg\API_Client::get_unauthorized_raw_client();
try {
$client->fetchAccessTokenWithAuthCode( \Sgdg\GET_Helpers::get_string_variable( 'code' ) );
$access_token = $client->getAccessToken();
Expand Down Expand Up @@ -84,7 +84,7 @@ public static function revoke() {
return;
}

$client = \Sgdg\API_Client::get_raw_client();
$client = \Sgdg\API_Client::get_unauthorized_raw_client();
try {
$client->revokeToken();
delete_option( 'sgdg_access_token' );
Expand Down
42 changes: 26 additions & 16 deletions src/php/class-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class API_Client {
private static $pending_requests;

/**
* Returns a fully set-up Google client.
* Returns a Google client with set-up app info, but without authorization.
*
* @return \Sgdg\Vendor\Google\Client
*/
public static function get_raw_client() {
public static function get_unauthorized_raw_client() {
$raw_client = self::$raw_client;
if ( null === $raw_client ) {
$raw_client = new \Sgdg\Vendor\Google\Client();
Expand All @@ -66,28 +66,38 @@ public static function get_raw_client() {
}

/**
* Returns a fully set-up Google Drive API client.
* Returns a fully configured and authorized Google client.
*
* @throws \Sgdg\Exceptions\Plugin_Not_Authorized_Exception Not authorized.
*
* @return \Sgdg\Vendor\Google\Client
*/
public static function get_authorized_raw_client() {
$raw_client = self::get_unauthorized_raw_client();
$access_token = get_option( 'sgdg_access_token', false );
if ( false === $access_token ) {
throw new \Sgdg\Exceptions\Plugin_Not_Authorized_Exception();
}
$raw_client->setAccessToken( $access_token );

if ( $raw_client->isAccessTokenExpired() ) {
$raw_client->fetchAccessTokenWithRefreshToken( $raw_client->getRefreshToken() );
$new_access_token = $raw_client->getAccessToken();
$merged_access_token = array_merge( $access_token, $new_access_token );
update_option( 'sgdg_access_token', $merged_access_token );
}
return $raw_client;
}

/**
* Returns a fully set-up Google Drive API client.
*
* @return \Sgdg\Vendor\Google\Service\Drive
*/
public static function get_drive_client() {
$drive_client = self::$drive_client;
if ( null === $drive_client ) {
$raw_client = self::get_raw_client();
$access_token = get_option( 'sgdg_access_token', false );
if ( false === $access_token ) {
throw new \Sgdg\Exceptions\Plugin_Not_Authorized_Exception();
}
$raw_client->setAccessToken( $access_token );

if ( $raw_client->isAccessTokenExpired() ) {
$raw_client->fetchAccessTokenWithRefreshToken( $raw_client->getRefreshToken() );
$new_access_token = $raw_client->getAccessToken();
$merged_access_token = array_merge( $access_token, $new_access_token );
update_option( 'sgdg_access_token', $merged_access_token );
}
$raw_client = self::get_authorized_raw_client();
$drive_client = new \Sgdg\Vendor\Google\Service\Drive( $raw_client );
self::$drive_client = $drive_client;
}
Expand Down
4 changes: 4 additions & 0 deletions src/php/class-api-facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,16 @@ private static function list_files( $parent_id, $fields, $order_by, $pagination_
'id',
'name',
'mimeType',
'size',
'createdTime',
'copyRequiresWriterPermission',
'imageMediaMetadata' => array( 'width', 'height', 'time' ),
'videoMediaMetadata' => array( 'width', 'height' ),
'webContentLink',
'webViewLink',
'thumbnailLink',
'description',
'permissions' => array( 'type', 'role' ),
)
) ) {
throw new \Sgdg\Exceptions\Unsupported_Value_Exception( $fields, 'list_files' );
Expand Down
1 change: 1 addition & 0 deletions src/php/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct() {
new \Sgdg\Frontend\Block();
new \Sgdg\Frontend\Page();
new \Sgdg\Frontend\Gallery();
new \Sgdg\Frontend\Video_Proxy();
new \Sgdg\Admin\Settings_Pages();
new \Sgdg\Admin\TinyMCE_Plugin();
}
Expand Down
10 changes: 7 additions & 3 deletions src/php/frontend/class-api-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,19 @@ public function format() {
public function parse_response( $response ) {
$ret = array();
foreach ( $this->fields as $key => $value ) {
if ( is_string( $key ) && is_array( $value ) ) {
if ( is_array( $value ) ) {
foreach ( $value as $subvalue ) {
$ret[ $key ][ $subvalue ] = $response->$key->$subvalue;
if ( property_exists( $response, strval( $key ) ) && property_exists( $response->$key, $subvalue ) ) {
$ret[ $key ][ $subvalue ] = $response->$key->$subvalue;
}
}
} else {
if ( 'id' === $value ) {
$ret['id'] = $response->getMimeType() === 'application/vnd.google-apps.shortcut' ? $response->getShortcutDetails()->getTargetId() : $response->getId();
} else {
$ret[ strval( $value ) ] = $response->$value;
if ( property_exists( $response, $value ) ) {
$ret[ strval( $value ) ] = $response->$value;
}
}
}
}
Expand Down
Loading

0 comments on commit 0d11b3b

Please sign in to comment.