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; |
|
|
|
|
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
|
|
|
|