| 1 |
<?php |
| 2 |
/** |
| 3 |
* JetEngine Field Options Generator with schema support. |
| 4 |
* |
| 5 |
* @package JFB_Compatibility\Jet_Engine\Generators |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JFB_Compatibility\Jet_Engine\Generators; |
| 9 |
|
| 10 |
use Jet_Form_Builder\Generators\Base_V2; |
| 11 |
|
| 12 |
// If this file is called directly, abort. |
| 13 |
if ( ! defined( 'WPINC' ) ) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Get_From_Field class. |
| 19 |
* |
| 20 |
* Gets options from JetEngine meta field definitions. |
| 21 |
*/ |
| 22 |
class Get_From_Field extends Base_V2 { |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns generator ID. |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function get_id() { |
| 30 |
return 'get_from_field'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns generator name. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_name() { |
| 39 |
return __( 'Get values list from JetEngine field options', 'jet-form-builder' ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns structured settings schema. |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function get_settings_schema(): array { |
| 48 |
return array( |
| 49 |
'field_name' => array( |
| 50 |
'type' => 'string', |
| 51 |
'default' => '', |
| 52 |
'label' => __( 'Option name', 'jet-form-builder' ), |
| 53 |
'control' => 'select', |
| 54 |
'options' => $this->get_fields_for_select(), |
| 55 |
'required' => true, |
| 56 |
'help' => __( 'Select a JetEngine meta field to get options from.', 'jet-form-builder' ), |
| 57 |
), |
| 58 |
'sub_field' => array( |
| 59 |
'type' => 'string', |
| 60 |
'default' => '', |
| 61 |
'label' => __( 'Repeater Sub-field', 'jet-form-builder' ), |
| 62 |
'control' => 'text', |
| 63 |
'placeholder' => '', |
| 64 |
'help' => __( 'Optional. For repeater fields, specify the sub-field name.', 'jet-form-builder' ), |
| 65 |
), |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get available JetEngine fields for select control. |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
private function get_fields_for_select(): array { |
| 75 |
$options = array( |
| 76 |
array( |
| 77 |
'value' => '', |
| 78 |
'label' => __( '-- Select Field --', 'jet-form-builder' ), |
| 79 |
), |
| 80 |
); |
| 81 |
|
| 82 |
if ( ! function_exists( 'jet_engine' ) || ! jet_engine()->meta_boxes ) { |
| 83 |
return $options; |
| 84 |
} |
| 85 |
|
| 86 |
$all_fields = jet_engine()->meta_boxes->get_registered_fields(); |
| 87 |
|
| 88 |
foreach ( $all_fields as $object => $fields ) { |
| 89 |
foreach ( $fields as $field_data ) { |
| 90 |
if ( empty( $field_data['name'] ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
// Check if field has options directly |
| 95 |
$has_options = ! empty( $field_data['options'] ) || |
| 96 |
'manual_bulk' === ( $field_data['options_source'] ?? '' ); |
| 97 |
|
| 98 |
// Also check if it's a repeater with sub-fields that have options |
| 99 |
if ( ! $has_options && ! empty( $field_data['repeater-fields'] ) ) { |
| 100 |
$has_options = $this->repeater_has_options( $field_data['repeater-fields'] ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( $has_options ) { |
| 104 |
$options[] = array( |
| 105 |
'value' => $field_data['name'], |
| 106 |
'label' => $field_data['title'] ?? $field_data['name'], |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $options; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Check if repeater sub-fields have options. |
| 117 |
* |
| 118 |
* @param array $repeater_fields Repeater sub-fields array. |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
private function repeater_has_options( array $repeater_fields ): bool { |
| 123 |
foreach ( $repeater_fields as $sub_field ) { |
| 124 |
if ( ! empty( $sub_field['options'] ) || |
| 125 |
'manual_bulk' === ( $sub_field['options_source'] ?? '' ) ) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Check if generator can generate options. |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
public function can_generate() { |
| 139 |
return function_exists( 'jet_engine' ) && jet_engine()->meta_boxes; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns generated options list. |
| 144 |
* |
| 145 |
* @param array|string $args Settings array or legacy pipe-delimited string. |
| 146 |
* |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
public function generate( $args ) { |
| 150 |
$result = array(); |
| 151 |
|
| 152 |
// Handle legacy string format (pipe-delimited: "field_name|sub_field") |
| 153 |
if ( is_string( $args ) ) { |
| 154 |
$parse_field = explode( '|', $args ); |
| 155 |
$field_name = $parse_field[0] ?? ''; |
| 156 |
$sub_field = $parse_field[1] ?? ''; |
| 157 |
} elseif ( is_array( $args ) ) { |
| 158 |
// New structured format |
| 159 |
$field_name = $args['field_name'] ?? ''; |
| 160 |
$sub_field = $args['sub_field'] ?? ''; |
| 161 |
|
| 162 |
// Legacy format support: check for generator_field |
| 163 |
if ( empty( $field_name ) && ! empty( $args['generator_field'] ) ) { |
| 164 |
$parse_field = explode( '|', $args['generator_field'] ); |
| 165 |
$field_name = $parse_field[0] ?? ''; |
| 166 |
$sub_field = $parse_field[1] ?? ''; |
| 167 |
} |
| 168 |
} else { |
| 169 |
return $result; |
| 170 |
} |
| 171 |
|
| 172 |
if ( empty( $field_name ) ) { |
| 173 |
return $result; |
| 174 |
} |
| 175 |
|
| 176 |
$all_fields = jet_engine()->meta_boxes->get_registered_fields(); |
| 177 |
$found_field = null; |
| 178 |
|
| 179 |
foreach ( $all_fields as $object => $fields ) { |
| 180 |
foreach ( $fields as $field_data ) { |
| 181 |
if ( ! empty( $field_data['name'] ) && $field_name === $field_data['name'] ) { |
| 182 |
$found_field = $field_data; |
| 183 |
break 2; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if ( ! empty( $sub_field ) && ! empty( $found_field['repeater-fields'] ) ) { |
| 189 |
foreach ( $found_field['repeater-fields'] as $repeater_field_data ) { |
| 190 |
if ( ! empty( $repeater_field_data['name'] ) && $sub_field === $repeater_field_data['name'] ) { |
| 191 |
$found_field = $repeater_field_data; |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $found_field ) && 'manual_bulk' === ( $found_field['options_source'] ?? '' ) ) { |
| 198 |
return iterator_to_array( |
| 199 |
$this->get_form_bulk_options( $found_field ) |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
if ( empty( $found_field['options'] ) ) { |
| 204 |
return $result; |
| 205 |
} |
| 206 |
|
| 207 |
foreach ( $found_field['options'] as $option ) { |
| 208 |
$result[] = array( |
| 209 |
'value' => $option['key'], |
| 210 |
'label' => $option['value'], |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
return $result; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Parse bulk options from field definition. |
| 219 |
* |
| 220 |
* @param array $field Field definition. |
| 221 |
* |
| 222 |
* @return \Generator |
| 223 |
*/ |
| 224 |
private function get_form_bulk_options( array $field ): \Generator { |
| 225 |
$options = $field['bulk_options'] ?? ''; |
| 226 |
|
| 227 |
if ( ! $options ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
$options = explode( "\n", $options ); |
| 232 |
|
| 233 |
foreach ( $options as $option ) { |
| 234 |
$parts = explode( '::', $option ); |
| 235 |
|
| 236 |
yield array( |
| 237 |
'value' => $parts[0], |
| 238 |
'label' => $parts[1] ?? $parts[0], |
| 239 |
); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|