simple-payments.php
102 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Pay with PayPal block (aka Simple Payments). |
| 4 | * |
| 5 | * @since 9.0.0 |
| 6 | * |
| 7 | * @package automattic/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::get_instance(); |
| 54 | |
| 55 | if ( ! $simple_payments->is_valid( $attr ) ) { |
| 56 | return ''; |
| 57 | } |
| 58 | |
| 59 | $simple_payments->enqueue_frontend_assets(); |
| 60 | |
| 61 | // For AMP requests, make sure the purchase link redirects to the non-AMP post URL. |
| 62 | if ( Blocks::is_amp_request() ) { |
| 63 | $content = preg_replace( |
| 64 | '#(<a class="jetpack-simple-payments-purchase".*)rel="(.*)"(.*>.*</a>)#i', |
| 65 | '$1rel="$2 noamphtml"$3', |
| 66 | $content |
| 67 | ); |
| 68 | return $content; |
| 69 | } |
| 70 | |
| 71 | // Augment block UI with a PayPal button if rendered on the frontend. |
| 72 | $product_id = $attr['productId']; |
| 73 | $dom_id = wp_unique_id( "jetpack-simple-payments-{$product_id}_" ); |
| 74 | $is_multiple = get_post_meta( $product_id, 'spay_multiple', true ) || '0'; |
| 75 | |
| 76 | $simple_payments->setup_paypal_checkout_button( $product_id, $dom_id, $is_multiple ); |
| 77 | |
| 78 | $purchase_box = $simple_payments->output_purchase_box( $dom_id, $is_multiple ); |
| 79 | $content = preg_replace( '#<a class="jetpack-simple-payments-purchase(.*)</a>#i', $purchase_box, $content ); |
| 80 | |
| 81 | return $content; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Determine if AMP should be disabled on posts having "Pay with PayPal" blocks. |
| 86 | * |
| 87 | * @param bool $skip Skipped. |
| 88 | * @param int $post_id Post ID. |
| 89 | * @param WP_Post $post Post. |
| 90 | * |
| 91 | * @return bool Whether to skip the post from AMP. |
| 92 | */ |
| 93 | function amp_skip_post( $skip, $post_id, $post ) { |
| 94 | // When AMP is on standard mode, there are no non-AMP posts to link to where the purchase can be completed, so let's |
| 95 | // prevent the post from being available in AMP. |
| 96 | if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( BLOCK_NAME, $post->post_content ) ) { |
| 97 | return true; |
| 98 | } |
| 99 | return $skip; |
| 100 | } |
| 101 | add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 ); |
| 102 |