PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.1
JetFormBuilder — Dynamic Blocks Form Builder v2.1.1
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
242 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 $this->conditions = array_map( array( new Condition_Instance(), 'set_condition' ), $conditions );
137
138 return $this;
139 }
140
141 public function set_condition_operator( string $operator ): Condition_Manager {
142 $this->operator = $operator;
143
144 return $this;
145 }
146
147 protected function set_fields_conditions(): Condition_Manager {
148 return $this;
149 }
150
151 /**
152 * @param $is_success
153 * @param mixed ...$additional
154 *
155 * @throws Condition_Silence_Exception
156 */
157 public function throw_by_method( $is_success, ...$additional ) {
158 switch ( $this->operator ) {
159 case 'or':
160 if ( $is_success ) {
161 throw new Condition_Silence_Exception(
162 self::THROW_IF_ONE_MATCH,
163 ...$additional
164 );
165 }
166 break;
167 case 'and':
168 if ( ! $is_success ) {
169 throw new Condition_Silence_Exception(
170 self::THROW_IF_ONE_WRONG,
171 ...$additional
172 );
173 }
174 break;
175 }
176 }
177
178 public function get_settings() {
179 if ( ! $this->settings ) {
180 $this->settings = apply_filters(
181 'jet-form-builder/register/action-condition-settings',
182 $this->settings()
183 );
184 }
185
186 return $this->settings;
187 }
188
189 public function get_settings_list( $list_key, $return_key = 'value' ): array {
190 $list = $this->get_settings()[ $list_key ] ?? array();
191
192 return array_map(
193 function ( $operator ) use ( $return_key ) {
194 return $operator[ $return_key ] ?? false;
195 },
196 $list
197 );
198 }
199
200 /**
201 * @param $key
202 * @param $must_be_value
203 * @param $list_key
204 *
205 * @return false|array
206 */
207 public function get_settings_list_find( $key, $must_be_value, $list_key ) {
208 $list = $this->get_settings()[ $list_key ] ?? array();
209
210 foreach ( $list as $item ) {
211 if ( ( $item[ $key ] ?? false ) === $must_be_value ) {
212 return $item;
213 }
214 }
215
216 return false;
217 }
218
219 /**
220 * @param $key
221 * @param $must_be_value
222 * @param $list_key
223 * @param string $return_key
224 *
225 * @return array
226 */
227 public function get_settings_list_filter( $key, $must_be_value, $list_key, $return_key = 'value' ) {
228 $list = $this->get_settings()[ $list_key ] ?? array();
229 $filtered = array();
230
231 foreach ( $list as $item ) {
232 if ( ( $item[ $key ] ?? false ) === $must_be_value ) {
233 $filtered[] = ( false !== $return_key ) ? ( $item[ $return_key ] ?? false ) : $item;
234 }
235 }
236
237 return array_filter( $filtered );
238 }
239
240
241 }
242