Block.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\CheckoutForm; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\BaseBlock; |
| 6 | |
| 7 | /** |
| 8 | * Logout Button Block. |
| 9 | */ |
| 10 | class Block extends BaseBlock { |
| 11 | /** |
| 12 | * Render the block |
| 13 | * |
| 14 | * @param array $attributes Block attributes. |
| 15 | * @param string $content Post content. |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | public function render( $attributes, $content ) { |
| 20 | static $seen_forms = array(); |
| 21 | |
| 22 | if ( empty( $attributes['id'] ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | $form = get_post( $attributes['id'] ); |
| 27 | if ( ! $form || 'sc_form' !== $form->post_type ) { |
| 28 | return ''; |
| 29 | } |
| 30 | |
| 31 | if ( isset( $seen_forms[ $attributes['id'] ] ) ) { |
| 32 | // WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent |
| 33 | // is set in `wp_debug_mode()`. |
| 34 | $is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG && |
| 35 | defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY; |
| 36 | |
| 37 | return $is_debug ? |
| 38 | // translators: Visible only in the front end, this warning takes the place of a faulty block. |
| 39 | __( '[block rendering halted]', 'surecart' ) : |
| 40 | ''; |
| 41 | } |
| 42 | |
| 43 | if ( 'publish' !== $form->post_status || ! empty( $form->post_password ) ) { |
| 44 | return ''; |
| 45 | } |
| 46 | |
| 47 | $seen_forms[ $attributes['id'] ] = true; |
| 48 | |
| 49 | global $sc_form_id; |
| 50 | $sc_form_id = $attributes['id']; |
| 51 | $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $attributes['textalign'] ?? '' ] ); |
| 52 | $result = do_blocks( $form->post_content ); |
| 53 | unset( $seen_forms[ $attributes['id'] ] ); |
| 54 | return '<div ' . $wrapper_attributes . '>' . $result . '</div>'; |
| 55 | } |
| 56 | } |
| 57 |