| @@ -9,9 +9,8 @@ | ||
| 9 | 9 | * @param int $entry_id |
| 10 | 10 | * @param int $field_id |
| 11 | 11 | * @param string $meta_key usually set to '' as this parameter is no longer used. |
| 12 | 12 | * @param mixed $meta_value |
| 13 | - * @return int | |
| 14 | 13 | */ |
| 15 | 14 | public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { |
| 16 | 15 | global $wpdb; |
| 17 | 16 | |
| @@ -43,11 +42,11 @@ | ||
| 43 | 42 | return $id; |
| 44 | 43 | } |
| 45 | 44 | |
| 46 | 45 | /** |
| 47 | - * @param int $entry_id | |
| 48 | - * @param int $field_id | |
| 49 | - * @param string $meta_key Deprecated. | |
| 46 | + * @param int $entry_id | |
| 47 | + * @param int $field_id | |
| 48 | + * @param string $meta_key deprecated | |
| 50 | 49 | * @param array|string $meta_value |
| 51 | 50 | * |
| 52 | 51 | * @return bool|false|int |
| 53 | 52 | */ |
| @@ -107,18 +106,13 @@ | ||
| 107 | 106 | |
| 108 | 107 | $value = apply_filters( 'frm_prepare_data_before_db', $value, $atts['field_id'], $atts['entry_id'], array( 'field' => $atts['field'] ) ); |
| 109 | 108 | } |
| 110 | 109 | |
| 111 | - /** | |
| 112 | - * @param int|string $entry_id | |
| 113 | - * @param array $values Either indexed by field ID or field key. | |
| 114 | - * @return void | |
| 115 | - */ | |
| 116 | 110 | public static function update_entry_metas( $entry_id, $values ) { |
| 117 | 111 | global $wpdb; |
| 118 | 112 | |
| 119 | - $previous_field_ids = FrmDb::get_col( | |
| 120 | - 'frm_item_metas', | |
| 113 | + $prev_values = FrmDb::get_col( | |
| 114 | + $wpdb->prefix . 'frm_item_metas', | |
| 121 | 115 | array( |
| 122 | 116 | 'item_id' => $entry_id, |
| 123 | 117 | 'field_id !' => 0, |
| 124 | 118 | ), |
| @@ -124,30 +118,21 @@ | ||
| 124 | 118 | ), |
| 125 | 119 | 'field_id' |
| 126 | 120 | ); |
| 127 | 121 | |
| 128 | - $values_indexed_by_field_id = array(); | |
| 129 | - foreach ( $values as $field_id_or_key => $meta_value ) { | |
| 130 | - $field_id = $field_id_or_key; | |
| 131 | - $field = null; | |
| 132 | - | |
| 133 | - if ( $field_id_or_key ) { | |
| 134 | - $field = FrmField::getOne( $field_id_or_key ); | |
| 135 | - | |
| 136 | - if ( is_object( $field ) ) { | |
| 137 | - $field_id = $field->id; | |
| 138 | - } | |
| 122 | + foreach ( $values as $field_id => $meta_value ) { | |
| 123 | + $field = false; | |
| 124 | + if ( ! empty( $field_id ) ) { | |
| 125 | + $field = FrmField::getOne( $field_id ); | |
| 139 | 126 | } |
| 140 | 127 | |
| 141 | - $values_indexed_by_field_id[ $field_id ] = $meta_value; | |
| 142 | - | |
| 143 | 128 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 144 | 129 | |
| 145 | - if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { | |
| 130 | + if ( $prev_values && in_array( $field_id, $prev_values ) ) { | |
| 146 | 131 | |
| 147 | 132 | if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { |
| 148 | - // Remove blank fields. | |
| 149 | - unset( $values_indexed_by_field_id[ $field_id ] ); | |
| 133 | + // remove blank fields | |
| 134 | + unset( $values[ $field_id ] ); | |
| 150 | 135 | } else { |
| 151 | 136 | // if value exists, then update it |
| 152 | 137 | self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 153 | 138 | } |
| @@ -154,17 +139,17 @@ | ||
| 154 | 139 | } else { |
| 155 | 140 | // if value does not exist, then create it |
| 156 | 141 | self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 157 | 142 | } |
| 158 | - }//end foreach | |
| 143 | + } | |
| 159 | 144 | |
| 160 | - if ( empty( $previous_field_ids ) ) { | |
| 145 | + if ( empty( $prev_values ) ) { | |
| 161 | 146 | return; |
| 162 | 147 | } |
| 163 | 148 | |
| 164 | - $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) ); | |
| 149 | + $prev_values = array_diff( $prev_values, array_keys( $values ) ); | |
| 165 | 150 | |
| 166 | - if ( ! $field_ids_to_remove ) { | |
| 151 | + if ( empty( $prev_values ) ) { | |
| 167 | 152 | return; |
| 168 | 153 | } |
| 169 | 154 | |
| 170 | 155 | // prepare the query |
| @@ -169,9 +154,9 @@ | ||
| 169 | 154 | |
| 170 | 155 | // prepare the query |
| 171 | 156 | $where = array( |
| 172 | 157 | 'item_id' => $entry_id, |
| 173 | - 'field_id' => $field_ids_to_remove, | |
| 158 | + 'field_id' => $prev_values, | |
| 174 | 159 | ); |
| 175 | 160 | FrmDb::get_where_clause_and_values( $where ); |
| 176 | 161 | |
| 177 | 162 | // Delete any leftovers |
| @@ -216,25 +201,17 @@ | ||
| 216 | 201 | } |
| 217 | 202 | |
| 218 | 203 | /** |
| 219 | 204 | * @since 2.0.9 |
| 220 | - * | |
| 221 | - * @param stdClass $entry | |
| 222 | - * @param int|string $field_id | |
| 223 | - * @return mixed | |
| 224 | 205 | */ |
| 225 | 206 | public static function get_meta_value( $entry, $field_id ) { |
| 226 | 207 | if ( isset( $entry->metas ) ) { |
| 227 | - return $entry->metas[ $field_id ] ?? false; | |
| 208 | + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false; | |
| 209 | + } else { | |
| 210 | + return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 228 | 211 | } |
| 229 | - return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 230 | 212 | } |
| 231 | 213 | |
| 232 | - /** | |
| 233 | - * @param int|object|string $entry_id | |
| 234 | - * @param int|string $field_id This function supports field keys as field id. | |
| 235 | - * @return mixed | |
| 236 | - */ | |
| 237 | 214 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 238 | 215 | global $wpdb; |
| 239 | 216 | |
| 240 | 217 | if ( is_object( $entry_id ) ) { |
| @@ -254,21 +231,16 @@ | ||
| 254 | 231 | |
| 255 | 232 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
| 256 | 233 | $query = array( 'item_id' => $entry_id ); |
| 257 | 234 | if ( is_numeric( $field_id ) ) { |
| 258 | - // Query by field ID. | |
| 259 | 235 | $query['field_id'] = $field_id; |
| 260 | 236 | } else { |
| 261 | - // Query by field key. | |
| 262 | - $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 237 | + $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 263 | 238 | $query['fi.field_key'] = $field_id; |
| 264 | 239 | } |
| 265 | 240 | |
| 266 | 241 | $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); |
| 267 | - | |
| 268 | - $field_type = FrmField::get_type( $field_id ); | |
| 269 | - FrmFieldsHelper::prepare_field_value( $result, $field_type ); | |
| 270 | - | |
| 242 | + FrmAppHelper::unserialize_or_decode( $result ); | |
| 271 | 243 | $result = wp_unslash( $result ); |
| 272 | 244 | |
| 273 | 245 | return $result; |
| 274 | 246 | } |
| @@ -302,13 +274,10 @@ | ||
| 302 | 274 | return wp_unslash( $values ); |
| 303 | 275 | } |
| 304 | 276 | |
| 305 | 277 | /** |
| 306 | - * @param int|string $field_id | |
| 307 | - * @param string $order | |
| 308 | - * @param string $limit | |
| 309 | - * @param array $args | |
| 310 | - * @param array $query | |
| 278 | + * @param string $order | |
| 279 | + * @param string $limit | |
| 311 | 280 | */ |
| 312 | 281 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 313 | 282 | global $wpdb; |
| 314 | 283 | $query[] = 'SELECT'; |
| @@ -338,15 +307,8 @@ | ||
| 338 | 307 | public static function get_entry_meta_info( $entry_id ) { |
| 339 | 308 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 340 | 309 | } |
| 341 | 310 | |
| 342 | - /** | |
| 343 | - * @param array $where | |
| 344 | - * @param string $order_by | |
| 345 | - * @param string $limit | |
| 346 | - * @param bool $stripslashes | |
| 347 | - * @return array | |
| 348 | - */ | |
| 349 | 311 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 350 | 312 | global $wpdb; |
| 351 | 313 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 352 | 314 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| @@ -399,41 +361,12 @@ | ||
| 399 | 361 | return self::getEntryIds( $query, '', '', true, $args ); |
| 400 | 362 | } |
| 401 | 363 | |
| 402 | 364 | /** |
| 403 | - * Returns true if the where clause refers to a field table column that is not form_id. It also updates | |
| 404 | - * the where clause to refer to the entry table for form_id if fields table should not be joined. | |
| 405 | - * | |
| 406 | - * @since 6.16.1 | |
| 407 | - * @param array|string $where | |
| 408 | - * @return bool | |
| 365 | + * @param string|array $where | |
| 366 | + * @param string $order_by | |
| 367 | + * @param string $limit | |
| 409 | 368 | */ |
| 410 | - private static function should_join_fields_table( &$where ) { | |
| 411 | - if ( is_string( $where ) ) { | |
| 412 | - if ( preg_match( '/\bfi\.(?!form_id)\w+/i', $where ) ) { | |
| 413 | - return true; | |
| 414 | - } | |
| 415 | - $where = str_ireplace( 'fi.form_id', 'e.form_id', $where ); | |
| 416 | - return false; | |
| 417 | - } | |
| 418 | - $where_fields = array_keys( $where ); | |
| 419 | - foreach ( $where_fields as $where_field ) { | |
| 420 | - if ( strpos( $where_field, 'fi.' ) === 0 && 'fi.form_id' !== $where_field ) { | |
| 421 | - return true; | |
| 422 | - } | |
| 423 | - } | |
| 424 | - if ( isset( $where['fi.form_id'] ) ) { | |
| 425 | - $where['e.form_id'] = $where['fi.form_id']; | |
| 426 | - unset( $where['fi.form_id'] ); | |
| 427 | - } | |
| 428 | - return false; | |
| 429 | - } | |
| 430 | - | |
| 431 | - /** | |
| 432 | - * @param array|string $where | |
| 433 | - * @param string $order_by | |
| 434 | - * @param string $limit | |
| 435 | - */ | |
| 436 | 369 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
| 437 | 370 | global $wpdb; |
| 438 | 371 | $query[] = 'SELECT'; |
| 439 | 372 | $defaults = array( |
| @@ -453,44 +386,17 @@ | ||
| 453 | 386 | } else { |
| 454 | 387 | $query[] = 'it.item_id'; |
| 455 | 388 | } |
| 456 | 389 | |
| 457 | - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 390 | + $query[] = 'FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 458 | 391 | |
| 459 | - $should_join_fields_table__where = self::should_join_fields_table( $where ); | |
| 460 | - $should_join_fields_table__order_by = self::should_join_fields_table( $order_by ); | |
| 461 | - if ( $should_join_fields_table__where || $should_join_fields_table__order_by ) { | |
| 462 | - $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 463 | - } | |
| 464 | - | |
| 465 | - $query[] = $from; | |
| 466 | 392 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 467 | 393 | if ( is_array( $where ) ) { |
| 468 | 394 | if ( ! $args['is_draft'] ) { |
| 469 | 395 | $where['e.is_draft'] = 0; |
| 470 | - } elseif ( is_numeric( $args['is_draft'] ) ) { | |
| 471 | - if ( class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 472 | - $where['e.is_draft'] = absint( $args['is_draft'] ); | |
| 473 | - } else { | |
| 474 | - $where['e.is_draft'] = 1; | |
| 475 | - } | |
| 476 | - } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 477 | - $where['e.is_draft'] = array( 0, 1 ); | |
| 478 | - } elseif ( false !== strpos( $args['is_draft'], ',' ) ) { | |
| 479 | - $is_draft = array_reduce( | |
| 480 | - explode( ',', $args['is_draft'] ), | |
| 481 | - function ( $total, $current ) { | |
| 482 | - if ( is_numeric( $current ) ) { | |
| 483 | - $total[] = absint( $current ); | |
| 484 | - } | |
| 485 | - return $total; | |
| 486 | - }, | |
| 487 | - array() | |
| 488 | - ); | |
| 489 | - if ( $is_draft ) { | |
| 490 | - $where['e.is_draft'] = $is_draft; | |
| 491 | - } | |
| 492 | - }//end if | |
| 396 | + } elseif ( $args['is_draft'] == 1 ) { | |
| 397 | + $where['e.is_draft'] = 1; | |
| 398 | + } | |
| 493 | 399 | |
| 494 | 400 | if ( ! empty( $args['user_id'] ) ) { |
| 495 | 401 | $where['e.user_id'] = $args['user_id']; |
| 496 | 402 | } |
| @@ -500,9 +406,9 @@ | ||
| 500 | 406 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| 501 | 407 | } |
| 502 | 408 | |
| 503 | 409 | return; |
| 504 | - }//end if | |
| 410 | + } | |
| 505 | 411 | |
| 506 | 412 | $draft_where = ''; |
| 507 | 413 | $user_where = ''; |
| 508 | 414 | if ( ! $args['is_draft'] ) { |
| @@ -556,15 +462,15 @@ | ||
| 556 | 462 | } |
| 557 | 463 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 558 | 464 | } |
| 559 | 465 | $where .= $wpdb->prepare( ' field_id=%d', $field_id ); |
| 560 | - $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 466 | + $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 561 | 467 | } else { |
| 562 | - if ( $operator === 'LIKE' ) { | |
| 468 | + if ( $operator == 'LIKE' ) { | |
| 563 | 469 | $search = '%' . $search . '%'; |
| 564 | 470 | } |
| 565 | 471 | $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 |
| 566 | - }//end if | |
| 472 | + } | |
| 567 | 473 | |
| 568 | 474 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 569 | 475 | FrmDb::set_cache( $cache_key, $results, 'frm_entry' ); |
| 570 | 476 | |