| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Field Name block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Form_Field_Name_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Form_Field_Name_Block { |
| 16 |
/** |
| 17 |
* GhostKit_Form_Field_Name_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__( 'Name', 'ghostkit' ), |
| 35 |
), |
| 36 |
'nameFields' => array( |
| 37 |
'type' => 'string', |
| 38 |
'default' => 'first', |
| 39 |
), |
| 40 |
|
| 41 |
'descriptionLast' => array( |
| 42 |
'type' => 'string', |
| 43 |
), |
| 44 |
'placeholderLast' => array( |
| 45 |
'type' => 'string', |
| 46 |
), |
| 47 |
'defaultLast' => array( |
| 48 |
'type' => 'string', |
| 49 |
), |
| 50 |
|
| 51 |
'descriptionMiddle' => array( |
| 52 |
'type' => 'string', |
| 53 |
), |
| 54 |
'placeholderMiddle' => array( |
| 55 |
'type' => 'string', |
| 56 |
), |
| 57 |
'defaultMiddle' => array( |
| 58 |
'type' => 'string', |
| 59 |
), |
| 60 |
) |
| 61 |
), |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register gutenberg block output |
| 68 |
* |
| 69 |
* @param array $attributes - block attributes. |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function block_render( $attributes ) { |
| 74 |
$attributes = array_merge( |
| 75 |
array( |
| 76 |
'slug' => '', |
| 77 |
'nameFields' => 'first', |
| 78 |
'description' => '', |
| 79 |
'descriptionLast' => '', |
| 80 |
'placeholderLast' => '', |
| 81 |
'defaultLast' => '', |
| 82 |
'descriptionMiddle' => '', |
| 83 |
'placeholderMiddle' => '', |
| 84 |
'defaultMiddle' => '', |
| 85 |
'hideDescription' => false, |
| 86 |
), |
| 87 |
$attributes |
| 88 |
); |
| 89 |
|
| 90 |
ob_start(); |
| 91 |
|
| 92 |
$class = 'ghostkit-form-field ghostkit-form-field-name'; |
| 93 |
|
| 94 |
if ( 'first-middle-last' === $attributes['nameFields'] || 'first-last' === $attributes['nameFields'] ) { |
| 95 |
$class .= ' ghostkit-form-field-name-with-last'; |
| 96 |
} |
| 97 |
if ( 'first-middle-last' === $attributes['nameFields'] ) { |
| 98 |
$class .= ' ghostkit-form-field-name-with-middle'; |
| 99 |
} |
| 100 |
|
| 101 |
if ( isset( $attributes['className'] ) ) { |
| 102 |
$class .= ' ' . $attributes['className']; |
| 103 |
} |
| 104 |
|
| 105 |
$last_attributes = array( |
| 106 |
'slug' => $attributes['slug'], |
| 107 |
'slug_sub' => 'last', |
| 108 |
'description' => $attributes['descriptionLast'], |
| 109 |
'placeholder' => $attributes['placeholderLast'], |
| 110 |
'default' => $attributes['defaultLast'], |
| 111 |
'hideDescription' => $attributes['hideDescription'], |
| 112 |
); |
| 113 |
|
| 114 |
$middle_attributes = array( |
| 115 |
'slug' => $attributes['slug'], |
| 116 |
'slug_sub' => 'middle', |
| 117 |
'description' => $attributes['descriptionMiddle'], |
| 118 |
'placeholder' => $attributes['placeholderMiddle'], |
| 119 |
'default' => $attributes['defaultMiddle'], |
| 120 |
'hideDescription' => $attributes['hideDescription'], |
| 121 |
); |
| 122 |
|
| 123 |
?> |
| 124 |
|
| 125 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 126 |
<?php GhostKit_Form_Field_Label::get( $attributes ); ?> |
| 127 |
|
| 128 |
<?php if ( 'first-last' === $attributes['nameFields'] || 'first-middle-last' === $attributes['nameFields'] ) : ?> |
| 129 |
<div class="ghostkit-form-field-row"> |
| 130 |
<div class="ghostkit-form-field-name-first"> |
| 131 |
<?php endif; ?> |
| 132 |
|
| 133 |
<input type="text" <?php GhostKit_Form_Field_Attributes::get( $attributes ); ?> /> |
| 134 |
|
| 135 |
<?php GhostKit_Form_Field_Description::get( $attributes ); ?> |
| 136 |
|
| 137 |
<?php if ( 'first-middle-last' === $attributes['nameFields'] ) : ?> |
| 138 |
</div> |
| 139 |
<div class="ghostkit-form-field-name-middle"> |
| 140 |
<input type="text" <?php GhostKit_Form_Field_Attributes::get( $middle_attributes ); ?> /> |
| 141 |
|
| 142 |
<?php GhostKit_Form_Field_Description::get( $middle_attributes ); ?> |
| 143 |
<?php endif; ?> |
| 144 |
|
| 145 |
<?php if ( 'first-last' === $attributes['nameFields'] || 'first-middle-last' === $attributes['nameFields'] ) : ?> |
| 146 |
</div> |
| 147 |
<div class="ghostkit-form-field-name-last"> |
| 148 |
<input type="text" <?php GhostKit_Form_Field_Attributes::get( $last_attributes ); ?> /> |
| 149 |
|
| 150 |
<?php GhostKit_Form_Field_Description::get( $last_attributes ); ?> |
| 151 |
</div> |
| 152 |
</div> |
| 153 |
<?php endif; ?> |
| 154 |
</div> |
| 155 |
|
| 156 |
<?php |
| 157 |
|
| 158 |
return ob_get_clean(); |
| 159 |
} |
| 160 |
} |
| 161 |
new GhostKit_Form_Field_Name_Block(); |
| 162 |
|