je-query-object-handlers
2 years ago
get-from-field.php
1 year ago
get-from-je-query.php
2 years ago
get-from-field.php
104 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 ) && 'manual_bulk' === ( $found_field['options_source'] ?? '' ) ) { |
| 65 | return iterator_to_array( |
| 66 | $this->get_form_bulk_options( $found_field ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | if ( empty( $found_field['options'] ) ) { |
| 71 | return $result; |
| 72 | } |
| 73 | |
| 74 | foreach ( $found_field['options'] as $option ) { |
| 75 | $result[] = array( |
| 76 | 'value' => $option['key'], |
| 77 | 'label' => $option['value'], |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | return $result; |
| 82 | } |
| 83 | |
| 84 | private function get_form_bulk_options( array $field ): \Generator { |
| 85 | $options = $field['bulk_options'] ?? ''; |
| 86 | |
| 87 | if ( ! $options ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $options = explode( "\n", $options ); |
| 92 | |
| 93 | foreach ( $options as $option ) { |
| 94 | $parts = explode( '::', $option ); |
| 95 | |
| 96 | yield array( |
| 97 | 'value' => $parts[0], |
| 98 | 'label' => $parts[1] ?? $parts[0], |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |
| 104 |