| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Field Select block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Form_Field_Select_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Form_Field_Select_Block { |
| 16 |
/** |
| 17 |
* GhostKit_Form_Field_Select_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 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
register_block_type( |
| 32 |
'ghostkit/form-field-select', |
| 33 |
array( |
| 34 |
'parent' => array( 'ghostkit/form' ), |
| 35 |
'render_callback' => array( $this, 'block_render' ), |
| 36 |
'attributes' => GhostKit_Form_Field_Attributes::get_block_attributes( |
| 37 |
array( |
| 38 |
'label' => array( |
| 39 |
'default' => esc_html__( 'Select', 'ghostkit' ), |
| 40 |
), |
| 41 |
'options' => array( |
| 42 |
'type' => 'array', |
| 43 |
'items' => array( |
| 44 |
'type' => 'object', |
| 45 |
), |
| 46 |
), |
| 47 |
'multiple' => array( |
| 48 |
'type' => 'boolean', |
| 49 |
), |
| 50 |
) |
| 51 |
), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Register gutenberg block output |
| 58 |
* |
| 59 |
* @param array $attributes - block attributes. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function block_render( $attributes ) { |
| 64 |
$attributes = array_merge( |
| 65 |
array( |
| 66 |
'options' => array(), |
| 67 |
'multiple' => false, |
| 68 |
), |
| 69 |
$attributes |
| 70 |
); |
| 71 |
|
| 72 |
$options = $attributes['options']; |
| 73 |
if ( ! is_array( $options ) ) { |
| 74 |
$options = array(); |
| 75 |
} |
| 76 |
|
| 77 |
// Add null option. |
| 78 |
if ( ! $attributes['multiple'] ) { |
| 79 |
$add_null_option = true; |
| 80 |
|
| 81 |
foreach ( $options as $data ) { |
| 82 |
if ( $data['selected'] ) { |
| 83 |
$add_null_option = false; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if ( $add_null_option ) { |
| 88 |
array_unshift( |
| 89 |
$options, |
| 90 |
array( |
| 91 |
'label' => esc_attr__( '--- Select ---', 'ghostkit' ), |
| 92 |
'value' => '', |
| 93 |
'selected' => true, |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
ob_start(); |
| 100 |
|
| 101 |
$class = 'ghostkit-form-field ghostkit-form-field-select'; |
| 102 |
|
| 103 |
if ( isset( $attributes['className'] ) ) { |
| 104 |
$class .= ' ' . $attributes['className']; |
| 105 |
} |
| 106 |
|
| 107 |
?> |
| 108 |
|
| 109 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 110 |
<?php GhostKit_Form_Field_Label::get( $attributes ); ?> |
| 111 |
|
| 112 |
<select <?php GhostKit_Form_Field_Attributes::get( $attributes ); ?>> |
| 113 |
<?php foreach ( $options as $option ) : ?> |
| 114 |
<option <?php selected( $option['selected'] ); ?> value="<?php echo esc_attr( $option['value'] ); ?>"> |
| 115 |
<?php echo esc_html( $option['label'] ); ?> |
| 116 |
</option> |
| 117 |
<?php endforeach; ?> |
| 118 |
</select> |
| 119 |
|
| 120 |
<?php GhostKit_Form_Field_Description::get( $attributes ); ?> |
| 121 |
</div> |
| 122 |
|
| 123 |
<?php |
| 124 |
|
| 125 |
return ob_get_clean(); |
| 126 |
} |
| 127 |
} |
| 128 |
new GhostKit_Form_Field_Select_Block(); |
| 129 |
|