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