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