Changeset 7808
- Timestamp:
- 02/06/2014 02:23:51 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-xprofile/bp-xprofile-classes.php
r7807 r7808 1279 1279 1280 1280 /** 1281 * BP_XProfile_ProfileData::get_all_for_user()1282 *1283 1281 * Get all of the profile information for a specific user. 1282 * 1283 * @param int $user_id ID of the user. 1284 * @return array 1284 1285 */ 1285 1286 public static function get_all_for_user( $user_id ) { 1286 1287 global $wpdb, $bp; 1287 1288 1288 $results = $wpdb->get_results( $wpdb->prepare( "SELECT g.id as field_group_id, g.name as field_group_name, f.id as field_id, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email FROM {$bp->profile->table_name_groups} g LEFT JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp->profile->table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb->users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''", $user_id ) ); 1289 $groups = BP_XProfile_Group::get( array( 1290 'user_id' => $user_id, 1291 'hide_empty_groups' => true, 1292 'hide_empty_fields' => true, 1293 'fetch_fields' => true, 1294 'fetch_field_data' => true, 1295 ) ); 1296 1289 1297 $profile_data = array(); 1290 1298 1291 if ( !empty( $results ) ) { 1292 $profile_data['user_login'] = $results[0]->user_login; 1293 $profile_data['user_nicename'] = $results[0]->user_nicename; 1294 $profile_data['user_email'] = $results[0]->user_email; 1295 1296 foreach( (array) $results as $field ) { 1297 $profile_data[$field->field_name] = array( 1298 'field_group_id' => $field->field_group_id, 1299 'field_group_name' => $field->field_group_name, 1300 'field_id' => $field->field_id, 1301 'field_type' => $field->field_type, 1302 'field_data' => $field->field_data 1303 ); 1299 if ( ! empty( $groups ) ) { 1300 $user = new WP_User( $user_id ); 1301 1302 $profile_data['user_login'] = $user->user_login; 1303 $profile_data['user_nicename'] = $user->user_nicename; 1304 $profile_data['user_email'] = $user->user_email; 1305 1306 foreach ( (array) $groups as $group ) { 1307 if ( empty( $group->fields ) ) { 1308 continue; 1309 } 1310 1311 foreach ( (array) $group->fields as $field ) { 1312 $profile_data[ $field->name ] = array( 1313 'field_group_id' => $group->id, 1314 'field_group_name' => $group->name, 1315 'field_id' => $field->id, 1316 'field_type' => $field->type, 1317 'field_data' => $field->data->value, 1318 ); 1319 } 1304 1320 } 1305 1321 } -
trunk/tests/testcases/xprofile/class-bp-xprofile-profiledata.php
r7806 r7808 264 264 } 265 265 266 /** 267 * @group get_all_for_user 268 */ 269 public function test_get_all_for_user_uncached() { 270 $u = $this->create_user(); 271 $g1 = $this->factory->xprofile_group->create(); 272 $g2 = $this->factory->xprofile_group->create(); 273 $f1 = $this->factory->xprofile_field->create( array( 274 'type' => 'textbox', 275 'field_group_id' => $g1->id, 276 ) ); 277 $f2 = $this->factory->xprofile_field->create( array( 278 'type' => 'radio', 279 'field_group_id' => $g2->id, 280 ) ); 281 282 $time = bp_core_current_time(); 283 284 // Get the fullname field - hackish 285 $f0_id = xprofile_get_field_id_from_name( bp_xprofile_fullname_field_name() ); 286 $f0 = new BP_XProfile_Field( $f0_id ); 287 $g0 = new BP_XProfile_Group( $f0->group_id ); 288 $d0 = new BP_XProfile_ProfileData( $f0->id, $u ); 289 290 $d1 = new BP_XProfile_ProfileData(); 291 $d1->user_id = $u; 292 $d1->field_id = $f1->id; 293 $d1->value = 'foo'; 294 $d1->last_updated = $time; 295 $d1->save(); 296 297 $d2 = new BP_XProfile_ProfileData(); 298 $d2->user_id = $u; 299 $d2->field_id = $f2->id; 300 $d2->value = 'bar'; 301 $d2->last_updated = $time; 302 $d2->save(); 303 304 // Ensure it's deleted from cache 305 wp_cache_delete( $f1->id, 'bp_xprofile_data_' . $u ); 306 wp_cache_delete( $f2->id, 'bp_xprofile_data_' . $u ); 307 308 $u_obj = new WP_User( $u ); 309 310 $expected = array( 311 'user_login' => $u_obj->user_login, 312 'user_nicename' => $u_obj->user_nicename, 313 'user_email' => $u_obj->user_email, 314 $f0->name => array( 315 'field_group_id' => $g0->id, 316 'field_group_name' => $g0->name, 317 'field_id' => $f0->id, 318 'field_type' => $f0->type, 319 'field_data' => $d0->value, 320 ), 321 $f1->name => array( 322 'field_group_id' => $g1->id, 323 'field_group_name' => $g1->name, 324 'field_id' => $f1->id, 325 'field_type' => $f1->type, 326 'field_data' => $d1->value, 327 ), 328 $f2->name => array( 329 'field_group_id' => $g2->id, 330 'field_group_name' => $g2->name, 331 'field_id' => $f2->id, 332 'field_type' => $f2->type, 333 'field_data' => $d2->value, 334 ), 335 ); 336 337 $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) ); 338 } 339 340 /** 341 * @group get_all_for_user 342 */ 343 public function test_get_all_for_user_cached() { 344 $u = $this->create_user(); 345 $g1 = $this->factory->xprofile_group->create(); 346 $g2 = $this->factory->xprofile_group->create(); 347 $f1 = $this->factory->xprofile_field->create( array( 348 'type' => 'textbox', 349 'field_group_id' => $g1->id, 350 ) ); 351 $f2 = $this->factory->xprofile_field->create( array( 352 'type' => 'radio', 353 'field_group_id' => $g2->id, 354 ) ); 355 356 $time = bp_core_current_time(); 357 358 $g0 = new BP_XProfile_Group( 1 ); 359 $f0 = new BP_XProfile_Field( 1 ); 360 $d0 = new BP_XProfile_ProfileData( 1, $u ); 361 362 $d1 = new stdClass; 363 $d1->user_id = $u; 364 $d1->field_id = $f1->id; 365 $d1->value = 'foo'; 366 $d1->last_updated = $time; 367 368 $d2 = new stdClass; 369 $d2->user_id = $u; 370 $d2->field_id = $f2->id; 371 $d2->value = 'bar'; 372 $d2->last_updated = $time; 373 374 wp_cache_set( $f1->id, $d1, 'bp_xprofile_data_' . $u ); 375 wp_cache_set( $f2->id, $d2, 'bp_xprofile_data_' . $u ); 376 377 $u_obj = new WP_User( $u ); 378 379 $expected = array( 380 'user_login' => $u_obj->user_login, 381 'user_nicename' => $u_obj->user_nicename, 382 'user_email' => $u_obj->user_email, 383 $f0->name => array( 384 'field_group_id' => $g0->id, 385 'field_group_name' => $g0->name, 386 'field_id' => $f0->id, 387 'field_type' => $f0->type, 388 'field_data' => $d0->value, 389 ), 390 $f1->name => array( 391 'field_group_id' => $g1->id, 392 'field_group_name' => $g1->name, 393 'field_id' => $f1->id, 394 'field_type' => $f1->type, 395 'field_data' => $d1->value, 396 ), 397 $f2->name => array( 398 'field_group_id' => $g2->id, 399 'field_group_name' => $g2->name, 400 'field_id' => $f2->id, 401 'field_type' => $f2->type, 402 'field_data' => $d2->value, 403 ), 404 ); 405 406 $this->assertEquals( $expected, BP_XProfile_ProfileData::get_all_for_user( $u ) ); 407 } 408 266 409 }
Note: See TracChangeset
for help on using the changeset viewer.