AddToCartButton
3 years ago
Address
3 years ago
BumpLineItem
3 years ago
Button
3 years ago
BuyButton
3 years ago
Card
3 years ago
Cart
3 years ago
CartBumpLineItem
3 years ago
CartCoupon
3 years ago
CartHeader
3 years ago
CartItems
3 years ago
CartMessage
3 years ago
CartSubmit
3 years ago
CartSubtotal
3 years ago
Checkbox
3 years ago
CheckoutForm
3 years ago
Column
3 years ago
Columns
3 years ago
Confirmation
3 years ago
Coupon
3 years ago
CustomerDashboardButton
3 years ago
Dashboard
3 years ago
Divider
3 years ago
Donation
3 years ago
DonationAmount
3 years ago
Email
3 years ago
ExpressPayment
3 years ago
Form
3 years ago
Heading
3 years ago
Input
3 years ago
LineItems
3 years ago
LogoutButton
3 years ago
Name
3 years ago
NameYourPrice
3 years ago
OrderBumps
3 years ago
OrderConfirmationCustomer
3 years ago
OrderConfirmationLineItems
3 years ago
Password
3 years ago
Payment
3 years ago
PriceChoice
3 years ago
PriceSelector
3 years ago
Radio
3 years ago
RadioGroup
3 years ago
SessionDetail
3 years ago
Submit
3 years ago
Subtotal
3 years ago
Switch
3 years ago
TaxIdInput
3 years ago
TaxLineItem
3 years ago
Textarea
3 years ago
Total
3 years ago
Totals
3 years ago
BaseBlock.php
3 years ago
BlockService.php
3 years ago
BlockServiceProvider.php
3 years ago
CartBlock.php
3 years ago
BaseBlock.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks; |
| 4 | |
| 5 | /** |
| 6 | * Checkout block |
| 7 | */ |
| 8 | abstract class BaseBlock { |
| 9 | /** |
| 10 | * Optional directory to .json block data files. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $directory = ''; |
| 15 | |
| 16 | /** |
| 17 | * Holds the block. |
| 18 | * |
| 19 | * @var object |
| 20 | */ |
| 21 | protected $block; |
| 22 | |
| 23 | /** |
| 24 | * Register the block for dynamic output |
| 25 | * |
| 26 | * @param \Pimple\Container $container Service container. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function register() { |
| 31 | register_block_type_from_metadata( |
| 32 | $this->getDir(), |
| 33 | apply_filters( |
| 34 | 'surecart/block/registration/args', |
| 35 | [ 'render_callback' => [ $this, 'preRender' ] ], |
| 36 | ), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the called class directory path |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | public function getDir() { |
| 46 | if ( $this->directory ) { |
| 47 | return $this->directory; |
| 48 | } |
| 49 | |
| 50 | $reflector = new \ReflectionClass( $this ); |
| 51 | $fn = $reflector->getFileName(); |
| 52 | return dirname( $fn ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Optionally run a function to modify attibuutes before rendering. |
| 58 | * |
| 59 | * @param array $attributes Block attributes. |
| 60 | * @param string $content Post content. |
| 61 | * |
| 62 | * @return function |
| 63 | */ |
| 64 | public function preRender( $attributes, $content, $block ) { |
| 65 | $this->block = $block; |
| 66 | |
| 67 | // run middlware. |
| 68 | $render = $this->middleware( $attributes, $content ); |
| 69 | |
| 70 | if ( is_wp_error( $render ) ) { |
| 71 | return $render->get_error_message(); |
| 72 | } |
| 73 | |
| 74 | if ( true !== $render ) { |
| 75 | return $render; |
| 76 | } |
| 77 | |
| 78 | $attributes = $this->getAttributes( $attributes ); |
| 79 | |
| 80 | // render. |
| 81 | return $this->render( $attributes, $content, $block ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Run any block middleware before rendering. |
| 86 | * |
| 87 | * @param array $attributes Block attributes. |
| 88 | * @param string $content Post content. |
| 89 | * @return boolean|\WP_Error; |
| 90 | */ |
| 91 | protected function middleware( $attributes, $content ) { |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Allows filtering of attributes before rendering. |
| 97 | * |
| 98 | * @param array $attributes Block attributes. |
| 99 | * @return array $attributes |
| 100 | */ |
| 101 | public function getAttributes( $attributes ) { |
| 102 | return $attributes; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Render the block |
| 107 | * |
| 108 | * @param array $attributes Block attributes. |
| 109 | * @param string $content Post content. |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function render( $attributes, $content ) { |
| 114 | return ''; |
| 115 | } |
| 116 | } |
| 117 |