| @@ -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 | - * | |
| 14 | 13 | * @return int |
| 15 | 14 | */ |
| 16 | 15 | public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { |
| 17 | 16 | global $wpdb; |
| @@ -70,9 +69,8 @@ | ||
| 70 | 69 | |
| 71 | 70 | if ( is_array( $values['meta_value'] ) ) { |
| 72 | 71 | $values['meta_value'] = array_filter( $values['meta_value'], 'FrmAppHelper::is_not_empty_value' ); |
| 73 | 72 | } |
| 74 | - | |
| 75 | 73 | $meta_value = maybe_serialize( $values['meta_value'] ); |
| 76 | 74 | |
| 77 | 75 | wp_cache_delete( $entry_id, 'frm_entry' ); |
| 78 | 76 | self::clear_cache(); |
| @@ -81,16 +79,11 @@ | ||
| 81 | 79 | } |
| 82 | 80 | |
| 83 | 81 | /** |
| 84 | 82 | * @since 3.0 |
| 85 | - * | |
| 86 | - * @param array $values | |
| 87 | - * | |
| 88 | - * @return void | |
| 89 | 83 | */ |
| 90 | 84 | private static function set_value_before_save( &$values ) { |
| 91 | 85 | $field = FrmField::getOne( $values['field_id'] ); |
| 92 | - | |
| 93 | 86 | if ( $field ) { |
| 94 | 87 | $field_obj = FrmFieldFactory::get_field_object( $field ); |
| 95 | 88 | |
| 96 | 89 | $values['meta_value'] = $field_obj->set_value_before_save( $values['meta_value'] ); |
| @@ -98,13 +91,8 @@ | ||
| 98 | 91 | } |
| 99 | 92 | |
| 100 | 93 | /** |
| 101 | 94 | * @since 3.0 |
| 102 | - * | |
| 103 | - * @param array $atts | |
| 104 | - * @param mixed $value | |
| 105 | - * | |
| 106 | - * @return void | |
| 107 | 95 | */ |
| 108 | 96 | private static function get_value_to_save( $atts, &$value ) { |
| 109 | 97 | if ( is_object( $atts['field'] ) ) { |
| 110 | 98 | $field_obj = FrmFieldFactory::get_field_object( $atts['field'] ); |
| @@ -119,19 +107,13 @@ | ||
| 119 | 107 | |
| 120 | 108 | $value = apply_filters( 'frm_prepare_data_before_db', $value, $atts['field_id'], $atts['entry_id'], array( 'field' => $atts['field'] ) ); |
| 121 | 109 | } |
| 122 | 110 | |
| 123 | - /** | |
| 124 | - * @param int|string $entry_id | |
| 125 | - * @param array $values Either indexed by field ID or field key. | |
| 126 | - * | |
| 127 | - * @return void | |
| 128 | - */ | |
| 129 | 111 | public static function update_entry_metas( $entry_id, $values ) { |
| 130 | 112 | global $wpdb; |
| 131 | 113 | |
| 132 | - $previous_field_ids = FrmDb::get_col( | |
| 133 | - 'frm_item_metas', | |
| 114 | + $prev_values = FrmDb::get_col( | |
| 115 | + $wpdb->prefix . 'frm_item_metas', | |
| 134 | 116 | array( |
| 135 | 117 | 'item_id' => $entry_id, |
| 136 | 118 | 'field_id !' => 0, |
| 137 | 119 | ), |
| @@ -137,31 +119,21 @@ | ||
| 137 | 119 | ), |
| 138 | 120 | 'field_id' |
| 139 | 121 | ); |
| 140 | 122 | |
| 141 | - $values_indexed_by_field_id = array(); | |
| 142 | - | |
| 143 | - foreach ( $values as $field_id_or_key => $meta_value ) { | |
| 144 | - $field_id = $field_id_or_key; | |
| 145 | - $field = null; | |
| 146 | - | |
| 147 | - if ( $field_id_or_key ) { | |
| 148 | - $field = FrmField::getOne( $field_id_or_key ); | |
| 149 | - | |
| 150 | - if ( is_object( $field ) ) { | |
| 151 | - $field_id = $field->id; | |
| 152 | - } | |
| 123 | + foreach ( $values as $field_id => $meta_value ) { | |
| 124 | + $field = false; | |
| 125 | + if ( ! empty( $field_id ) ) { | |
| 126 | + $field = FrmField::getOne( $field_id ); | |
| 153 | 127 | } |
| 154 | 128 | |
| 155 | - $values_indexed_by_field_id[ $field_id ] = $meta_value; | |
| 156 | - | |
| 157 | 129 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 158 | 130 | |
| 159 | - if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { | |
| 131 | + if ( $prev_values && in_array( $field_id, $prev_values ) ) { | |
| 160 | 132 | |
| 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 ] ); | |
| 133 | + if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { | |
| 134 | + // remove blank fields | |
| 135 | + unset( $values[ $field_id ] ); | |
| 164 | 136 | } else { |
| 165 | 137 | // if value exists, then update it |
| 166 | 138 | self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 167 | 139 | } |
| @@ -170,15 +142,15 @@ | ||
| 170 | 142 | self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 171 | 143 | } |
| 172 | 144 | }//end foreach |
| 173 | 145 | |
| 174 | - if ( empty( $previous_field_ids ) ) { | |
| 146 | + if ( empty( $prev_values ) ) { | |
| 175 | 147 | return; |
| 176 | 148 | } |
| 177 | 149 | |
| 178 | - $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) ); | |
| 150 | + $prev_values = array_diff( $prev_values, array_keys( $values ) ); | |
| 179 | 151 | |
| 180 | - if ( ! $field_ids_to_remove ) { | |
| 152 | + if ( empty( $prev_values ) ) { | |
| 181 | 153 | return; |
| 182 | 154 | } |
| 183 | 155 | |
| 184 | 156 | // prepare the query |
| @@ -183,9 +155,9 @@ | ||
| 183 | 155 | |
| 184 | 156 | // prepare the query |
| 185 | 157 | $where = array( |
| 186 | 158 | 'item_id' => $entry_id, |
| 187 | - 'field_id' => $field_ids_to_remove, | |
| 159 | + 'field_id' => $prev_values, | |
| 188 | 160 | ); |
| 189 | 161 | FrmDb::get_where_clause_and_values( $where ); |
| 190 | 162 | |
| 191 | 163 | // Delete any leftovers |
| @@ -192,14 +164,8 @@ | ||
| 192 | 164 | $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 193 | 165 | self::clear_cache(); |
| 194 | 166 | } |
| 195 | 167 | |
| 196 | - /** | |
| 197 | - * @param int $old_id | |
| 198 | - * @param int $new_id | |
| 199 | - * | |
| 200 | - * @return void | |
| 201 | - */ | |
| 202 | 168 | public static function duplicate_entry_metas( $old_id, $new_id ) { |
| 203 | 169 | $metas = self::get_entry_meta_info( $old_id ); |
| 204 | 170 | |
| 205 | 171 | /** |
| @@ -209,9 +175,8 @@ | ||
| 209 | 175 | * |
| 210 | 176 | * @param array $metas The list of entry meta values. |
| 211 | 177 | */ |
| 212 | 178 | $metas = apply_filters( 'frm_before_duplicate_entry_values', $metas ); |
| 213 | - | |
| 214 | 179 | foreach ( $metas as $meta ) { |
| 215 | 180 | self::add_entry_meta( $new_id, $meta->field_id, '', $meta->meta_value ); |
| 216 | 181 | unset( $meta ); |
| 217 | 182 | } |
| @@ -217,14 +182,8 @@ | ||
| 217 | 182 | } |
| 218 | 183 | self::clear_cache(); |
| 219 | 184 | } |
| 220 | 185 | |
| 221 | - /** | |
| 222 | - * @param int $entry_id | |
| 223 | - * @param int $field_id | |
| 224 | - * | |
| 225 | - * @return false|int | |
| 226 | - */ | |
| 227 | 186 | public static function delete_entry_meta( $entry_id, $field_id ) { |
| 228 | 187 | global $wpdb; |
| 229 | 188 | self::clear_cache(); |
| 230 | 189 | |
| @@ -235,10 +194,8 @@ | ||
| 235 | 194 | * Clear entry meta caching |
| 236 | 195 | * Called when a meta is added or changed |
| 237 | 196 | * |
| 238 | 197 | * @since 2.0.5 |
| 239 | - * | |
| 240 | - * @return void | |
| 241 | 198 | */ |
| 242 | 199 | public static function clear_cache() { |
| 243 | 200 | FrmDb::cache_delete_group( 'frm_entry_meta' ); |
| 244 | 201 | FrmDb::cache_delete_group( 'frm_item_meta' ); |
| @@ -248,24 +205,17 @@ | ||
| 248 | 205 | * @since 2.0.9 |
| 249 | 206 | * |
| 250 | 207 | * @param stdClass $entry |
| 251 | 208 | * @param int|string $field_id |
| 252 | - * | |
| 253 | 209 | * @return mixed |
| 254 | 210 | */ |
| 255 | 211 | public static function get_meta_value( $entry, $field_id ) { |
| 256 | 212 | if ( isset( $entry->metas ) ) { |
| 257 | - return $entry->metas[ $field_id ] ?? false; | |
| 213 | + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false; | |
| 258 | 214 | } |
| 259 | 215 | return self::get_entry_meta_by_field( $entry->id, $field_id ); |
| 260 | 216 | } |
| 261 | 217 | |
| 262 | - /** | |
| 263 | - * @param int|object|string $entry_id | |
| 264 | - * @param int|string $field_id This function supports field keys as field id. | |
| 265 | - * | |
| 266 | - * @return mixed | |
| 267 | - */ | |
| 268 | 218 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 269 | 219 | global $wpdb; |
| 270 | 220 | |
| 271 | 221 | if ( is_object( $entry_id ) ) { |
| @@ -284,15 +234,12 @@ | ||
| 284 | 234 | } |
| 285 | 235 | |
| 286 | 236 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
| 287 | 237 | $query = array( 'item_id' => $entry_id ); |
| 288 | - | |
| 289 | 238 | if ( is_numeric( $field_id ) ) { |
| 290 | - // Query by field ID. | |
| 291 | 239 | $query['field_id'] = $field_id; |
| 292 | 240 | } else { |
| 293 | - // Query by field key. | |
| 294 | - $get_table .= ' it JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 241 | + $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 295 | 242 | $query['fi.field_key'] = $field_id; |
| 296 | 243 | } |
| 297 | 244 | |
| 298 | 245 | $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); |
| @@ -304,16 +251,8 @@ | ||
| 304 | 251 | |
| 305 | 252 | return $result; |
| 306 | 253 | } |
| 307 | 254 | |
| 308 | - /** | |
| 309 | - * @param int|string $field_id | |
| 310 | - * @param string $order | |
| 311 | - * @param string $limit | |
| 312 | - * @param array $args | |
| 313 | - * | |
| 314 | - * @return array | |
| 315 | - */ | |
| 316 | 255 | public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) { |
| 317 | 256 | $defaults = array( |
| 318 | 257 | 'value' => false, |
| 319 | 258 | 'unique' => false, |
| @@ -347,10 +286,8 @@ | ||
| 347 | 286 | * @param string $order |
| 348 | 287 | * @param string $limit |
| 349 | 288 | * @param array $args |
| 350 | 289 | * @param array $query |
| 351 | - * | |
| 352 | - * @return void | |
| 353 | 290 | */ |
| 354 | 291 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 355 | 292 | global $wpdb; |
| 356 | 293 | $query[] = 'SELECT'; |
| @@ -376,13 +313,8 @@ | ||
| 376 | 313 | } |
| 377 | 314 | $query[] = $order . $limit; |
| 378 | 315 | } |
| 379 | 316 | |
| 380 | - /** | |
| 381 | - * @param int|string $entry_id | |
| 382 | - * | |
| 383 | - * @return array | |
| 384 | - */ | |
| 385 | 317 | public static function get_entry_meta_info( $entry_id ) { |
| 386 | 318 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 387 | 319 | } |
| 388 | 320 | |
| @@ -390,10 +322,9 @@ | ||
| 390 | 322 | * @param array $where |
| 391 | 323 | * @param string $order_by |
| 392 | 324 | * @param string $limit |
| 393 | 325 | * @param bool $stripslashes |
| 394 | - * | |
| 395 | - * @return mixed | |
| 326 | + * @return array | |
| 396 | 327 | */ |
| 397 | 328 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 398 | 329 | global $wpdb; |
| 399 | 330 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| @@ -416,17 +347,8 @@ | ||
| 416 | 347 | |
| 417 | 348 | return $results; |
| 418 | 349 | } |
| 419 | 350 | |
| 420 | - /** | |
| 421 | - * @param array|string $where | |
| 422 | - * @param string $order_by | |
| 423 | - * @param string $limit | |
| 424 | - * @param bool $unique | |
| 425 | - * @param array $args | |
| 426 | - * | |
| 427 | - * @return array|string|null | |
| 428 | - */ | |
| 429 | 351 | public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) { |
| 430 | 352 | $defaults = array( |
| 431 | 353 | 'is_draft' => false, |
| 432 | 354 | 'user_id' => '', |
| @@ -448,9 +370,8 @@ | ||
| 448 | 370 | * If a child entry id is matched, its parent will be returned in its place |
| 449 | 371 | * |
| 450 | 372 | * @param array $query |
| 451 | 373 | * @param array $args |
| 452 | - * | |
| 453 | 374 | * @return array |
| 454 | 375 | */ |
| 455 | 376 | public static function get_top_level_entry_ids( $query, $args ) { |
| 456 | 377 | $args['return_parent_id_if_0_return_id'] = true; |
| @@ -457,51 +378,11 @@ | ||
| 457 | 378 | return self::getEntryIds( $query, '', '', true, $args ); |
| 458 | 379 | } |
| 459 | 380 | |
| 460 | 381 | /** |
| 461 | - * Returns true if the where clause refers to a field table column that is not form_id. It also updates | |
| 462 | - * the where clause to refer to the entry table for form_id if fields table should not be joined. | |
| 463 | - * | |
| 464 | - * @since 6.16.1 | |
| 465 | - * | |
| 466 | 382 | * @param array|string $where |
| 467 | - * | |
| 468 | - * @return bool | |
| 469 | - */ | |
| 470 | - private static function should_join_fields_table( &$where ) { | |
| 471 | - if ( is_string( $where ) ) { | |
| 472 | - if ( preg_match( '/\bfi\.(?!form_id)\w+/i', $where ) ) { | |
| 473 | - return true; | |
| 474 | - } | |
| 475 | - | |
| 476 | - $where = str_ireplace( 'fi.form_id', 'e.form_id', $where ); | |
| 477 | - return false; | |
| 478 | - } | |
| 479 | - | |
| 480 | - $where_fields = array_keys( $where ); | |
| 481 | - | |
| 482 | - foreach ( $where_fields as $where_field ) { | |
| 483 | - if ( strpos( $where_field, 'fi.' ) === 0 && 'fi.form_id' !== $where_field ) { | |
| 484 | - return true; | |
| 485 | - } | |
| 486 | - } | |
| 487 | - | |
| 488 | - if ( isset( $where['fi.form_id'] ) ) { | |
| 489 | - $where['e.form_id'] = $where['fi.form_id']; | |
| 490 | - unset( $where['fi.form_id'] ); | |
| 491 | - } | |
| 492 | - return false; | |
| 493 | - } | |
| 494 | - | |
| 495 | - /** | |
| 496 | - * @param array|string $where | |
| 497 | 383 | * @param string $order_by |
| 498 | 384 | * @param string $limit |
| 499 | - * @param bool $unique | |
| 500 | - * @param array $args | |
| 501 | - * @param array $query | |
| 502 | - * | |
| 503 | - * @return void | |
| 504 | 385 | */ |
| 505 | 386 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
| 506 | 387 | global $wpdb; |
| 507 | 388 | $query[] = 'SELECT'; |
| @@ -522,20 +403,11 @@ | ||
| 522 | 403 | } else { |
| 523 | 404 | $query[] = 'it.item_id'; |
| 524 | 405 | } |
| 525 | 406 | |
| 526 | - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 407 | + $query[] = 'FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 527 | 408 | |
| 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 ) { | |
| 532 | - $from .= ' LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 533 | - } | |
| 534 | - | |
| 535 | - $query[] = $from; | |
| 536 | 409 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 537 | - | |
| 538 | 410 | if ( is_array( $where ) ) { |
| 539 | 411 | if ( ! $args['is_draft'] ) { |
| 540 | 412 | $where['e.is_draft'] = 0; |
| 541 | 413 | } elseif ( is_numeric( $args['is_draft'] ) ) { |
| @@ -556,9 +428,8 @@ | ||
| 556 | 428 | return $total; |
| 557 | 429 | }, |
| 558 | 430 | array() |
| 559 | 431 | ); |
| 560 | - | |
| 561 | 432 | if ( $is_draft ) { |
| 562 | 433 | $where['e.is_draft'] = $is_draft; |
| 563 | 434 | } |
| 564 | 435 | }//end if |
| @@ -576,9 +447,8 @@ | ||
| 576 | 447 | }//end if |
| 577 | 448 | |
| 578 | 449 | $draft_where = ''; |
| 579 | 450 | $user_where = ''; |
| 580 | - | |
| 581 | 451 | if ( ! $args['is_draft'] ) { |
| 582 | 452 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 ); |
| 583 | 453 | } elseif ( $args['is_draft'] == 1 ) { |
| 584 | 454 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 ); |
| @@ -601,28 +471,18 @@ | ||
| 601 | 471 | // The query has already been prepared |
| 602 | 472 | $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 603 | 473 | } |
| 604 | 474 | |
| 605 | - /** | |
| 606 | - * @param array|string $search | |
| 607 | - * @param int|string $field_id | |
| 608 | - * @param string $operator | |
| 609 | - * | |
| 610 | - * @return array | |
| 611 | - */ | |
| 612 | 475 | public static function search_entry_metas( $search, $field_id, $operator ) { |
| 613 | 476 | $cache_key = 'search_' . FrmAppHelper::maybe_json_encode( $search ) . $field_id . $operator; |
| 614 | 477 | $results = wp_cache_get( $cache_key, 'frm_entry' ); |
| 615 | - | |
| 616 | 478 | if ( false !== $results ) { |
| 617 | 479 | return $results; |
| 618 | 480 | } |
| 619 | 481 | |
| 620 | 482 | global $wpdb; |
| 621 | - | |
| 622 | 483 | if ( is_array( $search ) ) { |
| 623 | 484 | $where = ''; |
| 624 | - | |
| 625 | 485 | foreach ( $search as $field => $value ) { |
| 626 | 486 | if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) { |
| 627 | 487 | continue; |
| 628 | 488 | } |
| @@ -636,12 +496,10 @@ | ||
| 636 | 496 | break; |
| 637 | 497 | case 'day': |
| 638 | 498 | $value = '%' . $value . '%'; |
| 639 | 499 | } |
| 640 | - | |
| 641 | 500 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 642 | 501 | } |
| 643 | - | |
| 644 | 502 | $where .= $wpdb->prepare( ' field_id=%d', $field_id ); |
| 645 | 503 | $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); |
| 646 | 504 | } else { |
| 647 | 505 | if ( $operator === 'LIKE' ) { |
| @@ -646,9 +504,8 @@ | ||
| 646 | 504 | } else { |
| 647 | 505 | if ( $operator === 'LIKE' ) { |
| 648 | 506 | $search = '%' . $search . '%'; |
| 649 | 507 | } |
| 650 | - | |
| 651 | 508 | $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 | 509 | }//end if |
| 653 | 510 | |
| 654 | 511 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |