je-query-object-handlers
3 years ago
get-from-field.php
3 years ago
get-from-je-query.php
3 years ago
get-from-field.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Compatibility\Jet_Engine\Generators; |
| 4 | |
| 5 | use Jet_Form_Builder\Generators\Base; |
| 6 | |
| 7 | class Get_From_Field extends Base { |
| 8 | |
| 9 | /** |
| 10 | * Returns generator ID |
| 11 | * |
| 12 | * @return string |
| 13 | */ |
| 14 | public function get_id() { |
| 15 | return 'get_from_field'; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Returns generator name |
| 20 | * |
| 21 | * @return string |
| 22 | */ |
| 23 | public function get_name() { |
| 24 | return __( 'Get values list from JetEngine field options', 'jet-form-builder' ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns generated options list |
| 29 | * |
| 30 | * @param $args |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | public function generate( $args ) { |
| 35 | $field = $args['generator_field'] ?? ''; |
| 36 | $all_fields = jet_engine()->meta_boxes->get_registered_fields(); |
| 37 | $found_field = null; |
| 38 | $result = array(); |
| 39 | $parse_field = explode( '|', $field ); |
| 40 | $field = $parse_field[0]; |
| 41 | $sub_field = isset( $parse_field[1] ) ? $parse_field[1] : false; |
| 42 | |
| 43 | foreach ( $all_fields as $object => $fields ) { |
| 44 | foreach ( $fields as $field_data ) { |
| 45 | if ( ! empty( $field_data['name'] ) && $field === $field_data['name'] ) { |
| 46 | $found_field = $field_data; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if ( ! empty( $sub_field ) && ! empty( $found_field['repeater-fields'] ) ) { |
| 52 | foreach ( $found_field['repeater-fields'] as $repeater_field_data ) { |
| 53 | if ( ! empty( $repeater_field_data['name'] ) && $sub_field === $repeater_field_data['name'] ) { |
| 54 | $found_field = $repeater_field_data; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if ( empty( $found_field['options'] ) ) { |
| 60 | return $result; |
| 61 | } |
| 62 | |
| 63 | foreach ( $found_field['options'] as $option ) { |
| 64 | $result[] = array( |
| 65 | 'value' => $option['key'], |
| 66 | 'label' => $option['value'], |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return $result; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | } |
| 75 |