Ticket #6413: 6413.diff
| File 6413.diff, 9.1 KB (added by , 10 years ago) |
|---|
-
src/bp-core/bp-core-update.php
diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php index a85c41e..daa09e5 100644
function bp_version_updater() { 260 260 if ( $raw_db_version < 9615 ) { 261 261 bp_update_to_2_3(); 262 262 } 263 264 if ( $raw_db_version < 10457 ) { 265 bp_update_to_2_5(); 266 } 263 267 } 264 268 265 269 /** All done! *************************************************************/ … … function bp_update_to_2_3() { 484 488 } 485 489 486 490 /** 491 * 2.5.0 update routine. 492 * 493 * - Kick off cron job to migrate XProfile visibility data. 494 * 495 * @since 2.5.0 496 */ 497 function bp_update_to_2_5() { 498 bp_update_option( 'bp_xprofile_migrated_field_visibility', 0 ); 499 wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'bp_xprofile_field_visibility_migrate_hook' ); 500 } 501 502 /** 487 503 * Updates the component field for new_members type. 488 504 * 489 505 * @since 2.2.0 -
src/bp-loader.php
diff --git src/bp-loader.php src/bp-loader.php index 77d5408..f01f5c6 100644
class BuddyPress { 328 328 /** Versions **********************************************************/ 329 329 330 330 $this->version = '2.5.0-alpha'; 331 $this->db_version = 10 071;331 $this->db_version = 10457; 332 332 333 333 /** Loading ***********************************************************/ 334 334 -
src/bp-xprofile/bp-xprofile-functions.php
diff --git src/bp-xprofile/bp-xprofile-functions.php src/bp-xprofile/bp-xprofile-functions.php index 0996257..285a69a 100644
function xprofile_set_field_visibility_level( $field_id = 0, $user_id = 0, $visi 470 470 return false; 471 471 } 472 472 473 // Stored in an array in usermeta. 474 $current_visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); 475 476 if ( !$current_visibility_levels ) { 477 $current_visibility_levels = array(); 473 $data_object = new BP_XProfile_ProfileData( $field_id, $user_id ); 474 if ( ! $data_object->exists() ) { 475 return false; 478 476 } 479 477 480 $current_visibility_levels[$field_id] = $visibility_level; 481 482 return bp_update_user_meta( $user_id, 'bp_xprofile_visibility_levels', $current_visibility_levels ); 478 return (bool) $data_object->set_visibility_level( $visibility_level ); 483 479 } 484 480 485 481 /** … … function xprofile_get_field_visibility_level( $field_id = 0, $user_id = 0 ) { 498 494 return $current_level; 499 495 } 500 496 501 $ current_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true);502 $current_level = isset( $current_levels[ $field_id ] ) ? $current_levels[ $field_id ] : '';497 $data_object = new BP_XProfile_ProfileData( $field_id, $user_id ); 498 $current_level = $data_object->get_visibility_level(); 503 499 504 500 // Use the user's stored level, unless custom visibility is disabled. 505 501 $field = xprofile_get_field( $field_id ); … … function xprofile_get_field_visibility_level( $field_id = 0, $user_id = 0 ) { 517 513 } 518 514 519 515 /** 516 * Migrate field visibility from usermeta to xprofilemeta. 517 * 518 * Runs on a cron hook, 'bp_xprofile_field_visibility_migrate_hook'. See bp_update_to_2_5(). 519 * 520 * @since BuddyPress 2.5.0 521 */ 522 function bp_xprofile_field_visibility_migrate() { 523 global $wpdb; 524 525 if ( bp_get_option( 'bp_xprofile_migrated_field_visibility' ) ) { 526 return; 527 } 528 529 bp_update_option( 'bp_xprofile_field_visibility_migration_in_progress', 1 ); 530 531 $bp = buddypress(); 532 533 $migrate_user_ids = get_users( array( 534 'fields' => 'ids', 535 'number' => 25, 536 'meta_query' => array( 537 array( 538 'key' => 'bp_xprofile_visibility_levels', 539 'compare' => 'EXISTS', 540 ), 541 ), 542 ) ); 543 544 if ( empty( $migrate_user_ids ) ) { 545 // Nothing more to do here. 546 bp_update_option( 'bp_xprofile_migrated_field_visibility', 1 ); 547 } else { 548 foreach ( $migrate_user_ids as $u ) { 549 $visibility_levels = bp_get_user_meta( $u, 'bp_xprofile_visibility_levels', true ); 550 $failed_levels = array(); 551 foreach ( $visibility_levels as $field_id => $level ) { 552 $data = new BP_XProfile_ProfileData( $field_id, $u ); 553 if ( ! $data->exists() ) { 554 // If the data doesn't exist, there's nothing to migrate. 555 continue; 556 } 557 558 // If the field already has the visibility level, there's nothing to migrate. 559 if ( $level === $data->get_visibility_level() ) { 560 continue; 561 } 562 563 if ( ! $data->set_visibility_level( $level ) ) { 564 $failed_levels[ $field_id ] = $level; 565 } 566 } 567 568 if ( empty( $failed_levels ) ) { 569 bp_delete_user_meta( $u, 'bp_xprofile_visibility_levels' ); 570 } else { 571 bp_update_user_meta( $u, 'bp_xprofile_visibility_levels', $visibility_levels ); 572 } 573 } 574 575 // Run again in a minute. 576 wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'bp_xprofile_field_visibility_migrate_hook' ); 577 } 578 579 bp_delete_option( 'bp_xprofile_field_visibility_migration_in_progress' ); 580 } 581 add_action( 'bp_xprofile_field_visibility_migrate_hook', 'bp_xprofile_field_visibility_migrate' ); 582 583 /** 520 584 * Delete XProfile field data. 521 585 * 522 586 * @param string $field Field to delete. -
src/bp-xprofile/classes/class-bp-xprofile-profiledata.php
diff --git src/bp-xprofile/classes/class-bp-xprofile-profiledata.php src/bp-xprofile/classes/class-bp-xprofile-profiledata.php index 7f46cd4..f98f12c 100644
class BP_XProfile_ProfileData { 53 53 public $last_updated; 54 54 55 55 /** 56 * Data visibility level. 57 * 58 * @since 2.5.0 59 * 60 * @var string 61 */ 62 protected $visibility_level; 63 64 /** 56 65 * BP_XProfile_ProfileData constructor. 57 66 * 58 67 * @param null $field_id Field ID to instantiate. … … class BP_XProfile_ProfileData { 252 261 return true; 253 262 } 254 263 264 /** 265 * Get the visibility level for this data. 266 * 267 * @since 2.5.0 268 * 269 * @return string 270 */ 271 public function get_visibility_level() { 272 if ( null === $this->visibility_level ) { 273 $this->visibility_level = bp_xprofile_get_meta( $this->id, 'data', 'visibility_level', true ); 274 } 275 276 return $this->visibility_level; 277 } 278 279 /** 280 * Set the visibility level for this data. 281 * 282 * Does not validate level against whitelist. 283 * 284 * @since 2.5.0 285 * 286 * @param string $level New visibility level. 287 * @return bool True on success, false on failure. 288 */ 289 public function set_visibility_level( $level ) { 290 if ( ! bp_xprofile_update_meta( $this->id, 'data', 'visibility_level', $level ) ) { 291 return false; 292 } 293 294 $this->visibility_level = null; 295 return true; 296 } 297 255 298 /** Static Methods ********************************************************/ 256 299 257 300 /** -
tests/phpunit/testcases/xprofile/functions.php
diff --git tests/phpunit/testcases/xprofile/functions.php tests/phpunit/testcases/xprofile/functions.php index f5f56e4..a0a14dd 100644
Bar!'; 102 102 $f = $this->factory->xprofile_field->create( array( 103 103 'field_group_id' => $g, 104 104 ) ); 105 xprofile_set_field_data( $f, $u, 'foo' ); 105 106 106 107 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 107 108 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'allowed' ); … … Bar!'; 120 121 $f = $this->factory->xprofile_field->create( array( 121 122 'field_group_id' => $g, 122 123 ) ); 124 xprofile_set_field_data( $f, $u, 'foo' ); 123 125 124 126 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 125 127 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'allowed' ); … … Bar!'; 137 139 $f = $this->factory->xprofile_field->create( array( 138 140 'field_group_id' => $g, 139 141 ) ); 142 xprofile_set_field_data( $f, $u, 'foo' ); 140 143 141 144 bp_xprofile_update_meta( $f, 'field', 'default_visibility', 'adminsonly' ); 142 145 bp_xprofile_update_meta( $f, 'field', 'allow_custom_visibility', 'disabled' ); … … Bar!'; 147 150 } 148 151 149 152 /** 153 * @group bp_xprofile_field_visibility_migrate 154 */ 155 public function test_bp_xprofile_field_visibility_migrate_should_move_all_data() { 156 $users = $this->factory->user->create_many( 2 ); 157 $g = $this->factory->xprofile_group->create(); 158 $f1 = $this->factory->xprofile_field->create( array( 159 'field_group_id' => $g, 160 ) ); 161 $f2 = $this->factory->xprofile_field->create( array( 162 'field_group_id' => $g, 163 ) ); 164 165 // Set up and verify old-style data. 166 foreach ( $users as $user ) { 167 xprofile_set_field_data( $f1, $user, 'foo' ); 168 xprofile_set_field_data( $f2, $user, 'foo' ); 169 170 bp_update_user_meta( $user, 'bp_xprofile_visibility_levels', array( 171 $f1 => 'loggedin', 172 $f2 => 'adminsonly', 173 ) ); 174 175 $data1 = new BP_XProfile_ProfileData( $f1, $user ); 176 $data2 = new BP_XProfile_ProfileData( $f2, $user ); 177 178 $this->assertSame( '', $data1->get_visibility_level() ); 179 $this->assertSame( '', $data2->get_visibility_level() ); 180 } 181 182 bp_xprofile_field_visibility_migrate(); 183 184 foreach ( $users as $user ) { 185 $data1 = new BP_XProfile_ProfileData( $f1, $user ); 186 $data2 = new BP_XProfile_ProfileData( $f2, $user ); 187 188 $this->assertSame( '', bp_get_user_meta( $user, 'bp_xprofile_visibility_levels', true ) ); 189 $this->assertSame( 'loggedin', $data1->get_visibility_level() ); 190 $this->assertSame( 'adminsonly', $data2->get_visibility_level() ); 191 } 192 } 193 194 /** 150 195 * @group xprofilemeta 151 196 * @group bp_xprofile_delete_meta 152 197 */