base-source.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
134 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Dev_Mode\Manager; |
| 9 | use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 10 | use Jet_Form_Builder\Presets\Types\Base_Preset; |
| 11 | |
| 12 | abstract class Base_Source { |
| 13 | |
| 14 | protected $fields_map; |
| 15 | protected $field_data = array(); |
| 16 | protected $field_args; |
| 17 | protected $preset_data; |
| 18 | protected $field = '__condition__'; |
| 19 | protected $prop; |
| 20 | private $src; |
| 21 | |
| 22 | const FUNC_PREFIX = '_source__'; |
| 23 | const EMPTY = ''; |
| 24 | |
| 25 | abstract public function query_source(); |
| 26 | |
| 27 | abstract public function get_id(); |
| 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->_query_source(); |
| 37 | |
| 38 | $this->after_init(); |
| 39 | |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function after_init() { |
| 44 | } |
| 45 | |
| 46 | public function _query_source() { |
| 47 | if ( $this->prop ) { |
| 48 | return $this->query_source(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | protected function get_field_data() { |
| 53 | if ( $this->has_field_in_map() ) { |
| 54 | return $this->fields_map[ $this->field ]; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function has_field_in_map() { |
| 59 | return ( isset( $this->fields_map[ $this->field ] ) && ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) ) ); |
| 60 | } |
| 61 | |
| 62 | public function src() { |
| 63 | return $this->src; |
| 64 | } |
| 65 | |
| 66 | protected function can_get_preset() { |
| 67 | return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) ); |
| 68 | } |
| 69 | |
| 70 | public function default_prop( string $prop ) { |
| 71 | $source = $this->src; |
| 72 | |
| 73 | if ( isset( $source->$prop ) ) { |
| 74 | return $source->$prop; |
| 75 | } elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) { |
| 76 | return $source->data->$prop; |
| 77 | } |
| 78 | |
| 79 | return self::EMPTY; |
| 80 | } |
| 81 | |
| 82 | protected function get_prop() { |
| 83 | return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : false ); |
| 84 | } |
| 85 | |
| 86 | public function get_result_on_prop() { |
| 87 | $func_name = self::FUNC_PREFIX . $this->prop; |
| 88 | |
| 89 | if ( is_callable( array( $this, $func_name ) ) ) { |
| 90 | return call_user_func( array( $this, $func_name ) ); |
| 91 | } |
| 92 | |
| 93 | return $this->default_prop( $this->prop ); |
| 94 | } |
| 95 | |
| 96 | final public function result() { |
| 97 | if ( ! $this->can_get_preset() ) { |
| 98 | return self::EMPTY; |
| 99 | } |
| 100 | |
| 101 | return $this->parse_result_value( $this->get_result_on_prop() ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Try to get values from request if passed |
| 107 | * |
| 108 | * @param [type] $args [description] |
| 109 | * |
| 110 | * @return [type] [description] |
| 111 | */ |
| 112 | public function maybe_adjust_value() { |
| 113 | |
| 114 | $value = isset( $this->field_args['default'] ) ? $this->field_args['default'] : ''; |
| 115 | $request_val = ! empty( $_REQUEST['values'] ) ? $_REQUEST['values'] : array(); |
| 116 | |
| 117 | if ( isset( $request_val[ $this->field ] ) ) { |
| 118 | $value = $request_val[ $this->field ]; |
| 119 | } |
| 120 | |
| 121 | return $value; |
| 122 | |
| 123 | } |
| 124 | |
| 125 | public function parse_result_value( $value ) { |
| 126 | // Prepare value for date field |
| 127 | if ( 'date-field' === $this->field_args['type'] && Tools::is_valid_timestamp( $value ) ) { |
| 128 | $value = date_i18n( 'Y-m-d', $value ); |
| 129 | } |
| 130 | |
| 131 | return $value; |
| 132 | } |
| 133 | |
| 134 | } |