| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Field Checkbox block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Form_Field_Checkbox_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Form_Field_Checkbox_Block { |
| 16 |
/** |
| 17 |
* GhostKit_Form_Field_Checkbox_Block constructor. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'init', array( $this, 'init' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Init. |
| 25 |
*/ |
| 26 |
public function init() { |
| 27 |
register_block_type_from_metadata( |
| 28 |
dirname( __FILE__ ), |
| 29 |
array( |
| 30 |
'render_callback' => array( $this, 'block_render' ), |
| 31 |
'attributes' => GhostKit_Form_Field_Attributes::get_block_attributes( |
| 32 |
array( |
| 33 |
'label' => array( |
| 34 |
'default' => esc_html__( 'Checkbox', 'ghostkit' ), |
| 35 |
), |
| 36 |
'options' => array( |
| 37 |
'type' => 'array', |
| 38 |
'items' => array( |
| 39 |
'type' => 'object', |
| 40 |
), |
| 41 |
), |
| 42 |
'inline' => array( |
| 43 |
'type' => 'boolean', |
| 44 |
), |
| 45 |
) |
| 46 |
), |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register gutenberg block output |
| 53 |
* |
| 54 |
* @param array $attributes - block attributes. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function block_render( $attributes ) { |
| 59 |
$attributes = array_merge( |
| 60 |
array( |
| 61 |
'slug' => '', |
| 62 |
'options' => array(), |
| 63 |
'inline' => false, |
| 64 |
), |
| 65 |
$attributes |
| 66 |
); |
| 67 |
|
| 68 |
ob_start(); |
| 69 |
|
| 70 |
$class = 'ghostkit-form-field ghostkit-form-field-checkbox'; |
| 71 |
|
| 72 |
if ( $attributes['inline'] ) { |
| 73 |
$class .= ' ghostkit-form-field-checkbox-inline'; |
| 74 |
} |
| 75 |
|
| 76 |
if ( isset( $attributes['className'] ) ) { |
| 77 |
$class .= ' ' . $attributes['className']; |
| 78 |
} |
| 79 |
|
| 80 |
?> |
| 81 |
|
| 82 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 83 |
<?php GhostKit_Form_Field_Label::get( $attributes ); ?> |
| 84 |
|
| 85 |
<div class="ghostkit-form-field-checkbox-items"> |
| 86 |
<?php |
| 87 |
foreach ( $attributes['options'] as $k => $option ) : |
| 88 |
$item_id = $attributes['slug'] . '-item-' . $k; |
| 89 |
$item_attrs = array_merge( |
| 90 |
$attributes, |
| 91 |
array( |
| 92 |
'id' => $item_id, |
| 93 |
'fieldIsArray' => true, |
| 94 |
) |
| 95 |
); |
| 96 |
?> |
| 97 |
<label class="ghostkit-form-field-checkbox-item" for="<?php echo esc_attr( $item_id ); ?>"> |
| 98 |
<input type="checkbox" <?php checked( $option['selected'] ); ?> value="<?php echo esc_attr( $option['value'] ); ?>" <?php GhostKit_Form_Field_Attributes::get( $item_attrs ); ?> /> |
| 99 |
<?php echo esc_html( $option['label'] ); ?> |
| 100 |
</label> |
| 101 |
<?php endforeach; ?> |
| 102 |
</div> |
| 103 |
|
| 104 |
<?php GhostKit_Form_Field_Description::get( $attributes ); ?> |
| 105 |
</div> |
| 106 |
|
| 107 |
<?php |
| 108 |
|
| 109 |
return ob_get_clean(); |
| 110 |
} |
| 111 |
} |
| 112 |
new GhostKit_Form_Field_Checkbox_Block(); |
| 113 |
|