-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache preview images of docs locally & Fix showing doc preview issues #6
base: custom-oembed-provider
Are you sure you want to change the base?
Changes from 3 commits
537c983
540fcc0
18aa75a
165a371
5d1d5d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,9 +96,9 @@ public function wp_google_login_scopes( $scopes ) { | |
|
||
/** | ||
* Register custom oembed provider for google drive urls. | ||
* | ||
* | ||
* @param array $providers Default providers. | ||
* | ||
* | ||
* @return array Modified providers. | ||
*/ | ||
public function oembed_providers( $providers ) { | ||
|
@@ -109,6 +109,7 @@ public function oembed_providers( $providers ) { | |
'#https?:\\/\\/docs\\.google\\.com\\/presentation\\/d\\/(.*)\\/(.*)?#i', | ||
'#https?:\\/\\/drive\\.google\\.com\\/open\\?id\\=(.*)?#i', | ||
'#https?:\\/\\/drive\\.google\\.com\\/file\\/d\\/(.*)\\/(.*)?#i', | ||
'#https?:\\/\\/docs\\.google\\.com\\/drawings\\/d\\/(.*)\\/(.*)?#i', | ||
); | ||
|
||
// Pass current user id in URL so callback can receive it. | ||
|
@@ -127,7 +128,7 @@ public function oembed_providers( $providers ) { | |
|
||
/** | ||
* Define required plugin constants. | ||
* | ||
* | ||
* @return void | ||
*/ | ||
private function add_plugin_constants() { | ||
|
@@ -137,7 +138,7 @@ private function add_plugin_constants() { | |
|
||
/** | ||
* Loads plugin textdomain. | ||
* | ||
* | ||
* @return void | ||
*/ | ||
public function load_textdomain() { | ||
|
@@ -146,7 +147,7 @@ public function load_textdomain() { | |
|
||
/** | ||
* Registers all supported embeds. | ||
* | ||
* | ||
* @return void | ||
*/ | ||
public function register_embeds() { | ||
|
@@ -197,6 +198,14 @@ public function register_embeds() { | |
$gdrive_common_file_oembed_pattern, | ||
array( $this, 'wpdocs_embed_handler_google_drive' ) | ||
); | ||
|
||
// Common Drawings regex. | ||
$gdrive_common_file_oembed_pattern = '#https?:\\/\\/docs\\.google\\.com\\/drawings\\/d\\/(.*)\\/(.*)?#i'; | ||
wp_embed_register_handler( | ||
'rt_google_drawings', | ||
$gdrive_common_file_oembed_pattern, | ||
array( $this, 'wpdocs_embed_handler_google_drive' ) | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -216,11 +225,32 @@ public function wpdocs_embed_handler_google_drive( $matches, $attr, $url ) { | |
return ''; | ||
} | ||
|
||
$matches = array(); | ||
$file_id = ''; | ||
$basedir = ''; | ||
$baseurl = ''; | ||
|
||
preg_match( '/[-\w]{25,}/', $url, $matches ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
if ( ! empty( $matches[0] ) ) { | ||
$file_id = $matches[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if the matches are empty then it shouldn't go ahead. |
||
} | ||
|
||
$upload_dir = wp_upload_dir(); | ||
$uploadpath = path_join( $upload_dir['basedir'], 'cache/wp-google-drive' ); | ||
|
||
$cached_file = path_join( $uploadpath, "{$file_id}.png" ); | ||
$cached_url = esc_url_raw( $upload_dir['baseurl'] . "/cache/wp-google-drive/{$file_id}.png" ); | ||
|
||
var_dump( $cached_file ); | ||
var_dump( $cached_url ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why there are |
||
|
||
return $this->render_embed( | ||
'google-drive-file', | ||
array( | ||
'drive_file_url' => $url, | ||
'thumbnail_url' => $thumbnail_url, | ||
'cached_file' => $cached_file, | ||
'cached_url' => $cached_url, | ||
) | ||
); | ||
} | ||
|
@@ -262,14 +292,16 @@ private function get_thumbnail_url( $file_id, $user_id = false ) { | |
$thumbnail_url = sprintf( 'https://drive.google.com/thumbnail?id=%s&sz=w400-h400', $file_id ); | ||
$response = wp_remote_get( $thumbnail_url ); | ||
if ( ! is_wp_error( $response ) ) { | ||
|
||
// Check if retrieved content is image and not google sign up page. | ||
$content_type = wp_remote_retrieve_header( $response, 'content-type' ); | ||
$contents = wp_remote_retrieve_body( $response ); | ||
if ( false !== strpos( $content_type, 'image/' ) ) { | ||
|
||
// Check if retrieved http code is 200. | ||
$status_code = wp_remote_retrieve_response_code( $response ); | ||
if ( 200 === $status_code ) { | ||
// Save the thumbnail. | ||
$this->save_thumbnail( $file_id, $contents ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to cache image which is being fetched on line 322 of this file too. And test once if it's working fine even after token is expired There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try implementing this? Current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also |
||
|
||
return $thumbnail_url; | ||
} | ||
} | ||
|
@@ -316,7 +348,7 @@ private function get_thumbnail_url( $file_id, $user_id = false ) { | |
|
||
/** | ||
* Register endpoints. | ||
* | ||
* | ||
* @return void | ||
*/ | ||
public function register_routes() { | ||
|
@@ -333,7 +365,7 @@ public function register_routes() { | |
], | ||
] | ||
); | ||
|
||
// Route for custom oembed provider for google drive. | ||
register_rest_route( | ||
'rt-google-embed/v1', | ||
|
@@ -442,6 +474,33 @@ public function get_thumb_preview( \WP_REST_Request $request ) { | |
$data['preview_url'] = $this->get_thumbnail_url( $file_id ); | ||
return new \WP_REST_Response( $data, 200 ); | ||
} | ||
|
||
/** | ||
* Save thumbnail of a doc. | ||
* | ||
* @param string $file_id File ID. | ||
* @param mixed $contents Contents of file. | ||
* | ||
* @return void | ||
*/ | ||
public function save_thumbnail( $file_id, $contents ) { | ||
$upload_dir = wp_upload_dir(); | ||
$basedir = $upload_dir['basedir']; | ||
|
||
$uploadpath = path_join( $upload_dir['basedir'], 'cache/wp-google-drive' ); | ||
|
||
if ( ! file_exists( $uploadpath ) ) { | ||
mkdir( $uploadpath, 0755, true ); | ||
} | ||
|
||
$uploadfile = path_join( $uploadpath, "{$file_id}.png" ); | ||
|
||
if ( file_exists( $uploadfile ) ) { | ||
return; | ||
} | ||
|
||
file_put_contents( $uploadfile, $contents ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents -- Safe to write image in the cache directory. | ||
} | ||
} | ||
|
||
// Initialize the class. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,18 @@ | |
// prevent direct access to this file. | ||
exit; | ||
} | ||
?> | ||
<div style="border: 1px solid #000; text-align: center;"> | ||
<a href="<?php echo esc_url( $drive_file_url ); ?>" title="<?php esc_attr_e( 'Open the Shared Document', 'rt-google-embeds' ); ?>" target="_blank" rel="noopener noreferrer" style="color: #cd2653;"> | ||
<?php esc_html_e( 'Open Shared Document.', 'rt-google-embeds' ); ?> | ||
</a> | ||
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" /> | ||
</div> | ||
if ( file_exists( $cached_file ) ) : ?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There won't be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding https://github.com/rtCamp/wp-google-drive/pull/6/files#diff-59c6e676df5dade9eec1de91f5c5b44fR252-R253 lines, you'll only use the URL retrieved from Whenever network request is successful, you update the cached image. And when network request fails, you return the cached image URL. |
||
<div style="border: 1px solid #000; text-align: center;"> | ||
<a href="<?php echo esc_url( $drive_file_url ); ?>" title="<?php esc_attr_e( 'Open the Shared Document', 'rt-google-embeds' ); ?>" target="_blank" rel="noopener noreferrer" style="color: #cd2653;"> | ||
<?php esc_html_e( 'Open Shared Document.', 'rt-google-embeds' ); ?> | ||
</a> | ||
<img src="<?php echo esc_url( $cached_url ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview from Cache', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" /> | ||
</div> | ||
<?php else : ?> | ||
<div style="border: 1px solid #000; text-align: center;"> | ||
<a href="<?php echo esc_url( $drive_file_url ); ?>" title="<?php esc_attr_e( 'Open the Shared Document', 'rt-google-embeds' ); ?>" target="_blank" rel="noopener noreferrer" style="color: #cd2653;"> | ||
<?php esc_html_e( 'Open Shared Document.', 'rt-google-embeds' ); ?> | ||
</a> | ||
<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php esc_attr_e( 'Shared Document Preview', 'rt-google-embeds' ); ?>" style="border: 1px solid #eee; margin: 15px auto; display: block;" /> | ||
</div> | ||
<?php endif; ?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PHPCS indentations