Completed
Push — renovate/webpack-cli-3.x ( a49c46...d89017 )
by
unknown
44:08 queued 37:51
created

revue.php ➔ jetpack_render_revue_block()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 89
rs 8.24
c 0
b 0
f 0

How to fix   Long Method   

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
jetpack_register_block(
11
	'jetpack/revue',
12
	array(
13
		'render_callback' => 'jetpack_render_revue_block',
14
	)
15
);
16
17
/**
18
 * Revue block render callback.
19
 *
20
 * @param array $attributes Array containing the Revue block attributes.
21
 *
22
 * @return string
23
 */
24
function jetpack_render_revue_block( $attributes ) {
25
	if ( ! array_key_exists( 'revueUsername', $attributes ) ) {
26
		return '';
27
	}
28
29
	$email_label            = jetpack_get_revue_attribute( 'emailLabel', $attributes );
30
	$email_placeholder      = jetpack_get_revue_attribute( 'emailPlaceholder', $attributes );
31
	$first_name_label       = jetpack_get_revue_attribute( 'firstNameLabel', $attributes );
32
	$first_name_placeholder = jetpack_get_revue_attribute( 'firstNamePlaceholder', $attributes );
33
	$first_name_show        = jetpack_get_revue_attribute( 'firstNameShow', $attributes );
34
	$last_name_label        = jetpack_get_revue_attribute( 'lastNameLabel', $attributes );
35
	$last_name_placeholder  = jetpack_get_revue_attribute( 'lastNamePlaceholder', $attributes );
36
	$last_name_show         = jetpack_get_revue_attribute( 'lastNameShow', $attributes );
37
	$url                    = sprintf( 'https://www.getrevue.co/profile/%s/add_subscriber', $attributes['revueUsername'] );
38
	$base_class             = Jetpack_Gutenberg::block_classes( 'revue', array() ) . '__';
39
	$classes                = Jetpack_Gutenberg::block_classes( 'revue', $attributes );
40
41
	Jetpack_Gutenberg::load_assets_as_required( 'revue' );
42
43
	ob_start();
44
	?>
45
46
<div class="<?php echo esc_attr( $classes ); ?>">
47
	<form
48
		action="<?php echo esc_url( $url ); ?>"
49
		class="<?php echo esc_attr( $base_class . 'form is-visible' ); ?>"
50
		method="post"
51
		name="revue-form"
52
		target="_blank"
53
	>
54
		<div>
55
			<label>
56
				<?php echo esc_html( $email_label ); ?>
57
				<span class="required"><?php esc_html_e( '(required)', 'jetpack' ); ?></span>
58
				<input
59
					class="<?php echo esc_attr( $base_class . 'email' ); ?>"
60
					name="member[email]"
61
					placeholder="<?php echo esc_attr( $email_placeholder ); ?>"
62
					required
63
					type="email"
64
				/>
65
			</label>
66
		</div>
67
		<?php if ( $first_name_show ) : ?>
68
			<div>
69
				<label>
70
					<?php echo esc_html( $first_name_label ); ?>
71
					<input
72
						class="<?php echo esc_attr( $base_class . 'first-name' ); ?>"
73
						name="member[first_name]"
74
						placeholder="<?php echo esc_attr( $first_name_placeholder ); ?>"
75
						type="text"
76
					/>
77
				</label>
78
			</div>
79
			<?php
80
			endif;
81
		if ( $last_name_show ) :
82
			?>
83
			<div>
84
				<label>
85
					<?php echo esc_html( $last_name_label ); ?>
86
					<input
87
						class="<?php echo esc_attr( $base_class . 'last-name' ); ?>"
88
						name="member[last_name]"
89
						placeholder="<?php echo esc_attr( $last_name_placeholder ); ?>"
90
						type="text"
91
					/>
92
				</label>
93
			</div>
94
			<?php
95
			endif;
96
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
97
			echo jetpack_get_revue_button( $attributes );
98
		?>
99
	</form>
100
	<div class="<?php echo esc_attr( $base_class . 'message' ); ?>">
101
		<p>
102
			<strong><?php esc_html_e( 'Subscription received!', 'jetpack' ); ?></strong>
103
		</p>
104
		<p>
105
			<?php esc_html_e( 'Please check your email to confirm your newsletter subscription.', 'jetpack' ); ?>
106
		</p>
107
	</div>
108
</div>
109
110
	<?php
111
	return ob_get_clean();
112
}
113
114
/**
115
 * Create the Revue subscribe button.
116
 *
117
 * @see https://github.com/WordPress/gutenberg/blob/015555fcdf648b13af57e08cee60bf3f3501ff63/packages/block-library/src/navigation/index.php
118
 *
119
 * @param array $attributes Array containing the Revue block attributes.
120
 *
121
 * @return string
122
 */
