PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0
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 +42 -234 6.265.0 View file →
@@ -5,14 +5,9 @@
5 5
6 6 class FrmEntryMeta {
7 7
8 8 /**
9 - * @param int $entry_id
10 - * @param int $field_id
11 - * @param string $meta_key usually set to '' as this parameter is no longer used.
12 - * @param mixed $meta_value
13 - *
14 - * @return int
9 + * @param string $meta_key
15 10 */
16 11 public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) {
17 12 global $wpdb;
18 13
@@ -44,11 +39,11 @@
44 39 return $id;
45 40 }
46 41
47 42 /**
48 - * @param int $entry_id
49 - * @param int $field_id
50 - * @param string $meta_key Deprecated.
43 + * @param int $entry_id
44 + * @param int $field_id
45 + * @param string $meta_key deprecated
51 46 * @param array|string $meta_value
52 47 *
53 48 * @return bool|false|int
54 49 */
@@ -70,9 +65,8 @@
70 65
71 66 if ( is_array( $values['meta_value'] ) ) {
72 67 $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' );
73 68 }
74 -
75 69 $meta_value = maybe_serialize( $values['meta_value'] );
76 70
77 71 wp_cache_delete( $entry_id, 'frm_entry' );
78 72 self::clear_cache();
@@ -81,16 +75,11 @@
81 75 }
82 76
83 77 /**
84 78 * @since 3.0
85 - *
86 - * @param array $values
87 - *
88 - * @return void
89 79 */
90 80 private static function set_value_before_save( &$values ) {
91 81 $field = FrmField::getOne( $values['field_id'] );
92 -
93 82 if ( $field ) {
94 83 $field_obj = FrmFieldFactory::get_field_object( $field );
95 84
96 85 $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] );
@@ -98,13 +87,8 @@
98 87 }
99 88
100 89 /**
101 90 * @since 3.0
102 - *
103 - * @param array $atts
104 - * @param mixed $value
105 - *
106 - * @return void
107 91 */
108 92 private static function get_value_to_save( $atts, &$value ) {
109 93 if ( is_object( $atts['field'] ) ) {
110 94 $field_obj = FrmFieldFactory::get_field_object( $atts['field'] );
@@ -119,19 +103,13 @@
119 103
120 104 $value = apply_filters( 'frm_prepare_data_before_db', $value, $atts['field_id'], $atts['entry_id'], array( 'field' => $atts['field'] ) );
121 105 }
122 106
123 - /**
124 - * @param int|string $entry_id
125 - * @param array $values Either indexed by field ID or field key.
126 - *
127 - * @return void
128 - */
129 107 public static function update_entry_metas( $entry_id, $values ) {
130 108 global $wpdb;
131 109
132 - $previous_field_ids = FrmDb::get_col(
133 - 'frm_item_metas',
110 + $prev_values = FrmDb::get_col(
111 + $wpdb->prefix . 'frm_item_metas',
134 112 array(
135 113 'item_id' => $entry_id,
136 114 'field_id !' => 0,
137 115 ),
@@ -137,31 +115,21 @@
137 115 ),
138 116 'field_id'
139 117 );
140 118
141 - $values_indexed_by_field_id = array();
142 -
143 - foreach ( $values as $field_id_or_key => $meta_value ) {
144 - $field_id = $field_id_or_key;
145 - $field = null;
146 -
147 - if ( $field_id_or_key ) {
148 - $field = FrmField::getOne( $field_id_or_key );
149 -
150 - if ( is_object( $field ) ) {
151 - $field_id = $field->id;
152 - }
119 + foreach ( $values as $field_id => $meta_value ) {
120 + $field = false;
121 + if ( ! empty( $field_id ) ) {
122 + $field = FrmField::getOne( $field_id );
153 123 }
154 124
155 - $values_indexed_by_field_id[ $field_id ] = $meta_value;
156 -
157 125 self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value );
158 126
159 - if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) {
127 + if ( $prev_values && in_array( $field_id, $prev_values ) ) {
160 128
161 - if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) {
162 - // Remove blank fields.
163 - unset( $values_indexed_by_field_id[ $field_id ] );
129 + if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) {
130 + // remove blank fields
131 + unset( $values[ $field_id ] );
164 132 } else {
165 133 // if value exists, then update it
166 134 self::update_entry_meta( $entry_id, $field_id, '', $meta_value );
167 135 }
@@ -168,17 +136,17 @@
168 136 } else {
169 137 // if value does not exist, then create it
170 138 self::add_entry_meta( $entry_id, $field_id, '', $meta_value );
171 139 }
172 - }//end foreach
140 + }
173 141
174 - if ( empty( $previous_field_ids ) ) {
142 + if ( empty( $prev_values ) ) {
175 143 return;
176 144 }
177 145
178 - $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) );
146 + $prev_values = array_diff( $prev_values, array_keys( $values ) );
179 147
180 - if ( ! $field_ids_to_remove ) {
148 + if ( empty( $prev_values ) ) {
181 149 return;
182 150 }
183 151
184 152 // prepare the query
@@ -183,48 +151,26 @@
183 151
184 152 // prepare the query
185 153 $where = array(
186 154 'item_id' => $entry_id,
187 - 'field_id' => $field_ids_to_remove,
155 + 'field_id' => $prev_values,
188 156 );
189 157 FrmDb::get_where_clause_and_values( $where );
190 158
191 159 // Delete any leftovers
192 - $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
160 + $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // WPCS: unprepared SQL ok.
193 161 self::clear_cache();
194 162 }
195 163
196 - /**
197 - * @param int $old_id
198 - * @param int $new_id
199 - *
200 - * @return void
201 - */
202 164 public static function duplicate_entry_metas( $old_id, $new_id ) {
203 165 $metas = self::get_entry_meta_info( $old_id );
204 -
205 - /**
206 - * Allows changing entry duplicate values before save.
207 - *
208 - * @since 5.4.4
209 - *
210 - * @param array $metas The list of entry meta values.
211 - */
212 - $metas = apply_filters( 'frm_before_duplicate_entry_values', $metas );
213 -
214 166 foreach ( $metas as $meta ) {
215 - self::add_entry_meta( $new_id, $meta->field_id, '', $meta->meta_value );
167 + self::add_entry_meta( $new_id, $meta->field_id, null, $meta->meta_value );
216 168 unset( $meta );
217 169 }
218 170 self::clear_cache();
219 171 }
220 172
221 - /**
222 - * @param int $entry_id
223 - * @param int $field_id
224 - *
225 - * @return false|int
226 - */
227 173 public static function delete_entry_meta( $entry_id, $field_id ) {
228 174 global $wpdb;
229 175 self::clear_cache();
230 176
@@ -235,10 +181,8 @@
235 181 * Clear entry meta caching
236 182 * Called when a meta is added or changed
237 183 *
238 184 * @since 2.0.5
239 - *
240 - * @return void
241 185 */
242 186 public static function clear_cache() {
243 187 FrmDb::cache_delete_group( 'frm_entry_meta' );
244 188 FrmDb::cache_delete_group( 'frm_item_meta' );
@@ -245,27 +189,17 @@
245 189 }
246 190
247 191 /**
248 192 * @since 2.0.9
249 - *
250 - * @param stdClass $entry
251 - * @param int|string $field_id
252 - *
253 - * @return mixed
254 193 */
255 194 public static function get_meta_value( $entry, $field_id ) {
256 195 if ( isset( $entry->metas ) ) {
257 - return $entry->metas[ $field_id ] ?? false;
196 + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false;
197 + } else {
198 + return self::get_entry_meta_by_field( $entry->id, $field_id );
258 199 }
259 - return self::get_entry_meta_by_field( $entry->id, $field_id );
260 200 }
261 201
262 - /**
263 - * @param int|object|string $entry_id
264 - * @param int|string $field_id This function supports field keys as field id.
265 - *
266 - * @return mixed
267 - */
268 202 public static function get_entry_meta_by_field( $entry_id, $field_id ) {
269 203 global $wpdb;
270 204
271 205 if ( is_object( $entry_id ) ) {
@@ -284,36 +218,22 @@
284 218 }
285 219
286 220 $get_table = $wpdb->prefix . 'frm_item_metas';
287 221 $query = array( 'item_id' => $entry_id );
288 -
289 222 if ( is_numeric( $field_id ) ) {
290 - // Query by field ID.
291 223 $query['field_id'] = $field_id;
292 224 } else {
293 - // Query by field key.
294 - $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
225 + $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
295 226 $query['fi.field_key'] = $field_id;
296 227 }
297 228
298 229 $result = FrmDb::get_var( $get_table, $query, 'meta_value' );
299 -
300 - $field_type = FrmField::get_type( $field_id );
301 - FrmFieldsHelper::prepare_field_value( $result, $field_type );
302 -
230 + FrmAppHelper::unserialize_or_decode( $result );
303 231 $result = wp_unslash( $result );
304 232
305 233 return $result;
306 234 }
307 235
308 - /**
309 - * @param int|string $field_id
310 - * @param string $order
311 - * @param string $limit
312 - * @param array $args
313 - *
314 - * @return array
315 - */
316 236 public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) {
317 237 $defaults = array(
318 238 'value' => false,
319 239 'unique' => false,
@@ -342,15 +262,10 @@
342 262 return wp_unslash( $values );
343 263 }
344 264
345 265 /**
346 - * @param int|string $field_id
347 - * @param string $order
348 - * @param string $limit
349 - * @param array $args
350 - * @param array $query
351 - *
352 - * @return void
266 + * @param string $order
267 + * @param string $limit
353 268 */
354 269 private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) {
355 270 global $wpdb;
356 271 $query[] = 'SELECT';
@@ -376,25 +291,12 @@
376 291 }
377 292 $query[] = $order . $limit;
378 293 }
379 294
380 - /**
381 - * @param int|string $entry_id
382 - *
383 - * @return array
384 - */
385 295 public static function get_entry_meta_info( $entry_id ) {
386 296 return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) );
387 297 }
388 298
389 - /**
390 - * @param array $where
391 - * @param string $order_by
392 - * @param string $limit
393 - * @param bool $stripslashes
394 - *
395 - * @return mixed
396 - */
397 299 public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) {
398 300 global $wpdb;
399 301 $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key,
400 302 fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options
@@ -416,17 +318,8 @@
416 318
417 319 return $results;
418 320 }
419 321
420 - /**
421 - * @param array|string $where
422 - * @param string $order_by
423 - * @param string $limit
424 - * @param bool $unique
425 - * @param array $args
426 - *
427 - * @return array|string|null
428 - */
429 322 public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) {
430 323 $defaults = array(
431 324 'is_draft' => false,
432 325 'user_id' => '',
@@ -448,9 +341,8 @@
448 341 * If a child entry id is matched, its parent will be returned in its place
449 342 *
450 343 * @param array $query
451 344 * @param array $args
452 - *
453 345 * @return array
454 346 */
455 347 public static function get_top_level_entry_ids( $query, $args ) {
456 348 $args['return_parent_id_if_0_return_id'] = true;
@@ -457,52 +349,12 @@
457 349 return self::getEntryIds( $query, '', '', true, $args );
458 350 }
459 351
460 352 /**
461 - * Returns true if the where clause refers to a field table column that is not form_id. It also updates
462 - * the where clause to refer to the entry table for form_id if fields table should not be joined.
463 - *
464 - * @since 6.16.1
465 - *
466 - * @param array|string $where
467 - *
468 - * @return bool
353 + * @param string|array $where
354 + * @param string $order_by
355 + * @param string $limit
469 356 */
470 - private static function should_join_fields_table( &$where ) {
471 - if ( is_string( $where ) ) {
472 - if ( preg_match( '/\bfi\.(?!form_id)\w+/i', $where ) ) {
473 - return true;
474 - }
475 -
476 - $where = str_ireplace( 'fi.form_id', 'e.form_id', $where );
477 - return false;
478 - }
479 -
480 - $where_fields = array_keys( $where );
481 -
482 - foreach ( $where_fields as $where_field ) {
483 - if ( strpos( $where_field, 'fi.' ) === 0 && 'fi.form_id' !== $where_field ) {
484 - return true;
485 - }
486 - }
487 -
488 - if ( isset( $where['fi.form_id'] ) ) {
489 - $where['e.form_id'] = $where['fi.form_id'];
490 - unset( $where['fi.form_id'] );
491 - }
492 - return false;
493 - }
494 -
495 - /**
496 - * @param array|string $where
497 - * @param string $order_by
498 - * @param string $limit
499 - * @param bool $unique
500 - * @param array $args
501 - * @param array $query
502 - *
503 - * @return void
504 - */
505 357 private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) {
506 358 global $wpdb;
507 359 $query[] = 'SELECT';
508 360 $defaults = array(
@@ -522,48 +374,18 @@
522 374 } else {
523 375 $query[] = 'it.item_id';
524 376 }
525 377
526 - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it';
378 + $query[] = 'FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
527 379
528 - $should_join_fields_table__where = self::should_join_fields_table( $where );
529 - $should_join_fields_table__order_by = self::should_join_fields_table( $order_by );
530 -
531 - if ( $should_join_fields_table__where || $should_join_fields_table__order_by ) {
532 - $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id';
533 - }
534 -
535 - $query[] = $from;
536 380 $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)';
537 -
538 381 if ( is_array( $where ) ) {
539 382 if ( ! $args['is_draft'] ) {
540 383 $where['e.is_draft'] = 0;
541 - } elseif ( is_numeric( $args['is_draft'] ) ) {
542 - if ( class_exists( 'FrmAbandonmentHooksController', false ) ) {
543 - $where['e.is_draft'] = absint( $args['is_draft'] );
544 - } else {
545 - $where['e.is_draft'] = 1;
546 - }
547 - } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) {
548 - $where['e.is_draft'] = array( 0, 1 );
549 - } elseif ( false !== strpos( $args['is_draft'], ',' ) ) {
550 - $is_draft = array_reduce(
551 - explode( ',', $args['is_draft'] ),
552 - function ( $total, $current ) {
553 - if ( is_numeric( $current ) ) {
554 - $total[] = absint( $current );
555 - }
556 - return $total;
557 - },
558 - array()
559 - );
384 + } elseif ( $args['is_draft'] == 1 ) {
385 + $where['e.is_draft'] = 1;
386 + }
560 387
561 - if ( $is_draft ) {
562 - $where['e.is_draft'] = $is_draft;
563 - }
564 - }//end if
565 -
566 388 if ( ! empty( $args['user_id'] ) ) {
567 389 $where['e.user_id'] = $args['user_id'];
568 390 }
569 391 $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
@@ -572,13 +394,12 @@
572 394 $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] );
573 395 }
574 396
575 397 return;
576 - }//end if
398 + }
577 399
578 400 $draft_where = '';
579 401 $user_where = '';
580 -
581 402 if ( ! $args['is_draft'] ) {
582 403 $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 );
583 404 } elseif ( $args['is_draft'] == 1 ) {
584 405 $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 );
@@ -601,28 +422,18 @@
601 422 // The query has already been prepared
602 423 $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
603 424 }
604 425
605 - /**
606 - * @param array|string $search
607 - * @param int|string $field_id
608 - * @param string $operator
609 - *
610 - * @return array
611 - */
612 426 public static function search_entry_metas( $search, $field_id, $operator ) {
613 427 $cache_key = 'search_' . FrmAppHelper::maybe_json_encode( $search ) . $field_id . $operator;
614 428 $results = wp_cache_get( $cache_key, 'frm_entry' );
615 -
616 429 if ( false !== $results ) {
617 430 return $results;
618 431 }
619 432
620 433 global $wpdb;
621 -
622 434 if ( is_array( $search ) ) {
623 435 $where = '';
624 -
625 436 foreach ( $search as $field => $value ) {
626 437 if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) {
627 438 continue;
628 439 }
@@ -636,23 +447,20 @@
636 447 break;
637 448 case 'day':
638 449 $value = '%' . $value . '%';
639 450 }
640 -
641 - $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
451 + $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // WPCS: unprepared SQL ok.
642 452 }
643 -
644 453 $where .= $wpdb->prepare( ' field_id=%d', $field_id );
645 - $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where );
454 + $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where );
646 455 } else {
647 - if ( $operator === 'LIKE' ) {
456 + if ( $operator == 'LIKE' ) {
648 457 $search = '%' . $search . '%';
649 458 }
459 + $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 ); // WPCS: unprepared SQL ok.
460 + }
650 461
651 - $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
652 - }//end if
653 -
654 - $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
462 + $results = $wpdb->get_col( $query, 0 ); // WPCS: unprepared SQL ok.
655 463 FrmDb::set_cache( $cache_key, $results, 'frm_entry' );
656 464
657 465 return $results;
658 466 }