← All changes
|
extensions/blocks/simple-payments/simple-payments.php
+12
-85
13.0.2
→
16.3-a.7
View file →
| @@ -6,96 +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 | - | |
| 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 | - __DIR__, | |
| 26 | - array( | |
| 27 | - 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 28 | - 'plan_check' => true, | |
| 29 | - ) | |
| 30 | - ); | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 31 | 14 | } |
| 32 | -add_action( 'init', __NAMESPACE__ . '\register_block' ); | |
| 33 | 15 | |
| 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 | - } | |
| 16 | +// Register the block | |
| 17 | +add_action( 'init', array( Block::class, 'register_block' ), 9 ); | |
| 47 | 18 | |
| 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 | - } | |
| 19 | +// Disable AMP for posts with the block. | |
| 20 | +add_filter( 'amp_skip_post', array( Block::class, 'amp_skip_post' ), 10, 3 ); | |
| 52 | 21 | |
| 53 | - $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 ); | |
| 54 | 24 | |
| 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; | |
| 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 ); | |
| 82 | 28 | } |
| 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 ); | |