PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.2
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / actions / methods / abstract-modifier.php
jetformbuilder / includes / actions / methods Last commit date
exceptions 2 years ago abstract-modifier.php 1 year ago base-modifier-action.php 2 years ago base-object-property.php 2 years ago object-dynamic-property.php 2 years ago object-properties-collection.php 2 years ago
abstract-modifier.php
231 lines
1 <?php
2
3 namespace Jet_Form_Builder\Actions\Methods;
4
5 use Jet_Form_Builder\Classes\Arrayable\Array_Tools;
6 use Jet_Form_Builder\Classes\Arrayable\Collection;
7 use Jet_Form_Builder\Classes\Tools;
8 use Jet_Form_Builder\Exceptions\Silence_Exception;
9
10 // If this file is called directly, abort.
11 if ( ! defined( 'WPINC' ) ) {
12 die;
13 }
14
15 abstract class Abstract_Modifier {
16
17 public $source_arr = array();
18 public $fields_map = array();
19 protected $request = array();
20
21 /** @var Object_Properties_Collection */
22 public $properties;
23
24 /** @var Collection */
25 public $actions;
26
27 /** @var Base_Modifier_Action */
28 protected $action;
29
30 abstract protected function get_actions(): Collection;
31
32 abstract protected function get_properties(): Object_Properties_Collection;
33
34 public function __construct() {
35 $this->properties = $this->get_properties();
36 // install actions
37 $this->actions = $this->get_actions();
38 }
39
40 public function run() {
41
42 /**
43 * Fires before run modifier.
44 * Allows to put custom data into the fields_map before propeties are attached.
45 *
46 * @param Abstract_Modifier $this
47 */
48 do_action( 'jet-form-builder/modifier/before-run', $this );
49
50 foreach ( $this->request as $key => $value ) {
51 $this->attach_item( $key, $value );
52 }
53
54 $this->attach_properties();
55 $this->do_action();
56
57 /** @var Base_Object_Property $property */
58 foreach ( $this->properties as $property ) {
59 $property->do_after( $this );
60 }
61
62 /**
63 * Fires after run modifier.
64 * Allows to do additional modifications.
65 *
66 * @param Abstract_Modifier $this
67 */
68 do_action( 'jet-form-builder/modifier/after-run', $this );
69 }
70
71 protected function attach_item( $field_name, $value ) {
72 $key = $this->get_field_key( $field_name );
73
74 if ( ! $key ) {
75 return;
76 }
77
78 /** @var Base_Object_Property $property */
79 foreach ( $this->properties->get_by_id( $key ) as $property ) {
80 $property->set_value( $key, $value, $this );
81 }
82
83 if ( $this->properties->has_by_id( $key ) ) {
84 return;
85 }
86
87 /** @var Base_Object_Property $item */
88 foreach ( $this->properties->get_dynamic() as $item ) {
89 $item->set_value( $key, $value, $this );
90 }
91 }
92
93 protected function attach_properties() {
94 /** @var Base_Object_Property $property */
95 foreach ( $this->properties as $property ) {
96 try {
97 $value = $property->get_value( $this );
98 } catch ( Silence_Exception $exception ) {
99 continue;
100 }
101
102 if ( ! is_array( $value ) || ! $property->is_merge_value() ) {
103 $this->source_arr[ $property->get_attach_id() ] = $value;
104
105 continue;
106 }
107
108 $this->source_arr = array_merge(
109 $this->source_arr,
110 $value
111 );
112 }
113 }
114
115 protected function do_action() {
116
117 $this->action = $this->get_action();
118
119 $this->action->set_modifier( $this );
120 $this->action->do_action();
121 $this->action->do_after();
122 }
123
124 protected function get_supported_action(): Base_Modifier_Action {
125 /** @var Base_Modifier_Action $current */
126 foreach ( Array_Tools::reverse( $this->actions ) as $current ) {
127 if ( ! $current::is_supported( $this ) ) {
128 continue;
129 }
130
131 return $current;
132 }
133
134 wp_die( 'Something went wrong' );
135 }
136
137 public function set( string $property, $value, $key = null ): Abstract_Modifier {
138 /** @var Base_Object_Property $prop_item */
139 $prop_item = $this->properties->get_by_id( $property )->current();
140 $key = $key ?? $prop_item->get_id();
141
142 $prop_item->set_value( $key, $value, $this );
143
144 return $this;
145 }
146
147 public function get( string $property ): Base_Object_Property {
148 return $this->properties->get_by_id( $property )->current();
149 }
150
151 /**
152 * @param string $field_name
153 *
154 * @return string
155 */
156 public function get_field_key( string $field_name ): string {
157 return Tools::sanitize_text_field( $this->fields_map[ $field_name ] ?? '' );
158 }
159
160 public function get_value( string $field_name ) {
161 return $this->request[ $field_name ] ?? false;
162 }
163
164 public function get_field_name_by_prop( string $search_prop ) {
165 foreach ( $this->fields_map as $field_name => $prop ) {
166 if ( $search_prop === $prop ) {
167 return $field_name;
168 }
169 }
170
171 return false;
172 }
173
174 /**
175 * [field_name] => [property]
176 *
177 * @param array $fields_map
178 *
179 * Pass TRUE, if your fields_map in such format:
180 * [property] => [field_name]
181 * @param false $revert
182 *
183 * @return $this
184 */
185 public function set_fields_map( array $fields_map, bool $revert = false ): self {
186 $fields_map = array_filter( $fields_map );
187
188 if ( ! $revert ) {
189 $this->fields_map = $fields_map;
190
191 return $this;
192 }
193
194 $this->fields_map = array_combine(
195 array_values( $fields_map ),
196 array_keys( $fields_map )
197 );
198
199 return $this;
200 }
201
202 public function set_request( $request ): Abstract_Modifier {
203 $this->request = array_merge( $this->request, $request );
204
205 return $this;
206 }
207
208 /**
209 * Get current request array.
210 *
211 * @return array
212 */
213 public function get_request(): array {
214 return $this->request;
215 }
216
217 /**
218 * Can be used after run action.
219 * Ex. in Base_Object_Property::do_after()
220 *
221 * @return Base_Modifier_Action
222 */
223 public function get_action(): Base_Modifier_Action {
224 if ( is_null( $this->action ) ) {
225 $this->action = $this->get_supported_action();
226 }
227
228 return $this->action;
229 }
230 }
231