PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.5.3
JetFormBuilder — Dynamic Blocks Form Builder v1.5.3
3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / presets / sources / base-source.php
jetformbuilder / includes / presets / sources Last commit date
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
233 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Presets\Sources;
5
6 use Jet_Form_Builder\Blocks\Types\Base;
7 use Jet_Form_Builder\Exceptions\Preset_Exception;
8 use Jet_Form_Builder\Presets\Preset_Manager;
9
10 abstract class Base_Source {
11
12 protected $fields_map;
13 protected $field_data = array();
14 protected $field_args;
15 protected $preset_data;
16 protected $field = '__condition__';
17 protected $prop;
18 private $src;
19
20 protected $permission;
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 condition(): bool {
30 return true;
31 }
32
33 /**
34 * @param $fields_map
35 * @param $preset_data
36 * @param $args
37 *
38 * @return $this
39 * @throws Preset_Exception
40 */
41 public function init_source( $fields_map, $preset_data, $args ): Base_Source {
42 $this->field_args = $args;
43 $this->field = $args['name'] ?? '';
44 $this->fields_map = $fields_map;
45 $this->preset_data = $preset_data;
46 $this->field_data = $this->get_field_data();
47 $this->prop = $this->get_prop();
48
49 return $this;
50 }
51
52 public function after_init(): Base_Source {
53 return $this;
54 }
55
56 public function after_register() {
57 }
58
59 public function on_sanitize(): bool {
60 return true;
61 }
62
63 public function is_need_prop() {
64 return true;
65 }
66
67 /**
68 * @return mixed
69 * @throws Preset_Exception
70 */
71 public function maybe_query_source() {
72 if ( $this->prop ) {
73 $this->src = $this->query_source();
74
75 return $this;
76 }
77
78 throw new Preset_Exception( 'Empty `prop` in ' . get_class( $this ), $this->field_data );
79 }
80
81 /**
82 * @return mixed
83 * @throws Preset_Exception
84 */
85 protected function get_field_data() {
86 if ( $this->has_field_in_map() ) {
87 return $this->fields_map[ $this->field ];
88 }
89
90 throw new Preset_Exception(
91 "Empty `fields_map['{$this->field}']` in " . get_class( $this ),
92 $this->fields_map
93 );
94 }
95
96 public function has_field_in_map() {
97 return ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) );
98 }
99
100 /**
101 * @return mixed
102 */
103 public function src() {
104 return $this->src;
105 }
106
107 /**
108 * @return mixed
109 * @throws Preset_Exception
110 */
111 public function safe_src() {
112 $this->throw_if_preset_not_available();
113
114 return $this->src();
115 }
116
117 /**
118 * @return bool
119 * @throws Preset_Exception
120 */
121 protected function can_get_preset() {
122 return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) );
123 }
124
125 /**
126 * @return bool
127 * @throws Preset_Exception
128 */
129 protected function has_permission(): bool {
130 if ( is_null( $this->permission ) ) {
131 $this->permission = $this->can_get_preset();
132 }
133
134 return $this->permission;
135 }
136
137 /**
138 * @throws Preset_Exception
139 */
140 final protected function throw_if_preset_not_available() {
141 if ( ! $this->has_permission() ) {
142 throw new Preset_Exception( static::class . '::can_get_preset return FALSE' );
143 }
144 }
145
146
147 /**
148 * @param string $prop
149 *
150 * @return mixed
151 * @throws Preset_Exception
152 */
153 public function default_prop( string $prop ) {
154 $source = $this->src;
155
156 if ( isset( $source->$prop ) ) {
157 return $source->$prop;
158 } elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) {
159 return $source->data->$prop;
160 }
161
162 throw new Preset_Exception( "Can't get value from " . get_class( $source ) );
163 }
164
165 protected function get_prop() {
166 if ( ! $this->is_need_prop() ) {
167 return true;
168 }
169 return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : false );
170 }
171
172 /**
173 * @return false|mixed
174 * @throws Preset_Exception
175 */
176 public function get_result_on_prop() {
177 if ( ! $this->is_need_prop() ) {
178 return $this->src();
179 }
180 $func_name = self::FUNC_PREFIX . $this->prop;
181
182 if ( is_callable( array( $this, $func_name ) ) ) {
183 return call_user_func( array( $this, $func_name ) );
184 }
185
186 return $this->default_prop( $this->prop );
187 }
188
189
190 /**
191 * @return mixed
192 * @throws Preset_Exception
193 */
194 final public function result() {
195 $this->throw_if_preset_not_available();
196
197 return $this->parse_result_value( $this->get_result_on_prop() );
198 }
199
200 public function parse_result_value( $value ) {
201 if ( ! isset( $this->field_args['type'] ) ) {
202 return $value;
203 }
204
205 return Preset_Manager::instance()->prepare_result( $this->field_args['type'], $value );
206 }
207
208
209 /**
210 * @return Base
211 * @throws Preset_Exception
212 */
213 public function get_field_object(): Base {
214 $type = $this->field_args['type'] ?? false;
215 $block = jet_form_builder()->blocks->get_field_by_name( $type );
216
217 if ( ! $block ) {
218 throw new Preset_Exception( 'Undefined block_type: ' . $type, $this->field_args );
219 }
220
221 return $block;
222 }
223
224 /**
225 * @return mixed
226 * @throws Preset_Exception
227 */
228 public function get_expected_format() {
229 return $this->get_field_object()->expected_preset_type()[0] ?? 'raw';
230 }
231
232 }
233