1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Google Calendar Block. |
4
|
|
|
* |
5
|
|
|
* @since 8.3.0 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jetpack\Google_Calendar_Block; |
11
|
|
|
|
12
|
|
|
const FEATURE_NAME = 'google-calendar'; |
13
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
14
|
|
|
/** |
15
|
|
|
* Check if the block should be available on the site. |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
|
|
function is_available() { |
20
|
|
|
if ( |
21
|
|
|
defined( 'IS_WPCOM' ) |
22
|
|
|
&& IS_WPCOM |
23
|
|
|
&& function_exists( 'has_any_blog_stickers' ) |
24
|
|
|
) { |
25
|
|
|
if ( has_any_blog_stickers( |
26
|
|
|
array( 'premium-plan', 'business-plan', 'ecommerce-plan' ), |
27
|
|
|
get_current_blog_id() |
28
|
|
|
) ) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Registers the block for use in Gutenberg |
39
|
|
|
* This is done via an action so that we can disable |
40
|
|
|
* registration if we need to. |
41
|
|
|
*/ |
42
|
|
|
function register_block() { |
43
|
|
|
if ( is_available() ) { |
44
|
|
|
jetpack_register_block( |
45
|
|
|
BLOCK_NAME, |
46
|
|
|
array( |
47
|
|
|
'render_callback' => 'Jetpack\Google_Calendar_Block\load_assets', |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
add_action( 'init', 'Jetpack\Google_Calendar_Block\register_block' ); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the availability of the block as the editor |
56
|
|
|
* is loaded |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
function set_availability() { |
|
|
|
|
59
|
|
|
if ( is_available() ) { |
60
|
|
|
\Jetpack_Gutenberg::set_extension_available( BLOCK_NAME ); |
61
|
|
|
} else { |
62
|
|
|
\Jetpack_Gutenberg::set_extension_unavailable( |
63
|
|
|
BLOCK_NAME, |
64
|
|
|
'missing_plan', |
65
|
|
|
array( |
66
|
|
|
'required_feature' => 'google_calendar', |
67
|
|
|
'required_plan' => 'value_bundle', |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
add_action( 'init', 'Jetpack\Google_Calendar_Block\set_availability' ); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Google Calendar block registration/dependency declaration. |
76
|
|
|
* |
77
|
|
|
* @param array $attr Array containing the Google Calendar block attributes. |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
function load_assets( $attr ) { |
81
|
|
|
$width = isset( $attr['width'] ) ? $attr['width'] : '800'; |
82
|
|
|
$height = isset( $attr['height'] ) ? $attr['height'] : '600'; |
83
|
|
|
$url = isset( $attr['url'] ) ? $attr['url'] : ''; |
84
|
|
|
$classes = \Jetpack_Gutenberg::block_classes( 'google-calendar', $attr ); |
85
|
|
|
|
86
|
|
|
if ( empty( $url ) ) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ( class_exists( 'Jetpack_AMP_Support' ) && \Jetpack_AMP_Support::is_amp_request() ) { |
91
|
|
|
return sprintf( |
92
|
|
|
'<div class="%1$s"><amp-iframe src="%2$s" frameborder="0" style="border:0" scrolling="no" width="%3$d" height="%4$d" sandbox="allow-scripts allow-same-origin" layout="responsive"></amp-iframe></div>', |
93
|
|
|
esc_attr( $classes ), |
94
|
|
|
esc_url( $url ), |
95
|
|
|
absint( $width ), |
96
|
|
|
absint( $height ) |
97
|
|
|
); |
98
|
|
|
} else { |
99
|
|
|
return sprintf( |
100
|
|
|
'<div class="%1$s"><iframe src="%2$s" frameborder="0" style="border:0" scrolling="no" width="%3$d" height="%4$d"></iframe></div>', |
101
|
|
|
esc_attr( $classes ), |
102
|
|
|
esc_url( $url ), |
103
|
|
|
absint( $width ), |
104
|
|
|
absint( $height ) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.