Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Block
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 5
210
0.00% covered (danger)
0.00%
0 / 1
 register_block
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 render_block
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
42
 load_editor_styles
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 load_editor_scripts
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 amp_skip_post
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Pay with PayPal block (aka Simple Payments).
4 *
5 * @since jetpack-9.0.0
6 *
7 * @package automattic/jetpack-paypal-payments
8 */
9
10namespace Automattic\Jetpack\PaypalPayments\SimplePayments;
11
12use Automattic\Jetpack\Assets;
13use Automattic\Jetpack\Blocks;
14use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
15use Automattic\Jetpack\Paypal_Payments\Simple_Payments;
16use Automattic\Jetpack\Status\Request;
17use WP_Post;
18
19/**
20 * Register and render the block.
21 */
22class Block {
23    /**
24     * The block full slugname.
25     *
26     * @var string
27     */
28    const BLOCK_NAME = 'jetpack/simple-payments';
29
30    /**
31     * Registers the block for use in Gutenberg
32     * This is done via an action so that we can disable
33     * registration if we need to.
34     */
35    public static function register_block() {
36        if ( ! Jetpack_Plan::supports( 'simple-payments' ) ) {
37            return;
38        }
39
40        Blocks::jetpack_register_block(
41            __DIR__,
42            array( 'render_callback' => array( __CLASS__, 'render_block' ) )
43        );
44    }
45
46    /**
47     * Pay with PayPal block dynamic rendering.
48     *
49     * @param array  $attr    Array containing the block attributes.
50     * @param string $content String containing the block content.
51     *
52     * @return string
53     */
54    public static function render_block( $attr, $content ) {
55        // Do nothing if block content is a `simple-payment` shortcode.
56        if ( preg_match( '/\[simple-payment(.*)]/', $content ) ) {
57            return $content;
58        }
59
60        // Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
61        if ( ! Request::is_frontend() ) {
62            return $content;
63        }
64
65        $simple_payments = Simple_Payments::get_instance();
66
67        if ( ! $simple_payments->is_valid( $attr ) ) {
68            return '';
69        }
70
71        $simple_payments->enqueue_frontend_assets();
72
73        // For AMP requests, make sure the purchase link redirects to the non-AMP post URL.
74        if ( Blocks::is_amp_request() ) {
75            $content = preg_replace(
76                '#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i',
77                '$1rel="$2 noamphtml"$3',
78                $content
79            );
80            return $content;
81        }
82
83        // Augment block UI with a PayPal button if rendered on the frontend.
84        $product_id  = $attr['productId'];
85        $dom_id      = wp_unique_id( "jetpack-simple-payments-{$product_id}_" );
86        $is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0';
87
88        $simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple );
89
90        $purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple );
91        $content      = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content );
92
93        return $content;
94    }
95
96    /**
97     * Load editor styles for the block.
98     * These are loaded via enqueue_block_assets to ensure proper loading in the editor iframe context.
99     */
100    public static function load_editor_styles() {
101        $handle = 'jp-paypal-payments-blocks';
102
103        Assets::register_script(
104            $handle,
105            '../../dist/block/editor.js',
106            __FILE__,
107            array(
108                'css_path'   => '../../dist/block/editor.css',
109                'textdomain' => 'jetpack-paypal-payments',
110            )
111        );
112        wp_enqueue_style( $handle );
113    }
114
115    /**
116     * Loads scripts
117     */
118    public static function load_editor_scripts() {
119        Assets::register_script(
120            'jp-paypal-payments-blocks',
121            '../../dist/block/editor.js',
122            __FILE__,
123            array(
124                'in_footer'  => true,
125                'textdomain' => 'jetpack-paypal-payments',
126                'enqueue'    => true,
127                // Editor styles are loaded separately, see load_editor_styles().
128                'css_path'   => null,
129            )
130        );
131    }
132
133    /**
134     * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks.
135     *
136     * @param bool    $skip Skipped.
137     * @param int     $post_id Post ID.
138     * @param WP_Post $post Post.
139     *
140     * @return bool Whether to skip the post from AMP.
141     */
142    public static function amp_skip_post( $skip, $post_id, $post ) {
143        /*
144         * When AMP is on standard mode,
145         * there are no non-AMP posts to link to where
146         * the purchase can be completed,
147         * so let's prevent the post from being available in AMP.
148         */
149        if (
150            function_exists( 'amp_is_canonical' )
151            && \amp_is_canonical()
152            && has_block( self::BLOCK_NAME, $post->post_content )
153        ) {
154            return true;
155        }
156
157        return $skip;
158    }
159}