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