← All changes
|
extensions/blocks/google-docs-embed/google-docs-embed.php
+9
-83
12.0.3
→
16.3-a.1
View file →
| @@ -9,11 +9,12 @@ | ||
| 9 | 9 | |
| 10 | 10 | namespace Automattic\Jetpack\Extensions\GoogleDocsEmbed; |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Blocks; |
| 13 | -use Jetpack_Gutenberg; | |
| 14 | 13 | |
| 15 | -const FEATURE_NAME = 'google-docs-embed'; | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | 19 | * Registers the blocks for use in Gutenberg |
| 19 | 20 | * This is done via an action so that we can disable |
| @@ -21,9 +22,9 @@ | ||
| 21 | 22 | */ |
| 22 | 23 | function register_blocks() { |
| 23 | 24 | |
| 24 | 25 | Blocks::jetpack_register_block( |
| 25 | - 'jetpack/' . FEATURE_NAME, | |
| 26 | + __DIR__, | |
| 26 | 27 | array( |
| 27 | 28 | 'render_callback' => __NAMESPACE__ . '\render_callback', |
| 28 | 29 | ) |
| 29 | 30 | ); |
| @@ -32,92 +33,17 @@ | ||
| 32 | 33 | |
| 33 | 34 | /** |
| 34 | 35 | * The block rendering callback. |
| 35 | 36 | * |
| 37 | + * The render implementation lives in render.php and is only loaded when the | |
| 38 | + * block is actually rendered, keeping it out of the eager front-end path. | |
| 39 | + * | |
| 36 | 40 | * @param array $attributes attributes. |
| 37 | 41 | * @return string |
| 38 | 42 | */ |
| 39 | 43 | function render_callback( $attributes ) { |
| 40 | - | |
| 41 | - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME ); | |
| 42 | - wp_localize_script( | |
| 43 | - 'jetpack-block-' . sanitize_title_with_dashes( FEATURE_NAME ), | |
| 44 | - 'Jetpack_Google_Docs', | |
| 45 | - array( | |
| 46 | - 'error_msg' => __( 'This document is private. To view the document, login to a Google account that the document has been shared with and then refresh this page.', 'jetpack' ), | |
| 47 | - ) | |
| 48 | - ); | |
| 49 | - | |
| 50 | - $url = empty( $attributes['url'] ) ? '' : map_gsuite_url( $attributes['url'] ); | |
| 51 | - $aspect_ratio = empty( $attributes['aspectRatio'] ) ? '' : $attributes['aspectRatio']; | |
| 52 | - | |
| 53 | - switch ( $attributes['variation'] ) { | |
| 54 | - case 'google-docs': | |
| 55 | - default: | |
| 56 | - $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:document)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; | |
| 57 | - break; | |
| 58 | - case 'google-sheets': | |
| 59 | - $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:spreadsheets)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; | |
| 60 | - break; | |
| 61 | - case 'google-slides': | |
| 62 | - $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:presentation)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; | |
| 63 | - break; | |
| 64 | - } | |
| 65 | - | |
| 66 | - if ( empty( $attributes['url'] ) ) { | |
| 67 | - return ''; | |
| 68 | - } | |
| 69 | - | |
| 70 | - if ( $pattern && ! preg_match( $pattern, $url ) ) { | |
| 71 | - return ''; | |
| 72 | - } | |
| 73 | - | |
| 74 | - // Add loader for Google Document/Spreadsheets/Presentation blocks. | |
| 75 | - $iframe_markup = '<iframe src="' . esc_url( $url ) . '" allowFullScreen frameborder="0" title="' . esc_html__( 'Google Document Embed', 'jetpack' ) . '" height="450"></iframe>'; | |
| 76 | - $loading_markup = ''; | |
| 77 | - $amp_markup = ''; | |
| 78 | - | |
| 79 | - if ( | |
| 80 | - false !== strpos( $url, '/document/d/' ) || | |
| 81 | - false !== strpos( $url, '/spreadsheets/d/' ) || | |
| 82 | - false !== strpos( $url, '/presentation/d/' ) | |
| 83 | - ) { | |
| 84 | - if ( function_exists( 'amp_is_request' ) && amp_is_request() ) { | |
| 85 | - | |
| 86 | - $type = false !== strpos( $url, '/document/d/' ) ? __( 'Google Docs', 'jetpack' ) : ''; | |
| 87 | - $type = empty( $type ) && false !== strpos( $url, '/spreadsheets/d/' ) ? __( 'Google Sheets', 'jetpack' ) : $type; | |
| 88 | - $type = empty( $type ) && false !== strpos( $url, '/presentation/d/' ) ? __( 'Google Slides', 'jetpack' ) : $type; | |
| 89 | - | |
| 90 | - $iframe_markup = ''; | |
| 91 | - | |
| 92 | - $amp_markup_message = sprintf( | |
| 93 | - /* translators: Placeholder is a google product, eg. Google Docs, Google Sheets, or Google Slides. */ | |
| 94 | - __( 'Tap to open embedded document in %s.', 'jetpack' ), | |
| 95 | - esc_html( $type ) | |
| 96 | - ); | |
| 97 | - | |
| 98 | - $amp_markup = sprintf( | |
| 99 | - '<p class="wp-block-jetpack-google-docs-embed__error-msg"><a target="_blank" rel="noopener noreferrer" href="%s">%s</a></p>', | |
| 100 | - esc_url( $url ), | |
| 101 | - $amp_markup_message | |
| 102 | - ); | |
| 103 | - | |
| 104 | - } else { | |
| 105 | - $loading_markup = '<div class="loader is-active"><span>' . esc_html__( 'Loading...', 'jetpack' ) . '</span></div>'; | |
| 106 | - } | |
| 107 | - } | |
| 108 | - | |
| 109 | - $block_classes = Blocks::classes( FEATURE_NAME, $attributes, array( $aspect_ratio ) ); | |
| 110 | - | |
| 111 | - $html = | |
| 112 | - '<figure class="' . esc_attr( $block_classes ) . '">' . | |
| 113 | - '<div class="wp-block-jetpack-google-docs-embed__wrapper">' . | |
| 114 | - $loading_markup . | |
| 115 | - $amp_markup . | |
| 116 | - $iframe_markup . | |
| 117 | - '</div>' . | |
| 118 | - '</figure>'; | |
| 119 | - return $html; | |
| 44 | + require_once __DIR__ . '/render.php'; | |
| 45 | + return render_callback_implementation( $attributes ); | |
| 120 | 46 | } |
| 121 | 47 | |
| 122 | 48 | /** |
| 123 | 49 | * Convert GSuite URL to a preview URL. |