1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Button Block. |
4
|
|
|
* |
5
|
|
|
* @since 8.5.0 |
6
|
|
|
* |
7
|
|
|
* @package automattic/jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Extensions\Button; |
11
|
|
|
|
12
|
|
|
use Automattic\Jetpack\Blocks; |
13
|
|
|
use Jetpack_Gutenberg; |
14
|
|
|
|
15
|
|
|
const FEATURE_NAME = 'button'; |
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
|
|
|
Blocks::jetpack_register_block( |
25
|
|
|
BLOCK_NAME, |
26
|
|
|
array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_block' ); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Button block render callback. |
33
|
|
|
* |
34
|
|
|
* @param array $attributes Array containing the Button block attributes. |
35
|
|
|
* @param string $content The Button block content. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
function render_block( $attributes, $content ) { |
40
|
|
|
$save_in_post_content = get_attribute( $attributes, 'saveInPostContent' ); |
41
|
|
|
|
42
|
|
|
if ( Blocks::is_amp_request() ) { |
43
|
|
|
Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ( $save_in_post_content || ! class_exists( 'DOMDocument' ) ) { |
47
|
|
|
return $content; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$element = get_attribute( $attributes, 'element' ); |
51
|
|
|
$text = get_attribute( $attributes, 'text' ); |
52
|
|
|
$unique_id = get_attribute( $attributes, 'uniqueId' ); |
53
|
|
|
$url = get_attribute( $attributes, 'url' ); |
54
|
|
|
$classes = Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-block-button' ) ); |
55
|
|
|
|
56
|
|
|
$button_classes = get_button_classes( $attributes ); |
57
|
|
|
$button_styles = get_button_styles( $attributes ); |
58
|
|
|
$wrapper_styles = get_button_wrapper_styles( $attributes ); |
59
|
|
|
|
60
|
|
|
$wrapper_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $classes ), esc_attr( $wrapper_styles ) ); |
61
|
|
|
$button_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $button_classes ), esc_attr( $button_styles ) ); |
62
|
|
|
|
63
|
|
|
if ( empty( $unique_id ) ) { |
64
|
|
|
$button_attributes .= ' data-id-attr="placeholder"'; |
65
|
|
|
} else { |
66
|
|
|
$button_attributes .= sprintf( ' data-id-attr="%1$s" id="%1$s"', esc_attr( $unique_id ) ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( 'a' === $element ) { |
70
|
|
|
$button_attributes .= sprintf( ' href="%s" target="_blank" role="button" rel="noopener noreferrer"', esc_url( $url ) ); |
71
|
|
|
} elseif ( 'button' === $element ) { |
72
|
|
|
$button_attributes .= ' type="submit"'; |
73
|
|
|
} elseif ( 'input' === $element ) { |
74
|
|
|
$button_attributes .= sprintf( ' type="submit" value="%s"', wp_strip_all_tags( $text, true ) ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$button = 'input' === $element |
78
|
|
|
? '<' . $element . $button_attributes . ' />' |
79
|
|
|
: '<' . $element . $button_attributes . '>' . $text . '</' . $element . '>'; |
80
|
|
|
|
81
|
|
|
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
82
|
|
|
return '<div "' . $wrapper_attributes . '">' . $button . '</div>'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the Button block classes. |
87
|
|
|
* |
88
|
|
|
* @param array $attributes Array containing the block attributes. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
function get_button_classes( $attributes ) { |
93
|
|
|
$classes = array( 'wp-block-button__link' ); |
94
|
|
|
$has_class_name = array_key_exists( 'className', $attributes ); |
95
|
|
|
$has_named_text_color = array_key_exists( 'textColor', $attributes ); |
96
|
|
|
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); |
97
|
|
|
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); |
98
|
|
|
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); |
99
|
|
|
$has_named_gradient = array_key_exists( 'gradient', $attributes ); |
100
|
|
|
$has_custom_gradient = array_key_exists( 'customGradient', $attributes ); |
101
|
|
|
$has_border_radius = array_key_exists( 'borderRadius', $attributes ); |
102
|
|
|
|
103
|
|
|
if ( $has_class_name ) { |
104
|
|
|
$classes[] = $attributes['className']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( $has_named_text_color || $has_custom_text_color ) { |
108
|
|
|
$classes[] = 'has-text-color'; |
109
|
|
|
} |
110
|
|
|
if ( $has_named_text_color ) { |
111
|
|
|
$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( |
115
|
|
|
$has_named_background_color || |
116
|
|
|
$has_custom_background_color || |
117
|
|
|
$has_named_gradient || |
118
|
|
|
$has_custom_gradient |
119
|
|
|
) { |
120
|
|
|
$classes[] = 'has-background'; |
121
|
|
|
} |
122
|
|
|
if ( $has_named_background_color && ! $has_custom_gradient ) { |
123
|
|
|
$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); |
124
|
|
|
} |
125
|
|
|
if ( $has_named_gradient ) { |
126
|
|
|
$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
130
|
|
|
if ( $has_border_radius && 0 == $attributes['borderRadius'] ) { |
131
|
|
|
$classes[] = 'no-border-radius'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return implode( ' ', $classes ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the Button block styles. |
139
|
|
|
* |
140
|
|
|
* @param array $attributes Array containing the block attributes. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
function get_button_styles( $attributes ) { |
145
|
|
|
$styles = array(); |
146
|
|
|
$has_named_text_color = array_key_exists( 'textColor', $attributes ); |
147
|
|
|
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); |
148
|
|
|
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); |
149
|
|
|
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); |
150
|
|
|
$has_named_gradient = array_key_exists( 'gradient', $attributes ); |
151
|
|
|
$has_custom_gradient = array_key_exists( 'customGradient', $attributes ); |
152
|
|
|
$has_border_radius = array_key_exists( 'borderRadius', $attributes ); |
153
|
|
|
$has_width = array_key_exists( 'width', $attributes ); |
154
|
|
|
|
155
|
|
|
if ( ! $has_named_text_color && $has_custom_text_color ) { |
156
|
|
|
$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) { |
160
|
|
|
$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
if ( |
164
|
|
|
$has_custom_background_color && |
165
|
|
|
! $has_named_background_color && |
166
|
|
|
! $has_named_gradient && |
167
|
|
|
! $has_custom_gradient |
168
|
|
|
) { |
169
|
|
|
$styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
173
|
|
View Code Duplication |
if ( $has_border_radius && 0 != $attributes['borderRadius'] ) { |
174
|
|
|
$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( $has_width ) { |
178
|
|
|
$styles[] = sprintf( 'width: %s;', $attributes['width'] ); |
179
|
|
|
$styles[] = 'max-width: 100%'; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return implode( ' ', $styles ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the Button wrapper block styles. |
187
|
|
|
* |
188
|
|
|
* @param array $attributes Array containing the block attributes. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
function get_button_wrapper_styles( $attributes ) { |
193
|
|
|
$styles = array(); |
194
|
|
|
$has_width = array_key_exists( 'width', $attributes ); |
195
|
|
|
|
196
|
|
|
if ( $has_width ) { |
197
|
|
|
$styles[] = 'max-width: 100%'; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return implode( ' ', $styles ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get filtered attributes. |
205
|
|
|
* |
206
|
|
|
* @param array $attributes Array containing the Button block attributes. |
207
|
|
|
* @param string $attribute_name String containing the attribute name to get. |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
function get_attribute( $attributes, $attribute_name ) { |
212
|
|
|
if ( isset( $attributes[ $attribute_name ] ) ) { |
213
|
|
|
return $attributes[ $attribute_name ]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$default_attributes = array( |
217
|
|
|
'url' => '#', |
218
|
|
|
'element' => 'a', |
219
|
|
|
'saveInPostContent' => false, |
220
|
|
|
); |
221
|
|
|
|
222
|
|
|
if ( isset( $default_attributes[ $attribute_name ] ) ) { |
223
|
|
|
return $default_attributes[ $attribute_name ]; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|