google-calendar.php
84 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Google Calendar Block. |
| 4 | * |
| 5 | * @since 8.3.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Google_Calendar; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Jetpack_Gutenberg; |
| 14 | |
| 15 | /** |
| 16 | * Registers the block 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_block() { |
| 21 | Blocks::jetpack_register_block( |
| 22 | __DIR__, |
| 23 | array( |
| 24 | 'render_callback' => __NAMESPACE__ . '\load_assets', |
| 25 | ) |
| 26 | ); |
| 27 | } |
| 28 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 29 | |
| 30 | /** |
| 31 | * Google Calendar block registration/dependency declaration. |
| 32 | * |
| 33 | * @param array $attr Array containing the Google Calendar block attributes. |
| 34 | * @return string |
| 35 | */ |
| 36 | function load_assets( $attr ) { |
| 37 | $height = isset( $attr['height'] ) ? $attr['height'] : '600'; |
| 38 | $url = isset( $attr['url'] ) |
| 39 | ? Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], array( 'calendar.google.com' ) ) : |
| 40 | ''; |
| 41 | $classes = Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ); |
| 42 | |
| 43 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 44 | |
| 45 | if ( empty( $url ) ) { |
| 46 | return ''; |
| 47 | } |
| 48 | |
| 49 | $sandbox = 'allow-scripts allow-same-origin allow-popups'; |
| 50 | if ( Blocks::is_amp_request() ) { |
| 51 | $noscript_src = str_replace( |
| 52 | '//calendar.google.com/calendar/embed', |
| 53 | '//calendar.google.com/calendar/htmlembed', |
| 54 | $url |
| 55 | ); |
| 56 | |
| 57 | $iframe = sprintf( |
| 58 | '<amp-iframe src="%1$s" frameborder="0" scrolling="no" height="%2$d" layout="fixed-height" sandbox="%3$s">%4$s%5$s</amp-iframe>', |
| 59 | esc_url( $url ), |
| 60 | absint( $height ), |
| 61 | esc_attr( $sandbox ), |
| 62 | sprintf( |
| 63 | '<a href="%s" placeholder>%s</a>', |
| 64 | esc_url( $url ), |
| 65 | esc_html__( 'Google Calendar', 'jetpack' ) |
| 66 | ), |
| 67 | sprintf( |
| 68 | '<noscript><iframe src="%1$s" frameborder="0" scrolling="no" sandbox="%2$s"></iframe></noscript>', |
| 69 | esc_url( $noscript_src ), |
| 70 | esc_attr( $sandbox ) |
| 71 | ) |
| 72 | ); |
| 73 | } else { |
| 74 | $iframe = sprintf( |
| 75 | '<iframe src="%1$s" frameborder="0" style="border:0" scrolling="no" height="%2$d" width="100%%" sandbox="%3$s"></iframe>', |
| 76 | esc_url( $url ), |
| 77 | absint( $height ), |
| 78 | esc_attr( $sandbox ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return sprintf( '<div class="%s">%s</div>', esc_attr( $classes ), $iframe ); |
| 83 | } |
| 84 |