Completed
Push — e2e/e2e-runner-groups ( 261a6b...698261 )
by
unknown
65:29 queued 54:32
created

button.php ➔ enqueue_existing_button_style_dependency()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
	// The Jetpack Button block depends on the core button block styles.
43
	// The following ensures that those styles are enqueued when rendering this block.
44
	enqueue_existing_button_style_dependency( 'core/button' );
45
	enqueue_existing_button_style_dependency( 'core/buttons' );
46
47
	Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
48
49
	if ( $save_in_post_content || ! class_exists( 'DOMDocument' ) ) {
50
		return $content;
51
	}
52
53
	$element   = get_attribute( $attributes, 'element' );
54
	$text      = get_attribute( $attributes, 'text' );
55
	$unique_id = get_attribute( $attributes, 'uniqueId' );
56
	$url       = get_attribute( $attributes, 'url' );
57
	$classes   = Blocks::classes( FEATURE_NAME, $attributes, array( 'wp-block-button' ) );
58
59
	$button_classes = get_button_classes( $attributes );
60
	$button_styles  = get_button_styles( $attributes );
61
	$wrapper_styles = get_button_wrapper_styles( $attributes );
62
63
	$wrapper_attributes = sprintf( ' class="%s" style="%s"', esc_attr( $classes ), esc_attr( $wrapper_styles ) );
64
	$button_attributes  = sprintf( ' class="%s" style="%s"', esc_attr( $button_classes ), esc_attr( $button_styles ) );
65
66
	if ( empty( $unique_id ) ) {
67
		$button_attributes .= ' data-id-attr="placeholder"';
68
	} else {
69
		$button_attributes .= sprintf( ' data-id-attr="%1$s" id="%1$s"', esc_attr( $unique_id ) );
70
	}
71
72
	if ( 'a' === $element ) {
73
		$button_attributes .= sprintf( ' href="%s" target="_blank" role="button" rel="noopener noreferrer"', esc_url( $url ) );
74
	} elseif ( 'button' === $element ) {
75
		$button_attributes .= ' type="submit"';
76
	} elseif ( 'input' === $element ) {
77
		$button_attributes .= sprintf( ' type="submit" value="%s"', wp_strip_all_tags( $text, true ) );
78
	}
79
80
	$button = 'input' === $element
81
		? '<' . $element . $button_attributes . ' />'
82
		: '<' . $element . $button_attributes . '>' . $text . '</' . $element . '>';
83
84
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
85
	return '<div "' . $wrapper_attributes . '">' . $button . '</div>';
86
}
87
88
/**
89
 * Get the Button block classes.
90
 *
91
 * @param array $attributes Array containing the block attributes.
92
 *
93
 * @return string
94
 */
95
function get_button_classes( $attributes ) {
96
	$classes                     = array( 'wp-block-button__link' );
97
	$has_class_name              = array_key_exists( 'className', $attributes );
98
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
99
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
100
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
101
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
102
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
103
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
104
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
105
106
	if ( $has_class_name ) {
107
		$classes[] = $attributes['className'];
108
	}
109
110
	if ( $has_named_text_color || $has_custom_text_color ) {
111
		$classes[] = 'has-text-color';
112
	}
113
	if ( $has_named_text_color ) {
114
		$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
115
	}
116
117
	if (
118
		$has_named_background_color ||
119
		$has_custom_background_color ||
120
		$has_named_gradient ||
121
		$has_custom_gradient
122
	) {
123
		$classes[] = 'has-background';
124
	}
125
	if ( $has_named_background_color && ! $has_custom_gradient ) {
126
		$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
127
	}
128
	if ( $has_named_gradient ) {
129
		$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
130
	}
131
132
	// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
133
	if ( $has_border_radius && 0 == $attributes['borderRadius'] ) {
134
		$classes[] = 'no-border-radius';
135
	}
136
137
	return implode( ' ', $classes );
138
}
139
140
/**
141
 * Get the Button block styles.
142
 *
143
 * @param array $attributes Array containing the block attributes.
144
 *
145
 * @return string
146
 */
147
function get_button_styles( $attributes ) {
148
	$styles                      = array();
149
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
150
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
151
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
152
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
153
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
154
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
155
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
156
	$has_width                   = array_key_exists( 'width', $attributes );
157
158
	if ( ! $has_named_text_color && $has_custom_text_color ) {
159
		$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
160
	}
161
162
	if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) {
163
		$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
164
	}
165
166 View Code Duplication
	if (
167
		$has_custom_background_color &&
168
		! $has_named_background_color &&
169
		! $has_named_gradient &&
170
		! $has_custom_gradient
171
	) {
172
		$styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
173
	}
174
175
	// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
176 View Code Duplication
	if ( $has_border_radius && 0 != $attributes['borderRadius'] ) {
177
		$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
178
	}
179
180
	if ( $has_width ) {
181
		$styles[] = sprintf( 'width: %s;', $attributes['width'] );
182
		$styles[] = 'max-width: 100%';
183
	}
184
185
	return implode( ' ', $styles );
186
}
187
188
/**
189
 * Get the Button wrapper block styles.
190
 *
191
 * @param array $attributes Array containing the block attributes.
192
 *
193
 * @return string
194
 */
195
function get_button_wrapper_styles( $attributes ) {
196
	$styles    = array();
197
	$has_width = array_key_exists( 'width', $attributes );
198
199
	if ( $has_width ) {
200
		$styles[] = 'max-width: 100%';
201
	}
202
203
	return implode( ' ', $styles );
204
}
205
206
/**
207
 * Get filtered attributes.
208
 *
209
 * @param array  $attributes     Array containing the Button block attributes.
210
 * @param string $attribute_name String containing the attribute name to get.
211
 *
212
 * @return string
213
 */
214
function get_attribute( $attributes, $attribute_name ) {
215
	if ( isset( $attributes[ $attribute_name ] ) ) {
216
		return $attributes[ $attribute_name ];
217
	}
218
219
	$default_attributes = array(
220
		'url'               => '#',
221
		'element'           => 'a',
222
		'saveInPostContent' => false,
223
	);
224
225
	if ( isset( $default_attributes[ $attribute_name ] ) ) {
226
		return $default_attributes[ $attribute_name ];
227
	}
228
}
229
230
/**
231
 * Enqueue style for an existing block.
232
 *
233
 * The Jetpack Button block depends on styles from the core button block.
234
 * In case that block is not already within the post content, we can use
235
 * this function to ensure the block's style assets are enqueued.
236
 *
237
 * @param string $block_name Block type name including namespace.
238
 */
239
function enqueue_existing_button_style_dependency( $block_name ) {
240
	$existing_block = \WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
241
	if ( isset( $existing_block ) && ! empty( $existing_block->style ) ) {
242
		wp_enqueue_style( $existing_block->style );
243
	}
244
}
245