Completed
Push — add/license-support ( 73d07d...1a145a )
by Atanas
09:09
created

google-calendar.php ➔ load_assets()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 12
nop 1
dl 0
loc 29
rs 9.1448
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
	if ( Blocks::is_amp_request() ) {
53
		return sprintf(
54
			'<div class="%1$s"><amp-iframe src="%2$s" frameborder="0" style="border:0" scrolling="no" height="%3$d" sandbox="allow-scripts allow-same-origin" layout="responsive"></amp-iframe></div>',
55
			esc_attr( $classes ),
56
			esc_url( $url ),
57
			absint( $height )
58
		);
59
	} else {
60
		return sprintf(
61
			'<div class="%1$s"><iframe src="%2$s" frameborder="0" style="border:0" scrolling="no" height="%3$d"></iframe></div>',
62
			esc_attr( $classes ),
63
			esc_url( $url ),
64
			absint( $height )
65
		);
66
	}
67
}
68