Completed
Push — master ( e2489a...f93ff4 )
by Kirk
52:16 queued 44:06
created

simple-payments.php ➔ render_block()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 26
rs 9.504
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 Automattic\Jetpack\Blocks;
13
use Jetpack_Simple_Payments;
14
use Automattic\Jetpack\Blocks;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Automattic\Jetpack\Blocks as Blocks because the name is already in use
Loading history...
15
16
const FEATURE_NAME = 'simple-payments';
17
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
18
19
/**
20
 * Registers the block for use in Gutenberg
21
 * This is done via an action so that we can disable
22
 * registration if we need to.
23
 */
24
function register_block() {
25
	Blocks::jetpack_register_block(
26
		BLOCK_NAME,
27
		array(
28
			'render_callback' => __NAMESPACE__ . '\render_block',
29
			'plan_check'      => true,
30
		)
31
	);
32
}
33
add_action( 'init', __NAMESPACE__ . '\register_block' );
34
35
/**
36
 * Pay with PayPal block dynamic rendering.
37
 *
38
 * @param array  $attr    Array containing the block attributes.
39
 * @param string $content String containing the block content.
40
 *
41
 * @return string
42
 */
43
function render_block( $attr, $content ) {
44
	// Do nothing if block content is a `simple-payment` shortcode.
45
	if ( preg_match( '/\[simple-payment(.*)]/', $content ) ) {
46
		return $content;
47
	}
48
49
	// Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
50
	if ( ! jetpack_is_frontend() ) {
51
		return $content;
52
	}
53
54
	$simple_payments = Jetpack_Simple_Payments::getInstance();
55
	$simple_payments->enqueue_frontend_assets();
56
57
	// For AMP requests, make sure the purchase link redirects to the non-AMP post URL.
58
	if ( Blocks::is_amp_request() ) {
59
		$content = preg_replace(
60
			'#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i',
61
			'$1rel="$2 noamphtml"$3',
62
			$content
63
		);
64
		return $content;
65
	}
66
67
	// Augment block UI with a PayPal button if rendered on the frontend.
68
	$product_id  = $attr['productId'];
69
	$dom_id      = wp_unique_id( "jetpack-simple-payments-{$product_id}_" );
70
	$is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
71
72
	$simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
73
74
	$purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
75
	$content      = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
76
77
	return $content;
78
}
79
80
/**
81
 * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks.
82
 *
83
 * @param bool    $skip Skipped.
84
 * @param int     $post_id Post ID.
85
 * @param WP_Post $post Post.
86
 *
87
 * @return bool Whether to skip the post from AMP.
88
 */
89
function amp_skip_post( $skip, $post_id, $post ) {
90
	// When AMP is on standard mode, there are no non-AMP posts to link to where the purchase can be completed, so let's
91
	// prevent the post from being available in AMP.
92
	if ( amp_is_canonical() && has_block( 'jetpack/simple-payments', $post->post_content ) ) {
93
		return true;
94
	}
95
	return $skip;
96
}
97
add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );
98