base-source.php
3 years ago
preset-source-options-page.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
234 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 = apply_filters( 'jet-form-builder/preset-sanitize', $this->can_get_preset(), $this ); |
| 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 | |
| 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 | $func_name = self::FUNC_PREFIX . $this->prop; |
| 182 | |
| 183 | if ( is_callable( array( $this, $func_name ) ) ) { |
| 184 | return call_user_func( array( $this, $func_name ) ); |
| 185 | } |
| 186 | |
| 187 | return $this->default_prop( $this->prop ); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * @return mixed |
| 193 | * @throws Preset_Exception |
| 194 | */ |
| 195 | final public function result() { |
| 196 | $this->throw_if_preset_not_available(); |
| 197 | |
| 198 | return $this->parse_result_value( $this->get_result_on_prop() ); |
| 199 | } |
| 200 | |
| 201 | public function parse_result_value( $value ) { |
| 202 | if ( ! isset( $this->field_args['type'] ) ) { |
| 203 | return $value; |
| 204 | } |
| 205 | |
| 206 | return Preset_Manager::instance()->prepare_result( $this->field_args['type'], $value ); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * @return Base |
| 212 | * @throws Preset_Exception |
| 213 | */ |
| 214 | public function get_field_object(): Base { |
| 215 | $type = $this->field_args['type'] ?? false; |
| 216 | $block = jet_form_builder()->blocks->get_field_by_name( $type ); |
| 217 | |
| 218 | if ( ! $block ) { |
| 219 | throw new Preset_Exception( 'Undefined block_type: ' . $type, $this->field_args ); |
| 220 | } |
| 221 | |
| 222 | return $block; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * @return mixed |
| 227 | * @throws Preset_Exception |
| 228 | */ |
| 229 | public function get_expected_format() { |
| 230 | return $this->get_field_object()->expected_preset_type()[0] ?? 'raw'; |
| 231 | } |
| 232 | |
| 233 | } |
| 234 |