PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
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/FrmEntry.php +344 -517 6.43.06.06 View file →
@@ -5,14 +5,13 @@
5 5
6 6 class FrmEntry {
7 7
8 8 /**
9 - * Create a new entry
10 - *
11 - * @param array $values
12 - *
13 - * @return int | boolean $entry_id
14 - */
9 + * Create a new entry
10 + *
11 + * @param array $values
12 + * @return int | boolean $entry_id
13 + */
15 14 public static function create( $values ) {
16 15 $entry_id = self::create_entry( $values, 'standard' );
17 16
18 17 return $entry_id;
@@ -18,15 +17,14 @@
18 17 return $entry_id;
19 18 }
20 19
21 20 /**
22 - * Create a new entry with some differences depending on type
23 - *
24 - * @param array $values
25 - * @param string $type
26 - *
27 - * @return int | boolean $entry_id
28 - */
21 + * Create a new entry with some differences depending on type
22 + *
23 + * @param array $values
24 + * @param string $type
25 + * @return int | boolean $entry_id
26 + */
29 27 private static function create_entry( $values, $type ) {
30 28 $new_values = self::before_insert_entry_in_database( $values, $type );
31 29
32 30 // Don't check XML entries for duplicates
@@ -50,49 +48,43 @@
50 48 if ( false === self::is_duplicate_check_needed( $values, $duplicate_entry_time ) ) {
51 49 return false;
52 50 }
53 51
54 - $check_val = $new_values;
55 - $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) );
52 + $check_val = $new_values;
53 + $check_val['created_at >'] = date( 'Y-m-d H:i:s', ( strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ) );
56 54
57 - unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
55 + unset( $check_val['created_at'], $check_val['updated_at'] );
56 + unset( $check_val['is_draft'], $check_val['id'], $check_val['item_key'] );
58 57
59 - if ( $new_values['item_key'] == $new_values['name'] ) {
58 + if ( $new_values['item_key'] == $new_values['name'] ) {
60 59 unset( $check_val['name'] );
61 - }
60 + }
62 61
63 - $check_val = apply_filters( 'frm_duplicate_check_val', $check_val );
64 -
65 - global $wpdb;
62 + global $wpdb;
66 63 $entry_exists = FrmDb::get_col( $wpdb->prefix . 'frm_items', $check_val, 'id', array( 'order_by' => 'created_at DESC' ) );
67 64
68 - if ( ! $entry_exists || ! isset( $values['item_meta'] ) ) {
69 - return false;
70 - }
65 + if ( ! $entry_exists || empty( $entry_exists ) || ! isset( $values['item_meta'] ) ) {
66 + return false;
67 + }
71 68
72 - global $frm_vars;
73 - $frm_vars['checking_duplicates'] = true;
69 + $is_duplicate = false;
70 + foreach ( $entry_exists as $entry_exist ) {
71 + $is_duplicate = true;
74 72
75 - $is_duplicate = false;
76 - foreach ( $entry_exists as $entry_exist ) {
77 - $is_duplicate = true;
78 -
79 73 // make sure it's a duplicate
80 - $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
81 - $field_metas = array();
82 - foreach ( $metas as $meta ) {
74 + $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist );
75 + $field_metas = array();
76 + foreach ( $metas as $meta ) {
83 77 $field_metas[ $meta->field_id ] = $meta->meta_value;
84 - }
78 + }
85 79
86 - $filtered_vals = array_filter( $values['item_meta'] );
87 - $filtered_vals = self::convert_values_to_their_saved_value( $filtered_vals, $entry_exist );
80 + // If prev entry is empty and current entry is not, they are not duplicates
81 + $filtered_vals = array_filter( $values['item_meta'] );
88 82 $field_metas = array_filter( $field_metas );
83 + if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) {
84 + return false;
85 + }
89 86
90 - // If prev entry is empty and current entry is not, they are not duplicates
91 - if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) {
92 - return false;
93 - }
94 -
95 87 // compare serialized values and not arrays
96 88 $new_meta = array_map( 'maybe_serialize', $filtered_vals );
97 89
98 90 if ( $field_metas === $new_meta ) {
@@ -106,54 +98,29 @@
106 98 continue;
107 99 }
108 100
109 101 $diff = array_diff_assoc( $field_metas, $new_meta );
110 - foreach ( $diff as $field_id => $meta_value ) {
102 + foreach ( $diff as $field_id => $meta_value ) {
111 103 if ( ! empty( $meta_value ) ) {
112 - $is_duplicate = false;
113 - continue;
114 - }
115 - }
104 + $is_duplicate = false;
105 + continue;
106 + }
107 + }
116 108
117 - if ( $is_duplicate ) {
109 + if ( $is_duplicate ) {
118 110 break;
119 - }
120 - }
111 + }
112 + }
121 113
122 - $frm_vars['checking_duplicates'] = false;
114 + return $is_duplicate;
115 + }
123 116
124 - return $is_duplicate;
125 - }
126 -
127 117 /**
128 - * Convert form data to the actual value that would be saved into the database.
129 - * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' (a signature value) is actually an empty string and does not get saved.
130 - *
131 - * @param array $filter_vals
132 - * @param int $entry_id
133 - * @return array
134 - */
135 - private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) {
136 - $reduced = array();
137 - foreach ( $filter_vals as $field_id => $value ) {
138 - $field = FrmFieldFactory::get_field_object( $field_id );
139 - $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) );
140 - $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] );
141 - if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) {
142 - unset( $reduced[ $field_id ] );
143 - }
144 - }
145 - return $reduced;
146 - }
147 -
148 - /**
149 118 * Determine if an entry needs to be checked as a possible duplicate
150 119 *
151 120 * @since 2.0.23
152 - *
153 121 * @param array $values
154 122 * @param int $duplicate_entry_time
155 - *
156 123 * @return bool
157 124 */
158 125 private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) {
159 126 // If time for checking duplicates is set to an empty value, don't check for duplicates
@@ -178,9 +145,9 @@
178 145 global $wpdb;
179 146
180 147 $values = self::getOne( $id );
181 148
182 - $new_values = array();
149 + $new_values = array();
183 150 $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' );
184 151 $new_values['name'] = $values->name;
185 152 $new_values['is_draft'] = $values->is_draft;
186 153 $new_values['user_id'] = (int) $values->user_id;
@@ -189,36 +156,34 @@
189 156 $new_values['created_at'] = current_time( 'mysql', 1 );
190 157 $new_values['updated_at'] = $new_values['created_at'];
191 158
192 159 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
193 - if ( ! $query_results ) {
194 - return false;
195 - }
160 + if ( ! $query_results ) {
161 + return false;
162 + }
196 163
197 - $entry_id = $wpdb->insert_id;
164 + $entry_id = $wpdb->insert_id;
198 165
199 - global $frm_vars;
166 + global $frm_vars;
200 167 if ( ! isset( $frm_vars['saved_entries'] ) ) {
201 - $frm_vars['saved_entries'] = array();
202 - }
203 - $frm_vars['saved_entries'][] = (int) $entry_id;
168 + $frm_vars['saved_entries'] = array();
169 + }
170 + $frm_vars['saved_entries'][] = (int) $entry_id;
204 171
205 172 FrmEntryMeta::duplicate_entry_metas( $id, $entry_id );
206 173 self::clear_cache();
207 174
208 175 do_action( 'frm_after_duplicate_entry', $entry_id, $new_values['form_id'], array( 'old_id' => $id ) );
176 + return $entry_id;
177 + }
209 178
210 - return $entry_id;
211 - }
212 -
213 179 /**
214 - * Update an entry (not via XML)
215 - *
216 - * @param int $id
217 - * @param array $values
218 - *
219 - * @return boolean|int $update_results
220 - */
180 + * Update an entry (not via XML)
181 + *
182 + * @param int $id
183 + * @param array $values
184 + * @return boolean|int $update_results
185 + */
221 186 public static function update( $id, $values ) {
222 187 $update_results = self::update_entry( $id, $values, 'standard' );
223 188
224 189 return $update_results;
@@ -224,17 +189,16 @@
224 189 return $update_results;
225 190 }
226 191
227 192 /**
228 - * Update an entry with some differences depending on the update type
229 - *
230 - * @since 2.0.16
231 - *
232 - * @param int $id
233 - * @param array $values
234 - *
235 - * @return boolean|int $query_results
236 - */
193 + * Update an entry with some differences depending on the update type
194 + *
195 + * @since 2.0.16
196 + *
197 + * @param int $id
198 + * @param array $values
199 + * @return boolean|int $query_results
200 + */
237 201 private static function update_entry( $id, $values, $update_type ) {
238 202 global $wpdb;
239 203
240 204 $update = self::before_update_entry( $id, $values, $update_type );
@@ -250,30 +214,18 @@
250 214
251 215 return $query_results;
252 216 }
253 217
254 - /**
255 - * Delete an entry.
256 - *
257 - * @param string|int $id
258 - * @return bool True on success, false if nothing was deleted.
259 - */
260 218 public static function destroy( $id ) {
261 - global $wpdb;
262 - $id = (int) $id;
219 + global $wpdb;
220 + $id = (int) $id;
263 221
264 - $entry = self::getOne( $id, true ); // Item meta is required for conditional logic in actions with 'delete' events.
265 - if ( ! $entry ) {
266 - $result = false;
267 - return $result;
268 - }
222 + $entry = self::getOne( $id );
223 + if ( ! $entry ) {
224 + $result = false;
225 + return $result;
226 + }
269 227
270 - /**
271 - * Trigger an action to run custom logic before the entry is deleted.
272 - *
273 - * @param int $id The id of the entry that was destroyed.
274 - * @param stdClass $entry The entry object.
275 - */
276 228 do_action( 'frm_before_destroy_entry', $id, $entry );
277 229
278 230 $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas WHERE item_id=%d', $id ) );
279 231 $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
@@ -279,33 +231,21 @@
279 231 $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $id ) );
280 232
281 233 self::clear_cache();
282 234
283 - /**
284 - * Trigger an action to run custom logic after the entry is deleted.
285 - * Use this hook if you need to update caching after an entry is deleted.
286 - *
287 - * @since 5.4.1
288 - *
289 - * @param int $id The id of the entry that was destroyed.
290 - * @param stdClass $entry The entry object.
291 - */
292 - do_action( 'frm_after_destroy_entry', $id, $entry );
235 + return $result;
236 + }
293 237
294 - return $result;
295 - }
296 -
297 238 public static function update_form( $id, $value, $form_id ) {
298 - global $wpdb;
239 + global $wpdb;
299 240 $form_id = isset( $value ) ? $form_id : null;
300 - $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
241 + $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) );
301 242 if ( $result ) {
302 243 self::clear_cache();
303 244 }
245 + return $result;
246 + }
304 247
305 - return $result;
306 - }
307 -
308 248 /**
309 249 * Clear entry caching
310 250 * Called when an entry is changed
311 251 *
@@ -328,9 +268,8 @@
328 268 $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default );
329 269 if ( is_array( $name ) ) {
330 270 $name = reset( $name );
331 271 }
332 -
333 272 return $name;
334 273 }
335 274
336 275 /**
@@ -336,215 +275,178 @@
336 275 /**
337 276 * If $entry is numeric, get the entry object
338 277 *
339 278 * @param int|object $entry by reference
340 - *
341 279 * @since 2.0.9
342 280 */
343 281 public static function maybe_get_entry( &$entry ) {
344 282 if ( $entry && is_numeric( $entry ) ) {
345 283 $entry = self::getOne( $entry );
346 - } elseif ( empty( $entry ) || 'false' === $entry ) {
284 + } elseif ( empty( $entry ) ) {
347 285 $entry = false;
348 286 }
349 287 }
350 288
351 289 public static function getOne( $id, $meta = false ) {
352 - global $wpdb;
290 + global $wpdb;
353 291
354 - $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
292 + $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it
355 293 LEFT OUTER JOIN {$wpdb->prefix}frm_forms fr ON it.form_id=fr.id WHERE ";
356 294
357 - $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
358 - $query_args = array( $id );
359 - $query = $wpdb->prepare( $query, $query_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
295 + $query .= is_numeric( $id ) ? 'it.id=%d' : 'it.item_key=%s';
296 + $query_args = array( $id );
297 + $query = $wpdb->prepare( $query, $query_args ); // WPCS: unprepared SQL ok.
360 298
361 - if ( ! $meta ) {
299 + if ( ! $meta ) {
362 300 $entry = FrmDb::check_cache( $id . '_nometa', 'frm_entry', $query, 'get_row' );
363 - self::prepare_entry( $entry );
364 - return $entry;
365 - }
301 + return stripslashes_deep( $entry );
302 + }
366 303
367 304 $entry = FrmDb::check_cache( $id, 'frm_entry' );
368 305 if ( $entry !== false ) {
369 - self::prepare_entry( $entry );
370 - return $entry;
306 + return stripslashes_deep( $entry );
371 307 }
372 308
373 - $entry = $wpdb->get_row( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
309 + $entry = $wpdb->get_row( $query ); // WPCS: unprepared SQL ok.
374 310 $entry = self::get_meta( $entry );
375 - self::prepare_entry( $entry );
376 311
377 - return $entry;
378 - }
312 + return stripslashes_deep( $entry );
313 + }
379 314
380 - /**
381 - * @since 4.02.03
382 - *
383 - * @param object $entry
384 - */
385 - private static function prepare_entry( &$entry ) {
386 - if ( empty( $entry ) ) {
387 - return;
388 - }
389 -
390 - FrmAppHelper::unserialize_or_decode( $entry->description );
391 - $entry = wp_unslash( $entry ); // TODO: Remove slashes on input only, not output.
392 - }
393 -
394 - /**
395 - * @since 4.02.03
396 - *
397 - * @param array $entries
398 - */
399 - private static function prepare_entries( &$entries ) {
400 - foreach ( $entries as $k => $entry ) {
401 - self::prepare_entry( $entry );
402 - $entries[ $k ] = $entry;
403 - }
404 - }
405 -
406 315 public static function get_meta( $entry ) {
407 - if ( ! $entry ) {
408 - return $entry;
409 - }
316 + if ( ! $entry ) {
317 + return $entry;
318 + }
410 319
411 - global $wpdb;
320 + global $wpdb;
412 321 $metas = FrmDb::get_results(
413 322 $wpdb->prefix . 'frm_item_metas m LEFT JOIN ' . $wpdb->prefix . 'frm_fields f ON m.field_id=f.id',
414 323 array(
415 - 'item_id' => $entry->id,
324 + 'item_id' => $entry->id,
416 325 'field_id !' => 0,
417 326 ),
418 - 'field_id, meta_value, field_key, item_id, f.type'
327 + 'field_id, meta_value, field_key, item_id'
419 328 );
420 329
421 - $entry->metas = array();
330 + $entry->metas = array();
422 331
423 332 $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) );
424 - foreach ( $metas as $meta_val ) {
425 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
426 -
427 - if ( $meta_val->item_id == $entry->id ) {
428 - $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value;
333 + foreach ( $metas as $meta_val ) {
334 + if ( $meta_val->item_id == $entry->id ) {
335 + $entry->metas[ $meta_val->field_id ] = maybe_unserialize( $meta_val->meta_value );
429 336 if ( $include_key ) {
430 337 $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ];
431 338 }
432 - continue;
433 - }
339 + continue;
340 + }
434 341
435 - // include sub entries in an array
436 - if ( ! isset( $entry->metas[ $meta_val->field_id ] ) ) {
342 + // include sub entries in an array
343 + if ( ! isset( $entry_metas[ $meta_val->field_id ] ) ) {
437 344 $entry->metas[ $meta_val->field_id ] = array();
438 - }
345 + }
439 346
440 - $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value;
347 + $entry->metas[ $meta_val->field_id ][] = maybe_unserialize( $meta_val->meta_value );
441 348
442 349 unset( $meta_val );
443 - }
350 + }
444 351 unset( $metas );
445 352
446 353 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
447 354
448 - return $entry;
449 - }
355 + return $entry;
356 + }
450 357
451 - /**
452 - * @param string $id
453 - */
358 + /**
359 + * @param string $id
360 + */
454 361 public static function exists( $id ) {
455 - global $wpdb;
362 + global $wpdb;
456 363
457 - if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
458 - $exists = true;
364 + if ( FrmDb::check_cache( $id, 'frm_entry' ) ) {
365 + $exists = true;
366 + return $exists;
367 + }
459 368
460 - return $exists;
461 - }
462 -
463 369 if ( is_numeric( $id ) ) {
464 - $where = array( 'id' => $id );
465 - } else {
466 - $where = array( 'item_key' => $id );
467 - }
370 + $where = array( 'id' => $id );
371 + } else {
372 + $where = array( 'item_key' => $id );
373 + }
468 374 $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where );
469 375
470 376 return ( $id && $id > 0 );
471 - }
377 + }
472 378
473 - public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
379 + public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) {
474 380 global $wpdb;
475 381
476 382 $limit = FrmDb::esc_limit( $limit );
477 383
478 - $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form;
479 - $entries = wp_cache_get( $cache_key, 'frm_entry' );
384 + $cache_key = maybe_serialize( $where ) . $order_by . $limit . $inc_form;
385 + $entries = wp_cache_get( $cache_key, 'frm_entry' );
480 386
481 - if ( false === $entries ) {
482 - $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft, it.description';
483 - $table = $wpdb->prefix . 'frm_items it ';
387 + if ( false === $entries ) {
388 + $fields = 'it.id, it.item_key, it.name, it.ip, it.form_id, it.post_id, it.user_id, it.parent_item_id, it.updated_by, it.created_at, it.updated_at, it.is_draft';
389 + $table = $wpdb->prefix . 'frm_items it ';
484 390
485 - if ( $inc_form ) {
486 - $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
487 - $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
488 - }
391 + if ( $inc_form ) {
392 + $fields = 'it.*, fr.name as form_name,fr.form_key as form_key';
393 + $table .= 'LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id ';
394 + }
489 395
490 - if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
491 - $fields .= self::sort_by_field( $order_matches[1] );
492 - unset( $order_matches );
493 - }
396 + if ( preg_match( '/ meta_([0-9]+)/', $order_by, $order_matches ) ) {
397 + // sort by a requested field
398 + $field_id = (int) $order_matches[1];
399 + $fields .= ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
400 + unset( $order_matches, $field_id );
401 + }
494 402
495 403 // prepare the query
496 404 $query = 'SELECT ' . $fields . ' FROM ' . $table . FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit;
497 405
498 - $entries = $wpdb->get_results( $query, OBJECT_K ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
406 + $entries = $wpdb->get_results( $query, OBJECT_K ); // WPCS: unprepared SQL ok.
499 407 unset( $query );
500 408
501 409 FrmDb::set_cache( $cache_key, $entries, 'frm_entry' );
502 - }
410 + }
503 411
504 - if ( ! $meta || ! $entries ) {
505 - self::prepare_entries( $entries );
506 - return $entries;
507 - }
412 + if ( ! $meta || ! $entries ) {
413 + return stripslashes_deep( $entries );
414 + }
508 415 unset( $meta );
509 416
510 417 if ( ! is_array( $where ) && preg_match( '/^it\.form_id=\d+$/', $where ) ) {
511 418 $where = array( 'it.form_id' => substr( $where, 11 ) );
512 - }
419 + }
513 420
514 - $meta_where = array( 'field_id !' => 0 );
421 + $meta_where = array( 'field_id !' => 0 );
515 422 if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) {
516 - $meta_where['fi.form_id'] = $where['it.form_id'];
517 - } else {
518 - $meta_where['item_id'] = array_keys( $entries );
519 - }
423 + $meta_where['fi.form_id'] = $where['it.form_id'];
424 + } else {
425 + $meta_where['item_id'] = array_keys( $entries );
426 + }
520 427
521 - $metas = FrmDb::get_results(
522 - $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id = fi.id',
523 - $meta_where,
524 - 'item_id, meta_value, field_id, field_key, form_id, fi.type'
525 - );
428 + $metas = FrmDb::get_results( $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON (it.field_id = fi.id)', $meta_where, 'item_id, meta_value, field_id, field_key, form_id' );
526 429
527 - unset( $meta_where );
430 + unset( $meta_where );
528 431
529 - if ( ! $metas ) {
530 - self::prepare_entries( $entries );
531 - return $entries;
532 - }
432 + if ( ! $metas ) {
433 + return stripslashes_deep( $entries );
434 + }
533 435
534 - foreach ( $metas as $m_key => $meta_val ) {
535 - if ( ! isset( $entries[ $meta_val->item_id ] ) ) {
536 - continue;
537 - }
436 + foreach ( $metas as $m_key => $meta_val ) {
437 + if ( ! isset( $entries[ $meta_val->item_id ] ) ) {
438 + continue;
439 + }
538 440
539 - if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
441 + if ( ! isset( $entries[ $meta_val->item_id ]->metas ) ) {
540 442 $entries[ $meta_val->item_id ]->metas = array();
541 - }
443 + }
542 444
543 - FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type );
544 - $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = $meta_val->meta_value;
445 + $entries[ $meta_val->item_id ]->metas[ $meta_val->field_id ] = maybe_unserialize( $meta_val->meta_value );
446 +
545 447 unset( $m_key, $meta_val );
546 - }
448 + }
547 449
548 450 if ( ! FrmAppHelper::prevent_caching() ) {
549 451 foreach ( $entries as $entry ) {
550 452 FrmDb::set_cache( $entry->id, $entry, 'frm_entry' );
@@ -551,59 +453,38 @@
551 453 unset( $entry );
552 454 }
553 455 }
554 456
555 - self::prepare_entries( $entries );
556 - return $entries;
557 - }
457 + return stripslashes_deep( $entries );
458 + }
558 459
460 + // Pagination Methods
559 461 /**
560 - * @param int $field_id
561 - * @return string
562 - */
563 - private static function sort_by_field( $field_id ) {
564 - global $wpdb;
565 - $field_id = (int) $field_id;
566 -
567 - $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' );
568 - FrmAppHelper::unserialize_or_decode( $field_options );
569 -
570 - if ( empty( $field_options['post_field'] ) ) {
571 - $sort = ', (SELECT meta_value FROM ' . $wpdb->prefix . 'frm_item_metas WHERE field_id = ' . $field_id . ' AND item_id = it.id) as meta_' . $field_id;
572 - } else {
573 - $sort = '';
574 - }
575 -
576 - return apply_filters( 'frm_handle_field_column_sort', $sort, $field_id, $field_options );
577 - }
578 -
579 - // Pagination Methods
580 - /**
581 462 * @param int|array|string If int, use the form id.
582 463 */
583 - public static function getRecordCount( $where = '' ) {
584 - global $wpdb;
464 + public static function getRecordCount( $where = '' ) {
465 + global $wpdb;
585 466 $table_join = $wpdb->prefix . 'frm_items it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_forms fr ON it.form_id=fr.id';
586 467
587 468 if ( is_numeric( $where ) ) {
588 - $table_join = 'frm_items';
589 - $where = array( 'form_id' => $where );
590 - }
469 + $table_join = 'frm_items';
470 + $where = array( 'form_id' => $where );
471 + }
591 472
592 - if ( is_array( $where ) ) {
593 - $count = FrmDb::get_count( $table_join, $where );
594 - } else {
595 - $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where );
596 - $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
597 - $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
598 - }
473 + if ( is_array( $where ) ) {
474 + $count = FrmDb::get_count( $table_join, $where );
475 + } else {
476 + $cache_key = 'count_' . maybe_serialize( $where );
477 + $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where );
478 + $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' );
479 + }
599 480
600 - return $count;
601 - }
481 + return $count;
482 + }
602 483
603 - public static function getPageCount( $p_size, $where = '' ) {
484 + public static function getPageCount( $p_size, $where = '' ) {
604 485 $p_size = (int) $p_size;
605 - $count = 1;
486 + $count = 1;
606 487 if ( $p_size ) {
607 488 if ( ! is_numeric( $where ) ) {
608 489 $where = self::getRecordCount( $where );
609 490 }
@@ -610,20 +491,18 @@
610 491 $count = ceil( (int) $where / $p_size );
611 492 }
612 493
613 494 return $count;
614 - }
495 + }
615 496
616 497 /**
617 - * Prepare the data before inserting it into the database
618 - *
619 - * @since 2.0.16
620 - *
621 - * @param array $values
622 - * @param string $type
623 - *
624 - * @return array $new_values
625 - */
498 + * Prepare the data before inserting it into the database
499 + *
500 + * @since 2.0.16
501 + * @param array $values
502 + * @param string $type
503 + * @return array $new_values
504 + */
626 505 private static function before_insert_entry_in_database( &$values, $type ) {
627 506
628 507 self::sanitize_entry_post( $values );
629 508
@@ -636,17 +515,15 @@
636 515 return $new_values;
637 516 }
638 517
639 518 /**
640 - * Create an entry and perform after create actions
641 - *
642 - * @since 2.0.16
643 - *
644 - * @param array $values
645 - * @param array $new_values
646 - *
647 - * @return boolean|int $entry_id
648 - */
519 + * Create an entry and perform after create actions
520 + *
521 + * @since 2.0.16
522 + * @param array $values
523 + * @param array $new_values
524 + * @return boolean|int $entry_id
525 + */
649 526 private static function continue_to_create_entry( $values, $new_values ) {
650 527 $entry_id = self::insert_entry_into_database( $new_values );
651 528 if ( ! $entry_id ) {
652 529 return false;
@@ -656,42 +533,39 @@
656 533
657 534 return $entry_id;
658 535 }
659 536
660 - /**
661 - * Sanitize the POST values before we use them
662 - *
663 - * @since 2.0
664 - *
665 - * @param array $values The POST values by reference
666 - */
667 - public static function sanitize_entry_post( &$values ) {
668 - $sanitize_method = array(
669 - 'form_id' => 'absint',
670 - 'frm_action' => 'sanitize_title',
671 - 'form_key' => 'sanitize_title',
672 - 'item_key' => 'sanitize_title',
673 - 'item_name' => 'sanitize_text_field',
674 - 'frm_saving_draft' => 'absint',
675 - 'is_draft' => 'absint',
676 - 'post_id' => 'absint',
677 - 'parent_item_id' => 'absint',
678 - 'created_at' => 'sanitize_text_field',
679 - 'updated_at' => 'sanitize_text_field',
680 - );
537 + /**
538 + * Sanitize the POST values before we use them
539 + *
540 + * @since 2.0
541 + * @param array $values The POST values by reference
542 + */
543 + public static function sanitize_entry_post( &$values ) {
544 + $sanitize_method = array(
545 + 'form_id' => 'absint',
546 + 'frm_action' => 'sanitize_title',
547 + 'form_key' => 'sanitize_title',
548 + 'item_key' => 'sanitize_title',
549 + 'item_name' => 'sanitize_text_field',
550 + 'frm_saving_draft' => 'absint',
551 + 'is_draft' => 'absint',
552 + 'post_id' => 'absint',
553 + 'parent_item_id' => 'absint',
554 + 'created_at' => 'sanitize_text_field',
555 + 'updated_at' => 'sanitize_text_field',
556 + );
681 557
682 - FrmAppHelper::sanitize_request( $sanitize_method, $values );
683 - }
558 + FrmAppHelper::sanitize_request( $sanitize_method, $values );
559 + }
684 560
685 561 /**
686 - * Prepare the new values for inserting into the database
687 - *
688 - * @since 2.0.16
689 - *
690 - * @param array $values
691 - *
692 - * @return array $new_values
693 - */
562 + * Prepare the new values for inserting into the database
563 + *
564 + * @since 2.0.16
565 + * @param array $values
566 + * @return array $new_values
567 + */
694 568 private static function package_entry_data( &$values ) {
695 569 global $wpdb;
696 570
697 571 if ( ! isset( $values['item_key'] ) ) {
@@ -697,21 +571,21 @@
697 571 if ( ! isset( $values['item_key'] ) ) {
698 572 $values['item_key'] = '';
699 573 }
700 574
701 - $item_name = self::get_new_entry_name( $values, $values['item_key'] );
575 + $item_name = self::get_new_entry_name( $values, $values['item_key'] );
702 576 $new_values = array(
703 - 'item_key' => FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key' ),
704 - 'name' => FrmAppHelper::truncate( $item_name, 255, 1, '' ),
705 - 'ip' => self::get_ip( $values ),
706 - 'is_draft' => self::get_is_draft_value( $values ),
707 - 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
708 - 'post_id' => (int) self::get_entry_value( $values, 'post_id', 0 ),
577 + 'item_key' => FrmAppHelper::get_unique_key( $values['item_key'], $wpdb->prefix . 'frm_items', 'item_key' ),
578 + 'name' => FrmAppHelper::truncate( $item_name, 255, 1, '' ),
579 + 'ip' => self::get_ip( $values ),
580 + 'is_draft' => self::get_is_draft_value( $values ),
581 + 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
582 + 'post_id' => (int) self::get_entry_value( $values, 'post_id', 0 ),
709 583 'parent_item_id' => (int) self::get_entry_value( $values, 'parent_item_id', 0 ),
710 - 'created_at' => self::get_created_at( $values ),
711 - 'updated_at' => self::get_updated_at( $values ),
712 - 'description' => self::get_entry_description( $values ),
713 - 'user_id' => self::get_entry_user_id( $values ),
584 + 'created_at' => self::get_created_at( $values ),
585 + 'updated_at' => self::get_updated_at( $values ),
586 + 'description' => self::get_entry_description( $values ),
587 + 'user_id' => self::get_entry_user_id( $values ),
714 588 );
715 589
716 590 $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id'];
717 591
@@ -726,11 +600,9 @@
726 600 * Get the ip for a new entry.
727 601 * Allow the import to override the value.
728 602 *
729 603 * @since 2.03.10
730 - *
731 604 * @param array $values
732 - *
733 605 * @return string
734 606 */
735 607 private static function get_ip( $values ) {
736 608 if ( ! FrmAppHelper::ips_saved() ) {
@@ -740,47 +612,40 @@
740 612 $ip = FrmAppHelper::get_ip_address();
741 613 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
742 614 $ip = self::get_entry_value( $values, 'ip', $ip );
743 615 }
744 -
745 616 return $ip;
746 617 }
747 618
748 619 /**
749 - * Get the is_draft value for a new entry
750 - *
751 - * @since 2.0.16
752 - *
753 - * @param array $values
754 - *
755 - * @return int
756 - */
620 + * Get the is_draft value for a new entry
621 + *
622 + * @since 2.0.16
623 + * @param array $values
624 + * @return int
625 + */
757 626 private static function get_is_draft_value( $values ) {
758 627 return ( ( isset( $values['frm_saving_draft'] ) && $values['frm_saving_draft'] == 1 ) || ( isset( $values['is_draft'] ) && $values['is_draft'] == 1 ) ) ? 1 : 0;
759 628 }
760 629
761 630 /**
762 - * Get the created_at value for a new entry
763 - *
764 - * @since 2.0.16
765 - *
766 - * @param array $values
767 - *
768 - * @return string
769 - */
631 + * Get the created_at value for a new entry
632 + *
633 + * @since 2.0.16
634 + * @param array $values
635 + * @return string
636 + */
770 637 private static function get_created_at( $values ) {
771 638 return self::get_entry_value( $values, 'created_at', current_time( 'mysql', 1 ) );
772 639 }
773 640
774 641 /**
775 - * Get the updated_at value for a new entry
776 - *
777 - * @since 2.0.16
778 - *
779 - * @param array $values
780 - *
781 - * @return string
782 - */
642 + * Get the updated_at value for a new entry
643 + *
644 + * @since 2.0.16
645 + * @param array $values
646 + * @return string
647 + */
783 648 private static function get_updated_at( $values ) {
784 649 if ( isset( $values['updated_at'] ) ) {
785 650 $updated_at = $values['updated_at'];
786 651 } else {
@@ -790,21 +655,19 @@
790 655 return $updated_at;
791 656 }
792 657
793 658 /**
794 - * Get the description value for a new entry
795 - *
796 - * @since 2.0.16
797 - *
798 - * @param array $values
799 - *
800 - * @return string
801 - */
659 + * Get the description value for a new entry
660 + *
661 + * @since 2.0.16
662 + * @param array $values
663 + * @return string
664 + */
802 665 private static function get_entry_description( $values ) {
803 666 if ( isset( $values['description'] ) && ! empty( $values['description'] ) ) {
804 - $description = FrmAppHelper::maybe_json_encode( $values['description'] );
667 + $description = maybe_serialize( $values['description'] );
805 668 } else {
806 - $description = json_encode(
669 + $description = serialize(
807 670 array(
808 671 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ),
809 672 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ),
810 673 )
@@ -814,22 +677,20 @@
814 677 return $description;
815 678 }
816 679
817 680 /**
818 - * Get the user_id value for a new entry
819 - *
820 - * @since 2.0.16
821 - *
822 - * @param array $values
823 - *
824 - * @return int
825 - */
681 + * Get the user_id value for a new entry
682 + *
683 + * @since 2.0.16
684 + * @param array $values
685 + * @return int
686 + */
826 687 private static function get_entry_user_id( $values ) {
827 688 if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) {
828 689 $user_id = $values['frm_user_id'];
829 690 } else {
830 691 $current_user_id = get_current_user_id();
831 - $user_id = $current_user_id ? $current_user_id : 0;
692 + $user_id = $current_user_id ? $current_user_id : 0;
832 693 }
833 694
834 695 return $user_id;
835 696 }
@@ -834,16 +695,14 @@
834 695 return $user_id;
835 696 }
836 697
837 698 /**
838 - * Insert new entry into the database
839 - *
840 - * @since 2.0.16
841 - *
842 - * @param array $new_values
843 - *
844 - * @return int | boolean $entry_id
845 - */
699 + * Insert new entry into the database
700 + *
701 + * @since 2.0.16
702 + * @param array $new_values
703 + * @return int | boolean $entry_id
704 + */
846 705 private static function insert_entry_into_database( $new_values ) {
847 706 global $wpdb;
848 707
849 708 $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values );
@@ -857,14 +716,13 @@
857 716 return $entry_id;
858 717 }
859 718
860 719 /**
861 - * Add the new entry to global $frm_vars
862 - *
863 - * @since 2.0.16
864 - *
865 - * @param int $entry_id
866 - */
720 + * Add the new entry to global $frm_vars
721 + *
722 + * @since 2.0.16
723 + * @param int $entry_id
724 + */
867 725 private static function add_new_entry_to_frm_vars( $entry_id ) {
868 726 global $frm_vars;
869 727
870 728 if ( ! isset( $frm_vars['saved_entries'] ) ) {
@@ -874,46 +732,27 @@
874 732 $frm_vars['saved_entries'][] = (int) $entry_id;
875 733 }
876 734
877 735 /**
878 - * Add entry metas, if there are any
879 - *
880 - * @since 2.0.16
881 - *
882 - * @param array $values
883 - * @param int $entry_id
884 - * @return void
885 - */
736 + * Add entry metas, if there are any
737 + *
738 + * @since 2.0.16
739 + * @param array $values
740 + * @param int $entry_id
741 + */
886 742 private static function maybe_add_entry_metas( $values, $entry_id ) {
887 743 if ( isset( $values['item_meta'] ) ) {
888 744 FrmEntryMeta::update_entry_metas( $entry_id, $values['item_meta'] );
889 745 }
890 - self::maybe_add_captcha_meta( (int) $values['form_id'], (int) $entry_id );
891 746 }
892 747
893 748 /**
894 - * @since 5.0.15
895 - *
896 - * @param int $form_id
897 - * @param int $entry_id
898 - * @return void
899 - */
900 - private static function maybe_add_captcha_meta( $form_id, $entry_id ) {
901 - global $frm_vars;
902 - if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) {
903 - $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] );
904 - FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) );
905 - }
906 - }
907 -
908 - /**
909 - * Trigger frm_after_create_entry hooks
910 - *
911 - * @since 2.0.16
912 - *
913 - * @param int $entry_id
914 - * @param array $new_values
915 - */
749 + * Trigger frm_after_create_entry hooks
750 + *
751 + * @since 2.0.16
752 + * @param int $entry_id
753 + * @param array $new_values
754 + */
916 755 private static function after_entry_created_actions( $entry_id, $values, $new_values ) {
917 756 // this is a child entry
918 757 $is_child = isset( $values['parent_form_id'] ) && isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' );
919 758
@@ -921,16 +760,15 @@
921 760 do_action( 'frm_after_create_entry_' . $new_values['form_id'], $entry_id, compact( 'is_child' ) );
922 761 }
923 762
924 763 /**
925 - * Actions to perform immediately after an entry is inserted in the frm_items database
926 - *
927 - * @since 2.0.16
928 - *
929 - * @param array $values
930 - * @param array $new_values
931 - * @param int $entry_id
932 - */
764 + * Actions to perform immediately after an entry is inserted in the frm_items database
765 + *
766 + * @since 2.0.16
767 + * @param array $values
768 + * @param array $new_values
769 + * @param int $entry_id
770 + */
933 771 private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) {
934 772
935 773 self::add_new_entry_to_frm_vars( $entry_id );
936 774
@@ -941,18 +779,16 @@
941 779 self::after_entry_created_actions( $entry_id, $values, $new_values );
942 780 }
943 781
944 782 /**
945 - * Perform some actions right before updating an entry
946 - *
947 - * @since 2.0.16
948 - *
949 - * @param int $id
950 - * @param array $values
951 - * @param string $update_type
952 - *
953 - * @return boolean $update
954 - */
783 + * Perform some actions right before updating an entry
784 + *
785 + * @since 2.0.16
786 + * @param int $id
787 + * @param array $values
788 + * @param string $update_type
789 + * @return boolean $update
790 + */
955 791 private static function before_update_entry( $id, &$values, $update_type ) {
956 792 $update = true;
957 793
958 794 global $frm_vars;
@@ -968,24 +804,22 @@
968 804 return $update;
969 805 }
970 806
971 807 /**
972 - * Package the entry data for updating
973 - *
974 - * @since 2.0.16
975 - *
976 - * @param int $id
977 - * @param array $values
978 - *
979 - * @return array $new_values
980 - */
808 + * Package the entry data for updating
809 + *
810 + * @since 2.0.16
811 + * @param int $id
812 + * @param array $values
813 + * @return array $new_values
814 + */
981 815 private static function package_entry_to_update( $id, $values ) {
982 816 global $wpdb;
983 817
984 818 $new_values = array(
985 - 'name' => self::get_new_entry_name( $values ),
986 - 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
987 - 'is_draft' => self::get_is_draft_value( $values ),
819 + 'name' => self::get_new_entry_name( $values ),
820 + 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ),
821 + 'is_draft' => self::get_is_draft_value( $values ),
988 822 'updated_at' => current_time( 'mysql', 1 ),
989 823 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(),
990 824 );
991 825
@@ -1010,17 +844,16 @@
1010 844 return $new_values;
1011 845 }
1012 846
1013 847 /**
1014 - * Perform some actions right after updating an entry
1015 - *
1016 - * @since 2.0.16
1017 - *
1018 - * @param boolean|int $query_results
1019 - * @param int $id
1020 - * @param array $values
1021 - * @param array $new_values
1022 - */
848 + * Perform some actions right after updating an entry
849 + *
850 + * @since 2.0.16
851 + * @param boolean|int $query_results
852 + * @param int $id
853 + * @param array $values
854 + * @param array $new_values
855 + */
1023 856 private static function after_update_entry( $query_results, $id, $values, $new_values ) {
1024 857 if ( $query_results ) {
1025 858 self::clear_cache();
1026 859 }
@@ -1040,17 +873,15 @@
1040 873 do_action( 'frm_after_update_entry_' . $new_values['form_id'], $id );
1041 874 }
1042 875
1043 876 /**
1044 - * Create entry from an XML import
1045 - * Certain actions aren't necessary when importing (like saving sub entries, checking for duplicates, etc.)
1046 - *
1047 - * @since 2.0.16
1048 - *
1049 - * @param array $values
1050 - *
1051 - * @return int | boolean $entry_id
1052 - */
877 + * Create entry from an XML import
878 + * Certain actions aren't necessary when importing (like saving sub entries, checking for duplicates, etc.)
879 + *
880 + * @since 2.0.16
881 + * @param array $values
882 + * @return int | boolean $entry_id
883 + */
1053 884 public static function create_entry_from_xml( $values ) {
1054 885 $entry_id = self::create_entry( $values, 'xml' );
1055 886
1056 887 return $entry_id;
@@ -1056,18 +887,16 @@
1056 887 return $entry_id;
1057 888 }
1058 889
1059 890 /**
1060 - * Update entry from an XML import
1061 - * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
1062 - *
1063 - * @since 2.0.16
1064 - *
1065 - * @param int $id
1066 - * @param array $values
1067 - *
1068 - * @return int | boolean $updated
1069 - */
891 + * Update entry from an XML import
892 + * Certain actions aren't necessary when importing (like saving sub entries and modifying other vals)
893 + *
894 + * @since 2.0.16
895 + * @param int $id
896 + * @param array $values
897 + * @return int | boolean $updated
898 + */
1070 899 public static function update_entry_from_xml( $id, $values ) {
1071 900 $updated = self::update_entry( $id, $values, 'xml' );
1072 901
1073 902 return $updated;
@@ -1074,13 +903,11 @@
1074 903 }
1075 904
1076 905 /**
1077 906 * @param string $key
1078 - *
1079 907 * @return int entry_id
1080 908 */
1081 909 public static function get_id_by_key( $key ) {
1082 910 $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) );
1083 -
1084 911 return (int) $entry_id;
1085 912 }
1086 913 }