| @@ -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,9 +31,9 @@ | ||
| 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 ); | |
| 35 | + self::set_value_before_save( $new_values, $field ); | |
| 32 | 36 | $new_values = apply_filters( 'frm_add_entry_meta', $new_values ); |
| 33 | 37 | $query_results = $wpdb->insert( $wpdb->prefix . 'frm_item_metas', $new_values ); |
| 34 | 38 | |
| 35 | 39 | if ( $query_results ) { |
| @@ -41,16 +45,20 @@ | ||
| 41 | 45 | return 0; |
| 42 | 46 | } |
| 43 | 47 | |
| 44 | 48 | /** |
| 45 | - * @param int $entry_id | |
| 46 | - * @param int $field_id | |
| 47 | - * @param string $meta_key Deprecated. | |
| 48 | - * @param array|string $meta_value | |
| 49 | + * @since 6.35 Added the $field parameter. | |
| 49 | 50 | * |
| 50 | - * @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 | |
| 51 | 59 | */ |
| 52 | - 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 ) { | |
| 53 | 61 | if ( ! $field_id ) { |
| 54 | 62 | return false; |
| 55 | 63 | } |
| 56 | 64 | |
| @@ -61,9 +69,9 @@ | ||
| 61 | 69 | 'field_id' => $field_id, |
| 62 | 70 | ); |
| 63 | 71 | $where_values = $values; |
| 64 | 72 | $values['meta_value'] = $meta_value; |
| 65 | - self::set_value_before_save( $values ); | |
| 73 | + self::set_value_before_save( $values, $field ); | |
| 66 | 74 | $values = apply_filters( 'frm_update_entry_meta', $values ); |
| 67 | 75 | |
| 68 | 76 | if ( is_array( $values['meta_value'] ) ) { |
| 69 | 77 | $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' ); |
| @@ -78,20 +86,27 @@ | ||
| 78 | 86 | } |
| 79 | 87 | |
| 80 | 88 | /** |
| 81 | 89 | * @since 3.0 |
| 90 | + * @since 6.35 Added the $field parameter. | |
| 82 | 91 | * |
| 83 | - * @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. | |
| 84 | 95 | * |
| 85 | 96 | * @return void |
| 86 | 97 | */ |
| 87 | - private static function set_value_before_save( &$values ) { | |
| 88 | - $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 | + } | |
| 89 | 102 | |
| 90 | - if ( $field ) { | |
| 91 | - $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 92 | - $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 103 | + if ( ! $field ) { | |
| 104 | + return; | |
| 93 | 105 | } |
| 106 | + | |
| 107 | + $field_obj = FrmFieldFactory::get_field_object( $field ); | |
| 108 | + $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); | |
| 94 | 109 | } |
| 95 | 110 | |
| 96 | 111 | /** |
| 97 | 112 | * @since 3.0 |
| @@ -151,20 +166,20 @@ | ||
| 151 | 166 | $values_indexed_by_field_id[ $field_id ] = $meta_value; |
| 152 | 167 | |
| 153 | 168 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 154 | 169 | |
| 155 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 156 | - if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { | |
| 157 | - if ( $meta_value === array() || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) { | |
| 158 | - // Remove blank fields. | |
| 159 | - unset( $values_indexed_by_field_id[ $field_id ] ); | |
| 160 | - } else { | |
| 161 | - // if value exists, then update it | |
| 162 | - self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); | |
| 163 | - } | |
| 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 | + } | |
| 175 | + | |
| 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 ] ); | |
| 164 | 179 | } else { |
| 165 | - // if value does not exist, then create it | |
| 166 | - 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 ); | |
| 167 | 182 | } |
| 168 | 183 | }//end foreach |
| 169 | 184 | |
| 170 | 185 | if ( ! $previous_field_ids ) { |
| @@ -176,9 +191,9 @@ | ||
| 176 | 191 | if ( ! $field_ids_to_remove ) { |
| 177 | 192 | return; |
| 178 | 193 | } |
| 179 | 194 | |
| 180 | - // prepare the query | |
| 195 | + // Prepare the query | |
| 181 | 196 | $where = array( |
| 182 | 197 | 'item_id' => $entry_id, |
| 183 | 198 | 'field_id' => $field_ids_to_remove, |
| 184 | 199 | ); |
| @@ -386,15 +401,17 @@ | ||
| 386 | 401 | * @return mixed |
| 387 | 402 | */ |
| 388 | 403 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 389 | 404 | global $wpdb; |
| 405 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 390 | 406 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 391 | 407 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| 392 | 408 | FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id' . |
| 393 | 409 | FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 410 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 394 | 411 | |
| 395 | 412 | $cache_key = 'all_' . FrmAppHelper::maybe_json_encode( $where ) . $order_by . $limit; |
| 396 | - $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, ( $limit == ' LIMIT 1' ? 'get_row' : 'get_results' ) ); // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 413 | + $results = FrmDb::check_cache( $cache_key, 'frm_entry', $query, $limit === ' LIMIT 1' ? 'get_row' : 'get_results' ); | |
| 397 | 414 | |
| 398 | 415 | if ( ! $results || ! $stripslashes ) { |
| 399 | 416 | return $results; |
| 400 | 417 | } |
| @@ -512,13 +529,11 @@ | ||
| 512 | 529 | } else { |
| 513 | 530 | $query[] = 'it.item_id'; |
| 514 | 531 | } |
| 515 | 532 | |
| 516 | - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 517 | - $should_join_fields_table__where = self::should_join_fields_table( $where ); | |
| 518 | - $should_join_fields_table__order_by = self::should_join_fields_table( $order_by ); | |
| 533 | + $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 519 | 534 | |
| 520 | - 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 ) ) { | |
| 521 | 536 | $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; |
| 522 | 537 | } |
| 523 | 538 | |
| 524 | 539 | $query[] = $from; |
| @@ -574,9 +589,9 @@ | ||
| 574 | 589 | $user_where = $wpdb->prepare( ' AND e.user_id=%d', $args['user_id'] ); |
| 575 | 590 | } |
| 576 | 591 | |
| 577 | 592 | if ( str_contains( $where, ' GROUP BY ' ) ) { |
| 578 | - // don't inject WHERE filtering after GROUP BY | |
| 593 | + // Don't inject WHERE filtering after GROUP BY | |
| 579 | 594 | $parts = explode( ' GROUP BY ', $where ); |
| 580 | 595 | $where = $parts[0]; |
| 581 | 596 | $where .= $draft_where . $user_where; |
| 582 | 597 | $where .= ' GROUP BY ' . $parts[1]; |