| @@ -19,9 +19,11 @@ | ||
| 19 | 19 | * |
| 20 | 20 | * @return bool|int $entry_id |
| 21 | 21 | */ |
| 22 | 22 | public static function create( $values ) { |
| 23 | - return self::create_entry( $values, 'standard' ); | |
| 23 | + $entry_id = self::create_entry( $values, 'standard' ); | |
| 24 | + | |
| 25 | + return $entry_id; | |
| 24 | 26 | } |
| 25 | 27 | |
| 26 | 28 | /** |
| 27 | 29 | * Create a new entry with some differences depending on type |
| @@ -38,9 +40,11 @@ | ||
| 38 | 40 | if ( $type !== 'xml' && self::is_duplicate( $new_values, $values ) ) { |
| 39 | 41 | return false; |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | - return self::continue_to_create_entry( $values, $new_values ); | |
| 44 | + $entry_id = self::continue_to_create_entry( $values, $new_values ); | |
| 45 | + | |
| 46 | + return $entry_id; | |
| 43 | 47 | } |
| 44 | 48 | |
| 45 | 49 | /** |
| 46 | 50 | * Flag the memoized unique id check after a new entry is created. |
| @@ -48,9 +52,8 @@ | ||
| 48 | 52 | * |
| 49 | 53 | * @since 6.16.3 |
| 50 | 54 | * |
| 51 | 55 | * @param string $unique_id |
| 52 | - * | |
| 53 | 56 | * @return void |
| 54 | 57 | */ |
| 55 | 58 | private static function flag_new_unique_key( $unique_id ) { |
| 56 | 59 | if ( ! isset( self::$unique_id_match_checks[ $unique_id ] ) ) { |
| @@ -60,11 +63,8 @@ | ||
| 60 | 63 | |
| 61 | 64 | /** |
| 62 | 65 | * Check for duplicate entries created in the last minute |
| 63 | 66 | * |
| 64 | - * @param array $new_values New values. | |
| 65 | - * @param array $values Values. | |
| 66 | - * | |
| 67 | 67 | * @return bool |
| 68 | 68 | */ |
| 69 | 69 | public static function is_duplicate( $new_values, $values ) { |
| 70 | 70 | $duplicate_entry_time = apply_filters( 'frm_time_to_check_duplicates', 60, $new_values ); |
| @@ -81,9 +81,8 @@ | ||
| 81 | 81 | $check_val['created_at >'] = gmdate( 'Y-m-d H:i:s', strtotime( $new_values['created_at'] ) - absint( $duplicate_entry_time ) ); |
| 82 | 82 | |
| 83 | 83 | unset( $check_val['created_at'], $check_val['updated_at'], $check_val['is_draft'], $check_val['id'], $check_val['item_key'] ); |
| 84 | 84 | |
| 85 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 86 | 85 | if ( $new_values['item_key'] == $new_values['name'] ) { |
| 87 | 86 | unset( $check_val['name'] ); |
| 88 | 87 | } |
| 89 | 88 | |
| @@ -97,10 +96,10 @@ | ||
| 97 | 96 | } |
| 98 | 97 | |
| 99 | 98 | global $frm_vars; |
| 100 | 99 | $frm_vars['checking_duplicates'] = true; |
| 101 | - $is_duplicate = false; | |
| 102 | 100 | |
| 101 | + $is_duplicate = false; | |
| 103 | 102 | foreach ( $entry_exists as $entry_exist ) { |
| 104 | 103 | $is_duplicate = true; |
| 105 | 104 | |
| 106 | 105 | // make sure it's a duplicate |
| @@ -105,14 +104,12 @@ | ||
| 105 | 104 | |
| 106 | 105 | // make sure it's a duplicate |
| 107 | 106 | $metas = FrmEntryMeta::get_entry_meta_info( $entry_exist ); |
| 108 | 107 | $field_metas = array(); |
| 109 | - | |
| 110 | 108 | foreach ( $metas as $meta ) { |
| 111 | 109 | if ( 0 === (int) $meta->field_id ) { |
| 112 | 110 | continue; |
| 113 | 111 | } |
| 114 | - | |
| 115 | 112 | $field_metas[ $meta->field_id ] = $meta->meta_value; |
| 116 | 113 | } |
| 117 | 114 | |
| 118 | 115 | $filtered_vals = array_filter( $values['item_meta'] ); |
| @@ -119,9 +116,9 @@ | ||
| 119 | 116 | $filtered_vals = self::convert_values_to_their_saved_value( $filtered_vals, $entry_exist ); |
| 120 | 117 | $field_metas = array_filter( $field_metas ); |
| 121 | 118 | |
| 122 | 119 | // If prev entry is empty and current entry is not, they are not duplicates |
| 123 | - if ( ! $field_metas && $filtered_vals ) { | |
| 120 | + if ( empty( $field_metas ) && ! empty( $filtered_vals ) ) { | |
| 124 | 121 | return false; |
| 125 | 122 | } |
| 126 | 123 | |
| 127 | 124 | // compare serialized values and not arrays |
| @@ -138,9 +135,8 @@ | ||
| 138 | 135 | continue; |
| 139 | 136 | } |
| 140 | 137 | |
| 141 | 138 | $diff = array_diff_assoc( $field_metas, $new_meta ); |
| 142 | - | |
| 143 | 139 | foreach ( $diff as $meta_value ) { |
| 144 | 140 | if ( ! empty( $meta_value ) ) { |
| 145 | 141 | $is_duplicate = false; |
| 146 | 142 | } |
| @@ -160,9 +156,8 @@ | ||
| 160 | 156 | * @since 6.16.3 |
| 161 | 157 | * |
| 162 | 158 | * @param array $values POST request data. |
| 163 | 159 | * @param string $created_at The timestamp of the entry we are checking for. |
| 164 | - * | |
| 165 | 160 | * @return bool |
| 166 | 161 | */ |
| 167 | 162 | private static function maybe_check_for_unique_id_match( $values, $created_at ) { |
| 168 | 163 | if ( ! self::should_check_for_unique_id_match() ) { |
| @@ -173,9 +168,8 @@ | ||
| 173 | 168 | return false; |
| 174 | 169 | } |
| 175 | 170 | |
| 176 | 171 | $unique_id = sanitize_key( $values['unique_id'] ); |
| 177 | - | |
| 178 | 172 | if ( ! $unique_id ) { |
| 179 | 173 | // Only continue if a unique ID was generated on form submit. |
| 180 | 174 | return false; |
| 181 | 175 | } |
| @@ -184,9 +178,8 @@ | ||
| 184 | 178 | return self::$unique_id_match_checks[ $unique_id ]; |
| 185 | 179 | } |
| 186 | 180 | |
| 187 | 181 | $timestamp = strtotime( $created_at ); |
| 188 | - | |
| 189 | 182 | if ( false === $timestamp ) { |
| 190 | 183 | $timestamp = time(); |
| 191 | 184 | } |
| 192 | 185 | |
| @@ -204,10 +197,8 @@ | ||
| 204 | 197 | } |
| 205 | 198 | |
| 206 | 199 | /** |
| 207 | 200 | * @since 6.16.3 |
| 208 | - * | |
| 209 | - * @return bool | |
| 210 | 201 | */ |
| 211 | 202 | private static function should_check_for_unique_id_match() { |
| 212 | 203 | /** |
| 213 | 204 | * Allow users to opt out of the DB query, in case it causes performance issues. |
| @@ -221,30 +212,24 @@ | ||
| 221 | 212 | } |
| 222 | 213 | |
| 223 | 214 | /** |
| 224 | 215 | * Convert form data to the actual value that would be saved into the database. |
| 216 | + * 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. | |
| 225 | 217 | * |
| 226 | - * This is important for the duplicate check as something like 'a:2:{s:5:"typed";s:0:"";s:6:"output";s:0:"";}' | |
| 227 | - * (a signature value) is actually an empty string and does not get saved. | |
| 228 | - * | |
| 229 | 218 | * @param array $filter_vals |
| 230 | 219 | * @param int $entry_id |
| 231 | - * | |
| 232 | 220 | * @return array |
| 233 | 221 | */ |
| 234 | 222 | private static function convert_values_to_their_saved_value( $filter_vals, $entry_id ) { |
| 235 | 223 | $reduced = array(); |
| 236 | - | |
| 237 | 224 | foreach ( $filter_vals as $field_id => $value ) { |
| 238 | 225 | $field = FrmFieldFactory::get_field_object( $field_id ); |
| 239 | 226 | $reduced[ $field_id ] = $field->get_value_to_save( $value, array( 'entry_id' => $entry_id ) ); |
| 240 | 227 | $reduced[ $field_id ] = $field->set_value_before_save( $reduced[ $field_id ] ); |
| 241 | - | |
| 242 | 228 | if ( '' === $reduced[ $field_id ] || ( is_array( $reduced[ $field_id ] ) && 0 === count( $reduced[ $field_id ] ) ) ) { |
| 243 | 229 | unset( $reduced[ $field_id ] ); |
| 244 | 230 | } |
| 245 | 231 | } |
| 246 | - | |
| 247 | 232 | return $reduced; |
| 248 | 233 | } |
| 249 | 234 | |
| 250 | 235 | /** |
| @@ -258,9 +243,9 @@ | ||
| 258 | 243 | * @return bool |
| 259 | 244 | */ |
| 260 | 245 | private static function is_duplicate_check_needed( $values, $duplicate_entry_time ) { |
| 261 | 246 | // If time for checking duplicates is set to an empty value, don't check for duplicates |
| 262 | - if ( ! $duplicate_entry_time ) { | |
| 247 | + if ( empty( $duplicate_entry_time ) ) { | |
| 263 | 248 | return false; |
| 264 | 249 | } |
| 265 | 250 | |
| 266 | 251 | // If CSV is importing, don't check for duplicates |
| @@ -268,20 +253,20 @@ | ||
| 268 | 253 | return false; |
| 269 | 254 | } |
| 270 | 255 | |
| 271 | 256 | // If repeating field entries are getting created, don't check for duplicates |
| 272 | - return empty( $values['parent_form_id'] ); | |
| 257 | + if ( isset( $values['parent_form_id'] ) && $values['parent_form_id'] ) { | |
| 258 | + return false; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return true; | |
| 273 | 262 | } |
| 274 | 263 | |
| 275 | - /** | |
| 276 | - * @param int|string $id | |
| 277 | - * | |
| 278 | - * @return false|int | |
| 279 | - */ | |
| 280 | 264 | public static function duplicate( $id ) { |
| 281 | 265 | global $wpdb; |
| 282 | 266 | |
| 283 | - $values = self::getOne( $id ); | |
| 267 | + $values = self::getOne( $id ); | |
| 268 | + | |
| 284 | 269 | $new_values = array(); |
| 285 | 270 | $new_values['item_key'] = FrmAppHelper::get_unique_key( '', $wpdb->prefix . 'frm_items', 'item_key' ); |
| 286 | 271 | $new_values['name'] = $values->name; |
| 287 | 272 | $new_values['is_draft'] = $values->is_draft; |
| @@ -291,9 +276,8 @@ | ||
| 291 | 276 | $new_values['created_at'] = current_time( 'mysql', 1 ); |
| 292 | 277 | $new_values['updated_at'] = $new_values['created_at']; |
| 293 | 278 | |
| 294 | 279 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values ); |
| 295 | - | |
| 296 | 280 | if ( ! $query_results ) { |
| 297 | 281 | return false; |
| 298 | 282 | } |
| 299 | 283 | |
| @@ -299,9 +283,8 @@ | ||
| 299 | 283 | |
| 300 | 284 | $entry_id = $wpdb->insert_id; |
| 301 | 285 | |
| 302 | 286 | global $frm_vars; |
| 303 | - | |
| 304 | 287 | if ( ! isset( $frm_vars['saved_entries'] ) ) { |
| 305 | 288 | $frm_vars['saved_entries'] = array(); |
| 306 | 289 | } |
| 307 | 290 | $frm_vars['saved_entries'][] = (int) $entry_id; |
| @@ -322,9 +305,11 @@ | ||
| 322 | 305 | * |
| 323 | 306 | * @return bool|int $update_results |
| 324 | 307 | */ |
| 325 | 308 | public static function update( $id, $values ) { |
| 326 | - return self::update_entry( $id, $values, 'standard' ); | |
| 309 | + $update_results = self::update_entry( $id, $values, 'standard' ); | |
| 310 | + | |
| 311 | + return $update_results; | |
| 327 | 312 | } |
| 328 | 313 | |
| 329 | 314 | /** |
| 330 | 315 | * Update an entry with some differences depending on the update type |
| @@ -330,11 +315,10 @@ | ||
| 330 | 315 | * Update an entry with some differences depending on the update type |
| 331 | 316 | * |
| 332 | 317 | * @since 2.0.16 |
| 333 | 318 | * |
| 334 | - * @param int $id | |
| 335 | - * @param array $values | |
| 336 | - * @param string $update_type | |
| 319 | + * @param int $id | |
| 320 | + * @param array $values | |
| 337 | 321 | * |
| 338 | 322 | * @return bool|int $query_results |
| 339 | 323 | */ |
| 340 | 324 | private static function update_entry( $id, $values, $update_type ) { |
| @@ -340,14 +324,14 @@ | ||
| 340 | 324 | private static function update_entry( $id, $values, $update_type ) { |
| 341 | 325 | global $wpdb; |
| 342 | 326 | |
| 343 | 327 | $update = self::before_update_entry( $id, $values, $update_type ); |
| 344 | - | |
| 345 | 328 | if ( ! $update ) { |
| 346 | 329 | return false; |
| 347 | 330 | } |
| 348 | 331 | |
| 349 | - $new_values = self::package_entry_to_update( $id, $values ); | |
| 332 | + $new_values = self::package_entry_to_update( $id, $values ); | |
| 333 | + | |
| 350 | 334 | $query_results = $wpdb->update( $wpdb->prefix . 'frm_items', $new_values, compact( 'id' ) ); |
| 351 | 335 | |
| 352 | 336 | self::after_update_entry( $query_results, $id, $values, $new_values ); |
| 353 | 337 | |
| @@ -357,9 +341,8 @@ | ||
| 357 | 341 | /** |
| 358 | 342 | * Delete an entry. |
| 359 | 343 | * |
| 360 | 344 | * @param int|string $id |
| 361 | - * | |
| 362 | 345 | * @return bool True on success, false if nothing was deleted. |
| 363 | 346 | */ |
| 364 | 347 | public static function destroy( $id ) { |
| 365 | 348 | global $wpdb; |
| @@ -366,11 +349,11 @@ | ||
| 366 | 349 | $id = (int) $id; |
| 367 | 350 | |
| 368 | 351 | // Item meta is required for conditional logic in actions with 'delete' events. |
| 369 | 352 | $entry = self::getOne( $id, true ); |
| 370 | - | |
| 371 | 353 | if ( ! $entry ) { |
| 372 | - return false; | |
| 354 | + $result = false; | |
| 355 | + return $result; | |
| 373 | 356 | } |
| 374 | 357 | |
| 375 | 358 | /** |
| 376 | 359 | * Trigger an action to run custom logic before the entry is deleted. |
| @@ -398,20 +381,12 @@ | ||
| 398 | 381 | |
| 399 | 382 | return $result; |
| 400 | 383 | } |
| 401 | 384 | |
| 402 | - /** | |
| 403 | - * @param int $id | |
| 404 | - * @param mixed $value | |
| 405 | - * @param int|string $form_id | |
| 406 | - * | |
| 407 | - * @return false|int | |
| 408 | - */ | |
| 409 | 385 | public static function update_form( $id, $value, $form_id ) { |
| 410 | 386 | global $wpdb; |
| 411 | 387 | $form_id = isset( $value ) ? $form_id : null; |
| 412 | 388 | $result = $wpdb->update( $wpdb->prefix . 'frm_items', array( 'form_id' => $form_id ), array( 'id' => $id ) ); |
| 413 | - | |
| 414 | 389 | if ( $result ) { |
| 415 | 390 | self::clear_cache(); |
| 416 | 391 | } |
| 417 | 392 | |
| @@ -422,10 +397,8 @@ | ||
| 422 | 397 | * Clear entry caching |
| 423 | 398 | * Called when an entry is changed |
| 424 | 399 | * |
| 425 | 400 | * @since 2.0.5 |
| 426 | - * | |
| 427 | - * @return void | |
| 428 | 401 | */ |
| 429 | 402 | public static function clear_cache() { |
| 430 | 403 | FrmDb::cache_delete_group( 'frm_entry' ); |
| 431 | 404 | FrmDb::cache_delete_group( 'frm_item' ); |
| @@ -437,17 +410,11 @@ | ||
| 437 | 410 | * After switching to the wp_loaded hook for processing entries, |
| 438 | 411 | * we can no longer use 'name', but check it as a fallback |
| 439 | 412 | * |
| 440 | 413 | * @since 2.0.11 |
| 441 | - * | |
| 442 | - * @param array $values | |
| 443 | - * @param array|string $default | |
| 444 | - * | |
| 445 | - * @return string | |
| 446 | 414 | */ |
| 447 | 415 | public static function get_new_entry_name( $values, $default = '' ) { |
| 448 | - $name = $values['item_name'] ?? $values['name'] ?? $default; | |
| 449 | - | |
| 416 | + $name = isset( $values['item_name'] ) ? $values['item_name'] : ( isset( $values['name'] ) ? $values['name'] : $default ); | |
| 450 | 417 | if ( is_array( $name ) ) { |
| 451 | 418 | $name = reset( $name ); |
| 452 | 419 | } |
| 453 | 420 | |
| @@ -459,25 +426,18 @@ | ||
| 459 | 426 | * |
| 460 | 427 | * @since 2.0.9 |
| 461 | 428 | * |
| 462 | 429 | * @param int|object $entry By reference. |
| 463 | - * | |
| 464 | 430 | * @return void |
| 465 | 431 | */ |
| 466 | 432 | public static function maybe_get_entry( &$entry ) { |
| 467 | 433 | if ( $entry && is_numeric( $entry ) ) { |
| 468 | 434 | $entry = self::getOne( $entry ); |
| 469 | - } elseif ( ! $entry || 'false' === $entry ) { | |
| 435 | + } elseif ( empty( $entry ) || 'false' === $entry ) { | |
| 470 | 436 | $entry = false; |
| 471 | 437 | } |
| 472 | 438 | } |
| 473 | 439 | |
| 474 | - /** | |
| 475 | - * @param int|string $id | |
| 476 | - * @param bool $meta | |
| 477 | - * | |
| 478 | - * @return object|null | |
| 479 | - */ | |
| 480 | 440 | public static function getOne( $id, $meta = false ) { |
| 481 | 441 | global $wpdb; |
| 482 | 442 | |
| 483 | 443 | $query = "SELECT it.*, fr.name as form_name, fr.form_key as form_key FROM {$wpdb->prefix}frm_items it |
| @@ -493,9 +453,8 @@ | ||
| 493 | 453 | return $entry; |
| 494 | 454 | } |
| 495 | 455 | |
| 496 | 456 | $entry = FrmDb::check_cache( $id, 'frm_entry' ); |
| 497 | - | |
| 498 | 457 | if ( $entry !== false ) { |
| 499 | 458 | self::prepare_entry( $entry ); |
| 500 | 459 | return $entry; |
| 501 | 460 | } |
| @@ -510,13 +469,11 @@ | ||
| 510 | 469 | /** |
| 511 | 470 | * @since 4.02.03 |
| 512 | 471 | * |
| 513 | 472 | * @param object $entry |
| 514 | - * | |
| 515 | - * @return void | |
| 516 | 473 | */ |
| 517 | 474 | private static function prepare_entry( &$entry ) { |
| 518 | - if ( ! $entry ) { | |
| 475 | + if ( empty( $entry ) ) { | |
| 519 | 476 | return; |
| 520 | 477 | } |
| 521 | 478 | |
| 522 | 479 | FrmAppHelper::unserialize_or_decode( $entry->description ); |
| @@ -527,10 +484,8 @@ | ||
| 527 | 484 | /** |
| 528 | 485 | * @since 4.02.03 |
| 529 | 486 | * |
| 530 | 487 | * @param array $entries |
| 531 | - * | |
| 532 | - * @return void | |
| 533 | 488 | */ |
| 534 | 489 | private static function prepare_entries( &$entries ) { |
| 535 | 490 | foreach ( $entries as $k => $entry ) { |
| 536 | 491 | self::prepare_entry( $entry ); |
| @@ -537,13 +492,8 @@ | ||
| 537 | 492 | $entries[ $k ] = $entry; |
| 538 | 493 | } |
| 539 | 494 | } |
| 540 | 495 | |
| 541 | - /** | |
| 542 | - * @param object|null $entry | |
| 543 | - * | |
| 544 | - * @return object|null | |
| 545 | - */ | |
| 546 | 496 | public static function get_meta( $entry ) { |
| 547 | 497 | if ( ! $entry ) { |
| 548 | 498 | return $entry; |
| 549 | 499 | } |
| @@ -560,15 +510,13 @@ | ||
| 560 | 510 | |
| 561 | 511 | $entry->metas = array(); |
| 562 | 512 | |
| 563 | 513 | $include_key = apply_filters( 'frm_include_meta_keys', false, array( 'form_id' => $entry->form_id ) ); |
| 564 | - | |
| 565 | 514 | foreach ( $metas as $meta_val ) { |
| 566 | 515 | FrmFieldsHelper::prepare_field_value( $meta_val->meta_value, $meta_val->type ); |
| 567 | 516 | |
| 568 | - if ( (int) $meta_val->item_id === (int) $entry->id ) { | |
| 517 | + if ( $meta_val->item_id == $entry->id ) { | |
| 569 | 518 | $entry->metas[ $meta_val->field_id ] = $meta_val->meta_value; |
| 570 | - | |
| 571 | 519 | if ( $include_key ) { |
| 572 | 520 | $entry->metas[ $meta_val->field_key ] = $entry->metas[ $meta_val->field_id ]; |
| 573 | 521 | } |
| 574 | 522 | continue; |
| @@ -581,9 +529,9 @@ | ||
| 581 | 529 | |
| 582 | 530 | $entry->metas[ $meta_val->field_id ][] = $meta_val->meta_value; |
| 583 | 531 | |
| 584 | 532 | unset( $meta_val ); |
| 585 | - }//end foreach | |
| 533 | + } | |
| 586 | 534 | unset( $metas ); |
| 587 | 535 | |
| 588 | 536 | FrmDb::set_cache( $entry->id, $entry, 'frm_entry' ); |
| 589 | 537 | |
| @@ -591,42 +539,38 @@ | ||
| 591 | 539 | } |
| 592 | 540 | |
| 593 | 541 | /** |
| 594 | 542 | * @param string $id |
| 595 | - * | |
| 596 | - * @return bool | |
| 597 | 543 | */ |
| 598 | 544 | public static function exists( $id ) { |
| 599 | 545 | global $wpdb; |
| 600 | 546 | |
| 601 | 547 | if ( FrmDb::check_cache( $id, 'frm_entry' ) ) { |
| 602 | - return true; | |
| 548 | + $exists = true; | |
| 549 | + | |
| 550 | + return $exists; | |
| 603 | 551 | } |
| 604 | 552 | |
| 605 | - $where = is_numeric( $id ) ? array( 'id' => $id ) : array( 'item_key' => $id ); | |
| 606 | - $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where ); | |
| 553 | + if ( is_numeric( $id ) ) { | |
| 554 | + $where = array( 'id' => $id ); | |
| 555 | + } else { | |
| 556 | + $where = array( 'item_key' => $id ); | |
| 557 | + } | |
| 558 | + $id = FrmDb::get_var( $wpdb->prefix . 'frm_items', $where ); | |
| 607 | 559 | |
| 608 | 560 | return $id && $id > 0; |
| 609 | 561 | } |
| 610 | 562 | |
| 611 | - /** | |
| 612 | - * @param array|string $where | |
| 613 | - * @param string $order_by | |
| 614 | - * @param string $limit | |
| 615 | - * @param bool $meta | |
| 616 | - * @param bool $inc_form | |
| 617 | - * | |
| 618 | - * @return array | |
| 619 | - */ | |
| 620 | 563 | public static function getAll( $where, $order_by = '', $limit = '', $meta = false, $inc_form = true ) { |
| 621 | 564 | global $wpdb; |
| 622 | 565 | |
| 623 | - $limit = FrmDb::esc_limit( $limit ); | |
| 566 | + $limit = FrmDb::esc_limit( $limit ); | |
| 567 | + | |
| 624 | 568 | $cache_key = FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit . $inc_form; |
| 625 | 569 | $entries = wp_cache_get( $cache_key, 'frm_entry' ); |
| 626 | 570 | |
| 627 | 571 | if ( false === $entries ) { |
| 628 | - $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'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 572 | + $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'; | |
| 629 | 573 | $table = $wpdb->prefix . 'frm_items it '; |
| 630 | 574 | |
| 631 | 575 | if ( $inc_form ) { |
| 632 | 576 | $fields = 'it.*, fr.name as form_name,fr.form_key as form_key'; |
| @@ -657,11 +601,9 @@ | ||
| 657 | 601 | $where = array( 'it.form_id' => substr( $where, 11 ) ); |
| 658 | 602 | } |
| 659 | 603 | |
| 660 | 604 | $meta_where = array( 'field_id !' => 0 ); |
| 661 | - | |
| 662 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 663 | - if ( $limit == '' && is_array( $where ) && count( $where ) === 1 && isset( $where['it.form_id'] ) ) { | |
| 605 | + if ( $limit == '' && is_array( $where ) && count( $where ) == 1 && isset( $where['it.form_id'] ) ) { | |
| 664 | 606 | $meta_where['fi.form_id'] = $where['it.form_id']; |
| 665 | 607 | } else { |
| 666 | 608 | $meta_where['item_id'] = array_keys( $entries ); |
| 667 | 609 | } |
| @@ -704,15 +646,15 @@ | ||
| 704 | 646 | return $entries; |
| 705 | 647 | } |
| 706 | 648 | |
| 707 | 649 | /** |
| 708 | - * @param int|string $field_id | |
| 709 | - * | |
| 650 | + * @param int $field_id | |
| 710 | 651 | * @return string |
| 711 | 652 | */ |
| 712 | 653 | private static function sort_by_field( $field_id ) { |
| 713 | 654 | global $wpdb; |
| 714 | - $field_id = (int) $field_id; | |
| 655 | + $field_id = (int) $field_id; | |
| 656 | + | |
| 715 | 657 | $field_options = FrmDb::get_var( 'frm_fields', array( 'id' => $field_id ), 'field_options' ); |
| 716 | 658 | FrmAppHelper::unserialize_or_decode( $field_options ); |
| 717 | 659 | |
| 718 | 660 | if ( empty( $field_options['post_field'] ) ) { |
| @@ -726,9 +668,8 @@ | ||
| 726 | 668 | |
| 727 | 669 | // Pagination Methods |
| 728 | 670 | /** |
| 729 | 671 | * @param array|int|string $where If int, use the form id. |
| 730 | - * | |
| 731 | 672 | * @return int|string |
| 732 | 673 | */ |
| 733 | 674 | public static function getRecordCount( $where = '' ) { |
| 734 | 675 | global $wpdb; |
| @@ -739,32 +680,29 @@ | ||
| 739 | 680 | $where = array( 'form_id' => $where ); |
| 740 | 681 | } |
| 741 | 682 | |
| 742 | 683 | if ( is_array( $where ) ) { |
| 743 | - return FrmDb::get_count( $table_join, $where ); | |
| 684 | + $count = FrmDb::get_count( $table_join, $where ); | |
| 685 | + } else { | |
| 686 | + $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where ); | |
| 687 | + $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 688 | + $count = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' ); | |
| 744 | 689 | } |
| 745 | 690 | |
| 746 | - $cache_key = 'count_' . FrmAppHelper::maybe_json_encode( $where ); | |
| 747 | - $query = 'SELECT COUNT(*) FROM ' . $table_join . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 748 | - | |
| 749 | - return FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_var' ); | |
| 691 | + return $count; | |
| 750 | 692 | } |
| 751 | 693 | |
| 752 | 694 | /** |
| 753 | - * @param int|string $p_size | |
| 754 | - * @param array|int|string $where | |
| 755 | - * | |
| 695 | + * @param int|string $p_size | |
| 756 | 696 | * @return int |
| 757 | 697 | */ |
| 758 | 698 | public static function getPageCount( $p_size, $where = '' ) { |
| 759 | 699 | $p_size = (int) $p_size; |
| 760 | 700 | $count = 1; |
| 761 | - | |
| 762 | 701 | if ( $p_size ) { |
| 763 | 702 | if ( ! is_numeric( $where ) ) { |
| 764 | 703 | $where = self::getRecordCount( $where ); |
| 765 | 704 | } |
| 766 | - | |
| 767 | 705 | $count = ceil( (int) $where / $p_size ); |
| 768 | 706 | } |
| 769 | 707 | |
| 770 | 708 | return $count; |
| @@ -780,8 +718,9 @@ | ||
| 780 | 718 | * |
| 781 | 719 | * @return array $new_values |
| 782 | 720 | */ |
| 783 | 721 | private static function before_insert_entry_in_database( &$values, $type ) { |
| 722 | + | |
| 784 | 723 | self::sanitize_entry_post( $values ); |
| 785 | 724 | |
| 786 | 725 | if ( $type !== 'xml' ) { |
| 787 | 726 | $values = apply_filters( 'frm_pre_create_entry', $values ); |
| @@ -786,9 +725,11 @@ | ||
| 786 | 725 | if ( $type !== 'xml' ) { |
| 787 | 726 | $values = apply_filters( 'frm_pre_create_entry', $values ); |
| 788 | 727 | } |
| 789 | 728 | |
| 790 | - return self::package_entry_data( $values ); | |
| 729 | + $new_values = self::package_entry_data( $values ); | |
| 730 | + | |
| 731 | + return $new_values; | |
| 791 | 732 | } |
| 792 | 733 | |
| 793 | 734 | /** |
| 794 | 735 | * Create an entry and perform after create actions |
| @@ -801,9 +742,8 @@ | ||
| 801 | 742 | * @return bool|int $entry_id |
| 802 | 743 | */ |
| 803 | 744 | private static function continue_to_create_entry( $values, $new_values ) { |
| 804 | 745 | $entry_id = self::insert_entry_into_database( $new_values ); |
| 805 | - | |
| 806 | 746 | if ( ! $entry_id ) { |
| 807 | 747 | return false; |
| 808 | 748 | } |
| 809 | 749 | |
| @@ -817,10 +757,8 @@ | ||
| 817 | 757 | * |
| 818 | 758 | * @since 2.0 |
| 819 | 759 | * |
| 820 | 760 | * @param array $values The POST values by reference. |
| 821 | - * | |
| 822 | - * @return void | |
| 823 | 761 | */ |
| 824 | 762 | public static function sanitize_entry_post( &$values ) { |
| 825 | 763 | $sanitize_method = array( |
| 826 | 764 | 'form_id' => 'absint', |
| @@ -869,22 +807,15 @@ | ||
| 869 | 807 | 'description' => self::get_entry_description( $values ), |
| 870 | 808 | 'user_id' => self::get_entry_user_id( $values ), |
| 871 | 809 | ); |
| 872 | 810 | |
| 873 | - $new_values['updated_by'] = $values['updated_by'] ?? $new_values['user_id']; | |
| 811 | + $new_values['updated_by'] = isset( $values['updated_by'] ) ? $values['updated_by'] : $new_values['user_id']; | |
| 874 | 812 | |
| 875 | 813 | return $new_values; |
| 876 | 814 | } |
| 877 | 815 | |
| 878 | - /** | |
| 879 | - * @param array $values | |
| 880 | - * @param string $name | |
| 881 | - * @param mixed $default | |
| 882 | - * | |
| 883 | - * @return mixed | |
| 884 | - */ | |
| 885 | 816 | private static function get_entry_value( $values, $name, $default ) { |
| 886 | - return $values[ $name ] ?? $default; | |
| 817 | + return isset( $values[ $name ] ) ? $values[ $name ] : $default; | |
| 887 | 818 | } |
| 888 | 819 | |
| 889 | 820 | /** |
| 890 | 821 | * Get the ip for a new entry. |
| @@ -901,9 +832,8 @@ | ||
| 901 | 832 | return ''; |
| 902 | 833 | } |
| 903 | 834 | |
| 904 | 835 | $ip = FrmAppHelper::get_ip_address(); |
| 905 | - | |
| 906 | 836 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 907 | 837 | $ip = self::get_entry_value( $values, 'ip', $ip ); |
| 908 | 838 | } |
| 909 | 839 | |
| @@ -949,9 +879,15 @@ | ||
| 949 | 879 | * |
| 950 | 880 | * @return string |
| 951 | 881 | */ |
| 952 | 882 | private static function get_updated_at( $values ) { |
| 953 | - return $values['updated_at'] ?? self::get_created_at( $values ); | |
| 883 | + if ( isset( $values['updated_at'] ) ) { | |
| 884 | + $updated_at = $values['updated_at']; | |
| 885 | + } else { | |
| 886 | + $updated_at = self::get_created_at( $values ); | |
| 887 | + } | |
| 888 | + | |
| 889 | + return $updated_at; | |
| 954 | 890 | } |
| 955 | 891 | |
| 956 | 892 | /** |
| 957 | 893 | * Get the description value for a new entry |
| @@ -963,17 +899,19 @@ | ||
| 963 | 899 | * @return string |
| 964 | 900 | */ |
| 965 | 901 | private static function get_entry_description( $values ) { |
| 966 | 902 | if ( ! empty( $values['description'] ) ) { |
| 967 | - return FrmAppHelper::maybe_json_encode( $values['description'] ); | |
| 903 | + $description = FrmAppHelper::maybe_json_encode( $values['description'] ); | |
| 904 | + } else { | |
| 905 | + $description = json_encode( | |
| 906 | + array( | |
| 907 | + 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ), | |
| 908 | + 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ), | |
| 909 | + ) | |
| 910 | + ); | |
| 968 | 911 | } |
| 969 | 912 | |
| 970 | - return json_encode( | |
| 971 | - array( | |
| 972 | - 'browser' => FrmAppHelper::get_server_value( 'HTTP_USER_AGENT' ), | |
| 973 | - 'referrer' => FrmAppHelper::get_server_value( 'HTTP_REFERER' ), | |
| 974 | - ) | |
| 975 | - ); | |
| 913 | + return $description; | |
| 976 | 914 | } |
| 977 | 915 | |
| 978 | 916 | /** |
| 979 | 917 | * Get the user_id value for a new entry |
| @@ -985,13 +923,15 @@ | ||
| 985 | 923 | * @return int |
| 986 | 924 | */ |
| 987 | 925 | private static function get_entry_user_id( $values ) { |
| 988 | 926 | if ( isset( $values['frm_user_id'] ) && ( is_numeric( $values['frm_user_id'] ) || FrmAppHelper::is_admin() ) ) { |
| 989 | - return $values['frm_user_id']; | |
| 927 | + $user_id = $values['frm_user_id']; | |
| 928 | + } else { | |
| 929 | + $current_user_id = get_current_user_id(); | |
| 930 | + $user_id = $current_user_id ? $current_user_id : 0; | |
| 990 | 931 | } |
| 991 | 932 | |
| 992 | - $current_user_id = get_current_user_id(); | |
| 993 | - return $current_user_id ? $current_user_id : 0; | |
| 933 | + return $user_id; | |
| 994 | 934 | } |
| 995 | 935 | |
| 996 | 936 | /** |
| 997 | 937 | * Insert new entry into the database |
| @@ -1006,9 +946,15 @@ | ||
| 1006 | 946 | global $wpdb; |
| 1007 | 947 | |
| 1008 | 948 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_items', $new_values ); |
| 1009 | 949 | |
| 1010 | - return ! $query_results ? false : $wpdb->insert_id; | |
| 950 | + if ( ! $query_results ) { | |
| 951 | + $entry_id = false; | |
| 952 | + } else { | |
| 953 | + $entry_id = $wpdb->insert_id; | |
| 954 | + } | |
| 955 | + | |
| 956 | + return $entry_id; | |
| 1011 | 957 | } |
| 1012 | 958 | |
| 1013 | 959 | /** |
| 1014 | 960 | * Add the new entry to global $frm_vars |
| @@ -1014,11 +960,9 @@ | ||
| 1014 | 960 | * Add the new entry to global $frm_vars |
| 1015 | 961 | * |
| 1016 | 962 | * @since 2.0.16 |
| 1017 | 963 | * |
| 1018 | - * @param int|string $entry_id | |
| 1019 | - * | |
| 1020 | - * @return void | |
| 964 | + * @param int $entry_id | |
| 1021 | 965 | */ |
| 1022 | 966 | private static function add_new_entry_to_frm_vars( $entry_id ) { |
| 1023 | 967 | global $frm_vars; |
| 1024 | 968 | |
| @@ -1033,11 +977,10 @@ | ||
| 1033 | 977 | * Add entry metas, if there are any |
| 1034 | 978 | * |
| 1035 | 979 | * @since 2.0.16 |
| 1036 | 980 | * |
| 1037 | - * @param array $values | |
| 1038 | - * @param int|string $entry_id | |
| 1039 | - * | |
| 981 | + * @param array $values | |
| 982 | + * @param int $entry_id | |
| 1040 | 983 | * @return void |
| 1041 | 984 | */ |
| 1042 | 985 | private static function maybe_add_entry_metas( $values, $entry_id ) { |
| 1043 | 986 | if ( isset( $values['item_meta'] ) ) { |
| @@ -1051,9 +994,8 @@ | ||
| 1051 | 994 | * @since 6.16.3 |
| 1052 | 995 | * |
| 1053 | 996 | * @param array $values |
| 1054 | 997 | * @param int $entry_id |
| 1055 | - * | |
| 1056 | 998 | * @return void |
| 1057 | 999 | */ |
| 1058 | 1000 | private static function maybe_add_unique_id_meta( $values, $entry_id ) { |
| 1059 | 1001 | if ( ! empty( $values['parent_form_id'] ) || empty( $values['unique_id'] ) || ! self::should_check_for_unique_id_match() ) { |
| @@ -1062,9 +1004,8 @@ | ||
| 1062 | 1004 | |
| 1063 | 1005 | // This unique ID is inserted with JS on form submit. |
| 1064 | 1006 | // It is used to check for duplicate entries. |
| 1065 | 1007 | $unique_id = sanitize_key( $values['unique_id'] ); |
| 1066 | - | |
| 1067 | 1008 | if ( $unique_id ) { |
| 1068 | 1009 | FrmEntryMeta::add_entry_meta( $entry_id, 0, '', compact( 'unique_id' ) ); |
| 1069 | 1010 | self::flag_new_unique_key( $unique_id ); |
| 1070 | 1011 | } |
| @@ -1074,14 +1015,12 @@ | ||
| 1074 | 1015 | * @since 5.0.15 |
| 1075 | 1016 | * |
| 1076 | 1017 | * @param int $form_id |
| 1077 | 1018 | * @param int $entry_id |
| 1078 | - * | |
| 1079 | 1019 | * @return void |
| 1080 | 1020 | */ |
| 1081 | 1021 | private static function maybe_add_captcha_meta( $form_id, $entry_id ) { |
| 1082 | 1022 | global $frm_vars; |
| 1083 | - | |
| 1084 | 1023 | if ( array_key_exists( 'captcha_scores', $frm_vars ) && array_key_exists( $form_id, $frm_vars['captcha_scores'] ) ) { |
| 1085 | 1024 | $captcha_score_meta = array( 'captcha_score' => $frm_vars['captcha_scores'][ $form_id ] ); |
| 1086 | 1025 | FrmEntryMeta::add_entry_meta( $entry_id, 0, '', maybe_serialize( $captcha_score_meta ) ); |
| 1087 | 1026 | } |
| @@ -1094,10 +1033,8 @@ | ||
| 1094 | 1033 | * |
| 1095 | 1034 | * @param int $entry_id |
| 1096 | 1035 | * @param array $values |
| 1097 | 1036 | * @param array $new_values |
| 1098 | - * | |
| 1099 | - * @return void | |
| 1100 | 1037 | */ |
| 1101 | 1038 | private static function after_entry_created_actions( $entry_id, $values, $new_values ) { |
| 1102 | 1039 | // This is a child entry. |
| 1103 | 1040 | $is_child = isset( $values['parent_nonce'] ) && ! empty( $values['parent_form_id'] ) && wp_verify_nonce( $values['parent_nonce'], 'parent' ); |
| @@ -1113,12 +1050,11 @@ | ||
| 1113 | 1050 | * |
| 1114 | 1051 | * @param array $values |
| 1115 | 1052 | * @param array $new_values |
| 1116 | 1053 | * @param int $entry_id |
| 1117 | - * | |
| 1118 | - * @return void | |
| 1119 | 1054 | */ |
| 1120 | 1055 | private static function after_insert_entry_in_database( $values, $new_values, $entry_id ) { |
| 1056 | + | |
| 1121 | 1057 | self::add_new_entry_to_frm_vars( $entry_id ); |
| 1122 | 1058 | |
| 1123 | 1059 | self::maybe_add_entry_metas( $values, $entry_id ); |
| 1124 | 1060 | |
| @@ -1131,11 +1067,11 @@ | ||
| 1131 | 1067 | * Perform some actions right before updating an entry |
| 1132 | 1068 | * |
| 1133 | 1069 | * @since 2.0.16 |
| 1134 | 1070 | * |
| 1135 | - * @param int|string $id | |
| 1136 | - * @param array $values | |
| 1137 | - * @param string $update_type | |
| 1071 | + * @param int $id | |
| 1072 | + * @param array $values | |
| 1073 | + * @param string $update_type | |
| 1138 | 1074 | * |
| 1139 | 1075 | * @return bool $update |
| 1140 | 1076 | */ |
| 1141 | 1077 | private static function before_update_entry( $id, &$values, $update_type ) { |
| @@ -1142,9 +1078,8 @@ | ||
| 1142 | 1078 | $update = true; |
| 1143 | 1079 | |
| 1144 | 1080 | global $frm_vars; |
| 1145 | 1081 | |
| 1146 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 1147 | 1082 | if ( isset( $frm_vars['saved_entries'] ) && is_array( $frm_vars['saved_entries'] ) && in_array( (int) $id, $frm_vars['saved_entries'] ) ) { |
| 1148 | 1083 | $update = false; |
| 1149 | 1084 | } |
| 1150 | 1085 | |
| @@ -1172,9 +1107,9 @@ | ||
| 1172 | 1107 | 'name' => self::get_new_entry_name( $values ), |
| 1173 | 1108 | 'form_id' => (int) self::get_entry_value( $values, 'form_id', null ), |
| 1174 | 1109 | 'is_draft' => self::get_is_draft_value( $values ), |
| 1175 | 1110 | 'updated_at' => current_time( 'mysql', 1 ), |
| 1176 | - 'updated_by' => $values['updated_by'] ?? get_current_user_id(), | |
| 1111 | + 'updated_by' => isset( $values['updated_by'] ) ? $values['updated_by'] : get_current_user_id(), | |
| 1177 | 1112 | ); |
| 1178 | 1113 | |
| 1179 | 1114 | if ( isset( $values['post_id'] ) ) { |
| 1180 | 1115 | $new_values['post_id'] = (int) $values['post_id']; |
| @@ -1191,9 +1126,11 @@ | ||
| 1191 | 1126 | if ( isset( $values['frm_user_id'] ) && is_numeric( $values['frm_user_id'] ) ) { |
| 1192 | 1127 | $new_values['user_id'] = $values['frm_user_id']; |
| 1193 | 1128 | } |
| 1194 | 1129 | |
| 1195 | - return apply_filters( 'frm_update_entry', $new_values, $id ); | |
| 1130 | + $new_values = apply_filters( 'frm_update_entry', $new_values, $id ); | |
| 1131 | + | |
| 1132 | + return $new_values; | |
| 1196 | 1133 | } |
| 1197 | 1134 | |
| 1198 | 1135 | /** |
| 1199 | 1136 | * Perform some actions right after updating an entry |
| @@ -1199,14 +1136,12 @@ | ||
| 1199 | 1136 | * Perform some actions right after updating an entry |
| 1200 | 1137 | * |
| 1201 | 1138 | * @since 2.0.16 |
| 1202 | 1139 | * |
| 1203 | - * @param bool|int $query_results | |
| 1204 | - * @param int|string $id | |
| 1205 | - * @param array $values | |
| 1206 | - * @param array $new_values | |
| 1207 | - * | |
| 1208 | - * @return void | |
| 1140 | + * @param bool|int $query_results | |
| 1141 | + * @param int $id | |
| 1142 | + * @param array $values | |
| 1143 | + * @param array $new_values | |
| 1209 | 1144 | */ |
| 1210 | 1145 | private static function after_update_entry( $query_results, $id, $values, $new_values ) { |
| 1211 | 1146 | if ( $query_results ) { |
| 1212 | 1147 | self::clear_cache(); |
| @@ -1212,9 +1147,8 @@ | ||
| 1212 | 1147 | self::clear_cache(); |
| 1213 | 1148 | } |
| 1214 | 1149 | |
| 1215 | 1150 | global $frm_vars; |
| 1216 | - | |
| 1217 | 1151 | if ( ! isset( $frm_vars['saved_entries'] ) ) { |
| 1218 | 1152 | $frm_vars['saved_entries'] = array(); |
| 1219 | 1153 | } |
| 1220 | 1154 | |
| @@ -1238,9 +1172,11 @@ | ||
| 1238 | 1172 | * |
| 1239 | 1173 | * @return bool|int $entry_id |
| 1240 | 1174 | */ |
| 1241 | 1175 | public static function create_entry_from_xml( $values ) { |
| 1242 | - return self::create_entry( $values, 'xml' ); | |
| 1176 | + $entry_id = self::create_entry( $values, 'xml' ); | |
| 1177 | + | |
| 1178 | + return $entry_id; | |
| 1243 | 1179 | } |
| 1244 | 1180 | |
| 1245 | 1181 | /** |
| 1246 | 1182 | * Update entry from an XML import |
| @@ -1253,9 +1189,11 @@ | ||
| 1253 | 1189 | * |
| 1254 | 1190 | * @return bool|int $updated |
| 1255 | 1191 | */ |
| 1256 | 1192 | public static function update_entry_from_xml( $id, $values ) { |
| 1257 | - return self::update_entry( $id, $values, 'xml' ); | |
| 1193 | + $updated = self::update_entry( $id, $values, 'xml' ); | |
| 1194 | + | |
| 1195 | + return $updated; | |
| 1258 | 1196 | } |
| 1259 | 1197 | |
| 1260 | 1198 | /** |
| 1261 | 1199 | * @param string $key |
| @@ -1263,8 +1201,9 @@ | ||
| 1263 | 1201 | * @return int entry_id |
| 1264 | 1202 | */ |
| 1265 | 1203 | public static function get_id_by_key( $key ) { |
| 1266 | 1204 | $entry_id = FrmDb::get_var( 'frm_items', array( 'item_key' => sanitize_title( $key ) ) ); |
| 1205 | + | |
| 1267 | 1206 | return (int) $entry_id; |
| 1268 | 1207 | } |
| 1269 | 1208 | |
| 1270 | 1209 | /** |