base-source.php
4 years ago
preset-source-options-page.php
4 years ago
preset-source-post.php
4 years ago
preset-source-query-var.php
4 years ago
preset-source-user.php
4 years ago
base-source.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | use Jet_Form_Builder\Presets\Preset_Manager; |
| 7 | |
| 8 | abstract class Base_Source { |
| 9 | |
| 10 | protected $fields_map; |
| 11 | protected $field_data = array(); |
| 12 | protected $field_args; |
| 13 | protected $preset_data; |
| 14 | protected $field = '__condition__'; |
| 15 | protected $prop; |
| 16 | private $src; |
| 17 | |
| 18 | const FUNC_PREFIX = '_source__'; |
| 19 | const EMPTY = ''; |
| 20 | |
| 21 | abstract public function query_source(); |
| 22 | |
| 23 | abstract public function get_id(); |
| 24 | |
| 25 | public function condition(): bool { |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | public function init_source( $fields_map, $field_args, $preset_data ) { |
| 30 | $this->fields_map = $fields_map; |
| 31 | $this->field_args = $field_args; |
| 32 | $this->preset_data = $preset_data; |
| 33 | $this->field = $this->field_args['name'] ?? ''; |
| 34 | $this->field_data = $this->get_field_data(); |
| 35 | $this->prop = $this->get_prop(); |
| 36 | $this->src = $this->maybe_query_source(); |
| 37 | |
| 38 | $this->after_init(); |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function after_init() { |
| 44 | } |
| 45 | |
| 46 | public function after_register() { |
| 47 | } |
| 48 | |
| 49 | public function maybe_query_source() { |
| 50 | if ( $this->prop ) { |
| 51 | return $this->query_source(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | protected function get_field_data() { |
| 56 | if ( $this->has_field_in_map() ) { |
| 57 | return $this->fields_map[ $this->field ]; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public function has_field_in_map() { |
| 62 | return ( isset( $this->fields_map[ $this->field ] ) && ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) ) ); |
| 63 | } |
| 64 | |
| 65 | public function src() { |
| 66 | return $this->src; |
| 67 | } |
| 68 | |
| 69 | protected function can_get_preset() { |
| 70 | return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) ); |
| 71 | } |
| 72 | |
| 73 | public function default_prop( string $prop ) { |
| 74 | $source = $this->src; |
| 75 | |
| 76 | if ( isset( $source->$prop ) ) { |
| 77 | return $source->$prop; |
| 78 | } elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) { |
| 79 | return $source->data->$prop; |
| 80 | } |
| 81 | |
| 82 | return self::EMPTY; |
| 83 | } |
| 84 | |
| 85 | protected function get_prop() { |
| 86 | return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : false ); |
| 87 | } |
| 88 | |
| 89 | public function get_result_on_prop() { |
| 90 | $func_name = self::FUNC_PREFIX . $this->prop; |
| 91 | |
| 92 | if ( is_callable( array( $this, $func_name ) ) ) { |
| 93 | return call_user_func( array( $this, $func_name ) ); |
| 94 | } |
| 95 | |
| 96 | return $this->default_prop( $this->prop ); |
| 97 | } |
| 98 | |
| 99 | final public function result() { |
| 100 | if ( ! $this->can_get_preset() ) { |
| 101 | return self::EMPTY; |
| 102 | } |
| 103 | |
| 104 | return $this->parse_result_value( $this->get_result_on_prop() ); |
| 105 | } |
| 106 | |
| 107 | public function parse_result_value( $value ) { |
| 108 | if ( ! isset( $this->field_args['type'] ) ) { |
| 109 | return $value; |
| 110 | } |
| 111 | |
| 112 | return Preset_Manager::instance()->prepare_result( $this->field_args['type'], $value ); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 |