PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.19
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.19
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/FrmEntryMeta.php +43 -115 6.276.19 View file →
@@ -9,9 +9,8 @@
9 9 * @param int $entry_id
10 10 * @param int $field_id
11 11 * @param string $meta_key usually set to '' as this parameter is no longer used.
12 12 * @param mixed $meta_value
13 - *
14 13 * @return int
15 14 */
16 15 public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) {
17 16 global $wpdb;
@@ -28,18 +27,21 @@
28 27 'created_at' => current_time( 'mysql', 1 ),
29 28 );
30 29
31 30 self::set_value_before_save( $new_values );
32 - $new_values = apply_filters( 'frm_add_entry_meta', $new_values );
31 + $new_values = apply_filters( 'frm_add_entry_meta', $new_values );
32 +
33 33 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_item_metas', $new_values );
34 34
35 35 if ( $query_results ) {
36 36 self::clear_cache();
37 37 wp_cache_delete( $entry_id, 'frm_entry' );
38 - return $wpdb->insert_id;
38 + $id = $wpdb->insert_id;
39 + } else {
40 + $id = 0;
39 41 }
40 42
41 - return 0;
43 + return $id;
42 44 }
43 45
44 46 /**
45 47 * @param int $entry_id
@@ -67,9 +69,8 @@
67 69
68 70 if ( is_array( $values['meta_value'] ) ) {
69 71 $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' );
70 72 }
71 -
72 73 $meta_value = maybe_serialize( $values['meta_value'] );
73 74
74 75 wp_cache_delete( $entry_id, 'frm_entry' );
75 76 self::clear_cache();
@@ -78,18 +79,14 @@
78 79 }
79 80
80 81 /**
81 82 * @since 3.0
82 - *
83 - * @param array $values
84 - *
85 - * @return void
86 83 */
87 84 private static function set_value_before_save( &$values ) {
88 85 $field = FrmField::getOne( $values['field_id'] );
86 + if ( $field ) {
87 + $field_obj = FrmFieldFactory::get_field_object( $field );
89 88
90 - if ( $field ) {
91 - $field_obj = FrmFieldFactory::get_field_object( $field );
92 89 $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] );
93 90 }
94 91 }
95 92
@@ -94,13 +91,8 @@
94 91 }
95 92
96 93 /**
97 94 * @since 3.0
98 - *
99 - * @param array $atts
100 - * @param mixed $value
101 - *
102 - * @return void
103 95 */
104 96 private static function get_value_to_save( $atts, &$value ) {
105 97 if ( is_object( $atts['field'] ) ) {
106 98 $field_obj = FrmFieldFactory::get_field_object( $atts['field'] );
@@ -118,9 +110,8 @@
118 110
119 111 /**
120 112 * @param int|string $entry_id
121 113 * @param array $values Either indexed by field ID or field key.
122 - *
123 114 * @return void
124 115 */
125 116 public static function update_entry_metas( $entry_id, $values ) {
126 117 global $wpdb;
@@ -134,9 +125,8 @@
134 125 'field_id'
135 126 );
136 127
137 128 $values_indexed_by_field_id = array();
138 -
139 129 foreach ( $values as $field_id_or_key => $meta_value ) {
140 130 $field_id = $field_id_or_key;
141 131 $field = null;
142 132
@@ -151,11 +141,11 @@
151 141 $values_indexed_by_field_id[ $field_id ] = $meta_value;
152 142
153 143 self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value );
154 144
155 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
156 145 if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) {
157 - if ( $meta_value === array() || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) {
146 +
147 + if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) {
158 148 // Remove blank fields.
159 149 unset( $values_indexed_by_field_id[ $field_id ] );
160 150 } else {
161 151 // if value exists, then update it
@@ -166,9 +156,9 @@
166 156 self::add_entry_meta( $entry_id, $field_id, '', $meta_value );
167 157 }
168 158 }//end foreach
169 159
170 - if ( ! $previous_field_ids ) {
160 + if ( empty( $previous_field_ids ) ) {
171 161 return;
172 162 }
173 163
174 164 $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) );
@@ -184,18 +174,12 @@
184 174 );
185 175 FrmDb::get_where_clause_and_values( $where );
186 176
187 177 // Delete any leftovers
188 - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong
178 + $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
189 179 self::clear_cache();
190 180 }
191 181
192 - /**
193 - * @param int $old_id
194 - * @param int $new_id
195 - *
196 - * @return void
197 - */
198 182 public static function duplicate_entry_metas( $old_id, $new_id ) {
199 183 $metas = self::get_entry_meta_info( $old_id );
200 184
201 185 /**
@@ -205,9 +189,8 @@
205 189 *
206 190 * @param array $metas The list of entry meta values.
207 191 */
208 192 $metas = apply_filters( 'frm_before_duplicate_entry_values', $metas );
209 -
210 193 foreach ( $metas as $meta ) {
211 194 self::add_entry_meta( $new_id, $meta->field_id, '', $meta->meta_value );
212 195 unset( $meta );
213 196 }
@@ -213,14 +196,8 @@
213 196 }
214 197 self::clear_cache();
215 198 }
216 199
217 - /**
218 - * @param int $entry_id
219 - * @param int $field_id
220 - *
221 - * @return false|int
222 - */
223 200 public static function delete_entry_meta( $entry_id, $field_id ) {
224 201 global $wpdb;
225 202 self::clear_cache();
226 203
@@ -231,10 +208,8 @@
231 208 * Clear entry meta caching
232 209 * Called when a meta is added or changed
233 210 *
234 211 * @since 2.0.5
235 - *
236 - * @return void
237 212 */
238 213 public static function clear_cache() {
239 214 FrmDb::cache_delete_group( 'frm_entry_meta' );
240 215 FrmDb::cache_delete_group( 'frm_item_meta' );
@@ -244,14 +219,13 @@
244 219 * @since 2.0.9
245 220 *
246 221 * @param stdClass $entry
247 222 * @param int|string $field_id
248 - *
249 223 * @return mixed
250 224 */
251 225 public static function get_meta_value( $entry, $field_id ) {
252 226 if ( isset( $entry->metas ) ) {
253 - return $entry->metas[ $field_id ] ?? false;
227 + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false;
254 228 }
255 229 return self::get_entry_meta_by_field( $entry->id, $field_id );
256 230 }
257 231
@@ -257,9 +231,8 @@
257 231
258 232 /**
259 233 * @param int|object|string $entry_id
260 234 * @param int|string $field_id This function supports field keys as field id.
261 - *
262 235 * @return mixed
263 236 */
264 237 public static function get_entry_meta_by_field( $entry_id, $field_id ) {
265 238 global $wpdb;
@@ -274,14 +247,14 @@
274 247 }
275 248
276 249 if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) {
277 250 $result = $cached->metas[ $field_id ];
251 +
278 252 return wp_unslash( $result );
279 253 }
280 254
281 255 $get_table = $wpdb->prefix . 'frm_item_metas';
282 256 $query = array( 'item_id' => $entry_id );
283 -
284 257 if ( is_numeric( $field_id ) ) {
285 258 // Query by field ID.
286 259 $query['field_id'] = $field_id;
287 260 } else {
@@ -289,23 +262,18 @@
289 262 $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
290 263 $query['fi.field_key'] = $field_id;
291 264 }
292 265
293 - $result = FrmDb::get_var( $get_table, $query, 'meta_value' );
266 + $result = FrmDb::get_var( $get_table, $query, 'meta_value' );
267 +
294 268 $field_type = FrmField::get_type( $field_id );
295 269 FrmFieldsHelper::prepare_field_value( $result, $field_type );
296 270
297 - return wp_unslash( $result );
271 + $result = wp_unslash( $result );
272 +
273 + return $result;
298 274 }
299 275
300 - /**
301 - * @param int|string $field_id
302 - * @param string $order
303 - * @param string $limit
304 - * @param array $args
305 - *
306 - * @return array
307 - */
308 276 public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) {
309 277 $defaults = array(
310 278 'value' => false,
311 279 'unique' => false,
@@ -312,11 +280,13 @@
312 280 'stripslashes' => true,
313 281 'is_draft' => false,
314 282 );
315 283 $args = wp_parse_args( $args, $defaults );
316 - $query = array();
284 +
285 + $query = array();
317 286 self::meta_field_query( $field_id, $order, $limit, $args, $query );
318 - $query = implode( ' ', $query );
287 + $query = implode( ' ', $query );
288 +
319 289 $cache_key = 'entry_metas_for_field_' . $field_id . $order . $limit . FrmAppHelper::maybe_json_encode( $args );
320 290 $values = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_col' );
321 291
322 292 if ( ! $args['stripslashes'] ) {
@@ -337,10 +307,8 @@
337 307 * @param string $order
338 308 * @param string $limit
339 309 * @param array $args
340 310 * @param array $query
341 - *
342 - * @return void
343 311 */
344 312 private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) {
345 313 global $wpdb;
346 314 $query[] = 'SELECT';
@@ -363,17 +331,11 @@
363 331
364 332 if ( $args['value'] ) {
365 333 $query[] = $wpdb->prepare( ' AND meta_value=%s', $args['value'] );
366 334 }
367 -
368 335 $query[] = $order . $limit;
369 336 }
370 337
371 - /**
372 - * @param int|string $entry_id
373 - *
374 - * @return array
375 - */
376 338 public static function get_entry_meta_info( $entry_id ) {
377 339 return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) );
378 340 }
379 341
@@ -381,10 +343,9 @@
381 343 * @param array $where
382 344 * @param string $order_by
383 345 * @param string $limit
384 346 * @param bool $stripslashes
385 - *
386 - * @return mixed
347 + * @return array
387 348 */
388 349 public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) {
389 350 global $wpdb;
390 351 $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key,
@@ -392,9 +353,9 @@
392 353 FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' .
393 354 FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
394 355
395 356 $cache_key = 'all_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit;
396 - $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, ( $limit == ' LIMIT 1' ? 'get_row' : 'get_results' ) ); // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong
357 + $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, ( $limit == ' LIMIT 1' ? 'get_row' : 'get_results' ) );
397 358
398 359 if ( ! $results || ! $stripslashes ) {
399 360 return $results;
400 361 }
@@ -407,17 +368,8 @@
407 368
408 369 return $results;
409 370 }
410 371
411 - /**
412 - * @param array|string $where
413 - * @param string $order_by
414 - * @param string $limit
415 - * @param bool $unique
416 - * @param array $args
417 - *
418 - * @return array|string|null
419 - */
420 372 public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) {
421 373 $defaults = array(
422 374 'is_draft' => false,
423 375 'user_id' => '',
@@ -423,11 +375,13 @@
423 375 'user_id' => '',
424 376 'group_by' => '',
425 377 );
426 378 $args = wp_parse_args( $args, $defaults );
427 - $query = array();
379 +
380 + $query = array();
428 381 self::get_ids_query( $where, $order_by, $limit, $unique, $args, $query );
429 - $query = implode( ' ', $query );
382 + $query = implode( ' ', $query );
383 +
430 384 $cache_key = 'ids_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . 'l' . $limit . 'u' . $unique . FrmAppHelper::maybe_json_encode( $args );
431 385 $type = 'get_' . ( ' LIMIT 1' === $limit ? 'var' : 'col' );
432 386 return FrmDb::check_cache( $cache_key, 'frm_entry', $query, $type );
433 387 }
@@ -437,9 +391,8 @@
437 391 * If a child entry id is matched, its parent will be returned in its place
438 392 *
439 393 * @param array $query
440 394 * @param array $args
441 - *
442 395 * @return array
443 396 */
444 397 public static function get_top_level_entry_ids( $query, $args ) {
445 398 $args['return_parent_id_if_0_return_id'] = true;
@@ -450,11 +403,9 @@
450 403 * Returns true if the where clause refers to a field table column that is not form_id. It also updates
451 404 * the where clause to refer to the entry table for form_id if fields table should not be joined.
452 405 *
453 406 * @since 6.16.1
454 - *
455 407 * @param array|string $where
456 - *
457 408 * @return bool
458 409 */
459 410 private static function should_join_fields_table( &$where ) {
460 411 if ( is_string( $where ) ) {
@@ -460,26 +411,21 @@
460 411 if ( is_string( $where ) ) {
461 412 if ( preg_match( '/\bfi\.(?!form_id)\w+/i', $where ) ) {
462 413 return true;
463 414 }
464 -
465 415 $where = str_ireplace( 'fi.form_id', 'e.form_id', $where );
466 416 return false;
467 417 }
468 -
469 418 $where_fields = array_keys( $where );
470 -
471 419 foreach ( $where_fields as $where_field ) {
472 - if ( str_starts_with( $where_field, 'fi.' ) && 'fi.form_id' !== $where_field ) {
420 + if ( strpos( $where_field, 'fi.' ) === 0 && 'fi.form_id' !== $where_field ) {
473 421 return true;
474 422 }
475 423 }
476 -
477 424 if ( isset( $where['fi.form_id'] ) ) {
478 425 $where['e.form_id'] = $where['fi.form_id'];
479 426 unset( $where['fi.form_id'] );
480 427 }
481 -
482 428 return false;
483 429 }
484 430
485 431 /**
@@ -485,15 +431,10 @@
485 431 /**
486 432 * @param array|string $where
487 433 * @param string $order_by
488 434 * @param string $limit
489 - * @param bool $unique
490 - * @param array $args
491 - * @param array $query
492 - *
493 - * @return void
494 435 */
495 - private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, SlevomatCodingStandard.Files.LineLength.LineTooLong
436 + private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) {
496 437 global $wpdb;
497 438 $query[] = 'SELECT';
498 439 $defaults = array(
499 440 'return_parent_id' => false,
@@ -512,12 +453,12 @@
512 453 } else {
513 454 $query[] = 'it.item_id';
514 455 }
515 456
516 - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it';
457 + $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it';
458 +
517 459 $should_join_fields_table__where = self::should_join_fields_table( $where );
518 460 $should_join_fields_table__order_by = self::should_join_fields_table( $order_by );
519 -
520 461 if ( $should_join_fields_table__where || $should_join_fields_table__order_by ) {
521 462 $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
522 463 }
523 464
@@ -522,17 +463,20 @@
522 463 }
523 464
524 465 $query[] = $from;
525 466 $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)';
526 -
527 467 if ( is_array( $where ) ) {
528 468 if ( ! $args['is_draft'] ) {
529 469 $where['e.is_draft'] = 0;
530 470 } elseif ( is_numeric( $args['is_draft'] ) ) {
531 - $where['e.is_draft'] = class_exists( 'FrmAbandonmentHooksController', false ) ? absint( $args['is_draft'] ) : 1;
471 + if ( class_exists( 'FrmAbandonmentHooksController', false ) ) {
472 + $where['e.is_draft'] = absint( $args['is_draft'] );
473 + } else {
474 + $where['e.is_draft'] = 1;
475 + }
532 476 } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) {
533 477 $where['e.is_draft'] = array( 0, 1 );
534 - } elseif ( str_contains( $args['is_draft'], ',' ) ) {
478 + } elseif ( false !== strpos( $args['is_draft'], ',' ) ) {
535 479 $is_draft = array_reduce(
536 480 explode( ',', $args['is_draft'] ),
537 481 function ( $total, $current ) {
538 482 if ( is_numeric( $current ) ) {
@@ -541,9 +485,8 @@
541 485 return $total;
542 486 },
543 487 array()
544 488 );
545 -
546 489 if ( $is_draft ) {
547 490 $where['e.is_draft'] = $is_draft;
548 491 }
549 492 }//end if
@@ -550,9 +493,8 @@
550 493
551 494 if ( ! empty( $args['user_id'] ) ) {
552 495 $where['e.user_id'] = $args['user_id'];
553 496 }
554 -
555 497 $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
556 498
557 499 if ( $args['group_by'] ) {
558 500 $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] );
@@ -562,12 +504,11 @@
562 504 }//end if
563 505
564 506 $draft_where = '';
565 507 $user_where = '';
566 -
567 508 if ( ! $args['is_draft'] ) {
568 509 $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 );
569 - } elseif ( $args['is_draft'] == 1 ) { // phpcs:ignore Universal.Operators.StrictComparisons
510 + } elseif ( $args['is_draft'] == 1 ) {
570 511 $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 );
571 512 }
572 513
573 514 if ( ! empty( $args['user_id'] ) ) {
@@ -573,9 +514,9 @@
573 514 if ( ! empty( $args['user_id'] ) ) {
574 515 $user_where = $wpdb->prepare( ' AND e.user_id=%d', $args['user_id'] );
575 516 }
576 517
577 - if ( str_contains( $where, ' GROUP BY ' ) ) {
518 + if ( strpos( $where, ' GROUP BY ' ) ) {
578 519 // don't inject WHERE filtering after GROUP BY
579 520 $parts = explode( ' GROUP BY ', $where );
580 521 $where = $parts[0];
581 522 $where .= $draft_where . $user_where;
@@ -587,30 +528,20 @@
587 528 // The query has already been prepared
588 529 $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
589 530 }
590 531
591 - /**
592 - * @param array|string $search
593 - * @param int|string $field_id
594 - * @param string $operator
595 - *
596 - * @return array
597 - */
598 532 public static function search_entry_metas( $search, $field_id, $operator ) {
599 533 $cache_key = 'search_' . FrmAppHelper::maybe_json_encode( $search ) . $field_id . $operator;
600 534 $results = wp_cache_get( $cache_key, 'frm_entry' );
601 -
602 535 if ( false !== $results ) {
603 536 return $results;
604 537 }
605 538
606 539 global $wpdb;
607 -
608 540 if ( is_array( $search ) ) {
609 541 $where = '';
610 -
611 542 foreach ( $search as $field => $value ) {
612 - if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ), true ) ) {
543 + if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) {
613 544 continue;
614 545 }
615 546
616 547 switch ( $field ) {
@@ -622,12 +553,10 @@
622 553 break;
623 554 case 'day':
624 555 $value = '%' . $value . '%';
625 556 }
626 -
627 557 $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
628 558 }
629 -
630 559 $where .= $wpdb->prepare( ' field_id=%d', $field_id );
631 560 $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where );
632 561 } else {
633 562 if ( $operator === 'LIKE' ) {
@@ -632,10 +561,9 @@
632 561 } else {
633 562 if ( $operator === 'LIKE' ) {
634 563 $search = '%' . $search . '%';
635 564 }
636 -
637 - $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong
565 + $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
638 566 }//end if
639 567
640 568 $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
641 569 FrmDb::set_cache( $cache_key, $results, 'frm_entry' );