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