base-source.php
3 years ago
preset-source-post.php
3 years ago
preset-source-query-var.php
3 years ago
preset-source-user.php
3 years ago
base-source.php
285 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\Classes\Macros_Parser; |
| 8 | use Jet_Form_Builder\Exceptions\Parse_Exception; |
| 9 | use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 10 | use Jet_Form_Builder\Presets\Preset_Manager; |
| 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 | protected $permission; |
| 23 | |
| 24 | const FUNC_PREFIX = 'source__'; |
| 25 | |
| 26 | abstract public function query_source(); |
| 27 | |
| 28 | abstract public function get_id(); |
| 29 | |
| 30 | public function condition(): bool { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param $fields_map |
| 36 | * @param $preset_data |
| 37 | * @param $args |
| 38 | * |
| 39 | * @return $this |
| 40 | * @throws Preset_Exception |
| 41 | */ |
| 42 | public function init_source( $fields_map, $preset_data, $args ): Base_Source { |
| 43 | $this->field_args = $args; |
| 44 | $this->field = $args['name'] ?? ''; |
| 45 | $this->fields_map = $fields_map; |
| 46 | $this->preset_data = $preset_data; |
| 47 | $this->field_data = $this->get_field_data(); |
| 48 | $this->prop = $this->get_prop(); |
| 49 | |
| 50 | return $this; |
| 51 | } |
| 52 | |
| 53 | public function after_init(): Base_Source { |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | public function after_register() { |
| 58 | } |
| 59 | |
| 60 | public function on_sanitize(): bool { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | public function is_need_prop() { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @return mixed |
| 70 | * @throws Preset_Exception |
| 71 | */ |
| 72 | public function maybe_query_source() { |
| 73 | if ( $this->prop ) { |
| 74 | $this->src = $this->query_source(); |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | throw new Preset_Exception( 'Empty `prop` in ' . get_class( $this ), $this->field_data ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return mixed |
| 84 | * @throws Preset_Exception |
| 85 | */ |
| 86 | protected function get_field_data() { |
| 87 | if ( $this->has_field_in_map() ) { |
| 88 | return $this->fields_map[ $this->field ]; |
| 89 | } |
| 90 | |
| 91 | throw new Preset_Exception( |
| 92 | "Empty `fields_map['{$this->field}']` in " . get_class( $this ), |
| 93 | $this->fields_map |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | public function has_field_in_map() { |
| 98 | return ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return mixed |
| 103 | */ |
| 104 | public function src() { |
| 105 | return $this->src; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return mixed |
| 110 | * @throws Preset_Exception |
| 111 | */ |
| 112 | public function safe_src() { |
| 113 | $this->throw_if_preset_not_available(); |
| 114 | |
| 115 | return $this->src(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @return bool |
| 120 | * @throws Preset_Exception |
| 121 | */ |
| 122 | protected function can_get_preset() { |
| 123 | return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return bool |
| 128 | * @throws Preset_Exception |
| 129 | */ |
| 130 | protected function has_permission(): bool { |
| 131 | if ( is_null( $this->permission ) ) { |
| 132 | $this->permission = apply_filters( 'jet-form-builder/preset-sanitize', $this->can_get_preset(), $this ); |
| 133 | } |
| 134 | |
| 135 | return $this->permission; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @throws Preset_Exception |
| 140 | */ |
| 141 | final protected function throw_if_preset_not_available() { |
| 142 | if ( ! $this->has_permission() ) { |
| 143 | throw new Preset_Exception( static::class . '::can_get_preset return FALSE' ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | protected function get_prop() { |
| 149 | if ( ! $this->is_need_prop() ) { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : false ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return false|mixed |
| 158 | * @throws Preset_Exception |
| 159 | */ |
| 160 | public function get_result_on_prop() { |
| 161 | if ( ! $this->is_need_prop() ) { |
| 162 | return $this->src(); |
| 163 | } |
| 164 | |
| 165 | $extra = $this->get_extra_fields(); |
| 166 | |
| 167 | if ( empty( $extra ) ) { |
| 168 | return $this->get_current_value(); |
| 169 | } |
| 170 | |
| 171 | $value = array(); |
| 172 | |
| 173 | foreach ( $extra as $name => $field ) { |
| 174 | $this->before_query_extra_field( $field ); |
| 175 | |
| 176 | $value[ $name ] = $this->get_current_value(); |
| 177 | } |
| 178 | |
| 179 | return $value; |
| 180 | } |
| 181 | |
| 182 | protected function before_query_extra_field( $field ) { |
| 183 | $this->field_data['key'] = $field; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @return false|mixed |
| 188 | * @throws Preset_Exception |
| 189 | */ |
| 190 | private function get_current_value() { |
| 191 | $func_name = self::FUNC_PREFIX . $this->prop; |
| 192 | |
| 193 | if ( is_callable( array( $this, $func_name ) ) ) { |
| 194 | return call_user_func( array( $this, $func_name ) ); |
| 195 | } |
| 196 | |
| 197 | return $this->default_prop( $this->prop ); |
| 198 | } |
| 199 | |
| 200 | private function get_extra_fields(): array { |
| 201 | try { |
| 202 | $extra = $this->get_field_object()->get_extra_fields( $this ); |
| 203 | } catch ( Preset_Exception $exception ) { |
| 204 | return array(); |
| 205 | } |
| 206 | |
| 207 | $parser = ( new Macros_Parser() )->set_replacements( |
| 208 | array( |
| 209 | 'key' => $this->field_data['key'] ?? '', |
| 210 | 'prop' => $this->prop, |
| 211 | ) |
| 212 | ); |
| 213 | |
| 214 | foreach ( $extra as $index => $field ) { |
| 215 | $extra[ $index ] = $parser->parse_macros( $field ); |
| 216 | } |
| 217 | |
| 218 | return $extra; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @param string $prop |
| 223 | * |
| 224 | * @return mixed |
| 225 | * @throws Preset_Exception |
| 226 | */ |
| 227 | public function default_prop( string $prop ) { |
| 228 | $source = $this->src; |
| 229 | |
| 230 | if ( isset( $source->$prop ) ) { |
| 231 | return $source->$prop; |
| 232 | } elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) { |
| 233 | return $source->data->$prop; |
| 234 | } |
| 235 | |
| 236 | throw new Preset_Exception( "Can't get value from " . get_class( $source ) ); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /** |
| 241 | * @return mixed |
| 242 | * @throws Preset_Exception |
| 243 | */ |
| 244 | final public function result() { |
| 245 | $this->throw_if_preset_not_available(); |
| 246 | |
| 247 | return $this->parse_result_value( $this->get_result_on_prop() ); |
| 248 | } |
| 249 | |
| 250 | public function parse_result_value( $value ) { |
| 251 | if ( ! isset( $this->field_args['type'] ) ) { |
| 252 | return $value; |
| 253 | } |
| 254 | |
| 255 | return Preset_Manager::instance()->prepare_result( $this->field_args['type'], $value ); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * @return Base |
| 261 | * @throws Preset_Exception |
| 262 | */ |
| 263 | public function get_field_object(): Base { |
| 264 | $type = $this->field_args['type'] ?? false; |
| 265 | $block = jet_form_builder()->blocks->get_field_by_name( $type ); |
| 266 | |
| 267 | if ( ! $block ) { |
| 268 | throw new Preset_Exception( 'Undefined block_type: ' . $type, $this->field_args ); |
| 269 | } |
| 270 | |
| 271 | $block->block_attrs = $this->field_args; |
| 272 | |
| 273 | return $block; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @return mixed |
| 278 | * @throws Preset_Exception |
| 279 | */ |
| 280 | public function get_expected_format() { |
| 281 | return $this->get_field_object()->expected_preset_type()[0] ?? 'raw'; |
| 282 | } |
| 283 | |
| 284 | } |
| 285 |