Completed
Push — add/sync-partial-sync-checksum... ( 93f4e2...c9ed3f )
by
unknown
253:26 queued 243:12
created

donations.php ➔ render_block()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 2
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
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
		)
30
	);
31
}
32
add_action( 'init', __NAMESPACE__ . '\register_block' );
33
34
/**
35
 * Donations block dynamic rendering.
36
 *
37
 * @param array  $attr    Array containing the Donations block attributes.
38
 * @param string $content String containing the Donations block content.
39
 *
40
 * @return string
41
 */
42
function render_block( $attr, $content ) {
43
	Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) );
44
45
	require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
46
	add_thickbox();
47
48
	$donations = array(
49
		'one-time' => $attr['oneTimeDonation'],
50
		'monthly'  => $attr['monthlyDonation'],
51
		'annual'   => $attr['annualDonation'],
52
	);
53
	foreach ( $donations as $interval => $donation ) {
54
		if ( ! $donation['show'] ) {
55
			continue;
56
		}
57
		$plan_id = (int) $donation['planId'];
58
		$plan    = get_post( $plan_id );
59
		if ( ! $plan || is_wp_error( $plan ) ) {
60
			continue;
61
		}
62
63
		$url     = \Jetpack_Memberships::get_instance()->get_subscription_url( $plan_id );
64
		$content = preg_replace( '/(donations__donate-button donations__' . $interval . '-item")/i', '$1 href="' . esc_url( $url ) . '"', $content );
65
	}
66
67
	return $content;
68
}
69