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