choice-control-render.php
2 years ago
choice-render.php
2 months ago
choices-field-render.php
2 months ago
choices-field-render.php
132 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 Jet_Form_Builder\Classes\Builder_Helper; |
| 10 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 11 | use JFB_Modules\Advanced_Choices\Block_Types\Choices_Field; |
| 12 | use JFB_Modules\Wp_Experiments\Module; |
| 13 | |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @property Choices_Field $block_type |
| 20 | * |
| 21 | * Class Choices_Field_Render |
| 22 | * @package Jet_Form_Builder\Blocks\Render |
| 23 | */ |
| 24 | class Choices_Field_Render extends Base { |
| 25 | |
| 26 | public function get_name() { |
| 27 | return 'choices-field'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param null $wp_block |
| 32 | * @param null $template |
| 33 | * |
| 34 | * @return false|string |
| 35 | * @throws Repository_Exception |
| 36 | */ |
| 37 | public function render( $wp_block = null, $template = null ) { |
| 38 | /** |
| 39 | * For radio options, you must specify these attributes on the wrapper |
| 40 | * |
| 41 | * @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/radiogroup_role |
| 42 | */ |
| 43 | $accessibility = Builder_Helper::attrs( |
| 44 | $this->block_type->is_allowed_multiple() |
| 45 | ? array() |
| 46 | : array( |
| 47 | array( 'role', 'radiogroup' ), |
| 48 | array( 'aria-required', empty( $this->block_type->block_attrs['required'] ) ? '' : 'true' ), |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | $attrs = get_block_wrapper_attributes( |
| 53 | array( |
| 54 | 'class' => 'jet-form-builder-choice', |
| 55 | 'data-jfb-sync' => true, |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | $content = ''; |
| 60 | $full_name = $this->block_type->get_field_name() . ( $this->block_type->is_allowed_multiple() ? '[]' : '' ); |
| 61 | |
| 62 | $default = $this->block_type->block_attrs['default'] ?? array(); |
| 63 | |
| 64 | if ( is_array( $default ) ) { |
| 65 | $default = array_map( array( $this, 'maybe_resolve_default_macro' ), $default ); |
| 66 | } |
| 67 | |
| 68 | foreach ( $wp_block['innerBlocks'] as $inner_block ) { |
| 69 | |
| 70 | $content .= Block_Helper::render_with_context( |
| 71 | $inner_block, |
| 72 | $this->block_type->block_context + array( |
| 73 | Choices_Field::CONTEXT_RAW_NAME => $this->block_type->block_attrs['name'] ?? '', |
| 74 | Choices_Field::CONTEXT_NAME => $full_name, |
| 75 | Choices_Field::CONTEXT_DEFAULT => $default, |
| 76 | Choices_Field::CONTEXT_MULTIPLE => $this->block_type->is_allowed_multiple(), |
| 77 | Choices_Field::CONTEXT_REQUIRED => $this->block_type->block_attrs['required'] ?? false, |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | $html = sprintf( |
| 83 | '<ul %1$s %3$s>%2$s</ul>', |
| 84 | $attrs, |
| 85 | $content, |
| 86 | $accessibility |
| 87 | ); |
| 88 | |
| 89 | /** @var Module $module */ |
| 90 | $module = jet_form_builder()->module( 'wp-experiments' ); |
| 91 | $module->enable_native_layout(); |
| 92 | |
| 93 | $html = wp_render_layout_support_flag( $html, Block_Helper::current_block() ); |
| 94 | |
| 95 | $module->remove_native_layout(); |
| 96 | |
| 97 | return parent::render( null, $html ); |
| 98 | } |
| 99 | |
| 100 | protected function maybe_resolve_default_macro( $default ) { |
| 101 | if ( ! is_string( $default ) ) { |
| 102 | return $default; |
| 103 | } |
| 104 | $raw_default = $default; |
| 105 | $default = trim( $default ); |
| 106 | $default = trim( $default, '\'"' ); |
| 107 | if ( ! preg_match( '/^%(.+?)%$/', $default, $matches ) ) { |
| 108 | return $raw_default; |
| 109 | } |
| 110 | $field_name = $matches[1]; |
| 111 | |
| 112 | $blocks = jet_fb_live()->blocks ?? array(); |
| 113 | foreach ( $blocks as $block ) { |
| 114 | if ( empty( $block['attrs']['name'] ) || $field_name !== $block['attrs']['name'] ) { |
| 115 | continue; |
| 116 | } |
| 117 | return $block['attrs']['default'] ?? $raw_default; |
| 118 | } |
| 119 | return $raw_default; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @see \Jet_Form_Builder\Blocks\Render\Calculated_Field_Render::get_fields_label_tag |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | protected function get_fields_label_tag(): string { |
| 128 | return 'div'; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 |