choice-control-render.php
2 years ago
choice-render.php
2 years ago
choices-field-render.php
2 years ago
choice-render.php
116 lines
| 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 = $this->has_choice_input() |
| 51 | ? array() |
| 52 | : array( |
| 53 | 'aria-checked' => $is_checked ? 'true' : 'false', |
| 54 | 'role' => $this->block_type->is_allowed_multiple() ? 'checkbox' : 'radio', |
| 55 | 'tabindex' => '0', |
| 56 | ); |
| 57 | |
| 58 | $attrs = get_block_wrapper_attributes( |
| 59 | array_merge( |
| 60 | array( |
| 61 | 'class' => 'jet-form-builder-choice--item' . ( $is_checked ? ' is-checked' : '' ), |
| 62 | ), |
| 63 | $accessibility_attrs |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | return sprintf( |
| 68 | '<li %1$s>%2$s</li>', |
| 69 | $attrs, |
| 70 | ( $this->block_type->block_content . |
| 71 | ( $this->has_choice_control() ? '' : $this->get_hidden_input_control() ) |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function get_hidden_input_control(): string { |
| 77 | /** |
| 78 | * Cannot delete this row. |
| 79 | * Although this input will be hidden and has no LABEL, if it is not done, |
| 80 | * the availability of other LABEL elements will be broken |
| 81 | */ |
| 82 | $this->block_type->get_field_id( '', 'label' ); |
| 83 | |
| 84 | return Choice_Control_Render::get_input_control( |
| 85 | $this->block_type, |
| 86 | array( |
| 87 | array( 'style', 'display:none;' ), |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | public function has_choice_control(): bool { |
| 93 | return $this->choice_control_exist; |
| 94 | } |
| 95 | |
| 96 | public function has_choice_input(): bool { |
| 97 | return $this->choice_input_exist; |
| 98 | } |
| 99 | |
| 100 | protected function set_choice_check( array $wp_block ) { |
| 101 | if ( empty( $wp_block['innerBlocks'] ) ) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | $control = Block_Helper::find_by_block_name( |
| 106 | $wp_block['innerBlocks'], |
| 107 | 'jet-forms/choice-control' |
| 108 | ); |
| 109 | |
| 110 | if ( $control ) { |
| 111 | $this->choice_control_exist = true; |
| 112 | $this->choice_input_exist = empty( $control['attrs']['control_type'] ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 |