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