PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 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 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-paypal-payments / src / block / class-block.php

class-block.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-paypal-payments/src/block/class-block.php

160 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
10 namespace Automattic\Jetpack\PaypalPayments\SimplePayments;
11
12 use Automattic\Jetpack\Assets;
13 use Automattic\Jetpack\Blocks;
14 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
15 use Automattic\Jetpack\Paypal_Payments\Simple_Payments;
16 use Automattic\Jetpack\Status\Request;
17 use WP_Post;
18
19 /**
20 * Register and render the block.
21 */
22 class 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 }
160