Completed
Push — fix/instant-search-non-ascii ( b82854...f54339 )
by
unknown
18:41 queued 10:23
created

simple-payments.php ➔ amp_skip_post()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 3
dl 8
loc 8
rs 10
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
15
const FEATURE_NAME = 'simple-payments';
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
 * Pay with PayPal block dynamic rendering.
36
 *
37
 * @param array  $attr    Array containing the block attributes.
38
 * @param string $content String containing the block content.
39
 *
40
 * @return string
41
 */
42
function render_block( $attr, $content ) {
43
	// Do nothing if block content is a `simple-payment` shortcode.
44
	if ( preg_match( '/\[simple-payment(.*)]/', $content ) ) {
45
		return $content;
46
	}
47
48
	// Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
49
	if ( ! jetpack_is_frontend() ) {
50
		return $content;
51
	}
52
53
	$simple_payments = Jetpack_Simple_Payments::getInstance();
54
	$simple_payments->enqueue_frontend_assets();
55
56
	// For AMP requests, make sure the purchase link redirects to the non-AMP post URL.
57
	if ( Blocks::is_amp_request() ) {
58
		$content = preg_replace(
59
			'#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i',
60
			'$1rel="$2 noamphtml"$3',
61
			$content
62
		);
63
		return $content;
64
	}
65
66
	// Augment block UI with a PayPal button if rendered on the frontend.
67
	$product_id  = $attr['productId'];
68
	$dom_id      = wp_unique_id( "jetpack-simple-payments-{$product_id}_" );
69
	$is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
70
71
	$simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
72
73
	$purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
74
	$content      = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
75
76
	return $content;
77
}
78
79
/**
80
 * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks.
81
 *
82
 * @param bool    $skip Skipped.
83
 * @param int     $post_id Post ID.
84
 * @param WP_Post $post Post.
85
 *
86
 * @return bool Whether to skip the post from AMP.
87
 */
88 View Code Duplication
function amp_skip_post( $skip, $post_id, $post ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
	// When AMP is on standard mode, there are no non-AMP posts to link to where the purchase can be completed, so let's
90
	// prevent the post from being available in AMP.
91
	if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( BLOCK_NAME, $post->post_content ) ) {
92
		return true;
93
	}
94
	return $skip;
95
}
96
add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );
97