__NAMESPACE__ . '\render_callback', ) ); } add_action( 'init', __NAMESPACE__ . '\register_blocks' ); /** * The block rendering callback. * * @param array $attributes attributes. * @return string */ function render_callback( $attributes ) { Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); wp_localize_script( 'jetpack-block-' . sanitize_title_with_dashes( Blocks::get_block_feature( __DIR__ ) ), 'Jetpack_Google_Docs', array( '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' ), ) ); $url = empty( $attributes['url'] ) ? '' : map_gsuite_url( $attributes['url'] ); $aspect_ratio = empty( $attributes['aspectRatio'] ) ? '' : $attributes['aspectRatio']; switch ( $attributes['variation'] ) { case 'google-docs': default: $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:document)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; break; case 'google-sheets': $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:spreadsheets)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; break; case 'google-slides': $pattern = '/^http[s]?:\/\/((?:www\.)?docs\.google\.com(?:.*)?(?:presentation)\/[a-z0-9\/\?=_\-\.\,&%$#\@\!\+]*)\/preview/i'; break; } if ( empty( $attributes['url'] ) ) { return ''; } if ( $pattern && ! preg_match( $pattern, $url ) ) { return ''; } // Add loader for Google Document/Spreadsheets/Presentation blocks. $iframe_markup = ''; $loading_markup = ''; $amp_markup = ''; if ( str_contains( $url, '/document/d/' ) || str_contains( $url, '/spreadsheets/d/' ) || str_contains( $url, '/presentation/d/' ) ) { if ( function_exists( 'amp_is_request' ) && amp_is_request() ) { $type = str_contains( $url, '/document/d/' ) ? __( 'Google Docs', 'jetpack' ) : ''; $type = empty( $type ) && str_contains( $url, '/spreadsheets/d/' ) ? __( 'Google Sheets', 'jetpack' ) : $type; $type = empty( $type ) && str_contains( $url, '/presentation/d/' ) ? __( 'Google Slides', 'jetpack' ) : $type; $iframe_markup = ''; $amp_markup_message = sprintf( /* translators: Placeholder is a google product, eg. Google Docs, Google Sheets, or Google Slides. */ __( 'Tap to open embedded document in %s.', 'jetpack' ), esc_html( $type ) ); $amp_markup = sprintf( '
', esc_url( $url ), $amp_markup_message ); } else { $loading_markup = '