| @@ -5,20 +5,24 @@ | ||
| 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 | |
| 9 | + * @since 6.35 Added the $field parameter. | |
| 13 | 10 | * |
| 11 | + * @param int $entry_id | |
| 12 | + * @param int $field_id | |
| 13 | + * @param string $meta_key usually set to '' as this parameter is no longer used. | |
| 14 | + * @param mixed $meta_value | |
| 15 | + * @param stdClass|null $field The field $field_id belongs to, when the caller already has | |
| 16 | + * it loaded. Looked up here when it is not passed. | |
| 17 | + * | |
| 14 | 18 | * @return int |
| 15 | 19 | */ |
| 16 | - public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { | |
| 20 | + public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value, $field = null ) { | |
| 17 | 21 | global $wpdb; |
| 18 | 22 | |
| 19 | 23 | if ( FrmAppHelper::is_empty_value( $meta_value ) ) { |
| 20 | - // don't save blank fields | |
| 24 | + // Don't save blank fields | |
| 21 | 25 | return 0; |
| 22 | 26 | } |
| 23 | 27 | |
| 24 | 28 | $new_values = array( |
| @@ -27,33 +31,34 @@ | ||
| 27 | 31 | 'field_id' => $field_id, |
| 28 | 32 | 'created_at' => current_time( 'mysql', 1 ), |
| 29 | 33 | ); |
| 30 | 34 | |
| 31 | - self::set_value_before_save( $new_values ); | |
| 32 | - $new_values = apply_filters( 'frm_add_entry_meta', $new_values ); | |
| 33 | - | |
| 35 | + self::set_value_before_save( $new_values, $field ); | |
| 36 | + $new_values = apply_filters( 'frm_add_entry_meta', $new_values ); | |
| 34 | 37 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_item_metas', $new_values ); |
| 35 | 38 | |
| 36 | 39 | if ( $query_results ) { |
| 37 | 40 | self::clear_cache(); |
| 38 | 41 | wp_cache_delete( $entry_id, 'frm_entry' ); |
| 39 | - $id = $wpdb->insert_id; | |
| 40 | - } else { | |
| 41 | - $id = 0; | |
| 42 | + return $wpdb->insert_id; | |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | - return $id; | |
| 45 | + return 0; | |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | /** |
| 48 | - * @param int $entry_id | |
| 49 | - * @param int $field_id | |
| 50 | - * @param string $meta_key Deprecated. | |
| 51 | - * @param array|string $meta_value | |
| 49 | + * @since 6.35 Added the $field parameter. | |
| 52 | 50 | * |
| 53 | - * @return bool|false|int | |
| 51 | + * @param int $entry_id | |
| 52 | + * @param int $field_id | |
| 53 | + * @param string $meta_key Deprecated. | |
| 54 | + * @param array|string $meta_value | |
| 55 | + * @param stdClass|null $field The field $field_id belongs to, when the caller already has | |
| 56 | + * it loaded. Looked up here when it is not passed. | |
| 57 | + * | |
| 58 | + * @return bool|int | |
| 54 | 59 | */ |
| 55 | - public static function update_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { | |
| 60 | + public static function update_entry_meta( $entry_id, $field_id, $meta_key, $meta_value, $field = null ) { | |
| 56 | 61 | if ( ! $field_id ) { |
| 57 | 62 | return false; |
| 58 | 63 | } |
| 59 | 64 | |
| @@ -64,9 +69,9 @@ | ||
| 64 | 69 | 'field_id' => $field_id, |
| 65 | 70 | ); |
| 66 | 71 | $where_values = $values; |
| 67 | 72 | $values['meta_value'] = $meta_value; |
| 68 | - self::set_value_before_save( $values ); | |
| 73 | + self::set_value_before_save( $values, $field ); | |
| 69 | 74 | $values = apply_filters( 'frm_update_entry_meta', $values ); |
| 70 | 75 | |
| 71 | 76 | if ( is_array( $values['meta_value'] ) ) { |
| 72 | 77 | $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' ); |
| @@ -81,21 +86,27 @@ | ||
| 81 | 86 | } |
| 82 | 87 | |
| 83 | 88 | /** |
| 84 | 89 | * @since 3.0 |
| 90 | + * @since 6.35 Added the $field parameter. | |
| 85 | 91 | * |
| 86 | - * @param array $values | |
| 92 | + * @param array $values | |
| 93 | + * @param stdClass|null $field The field for $values['field_id'], when the caller already has | |
| 94 | + * it loaded. Looked up here when it is not passed. | |
| 87 | 95 | * |
| 88 | 96 | * @return void |
| 89 | 97 | */ |
| 90 | - private static function set_value_before_save( &$values ) { | |
| 91 | - $field = FrmField::getOne( $values['field_id'] ); | |
| 98 | + private static function set_value_before_save( &$values, $field = null ) { | |
| 99 | + if ( ! is_object( $field ) ) { | |
| 100 | + $field = FrmField::getOne( $values['field_id'] ); | |
| 101 | + } | |
| 92 | 102 | |
| 93 | - if ( $field ) { | |
| 94 | - $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 103 | + if ( ! $field ) { | |
| 104 | + return; | |
| 105 | + } | |
| 95 | 106 | |
| 96 | - $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 97 | - } | |
| 107 | + $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 108 | + $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 98 | 109 | } |
| 99 | 110 | |
| 100 | 111 | /** |
| 101 | 112 | * @since 3.0 |
| @@ -155,24 +166,24 @@ | ||
| 155 | 166 | $values_indexed_by_field_id[ $field_id ] = $meta_value; |
| 156 | 167 | |
| 157 | 168 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 158 | 169 | |
| 159 | - if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { | |
| 170 | + if ( ! $previous_field_ids || ! in_array( $field_id, $previous_field_ids, true ) ) { | |
| 171 | + // If value does not exist, then create it | |
| 172 | + self::add_entry_meta( $entry_id, $field_id, '', $meta_value, $field ); | |
| 173 | + continue; | |
| 174 | + } | |
| 160 | 175 | |
| 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 ] ); | |
| 164 | - } else { | |
| 165 | - // if value exists, then update it | |
| 166 | - self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 167 | - } | |
| 176 | + if ( $meta_value === array() || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) { | |
| 177 | + // Remove blank fields. | |
| 178 | + unset( $values_indexed_by_field_id[ $field_id ] ); | |
| 168 | 179 | } else { |
| 169 | - // if value does not exist, then create it | |
| 170 | - self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 180 | + // If value exists, then update it | |
| 181 | + self::update_entry_meta( $entry_id, $field_id, '', $meta_value, $field ); | |
| 171 | 182 | } |
| 172 | 183 | }//end foreach |
| 173 | 184 | |
| 174 | - if ( empty( $previous_field_ids ) ) { | |
| 185 | + if ( ! $previous_field_ids ) { | |
| 175 | 186 | return; |
| 176 | 187 | } |
| 177 | 188 | |
| 178 | 189 | $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) ); |
| @@ -180,9 +191,9 @@ | ||
| 180 | 191 | if ( ! $field_ids_to_remove ) { |
| 181 | 192 | return; |
| 182 | 193 | } |
| 183 | 194 | |
| 184 | - // prepare the query | |
| 195 | + // Prepare the query | |
| 185 | 196 | $where = array( |
| 186 | 197 | 'item_id' => $entry_id, |
| 187 | 198 | 'field_id' => $field_ids_to_remove, |
| 188 | 199 | ); |
| @@ -188,9 +199,9 @@ | ||
| 188 | 199 | ); |
| 189 | 200 | FrmDb::get_where_clause_and_values( $where ); |
| 190 | 201 | |
| 191 | 202 | // 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 | |
| 203 | + $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 193 | 204 | self::clear_cache(); |
| 194 | 205 | } |
| 195 | 206 | |
| 196 | 207 | /** |
| @@ -278,9 +289,8 @@ | ||
| 278 | 289 | } |
| 279 | 290 | |
| 280 | 291 | if ( $cached && isset( $cached->metas ) && isset( $cached->metas[ $field_id ] ) ) { |
| 281 | 292 | $result = $cached->metas[ $field_id ]; |
| 282 | - | |
| 283 | 293 | return wp_unslash( $result ); |
| 284 | 294 | } |
| 285 | 295 | |
| 286 | 296 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
| @@ -294,16 +304,13 @@ | ||
| 294 | 304 | $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; |
| 295 | 305 | $query['fi.field_key'] = $field_id; |
| 296 | 306 | } |
| 297 | 307 | |
| 298 | - $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); | |
| 299 | - | |
| 308 | + $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); | |
| 300 | 309 | $field_type = FrmField::get_type( $field_id ); |
| 301 | 310 | FrmFieldsHelper::prepare_field_value( $result, $field_type ); |
| 302 | 311 | |
| 303 | - $result = wp_unslash( $result ); | |
| 304 | - | |
| 305 | - return $result; | |
| 312 | + return wp_unslash( $result ); | |
| 306 | 313 | } |
| 307 | 314 | |
| 308 | 315 | /** |
| 309 | 316 | * @param int|string $field_id |
| @@ -320,13 +327,11 @@ | ||
| 320 | 327 | 'stripslashes' => true, |
| 321 | 328 | 'is_draft' => false, |
| 322 | 329 | ); |
| 323 | 330 | $args = wp_parse_args( $args, $defaults ); |
| 324 | - | |
| 325 | - $query = array(); | |
| 331 | + $query = array(); | |
| 326 | 332 | self::meta_field_query( $field_id, $order, $limit, $args, $query ); |
| 327 | - $query = implode( ' ', $query ); | |
| 328 | - | |
| 333 | + $query = implode( ' ', $query ); | |
| 329 | 334 | $cache_key = 'entry_metas_for_field_' . $field_id . $order . $limit . FrmAppHelper::maybe_json_encode( $args ); |
| 330 | 335 | $values = FrmDb::check_cache( $cache_key, 'frm_entry', $query, 'get_col' ); |
| 331 | 336 | |
| 332 | 337 | if ( ! $args['stripslashes'] ) { |
| @@ -373,8 +378,9 @@ | ||
| 373 | 378 | |
| 374 | 379 | if ( $args['value'] ) { |
| 375 | 380 | $query[] = $wpdb->prepare( ' AND meta_value=%s', $args['value'] ); |
| 376 | 381 | } |
| 382 | + | |
| 377 | 383 | $query[] = $order . $limit; |
| 378 | 384 | } |
| 379 | 385 | |
| 380 | 386 | /** |
| @@ -395,15 +401,17 @@ | ||
| 395 | 401 | * @return mixed |
| 396 | 402 | */ |
| 397 | 403 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 398 | 404 | global $wpdb; |
| 405 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 399 | 406 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 400 | 407 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| 401 | 408 | FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' . |
| 402 | 409 | FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 410 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 403 | 411 | |
| 404 | 412 | $cache_key = 'all_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit; |
| 405 | - $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, ( $limit == ' LIMIT 1' ? 'get_row' : 'get_results' ) ); | |
| 413 | + $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, $limit === ' LIMIT 1' ? 'get_row' : 'get_results' ); | |
| 406 | 414 | |
| 407 | 415 | if ( ! $results || ! $stripslashes ) { |
| 408 | 416 | return $results; |
| 409 | 417 | } |
| @@ -432,13 +440,11 @@ | ||
| 432 | 440 | 'user_id' => '', |
| 433 | 441 | 'group_by' => '', |
| 434 | 442 | ); |
| 435 | 443 | $args = wp_parse_args( $args, $defaults ); |
| 436 | - | |
| 437 | - $query = array(); | |
| 444 | + $query = array(); | |
| 438 | 445 | self::get_ids_query( $where, $order_by, $limit, $unique, $args, $query ); |
| 439 | - $query = implode( ' ', $query ); | |
| 440 | - | |
| 446 | + $query = implode( ' ', $query ); | |
| 441 | 447 | $cache_key = 'ids_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . 'l' . $limit . 'u' . $unique . FrmAppHelper::maybe_json_encode( $args ); |
| 442 | 448 | $type = 'get_' . ( ' LIMIT 1' === $limit ? 'var' : 'col' ); |
| 443 | 449 | return FrmDb::check_cache( $cache_key, 'frm_entry', $query, $type ); |
| 444 | 450 | } |
| @@ -479,9 +485,9 @@ | ||
| 479 | 485 | |
| 480 | 486 | $where_fields = array_keys( $where ); |
| 481 | 487 | |
| 482 | 488 | foreach ( $where_fields as $where_field ) { |
| 483 | - if ( strpos( $where_field, 'fi.' ) === 0 && 'fi.form_id' !== $where_field ) { | |
| 489 | + if ( str_starts_with( $where_field, 'fi.' ) && 'fi.form_id' !== $where_field ) { | |
| 484 | 490 | return true; |
| 485 | 491 | } |
| 486 | 492 | } |
| 487 | 493 | |
| @@ -488,8 +494,9 @@ | ||
| 488 | 494 | if ( isset( $where['fi.form_id'] ) ) { |
| 489 | 495 | $where['e.form_id'] = $where['fi.form_id']; |
| 490 | 496 | unset( $where['fi.form_id'] ); |
| 491 | 497 | } |
| 498 | + | |
| 492 | 499 | return false; |
| 493 | 500 | } |
| 494 | 501 | |
| 495 | 502 | /** |
| @@ -501,9 +508,9 @@ | ||
| 501 | 508 | * @param array $query |
| 502 | 509 | * |
| 503 | 510 | * @return void |
| 504 | 511 | */ |
| 505 | - private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { | |
| 512 | + private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 506 | 513 | global $wpdb; |
| 507 | 514 | $query[] = 'SELECT'; |
| 508 | 515 | $defaults = array( |
| 509 | 516 | 'return_parent_id' => false, |
| @@ -524,12 +531,9 @@ | ||
| 524 | 531 | } |
| 525 | 532 | |
| 526 | 533 | $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; |
| 527 | 534 | |
| 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 ) { | |
| 535 | + if ( self::should_join_fields_table( $where ) || self::should_join_fields_table( $order_by ) ) { | |
| 532 | 536 | $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; |
| 533 | 537 | } |
| 534 | 538 | |
| 535 | 539 | $query[] = $from; |
| @@ -538,16 +542,12 @@ | ||
| 538 | 542 | if ( is_array( $where ) ) { |
| 539 | 543 | if ( ! $args['is_draft'] ) { |
| 540 | 544 | $where['e.is_draft'] = 0; |
| 541 | 545 | } 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 | - } | |
| 546 | + $where['e.is_draft'] = class_exists( 'FrmAbandonmentHooksController', false ) ? absint( $args['is_draft'] ) : 1; | |
| 547 | 547 | } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) { |
| 548 | 548 | $where['e.is_draft'] = array( 0, 1 ); |
| 549 | - } elseif ( false !== strpos( $args['is_draft'], ',' ) ) { | |
| 549 | + } elseif ( str_contains( $args['is_draft'], ',' ) ) { | |
| 550 | 550 | $is_draft = array_reduce( |
| 551 | 551 | explode( ',', $args['is_draft'] ), |
| 552 | 552 | function ( $total, $current ) { |
| 553 | 553 | if ( is_numeric( $current ) ) { |
| @@ -565,8 +565,9 @@ | ||
| 565 | 565 | |
| 566 | 566 | if ( ! empty( $args['user_id'] ) ) { |
| 567 | 567 | $where['e.user_id'] = $args['user_id']; |
| 568 | 568 | } |
| 569 | + | |
| 569 | 570 | $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 570 | 571 | |
| 571 | 572 | if ( $args['group_by'] ) { |
| 572 | 573 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| @@ -579,9 +580,9 @@ | ||
| 579 | 580 | $user_where = ''; |
| 580 | 581 | |
| 581 | 582 | if ( ! $args['is_draft'] ) { |
| 582 | 583 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 ); |
| 583 | - } elseif ( $args['is_draft'] == 1 ) { | |
| 584 | + } elseif ( $args['is_draft'] == 1 ) { // phpcs:ignore Universal.Operators.StrictComparisons | |
| 584 | 585 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 ); |
| 585 | 586 | } |
| 586 | 587 | |
| 587 | 588 | if ( ! empty( $args['user_id'] ) ) { |
| @@ -587,10 +588,10 @@ | ||
| 587 | 588 | if ( ! empty( $args['user_id'] ) ) { |
| 588 | 589 | $user_where = $wpdb->prepare( ' AND e.user_id=%d', $args['user_id'] ); |
| 589 | 590 | } |
| 590 | 591 | |
| 591 | - if ( strpos( $where, ' GROUP BY ' ) ) { | |
| 592 | - // don't inject WHERE filtering after GROUP BY | |
| 592 | + if ( str_contains( $where, ' GROUP BY ' ) ) { | |
| 593 | + // Don't inject WHERE filtering after GROUP BY | |
| 593 | 594 | $parts = explode( ' GROUP BY ', $where ); |
| 594 | 595 | $where = $parts[0]; |
| 595 | 596 | $where .= $draft_where . $user_where; |
| 596 | 597 | $where .= ' GROUP BY ' . $parts[1]; |
| @@ -622,9 +623,9 @@ | ||
| 622 | 623 | if ( is_array( $search ) ) { |
| 623 | 624 | $where = ''; |
| 624 | 625 | |
| 625 | 626 | foreach ( $search as $field => $value ) { |
| 626 | - if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) { | |
| 627 | + if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ), true ) ) { | |
| 627 | 628 | continue; |
| 628 | 629 | } |
| 629 | 630 | |
| 630 | 631 | switch ( $field ) { |
| @@ -647,9 +648,9 @@ | ||
| 647 | 648 | if ( $operator === 'LIKE' ) { |
| 648 | 649 | $search = '%' . $search . '%'; |
| 649 | 650 | } |
| 650 | 651 | |
| 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 | + $query = $wpdb->prepare( "SELECT DISTINCT item_id FROM {$wpdb->prefix}frm_item_metas WHERE meta_value {$operator} %s and field_id = %d", $search, $field_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 652 | 653 | }//end if |
| 653 | 654 | |
| 654 | 655 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 655 | 656 | FrmDb::set_cache( $cache_key, $results, 'frm_entry' ); |