| @@ -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 | */ |
| @@ -139,9 +140,9 @@ | ||
| 139 | 140 | } else { |
| 140 | 141 | // if value does not exist, then create it |
| 141 | 142 | self::add_entry_meta( $entry_id, $field_id, '', $meta_value ); |
| 142 | 143 | } |
| 143 | - } | |
| 144 | + }//end foreach | |
| 144 | 145 | |
| 145 | 146 | if ( empty( $prev_values ) ) { |
| 146 | 147 | return; |
| 147 | 148 | } |
| @@ -201,15 +202,18 @@ | ||
| 201 | 202 | } |
| 202 | 203 | |
| 203 | 204 | /** |
| 204 | 205 | * @since 2.0.9 |
| 206 | + * | |
| 207 | + * @param stdClass $entry | |
| 208 | + * @param int|string $field_id | |
| 209 | + * @return mixed | |
| 205 | 210 | */ |
| 206 | 211 | public static function get_meta_value( $entry, $field_id ) { |
| 207 | 212 | if ( isset( $entry->metas ) ) { |
| 208 | 213 | 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 | 214 | } |
| 215 | + return self::get_entry_meta_by_field( $entry->id, $field_id ); | |
| 212 | 216 | } |
| 213 | 217 | |
| 214 | 218 | public static function get_entry_meta_by_field( $entry_id, $field_id ) { |
| 215 | 219 | global $wpdb; |
| @@ -233,14 +237,17 @@ | ||
| 233 | 237 | $query = array( 'item_id' => $entry_id ); |
| 234 | 238 | if ( is_numeric( $field_id ) ) { |
| 235 | 239 | $query['field_id'] = $field_id; |
| 236 | 240 | } else { |
| 237 | - $get_table .= ' it LEFT OUTER 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'; | |
| 238 | 242 | $query['fi.field_key'] = $field_id; |
| 239 | 243 | } |
| 240 | 244 | |
| 241 | 245 | $result = FrmDb::get_var( $get_table, $query, 'meta_value' ); |
| 242 | - FrmAppHelper::unserialize_or_decode( $result ); | |
| 246 | + | |
| 247 | + $field_type = FrmField::get_type( $field_id ); | |
| 248 | + FrmFieldsHelper::prepare_field_value( $result, $field_type ); | |
| 249 | + | |
| 243 | 250 | $result = wp_unslash( $result ); |
| 244 | 251 | |
| 245 | 252 | return $result; |
| 246 | 253 | } |
| @@ -274,10 +281,13 @@ | ||
| 274 | 281 | return wp_unslash( $values ); |
| 275 | 282 | } |
| 276 | 283 | |
| 277 | 284 | /** |
| 278 | - * @param string $order | |
| 279 | - * @param string $limit | |
| 285 | + * @param int|string $field_id | |
| 286 | + * @param string $order | |
| 287 | + * @param string $limit | |
| 288 | + * @param array $args | |
| 289 | + * @param array $query | |
| 280 | 290 | */ |
| 281 | 291 | private static function meta_field_query( $field_id, $order, $limit, $args, array &$query ) { |
| 282 | 292 | global $wpdb; |
| 283 | 293 | $query[] = 'SELECT'; |
| @@ -307,8 +317,15 @@ | ||
| 307 | 317 | public static function get_entry_meta_info( $entry_id ) { |
| 308 | 318 | return FrmDb::get_results( 'frm_item_metas', array( 'item_id' => $entry_id ) ); |
| 309 | 319 | } |
| 310 | 320 | |
| 321 | + /** | |
| 322 | + * @param array $where | |
| 323 | + * @param string $order_by | |
| 324 | + * @param string $limit | |
| 325 | + * @param bool $stripslashes | |
| 326 | + * @return array | |
| 327 | + */ | |
| 311 | 328 | public static function getAll( $where = array(), $order_by = '', $limit = '', $stripslashes = false ) { |
| 312 | 329 | global $wpdb; |
| 313 | 330 | $query = 'SELECT it.*, fi.type as field_type, fi.field_key as field_key, |
| 314 | 331 | fi.required as required, fi.form_id as field_form_id, fi.name as field_name, fi.options as fi_options |
| @@ -361,11 +378,11 @@ | ||
| 361 | 378 | return self::getEntryIds( $query, '', '', true, $args ); |
| 362 | 379 | } |
| 363 | 380 | |
| 364 | 381 | /** |
| 365 | - * @param string|array $where | |
| 366 | - * @param string $order_by | |
| 367 | - * @param string $limit | |
| 382 | + * @param array|string $where | |
| 383 | + * @param string $order_by | |
| 384 | + * @param string $limit | |
| 368 | 385 | */ |
| 369 | 386 | private static function get_ids_query( $where, $order_by, $limit, $unique, $args, array &$query ) { |
| 370 | 387 | global $wpdb; |
| 371 | 388 | $query[] = 'SELECT'; |
| @@ -392,11 +409,31 @@ | ||
| 392 | 409 | $query[] = 'INNER JOIN ' . $wpdb->prefix . 'frm_items e ON (e.id=it.item_id)'; |
| 393 | 410 | if ( is_array( $where ) ) { |
| 394 | 411 | if ( ! $args['is_draft'] ) { |
| 395 | 412 | $where['e.is_draft'] = 0; |
| 396 | - } elseif ( $args['is_draft'] == 1 ) { | |
| 397 | - $where['e.is_draft'] = 1; | |
| 398 | - } | |
| 413 | + } elseif ( is_numeric( $args['is_draft'] ) ) { | |
| 414 | + if ( class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 415 | + $where['e.is_draft'] = absint( $args['is_draft'] ); | |
| 416 | + } else { | |
| 417 | + $where['e.is_draft'] = 1; | |
| 418 | + } | |
| 419 | + } elseif ( 'both' === $args['is_draft'] && class_exists( 'FrmAbandonmentHooksController', false ) ) { | |
| 420 | + $where['e.is_draft'] = array( 0, 1 ); | |
| 421 | + } elseif ( false !== strpos( $args['is_draft'], ',' ) ) { | |
| 422 | + $is_draft = array_reduce( | |
| 423 | + explode( ',', $args['is_draft'] ), | |
| 424 | + function ( $total, $current ) { | |
| 425 | + if ( is_numeric( $current ) ) { | |
| 426 | + $total[] = absint( $current ); | |
| 427 | + } | |
| 428 | + return $total; | |
| 429 | + }, | |
| 430 | + array() | |
| 431 | + ); | |
| 432 | + if ( $is_draft ) { | |
| 433 | + $where['e.is_draft'] = $is_draft; | |
| 434 | + } | |
| 435 | + }//end if | |
| 399 | 436 | |
| 400 | 437 | if ( ! empty( $args['user_id'] ) ) { |
| 401 | 438 | $where['e.user_id'] = $args['user_id']; |
| 402 | 439 | } |
| @@ -406,9 +443,9 @@ | ||
| 406 | 443 | $query[] = ' GROUP BY ' . sanitize_text_field( $args['group_by'] ); |
| 407 | 444 | } |
| 408 | 445 | |
| 409 | 446 | return; |
| 410 | - } | |
| 447 | + }//end if | |
| 411 | 448 | |
| 412 | 449 | $draft_where = ''; |
| 413 | 450 | $user_where = ''; |
| 414 | 451 | if ( ! $args['is_draft'] ) { |
| @@ -462,15 +499,15 @@ | ||
| 462 | 499 | } |
| 463 | 500 | $where .= $wpdb->prepare( ' meta_value ' . $operator . ' %s and', $value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 464 | 501 | } |
| 465 | 502 | $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 ); | |
| 503 | + $query = 'SELECT DISTINCT item_id FROM ' . $wpdb->prefix . 'frm_item_metas' . FrmDb::prepend_and_or_where( ' WHERE ', $where ); | |
| 467 | 504 | } else { |
| 468 | - if ( $operator == 'LIKE' ) { | |
| 505 | + if ( $operator === 'LIKE' ) { | |
| 469 | 506 | $search = '%' . $search . '%'; |
| 470 | 507 | } |
| 471 | 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 |
| 472 | - } | |
| 509 | + }//end if | |
| 473 | 510 | |
| 474 | 511 | $results = $wpdb->get_col( $query, 0 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 475 | 512 | FrmDb::set_cache( $cache_key, $results, 'frm_entry' ); |
| 476 | 513 | |