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