Skip to content

Commit

Permalink
Merge pull request #19 from rtCamp/update/plugin
Browse files Browse the repository at this point in the history
GH-18: Plugin Update
  • Loading branch information
SH4LIN authored Dec 18, 2023
2 parents 7c21cb4 + 0f819c8 commit 87254af
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 254 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,35 @@ Embed Google Drive

Embed a link and preview of Google Drive Documents by pasting a shared document link into the editor.

No configuration is required.
**Requires at least:** 5.5

**Tested up to:** 6.4

**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)

**Requires PHP:** 8.0+

## Installation

1. Download the plugin from [here](https://wordpress.org/plugins/embed-google-drive/)
<img src="https://github-production-user-asset-6210df.s3.amazonaws.com/56588503/290548823-0a2faf19-c9c4-4ea9-bed0-52f28fd66e3a.png" />
2. Add the plugin from the WordPress admin panel.
<img src="https://github-production-user-asset-6210df.s3.amazonaws.com/56588503/290547568-462f110a-7bf0-4b2d-84fb-5e1d92226bdb.png" />
<img src="https://github-production-user-asset-6210df.s3.amazonaws.com/56588503/290549849-4742d951-b798-4d0d-b1ef-edfd5e13dbc5.png" />
<img src="https://github-production-user-asset-6210df.s3.amazonaws.com/56588503/290550134-147a5435-5458-4abf-bf74-4c6990da2da3.png" />
3. Activate the plugin through the 'Plugins' menu in WordPress
<img src="https://github-production-user-asset-6210df.s3.amazonaws.com/56588503/290550390-6ee9cac6-0b57-460d-bd53-68c4cf376f68.png" />

## How to use
1. Copy the URL of drive document which you want to embed. <b>Note: The document must be shared with anyone with the link.</b>
2. Create new post/page or edit existing one.
3. Paste the URL in the editor. It will automatically embed the document.
![image](https://github.com/rtCamp/embed-google-drive/assets/56588503/546fbdcd-36dd-4c40-9249-4a9bdca375be)
![image](https://github.com/rtCamp/embed-google-drive/assets/56588503/77b84546-c5f8-4d3b-a613-ae37ed9d29f8)
![image](https://github.com/rtCamp/embed-google-drive/assets/56588503/57c5e5bb-7df2-469d-9e22-29a3e1655dc6)

## Video Guide
https://github.com/rtCamp/embed-google-drive/assets/56588503/d376c750-c0f8-4bfe-8b23-79ae1f070f46

## Development Notes

Expand Down
123 changes: 73 additions & 50 deletions includes/classes/class-rtcamp-google-embeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace RT_Google_Embeds;

use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
Expand All @@ -29,40 +32,44 @@ class rtCamp_Google_Embeds {
* @return rtCamp_Google_Embeds.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {

if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;

}

/**
* rtCamp_Google_Embeds constructor.
*/
public function __construct() {
$this->add_plugin_constants();

add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
add_action( 'init', array( $this, 'register_embeds' ) );
add_action( 'rest_api_init', array( $this, 'register_routes' ) );

// Register custom oembed provider for google drive urls.
add_filter( 'oembed_providers', array( $this, 'oembed_providers' ) );

}

/**
* Register custom oembed provider for google drive urls.
*
*
* @param array $providers Default providers.
*
*
* @return array Modified providers.
*/
public function oembed_providers( $providers ) {

global $wp_rewrite;

if ( is_null( $wp_rewrite ) ) {
if ( null === $wp_rewrite ) {
return $providers;
}

$formats = array(
'#https?:\\/\\/docs\\.google\\.com\\/document\\/d\\/(.*)\\/(.*)?#i',
'#https?:\\/\\/docs\\.google\\.com\\/forms\\/d\\/(.*)\\/(.*)?#i',
Expand All @@ -72,39 +79,32 @@ public function oembed_providers( $providers ) {
'#https?:\\/\\/drive\\.google\\.com\\/file\\/d\\/(.*)\\/(.*)?#i',
'#https?:\\/\\/docs\\.google\\.com\\/drawings\\/d\\/(.*)\\/(.*)?#i',
);

foreach ( $formats as $format ) {
$providers[ $format ] = array( get_rest_url( null, 'rt-google-embed/v1/oembed' ), true );
}

return $providers;
}

/**
* Define required plugin constants.
*
* @return void
*/
private function add_plugin_constants() {
define( 'RT_GOOGLE_EMBEDS_VERSION', '1.0' );
define( 'RT_GOOGLE_EMBEDS_PLUGIN_DIR', plugin_dir_path( RT_GOOGLE_EMBEDS_PLUGIN_FILE ) );
return $providers;
}

/**
* Loads plugin textdomain.
*
* Loads plugin text-domain.
*
* @return void
*/
public function load_textdomain() {

load_plugin_textdomain( 'rt-google-embeds', false, RT_GOOGLE_EMBEDS_PLUGIN_DIR . 'languages/' );

}

/**
* Registers all supported embeds.
*
*
* @return void
*/
public function register_embeds() {

// Google Docs regex.
$gdoc_oembed_pattern = '#https?:\\/\\/docs\\.google\\.com\\/document\\/d\\/(.*)\\/(.*)?#i';
wp_embed_register_handler(
Expand Down Expand Up @@ -160,6 +160,7 @@ public function register_embeds() {
$gdrawings_oembed_pattern,
array( $this, 'wpdocs_embed_handler_google_drive' )
);

}

/**
Expand All @@ -173,6 +174,7 @@ public function register_embeds() {
* @return false|string
*/
public function wpdocs_embed_handler_google_drive( $matches, $attr, $url ) {

$thumbnail_url = $this->get_thumbnail_url( $matches[1] );

if ( ! $thumbnail_url ) {
Expand All @@ -186,6 +188,7 @@ public function wpdocs_embed_handler_google_drive( $matches, $attr, $url ) {
'thumbnail_url' => $thumbnail_url,
)
);

}

/**
Expand All @@ -197,15 +200,18 @@ public function wpdocs_embed_handler_google_drive( $matches, $attr, $url ) {
* @return false|string
*/
public function render_embed( $type, $data ) {

ob_start();
$template = sprintf( 'templates/embeds/%s.php', $type );

if ( ! empty( $data ) && is_array( $data ) ) {
extract( $data, EXTR_OVERWRITE );
}
include RT_GOOGLE_EMBEDS_PLUGIN_DIR . $template; // phpcs:ignore
$embed_markup = ob_get_clean();

return $embed_markup;
include RT_GOOGLE_EMBEDS_PLUGIN_DIR . $template; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable

return ob_get_clean();

}

/**
Expand All @@ -216,18 +222,25 @@ public function render_embed( $type, $data ) {
* @return string|boolean
*/
private function get_thumbnail_url( $file_id ) {

if ( empty( $file_id ) ) {
return false;
}

// Check if a preview exists for supplied file id.
$thumbnail_url = sprintf( 'https://drive.google.com/thumbnail?id=%s&sz=w400-h400', $file_id );
$response = wp_remote_get( $thumbnail_url );

if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$response = vip_safe_wp_remote_get( $thumbnail_url );
} else {
$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' );
if ( false !== strpos( $content_type, 'image/' ) ) {
if ( str_contains( $content_type, 'image/' ) ) {

// Check if retrieved http code is 200.
$status_code = wp_remote_retrieve_response_code( $response );
Expand All @@ -238,63 +251,67 @@ private function get_thumbnail_url( $file_id ) {
}

return false;

}

/**
* Register endpoints.
*
*
* @return void
*/
public function register_routes() {

register_rest_route(
'rt-google-embed/v1',
'/get-preview-url',
[
'methods' => 'GET',
'callback' => [ $this, 'get_thumb_preview' ],
'args' => [
'media_id' => [
array(
'methods' => 'GET',
'callback' => array( $this, 'get_thumb_preview' ),
'args' => array(
'media_id' => array(
'file_id' => true,
],
],
),
),
'permission_callback' => '__return_true',
]
)
);

// Route for custom oembed provider for google drive.
register_rest_route(
'rt-google-embed/v1',
'/oembed',
array(
'methods' => 'GET',
'callback' => array( $this, 'oembed' ),
'methods' => 'GET',
'callback' => array( $this, 'oembed' ),
'permission_callback' => '__return_true',
)
);

}

/**
* REST API callback to get drive preview URL on block editor.
*
* @param \WP_REST_Request $request REST request Instance.
* @param WP_REST_Request $request REST request Instance.
*
* @return \WP_REST_Response
* @return WP_REST_Response
*/
public function oembed( \WP_REST_Request $request ) {
public function oembed( $request ) {

// Get id from url query string.
$url = $request->get_param( 'url' );

$file_id = $this->get_file_id_from_url( $url );
if ( empty( $file_id ) ) {
return new \WP_REST_Response( array(), 404 );
return new WP_REST_Response( array(), 404 );
}

// Get preview url.
$thumbnail_url = $this->get_thumbnail_url( $file_id );

// If permission is not set or invalid url, send 404.
if ( empty( $thumbnail_url ) ) {
return new \WP_REST_Response( array(), 404 );
return new WP_REST_Response( array(), 404 );
}

// Data to send as response.
Expand Down Expand Up @@ -324,7 +341,8 @@ public function oembed( \WP_REST_Request $request ) {

$data['thumbnail_url'] = $thumbnail_url;

return new \WP_REST_Response( $data, 200 );
return new WP_REST_Response( $data, 200 );

}

/**
Expand All @@ -335,33 +353,38 @@ public function oembed( \WP_REST_Request $request ) {
* @return bool|string Returns false or ID.
*/
public function get_file_id_from_url( $url ) {

$matches = array();
preg_match( '/[-\w]{25,}/', $url, $matches );
if ( empty( $matches[0] ) ) {
return false;
}

return $matches[0];

}

/**
* REST API callback to get drive preview URL.
*
* @param \WP_REST_Request $request REST Instance.
* @param WP_REST_Request $request REST Instance.
*
* @return \WP_REST_Response
* @return WP_REST_Response
*/
public function get_thumb_preview( \WP_REST_Request $request ) {
public function get_thumb_preview( $request ) {

$url = $request->get_param( 'url' );

$file_id = $this->get_file_id_from_url( $url );
if ( empty( $file_id ) ) {
return new \WP_REST_Response( array(), 404 );
return new WP_REST_Response( array(), 404 );
}

$data['preview_url'] = $this->get_thumbnail_url( $file_id );
return new \WP_REST_Response( $data, 200 );
return new WP_REST_Response( $data, 200 );

}

}

// Initialize the class.
Expand Down
Loading

0 comments on commit 87254af

Please sign in to comment.