| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Advanced_Choices\Block_Renders; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
use Jet_Form_Builder\Blocks\Block_Helper; |
| 8 |
use Jet_Form_Builder\Blocks\Render\Base; |
| 9 |
use JFB_Modules\Advanced_Choices\Block_Types\Choice; |
| 10 |
|
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @property Choice $block_type |
| 17 |
* |
| 18 |
* Class Choice_Render |
| 19 |
* @package Jet_Form_Builder\Blocks\Render |
| 20 |
*/ |
| 21 |
class Choice_Render extends Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Contains the jet-forms/choice-control block or not. |
| 25 |
* |
| 26 |
* Affects whether or not to add a hidden field |
| 27 |
* |
| 28 |
* @var bool |
| 29 |
*/ |
| 30 |
protected $choice_control_exist = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* Contains a jet-forms/choice-control block with a default type control. |
| 34 |
* That is, a normal input instead of a picture |
| 35 |
* |
| 36 |
* Affects whether to make this block accessible |
| 37 |
* |
| 38 |
* @var bool |
| 39 |
*/ |
| 40 |
protected $choice_input_exist = false; |
| 41 |
|
| 42 |
public function get_name() { |
| 43 |
return 'choice'; |
| 44 |
} |
| 45 |
|
| 46 |
public function render( $wp_block = null, $template = null ) { |
| 47 |
$this->set_choice_check( $wp_block ); |
| 48 |
$is_checked = $this->block_type->is_checked_current(); |
| 49 |
|
| 50 |
$accessibility_attrs = array( |
| 51 |
'aria-checked' => $is_checked ? 'true' : 'false', |
| 52 |
'role' => $this->block_type->is_allowed_multiple() ? 'checkbox' : 'radio', |
| 53 |
'tabindex' => '0', |
| 54 |
); |
| 55 |
|
| 56 |
$choice_attrs = array( |
| 57 |
'class' => 'jet-form-builder-choice--item' . ( $is_checked ? ' is-checked' : '' ), |
| 58 |
); |
| 59 |
|
| 60 |
$fixed_width_style = $this->get_fixed_width_style(); |
| 61 |
|
| 62 |
if ( $fixed_width_style ) { |
| 63 |
$choice_attrs['style'] = $fixed_width_style; |
| 64 |
} |
| 65 |
|
| 66 |
$attrs = get_block_wrapper_attributes( |
| 67 |
array_merge( |
| 68 |
$choice_attrs, |
| 69 |
$accessibility_attrs |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
return sprintf( |
| 74 |
'<li %1$s>%2$s</li>', |
| 75 |
$attrs, |
| 76 |
( $this->block_type->block_content . |
| 77 |
( $this->has_choice_control() ? '' : $this->get_hidden_input_control() ) |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
protected function get_fixed_width_style(): string { |
| 83 |
$layout = $this->block_type->block_attrs['style']['layout'] ?? array(); |
| 84 |
|
| 85 |
if ( |
| 86 |
empty( $layout['selfStretch'] ) || |
| 87 |
'fixed' !== $layout['selfStretch'] || |
| 88 |
empty( $layout['flexSize'] ) |
| 89 |
) { |
| 90 |
return ''; |
| 91 |
} |
| 92 |
|
| 93 |
return safecss_filter_attr( |
| 94 |
sprintf( |
| 95 |
'flex-basis:auto;width:%s;', |
| 96 |
$layout['flexSize'] |
| 97 |
) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
public function get_hidden_input_control(): string { |
| 102 |
/** |
| 103 |
* Cannot delete this row. |
| 104 |
* Although this input will be hidden and has no LABEL, if it is not done, |
| 105 |
* the availability of other LABEL elements will be broken |
| 106 |
*/ |
| 107 |
$this->block_type->get_field_id( '', 'label' ); |
| 108 |
|
| 109 |
return Choice_Control_Render::get_input_control( |
| 110 |
$this->block_type, |
| 111 |
array( |
| 112 |
array( 'style', 'display:none;' ), |
| 113 |
array( 'aria-label', 'advanced choice hidden input' ), |
| 114 |
) |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
public function has_choice_control(): bool { |
| 119 |
return $this->choice_control_exist; |
| 120 |
} |
| 121 |
|
| 122 |
public function has_choice_input(): bool { |
| 123 |
return $this->choice_input_exist; |
| 124 |
} |
| 125 |
|
| 126 |
protected function set_choice_check( array $wp_block ) { |
| 127 |
if ( empty( $wp_block['innerBlocks'] ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$control = Block_Helper::find_by_block_name( |
| 132 |
$wp_block['innerBlocks'], |
| 133 |
'jet-forms/choice-control' |
| 134 |
); |
| 135 |
|
| 136 |
if ( $control ) { |
| 137 |
$this->choice_control_exist = true; |
| 138 |
$this->choice_input_exist = empty( $control['attrs']['control_type'] ); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|