Completed
Push — try/using-another-block-button... ( d05531 )
by
unknown
06:02
created

revue.php ➔ jetpack_get_revue_button()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
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
	jetpack_require_lib( 'functions.jetpack-button-helper' );
125
126
	$classes = jetpack_get_button_classes( $attributes );
127
	$styles  = jetpack_get_button_styles( $attributes );
128
	$text    = jetpack_get_revue_attribute( 'buttonText', $attributes );
129
130
	ob_start();
131
	?>
132
133
<div class="wp-block-button">
134
	<button
135
		class="<?php echo esc_attr( $classes ); ?>"
136
		name="member[subscribe]"
137
		style="<?php echo esc_attr( $styles ); ?>"
138
		type="submit"
139
	>
140
		<?php echo wp_kses_post( $text ); ?>
141
	</button>
142
</div>
143
144
	<?php
145
	return ob_get_clean();
146
}
147
148
/**
149
 * Get Revue block attribute.
150
 *
151
 * @param string $attribute  String containing the attribute name to get.
152
 * @param array  $attributes Array containing the Revue block attributes.
153
 *
154
 * @return mixed
155
 */
156
function jetpack_get_revue_attribute( $attribute, $attributes ) {
157
	if ( array_key_exists( $attribute, $attributes ) ) {
158
		return $attributes[ $attribute ];
159
	}
160
161
	$default_attributes = array(
162
		'buttonText'           => __( 'Subscribe', 'jetpack' ),
163
		'emailLabel'           => __( 'Email address', 'jetpack' ),
164
		'emailPlaceholder'     => __( 'Enter your email address', 'jetpack' ),
165
		'firstNameLabel'       => __( 'First name', 'jetpack' ),
166
		'firstNamePlaceholder' => __( 'Enter your first name', 'jetpack' ),
167
		'firstNameShow'        => true,
168
		'lastNameLabel'        => __( 'Last name', 'jetpack' ),
169
		'lastNamePlaceholder'  => __( 'Enter your last name', 'jetpack' ),
170
		'lastNameShow'         => true,
171
	);
172
173
	if ( array_key_exists( $attribute, $default_attributes ) ) {
174
		return $default_attributes[ $attribute ];
175
	}
176
}
177