| @@ -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'] ); |
| @@ -122,9 +110,8 @@ | ||
| 122 | 110 | |
| 123 | 111 | /** |
| 124 | 112 | * @param int|string $entry_id |
| 125 | 113 | * @param array $values Either indexed by field ID or field key. |
| 126 | - * | |
| 127 | 114 | * @return void |
| 128 | 115 | */ |
| 129 | 116 | public static function update_entry_metas( $entry_id, $values ) { |
| 130 | 117 | global $wpdb; |
| @@ -138,9 +125,8 @@ | ||
| 138 | 125 | 'field_id' |
| 139 | 126 | ); |
| 140 | 127 | |
| 141 | 128 | $values_indexed_by_field_id = array(); |
| 142 | - | |
| 143 | 129 | foreach ( $values as $field_id_or_key => $meta_value ) { |
| 144 | 130 | $field_id = $field_id_or_key; |
| 145 | 131 | $field = null; |
| 146 | 132 | |
| @@ -157,9 +143,9 @@ | ||
| 157 | 143 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 158 | 144 | |
| 159 | 145 | if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { |
| 160 | 146 | |
| 161 | - if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) === '' ) ) { | |
| 147 | + if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { | |
| 162 | 148 | // Remove blank fields. |
| 163 | 149 | unset( $values_indexed_by_field_id[ $field_id ] ); |
| 164 | 150 | } else { |
| 165 | 151 | // if value exists, then update it |
| @@ -192,14 +178,8 @@ | ||
| 192 | 178 | $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'frm_item_metas ' . $where['where'], $where['values'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 193 | 179 | self::clear_cache(); |
| 194 | 180 | } |
| 195 | 181 | |
| 196 | - /** | |
| 197 | - * @param int $old_id | |
| 198 | - * @param int $new_id | |
| 199 | - * | |
| 200 | - * @return void | |
| 201 | - */ | |
| 202 | 182 | public static function duplicate_entry_metas( $old_id, $new_id ) { |
| 203 | 183 | $metas = self::get_entry_meta_info( $old_id ); |
| 204 | 184 | |
| 205 | 185 | /** |
| @@ -209,9 +189,8 @@ | ||
| 209 | 189 | * |
| 210 | 190 | * @param array $metas The list of entry meta values. |
| 211 | 191 | */ |
| 212 | 192 | $metas = apply_filters( 'frm_before_duplicate_entry_values', $metas ); |
| 213 | - | |
| 214 | 193 | foreach ( $metas as $meta ) { |
| 215 | 194 | self::add_entry_meta( $new_id, $meta->field_id, '', $meta->meta_value ); |
| 216 | 195 | unset( $meta ); |
| 217 | 196 | } |
| @@ -217,14 +196,8 @@ | ||
| 217 | 196 | } |
| 218 | 197 | self::clear_cache(); |
| 219 | 198 | } |
| 220 | 199 | |
| 221 | - /** | |
| 222 | - * @param int $entry_id | |
| 223 | - * @param int $field_id | |
| 224 | - * | |
| 225 | - * @return false|int | |
| 226 | - */ | |
| 227 | 200 | public static function delete_entry_meta( $entry_id, $field_id ) { |
| 228 | 201 | global $wpdb; |
| 229 | 202 | self::clear_cache(); |
| 230 | 203 | |
| @@ -235,10 +208,8 @@ | ||
| 235 | 208 | * Clear entry meta caching |
| 236 | 209 | * Called when a meta is added or changed |
| 237 | 210 | * |
| 238 | 211 | * @since 2.0.5 |
| 239 | - * | |
| 240 | - * @return void | |
| 241 | 212 | */ |
| 242 | 213 | public static function clear_cache() { |
| 243 | 214 | FrmDb::cache_delete_group( 'frm_entry_meta' ); |
| 244 | 215 | FrmDb::cache_delete_group( 'frm_item_meta' ); |
| @@ -248,14 +219,13 @@ | ||
| 248 | 219 | * @since 2.0.9 |
| 249 | 220 | * |
| 250 | 221 | * @param stdClass $entry |
| 251 | 222 | * @param int|string $field_id |
| 252 | - * | |
| 253 | 223 | * @return mixed |
| 254 | 224 | */ |
| 255 | 225 | public static function get_meta_value( $entry, $field_id ) { |
| 256 | 226 | if ( isset( $entry->metas ) ) { |
| 257 | - return $entry->metas[ $field_id ] ?? false; | |
| 227 | + return isset( $entry->metas[ $field_id ] ) ? $entry->metas[ $field_id ] : false; | |
| 258 | 228 | } |
| 259 | 229 | return self::get_entry_meta_by_field( $entry->id, $field_id ); |
| 260 | 230 | } |
| 261 | 231 | |
| @@ -261,9 +231,8 @@ | ||
| 261 | 231 | |
| 262 | 232 | /** |
| 263 | 233 | * @param int|object|string $entry_id |
| 264 | 234 | * @param int|string $field_id This function supports field keys as field id. |
| 265 | - * | |
| 266 | 235 | * @return mixed |
| 267 | 236 | */ |
| 268 | 237 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 269 | 238 | global $wpdb; |
| @@ -284,9 +253,8 @@ | ||
| 284 | 253 | } |
| 285 | 254 | |
| 286 | 255 | $get_table = $wpdb->prefix . 'frm_item_metas'; |
| 287 | 256 | $query = array( 'item_id' => $entry_id ); |
| 288 | - | |
| 289 | 257 | if ( is_numeric( $field_id ) ) { |
| 290 | 258 | // Query by field ID. |
| 291 | 259 | $query['field_id'] = $field_id; |
| 292 | 260 | } else { |
| @@ -304,16 +272,8 @@ | ||
| 304 | 272 | |
| 305 | 273 | return $result; |
| 306 | 274 | } |
| 307 | 275 | |
| 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 | 276 | public static function get_entry_metas_for_field( $field_id, $order = '', $limit = '', $args = array() ) { |
| 317 | 277 | $defaults = array( |
| 318 | 278 | 'value' => false, |
| 319 | 279 | 'unique' => false, |
| @@ -347,10 +307,8 @@ | ||
| 347 | 307 | * @param string $order |
| 348 | 308 | * @param string $limit |
| 349 | 309 | * @param array $args |
| 350 | 310 | * @param array $query |
| 351 | - * | |
| 352 | - * @return void | |
| 353 | 311 | */ |
| 354 | 312 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 355 | 313 | global $wpdb; |
| 356 | 314 | $query[] = 'SELECT'; |
| @@ -376,13 +334,8 @@ | ||
| 376 | 334 | } |
| 377 | 335 | $query[] = $order . $limit; |
| 378 | 336 | } |
| 379 | 337 | |
| 380 | - /** | |
| 381 | - * @param int|string $entry_id | |
| 382 | - * | |
| 383 | - * @return array | |
| 384 | - */ | |
| 385 | 338 | public static function get_entry_meta_info( $entry_id ) { |
| 386 | 339 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 387 | 340 | } |
| 388 | 341 | |
| @@ -390,10 +343,9 @@ | ||
| 390 | 343 | * @param array $where |
| 391 | 344 | * @param string $order_by |
| 392 | 345 | * @param string $limit |
| 393 | 346 | * @param bool $stripslashes |
| 394 | - * | |
| 395 | - * @return mixed | |
| 347 | + * @return array | |
| 396 | 348 | */ |
| 397 | 349 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 398 | 350 | global $wpdb; |
| 399 | 351 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| @@ -416,17 +368,8 @@ | ||
| 416 | 368 | |
| 417 | 369 | return $results; |
| 418 | 370 | } |
| 419 | 371 | |
| 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 | 372 | public static function getEntryIds( $where = array(), $order_by = '', $limit = '', $unique = true, $args = array() ) { |
| 430 | 373 | $defaults = array( |
| 431 | 374 | 'is_draft' => false, |
| 432 | 375 | 'user_id' => '', |
| @@ -448,9 +391,8 @@ | ||
| 448 | 391 | * If a child entry id is matched, its parent will be returned in its place |
| 449 | 392 | * |
| 450 | 393 | * @param array $query |
| 451 | 394 | * @param array $args |
| 452 | - * | |
| 453 | 395 | * @return array |
| 454 | 396 | */ |
| 455 | 397 | public static function get_top_level_entry_ids( $query, $args ) { |
| 456 | 398 | $args['return_parent_id_if_0_return_id'] = true; |
| @@ -457,51 +399,11 @@ | ||
| 457 | 399 | return self::getEntryIds( $query, '', '', true, $args ); |
| 458 | 400 | } |
| 459 | 401 | |
| 460 | 402 | /** |
| 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 | 403 | * @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 | 404 | * @param string $order_by |
| 498 | 405 | * @param string $limit |
| 499 | - * @param bool $unique | |
| 500 | - * @param array $args | |
| 501 | - * @param array $query | |
| 502 | - * | |
| 503 | - * @return void | |
| 504 | 406 | */ |
| 505 | 407 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
| 506 | 408 | global $wpdb; |
| 507 | 409 | $query[] = 'SELECT'; |
| @@ -522,20 +424,11 @@ | ||
| 522 | 424 | } else { |
| 523 | 425 | $query[] = 'it.item_id'; |
| 524 | 426 | } |
| 525 | 427 | |
| 526 | - $from = 'FROM ' . $wpdb->prefix . 'frm_item_metas it'; | |
| 428 | + $query[] = 'FROM ' . $wpdb->prefix . 'frm_item_metas it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 527 | 429 | |
| 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 | 430 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 537 | - | |
| 538 | 431 | if ( is_array( $where ) ) { |
| 539 | 432 | if ( ! $args['is_draft'] ) { |
| 540 | 433 | $where['e.is_draft'] = 0; |
| 541 | 434 | } elseif ( is_numeric( $args['is_draft'] ) ) { |
| @@ -556,9 +449,8 @@ | ||
| 556 | 449 | return $total; |
| 557 | 450 | }, |
| 558 | 451 | array() |
| 559 | 452 | ); |
| 560 | - | |
| 561 | 453 | if ( $is_draft ) { |
| 562 | 454 | $where['e.is_draft'] = $is_draft; |
| 563 | 455 | } |
| 564 | 456 | }//end if |
| @@ -576,9 +468,8 @@ | ||
| 576 | 468 | }//end if |
| 577 | 469 | |
| 578 | 470 | $draft_where = ''; |
| 579 | 471 | $user_where = ''; |
| 580 | - | |
| 581 | 472 | if ( ! $args['is_draft'] ) { |
| 582 | 473 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 0 ); |
| 583 | 474 | } elseif ( $args['is_draft'] == 1 ) { |
| 584 | 475 | $draft_where = $wpdb->prepare( ' AND e.is_draft=%d', 1 ); |
| @@ -601,28 +492,18 @@ | ||
| 601 | 492 | // The query has already been prepared |
| 602 | 493 | $query[] = FrmDb::prepend_and_or_where( ' WHERE ', $where ) . $order_by . $limit; |
| 603 | 494 | } |
| 604 | 495 | |
| 605 | - /** | |
| 606 | - * @param array|string $search | |
| 607 | - * @param int|string $field_id | |
| 608 | - * @param string $operator | |
| 609 | - * | |
| 610 | - * @return array | |
| 611 | - */ | |
| 612 | 496 | public static function search_entry_metas( $search, $field_id, $operator ) { |
| 613 | 497 | $cache_key = 'search_' . FrmAppHelper::maybe_json_encode( $search ) . $field_id . $operator; |
| 614 | 498 | $results = wp_cache_get( $cache_key, 'frm_entry' ); |
| 615 | - | |
| 616 | 499 | if ( false !== $results ) { |
| 617 | 500 | return $results; |
| 618 | 501 | } |
| 619 | 502 | |
| 620 | 503 | global $wpdb; |
| 621 | - | |
| 622 | 504 | if ( is_array( $search ) ) { |
| 623 | 505 | $where = ''; |
| 624 | - | |
| 625 | 506 | foreach ( $search as $field => $value ) { |
| 626 | 507 | if ( $value <= 0 || ! in_array( $field, array( 'year', 'month', 'day' ) ) ) { |
| 627 | 508 | continue; |
| 628 | 509 | } |
| @@ -636,12 +517,10 @@ | ||
| 636 | 517 | break; |
| 637 | 518 | case 'day': |
| 638 | 519 | $value = '%' . $value . '%'; |
| 639 | 520 | } |
| 640 | - | |
| 641 | 521 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 642 | 522 | } |
| 643 | - | |
| 644 | 523 | $where .= $wpdb->prepare( ' field_id=%d', $field_id ); |
| 645 | 524 | $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); |
| 646 | 525 | } else { |
| 647 | 526 | if ( $operator === 'LIKE' ) { |
| @@ -646,9 +525,8 @@ | ||
| 646 | 525 | } else { |
| 647 | 526 | if ( $operator === 'LIKE' ) { |
| 648 | 527 | $search = '%' . $search . '%'; |
| 649 | 528 | } |
| 650 | - | |
| 651 | 529 | $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 | 530 | }//end if |
| 653 | 531 | |
| 654 | 532 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |