PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / Block / PaymentFormBlock.php

PaymentFormBlock.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/Block/PaymentFormBlock.php

324 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Payment Form Block
4 *
5 * @package SimplePay
6 * @subpackage Core
7 * @copyright Copyright (c) 2022, Sandhills Development, LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 4.4.2
10 */
11
12 namespace SimplePay\Core\Block;
13
14 use SimplePay\Core\Admin\FormBuilder\FormStyle\Frontend as FormStyleFrontend;
15 use SimplePay\Core\Assets as CoreAssets;
16 use SimplePay\Core\Block\AbstractBlock;
17 use SimplePay\Core\License\LicenseAwareInterface;
18 use SimplePay\Core\License\LicenseAwareTrait;
19 use SimplePay\Pro\Assets as ProAssets;
20
21 /**
22 * PaymentFormBlock class.
23 *
24 * @since 4.4.2
25 */
26 class PaymentFormBlock extends AbstractBlock implements LicenseAwareInterface {
27
28 use LicenseAwareTrait;
29
30 /**
31 * {@inheritdoc}
32 */
33 public function register() {
34 $asset_file = SIMPLE_PAY_INC . 'core/assets/js/dist/simpay-block-payment-form.asset.php';
35
36 if ( ! file_exists( $asset_file ) ) {
37 return;
38 }
39
40 $this->register_assets( $asset_file );
41
42 register_block_type(
43 'simpay/payment-form',
44 array(
45 'title' => _x(
46 'WP Simple Pay - Payment Form',
47 'block title',
48 'stripe'
49 ),
50 'description' => _x(
51 'Display a WP Simple Pay payment form.',
52 'block description',
53 'stripe'
54 ),
55 'category' => 'widgets',
56 'keywords' => array(
57 _x( 'form', 'block keyword', 'stripe' ),
58 _x( 'payment', 'block keyword', 'stripe' ),
59 _x( 'stripe', 'block keyword', 'stripe' ),
60 _x( 'simple pay', 'block keyword', 'stripe' ),
61 _x( 'wp simple pay', 'block keyword', 'stripe' ),
62 ),
63 'attributes' => array(
64 'formId' => array(
65 'type' => 'integer',
66 ),
67 'showTitle' => array(
68 'type' => 'boolean',
69 'default' => true,
70 ),
71 'showDescription' => array(
72 'type' => 'boolean',
73 'default' => true,
74 ),
75 'preview' => array(
76 'type' => 'boolean',
77 'default' => false,
78 ),
79 ),
80 'example' => array(
81 'attributes' => array(
82 'preview' => true,
83 ),
84 ),
85 'supports' => array(
86 'html' => false,
87 'align' => array( 'center' ),
88 ),
89 'editor_script' => 'simpay-block-payment-form',
90 'editor_style' => 'simpay-block-payment-form',
91 'render_callback' => array( $this, 'render' ),
92 )
93 );
94 }
95
96 /**
97 * Renders the block's output on the server.
98 *
99 * @since 4.4.2
100 *
101 * @param array<mixed> $attributes The block attributes.
102 * @return string Block content.
103 */
104 public function render( $attributes ) {
105 if ( ! isset( $attributes['formId'] ) || 0 === $attributes['formId'] ) {
106 return '';
107 }
108
109 /** @var int $form_id */
110 $form_id = $attributes['formId'];
111 $form = simpay_get_form( $form_id );
112
113 if ( false === $form ) {
114 return '';
115 }
116
117 $is_frontend = (
118 ! defined( 'REST_REQUEST' ) ||
119 ( defined( 'REST_REQUEST' ) && ! REST_REQUEST )
120 );
121
122 $vars = $this->get_form_vars( $form );
123 $styles = $this->generate_inline_styles( $attributes );
124 $wrapper_attributes = get_block_wrapper_attributes(
125 array(
126 'id' => 'simpay-block-payment-form-' . $form_id,
127 )
128 );
129
130 // Include form style CSS for block editor previews, since
131 // wp_print_footer_scripts does not fire during REST requests.
132 if ( ! $is_frontend && class_exists( FormStyleFrontend::class ) ) {
133 $form_style_frontend = FormStyleFrontend::get_instance();
134 $styles .= $form_style_frontend->get_form_css( $form_id );
135 }
136
137 $form_html = do_shortcode( sprintf( '[simpay id="%d"]', $form_id ) );
138
139 // Inject data-form-id attributes inline for block editor previews,
140 // since the footer JS injection does not run during REST requests.
141 if ( ! $is_frontend ) {
142 $heading_js = sprintf(
143 '<script>
144 (function() {
145 var wrap = document.getElementById("simpay-embedded-form-wrap-%1$d");
146 if (wrap) {
147 wrap.setAttribute("data-form-id", "%1$d");
148 var heading = wrap.previousElementSibling;
149 if (heading && heading.classList && heading.classList.contains("simpay-embedded-heading")) {
150 heading.setAttribute("data-form-id", "%1$d");
151 }
152 }
153 })();
154 </script>',
155 $form_id
156 );
157 }
158
159 return sprintf(
160 '<div %1$s data-form-id="%2$s" data-form-vars=\'%3$s\'>%4$s</div><style>%5$s</style>%6$s',
161 $wrapper_attributes,
162 $form_id,
163 // Only attach form variables in the block editor to prevent possible conflicts on the frontend.
164 $is_frontend ? '' : wp_json_encode( $vars, JSON_HEX_QUOT | JSON_HEX_APOS ),
165 $form_html,
166 $styles,
167 ! $is_frontend ? $heading_js : ''
168 );
169 }
170
171 /**
172 * Registers assets required to render the payment form in the block editor.
173 *
174 * @since 4.4.2
175 *
176 * @param string $asset_file Block script asset file.
177 * @return void
178 */
179 private function register_assets( $asset_file ) {
180 // Register frontend payment form assets.
181 // Use the singleton to avoid adding duplicate wp_footer hooks for localize_scripts.
182 $assets = CoreAssets::get_instance();
183
184 if ( false === $this->license->is_lite() ) {
185 ProAssets::get_instance();
186 }
187
188 $assets->register();
189
190 $script_data = require $asset_file;
191
192 // Register block editor payment form assets.
193 wp_register_script(
194 'simpay-block-payment-form',
195 SIMPLE_PAY_INC_URL . 'core/assets/js/dist/simpay-block-payment-form.js',
196 array_merge(
197 $script_data['dependencies'],
198 array_keys( $assets->scripts )
199 ),
200 $script_data['version']
201 );
202
203 wp_localize_script(
204 'simpay-block-payment-form',
205 'simpayBlockPaymentForm',
206 array(
207 'isUpe' => true,
208 'isLite' => $this->license->is_lite() ? 1 : 0,
209 'previews' => array(
210 'pro' => SIMPLE_PAY_INC_URL . 'core/assets/images/blocks/payment-form-preview-pro.png',
211 'lite' => SIMPLE_PAY_INC_URL . 'core/assets/images/blocks/payment-form-preview-lite.png',
212 ),
213 )
214 );
215
216 wp_register_style(
217 'simpay-block-payment-form',
218 SIMPLE_PAY_INC_URL . 'core/assets/css/simpay-block-payment-form.min.css',
219 array_keys( $assets->styles ),
220 $script_data['version']
221 );
222 }
223
224 /**
225 * Returns a list of variables used to manually initialize the payment form in the block editor.
226 *
227 * The non-UPE is messy, weird, and confusing. You are not crazy. It is a way to simulate the
228 * form of the `var simplePayForms = []` script data normally output on the frontend.
229 *
230 * @link https://github.com/awesomemotive/wp-simple-pay-pro/issues/860
231 *
232 * @since 4.4.2
233 *
234 * @param \SimplePay\Core\Abstracts\Form $form Payment form.
235 * @return array<mixed>
236 */
237 private function get_form_vars( $form ) {
238 $is_lite = $this->license->is_lite();
239
240 /** @var \SimplePay\Core\Forms\Default_Form $form */
241
242 $vars = $form->get_upe_script_variables();
243
244 return $vars;
245 }
246
247 /**
248 * Generates CSS to show/hide pieces of the payemnt form.
249 *
250 * @since 4.4.2
251 *
252 * @param array<mixed> $attributes The block attributes.
253 * @return string
254 */
255 private function generate_inline_styles( $attributes ) {
256 $styles = '';
257 /** @var int $form_id */
258 $form_id = isset( $attributes['formId'] ) ? $attributes['formId'] : 0;
259
260 // Hide the title.
261 if (
262 isset( $attributes['showTitle'] ) &&
263 false === $attributes['showTitle']
264 ) {
265 $styles .= sprintf(
266 '[data-form-id="%d"] .simpay-form-title { display: none; }',
267 $form_id
268 );
269 }
270
271 // Hide the description.
272 if (
273 isset( $attributes['showDescription'] ) &&
274 false === $attributes['showDescription']
275 ) {
276 $styles .= sprintf(
277 '[data-form-id="%d"] .simpay-form-description { display: none; }',
278 $form_id
279 );
280 }
281
282 // If both title and description are hidden, hide the header wrapper (embedded only).
283 if (
284 (
285 isset( $attributes['showTitle'] ) &&
286 false === $attributes['showTitle']
287 ) &&
288 (
289 isset( $attributes['showDescription'] ) &&
290 false === $attributes['showDescription']
291 )
292 ) {
293 $styles .= sprintf(
294 '[data-form-id="%d"] .simpay-embedded-heading { display: none; }',
295 $form_id
296 );
297 }
298
299 // Alignment.
300 if ( isset( $attributes['align'] ) && 'center' === $attributes['align'] ) {
301 $styles .= sprintf(
302 '
303 [data-form-id="%1$d"] .simpay-checkout-form,
304 [data-form-id="%1$d"] .simpay-payment-btn,
305 [data-form-id="%1$s"] .simpay-modal-control-open,
306 [data-form-id="%1$s"] .simpay-embedded-heading {
307 display: block;
308 margin-left: auto;
309 margin-right: auto;
310 }
311 [data-form-id="%1$s"] .simpay-embedded-heading.simpay-styled {
312 max-width: 400px;
313 }
314 [data-form-id="%1$d"] .simpay-test-mode-badge-container {
315 text-align: center;
316 }',
317 $form_id
318 );
319 }
320
321 return $styles;
322 }
323 }
324