google-docs-embed.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GSuite Block. |
| 4 | * |
| 5 | * @since 11.3 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\GoogleDocsEmbed; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | /** |
| 16 | * Registers the blocks for use in Gutenberg |
| 17 | * This is done via an action so that we can disable |
| 18 | * registration if we need to. |
| 19 | */ |
| 20 | function register_blocks() { |
| 21 | |
| 22 | Blocks::jetpack_register_block( |
| 23 | __DIR__, |
| 24 | array( |
| 25 | 'render_callback' => __NAMESPACE__ . '\render_callback', |
| 26 | ) |
| 27 | ); |
| 28 | } |
| 29 | add_action( 'init', __NAMESPACE__ . '\register_blocks' ); |
| 30 | |
| 31 | /** |
| 32 | * The block rendering callback. |
| 33 | * |
| 34 | * @param array $attributes attributes. |
| 35 | * @return string |
| 36 | */ |
| 37 | 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 | str_contains( $url, '/document/d/' ) || |
| 79 | str_contains( $url, '/spreadsheets/d/' ) || |
| 80 | str_contains( $url, '/presentation/d/' ) |
| 81 | ) { |
| 82 | if ( function_exists( 'amp_is_request' ) && amp_is_request() ) { |
| 83 | |
| 84 | $type = str_contains( $url, '/document/d/' ) ? __( 'Google Docs', 'jetpack' ) : ''; |
| 85 | $type = empty( $type ) && str_contains( $url, '/spreadsheets/d/' ) ? __( 'Google Sheets', 'jetpack' ) : $type; |
| 86 | $type = empty( $type ) && str_contains( $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; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Convert GSuite URL to a preview URL. |
| 122 | * |
| 123 | * @param string $url The URL of the published Doc/Spreadsheet/Presentation. |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | function map_gsuite_url( $url ) { |
| 128 | |
| 129 | // Default regex for all the URLs. |
| 130 | $gsuite_regex = '/^(http|https):\/\/(docs\.google.com)\/(spreadsheets|document|presentation)\/d\/([A-Za-z0-9_-]+).*?$/i'; |
| 131 | |
| 132 | /** |
| 133 | * Check if the URL is valid. |
| 134 | * |
| 135 | * If not, return the original URL as is. |
| 136 | */ |
| 137 | preg_match( $gsuite_regex, $url, $matches ); |
| 138 | if ( |
| 139 | empty( $matches ) || |
| 140 | empty( $matches[1] ) || |
| 141 | empty( $matches[2] ) || |
| 142 | empty( $matches[3] ) || |
| 143 | empty( $matches[4] ) |
| 144 | ) { |
| 145 | return $url; |
| 146 | } |
| 147 | |
| 148 | return "{$matches[1]}://$matches[2]/$matches[3]/d/$matches[4]/preview"; |
| 149 | } |
| 150 |