Completed
Push — phpcs/pr-1 ( 512184...939cae )
by
unknown
09:45
created

donations.php ➔ render_block()   C

Complexity

Conditions 12
Paths 129

Size

Total Lines 142

Duplication

Lines 18
Ratio 12.68 %

Importance

Changes 0
Metric Value
cc 12
nc 129
nop 2
dl 18
loc 142
rs 5.38
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
 * Donations Block.
4
 *
5
 * @since 8.x
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\Donations;
11
12
use Automattic\Jetpack\Blocks;
13
use Jetpack_Gutenberg;
14
15
const FEATURE_NAME = 'donations';
16
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
17
18
/**
19
 * Registers the block for use in Gutenberg
20
 * This is done via an action so that we can disable
21
 * registration if we need to.
22
 */
23
function register_block() {
24
	Blocks::jetpack_register_block(
25
		BLOCK_NAME,
26
		array(
27
			'render_callback' => __NAMESPACE__ . '\render_block',
28
			'plan_check'      => true,
29
			'attributes'      => array(
30
				'currency'         => array(
31
					'type'    => 'string',
32
					'default' => 'USD',
33
				),
34
				'oneTimeDonation'  => array(
35
					'type'    => 'object',
36
					'default' => array(
37
						'show'       => true,
38
						'planId'     => null,
39
						'amounts'    => array( 5, 15, 100 ),
40
						'heading'    => __( 'Make a one-time donation', 'jetpack' ),
41
						'extraText'  => __( 'Your contribution is appreciated.', 'jetpack' ),
42
						'buttonText' => __( 'Donate', 'jetpack' ),
43
					),
44
				),
45
				'monthlyDonation'  => array(
46
					'type'    => 'object',
47
					'default' => array(
48
						'show'       => true,
49
						'planId'     => null,
50
						'amounts'    => array( 5, 15, 100 ),
51
						'heading'    => __( 'Make a monthly donation', 'jetpack' ),
52
						'extraText'  => __( 'Your contribution is appreciated.', 'jetpack' ),
53
						'buttonText' => __( 'Donate monthly', 'jetpack' ),
54
					),
55
				),
56
				'annualDonation'   => array(
57
					'type'    => 'object',
58
					'default' => array(
59
						'show'       => true,
60
						'planId'     => null,
61
						'amounts'    => array( 5, 15, 100 ),
62
						'heading'    => __( 'Make a yearly donation', 'jetpack' ),
63
						'extraText'  => __( 'Your contribution is appreciated.', 'jetpack' ),
64
						'buttonText' => __( 'Donate yearly', 'jetpack' ),
65
					),
66
				),
67
				'showCustomAmount' => array(
68
					'type'    => 'boolean',
69
					'default' => true,
70
				),
71
				'chooseAmountText' => array(
72
					'type'    => 'string',
73
					'default' => __( 'Choose an amount', 'jetpack' ),
74
				),
75
				'customAmountText' => array(
76
					'type'    => 'string',
77
					'default' => __( 'Or enter a custom amount', 'jetpack' ),
78
				),
79
				'fallbackLinkUrl'  => array(
80
					'type' => 'string',
81
				),
82
			),
83
		)
84
	);
85
}
86
add_action( 'init', __NAMESPACE__ . '\register_block' );
87
88
/**
89
 * Donations block dynamic rendering.
90
 *
91
 * @param array  $attr    Array containing the Donations block attributes.
92
 * @param string $content String containing the Donations block content.
93
 *
94
 * @return string
95
 */
96
function render_block( $attr, $content ) {
97
	// Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
98
	if ( ! jetpack_is_frontend() ) {
99
		return $content;
100
	}
101
102
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) );
103
	add_thickbox();
104
105
	require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
106
	jetpack_require_lib( 'class-jetpack-currencies' );
107
108
	$donations = array(
109
		'one-time' => array_merge(
110
			array(
111
				'title' => __( 'One-Time', 'jetpack' ),
112
				'class' => 'donations__one-time-item',
113
			),
114
			$attr['oneTimeDonation']
115
		),
116
	);
117 View Code Duplication
	if ( $attr['monthlyDonation']['show'] ) {
118
		$donations['1 month'] = array_merge(
119
			array(
120
				'title' => __( 'Monthly', 'jetpack' ),
121
				'class' => 'donations__monthly-item',
122
			),
123
			$attr['monthlyDonation']
124
		);
125
	}
126 View Code Duplication
	if ( $attr['annualDonation']['show'] ) {
127
		$donations['1 year'] = array_merge(
128
			array(
129
				'title' => __( 'Yearly', 'jetpack' ),
130
				'class' => 'donations__annual-item',
131
			),
132
			$attr['annualDonation']
133
		);
134
	}
