Completed
Push — ignore/lazy-images-linting-pac... ( 3c044f...b5c515 )
by Jeremy
367:06 queued 352:42
created

revue.php ➔ get_deprecated_v1_revue_button()   F

Complexity

Conditions 20
Paths 864

Size

Total Lines 78

Duplication

Lines 16
Ratio 20.51 %

Importance

Changes 0
Metric Value
cc 20
nc 864
nop 1
dl 16
loc 78
rs 0.1888
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Revue Block.
4
 *
5
 * @since 8.3.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Revue;
11
12
use Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'revue';
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
 * Revue block render callback.
33
 *
34
 * @param array  $attributes Array containing the Revue block attributes.
35
 * @param string $content    The Revue block content.
36
 *
37
 * @return string
38
 */
39
function render_block( $attributes, $content ) {
40
	if ( ! array_key_exists( 'revueUsername', $attributes ) ) {
41
		return '';
42
	}
43
44
	$email_label            = get_revue_attribute( 'emailLabel', $attributes );
45
	$email_placeholder      = get_revue_attribute( 'emailPlaceholder', $attributes );
46
	$first_name_label       = get_revue_attribute( 'firstNameLabel', $attributes );
47
	$first_name_placeholder = get_revue_attribute( 'firstNamePlaceholder', $attributes );
48
	$first_name_show        = get_revue_attribute( 'firstNameShow', $attributes );
49
	$last_name_label        = get_revue_attribute( 'lastNameLabel', $attributes );
50
	$last_name_placeholder  = get_revue_attribute( 'lastNamePlaceholder', $attributes );
51
	$last_name_show         = get_revue_attribute( 'lastNameShow', $attributes );
52
	$url                    = sprintf( 'https://www.getrevue.co/profile/%s/add_subscriber', $attributes['revueUsername'] );
53
	$base_class             = Blocks::classes( FEATURE_NAME, array() ) . '__';
54
	$classes                = Blocks::classes( FEATURE_NAME, $attributes );
55
56
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME );
57
58
	ob_start();
59
	?>
60
61
<div class="<?php echo esc_attr( $classes ); ?>">
62
	<form
63
		action="<?php echo esc_url( $url ); ?>"
64
		class="<?php echo esc_attr( $base_class . 'form is-visible' ); ?>"
65
		method="post"
66
		name="revue-form"
67
		target="_blank"
68
	>
69
		<div>
70
			<label>
71
				<?php echo esc_html( $email_label ); ?>
72
				<span class="required"><?php esc_html_e( '(required)', 'jetpack' ); ?></span>
73
				<input
74
					class="<?php echo esc_attr( $base_class . 'email' ); ?>"
75
					name="member[email]"
76
					placeholder="<?php echo esc_attr( $email_placeholder ); ?>"
77
					required
78
					type="email"
79
				/>
80
			</label>
81
		</div>
82
		<?php if ( $first_name_show ) : ?>
83
			<div>
84
				<label>
85
					<?php echo esc_html( $first_name_label ); ?>
86
					<input
87
						class="<?php echo esc_attr( $base_class . 'first-name' ); ?>"
88
						name="member[first_name]"
89
						placeholder="<?php echo esc_attr( $first_name_placeholder ); ?>"
90
						type="text"
91
					/>
92
				</label>
93
			</div>
94
			<?php
95
			endif;
96
		if ( $last_name_show ) :
97
			?>
98
			<div>
99
				<label>
100
					<?php echo esc_html( $last_name_label ); ?>
101
					<input
102
						class="<?php echo esc_attr( $base_class . 'last-name' ); ?>"
103
						name="member[last_name]"
104
						placeholder="<?php echo esc_attr( $last_name_placeholder ); ?>"
105
						type="text"
106
					/>
107
				</label>
108
			</div>
109
			<?php
110
		endif;
111
112
		// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
113
		if ( false !== strpos( $content, 'wp-block-jetpack-revue__fallback' ) ) {
114
			echo $content;
115
		} else {
116
			echo get_deprecated_v1_revue_button( $attributes );
117
		}
118
		// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
119
		?>
120
	</form>
