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
CartMenuButton
3 years ago
CartMessage
3 years ago
CartSubmit
3 years ago
CartSubtotal
3 years ago
Checkbox
3 years ago
CheckoutErrors
3 years ago
CheckoutForm
3 years ago
Column
3 years ago
Columns
3 years ago
ConditionalForm
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
FirstName
3 years ago
Form
3 years ago
Heading
3 years ago
Input
3 years ago
LastName
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
Phone
3 years ago
PriceChoice
3 years ago
PriceSelector
3 years ago
Radio
3 years ago
RadioGroup
3 years ago
SessionDetail
3 years ago
StoreLogo
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
4 years ago
BlockServiceProvider.php
4 years ago
CartBlock.php
4 years ago
BaseBlock.php
184 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks; |
| 4 | |
| 5 | use SureCartBlocks\Util\BlockStyleAttributes; |
| 6 | |
| 7 | /** |
| 8 | * Checkout block |
| 9 | */ |
| 10 | abstract class BaseBlock { |
| 11 | /** |
| 12 | * Optional directory to .json block data files. |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | protected $directory = ''; |
| 17 | |
| 18 | /** |
| 19 | * Holds the block. |
| 20 | * |
| 21 | * @var object |
| 22 | */ |
| 23 | protected $block; |
| 24 | |
| 25 | /** |
| 26 | * Get the class name for the color. |
| 27 | * |
| 28 | * @param string $color_context_name The color context name (color, background-color). |
| 29 | * @param string $color_slug (foreground, background, etc.). |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | public function getColorClassName( $color_context_name, $color_slug ) { |
| 34 | if ( ! $color_context_name || ! $color_slug ) { |
| 35 | return false; |
| 36 | } |
| 37 | $color_slug = _wp_to_kebab_case( $color_slug ); |
| 38 | return "has-$color_slug-$color_context_name"; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get the classes. |
| 43 | * |
| 44 | * @param array $attributes The block attributes. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function getClasses( $attributes ) { |
| 49 | // get block classes and styles. |
| 50 | [ 'classes' => $classes ] = BlockStyleAttributes::getClassesAndStylesFromAttributes( $attributes ); |
| 51 | // get text align class. |
| 52 | ['class' => $text_align_class] = BlockStyleAttributes::getTextAlignClassAndStyle( $attributes ); |
| 53 | return implode( ' ', array_filter( [ $classes, $text_align_class ] ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the styles |
| 58 | * |
| 59 | * @param array $attributes The block attributes. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function getStyles( $attributes ) { |
| 64 | [ 'styles' => $styles ] = BlockStyleAttributes::getClassesAndStylesFromAttributes( $attributes ); |
| 65 | return $styles; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the spacing preset css variable. |
| 70 | * |
| 71 | * @param string $value The value. |
| 72 | * |
| 73 | * @return string|void |
| 74 | */ |
| 75 | public function getSpacingPresetCssVar( $value ) { |
| 76 | if ( ! $value ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | preg_match( '/var:preset\|spacing\|(.+)/', $value, $matches ); |
| 81 | |
| 82 | if ( ! $matches ) { |
| 83 | return $value; |
| 84 | } |
| 85 | |
| 86 | return "var(--wp--preset--spacing--$matches[1])"; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Register the block for dynamic output |
| 92 | * |
| 93 | * @param \Pimple\Container $container Service container. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function register() { |
| 98 | register_block_type_from_metadata( |
| 99 | $this->getDir(), |
| 100 | apply_filters( |
| 101 | 'surecart/block/registration/args', |
| 102 | [ 'render_callback' => [ $this, 'preRender' ] ], |
| 103 | ), |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get the called class directory path |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function getDir() { |
| 113 | if ( $this->directory ) { |
| 114 | return $this->directory; |
| 115 | } |
| 116 | |
| 117 | $reflector = new \ReflectionClass( $this ); |
| 118 | $fn = $reflector->getFileName(); |
| 119 | return dirname( $fn ); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /** |
| 124 | * Optionally run a function to modify attibuutes before rendering. |
| 125 | * |
| 126 | * @param array $attributes Block attributes. |
| 127 | * @param string $content Post content. |
| 128 | * |
| 129 | * @return function |
| 130 | */ |
| 131 | public function preRender( $attributes, $content, $block ) { |
| 132 | $this->block = $block; |
| 133 | |
| 134 | // run middlware. |
| 135 | $render = $this->middleware( $attributes, $content ); |
| 136 | |
| 137 | if ( is_wp_error( $render ) ) { |
| 138 | return $render->get_error_message(); |
| 139 | } |
| 140 | |
| 141 | if ( true !== $render ) { |
| 142 | return $render; |
| 143 | } |
| 144 | |
| 145 | $attributes = $this->getAttributes( $attributes ); |
| 146 | |
| 147 | // render. |
| 148 | return $this->render( $attributes, $content, $block ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Run any block middleware before rendering. |
| 153 | * |
| 154 | * @param array $attributes Block attributes. |
| 155 | * @param string $content Post content. |
| 156 | * @return boolean|\WP_Error; |
| 157 | */ |
| 158 | protected function middleware( $attributes, $content ) { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Allows filtering of attributes before rendering. |
| 164 | * |
| 165 | * @param array $attributes Block attributes. |
| 166 | * @return array $attributes |
| 167 | */ |
| 168 | public function getAttributes( $attributes ) { |
| 169 | return $attributes; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Render the block |
| 174 | * |
| 175 | * @param array $attributes Block attributes. |
| 176 | * @param string $content Post content. |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | public function render( $attributes, $content ) { |
| 181 | return ''; |
| 182 | } |
| 183 | } |
| 184 |