135
136
	$currency   = $attr['currency'];
137
	$nav        = '';
138
	$headings   = '';
139
	$amounts    = '';
140
	$extra_text = '';
141
	$buttons    = '';
142
	foreach ( $donations as $interval => $donation ) {
143
		$plan_id = (int) $donation['planId'];
144
		$plan    = get_post( $plan_id );
145
		if ( ! $plan || is_wp_error( $plan ) ) {
146
			continue;
147
		}
148
149
		if ( count( $donations ) > 1 ) {
150
			if ( ! $nav ) {
151
				$nav .= '<div class="donations__nav">';
152
			}
153
			$nav .= sprintf(
154
				'<div role="button" tabindex="0" class="donations__nav-item" data-interval="%1$s">%2$s</div>',
155
				esc_attr( $interval ),
156
				esc_html( $donation['title'] )
157
			);
158
		}
159
		$headings .= sprintf(
160
			'<h4 class="%1$s">%2$s</h4>',
161
			esc_attr( $donation['class'] ),
162
			wp_kses_post( $donation['heading'] )
163
		);
164
		$amounts  .= sprintf(
165
			'<div class="donations__amounts %s">',
166
			esc_attr( $donation['class'] )
167
		);
168
		foreach ( $donation['amounts'] as $amount ) {
169
			$amounts .= sprintf(
170
				'<div class="donations__amount" data-amount="%1$s">%2$s</div>',
171
				esc_attr( $amount ),
172
				esc_html( \Jetpack_Currencies::format_price( $amount, $currency ) )
173
			);
174
		}
175
		$amounts    .= '</div>';
176
		$extra_text .= sprintf(
177
			'<p class="%1$s">%2$s</p>',
178
			esc_attr( $donation['class'] ),
179
			wp_kses_post( $donation['extraText'] )
180
		);
181
		$buttons    .= sprintf(
182
			'<a class="wp-block-button__link donations__donate-button %1$s" href="%2$s">%3$s</a>',
183
			esc_attr( $donation['class'] ),
184
			esc_url( \Jetpack_Memberships::get_instance()->get_subscription_url( $plan_id ) ),
185
			wp_kses_post( $donation['buttonText'] )
186
		);
187
	}
188
	if ( $nav ) {
189
		$nav .= '</div>';
190
	}
191
192
	$custom_amount = '';
193
	if ( $attr['showCustomAmount'] ) {
194
		$custom_amount        .= sprintf(
195
			'<p>%s</p>',
196
			wp_kses_post( $attr['customAmountText'] )
197
		);
198
		$default_custom_amount = \Jetpack_Memberships::SUPPORTED_CURRENCIES[ $currency ] * 100;
199
		$custom_amount        .= sprintf(
200
			'<div class="donations__amount donations__custom-amount">
201
				%1$s
202
				<div class="donations__amount-value" data-currency="%2$s" data-empty-text="%3$s"></div>
203
			</div>',
204
			esc_html( \Jetpack_Currencies::CURRENCIES[ $attr['currency'] ]['symbol'] ),
205
			esc_attr( $attr['currency'] ),
206
			esc_attr( \Jetpack_Currencies::format_price( $default_custom_amount, $currency, false ) )
207
		);
208
	}
209
210
	return sprintf(
211
		'
212
<div class="%1$s">
213
	<div class="donations__container">
214
	%2$s
215
	<div class="donations__content">
216
		<div class="donations__tab">
217
			%3$s
218
			<p>%4$s</p>
219
			%5$s
220
			%6$s
221
			<hr class="donations__separator">
222
			%7$s
223
			%8$s
224
		</div>
225
	</div>
226
</div>
227
',
228
		esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ),
229
		$nav,
230
		$headings,
231
		$attr['chooseAmountText'],
232
		$amounts,
233
		$custom_amount,
234
		$extra_text,
235
		$buttons
236
	);
237
}
238
239
/**
240
 * Determine if AMP should be disabled on posts having Donations blocks.
241
 *
242
 * @param bool    $skip Skipped.
243
 * @param int     $post_id Post ID.
244
 * @param WP_Post $post Post.
245
 *
246
 * @return bool Whether to skip the post from AMP.
247
 */
248 View Code Duplication
function amp_skip_post( $skip, $post_id, $post ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
	// When AMP is on standard mode, there are no non-AMP posts to link to where the donation can be completed, so let's
250
	// prevent the post from being available in AMP.
251
	if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( BLOCK_NAME, $post->post_content ) ) {
252
		return true;
253
	}
254
	return $skip;
255
}
256
add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );
257