render.php
74 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Google Calendar block render implementation. |
| 4 | * |
| 5 | * Loaded lazily from google-calendar.php only when the block is rendered, to keep |
| 6 | * the render body out of the eager front-end PHP/opcache footprint. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Extensions\Google_Calendar; |
| 12 | |
| 13 | use Automattic\Jetpack\Blocks; |
| 14 | use Jetpack_Gutenberg; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Dynamic rendering of the block. |
| 22 | * |
| 23 | * @param array $attr Array containing the Google Calendar block attributes. |
| 24 | * @return string |
| 25 | */ |
| 26 | function render_implementation( $attr ) { |
| 27 | $height = $attr['height'] ?? '600'; |
| 28 | $url = isset( $attr['url'] ) |
| 29 | ? Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], array( 'calendar.google.com' ) ) : |
| 30 | ''; |
| 31 | $classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ); |
| 32 | |
| 33 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 34 | |
| 35 | if ( empty( $url ) ) { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | $sandbox = 'allow-scripts allow-same-origin allow-popups'; |
| 40 | if ( Blocks::is_amp_request() ) { |
| 41 | $noscript_src = str_replace( |
| 42 | '//calendar.google.com/calendar/embed', |
| 43 | '//calendar.google.com/calendar/htmlembed', |
| 44 | $url |
| 45 | ); |
| 46 | |
| 47 | $iframe = sprintf( |
| 48 | '<amp-iframe src="%1$s" frameborder="0" scrolling="no" height="%2$d" layout="fixed-height" sandbox="%3$s">%4$s%5$s</amp-iframe>', |
| 49 | esc_url( $url ), |
| 50 | absint( $height ), |
| 51 | esc_attr( $sandbox ), |
| 52 | sprintf( |
| 53 | '<a href="%s" placeholder>%s</a>', |
| 54 | esc_url( $url ), |
| 55 | esc_html__( 'Google Calendar', 'jetpack' ) |
| 56 | ), |
| 57 | sprintf( |
| 58 | '<noscript><iframe src="%1$s" frameborder="0" scrolling="no" sandbox="%2$s"></iframe></noscript>', |
| 59 | esc_url( $noscript_src ), |
| 60 | esc_attr( $sandbox ) |
| 61 | ) |
| 62 | ); |
| 63 | } else { |
| 64 | $iframe = sprintf( |
| 65 | '<iframe src="%1$s" frameborder="0" style="border:0" scrolling="no" height="%2$d" width="100%%" sandbox="%3$s"></iframe>', |
| 66 | esc_url( $url ), |
| 67 | absint( $height ), |
| 68 | esc_attr( $sandbox ) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | return sprintf( '<div class="%s">%s</div>', esc_attr( $classes ), $iframe ); |
| 73 | } |
| 74 |