diff --git src/bp-xprofile/bp-xprofile-filters.php src/bp-xprofile/bp-xprofile-filters.php
index dd3f7c567..33997a130 100644
|
|
|
function xprofile_filter_kses( $content, $data_obj = null ) {
|
| 154 | 154 | * Safely runs profile field data through kses and force_balance_tags. |
| 155 | 155 | * |
| 156 | 156 | * @since 1.2.6 |
| | 157 | * @since 3.0.0 The `$field_value` parameter accepts an array. |
| 157 | 158 | * |
| 158 | | * @param string $field_value Field value being santized. |
| 159 | | * @param int $field_id Field ID being sanitized. |
| 160 | | * @param bool $reserialize Whether to reserialize arrays before returning. Defaults to true. |
| 161 | | * @param object|null $data_obj The BP_XProfile_ProfileData object. |
| | 159 | * @param string $field_value Field value being santized. |
| | 160 | * @param int $field_id Field ID being sanitized. |
| | 161 | * @param bool $reserialize Whether to reserialize arrays before returning. Defaults to true. |
| | 162 | * @param object|null $data_obj The BP_XProfile_ProfileData object. |
| | 163 | * @return string|array |
| 162 | 164 | * @return string |
| 163 | 165 | */ |
| 164 | 166 | function xprofile_sanitize_data_value_before_save( $field_value, $field_id = 0, $reserialize = true, $data_obj = null ) { |
| … |
… |
function xprofile_sanitize_data_value_before_save( $field_value, $field_id = 0,
|
| 176 | 178 | // Value might be a serialized array of options. |
| 177 | 179 | $field_value = maybe_unserialize( $field_value ); |
| 178 | 180 | |
| | 181 | // Return if empty. Special-case support for integer 0 for the number field type. |
| | 182 | if ( empty( $field_value ) && ! is_integer( $field_value ) && $field_value !== '0' ) { |
| | 183 | return $field_value; |
| | 184 | } |
| | 185 | |
| 179 | 186 | // Sanitize single field value. |
| 180 | | if ( ! is_array( $field_value ) ) { |
| | 187 | if ( ! $data_obj || ! bp_xprofile_field_supports_multiple_values( $data_obj->field_id ) ) { |
| 181 | 188 | $kses_field_value = xprofile_filter_kses( $field_value, $data_obj ); |
| 182 | 189 | $filtered_field_value = wp_rel_nofollow( force_balance_tags( $kses_field_value ) ); |
| 183 | 190 | |
| … |
… |
function xprofile_sanitize_data_value_before_save( $field_value, $field_id = 0,
|
| 185 | 192 | * Filters the kses-filtered data before saving to database. |
| 186 | 193 | * |
| 187 | 194 | * @since 1.5.0 |
| | 195 | * @since 3.0.0 The `$filtered_field_value` accepts an array. |
| 188 | 196 | * |
| 189 | | * @param string $filtered_field_value The filtered value. |
| | 197 | * @param string|array $filtered_field_value The filtered value. |
| 190 | 198 | * @param string $field_value The original value before filtering. |
| 191 | 199 | * @param BP_XProfile_ProfileData $data_obj The BP_XProfile_ProfileData object. |
| 192 | 200 | */ |
| 193 | 201 | $filtered_field_value = apply_filters( 'xprofile_filtered_data_value_before_save', $filtered_field_value, $field_value, $data_obj ); |
| 194 | 202 | |
| | 203 | if ( ! empty( $reserialize ) ) { |
| | 204 | $filtered_field_value = maybe_serialize( $filtered_field_value ); |
| | 205 | } |
| | 206 | |
| 195 | 207 | // Sanitize multiple individual option values. |
| 196 | 208 | } else { |
| 197 | 209 | $filtered_values = array(); |
| 198 | 210 | foreach ( (array) $field_value as $value ) { |
| 199 | | $kses_field_value = xprofile_filter_kses( $value, $data_obj ); |
| 200 | | $filtered_value = wp_rel_nofollow( force_balance_tags( $kses_field_value ) ); |
| | 211 | $unserialized_value = maybe_unserialize( $value ); |
| | 212 | $kses_field_value = xprofile_filter_kses( $unserialized_value, $data_obj ); |
| | 213 | $filtered_value = wp_rel_nofollow( force_balance_tags( $kses_field_value ) ); |
| 201 | 214 | |
| 202 | 215 | /** This filter is documented in bp-xprofile/bp-xprofile-filters.php */ |
| 203 | 216 | $filtered_values[] = apply_filters( 'xprofile_filtered_data_value_before_save', $filtered_value, $value, $data_obj ); |
| 204 | 217 | } |
| 205 | 218 | |
| 206 | | if ( !empty( $reserialize ) ) { |
| 207 | | $filtered_field_value = serialize( $filtered_values ); |
| | 219 | if ( ! empty( $reserialize ) ) { |
| | 220 | $filtered_field_value = array_map( 'maybe_serialize', $filtered_values ); |
| 208 | 221 | } else { |
| 209 | 222 | $filtered_field_value = $filtered_values; |
| 210 | 223 | } |
diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php
index 337d2cd79..c34625f01 100644
|
|
|
function xprofile_get_field_data( $field, $user_id = 0, $multi_format = 'array'
|
| 362 | 362 | |
| 363 | 363 | $values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $field_id, $user_id ) ); |
| 364 | 364 | |
| 365 | | if ( is_array( $values ) ) { |
| | 365 | // Consider multiple values. |
| | 366 | if ( bp_xprofile_field_supports_multiple_values( $field_id ) ) { |
| | 367 | $values = array_map( 'maybe_unserialize', $values ); |
| 366 | 368 | $data = array(); |
| 367 | 369 | foreach( (array) $values as $value ) { |
| 368 | 370 | |
| … |
… |
function xprofile_set_field_data( $field, $user_id, $value, $is_required = false
|
| 467 | 469 | $field->user_id = $user_id; |
| 468 | 470 | |
| 469 | 471 | // Gets un/reserialized via xprofile_sanitize_data_value_before_save() |
| 470 | | $field->value = maybe_serialize( $value ); |
| | 472 | if ( bp_xprofile_field_supports_multiple_values( $field_id ) ) { |
| | 473 | $field->value = array_map( 'maybe_serialize', $value ); |
| | 474 | } else { |
| | 475 | $field->value = maybe_serialize( $value ); |
| | 476 | } |
| 471 | 477 | |
| 472 | 478 | return $field->save(); |
| 473 | 479 | } |
| … |
… |
function xprofile_get_random_profile_data( $user_id, $exclude_fullname = true )
|
| 641 | 647 | * Formats a profile field according to its type. [ TODO: Should really be moved to filters ] |
| 642 | 648 | * |
| 643 | 649 | * @since 1.0.0 |
| | 650 | * @since 3.0.0 The `$field_value` parameter can be an array. |
| 644 | 651 | * |
| 645 | | * @param string $field_type The type of field: datebox, selectbox, textbox etc. |
| 646 | | * @param string $field_value The actual value. |
| | 652 | * @param string $field_type The type of field: datebox, selectbox, textbox etc. |
| | 653 | * @param string|array $field_value The actual value. |
| 647 | 654 | * @return string|bool The formatted value, or false if value is empty. |
| 648 | 655 | */ |
| 649 | 656 | function xprofile_format_profile_field( $field_type, $field_value ) { |
| … |
… |
function bp_xprofile_is_richtext_enabled_for_field( $field_id = null ) {
|
| 1146 | 1153 | return apply_filters( 'bp_xprofile_is_richtext_enabled_for_field', $enabled, $field_id ); |
| 1147 | 1154 | } |
| 1148 | 1155 | |
| | 1156 | /** |
| | 1157 | * Does this profile field support multiple values? |
| | 1158 | * |
| | 1159 | * @since 3.0.0 |
| | 1160 | * |
| | 1161 | * @param int|null $field_id Optional. Default current field ID. |
| | 1162 | * @return bool Whether the field supports multiple values |
| | 1163 | */ |
| | 1164 | function bp_xprofile_field_supports_multiple_values( $field_id = null ) { |
| | 1165 | if ( ! $field_id ) { |
| | 1166 | $field_id = bp_get_the_profile_field_id(); |
| | 1167 | } |
| | 1168 | |
| | 1169 | $field = xprofile_get_field( $field_id, null, false ); |
| | 1170 | |
| | 1171 | $supports = false; |
| | 1172 | if ( $field instanceof BP_XProfile_Field ) { |
| | 1173 | $supports = (bool) $field->type_obj->supports_multiple_values; |
| | 1174 | } |
| | 1175 | |
| | 1176 | /** |
| | 1177 | * Filters whether the given field supports multiple values. |
| | 1178 | * |
| | 1179 | * @since 3.0.0 |
| | 1180 | * |
| | 1181 | * @param bool $supports True if the field supports multiple values, otherwise false. |
| | 1182 | * @param int $field_id ID of the field. |
| | 1183 | */ |
| | 1184 | return (bool) apply_filters( 'bp_xprofile_field_supports_multiple_values', $supports, $field_id ); |
| | 1185 | } |
| | 1186 | |
| 1149 | 1187 | /** |
| 1150 | 1188 | * Get visibility levels out of the $bp global. |
| 1151 | 1189 | * |
diff --git src/bp-xprofile/bp-xprofile-template.php src/bp-xprofile/bp-xprofile-template.php
index afe5162ec..e4f8ad277 100644
|
|
|
function bp_the_profile_field_visibility_level_label() {
|
| 903 | 903 | * comma-separated string. |
| 904 | 904 | * |
| 905 | 905 | * @since 1.0.0 |
| | 906 | * @since 3.0.0 The `$value` parameter can be an array. |
| 906 | 907 | * |
| 907 | | * @param string $value Content to maybe unserialize. |
| | 908 | * @param string|array $value Content to maybe unserialize. |
| 908 | 909 | * @return string |
| 909 | 910 | */ |
| 910 | 911 | function bp_unserialize_profile_field( $value ) { |
| 911 | | if ( is_serialized($value) ) { |
| 912 | | $field_value = @unserialize($value); |
| | 912 | if ( is_serialized( $value ) || is_array( $value ) ) { |
| | 913 | $field_value = maybe_unserialize( $value ); |
| 913 | 914 | $field_value = implode( ', ', $field_value ); |
| 914 | 915 | return $field_value; |
| 915 | 916 | } |
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type-checkbox.php src/bp-xprofile/classes/class-bp-xprofile-field-type-checkbox.php
index f3717d3dc..895e07082 100644
|
|
|
class BP_XProfile_Field_Type_Checkbox extends BP_XProfile_Field_Type {
|
| 28 | 28 | $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' ); |
| 29 | 29 | $this->name = _x( 'Checkboxes', 'xprofile field type', 'buddypress' ); |
| 30 | 30 | |
| | 31 | $this->supports_multiple_values = true; |
| 31 | 32 | $this->supports_multiple_defaults = true; |
| 32 | 33 | $this->accepts_null_value = true; |
| 33 | 34 | $this->supports_options = true; |
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type-multiselectbox.php src/bp-xprofile/classes/class-bp-xprofile-field-type-multiselectbox.php
index d56b25be4..718b79d2a 100644
|
|
|
class BP_XProfile_Field_Type_Multiselectbox extends BP_XProfile_Field_Type {
|
| 28 | 28 | $this->category = _x( 'Multi Fields', 'xprofile field type category', 'buddypress' ); |
| 29 | 29 | $this->name = _x( 'Multi Select Box', 'xprofile field type', 'buddypress' ); |
| 30 | 30 | |
| | 31 | $this->supports_multiple_values = true; |
| 31 | 32 | $this->supports_multiple_defaults = true; |
| 32 | 33 | $this->accepts_null_value = true; |
| 33 | 34 | $this->supports_options = true; |
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type.php src/bp-xprofile/classes/class-bp-xprofile-field-type.php
index 65a970c68..66e451df4 100644
|
|
|
abstract class BP_XProfile_Field_Type {
|
| 65 | 65 | */ |
| 66 | 66 | public $supports_options = false; |
| 67 | 67 | |
| | 68 | /** |
| | 69 | * If allowed to store multiple values. |
| | 70 | * |
| | 71 | * @since 3.0.0 |
| | 72 | * @var bool Does this field support multiple values? e.g. multiselectbox, checkbox. |
| | 73 | */ |
| | 74 | public $supports_multiple_values = false; |
| | 75 | |
| 68 | 76 | /** |
| 69 | 77 | * If allowed to support multiple options as default. |
| 70 | 78 | * |
diff --git src/bp-xprofile/classes/class-bp-xprofile-profiledata.php src/bp-xprofile/classes/class-bp-xprofile-profiledata.php
index 9db8bfc3a..b6d97e9b4 100644
|
|
|
class BP_XProfile_ProfileData {
|
| 89 | 89 | $bp = buddypress(); |
| 90 | 90 | |
| 91 | 91 | $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); |
| 92 | | $profiledata = $wpdb->get_row( $sql ); |
| | 92 | $profiledata = $wpdb->get_results( $sql ); |
| 93 | 93 | |
| 94 | 94 | if ( $profiledata ) { |
| | 95 | |
| | 96 | // If the field supports multiple values, an array should be returned. |
| | 97 | if ( bp_xprofile_field_supports_multiple_values( $profiledata[0]->field_id ) ) { |
| | 98 | $profiledata[0]->value = wp_list_pluck( $profiledata, 'value' ); |
| | 99 | |
| | 100 | // Parse empty list |
| | 101 | if ( array() === maybe_unserialize( $profiledata[0]->value ) ) { |
| | 102 | $profiledata[0]->value = array(); |
| | 103 | } |
| | 104 | } |
| | 105 | |
| | 106 | // Get the first row |
| | 107 | $profiledata = $profiledata[0]; |
| 95 | 108 | wp_cache_set( $cache_key, $profiledata, 'bp_xprofile_data' ); |
| 96 | 109 | } |
| 97 | 110 | } |
| … |
… |
class BP_XProfile_ProfileData {
|
| 100 | 113 | $this->id = (int) $profiledata->id; |
| 101 | 114 | $this->user_id = (int) $profiledata->user_id; |
| 102 | 115 | $this->field_id = (int) $profiledata->field_id; |
| 103 | | $this->value = stripslashes( $profiledata->value ); |
| | 116 | $this->value = is_array( $profiledata->value ) ? array_map( 'stripslashes', $profiledata->value ) : stripslashes( $profiledata->value ); |
| 104 | 117 | $this->last_updated = $profiledata->last_updated; |
| 105 | 118 | |
| 106 | 119 | } else { |
| … |
… |
class BP_XProfile_ProfileData {
|
| 239 | 252 | do_action_ref_array( 'xprofile_data_before_save', array( $this ) ); |
| 240 | 253 | |
| 241 | 254 | if ( $this->is_valid_field() ) { |
| 242 | | if ( $this->exists() && strlen( trim( $this->value ) ) ) { |
| | 255 | |
| | 256 | // Special-case support for integer 0 for the number field type |
| | 257 | $value_is_empty = empty( $this->value ) && ! is_integer( $this->value ) && '0' !== $this->value; |
| | 258 | |
| | 259 | // Store as multiple values |
| | 260 | if ( bp_xprofile_field_supports_multiple_values( $this->field_id ) ) { |
| | 261 | |
| | 262 | // Delete previous rows. |
| | 263 | if ( $this->exists() ) { |
| | 264 | $this->delete(); |
| | 265 | } |
| | 266 | |
| | 267 | // Store empty list as serialized array |
| | 268 | if ( empty( $this->value ) ) { |
| | 269 | $this->value = array( serialize( array() ) ); |
| | 270 | } |
| | 271 | |
| | 272 | // Store each value in a separate row |
| | 273 | foreach ( array_values( (array) $this->value ) as $k => $value ) { |
| | 274 | $result = $wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_data} (user_id, field_id, value, last_updated) VALUES (%d, %d, %s, %s)", $this->user_id, $this->field_id, $value, $this->last_updated ) ); |
| | 275 | |
| | 276 | // Use the first inserted id as the new data identifier |
| | 277 | if ( 0 === $k ) { |
| | 278 | $this->id = $wpdb->insert_id; |
| | 279 | } |
| | 280 | } |
| | 281 | |
| | 282 | // Store as single value |
| | 283 | } elseif ( $this->exists() && ! $value_is_empty ) { |
| 243 | 284 | $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_data} SET value = %s, last_updated = %s WHERE user_id = %d AND field_id = %d", $this->value, $this->last_updated, $this->user_id, $this->field_id ) ); |
| 244 | 285 | |
| 245 | | } elseif ( $this->exists() && empty( $this->value ) ) { |
| 246 | | // Data removed, delete the entry. |
| | 286 | // Data removed, delete the entry. |
| | 287 | } elseif ( $this->exists() && $value_is_empty ) { |
| 247 | 288 | $result = $this->delete(); |
| 248 | 289 | |
| 249 | | } else { |
| | 290 | } elseif ( ! $value_is_empty ) { |
| 250 | 291 | $result = $wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_data} (user_id, field_id, value, last_updated) VALUES (%d, %d, %s, %s)", $this->user_id, $this->field_id, $this->value, $this->last_updated ) ); |
| 251 | 292 | $this->id = $wpdb->insert_id; |
| 252 | 293 | } |
| … |
… |
class BP_XProfile_ProfileData {
|
| 337 | 378 | // Rekey. |
| 338 | 379 | $queried_data = array(); |
| 339 | 380 | foreach ( $uncached_data as $ud ) { |
| 340 | | $d = new stdClass; |
| 341 | | $d->id = $ud->id; |
| 342 | | $d->user_id = $ud->user_id; |
| 343 | | $d->field_id = $ud->field_id; |
| 344 | | $d->value = $ud->value; |
| 345 | | $d->last_updated = $ud->last_updated; |
| 346 | | |
| 347 | | $queried_data[ $ud->field_id ] = $d; |
| | 381 | |
| | 382 | // Set initial field data |
| | 383 | if ( ! isset( $queried_data[ $ud->field_id ] ) ) { |
| | 384 | $d = new stdClass; |
| | 385 | $d->id = $ud->id; |
| | 386 | $d->user_id = $ud->user_id; |
| | 387 | $d->field_id = $ud->field_id; |
| | 388 | $d->last_updated = $ud->last_updated; |
| | 389 | |
| | 390 | // Collect multiple values in array |
| | 391 | // TODO: no stripslashing here? See ::populate() |
| | 392 | if ( bp_xprofile_field_supports_multiple_values( $ud->field_id ) ) { |
| | 393 | $d->value = ( array() === maybe_unserialize( $ud->value ) ) ? array() : array( $ud->value ); |
| | 394 | } else { |
| | 395 | $d->value = $ud->value; |
| | 396 | } |
| | 397 | |
| | 398 | $queried_data[ $ud->field_id ] = $d; |
| | 399 | |
| | 400 | // Collect multiple values per field |
| | 401 | } elseif ( bp_xprofile_field_supports_multiple_values( $ud->field_id ) ) { |
| | 402 | // TODO: no stripslashing here? See ::populate() |
| | 403 | $queried_data[ $ud->field_id ]->value[] = $ud->value; |
| | 404 | } |
| 348 | 405 | } |
| 349 | 406 | |
| 350 | 407 | // Set caches. |
| … |
… |
class BP_XProfile_ProfileData {
|
| 363 | 420 | $d->id = ''; |
| 364 | 421 | $d->user_id = $user_id; |
| 365 | 422 | $d->field_id = $field_id; |
| 366 | | $d->value = ''; |
| | 423 | $d->value = bp_xprofile_field_supports_multiple_values( $field_id ) ? array() : ''; |
| 367 | 424 | $d->last_updated = ''; |
| 368 | 425 | |
| 369 | 426 | wp_cache_set( $cache_key, $d, 'bp_xprofile_data' ); |
| … |
… |
class BP_XProfile_ProfileData {
|
| 443 | 500 | * Get the user's field data id by the id of the xprofile field. |
| 444 | 501 | * |
| 445 | 502 | * @since 1.6.0 |
| | 503 | * @since 3.0.0 Returns the first/main data id in case of multiple values. |
| 446 | 504 | * |
| 447 | 505 | * @param int $field_id Field ID being queried for. |
| 448 | 506 | * @param int $user_id User ID associated with field. |
| … |
… |
class BP_XProfile_ProfileData {
|
| 509 | 567 | $bp = buddypress(); |
| 510 | 568 | $uncached_ids_sql = implode( ',', $uncached_ids ); |
| 511 | 569 | $queried_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, field_id, value, last_updated FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$uncached_ids_sql})", $field_id ) ); |
| | 570 | $multiple = bp_xprofile_field_supports_multiple_values( $field_id ); |
| 512 | 571 | |
| 513 | 572 | // Rekey. |
| 514 | 573 | $qd = array(); |
| 515 | 574 | foreach ( $queried_data as $data ) { |
| 516 | | $qd[ $data->user_id ] = $data; |
| | 575 | |
| | 576 | // Set initial user data |
| | 577 | if ( ! isset( $qd[ $data->user_id ] ) ) { |
| | 578 | |
| | 579 | // Collect multiple values in array |
| | 580 | if ( $multiple ) { |
| | 581 | // TODO: no stripslashing here? See ::populate(). |
| | 582 | $data->value = ( array() === maybe_unserialize( $data->value ) ) ? array() : array( $data->value ); |
| | 583 | } |
| | 584 | |
| | 585 | $qd[ $data->user_id ] = $data; |
| | 586 | |
| | 587 | // Collect multiple values per field |
| | 588 | } elseif ( $multiple ) { |
| | 589 | // TODO: no stripslashing here? See ::populate(). |
| | 590 | $qd[ $data->user_id ]->value[] = $data->value; |
| | 591 | } |
| 517 | 592 | } |
| 518 | 593 | |
| 519 | 594 | foreach ( $uncached_ids as $id ) { |
| … |
… |
class BP_XProfile_ProfileData {
|
| 528 | 603 | $d->id = ''; |
| 529 | 604 | $d->user_id = $id; |
| 530 | 605 | $d->field_id = $field_id; |
| 531 | | $d->value = ''; |
| | 606 | $d->value = $multiple ? array() : ''; |
| 532 | 607 | $d->last_updated = ''; |
| 533 | 608 | } |
| 534 | 609 | |
| … |
… |
class BP_XProfile_ProfileData {
|
| 604 | 679 | $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields ); |
| 605 | 680 | } |
| 606 | 681 | |
| 607 | | $sql = $wpdb->prepare( "SELECT d.value, f.name FROM {$bp->profile->table_name_data} d, {$bp->profile->table_name_fields} f WHERE d.field_id = f.id AND d.user_id = %d AND f.parent_id = 0 $field_sql", $user_id ); |
| | 682 | $sql = $wpdb->prepare( "SELECT d.value, f.name, f.id FROM {$bp->profile->table_name_data} d, {$bp->profile->table_name_fields} f WHERE d.field_id = f.id AND d.user_id = %d AND f.parent_id = 0 $field_sql", $user_id ); |
| 608 | 683 | $values = $wpdb->get_results( $sql ); |
| 609 | 684 | |
| | 685 | // TODO: checking for empty result/WP_Error, but not in other methods? |
| 610 | 686 | if ( empty( $values ) || is_wp_error( $values ) ) { |
| 611 | 687 | return false; |
| 612 | 688 | } |
| … |
… |
class BP_XProfile_ProfileData {
|
| 617 | 693 | for ( $i = 0, $count = count( $values ); $i < $count; ++$i ) { |
| 618 | 694 | for ( $j = 0; $j < count( $fields ); $j++ ) { |
| 619 | 695 | if ( $values[$i]->name == $fields[$j] ) { |
| 620 | | $new_values[$fields[$j]] = $values[$i]->value; |
| | 696 | $multiple = bp_xprofile_field_supports_multiple_values( $values[$i]->id ); |
| | 697 | |
| | 698 | // TODO: no stripslashing here? See ::populate() |
| | 699 | if ( ! isset( $new_values[ $fields[$j] ] ) ) { |
| | 700 | if ( $multiple ) { |
| | 701 | $new_values[$fields[$j]] = ( array() === $values[$i]->value ) ? array() : array( $values[$i]->value ); |
| | 702 | } else { |
| | 703 | $new_values[$fields[$j]] = $values[$i]->value; |
| | 704 | } |
| | 705 | } elseif ( $multiple ) { |
| | 706 | $new_values[$fields[$j]][] = $values[$i]->value; |
| | 707 | } |
| 621 | 708 | } elseif ( !array_key_exists( $fields[$j], $new_values ) ) { |
| 622 | 709 | $new_values[$fields[$j]] = NULL; |
| 623 | 710 | } |
| 624 | 711 | } |
| 625 | 712 | } |
| 626 | 713 | } else { |
| 627 | | $new_values = $values[0]->value; |
| | 714 | |
| | 715 | // Collect multiple values in array |
| | 716 | // TODO: no stripslashing here? See ::populate() |
| | 717 | if ( bp_xprofile_field_supports_multiple_values( $values[0]->id ) ) { |
| | 718 | $new_values = wp_list_pluck( $values, 'value' ); |
| | 719 | |
| | 720 | // Parse empty list |
| | 721 | if ( array() === maybe_unserialize( $new_values[0] ) ) { |
| | 722 | $new_values = array(); |
| | 723 | } |
| | 724 | } else { |
| | 725 | $new_values = $values[0]->value; |
| | 726 | } |
| 628 | 727 | } |
| 629 | 728 | |
| 630 | 729 | return $new_values; |
| … |
… |
class BP_XProfile_ProfileData {
|
| 689 | 788 | * |
| 690 | 789 | * @since 1.0.0 |
| 691 | 790 | * |
| 692 | | * @param int $user_id User ID to query for. |
| 693 | | * @param string $exclude_fullname SQL portion used to exclude by field ID. |
| | 791 | * @param int $user_id User ID to query for. |
| | 792 | * @param bool $exclude_fullname Optional. Whether to exclude the fullname field. Defaults to false. |
| 694 | 793 | * @return array|null|object |
| 695 | 794 | */ |
| 696 | | public static function get_random( $user_id, $exclude_fullname ) { |
| | 795 | public static function get_random( $user_id, $exclude_fullname = false ) { |
| 697 | 796 | global $wpdb; |
| 698 | 797 | |
| 699 | 798 | $exclude_sql = ! empty( $exclude_fullname ) ? ' AND pf.id != 1' : ''; |
| 700 | 799 | |
| 701 | 800 | $bp = buddypress(); |
| 702 | 801 | |
| 703 | | return $wpdb->get_results( $wpdb->prepare( "SELECT pf.type, pf.name, pd.value FROM {$bp->profile->table_name_data} pd INNER JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id AND pd.user_id = %d {$exclude_sql} ORDER BY RAND() LIMIT 1", $user_id ) ); |
| | 802 | $rand_sql = "SELECT id FROM {$bp->profile->table_name_fields} ORDER BY RAND() LIMIT 1"; |
| | 803 | $result = $wpdb->get_results( $wpdb->prepare( "SELECT pf.type, pf.name, pd.value, pf.id FROM {$bp->profile->table_name_data} pd INNER JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id AND pd.user_id = %d {$exclude_sql} WHERE pf.id IN ($rand_sql)", $user_id ) ); |
| | 804 | |
| | 805 | // Collect multiple values in array |
| | 806 | if ( bp_xprofile_field_supports_multiple_values( $result[0]->id ) ) { |
| | 807 | // TODO: no stripslashing here? See ::populate() |
| | 808 | $result[0]->value = wp_list_pluck( $result, 'value' ); |
| | 809 | |
| | 810 | // Parse empty list |
| | 811 | if ( array() === maybe_unserialize( $result[0]->value ) ) { |
| | 812 | $result[0]->value = array(); |
| | 813 | } |
| | 814 | |
| | 815 | $result = array( $result[0] ); |
| | 816 | } |
| | 817 | |
| | 818 | return $result; |
| 704 | 819 | } |
| 705 | 820 | |
| 706 | 821 | /** |
diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php tests/phpunit/testcases/xprofile/class-bp-xprofile-profiledata.php
index 5f1de867d..a8fb4140e 100644
|
|
|
class BP_Tests_BP_XProfile_ProfileData_TestCases extends BP_UnitTestCase {
|
| 422 | 422 | public function filter_time() { |
| 423 | 423 | return $this->last_updated; |
| 424 | 424 | } |
| | 425 | |
| | 426 | /** |
| | 427 | * @ticket BP6789 |
| | 428 | */ |
| | 429 | public function test_get_data_for_user_should_properly_cache_multi_value_fields() { |
| | 430 | $u = self::factory()->user->create(); |
| | 431 | $g = self::factory()->xprofile_group->create(); |
| | 432 | $f = self::factory()->xprofile_field->create( array( |
| | 433 | 'type' => 'multiselectbox', |
| | 434 | 'field_group_id' => $g, |
| | 435 | ) ); |
| | 436 | |
| | 437 | xprofile_set_field_data( $f, $u, array( 'foo', 'bar' ) ); |
| | 438 | |
| | 439 | $expected = array( 'foo', 'bar' ); |
| | 440 | |
| | 441 | // Uncached query. |
| | 442 | $value = BP_XProfile_ProfileData::get_data_for_user( $u, array( $f ) ); |
| | 443 | $this->assertEqualSets( $expected, $value[0]->value ); |
| | 444 | |
| | 445 | // Cached query. |
| | 446 | $value = BP_XProfile_ProfileData::get_data_for_user( $u, array( $f ) ); |
| | 447 | $this->assertEqualSets( $expected, $value[0]->value ); |
| | 448 | } |
| 425 | 449 | } |