PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.5
JetFormBuilder — Dynamic Blocks Form Builder v2.0.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 / condition-manager.php
jetformbuilder / includes / actions Last commit date
executors 4 years ago methods 4 years ago types 4 years ago action-handler.php 4 years ago action-localize.php 4 years ago condition-instance.php 4 years ago condition-manager.php 4 years ago manager.php 4 years ago
condition-manager.php
246 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Actions;
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 private $conditions;
16 private $operator;
17 private $settings;
18
19 private function settings(): array {
20 return array(
21 'item' => array(
22 'execute' => true,
23 'operator' => '',
24 'field' => '',
25 'default' => '',
26 'compare_value_format_type' => '',
27 'compare_value_format_custom' => '',
28 ),
29 'compare_value_formats' => array(
30 array(
31 'label' => '--',
32 'value' => '',
33 ),
34 array(
35 'value' => self::TRANSFORM_DATE_TO_TIMESTAMP,
36 'callback' => 'strtotime',
37 'label' => __( 'String to Timestamp', 'jet-form-builder' ),
38 'help' => sprintf(
39 // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment
40 __( 'Useful when comparing dates. See more here: %1$sstrtotime%2$s', 'jet-form-builder' ),
41 '<a target="_blank" href="https://www.php.net/manual/en/function.strtotime.php">',
42 '</a>'
43 ),
44 ),
45 ),
46 'help_for_exploding_compare' => __( 'List the values separated by commas', 'jet-form-builder' ),
47 'operators' => array(
48 array(
49 'label' => '--',
50 'value' => '',
51 ),
52 array(
53 'label' => __( 'Equal', 'jet-form-builder' ),
54 'value' => 'equal',
55 ),
56 array(
57 'label' => __( 'Greater than', 'jet-form-builder' ),
58 'value' => 'greater',
59 ),
60 array(
61 'label' => __( 'Less than', 'jet-form-builder' ),
62 'value' => 'less',
63 ),
64 array(
65 'label' => __( 'Between', 'jet-form-builder' ),
66 'value' => 'between',
67 'need_explode' => true,
68 ),
69 array(
70 'label' => __( 'In the list', 'jet-form-builder' ),
71 'value' => 'one_of',
72 'need_explode' => true,
73 ),
74 array(
75 'label' => __( 'Contain text', 'jet-form-builder' ),
76 'value' => 'contain',
77 ),
78 ),
79 );
80 }
81
82 public function get_transformers( $value_key = 'value' ): array {
83 return $this->get_settings_list( 'compare_value_formats', $value_key );
84 }
85
86 /**
87 * @param $key
88 * @param $must_be_value
89 * @param string $return_key
90 *
91 * @return array
92 */
93 public function get_transformers_find( $key, $must_be_value ): array {
94 return $this->get_settings_list_find( $key, $must_be_value, 'compare_value_formats' );
95 }
96
97 public function get_operators_list( $value_key = 'value' ): array {
98 return $this->get_settings_list( 'operators', $value_key );
99 }
100
101 public function get_operators_filter( $key, $must_be_value, $return_key = 'value' ) {
102 return $this->get_settings_list_filter( $key, $must_be_value, 'operators', $return_key );
103 }
104
105
106 /**
107 * @throws Condition_Exception
108 */
109 public function check_all() {
110 if ( empty( $this->conditions ) ) {
111 return;
112 }
113
114 foreach ( $this->conditions as $condition ) {
115 try {
116 $this->is_correct( $condition );
117
118 } catch ( Condition_Silence_Exception $exception ) {
119 switch ( $exception->getMessage() ) {
120 case self::THROW_IF_ONE_MATCH:
121 return;
122 case self::THROW_IF_ONE_WRONG:
123 default:
124 throw new Condition_Exception( $exception->getMessage(), ...$exception->get_additional() );
125 }
126 }
127 }
128
129 if ( 'or' === $this->operator ) {
130 throw new Condition_Exception( 'None of the conditions are met' );
131 }
132 }
133
134 /**
135 * @param $condition
136 *
137 * @throws Condition_Silence_Exception
138 */
139 public function is_correct( $condition ) {
140 ( new Condition_Instance() )->set_condition( $condition )->is_correct_with_throw();
141 }
142
143 public function set_conditions( array $conditions ): Condition_Manager {
144 $this->conditions = $conditions;
145
146 return $this;
147 }
148
149 public function set_condition_operator( string $operator ): Condition_Manager {
150 $this->operator = $operator;
151
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