123
function jetpack_get_revue_button( $attributes ) {
124
	$classes = array( 'wp-block-button__link' );
125
	$styles  = array();
126
127
	$text                        = jetpack_get_revue_attribute( 'text', $attributes );
128
	$has_class_name              = array_key_exists( 'className', $attributes );
129
	$has_named_text_color        = array_key_exists( 'textColor', $attributes );
130
	$has_custom_text_color       = array_key_exists( 'customTextColor', $attributes );
131
	$has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
132
	$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
133
	$has_named_gradient          = array_key_exists( 'gradient', $attributes );
134
	$has_custom_gradient         = array_key_exists( 'customGradient', $attributes );
135
	$has_border_radius           = array_key_exists( 'borderRadius', $attributes );
136
137
	if ( $has_class_name ) {
138
		$classes[] = $attributes['className'];
139
	}
140
141
	if ( $has_named_text_color || $has_custom_text_color ) {
142
		$classes[] = 'has-text-color';
143
	}
144
	if ( $has_named_text_color ) {
145
		$classes[] = sprintf( 'has-%s-color', $attributes['textColor'] );
146
	} elseif ( $has_custom_text_color ) {
147
		$styles[] = sprintf( 'color: %s;', $attributes['customTextColor'] );
148
	}
149
150
	if (
151
		$has_named_background_color ||
152
		$has_custom_background_color ||
153
		$has_named_gradient ||
154
		$has_custom_gradient
155
	) {
156
		$classes[] = 'has-background';
157
	}
158
	if ( $has_named_background_color && ! $has_custom_gradient ) {
159
		$classes[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
160
	}
161
	if ( $has_named_gradient ) {
162
		$classes[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
163
	} elseif ( $has_custom_gradient ) {
164
		$styles[] = sprintf( 'background: %s;', $attributes['customGradient'] );
165
	}
166
	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
	if ( $has_border_radius ) {
176
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
177
		if ( 0 == $attributes['borderRadius'] ) {
178
			$classes[] = 'no-border-radius';
179
		} else {
180
			$styles[] = sprintf( 'border-radius: %spx;', $attributes['borderRadius'] );
181
		}
182
	}
183
184
	ob_start();
185
	?>
186
187
<div class="wp-block-button">
188
	<button
189
		class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
190
		name="member[subscribe]"
191
		style="<?php echo esc_attr( implode( ' ', $styles ) ); ?>"
192
		type="submit"
193
	>
194
		<?php echo wp_kses_post( $text ); ?>
195
	</button>
196
</div>
197
198
	<?php
199
	return ob_get_clean();
200
}
201
202
/**
203
 * Get Revue block attribute.
204
 *
205
 * @param string $attribute  String containing the attribute name to get.
206
 * @param array  $attributes Array containing the Revue block attributes.
207
 *
208
 * @return mixed
209
 */
210
function jetpack_get_revue_attribute( $attribute, $attributes ) {
211
	if ( array_key_exists( $attribute, $attributes ) ) {
212
		return $attributes[ $attribute ];
213
	}
214
215
	$default_attributes = array(
216
		'text'                 => __( 'Subscribe', 'jetpack' ),
217
		'emailLabel'           => __( 'Email address', 'jetpack' ),
218
		'emailPlaceholder'     => __( 'Enter your email address', 'jetpack' ),
219
		'firstNameLabel'       => __( 'First name', 'jetpack' ),
220
		'firstNamePlaceholder' => __( 'Enter your first name', 'jetpack' ),
221
		'firstNameShow'        => true,
222
		'lastNameLabel'        => __( 'Last name', 'jetpack' ),
223
		'lastNamePlaceholder'  => __( 'Enter your last name', 'jetpack' ),
224
		'lastNameShow'         => true,
225
	);
226
227
	if ( array_key_exists( $attribute, $default_attributes ) ) {
228
		return $default_attributes[ $attribute ];
229
	}
230
}
231