PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | extensions/blocks/simple-payments/simple-payments.php +12 -86 13.4.5 → 16.3-a.7 View file →
@@ -6,97 +6,23 @@
6 6 *
7 7 * @package automattic/jetpack
8 8 */
9 9
10 -namespace Automattic\Jetpack\Extensions\SimplePayments;
10 +use Automattic\Jetpack\PaypalPayments\SimplePayments\Block;
11 11
12 -use Automattic\Jetpack\Blocks;
13 -use Jetpack_Simple_Payments;
14 -use WP_Post;
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 - __DIR__,
27 - array(
28 - 'render_callback' => __NAMESPACE__ . '\render_block',
29 - 'plan_check' => true,
30 - )
31 - );
12 +if ( ! defined( 'ABSPATH' ) ) {
13 + exit( 0 );
32 14 }
33 -add_action( 'init', __NAMESPACE__ . '\register_block' );
34 15
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 - }
16 +// Register the block
17 +add_action( 'init', array( Block::class, 'register_block' ), 9 );
48 18
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 - }
19 +// Disable AMP for posts with the block.
20 +add_filter( 'amp_skip_post', array( Block::class, 'amp_skip_post' ), 10, 3 );
53 21
54 - $simple_payments = Jetpack_Simple_Payments::get_instance();
22 +// Load scripts for the editing interface
23 +add_action( 'enqueue_block_editor_assets', array( Block::class, 'load_editor_scripts' ), 9 );
55 24
56 - if ( ! $simple_payments->is_valid( $attr ) ) {
57 - return '';
58 - }
59 -
60 - $simple_payments->enqueue_frontend_assets();
61 -
62 - // For AMP requests, make sure the purchase link redirects to the non-AMP post URL.
63 - if ( Blocks::is_amp_request() ) {
64 - $content = preg_replace(
65 - '#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i',
66 - '$1rel="$2 noamphtml"$3',
67 - $content
68 - );
69 - return $content;
70 - }
71 -
72 - // Augment block UI with a PayPal button if rendered on the frontend.
73 - $product_id = $attr['productId'];
74 - $dom_id = wp_unique_id( "jetpack-simple-payments-{$product_id}_" );
75 - $is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
76 -
77 - $simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
78 -
79 - $purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
80 - $content = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
81 -
82 - return $content;
25 +// Load styles in the editor iframe context
26 +if ( is_admin() ) {
27 + add_action( 'enqueue_block_assets', array( Block::class, 'load_editor_styles' ), 9 );
83 28 }
84 -
85 -/**
86 - * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks.
87 - *
88 - * @param bool $skip Skipped.
89 - * @param int $post_id Post ID.
90 - * @param WP_Post $post Post.
91 - *
92 - * @return bool Whether to skip the post from AMP.
93 - */
94 -function amp_skip_post( $skip, $post_id, $post ) {
95 - // When AMP is on standard mode, there are no non-AMP posts to link to where the purchase can be completed, so let's
96 - // prevent the post from being available in AMP.
97 - if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( BLOCK_NAME, $post->post_content ) ) {
98 - return true;
99 - }
100 - return $skip;
101 -}
102 -add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );