Completed
Push — instant-search-master ( 404687...5ddf14 )
by
unknown
10:34 queued 04:11
created

revue.php ➔ jetpack_get_revue_button()   F

Complexity

Conditions 20
Paths 864

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

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