jetformbuilder
/
compatibility
/
jet-engine
/
generators
/
je-query-object-handlers
/
base-object-handler.php
jetformbuilder
/
compatibility
/
jet-engine
/
generators
/
je-query-object-handlers
Last commit date
base-object-handler.php
2 years ago
user-object-handler.php
2 years ago
base-object-handler.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Compatibility\Jet_Engine\Generators\Je_Query_Object_Handlers; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | if ( ! defined( 'WPINC' ) ) { |
| 8 | die; |
| 9 | } |
| 10 | |
| 11 | class Base_Object_Handler { |
| 12 | |
| 13 | protected $value_field; |
| 14 | protected $label_field; |
| 15 | protected $calc_field; |
| 16 | protected $additional; |
| 17 | |
| 18 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 19 | public function is_supported( $current ): bool { |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | public function set_fields( array $args ) { |
| 24 | $this->value_field = $args[1] ?? 'ID'; |
| 25 | $this->label_field = $args[2] ?? 'post_title'; |
| 26 | $this->calc_field = $args[3] ?? false; |
| 27 | $this->additional = $args[4] ?? false; |
| 28 | } |
| 29 | |
| 30 | final public function to_array( $current ): array { |
| 31 | // convert to array because in 8.2 dynamic properties will be deprecated |
| 32 | $converted = $this->get_converted( $current ); |
| 33 | |
| 34 | $value = $this->get_value( $converted, $current ); |
| 35 | $label = $this->get_label( $converted, $current ); |
| 36 | $calculated = $this->get_calc( $converted, $current ); |
| 37 | |
| 38 | $value = apply_filters( 'jet-forms-generate-from-query/value', $value, $current, $this->additional ); |
| 39 | $label = apply_filters( 'jet-forms-generate-from-query/label', $label, $current, $this->additional ); |
| 40 | $calculated = apply_filters( 'jet-forms-generate-from-query/calculated', $calculated, $current, $this->additional ); |
| 41 | |
| 42 | $item = array(); |
| 43 | |
| 44 | if ( $value ) { |
| 45 | $item['value'] = $value; |
| 46 | $item['label'] = $label; |
| 47 | } |
| 48 | |
| 49 | if ( $this->calc_field ) { |
| 50 | $item['calculate'] = $calculated; |
| 51 | } |
| 52 | |
| 53 | return $item; |
| 54 | } |
| 55 | |
| 56 | protected function get_converted( $current ) { |
| 57 | // convert to array because in 8.2 dynamic properties will be deprecated |
| 58 | return get_object_vars( $current ); |
| 59 | } |
| 60 | |
| 61 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 62 | protected function get_value( $converted, $current ) { |
| 63 | return $converted[ $this->value_field ] ?? false; |
| 64 | } |
| 65 | |
| 66 | protected function get_label( $converted, $current ) { |
| 67 | return $converted[ $this->label_field ] ?? $this->get_value( $converted, $current ); |
| 68 | } |
| 69 | |
| 70 | // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 71 | protected function get_calc( $converted, $current ) { |
| 72 | return $converted[ $this->calc_field ] ?? false; |
| 73 | } |
| 74 | |
| 75 | } |
| 76 |