Completed
Push — add/publishing-tweetstorms ( 53ba9c...ada03e )
by Gary
08:38
created

google-calendar.php ➔ load_assets()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 1
dl 0
loc 48
rs 8.8234
c 0
b 0
f 0
1
<?php
2
/**
3
 * Google Calendar Block.
4
 *
5
 * @since 8.3.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Google_Calendar;
11
12
use Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'google-calendar';
16
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
17
18
/**
19
 * Registers the block for use in Gutenberg
20
 * This is done via an action so that we can disable
21
 * registration if we need to.
22
 */
23
function register_block() {
24
	jetpack_register_block(
25
		BLOCK_NAME,
26
		array(
27
			'render_callback' => __NAMESPACE__ . '\load_assets',
28
		)
29
	);
30
}
31
add_action( 'init', __NAMESPACE__ . '\register_block' );
32
33
/**
34
 * Google Calendar block registration/dependency declaration.
35
 *
36
 * @param array $attr Array containing the Google Calendar block attributes.
37
 * @return string
38
 */
39
function load_assets( $attr ) {
40
	$height  = isset( $attr['height'] ) ? $attr['height'] : '600';
41
	$url     = isset( $attr['url'] )
42
		? Jetpack_Gutenberg::validate_block_embed_url( $attr['url'], array( 'calendar.google.com' ) ) :
43
		'';
44
	$classes = Blocks::classes( FEATURE_NAME, $attr );
45
46
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
47
48
	if ( empty( $url ) ) {
49
		return '';
50
	}
51
52
	$sandbox = 'allow-scripts allow-same-origin';
53
	if ( Blocks::is_amp_request() ) {
54
		$noscript_src = str_replace(
55
			'//calendar.google.com/calendar/embed',
56
			'//calendar.google.com/calendar/htmlembed',
57
			$url
58
		);
59
60
		$iframe = sprintf(
61
			'<amp-iframe src="%1$s" frameborder="0" scrolling="no" height="%2$d" layout="fixed-height" sandbox="%3$s">%4$s%5$s</amp-iframe>',
62
			esc_url( $url ),
63
			absint( $height ),
64
			esc_attr( $sandbox ),
65
			sprintf(
66
				'<a href="%s" placeholder>%s</a>',
67
				esc_url( $url ),
68
				esc_html__( 'Google Calendar', 'jetpack' )
69
			),
70
			sprintf(
71
				'<noscript><iframe src="%1$s" frameborder="0" scrolling="no" sandbox="%2$s"></iframe></noscript>',
72
				esc_url( $noscript_src ),
73
				esc_attr( $sandbox )
74
			)
75
		);
76
	} else {
77
		$iframe = sprintf(
78
			'<iframe src="%1$s" frameborder="0" style="border:0" scrolling="no" height="%2$d" width="100%%" sandbox="%3$s"></iframe>',
79
			esc_url( $url ),
80
			absint( $height ),
81
			esc_attr( $sandbox )
82
		);
83
	}
84
85
	return sprintf( '<div class="%s">%s</div>', esc_attr( $classes ), $iframe );
86
}
87