PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.01.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.01.02
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmForm.php +172 -635 6.284.01.02 View file →
@@ -5,49 +5,38 @@
5 5
6 6 class FrmForm {
7 7
8 8 /**
9 - * @param array $values
10 - *
11 - * @return bool|int id on success or false on failure.
9 + * @return int|boolean id on success or false on failure
12 10 */
13 11 public static function create( $values ) {
14 12 global $wpdb;
15 13
16 - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
17 -
18 14 $new_values = array(
19 15 'form_key' => FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key' ),
20 16 'name' => $values['name'],
21 17 'description' => $values['description'],
22 - 'status' => $values['status'] ?? 'published',
23 - 'logged_in' => $values['logged_in'] ?? 0,
18 + 'status' => isset( $values['status'] ) ? $values['status'] : 'published',
19 + 'logged_in' => isset( $values['logged_in'] ) ? $values['logged_in'] : 0,
24 20 'is_template' => isset( $values['is_template'] ) ? (int) $values['is_template'] : 0,
25 21 'parent_form_id' => isset( $values['parent_form_id'] ) ? absint( $values['parent_form_id'] ) : 0,
26 22 'editable' => isset( $values['editable'] ) ? (int) $values['editable'] : 0,
27 - 'created_at' => $values['created_at'] ?? current_time( 'mysql', 1 ),
23 + 'created_at' => isset( $values['created_at'] ) ? $values['created_at'] : current_time( 'mysql', 1 ),
28 24 );
29 25
30 26 $options = isset( $values['options'] ) ? (array) $values['options'] : array();
31 27 FrmFormsHelper::fill_form_options( $options, $values );
32 28
33 - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
34 - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' );
35 - $options['submit_html'] = $values['options']['submit_html'] ?? FrmFormsHelper::get_default_html( 'submit' );
29 + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
30 + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
31 + $options['submit_html'] = isset( $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
36 32
37 - /**
38 - * Allows modifying form options before updating or creating.
39 - *
40 - * @since 5.4 Add the third param.
41 - *
42 - * @param array $options Form options.
43 - * @param array $values Form data.
44 - * @param bool $update Is form updating or creating. It's `true` if is updating.
45 - */
46 - $options = apply_filters( 'frm_form_options_before_update', $options, $values, false );
47 - $options = self::maybe_filter_form_options( $options );
33 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
48 34 $new_values['options'] = serialize( $options );
49 35
36 + //if(isset($values['id']) && is_numeric($values['id']))
37 + // $new_values['id'] = $values['id'];
38 +
50 39 $wpdb->insert( $wpdb->prefix . 'frm_forms', $new_values );
51 40
52 41 $id = $wpdb->insert_id;
53 42
@@ -57,36 +46,14 @@
57 46 return $id;
58 47 }
59 48
60 49 /**
61 - * @since 5.0.08
62 - *
63 - * @param array $options
64 - *
65 - * @return array
50 + * @return int|boolean ID on success or false on failure
66 51 */
67 - private static function maybe_filter_form_options( $options ) {
68 - if ( ! FrmAppHelper::allow_unfiltered_html() && ! empty( $options['submit_html'] ) ) {
69 - $options['submit_html'] = FrmAppHelper::kses_submit_button( $options['submit_html'] );
70 - }
71 - return FrmAppHelper::maybe_filter_array( $options, array( 'submit_value', 'success_msg', 'before_html', 'after_html' ) );
72 - }
73 -
74 - /**
75 - * Duplicate a form.
76 - *
77 - * @param int $id Form ID to duplicate.
78 - * @param bool $template Whether the duplicated form is a template.
79 - * @param bool $copy_keys Whether to copy the original form key.
80 - * @param false|int $blog_id Blog ID when duplicating across sites, or false for current site.
81 - *
82 - * @return bool|int ID on success or false on failure
83 - */
84 52 public static function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
85 53 global $wpdb;
86 54
87 55 $values = self::getOne( $id, $blog_id );
88 -
89 56 if ( ! $values ) {
90 57 return false;
91 58 }
92 59
@@ -103,11 +70,10 @@
103 70 'is_template' => $template ? 1 : 0,
104 71 );
105 72
106 73 if ( $blog_id ) {
107 - $new_values['status'] = 'published';
108 - $new_options = $values->options;
109 - FrmAppHelper::unserialize_or_decode( $new_options );
74 + $new_values['status'] = 'published';
75 + $new_options = maybe_unserialize( $values->options );
110 76 $new_options['email_to'] = get_option( 'admin_email' );
111 77 $new_options['copy'] = false;
112 78 $new_values['options'] = $new_options;
113 79 } else {
@@ -126,9 +92,9 @@
126 92
127 93 $form_id = $wpdb->insert_id;
128 94 FrmField::duplicate( $id, $form_id, $copy_keys, $blog_id );
129 95
130 - // Update form settings after fields are created
96 + // update form settings after fields are created
131 97 do_action( 'frm_after_duplicate_form', $form_id, $new_values, array( 'old_id' => $id ) );
132 98
133 99 return $form_id;
134 100 }
@@ -135,17 +101,10 @@
135 101
136 102 return false;
137 103 }
138 104
139 - /**
140 - * @param int $form_id
141 - * @param array $values
142 - *
143 - * @return void
144 - */
145 105 public static function after_duplicate( $form_id, $values ) {
146 - $new_opts = $values['options'];
147 - FrmAppHelper::unserialize_or_decode( $new_opts );
106 + $new_opts = maybe_unserialize( $values['options'] );
148 107 $values['options'] = $new_opts;
149 108
150 109 if ( isset( $new_opts['success_msg'] ) ) {
151 110 $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids( $new_opts['success_msg'] );
@@ -152,110 +111,20 @@
152 111 }
153 112
154 113 $new_opts = apply_filters( 'frm_after_duplicate_form_values', $new_opts, $form_id );
155 114
156 - // phpcs:ignore Universal.Operators.StrictComparisons
157 115 if ( $new_opts != $values['options'] ) {
158 116 global $wpdb;
159 117 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'options' => maybe_serialize( $new_opts ) ), array( 'id' => $form_id ) );
160 118 }
161 -
162 - self::switch_field_ids_in_fields( $form_id );
163 119 }
164 120
165 121 /**
166 - * Switches field ID in fields.
167 - *
168 - * @since 5.3
169 - *
170 - * @param int $form_id Form ID.
171 - *
172 - * @return void
122 + * @return int|boolean
173 123 */
174 - private static function switch_field_ids_in_fields( $form_id ) {
175 - global $wpdb;
176 -
177 - // Keys of fields that you want to check to replace field ID.
178 - $keys = array( 'default_value', 'field_options' );
179 - $sql_cols = 'fi.id';
180 -
181 - foreach ( $keys as $key ) {
182 - $sql_cols .= ',fi.' . $key;
183 - }
184 -
185 - $fields = FrmDb::get_results(
186 - "{$wpdb->prefix}frm_fields AS fi LEFT OUTER JOIN {$wpdb->prefix}frm_forms AS fr ON fi.form_id = fr.id",
187 - array(
188 - 'or' => 1,
189 - 'fi.form_id' => $form_id,
190 - 'fr.parent_form_id' => $form_id,
191 - ),
192 - $sql_cols
193 - );
194 -
195 - if ( ! $fields || ! is_array( $fields ) ) {
196 - return;
197 - }
198 -
199 - foreach ( $fields as $field ) {
200 - self::switch_field_ids_in_field( (array) $field );
201 - }
202 - }
203 -
204 - /**
205 - * Switches field ID in a field.
206 - *
207 - * @since 5.3
208 - *
209 - * @param array $field Field array.
210 - *
211 - * @return void
212 - */
213 - private static function switch_field_ids_in_field( $field ) {
214 - $new_values = array();
215 -
216 - foreach ( $field as $key => $value ) {
217 - if ( 'id' === $key || ! $value ) {
218 - continue;
219 - }
220 -
221 - if ( ! is_string( $value ) && ! is_array( $value ) ) {
222 - continue;
223 - }
224 -
225 - if ( 'field_options' === $key ) {
226 - // Need to loop through field_options to prevent breaking serialized string when length changed.
227 - FrmAppHelper::unserialize_or_decode( $value );
228 - $new_val = FrmFieldsHelper::switch_field_ids( $value );
229 - $new_val = serialize( $new_val );
230 - } else {
231 - $new_val = FrmFieldsHelper::switch_field_ids( $value );
232 - }
233 -
234 - if ( $new_val !== $value ) {
235 - $new_values[ $key ] = $new_val;
236 - }
237 - }//end foreach
238 -
239 - if ( $new_values ) {
240 - FrmField::update( $field['id'], $new_values );
241 - }
242 - }
243 -
244 - /**
245 - * Update a form.
246 - *
247 - * @param int $id Form ID.
248 - * @param array $values Form values to update.
249 - * @param bool $create_link Whether this is being called from link creation.
250 - *
251 - * @return bool|int
252 - */
253 124 public static function update( $id, $values, $create_link = false ) {
254 125 global $wpdb;
255 126
256 - $values = FrmAppHelper::maybe_filter_array( $values, array( 'name', 'description' ) );
257 -
258 127 if ( ! isset( $values['status'] ) && ( $create_link || isset( $values['options'] ) || isset( $values['item_meta'] ) || isset( $values['field_options'] ) ) ) {
259 128 $values['status'] = 'published';
260 129 }
261 130
@@ -263,23 +132,23 @@
263 132 $values['form_key'] = FrmAppHelper::get_unique_key( $values['form_key'], $wpdb->prefix . 'frm_forms', 'form_key', $id );
264 133 }
265 134
266 135 $form_fields = array( 'form_key', 'name', 'description', 'status', 'parent_form_id' );
267 - $new_values = self::set_update_options( array(), $values, array( 'form_id' => $id ) );
268 136
137 + $new_values = self::set_update_options( array(), $values );
138 +
269 139 foreach ( $values as $value_key => $value ) {
270 - if ( $value_key && in_array( $value_key, $form_fields, true ) ) {
140 + if ( $value_key && in_array( $value_key, $form_fields ) ) {
271 141 $new_values[ $value_key ] = $value;
272 142 }
273 143 }
274 144
275 - if ( ! empty( $values['new_status'] ) ) {
145 + if ( isset( $values['new_status'] ) && ! empty( $values['new_status'] ) ) {
276 146 $new_values['status'] = $values['new_status'];
277 147 }
278 148
279 - if ( $new_values ) {
149 + if ( ! empty( $new_values ) ) {
280 150 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', $new_values, array( 'id' => $id ) );
281 -
282 151 if ( $query_results ) {
283 152 self::clear_form_cache();
284 153 }
285 154 } else {
@@ -295,38 +164,24 @@
295 164 return $query_results;
296 165 }
297 166
298 167 /**
299 - * @param array $new_values
300 - * @param array $values
301 - * @param array $args
302 - *
303 168 * @return array
304 169 */
305 - public static function set_update_options( $new_values, $values, $args = array() ) {
170 + public static function set_update_options( $new_values, $values ) {
306 171 if ( ! isset( $values['options'] ) ) {
307 172 return $new_values;
308 173 }
309 174
310 - $options = ! empty( $values['options'] ) ? (array) $values['options'] : array();
175 + $options = isset( $values['options'] ) ? (array) $values['options'] : array();
311 176 FrmFormsHelper::fill_form_options( $options, $values );
312 177
313 - $options['custom_style'] = $values['options']['custom_style'] ?? 0;
314 - $options['before_html'] = $values['options']['before_html'] ?? FrmFormsHelper::get_default_html( 'before' );
315 - $options['after_html'] = $values['options']['after_html'] ?? FrmFormsHelper::get_default_html( 'after' );
316 - $options['submit_html'] = isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
178 + $options['custom_style'] = isset( $values['options']['custom_style'] ) ? $values['options']['custom_style'] : 0;
179 + $options['before_html'] = isset( $values['options']['before_html'] ) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html( 'before' );
180 + $options['after_html'] = isset( $values['options']['after_html'] ) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html( 'after' );
181 + $options['submit_html'] = ( isset( $values['options']['submit_html'] ) && '' !== $values['options']['submit_html'] ) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html( 'submit' );
317 182
318 - /**
319 - * Allows modifying form options before updating or creating.
320 - *
321 - * @since 5.4 Added the third param.
322 - *
323 - * @param array $options Form options.
324 - * @param array $values Form data.
325 - * @param bool $update Is form updating or creating. It's `true` if is updating.
326 - */
327 - $options = apply_filters( 'frm_form_options_before_update', $options, $values, true );
328 - $options = self::maybe_filter_form_options( $options );
183 + $options = apply_filters( 'frm_form_options_before_update', $options, $values );
329 184 $new_values['options'] = serialize( $options );
330 185
331 186 return $new_values;
332 187 }
@@ -331,14 +186,11 @@
331 186 return $new_values;
332 187 }
333 188
334 189 /**
335 - * @param int $id Form ID.
336 - * @param array $values Form values array.
337 - *
338 190 * @return array
339 191 */
340 - public static function update_fields( $id, $values ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
192 + public static function update_fields( $id, $values ) {
341 193
342 194 if ( ! isset( $values['item_meta'] ) && ! isset( $values['field_options'] ) ) {
343 195 return $values;
344 196 }
@@ -343,10 +195,9 @@
343 195 return $values;
344 196 }
345 197
346 198 $all_fields = FrmField::get_all_for_form( $id );
347 -
348 - if ( ! $all_fields ) {
199 + if ( empty( $all_fields ) ) {
349 200 return $values;
350 201 }
351 202
352 203 if ( ! isset( $values['item_meta'] ) ) {
@@ -354,28 +205,28 @@
354 205 }
355 206
356 207 $field_array = array();
357 208 $existing_keys = array_keys( $values['item_meta'] );
358 -
359 209 foreach ( $all_fields as $fid ) {
360 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict, SlevomatCodingStandard.Files.LineLength.LineTooLong
361 210 if ( ! in_array( $fid->id, $existing_keys ) && ( isset( $values['frm_fields_submitted'] ) && in_array( $fid->id, $values['frm_fields_submitted'] ) ) || isset( $values['options'] ) ) {
362 211 $values['item_meta'][ $fid->id ] = '';
363 212 }
364 -
365 213 $field_array[ $fid->id ] = $fid;
366 214 }
367 215 unset( $all_fields );
368 216
369 217 foreach ( $values['item_meta'] as $field_id => $default_value ) {
370 - $field = $field_array[ $field_id ] ?? FrmField::getOne( $field_id );
218 + if ( isset( $field_array[ $field_id ] ) ) {
219 + $field = $field_array[ $field_id ];
220 + } else {
221 + $field = FrmField::getOne( $field_id );
222 + }
371 223
372 224 if ( ! $field ) {
373 225 continue;
374 226 }
375 227
376 - $is_settings_page = isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] );
377 -
228 + $is_settings_page = ( isset( $values['options'] ) || isset( $values['field_options'][ 'custom_html_' . $field_id ] ) );
378 229 if ( $is_settings_page ) {
379 230 self::get_settings_page_html( $values, $field );
380 231
381 232 if ( ! defined( 'WP_IMPORTING' ) ) {
@@ -382,20 +233,15 @@
382 233 continue;
383 234 }
384 235 }
385 236
386 - // Updating the form.
237 + //updating the form
387 238 $update_options = FrmFieldsHelper::get_default_field_options_from_field( $field );
388 - // Don't check for POST html.
389 - unset( $update_options['custom_html'] );
239 + unset( $update_options['custom_html'] ); // don't check for POST html
390 240 $update_options = apply_filters( 'frm_field_options_to_update', $update_options );
391 241
392 - if ( ! is_array( $field->field_options ) ) {
393 - $field->field_options = array();
394 - }
395 -
396 242 foreach ( $update_options as $opt => $default ) {
397 - $field->field_options[ $opt ] = $values['field_options'][ $opt . '_' . $field_id ] ?? $default;
243 + $field->field_options[ $opt ] = isset( $values['field_options'][ $opt . '_' . $field_id ] ) ? $values['field_options'][ $opt . '_' . $field_id ] : $default;
398 244 self::sanitize_field_opt( $opt, $field->field_options[ $opt ] );
399 245 }
400 246
401 247 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
@@ -401,160 +247,54 @@
401 247 $field->field_options = apply_filters( 'frm_update_field_options', $field->field_options, $field, $values );
402 248
403 249 $new_field = array(
404 250 'field_options' => $field->field_options,
405 - 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? FrmAppHelper::maybe_json_encode( $values[ 'default_value_' . $field_id ] ) : '',
251 + 'default_value' => isset( $values[ 'default_value_' . $field_id ] ) ? maybe_serialize( $values[ 'default_value_' . $field_id ] ) : '',
406 252 );
407 253
408 - if ( ! FrmAppHelper::allow_unfiltered_html() && isset( $values['field_options'][ 'options_' . $field_id ] ) && is_array( $values['field_options'][ 'options_' . $field_id ] ) ) { // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
409 - foreach ( $values['field_options'][ 'options_' . $field_id ] as $option_key => $option ) {
410 - if ( is_array( $option ) ) {
411 - foreach ( $option as $key => $item ) {
412 - $values['field_options'][ 'options_' . $field_id ][ $option_key ][ $key ] = FrmAppHelper::kses( $item, 'all' );
413 - }
414 - }
415 - }
416 - }
417 -
418 254 self::prepare_field_update_values( $field, $values, $new_field );
419 - self::maybe_update_max_option( $field, $values, $new_field );
420 255
421 256 FrmField::update( $field_id, $new_field );
422 257
423 258 FrmField::delete_form_transient( $field->form_id );
424 - }//end foreach
259 + }
425 260 self::clear_form_cache();
426 261
427 262 return $values;
428 263 }
429 264
430 - /**
431 - * Resets the 'max' option of a field when changing paragraph field type to other field types like text, email etc.
432 - *
433 - * @since 6.7
434 - *
435 - * @param object $field
436 - * @param array $values
437 - * @param array $new_field
438 - *
439 - * @return void
440 - */
441 - private static function maybe_update_max_option( $field, $values, &$new_field ) {
442 - if ( $field->type === 'textarea' &&
443 - ! empty( $values['field_options'][ 'type_' . $field->id ] ) &&
444 - in_array( $values['field_options'][ 'type_' . $field->id ], array( 'text', 'email', 'url', 'password', 'phone' ), true ) ) {
445 - $new_field['field_options']['max'] = '';
446 -
447 - /**
448 - * Update posted field setting so that new 'max' option is displayed after form is saved and page reloads.
449 - * FrmFieldsHelper::fill_default_field_opts populates field options by calling self::get_posted_field_setting.
450 - */
451 - $_POST['field_options'][ 'max_' . $field->id ] = '';
452 - }
453 - }
454 -
455 - /**
456 - * @param string $opt
457 - * @param mixed $value
458 - *
459 - * @return void
460 - */
461 265 private static function sanitize_field_opt( $opt, &$value ) {
462 - if ( ! is_string( $value ) ) {
463 - return;
266 + if ( is_string( $value ) ) {
267 + if ( $opt === 'calc' ) {
268 + $value = strip_tags( $value );
269 + } else {
270 + $value = FrmAppHelper::kses( $value, 'all' );
271 + }
272 + $value = trim( $value );
464 273 }
465 -
466 - /**
467 - * Allow the option to turn off sanitization for a field. This way a custom rule can be used instead.
468 - * Make sure to add custom sanitization using the frm_update_field_options filter as the data will no longer be sanitized.
469 - *
470 - * @since 6.0
471 - *
472 - * @param bool $should_sanitize
473 - * @param string $opt
474 - */
475 - $should_sanitize = apply_filters( 'frm_should_sanitize_field_opt_string', true, $opt );
476 -
477 - if ( ! $should_sanitize ) {
478 - return;
479 - }
480 -
481 - $value = $opt === 'calc' ? self::sanitize_calc( $value ) : FrmAppHelper::kses( $value, 'all' );
482 - $value = trim( $value );
483 274 }
484 275
485 276 /**
486 - * @param string $value
487 - *
488 - * @return string
489 - */
490 - private static function sanitize_calc( $value ) {
491 - if ( str_contains( $value, '<' ) ) {
492 - $value = self::normalize_calc_spaces( $value );
493 - }
494 - // Allow <= and >=.
495 - $allow = array( '<= ', ' >=' );
496 - $temp = array( '< = ', ' > =' );
497 - $value = str_replace( $allow, $temp, $value );
498 - $value = strip_tags( $value );
499 - return str_replace( $temp, $allow, $value );
500 - }
501 -
502 - /**
503 - * Format a comparison like 5<10 to 5 < 10. Also works on 5< 10, 5 <10, 5<=10 variations.
504 - * This is to avoid an issue with unspaced calculations being recognized as HTML that gets removed when strip_tags is called.
505 - *
506 - * @param string $calc
507 - *
508 - * @return string
509 - */
510 - private static function normalize_calc_spaces( $calc ) {
511 - // Check for a pattern with 5 parts
512 - // $1 \d|\] the first comparison digit or the end of a comparison shortcode.
513 - // $2 a space (optional).
514 - // $3 an equals sign (optional) that follows the < operator for <= comparisons.
515 - // $4 another space (optional).
516 - // $5 \d|\[ the second comparison digit or the start of a comparison shortcode.
517 - return preg_replace( '/(\d|\])( ){0,1}<(=){0,1}( ){0,1}(\d|\[)/', '$1 <$3 $5', $calc );
518 - }
519 -
520 - /**
521 277 * Updating the settings page
522 - *
523 - * @param array $values Form values array.
524 - * @param object $field Field object, passed by reference.
525 - *
526 - * @return void
527 278 */
528 279 private static function get_settings_page_html( $values, &$field ) {
529 280 if ( isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ) {
530 281 $prev_opts = array();
531 - $fallback_html = $field->field_options['custom_html'] ?? FrmFieldsHelper::get_default_html( $field->type );
282 + $fallback_html = isset( $field->field_options['custom_html'] ) ? $field->field_options['custom_html'] : FrmFieldsHelper::get_default_html( $field->type );
532 283
533 - $field->field_options['custom_html'] = $values['field_options'][ 'custom_html_' . $field->id ] ?? $fallback_html;
534 - } elseif ( $field->type === 'hidden' || $field->type === 'user_id' ) {
284 + $field->field_options['custom_html'] = isset( $values['field_options'][ 'custom_html_' . $field->id ] ) ? $values['field_options'][ 'custom_html_' . $field->id ] : $fallback_html;
285 + } elseif ( $field->type == 'hidden' || $field->type == 'user_id' ) {
535 286 $prev_opts = $field->field_options;
536 287 }
537 288
538 - if ( ! isset( $prev_opts ) ) {
539 - return;
289 + if ( isset( $prev_opts ) ) {
290 + $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
291 + if ( $prev_opts != $field->field_options ) {
292 + FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
293 + }
540 294 }
541 -
542 - $field->field_options = apply_filters( 'frm_update_form_field_options', $field->field_options, $field, $values );
543 -
544 - // phpcs:ignore Universal.Operators.StrictComparisons
545 - if ( $prev_opts != $field->field_options ) {
546 - FrmField::update( $field->id, array( 'field_options' => $field->field_options ) );
547 - }
548 295 }
549 296
550 - /**
551 - * @param object $field
552 - * @param array $values
553 - * @param array $new_field
554 - *
555 - * @return void
556 - */
557 297 private static function prepare_field_update_values( $field, $values, &$new_field ) {
558 298 $field_cols = array(
559 299 'field_order' => 0,
560 300 'field_key' => '',
@@ -563,16 +303,12 @@
563 303 'description' => '',
564 304 'options' => '',
565 305 'name' => '',
566 306 );
567 -
568 307 foreach ( $field_cols as $col => $default ) {
569 - $default = $default === '' ? $field->{$col} : $default;
570 - $new_field[ $col ] = $values['field_options'][ $col . '_' . $field->id ] ?? $default;
571 - }
308 + $default = ( $default === '' ) ? $field->{$col} : $default;
572 309
573 - if ( $field->type === 'submit' && isset( $new_field['field_order'] ) && (int) $new_field['field_order'] === FrmSubmitHelper::DEFAULT_ORDER ) {
574 - $new_field['field_order'] = $field->field_order;
310 + $new_field[ $col ] = isset( $values['field_options'][ $col . '_' . $field->id ] ) ? $values['field_options'][ $col . '_' . $field->id ] : $default;
575 311 }
576 312
577 313 // Don't save the template option.
578 314 if ( is_array( $new_field['options'] ) && isset( $new_field['options']['000'] ) ) {
@@ -584,12 +320,9 @@
584 320 * Get a list of all form settings that should be translated
585 321 * on a multilingual site.
586 322 *
587 323 * @since 3.06.01
588 - *
589 - * @param object $form The form object.
590 - *
591 - * @return array
324 + * @param object $form - The form object
592 325 */
593 326 public static function translatable_strings( $form ) {
594 327 $strings = array(
595 328 'name',
@@ -596,12 +329,8 @@
596 329 'description',
597 330 'submit_value',
598 331 'submit_msg',
599 332 'success_msg',
600 - 'invalid_msg',
601 - 'failed_msg',
602 - 'login_msg',
603 - 'admin_permission',
604 333 );
605 334
606 335 return apply_filters( 'frm_form_strings', $strings, $form );
607 336 }
@@ -606,21 +335,19 @@
606 335 return apply_filters( 'frm_form_strings', $strings, $form );
607 336 }
608 337
609 338 /**
610 - * @param array|int $id
611 - * @param string $status
339 + * @param string $status
612 340 *
613 - * @return bool|int
341 + * @return int|boolean
614 342 */
615 343 public static function set_status( $id, $status ) {
616 - if ( 'trash' === $status ) {
344 + if ( 'trash' == $status ) {
617 345 return self::trash( $id );
618 346 }
619 347
620 348 $statuses = array( 'published', 'draft', 'trash' );
621 -
622 - if ( ! in_array( $status, $statuses, true ) ) {
349 + if ( ! in_array( $status, $statuses ) ) {
623 350 return false;
624 351 }
625 352
626 353 global $wpdb;
@@ -633,9 +360,9 @@
633 360 );
634 361 FrmDb::get_where_clause_and_values( $where );
635 362 array_unshift( $where['values'], $status );
636 363
637 - $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong
364 + $query_results = $wpdb->query( $wpdb->prepare( 'UPDATE ' . $wpdb->prefix . 'frm_forms SET status = %s ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok.
638 365 } else {
639 366 $query_results = $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'id' => $id ) );
640 367 $wpdb->update( $wpdb->prefix . 'frm_forms', array( 'status' => $status ), array( 'parent_form_id' => $id ) );
641 368 }
@@ -647,11 +374,9 @@
647 374 return $query_results;
648 375 }
649 376
650 377 /**
651 - * @param int|string $id Form ID.
652 - *
653 - * @return bool|int
378 + * @return int|boolean
654 379 */
655 380 public static function trash( $id ) {
656 381 if ( ! EMPTY_TRASH_DAYS ) {
657 382 return self::destroy( $id );
@@ -657,17 +382,12 @@
657 382 return self::destroy( $id );
658 383 }
659 384
660 385 $form = self::getOne( $id );
661 -
662 386 if ( ! $form ) {
663 387 return false;
664 388 }
665 389
666 - if ( $form->status === 'trash' ) {
667 - return false;
668 - }
669 -
670 390 $options = $form->options;
671 391 $options['trash_time'] = time();
672 392
673 393 global $wpdb;
@@ -700,26 +420,21 @@
700 420 return $query_results;
701 421 }
702 422
703 423 /**
704 - * @param int|string $id Form ID.
705 - *
706 - * @return bool|int
424 + * @return int|boolean
707 425 */
708 426 public static function destroy( $id ) {
709 427 global $wpdb;
710 428
711 429 $form = self::getOne( $id );
712 -
713 430 if ( ! $form ) {
714 431 return false;
715 432 }
716 -
717 433 $id = $form->id;
718 434
719 435 // Disconnect the entries from this form
720 - $entries = FrmDb::get_col( 'frm_items', array( 'form_id' => $id ) );
721 -
436 + $entries = FrmDb::get_col( $wpdb->prefix . 'frm_items', array( 'form_id' => $id ) );
722 437 foreach ( $entries as $entry_id ) {
723 438 FrmEntry::destroy( $entry_id );
724 439 unset( $entry_id );
725 440 }
@@ -724,29 +439,23 @@
724 439 unset( $entry_id );
725 440 }
726 441
727 442 // Disconnect the fields from this form
728 - $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
443 + $wpdb->query( $wpdb->prepare( 'DELETE fi FROM ' . $wpdb->prefix . 'frm_fields AS fi LEFT JOIN ' . $wpdb->prefix . 'frm_forms fr ON (fi.form_id = fr.id) WHERE fi.form_id=%d OR parent_form_id=%d', $id, $id ) );
729 444
730 445 $query_results = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_forms WHERE id=%d OR parent_form_id=%d', $id, $id ) );
446 + if ( $query_results ) {
447 + // Delete all form actions linked to this form
448 + $action_control = FrmFormActionsController::get_form_actions( 'email' );
449 + $action_control->destroy( $id, 'all' );
731 450
732 - if ( ! $query_results ) {
733 - return $query_results;
451 + // Clear form caching
452 + self::clear_form_cache();
453 +
454 + do_action( 'frm_destroy_form', $id );
455 + do_action( 'frm_destroy_form_' . $id );
734 456 }
735 457
736 - // Delete all form actions linked to this form
737 - /**
738 - * @var FrmFormAction
739 - */
740 - $action_control = FrmFormActionsController::get_form_actions( 'email' );
741 - $action_control->destroy( $id, 'all' );
742 -
743 - // Clear form caching
744 - self::clear_form_cache();
745 -
746 - do_action( 'frm_destroy_form', $id );
747 - do_action( 'frm_destroy_form_' . $id );
748 -
749 458 return $query_results;
750 459 }
751 460
752 461 /**
@@ -751,34 +460,29 @@
751 460
752 461 /**
753 462 * Delete trashed forms based on how long they have been trashed
754 463 *
755 - * @param int|string $delete_timestamp Timestamp cutoff for deletion.
756 - *
757 464 * @return int The number of forms deleted
758 465 */
759 466 public static function scheduled_delete( $delete_timestamp = '' ) {
760 - $trash_forms = FrmDb::get_results( 'frm_forms', array( 'status' => 'trash' ), 'id, parent_form_id, options' );
467 + global $wpdb;
761 468
469 + $trash_forms = FrmDb::get_results( $wpdb->prefix . 'frm_forms', array( 'status' => 'trash' ), 'id, options' );
470 +
762 471 if ( ! $trash_forms ) {
763 - return 0;
472 + return;
764 473 }
765 474
766 - if ( ! $delete_timestamp ) {
475 + if ( empty( $delete_timestamp ) ) {
767 476 $delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS );
768 477 }
769 478
770 479 $count = 0;
771 -
772 480 foreach ( $trash_forms as $form ) {
773 - FrmAppHelper::unserialize_or_decode( $form->options );
774 -
481 + $form->options = maybe_unserialize( $form->options );
775 482 if ( ! isset( $form->options['trash_time'] ) || $form->options['trash_time'] < $delete_timestamp ) {
776 483 self::destroy( $form->id );
777 -
778 - if ( empty( $form->parent_form_id ) ) {
779 - ++$count;
780 - }
484 + $count ++;
781 485 }
782 486
783 487 unset( $form );
784 488 }
@@ -786,24 +490,23 @@
786 490 return $count;
787 491 }
788 492
789 493 /**
790 - * @param int|string $id Form ID or key.
791 - *
792 494 * @return string form name
793 495 */
794 496 public static function getName( $id ) {
795 497 $form = FrmDb::check_cache( $id, 'frm_form' );
498 + if ( $form ) {
499 + $r = stripslashes( $form->name );
796 500
797 - if ( $form ) {
798 - return stripslashes( $form->name );
501 + return $r;
799 502 }
800 503
801 504 $query_key = is_numeric( $id ) ? 'id' : 'form_key';
802 505 $r = FrmDb::get_var( 'frm_forms', array( $query_key => $id ), 'name' );
506 + $r = stripslashes( $r );
803 507
804 - // An empty form name can result in a null value.
805 - return is_null( $r ) ? '' : stripslashes( $r );
508 + return $r;
806 509 }
807 510
808 511 /**
809 512 * @since 3.0
@@ -818,9 +521,9 @@
818 521
819 522 /**
820 523 * @since 3.0
821 524 *
822 - * @param int|string $id
525 + * @param int $id
823 526 *
824 527 * @return string form key
825 528 */
826 529 public static function get_key_by_id( $id ) {
@@ -825,106 +528,76 @@
825 528 */
826 529 public static function get_key_by_id( $id ) {
827 530 $id = (int) $id;
828 531 $cache = FrmDb::check_cache( $id, 'frm_form' );
829 -
830 532 if ( $cache ) {
831 533 return $cache->form_key;
832 534 }
833 535
834 - return FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
536 + $key = FrmDb::get_var( 'frm_forms', array( 'id' => $id ), 'form_key' );
537 +
538 + return $key;
835 539 }
836 540
837 541 /**
838 542 * If $form is numeric, get the form object
839 543 *
544 + * @param object|int $form
545 + *
840 546 * @since 2.0.9
841 - *
842 - * @param array|int|object $form
843 - *
844 - * @return void
845 547 */
846 548 public static function maybe_get_form( &$form ) {
847 - if ( ! is_object( $form ) && ! is_array( $form ) && $form ) {
549 + if ( ! is_object( $form ) && ! is_array( $form ) && ! empty( $form ) ) {
848 550 $form = self::getOne( $form );
849 551 }
850 552 }
851 553
852 554 /**
853 - * @param int|string $id
854 - * @param false|int $blog_id
855 - *
856 - * @return stdClass|null
555 + * @return object form
857 556 */
858 557 public static function getOne( $id, $blog_id = false ) {
859 558 global $wpdb;
860 559
861 560 if ( $blog_id && is_multisite() ) {
862 - $prefix = $wpdb->get_blog_prefix( $blog_id );
561 + global $wpmuBaseTablePrefix;
562 + $prefix = $wpmuBaseTablePrefix ? $wpmuBaseTablePrefix . $blog_id . '_' : $wpdb->get_blog_prefix( $blog_id );
563 +
863 564 $table_name = $prefix . 'frm_forms';
864 565 } else {
865 566 $table_name = $wpdb->prefix . 'frm_forms';
866 567 $cache = wp_cache_get( $id, 'frm_form' );
867 -
868 568 if ( $cache ) {
869 569 if ( isset( $cache->options ) ) {
870 - FrmAppHelper::unserialize_or_decode( $cache->options );
570 + $cache->options = maybe_unserialize( $cache->options );
871 571 }
872 572
873 - return self::prepare_form_row_data( $cache );
573 + return wp_unslash( $cache );
874 574 }
875 575 }
876 576
877 - $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'form_key' => $id );
577 + if ( is_numeric( $id ) ) {
578 + $where = array( 'id' => $id );
579 + } else {
580 + $where = array( 'form_key' => $id );
581 + }
582 +
878 583 $results = FrmDb::get_row( $table_name, $where );
879 584
880 585 if ( isset( $results->options ) ) {
881 586 FrmDb::set_cache( $results->id, $results, 'frm_form' );
882 - FrmAppHelper::unserialize_or_decode( $results->options );
587 + $results->options = maybe_unserialize( $results->options );
883 588 }
884 589
885 - return self::prepare_form_row_data( $results );
590 + return wp_unslash( $results );
886 591 }
887 592
888 593 /**
889 - * Make sure that if $row is an object, that $row->options is an array and not a string.
890 - *
891 - * @since 6.8.3
892 - *
893 - * @param stdClass|null $row The database row for a target form.
894 - *
895 - * @return stdClass|null
594 + * @return object|array of objects
896 595 */
897 - private static function prepare_form_row_data( $row ) {
898 - $row = wp_unslash( $row );
899 -
900 - if ( ! is_object( $row ) ) {
901 - return $row;
902 - }
903 -
904 - if ( ! is_array( $row->options ) ) {
905 - $row->options = FrmFormsHelper::get_default_opts();
906 - }
907 -
908 - /**
909 - * @since 4.03.02
910 - *
911 - * @param stdClass $row
912 - */
913 - return apply_filters( 'frm_form_object', $row );
914 - }
915 -
916 - /**
917 - * @param array|string $where Where conditions array or raw WHERE string.
918 - * @param string $order_by Order by clause.
919 - * @param int|string $limit Limit clause or number.
920 - *
921 - * @return array|object Array of objects. If $limit is 1, a single object is returned.
922 - */
923 596 public static function getAll( $where = array(), $order_by = '', $limit = '' ) {
924 - if ( is_array( $where ) && $where ) {
925 - if ( ! empty( $where['is_template'] ) && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
926 - // Don't get trashed templates
597 + if ( is_array( $where ) && ! empty( $where ) ) {
598 + if ( isset( $where['is_template'] ) && $where['is_template'] && ! isset( $where['status'] ) && ! isset( $where['status !'] ) ) {
599 + // don't get trashed templates
927 600 $where['status'] = array( null, '', 'published' );
928 601 }
929 602
930 603 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
@@ -930,22 +603,22 @@
930 603 $results = FrmDb::get_results( 'frm_forms', $where, '*', compact( 'order_by', 'limit' ) );
931 604 } else {
932 605 global $wpdb;
933 606
934 - // The query has already been prepared if this is not an array.
935 - $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
936 - $results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
607 + // the query has already been prepared if this is not an array
608 + $query = 'SELECT * FROM ' . $wpdb->prefix . 'frm_forms' . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . FrmDb::esc_order( $order_by ) . FrmDb::esc_limit( $limit );
609 + $results = $wpdb->get_results( $query ); // WPCS: unprepared SQL ok.
937 610 }
938 611
939 612 if ( $results ) {
940 613 foreach ( $results as $result ) {
941 614 FrmDb::set_cache( $result->id, $result, 'frm_form' );
942 - FrmAppHelper::unserialize_or_decode( $result->options );
615 + $result->options = maybe_unserialize( $result->options );
943 616 }
944 617 }
945 618
946 - if ( $limit === ' LIMIT 1' || (int) $limit === 1 ) {
947 - // Return the first form object if we are only getting one form
619 + if ( $limit == ' LIMIT 1' || $limit == 1 ) {
620 + // return the first form object if we are only getting one form
948 621 $results = reset( $results );
949 622 }
950 623
951 624 return wp_unslash( $results );
@@ -954,24 +627,20 @@
954 627 /**
955 628 * Get all published forms
956 629 *
957 630 * @since 2.0
958 - *
959 - * @param array $query
960 - * @param int $limit
961 - * @param string $inc_children
962 - *
963 - * @return array|object Array of forms. A single form object is returned if $limit is set to 1.
631 + * @return array of forms
964 632 */
965 633 public static function get_published_forms( $query = array(), $limit = 999, $inc_children = 'exclude' ) {
966 634 $query['is_template'] = 0;
967 635 $query['status'] = array( null, '', 'published' );
968 -
969 - if ( $inc_children === 'exclude' ) {
636 + if ( $inc_children == 'exclude' ) {
970 637 $query['parent_form_id'] = array( null, 0 );
971 638 }
972 639
973 - return self::getAll( $query, 'name', $limit );
640 + $forms = self::getAll( $query, 'name', $limit );
641 +
642 + return $forms;
974 643 }
975 644
976 645 /**
977 646 * @return object count of forms
@@ -976,16 +645,18 @@
976 645 /**
977 646 * @return object count of forms
978 647 */
979 648 public static function get_count() {
649 + global $wpdb;
650 +
980 651 $cache_key = 'frm_form_counts';
981 - $counts = wp_cache_get( $cache_key, 'frm_form' );
982 652
653 + $counts = wp_cache_get( $cache_key, 'frm_form' );
983 654 if ( false !== $counts ) {
984 655 return $counts;
985 656 }
986 657
987 - $results = FrmDb::get_results(
658 + $results = (array) FrmDb::get_results(
988 659 'frm_forms',
989 660 array(
990 661 'or' => 1,
991 662 'parent_form_id' => null,
@@ -997,20 +668,20 @@
997 668 $statuses = array( 'published', 'draft', 'template', 'trash' );
998 669 $counts = array_fill_keys( $statuses, 0 );
999 670
1000 671 foreach ( $results as $row ) {
1001 - if ( 'trash' !== $row->status ) {
672 + if ( 'trash' != $row->status ) {
1002 673 if ( $row->is_template ) {
1003 - ++$counts['template'];
674 + $counts['template'] ++;
1004 675 } else {
1005 - ++$counts['published'];
676 + $counts['published'] ++;
1006 677 }
1007 678 } else {
1008 - ++$counts['trash'];
679 + $counts['trash'] ++;
1009 680 }
1010 681
1011 - if ( 'draft' === $row->status ) {
1012 - ++$counts['draft'];
682 + if ( 'draft' == $row->status ) {
683 + $counts['draft'] ++;
1013 684 }
1014 685
1015 686 unset( $row );
1016 687 }
@@ -1026,10 +697,8 @@
1026 697 * Called when a form is created, updated, duplicated, or deleted
1027 698 * or when the form status is changed
1028 699 *
1029 700 * @since 2.0.4
1030 - *
1031 - * @return void
1032 701 */
1033 702 public static function clear_form_cache() {
1034 703 FrmDb::cache_delete_group( 'frm_form' );
1035 704 }
@@ -1034,22 +703,16 @@
1034 703 FrmDb::cache_delete_group( 'frm_form' );
1035 704 }
1036 705
1037 706 /**
1038 - * @param array $values Form values to validate.
1039 - *
1040 707 * @return array of errors
1041 708 */
1042 709 public static function validate( $values ) {
1043 710 $errors = array();
711 +
1044 712 return apply_filters( 'frm_validate_form', $errors, $values );
1045 713 }
1046 714
1047 - /**
1048 - * @param object|null $form
1049 - *
1050 - * @return array
1051 - */
1052 715 public static function get_params( $form = null ) {
1053 716 global $frm_vars;
1054 717
1055 718 if ( ! $form ) {
@@ -1061,9 +724,9 @@
1061 724 if ( isset( $frm_vars['form_params'] ) && is_array( $frm_vars['form_params'] ) && isset( $frm_vars['form_params'][ $form->id ] ) ) {
1062 725 return $frm_vars['form_params'][ $form->id ];
1063 726 }
1064 727
1065 - $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // phpcs:ignore WordPress.Security.NonceVerification.Missing
728 + $action_var = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; // WPCS: CSRF ok.
1066 729 $action = apply_filters( 'frm_show_new_entry_page', FrmAppHelper::get_param( $action_var, 'new', 'get', 'sanitize_title' ), $form );
1067 730
1068 731 $default_values = array(
1069 732 'id' => '',
@@ -1079,18 +742,16 @@
1079 742 );
1080 743
1081 744 $values = array();
1082 745 $values['posted_form_id'] = FrmAppHelper::get_param( 'form_id', '', 'get', 'absint' );
1083 -
1084 746 if ( ! $values['posted_form_id'] ) {
1085 747 $values['posted_form_id'] = FrmAppHelper::get_param( 'form', '', 'get', 'absint' );
1086 748 }
1087 749
1088 - // phpcs:ignore Universal.Operators.StrictComparisons
1089 750 if ( $form->id == $values['posted_form_id'] ) {
1090 - // If there are two forms on the same page, make sure not to submit both.
751 + //if there are two forms on the same page, make sure not to submit both
1091 752 foreach ( $default_values as $var => $default ) {
1092 - if ( $var === 'action' ) {
753 + if ( $var == 'action' ) {
1093 754 $values[ $var ] = FrmAppHelper::get_param( $action_var, $default, 'get', 'sanitize_title' );
1094 755 } else {
1095 756 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
1096 757 }
@@ -1102,10 +763,10 @@
1102 763 unset( $var, $default );
1103 764 }
1104 765 }
1105 766
1106 - if ( in_array( $values['action'], array( 'create', 'update' ), true ) &&
1107 - ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
767 + if ( in_array( $values['action'], array( 'create', 'update' ) ) &&
768 + ( ! $_POST || ( ! isset( $_POST['action'] ) && ! isset( $_POST['frm_action'] ) ) ) // WPCS: CSRF ok.
1108 769 ) {
1109 770 $values['action'] = 'new';
1110 771 }
1111 772
@@ -1111,11 +772,8 @@
1111 772
1112 773 return $values;
1113 774 }
1114 775
1115 - /**
1116 - * @return array
1117 - */
1118 776 public static function list_page_params() {
1119 777 $values = array();
1120 778 $defaults = array(
1121 779 'template' => 0,
@@ -1125,9 +783,8 @@
1125 783 'search' => '',
1126 784 'sort' => '',
1127 785 'sdir' => '',
1128 786 );
1129 -
1130 787 foreach ( $defaults as $var => $default ) {
1131 788 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
1132 789 }
1133 790
@@ -1133,16 +790,10 @@
1133 790
1134 791 return $values;
1135 792 }
1136 793
1137 - /**
1138 - * @param int|object|string|null $form
1139 - *
1140 - * @return array
1141 - */
1142 794 public static function get_admin_params( $form = null ) {
1143 795 $form_id = $form;
1144 -
1145 796 if ( $form === null ) {
1146 797 $form_id = self::get_current_form_id();
1147 798 } elseif ( $form && is_object( $form ) ) {
1148 799 $form_id = $form->id;
@@ -1160,9 +811,8 @@
1160 811 'sdir' => '',
1161 812 'fid' => '',
1162 813 'keep_post' => '',
1163 814 );
1164 -
1165 815 foreach ( $defaults as $var => $default ) {
1166 816 $values[ $var ] = FrmAppHelper::get_param( $var, $default, 'get', 'sanitize_text_field' );
1167 817 }
1168 818
@@ -1168,55 +818,47 @@
1168 818
1169 819 return $values;
1170 820 }
1171 821
1172 - /**
1173 - * @param string $default_form
1174 - *
1175 - * @return int|string
1176 - */
1177 822 public static function get_current_form_id( $default_form = 'none' ) {
1178 - $form = 'first' === $default_form ? self::get_current_form() : self::maybe_get_current_form();
1179 - return $form ? $form->id : 0;
823 + if ( 'first' == $default_form ) {
824 + $form = self::get_current_form();
825 + } else {
826 + $form = self::maybe_get_current_form();
827 + }
828 + $form_id = $form ? $form->id : 0;
829 +
830 + return $form_id;
1180 831 }
1181 832
1182 - /**
1183 - * @param int|string $form_id
1184 - *
1185 - * @return false|int|object|string
1186 - */
1187 833 public static function maybe_get_current_form( $form_id = 0 ) {
1188 834 global $frm_vars;
1189 835
1190 - if ( ! empty( $frm_vars['current_form'] ) && ( ! $form_id || (int) $form_id === (int) $frm_vars['current_form']->id ) ) {
836 + if ( isset( $frm_vars['current_form'] ) && $frm_vars['current_form'] && ( ! $form_id || $form_id == $frm_vars['current_form']->id ) ) {
1191 837 return $frm_vars['current_form'];
1192 838 }
1193 839
1194 840 $form_id = FrmAppHelper::get_param( 'form', $form_id, 'get', 'absint' );
841 + if ( $form_id ) {
842 + $form_id = self::set_current_form( $form_id );
843 + }
1195 844
1196 - return $form_id ? self::set_current_form( $form_id ) : $form_id;
845 + return $form_id;
1197 846 }
1198 847
1199 - /**
1200 - * @param int $form_id
1201 - *
1202 - * @return false|object
1203 - */
1204 848 public static function get_current_form( $form_id = 0 ) {
1205 849 $form = self::maybe_get_current_form( $form_id );
1206 - return is_numeric( $form ) ? self::set_current_form( $form ) : $form;
850 + if ( is_numeric( $form ) ) {
851 + $form = self::set_current_form( $form );
852 + }
853 +
854 + return $form;
1207 855 }
1208 856
1209 - /**
1210 - * @param int $form_id
1211 - *
1212 - * @return false|object
1213 - */
1214 857 public static function set_current_form( $form_id ) {
1215 858 global $frm_vars;
1216 859
1217 860 $query = array();
1218 -
1219 861 if ( $form_id ) {
1220 862 $query['id'] = $form_id;
1221 863 }
1222 864
@@ -1224,19 +866,11 @@
1224 866
1225 867 return $frm_vars['current_form'];
1226 868 }
1227 869
1228 - /**
1229 - * @param object $form
1230 - * @param int|string $this_load
1231 - * @param bool $global_load
1232 - *
1233 - * @return bool
1234 - */
1235 870 public static function is_form_loaded( $form, $this_load, $global_load ) {
1236 871 global $frm_vars;
1237 872 $small_form = new stdClass();
1238 -
1239 873 foreach ( array( 'id', 'form_key', 'name' ) as $var ) {
1240 874 $small_form->{$var} = $form->{$var};
1241 875 unset( $var );
1242 876 }
@@ -1242,61 +876,30 @@
1242 876 }
1243 877
1244 878 $frm_vars['forms_loaded'][] = $small_form;
1245 879
1246 - if ( $this_load && ! $global_load ) {
880 + if ( $this_load && empty( $global_load ) ) {
1247 881 $global_load = true;
1248 882 $frm_vars['load_css'] = true;
1249 883 }
1250 884
1251 - return empty( $frm_vars['css_loaded'] ) && $global_load;
885 + return ( ( ! isset( $frm_vars['css_loaded'] ) || ! $frm_vars['css_loaded'] ) && $global_load );
1252 886 }
1253 887
1254 - /**
1255 - * @since 4.06.03
1256 - *
1257 - * @param object $form
1258 - *
1259 - * @return bool
1260 - */
1261 - public static function is_visible_to_user( $form ) {
1262 - if ( $form->logged_in && isset( $form->options['logged_in_role'] ) ) {
1263 - $visible = FrmAppHelper::user_has_permission( $form->options['logged_in_role'] );
1264 - } else {
1265 - $visible = true;
1266 - }
888 + public static function show_submit( $form ) {
889 + $show = ( ! $form->is_template && $form->status == 'published' && ! FrmAppHelper::is_admin() );
890 + $show = apply_filters( 'frm_show_submit_button', $show, $form );
1267 891
1268 - /**
1269 - * @since 6.25
1270 - *
1271 - * @param bool $visible
1272 - * @param object $form
1273 - */
1274 - return (bool) apply_filters( 'frm_form_is_visible', $visible, $form );
892 + return $show;
1275 893 }
1276 894
1277 895 /**
1278 - * @param object $form
1279 - *
1280 - * @return bool
1281 - */
1282 - public static function show_submit( $form ) {
1283 - $show = ! $form->is_template && $form->status === 'published' && ! FrmAppHelper::is_admin();
1284 - return apply_filters( 'frm_show_submit_button', $show, $form );
1285 - }
1286 -
1287 - /**
1288 896 * @since 2.3
1289 - *
1290 - * @param array $atts Attributes including form, option, and default.
1291 - *
1292 - * @return mixed
1293 897 */
1294 898 public static function get_option( $atts ) {
1295 - $form = $atts['form'];
1296 - $default = $atts['default'] ?? '';
899 + $form = $atts['form'];
1297 900
1298 - return $form->options[ $atts['option'] ] ?? $default;
901 + return isset( $form->options[ $atts['option'] ] ) ? $form->options[ $atts['option'] ] : $atts['default'];
1299 902 }
1300 903
1301 904 /**
1302 905 * Get the link to edit this form.
@@ -1301,12 +904,9 @@
1301 904 /**
1302 905 * Get the link to edit this form.
1303 906 *
1304 907 * @since 4.0
1305 - *
1306 908 * @param int $form_id The id of the form.
1307 - *
1308 - * @return string
1309 909 */
1310 910 public static function get_edit_link( $form_id ) {
1311 911 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
1312 912 }
@@ -1311,65 +911,9 @@
1311 911 return admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id );
1312 912 }
1313 913
1314 914 /**
1315 - * Check if the "Submit this form with AJAX" setting is toggled on.
1316 - *
1317 - * @since 6.2
1318 - *
1319 - * @param stdClass $form
1320 - *
1321 - * @return bool
1322 - */
1323 - public static function is_ajax_on( $form ) {
1324 - return ! empty( $form->options['ajax_submit'] );
1325 - }
1326 -
1327 - /**
1328 - * Get the latest form available.
1329 - *
1330 - * @since 6.8
1331 - *
1332 - * @return object
1333 - */
1334 - public static function get_latest_form() {
1335 - $args = array(
1336 - array(
1337 - 'or' => 1,
1338 - 'parent_form_id' => null,
1339 - 'parent_form_id <' => 1,
1340 - ),
1341 - 'is_template' => 0,
1342 - 'status !' => 'trash',
1343 - );
1344 -
1345 - return self::getAll( $args, 'created_at desc', 1 );
1346 - }
1347 -
1348 - /**
1349 - * Count and return total forms.
1350 - *
1351 - * @since 6.8
1352 - *
1353 - * @return int
1354 - */
1355 - public static function get_forms_count() {
1356 - $args = array(
1357 - array(
1358 - 'or' => 1,
1359 - 'parent_form_id' => null,
1360 - 'parent_form_id <' => 1,
1361 - ),
1362 - 'is_template' => 0,
1363 - 'status !' => 'trash',
1364 - );
1365 -
1366 - return FrmDb::get_count( 'frm_forms', $args );
1367 - }
1368 -
1369 - /**
1370 - * @deprecated 2.03.05 This is still referenced in a few add ons (API, locations).
1371 - *
915 + * @deprecated 3.0
1372 916 * @codeCoverageIgnore
1373 917 *
1374 918 * @param string $key
1375 919 *
@@ -1375,22 +919,15 @@
1375 919 *
1376 920 * @return int form id
1377 921 */
1378 922 public static function getIdByKey( $key ) {
1379 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_id_by_key' );
1380 - return self::get_id_by_key( $key );
923 + return FrmFormDeprecated::getIdByKey( $key );
1381 924 }
1382 925
1383 926 /**
1384 - * @deprecated 2.03.05 This is still referenced in the API add on as of v1.13.
1385 - *
927 + * @deprecated 3.0
1386 928 * @codeCoverageIgnore
1387 - *
1388 - * @param int|string $id
1389 - *
1390 - * @return string
1391 929 */
1392 930 public static function getKeyById( $id ) {
1393 - _deprecated_function( __FUNCTION__, '2.03.05', 'FrmForm::get_key_by_id' );
1394 - return self::get_key_by_id( $id );
931 + return FrmFormDeprecated::getKeyById( $id );
1395 932 }
1396 933 }