Completed
Push — update/base-styles-210 ( 2e278b...ad767b )
by Jeremy
22:25 queued 13:15
created

donations.php ➔ load_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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