Completed
Push — update/react-router-5 ( 547899...3b7c17 )
by Yaroslav
07:46
created

button.php ➔ get_attribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
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
	$classes   = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
49
50
	$dom    = new \DOMDocument();
51
	$button = $dom->createElement( $element, $content );
0 ignored issues
show
Unused Code introduced by
$button is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
53
	if ( 'input' === $element ) {
54
		$button = $dom->createElement( 'input' );
55
56
		$attribute        = $dom->createAttribute( 'value' );
57
		$attribute->value = $text;
58
		$button->appendChild( $attribute );
59
	} else {
60
		$button = $dom->createElement( $element, $text );
61
	}
62
63
	$attribute        = $dom->createAttribute( 'class' );
64
	$attribute->value = get_button_classes( $attributes );
65
	$button->appendChild( $attribute );
66
67
	$button_styles = get_button_styles( $attributes );
68
	if ( ! empty( $button_styles ) ) {
69
		$attribute        = $dom->createAttribute( 'style' );
70
		$attribute->value = $button_styles;
71
		$button->appendChild( $attribute );
72
	}
73
74
	$attribute        = $dom->createAttribute( 'data-id-attr' );
75
	$attribute->value = empty( $unique_id ) ? 'placeholder' : $unique_id;
76
	$button->appendChild( $attribute );
77
	if ( $unique_id ) {
78
		$attribute        = $dom->createAttribute( 'id' );
79
		$attribute->value = $unique_id;
80
		$button->appendChild( $attribute );
81
	}
82
83
	if ( 'a' === $element ) {
84
		$attribute        = $dom->createAttribute( 'href' );
85
		$attribute->value = get_attribute( $attributes, 'url' );
86
		$button->appendChild( $attribute );
87
88
		$attribute        = $dom->createAttribute( 'target' );
89
		$attribute->value = '_blank';
90
		$button->appendChild( $attribute );
91
92
		$attribute        = $dom->createAttribute( 'role' );
93
		$attribute->value = 'button';
94
		$button->appendChild( $attribute );
95
96
		$attribute        = $dom->createAttribute( 'rel' );
97
		$attribute->value = 'noopener noreferrer';
98
		$button->appendChild( $attribute );
99
	} elseif ( 'button' === $element || 'input' === $element ) {
100
		$attribute        = $dom->createAttribute( 'type' );
101
		$attribute->value = 'submit';
102
		$button->appendChild( $attribute );
103
	}
104
105
	$dom->appendChild( $button );
106
107
	// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
108
	return '<div class="' . esc_attr( $classes ) . '">' . $dom->saveHTML() . '</div>';
109
}
110
111
/**
112
 * Get the Button block classes.
113
 *
114
 * @param array $attributes Array containing the block attributes.
115
 *
116
 * @return string
117
 */
118
function get_button_classes( $attributes ) {
119
	$classes                     = array( 'wp-block-button__link' );
120
	$has_class_name              = array_key_exists( 'className', $attributes );
121
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
122
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
123
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
124
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
125
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
126
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
127
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
128
129
	if ( $has_class_name ) {
130
		$classes[] = $attributes['className'];
131
	}
132
133
	if ( $has_named_text_color || $has_custom_text_color ) {
134
		$classes[] = 'has-text-color';
135
	}
136
	if ( $has_named_text_color ) {
137
		$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
138
	}
139
140
	if (
141
		$has_named_background_color ||
142
		$has_custom_background_color ||
143
		$has_named_gradient ||
144
		$has_custom_gradient
145
	) {
146
		$classes[] = 'has-background';
147
	}
148
	if ( $has_named_background_color && ! $has_custom_gradient ) {
149
		$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
150
	}
151
	if ( $has_named_gradient ) {
152
		$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
153
	}
154
155
	// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
156
	if ( $has_border_radius && 0 == $attributes['borderRadius'] ) {
157
		$classes[] = 'no-border-radius';
158
	}
159
160
	return implode( ' ', $classes );
161
}
162
163
/**
164
 * Get the Button block styles.
165
 *
166
 * @param array $attributes Array containing the block attributes.
167
 *
168
 * @return string
169
 */
170
function get_button_styles( $attributes ) {
171
	$styles                      = array();
172
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
173
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
174
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
175
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
176
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
177
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
178
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
179
180
	if ( ! $has_named_text_color && $has_custom_text_color ) {
181
		$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
182
	}
183
184
	if ( ! $has_named_background_color && ! $has_named_gradient && $has_custom_gradient ) {
185
		$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
186
	}
187
188 View Code Duplication
	if (
189
		$has_custom_background_color &&
190
		! $has_named_background_color &&
191
		! $has_named_gradient &&
192
		! $has_custom_gradient
193
	) {
194
		$styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
195
	}
196
197
	// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
198 View Code Duplication
	if ( $has_border_radius && 0 != $attributes['borderRadius'] ) {
199
		$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
200
	}
201
202
	return implode( ' ', $styles );
203
}
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