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