| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Presets\Sources; |
| 5 |
|
| 6 |
|
| 7 |
use Jet_Form_Builder\Classes\Tools; |
| 8 |
use Jet_Form_Builder\Dev_Mode\Manager; |
| 9 |
use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 10 |
use Jet_Form_Builder\Presets\Types\Base_Preset; |
| 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 |
const FUNC_PREFIX = '_source__'; |
| 23 |
const EMPTY = ''; |
| 24 |
|
| 25 |
abstract public function query_source(); |
| 26 |
|
| 27 |
public function __construct( $fields_map, $field_args, $preset_data ) { |
| 28 |
$this->fields_map = $fields_map; |
| 29 |
$this->field_args = $field_args; |
| 30 |
$this->preset_data = $preset_data; |
| 31 |
$this->field = $this->field_args['name']; |
| 32 |
$this->field_data = $this->get_field_data(); |
| 33 |
$this->prop = $this->get_prop(); |
| 34 |
$this->src = $this->query_source(); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
protected function get_field_data() { |
| 39 |
if ( $this->has_field_in_map() ) { |
| 40 |
return $this->fields_map[ $this->field ]; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public function has_field_in_map() { |
| 45 |
return ( isset( $this->fields_map[ $this->field ] ) && ( isset( $this->fields_map[ $this->field ]['prop'] ) || isset( $this->fields_map[ $this->field ]['key'] ) ) ); |
| 46 |
} |
| 47 |
|
| 48 |
public function src() { |
| 49 |
return $this->src; |
| 50 |
} |
| 51 |
|
| 52 |
protected function can_get_preset() { |
| 53 |
return ( ! empty( $this->src() ) && ! is_wp_error( $this->src() ) ); |
| 54 |
} |
| 55 |
|
| 56 |
public function default_prop( string $prop ) { |
| 57 |
$source = $this->src; |
| 58 |
|
| 59 |
if ( isset( $source->$prop ) ) { |
| 60 |
return $source->$prop; |
| 61 |
} elseif ( isset( $source->data ) && isset( $source->data->$prop ) ) { |
| 62 |
return $source->data->$prop; |
| 63 |
} |
| 64 |
|
| 65 |
return self::EMPTY; |
| 66 |
} |
| 67 |
|
| 68 |
protected function get_prop() { |
| 69 |
return ( ! empty( $this->field_data['prop'] ) ? $this->field_data['prop'] : 'post_title' ); |
| 70 |
} |
| 71 |
|
| 72 |
public function get_result_on_prop() { |
| 73 |
$func_name = self::FUNC_PREFIX . $this->prop; |
| 74 |
|
| 75 |
if ( is_callable( array( $this, $func_name ) ) ) { |
| 76 |
return call_user_func( array( $this, $func_name ) ); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->default_prop( $this->prop ); |
| 80 |
} |
| 81 |
|
| 82 |
final public function result() { |
| 83 |
if ( ! $this->can_get_preset() ) { |
| 84 |
return self::EMPTY; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->parse_result_value( $this->get_result_on_prop() ); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Try to get values from request if passed |
| 93 |
* |
| 94 |
* @param [type] $args [description] |
| 95 |
* |
| 96 |
* @return [type] [description] |
| 97 |
*/ |
| 98 |
public function maybe_adjust_value() { |
| 99 |
|
| 100 |
$value = isset( $this->field_args['default'] ) ? $this->field_args['default'] : ''; |
| 101 |
$request_val = ! empty( $_REQUEST['values'] ) ? $_REQUEST['values'] : array(); |
| 102 |
|
| 103 |
if ( isset( $request_val[ $this->field ] ) ) { |
| 104 |
$value = $request_val[ $this->field ]; |
| 105 |
} |
| 106 |
|
| 107 |
return $value; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public function parse_result_value( $value ) { |
| 112 |
// Prepare value for date field |
| 113 |
if ( 'date-field' === $this->field_args['type'] && Tools::is_valid_timestamp( $value ) ) { |
| 114 |
$value = date_i18n( 'Y-m-d', $value ); |
| 115 |
} |
| 116 |
|
| 117 |
return $value; |
| 118 |
} |
| 119 |
|
| 120 |
} |