Completed
Push — update/pay-with-paypal-save-fa... ( 4eb26c...a6d1fd )
by
unknown
14:09 queued 05:13
created

simple-payments.php ➔ render_block()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pay with PayPal block (aka Simple Payments).
4
 *
5
 * @since 9.0.0
6
 *
7
 * @package Jetpack
8
 */
9
10
namespace Automattic\Jetpack\Extensions\SimplePayments;
11
12
use Jetpack_Simple_Payments;
13
14
const FEATURE_NAME = 'simple-payments';
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
 * Pay with PayPal block dynamic rendering.
35
 *
36
 * @param array  $attr    Array containing the block attributes.
37
 * @param string $content String containing the block content.
38
 *
39
 * @return string
40
 */
41
function render_block( $attr, $content ) {
42
	$simple_payments = Jetpack_Simple_Payments::getInstance();
43
	$simple_payments->enqueue_frontend_assets();
44
45
	if ( ! jetpack_is_frontend() ) {
46
		return $content;
47
	}
48
49
	// Augment block UI with a PayPal button if rendered on the frontend.
50
	$product_id  = $attr['productId'];
51
	$dom_id      = uniqid( "jetpack-simple-payments-{$product_id}_", true );
52
	$is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
53
54
	$simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
55
56
	$purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
57
	$content      = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
58
59
	return $content;
60
}
61