PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.5
JetFormBuilder — Dynamic Blocks Form Builder v2.1.5
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 / conditions / condition-manager.php
jetformbuilder / includes / actions / conditions Last commit date
condition-instance.php 3 years ago condition-manager.php 3 years ago
condition-manager.php
246 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Actions\Conditions;
5
6 use Jet_Form_Builder\Exceptions\Condition_Exception;
7 use Jet_Form_Builder\Exceptions\Condition_Silence_Exception;
8
9 class Condition_Manager {
10
11 const THROW_IF_ONE_MATCH = 'throw_out_if_at_least_one_match';
12 const THROW_IF_ONE_WRONG = 'throw_out_if_at_least_one_wrong';
13 const TRANSFORM_DATE_TO_TIMESTAMP = 'date_to_timestamp';
14
15 /** @var Condition_Instance[] */
16 private $conditions;
17 private $operator;
18 private $settings;
19
20 private function settings(): array {
21 return array(
22 'item' => array(
23 'execute' => true,
24 'operator' => '',
25 'field' => '',
26 'default' => '',
27 'compare_value_format_type' => '',
28 'compare_value_format_custom' => '',
29 ),
30 'compare_value_formats' => array(
31 array(
32 'label' => '--',
33 'value' => '',
34 ),
35 array(
36 'value' => self::TRANSFORM_DATE_TO_TIMESTAMP,
37 'callback' => 'strtotime',
38 'label' => __( 'String to Timestamp', 'jet-form-builder' ),
39 'help' => sprintf(
40 // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
41 __( 'Useful when comparing dates. See more here: %1$sstrtotime%2$s', 'jet-form-builder' ),
42 '<a target="_blank" href="https://www.php.net/manual/en/function.strtotime.php">',
43 '</a>'
44 ),
45 ),
46 ),
47 'help_for_exploding_compare' => __( 'List the values separated by commas', 'jet-form-builder' ),
48 'operators' => array(
49 array(
50 'label' => '--',
51 'value' => '',
52 ),
53 array(
54 'label' => __( 'Equal', 'jet-form-builder' ),
55 'value' => 'equal',
56 ),
57 array(
58 'label' => __( 'Greater than', 'jet-form-builder' ),
59 'value' => 'greater',
60 ),
61 array(
62 'label' => __( 'Less than', 'jet-form-builder' ),
63 'value' => 'less',
64 ),
65 array(
66 'label' => __( 'Between', 'jet-form-builder' ),
67 'value' => 'between',
68 'need_explode' => true,
69 ),
70 array(
71 'label' => __( 'In the list', 'jet-form-builder' ),
72 'value' => 'one_of',
73 'need_explode' => true,
74 ),
75 array(
76 'label' => __( 'Contain text', 'jet-form-builder' ),
77 'value' => 'contain',
78 ),
79 ),
80 );
81 }
82
83 public function get_transformers( $value_key = 'value' ): array {
84 return $this->get_settings_list( 'compare_value_formats', $value_key );
85 }
86
87 /**
88 * @param $key
89 * @param $must_be_value
90 * @param string $return_key
91 *
92 * @return array
93 */
94 public function get_transformers_find( $key, $must_be_value ): array {
95 return $this->get_settings_list_find( $key, $must_be_value, 'compare_value_formats' );
96 }
97
98 public function get_operators_list( $value_key = 'value' ): array {
99 return $this->get_settings_list( 'operators', $value_key );
100 }
101
102 public function get_operators_filter( $key, $must_be_value, $return_key = 'value' ) {
103 return $this->get_settings_list_filter( $key, $must_be_value, 'operators', $return_key );
104 }
105
106
107 /**
108 * @throws Condition_Exception
109 */
110 public function check_all() {
111 if ( empty( $this->conditions ) ) {
112 return;
113 }
114
115 foreach ( $this->conditions as $condition ) {
116 try {
117 $condition->is_correct_with_throw();
118
119 } catch ( Condition_Silence_Exception $exception ) {
120 switch ( $exception->getMessage() ) {
121 case self::THROW_IF_ONE_MATCH:
122 return;
123 case self::THROW_IF_ONE_WRONG:
124 default:
125 throw new Condition_Exception( $exception->getMessage(), ...$exception->get_additional() );
126 }
127 }
128 }
129
130 if ( 'or' === $this->operator ) {
131 throw new Condition_Exception( 'None of the conditions are met' );
132 }
133 }
134
135 public function set_conditions( array $conditions ): Condition_Manager {
136 $instance = new Condition_Instance();
137
138 foreach ( $conditions as $condition ) {
139 $this->conditions[] = ( clone $instance )->set_condition( $condition );
140 }
141
142 return $this;
143 }
144
145 public function set_condition_operator( string $operator ): Condition_Manager {
146 $this->operator = $operator;
147
148 return $this;
149 }
150
151 protected function set_fields_conditions(): Condition_Manager {
152 return $this;
153 }
154
155 /**
156 * @param $is_success
157 * @param mixed ...$additional
158 *
159 * @throws Condition_Silence_Exception
160 */
161 public function throw_by_method( $is_success, ...$additional ) {
162 switch ( $this->operator ) {
163 case 'or':
164 if ( $is_success ) {
165 throw new Condition_Silence_Exception(
166 self::THROW_IF_ONE_MATCH,
167 ...$additional
168 );
169 }
170 break;
171 case 'and':
172 if ( ! $is_success ) {
173 throw new Condition_Silence_Exception(
174 self::THROW_IF_ONE_WRONG,
175 ...$additional
176 );
177 }
178 break;
179 }
180 }
181
182 public function get_settings() {
183 if ( ! $this->settings ) {
184 $this->settings = apply_filters(
185 'jet-form-builder/register/action-condition-settings',
186 $this->settings()
187 );
188 }
189
190 return $this->settings;
191 }
192
193 public function get_settings_list( $list_key, $return_key = 'value' ): array {
194 $list = $this->get_settings()[ $list_key ] ?? array();
195
196 return array_map(
197 function ( $operator ) use ( $return_key ) {
198 return $operator[ $return_key ] ?? false;
199 },
200 $list
201 );
202 }
203
204 /**
205 * @param $key
206 * @param $must_be_value
207 * @param $list_key
208 *
209 * @return false|array
210 */
211 public function get_settings_list_find( $key, $must_be_value, $list_key ) {
212 $list = $this->get_settings()[ $list_key ] ?? array();
213
214 foreach ( $list as $item ) {
215 if ( ( $item[ $key ] ?? false ) === $must_be_value ) {
216 return $item;
217 }
218 }
219
220 return false;
221 }
222
223 /**
224 * @param $key
225 * @param $must_be_value
226 * @param $list_key
227 * @param string $return_key
228 *
229 * @return array
230 */
231 public function get_settings_list_filter( $key, $must_be_value, $list_key, $return_key = 'value' ) {
232 $list = $this->get_settings()[ $list_key ] ?? array();
233 $filtered = array();
234
235 foreach ( $list as $item ) {
236 if ( ( $item[ $key ] ?? false ) === $must_be_value ) {
237 $filtered[] = ( false !== $return_key ) ? ( $item[ $return_key ] ?? false ) : $item;
238 }
239 }
240
241 return array_filter( $filtered );
242 }
243
244
245 }
246