preset-manager.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Classes\Instance_Trait; |
| 8 | use Jet_Form_Builder\Exceptions\Plain_Default_Exception; |
| 9 | use Jet_Form_Builder\Plugin; |
| 10 | use Jet_Form_Builder\Presets\Sources\Base_Source; |
| 11 | use Jet_Form_Builder\Presets\Sources\Preset_Source_Post; |
| 12 | use Jet_Form_Builder\Presets\Sources\Preset_Source_Query_Var; |
| 13 | use Jet_Form_Builder\Presets\Sources\Preset_Source_User; |
| 14 | use Jet_Form_Builder\Presets\Types\Base_Preset; |
| 15 | use Jet_Form_Builder\Presets\Types\Dynamic_Preset; |
| 16 | use Jet_Form_Builder\Presets\Types\General_Preset; |
| 17 | |
| 18 | if ( ! defined( 'WPINC' ) ) { |
| 19 | die; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @method static Preset_Manager instance() |
| 24 | * |
| 25 | * Class Preset_Manager |
| 26 | * @package Jet_Form_Builder\Presets |
| 27 | */ |
| 28 | class Preset_Manager { |
| 29 | |
| 30 | use Instance_Trait; |
| 31 | |
| 32 | private $form_id; |
| 33 | protected $data = null; |
| 34 | protected $source = null; |
| 35 | protected $defaults = array( |
| 36 | 'enabled' => false, |
| 37 | 'from' => 'post', |
| 38 | 'post_from' => 'current_post', |
| 39 | 'user_from' => 'current_user', |
| 40 | 'query_var' => '_post_id', |
| 41 | 'fields_map' => array(), |
| 42 | ); |
| 43 | |
| 44 | public $_preset_types; |
| 45 | public $manager_preset; |
| 46 | private $general; |
| 47 | private $_source_types; |
| 48 | |
| 49 | |
| 50 | private $plain_default = false; |
| 51 | |
| 52 | |
| 53 | protected function __construct() { |
| 54 | $this->general = new General_Preset(); |
| 55 | $this->_source_types = $this->get_source_types(); |
| 56 | } |
| 57 | |
| 58 | protected function set_data() { |
| 59 | $this->general->set_init_data( $this->general->preset_source( $this->form_id ) ); |
| 60 | } |
| 61 | |
| 62 | protected function preset_types() { |
| 63 | return array( |
| 64 | new Dynamic_Preset(), |
| 65 | $this->general, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | public function get_source_types() { |
| 70 | /** @var Base_Source[] $types */ |
| 71 | |
| 72 | $types = apply_filters( 'jet-form-builder/preset/source-types', array( |
| 73 | new Preset_Source_Post(), |
| 74 | new Preset_Source_User(), |
| 75 | new Preset_Source_Query_Var() |
| 76 | ) ); |
| 77 | |
| 78 | $prepared_types = array(); |
| 79 | |
| 80 | foreach ( $types as $type ) { |
| 81 | if ( $type instanceof Base_Source ) { |
| 82 | $prepared_types[ $type->get_id() ] = $type; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $prepared_types; |
| 87 | } |
| 88 | |
| 89 | protected function set_preset_type_manager( $args ) { |
| 90 | foreach ( $this->preset_types() as $type ) { |
| 91 | try { |
| 92 | if ( $type instanceof Base_Preset && $type->is_active_preset( $args ) ) { |
| 93 | $this->manager_preset = $type; |
| 94 | break; |
| 95 | } |
| 96 | } catch ( Plain_Default_Exception $exception ) { |
| 97 | $this->plain_default = $exception->getMessage(); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | protected function register_preset_type( Base_Preset $type ) { |
| 104 | $this->_preset_types[] = $type; |
| 105 | } |
| 106 | |
| 107 | public function set_form_id( $form_id ) { |
| 108 | $this->form_id = $form_id; |
| 109 | $this->set_data(); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Sanitize preset source |
| 115 | * |
| 116 | * @return [type] [description] |
| 117 | */ |
| 118 | public function sanitize_source() { |
| 119 | if ( empty( $this->general->data['enabled'] ) ) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | if ( ! $this->general->source instanceof Base_Source || ! $this->general->source->src() ) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | return $this->general->source->on_sanitize(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns field value |
| 132 | * |
| 133 | * @param array $args |
| 134 | * |
| 135 | * @return [type] [description] |
| 136 | */ |
| 137 | public function get_field_value( $args = array() ) { |
| 138 | $this->manager_preset = null; |
| 139 | |
| 140 | if ( empty( $args['name'] ) ) { |
| 141 | return ''; |
| 142 | } |
| 143 | |
| 144 | $this->set_preset_type_manager( $args ); |
| 145 | |
| 146 | if ( $this->plain_default ) { |
| 147 | return $this->get_plain_default(); |
| 148 | } |
| 149 | |
| 150 | if ( $this->manager_preset instanceof Base_Preset ) { |
| 151 | return $this->manager_preset->set_additional_data( $args )->source->result(); |
| 152 | } |
| 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | public function get_plain_default() { |
| 158 | $value = $this->plain_default; |
| 159 | $this->plain_default = false; |
| 160 | |
| 161 | return $value; |
| 162 | } |
| 163 | |
| 164 | public function get_source_by_type( $type ) { |
| 165 | if ( ! isset( $this->_source_types[ $type ] ) ) { |
| 166 | _doing_it_wrong( |
| 167 | __METHOD__, |
| 168 | "Undefined preset source type: {$type}", |
| 169 | '1.2.4' |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | return clone $this->_source_types[ $type ]; |
| 174 | } |
| 175 | |
| 176 | } |