121
	<div class="<?php echo esc_attr( $base_class . 'message' ); ?>">
122
		<p>
123
			<strong><?php esc_html_e( 'Subscription received!', 'jetpack' ); ?></strong>
124
		</p>
125
		<p>
126
			<?php esc_html_e( 'Please check your email to confirm your newsletter subscription.', 'jetpack' ); ?>
127
		</p>
128
	</div>
129
</div>
130
131
	<?php
132
	return ob_get_clean();
133
}
134
135
/**
136
 * Get Revue block attribute.
137
 *
138
 * @param string $attribute  String containing the attribute name to get.
139
 * @param array  $attributes Array containing the Revue block attributes.
140
 *
141
 * @return mixed
142
 */
143
function get_revue_attribute( $attribute, $attributes ) {
144
	if ( array_key_exists( $attribute, $attributes ) ) {
145
		return $attributes[ $attribute ];
146
	}
147
148
	$default_attributes = array(
149
		'text'                 => __( 'Subscribe', 'jetpack' ),
150
		'emailLabel'           => __( 'Email address', 'jetpack' ),
151
		'emailPlaceholder'     => __( 'Enter your email address', 'jetpack' ),
152
		'firstNameLabel'       => __( 'First name', 'jetpack' ),
153
		'firstNamePlaceholder' => __( 'Enter your first name', 'jetpack' ),
154
		'firstNameShow'        => true,
155
		'lastNameLabel'        => __( 'Last name', 'jetpack' ),
156
		'lastNamePlaceholder'  => __( 'Enter your last name', 'jetpack' ),
157
		'lastNameShow'         => true,
158
	);
159
160
	if ( array_key_exists( $attribute, $default_attributes ) ) {
161
		return $default_attributes[ $attribute ];
162
	}
163
}
164
165
/**
166
 * DEPRECATED V1
167
 */
168
169
/**
170
 * Create the Revue subscribe button.
171
 *
172
 * @param array $attributes Array containing the Revue block attributes.
173
 *
174
 * @return string
175
 */
176
function get_deprecated_v1_revue_button( $attributes ) {
177
	$classes = array( 'wp-block-button__link' );
178
	$styles  = array();
179
180
	$text                        = get_revue_attribute( 'text', $attributes );
181
	$has_class_name              = array_key_exists( 'className', $attributes );
182
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
183
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
184
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
185
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
186
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
187
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
188
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
189
190
	if ( $has_class_name ) {
191
		$classes[] = $attributes['className'];
192
	}
193
194
	if ( $has_named_text_color || $has_custom_text_color ) {
195
		$classes[] = 'has-text-color';
196
	}
197
	if ( $has_named_text_color ) {
198
		$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
199
	} elseif ( $has_custom_text_color ) {
200
		$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
201
	}
202
203
	if (
204
		$has_named_background_color ||
205
		$has_custom_background_color ||
206
		$has_named_gradient ||
207
		$has_custom_gradient
208
	) {
209
		$classes[] = 'has-background';
210
	}
211
	if ( $has_named_background_color && ! $has_custom_gradient ) {
212
		$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
213
	}
214
	if ( $has_named_gradient ) {
215
		$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
216
	} elseif ( $has_custom_gradient ) {
217
		$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
218
	}
219 View Code Duplication
	if (
220
		$has_custom_background_color &&
221
		! $has_named_background_color &&
222
		! $has_named_gradient &&
223
		! $has_custom_gradient
224
	) {
225
		$styles[] = sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
226
	}
227
228 View Code Duplication
	if ( $has_border_radius ) {
229
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
230
		if ( 0 == $attributes['borderRadius'] ) {
231
			$classes[] = 'no-border-radius';
232
		} else {
233
			$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
234
		}
235
	}
236
237
	ob_start();
238
	?>
239
240
<div class="wp-block-button">
241
	<button
242
		class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
243
		name="member[subscribe]"
244
		style="<?php echo esc_attr( implode( ' ', $styles ) ); ?>"
245
		type="submit"
246
	>
247
		<?php echo wp_kses_post( $text ); ?>
248
	</button>
249
</div>
250
251
	<?php
252
	return ob_get_clean();
253
}
254