| @@ -9,8 +9,9 @@ | ||
| 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 | |
| 13 | 14 | */ |
| 14 | 15 | public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_value ) { |
| 15 | 16 | global $wpdb; |
| 16 | 17 | |
| @@ -42,11 +43,11 @@ | ||
| 42 | 43 | return $id; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | 46 | /** |
| 46 | - * @param int $entry_id | |
| 47 | - * @param int $field_id | |
| 48 | - * @param string $meta_key deprecated | |
| 47 | + * @param int $entry_id | |
| 48 | + * @param int $field_id | |
| 49 | + * @param string $meta_key Deprecated. | |
| 49 | 50 | * @param array|string $meta_value |
| 50 | 51 | * |
| 51 | 52 | * @return bool|false|int |
| 52 | 53 | */ |
| @@ -106,13 +107,18 @@ | ||
| 106 | 107 | |
| 107 | 108 | $value = apply_filters( 'frm_prepare_data_before_db', $value, $atts['field_id'], $atts['entry_id'], array( 'field' => $atts['field'] ) ); |
| 108 | 109 | } |
| 109 | 110 | |
| 111 | + /** | |
| 112 | + * @param int|string $entry_id | |
| 113 | + * @param array $values Either indexed by field ID or field key. | |
| 114 | + * @return void | |
| 115 | + */ | |
| 110 | 116 | public static function update_entry_metas( $entry_id, $values ) { |
| 111 | 117 | global $wpdb; |
| 112 | 118 | |
| 113 | - $prev_values = FrmDb::get_col( | |
| 114 | - $wpdb->prefix . 'frm_item_metas', | |
| 119 | + $previous_field_ids = FrmDb::get_col( | |
| 120 | + 'frm_item_metas', | |
| 115 | 121 | array( |
| 116 | 122 | 'item_id' => $entry_id, |
| 117 | 123 | 'field_id !' => 0, |
| 118 | 124 | ), |
| @@ -118,21 +124,30 @@ | ||
| 118 | 124 | ), |
| 119 | 125 | 'field_id' |
| 120 | 126 | ); |
| 121 | 127 | |
| 122 | - foreach ( $values as $field_id => $meta_value ) { | |
| 123 | - $field = false; | |
| 124 | - if ( ! empty( $field_id ) ) { | |
| 125 | - $field = FrmField::getOne( $field_id ); | |
| 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 | + } | |
| 126 | 139 | } |
| 127 | 140 | |
| 141 | + $values_indexed_by_field_id[ $field_id ] = $meta_value; | |
| 142 | + | |
| 128 | 143 | self::get_value_to_save( compact( 'field', 'field_id', 'entry_id' ), $meta_value ); |
| 129 | 144 | |
| 130 | - if ( $prev_values && in_array( $field_id, $prev_values ) ) { | |
| 145 | + if ( $previous_field_ids && in_array( $field_id, $previous_field_ids ) ) { | |
| 131 | 146 | |
| 132 | 147 | if ( ( is_array( $meta_value ) && empty( $meta_value ) ) || ( ! is_array( $meta_value ) && trim( $meta_value ) == '' ) ) { |
| 133 | - // remove blank fields | |
| 134 | - unset( $values[ $field_id ] ); | |
| 148 | + // Remove blank fields. | |
| 149 | + unset( $values_indexed_by_field_id[ $field_id ] ); | |
| 135 | 150 | } else { |
| 136 | 151 | // if value exists, then update it |
| 137 | 152 | self::update_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 138 | 153 | } |
| @@ -139,17 +154,17 @@ | ||
| 139 | 154 | } else { |
| 140 | 155 | // if value does not exist, then create it |
| 141 | 156 | self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 142 | 157 | } |
| 143 | - } | |
| 158 | + }//end foreach | |
| 144 | 159 | |
| 145 | - if ( empty( $prev_values ) ) { | |
| 160 | + if ( empty( $previous_field_ids ) ) { | |
| 146 | 161 | return; |
| 147 | 162 | } |
| 148 | 163 | |
| 149 | - $prev_values = array_diff( $prev_values, array_keys( $values ) ); | |
| 164 | + $field_ids_to_remove = array_diff( $previous_field_ids, array_keys( $values_indexed_by_field_id ) ); | |
| 150 | 165 | |
| 151 | - if ( empty( $prev_values ) ) { | |
| 166 | + if ( ! $field_ids_to_remove ) { | |
| 152 | 167 | return; |
| 153 | 168 | } |
| 154 | 169 | |
| 155 | 170 | // prepare the query |
| @@ -154,9 +169,9 @@ | ||
| 154 | 169 | |
| 155 | 170 | // prepare the query |
| 156 | 171 | $where = array( |
| 157 | 172 | 'item_id' => $entry_id, |
| 158 | - 'field_id' => $prev_values, | |
| 173 | + 'field_id' => $field_ids_to_remove, | |
| 159 | 174 | ); |
| 160 | 175 | FrmDb::get_where_clause_and_values( $where ); |
| 161 | 176 | |
| 162 | 177 | // Delete any leftovers |
| @@ -201,15 +216,18 @@ | ||
| 201 | 216 | } |
| 202 | 217 | |
| 203 | 218 | /** |
| 204 | 219 | * @since 2.0.9 |
| 220 | + * | |
| 221 | + * @param stdClass $entry | |
| 222 | + * @param int|string $field_id | |
| 223 | + * @return mixed | |
| 205 | 224 | */ |
| 206 | 225 | public static function get_meta_value( $entry, $field_id ) { |
| 207 | 226 | if ( isset( $entry->metas ) ) { |
| 208 | 227 | 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 ); | |
| 211 | 228 | } |
| 229 | + return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 212 | 230 | } |
| 213 | 231 | |
| 214 | 232 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 215 | 233 | global $wpdb; |
| @@ -233,14 +251,17 @@ | ||
| 233 | 251 | $query = array( 'item_id' => $entry_id ); |
| 234 | 252 | if ( is_numeric( $field_id ) ) { |
| 235 | 253 | $query['field_id'] = $field_id; |
| 236 | 254 | } else { |
| 237 | - $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 255 | + $get_table .= ' it LEFT OUTER JOIN ' . $wpdb->prefix . 'frm_fields fi ON it.field_id=fi.id'; | |
| 238 | 256 | $query['fi.field_key'] = $field_id; |
| 239 | 257 | } |
| 240 | 258 | |
| 241 | 259 | $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); |
| 242 | - FrmAppHelper::unserialize_or_decode( $result ); | |
| 260 | + | |
| 261 | + $field_type = FrmField::get_type( $field_id ); | |
| 262 | + FrmFieldsHelper::prepare_field_value( $result, $field_type ); | |
| 263 | + | |
| 243 | 264 | $result = wp_unslash( $result ); |
| 244 | 265 | |
| 245 | 266 | return $result; |
| 246 | 267 | } |
| @@ -274,10 +295,13 @@ | ||
| 274 | 295 | return wp_unslash( $values ); |
| 275 | 296 | } |
| 276 | 297 | |
| 277 | 298 | /** |
| 278 | - * @param string $order | |
| 279 | - * @param string $limit | |
| 299 | + * @param int|string $field_id | |
| 300 | + * @param string $order | |
| 301 | + * @param string $limit | |
| 302 | + * @param array $args | |
| 303 | + * @param array $query | |
| 280 | 304 | */ |
| 281 | 305 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 282 | 306 | global $wpdb; |
| 283 | 307 | $query[] = 'SELECT'; |
| @@ -307,8 +331,15 @@ | ||
| 307 | 331 | public static function get_entry_meta_info( $entry_id ) { |
| 308 | 332 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 309 | 333 | } |
| 310 | 334 | |
| 335 | + /** | |
| 336 | + * @param array $where | |
| 337 | + * @param string $order_by | |
| 338 | + * @param string $limit | |
| 339 | + * @param bool $stripslashes | |
| 340 | + * @return array | |
| 341 | + */ | |
| 311 | 342 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 312 | 343 | global $wpdb; |
| 313 | 344 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 314 | 345 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| @@ -361,11 +392,11 @@ | ||
| 361 | 392 | return self::getEntryIds( $query, '', '', true, $args ); |
| 362 | 393 | } |
| 363 | 394 | |
| 364 | 395 | /** |
| 365 | - * @param string|array $where | |
| 366 | - * @param string $order_by | |
| 367 | - * @param string $limit | |
| 396 | + * @param array|string $where | |
| 397 | + * @param string $order_by | |
| 398 | + * @param string $limit | |
| 368 | 399 | */ |
| 369 | 400 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
| 370 | 401 | global $wpdb; |
| 371 | 402 | $query[] = 'SELECT'; |
| @@ -392,11 +423,31 @@ | ||
| 392 | 423 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 393 | 424 | if ( is_array( $where ) ) { |
| 394 | 425 | if ( ! $args['is_draft'] ) { |
| 395 | 426 | $where['e.is_draft'] = 0; |
| 396 | - } elseif ( $args['is_draft'] == 1 ) { | |
| 397 | - $where['e.is_draft'] = 1; | |
| 398 | - } | |
| 427 | + } elseif ( is_numeric( $args['is_draft'] ) ) { | |
| 428 | + if ( class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 429 | + $where['e.is_draft'] = absint( $args['is_draft'] ); | |
| 430 | + } else { | |
| 431 | + $where['e.is_draft'] = 1; | |
| 432 | + } | |
| 433 | + } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 434 | + $where['e.is_draft'] = array( 0, 1 ); | |
| 435 | + } elseif ( false !== strpos( $args['is_draft'], ',' ) ) { | |
| 436 | + $is_draft = array_reduce( | |
| 437 | + explode( ',', $args['is_draft'] ), | |
| 438 | + function ( $total, $current ) { | |
| 439 | + if ( is_numeric( $current ) ) { | |
| 440 | + $total[] = absint( $current ); | |
| 441 | + } | |
| 442 | + return $total; | |
| 443 | + }, | |
| 444 | + array() | |
| 445 | + ); | |
| 446 | + if ( $is_draft ) { | |
| 447 | + $where['e.is_draft'] = $is_draft; | |
| 448 | + } | |
| 449 | + }//end if | |
| 399 | 450 | |
| 400 | 451 | if ( ! empty( $args['user_id'] ) ) { |
| 401 | 452 | $where['e.user_id'] = $args['user_id']; |
| 402 | 453 | } |
| @@ -406,9 +457,9 @@ | ||
| 406 | 457 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| 407 | 458 | } |
| 408 | 459 | |
| 409 | 460 | return; |
| 410 | - } | |
| 461 | + }//end if | |
| 411 | 462 | |
| 412 | 463 | $draft_where = ''; |
| 413 | 464 | $user_where = ''; |
| 414 | 465 | if ( ! $args['is_draft'] ) { |
| @@ -462,15 +513,15 @@ | ||
| 462 | 513 | } |
| 463 | 514 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 464 | 515 | } |
| 465 | 516 | $where .= $wpdb->prepare( ' field_id=%d', $field_id ); |
| 466 | - $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 517 | + $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 467 | 518 | } else { |
| 468 | - if ( $operator == 'LIKE' ) { | |
| 519 | + if ( $operator === 'LIKE' ) { | |
| 469 | 520 | $search = '%' . $search . '%'; |
| 470 | 521 | } |
| 471 | 522 | $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 |
| 472 | - } | |
| 523 | + }//end if | |
| 473 | 524 | |
| 474 | 525 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 475 | 526 | FrmDb::set_cache( $cache_key, $results, 'frm_entry' ); |
| 476 | 527 | |