Changeset 9671
- Timestamp:
- 04/01/2015 02:23:20 PM (10 years ago)
- Location:
- trunk/src/bp-xprofile/classes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php
r9667 r9671 125 125 126 126 /** 127 * Populate or initialize aprofile field127 * Initialize and/or populate profile field 128 128 * 129 129 * @since BuddyPress (1.1.0) … … 146 146 147 147 /** 148 * Populate a profile field with148 * Populate a profile field object 149 149 * 150 150 * @since BuddyPress (1.1.0) -
trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php
r9635 r9671 11 11 12 12 class BP_XProfile_Group { 13 14 /** 15 * @since BuddyPress (1.1.0) 16 * 17 * @var int ID of field group 18 */ 13 19 public $id = null; 20 21 /** 22 * @since BuddyPress (1.1.0) 23 * 24 * @var string Name of field group 25 */ 14 26 public $name; 27 28 /** 29 * @since BuddyPress (1.1.0) 30 * 31 * @var string Description of field group 32 */ 15 33 public $description; 34 35 /** 36 * @since BuddyPress (1.1.0) 37 * 38 * @var bool Can this group be deleted? 39 */ 16 40 public $can_delete; 41 42 /** 43 * @since BuddyPress (1.1.0) 44 * 45 * @var int Group order relative to other groups 46 */ 17 47 public $group_order; 48 49 /** 50 * @since BuddyPress (1.1.0) 51 * 52 * @var array Fields of group 53 */ 18 54 public $fields; 19 55 56 /** 57 * Initialize and/or populate profile field group 58 * 59 * @since BuddyPress (1.1.0) 60 * 61 * @param int $id 62 * @param int $user_id 63 * @param bool $get_data 64 */ 20 65 public function __construct( $id = null ) { 21 if ( ! empty( $id ) )66 if ( ! empty( $id ) ) { 22 67 $this->populate( $id ); 23 } 24 68 } 69 } 70 71 /** 72 * Populate a profile field group 73 * 74 * @since BuddyPress (1.0.0) 75 * 76 * @global $wpdb $wpdb 77 * @param int $id 78 * 79 * @return boolean 80 */ 25 81 public function populate( $id ) { 26 82 global $wpdb; … … 44 100 } 45 101 102 /** 103 * Save a profile field group 104 * 105 * @since BuddyPress (1.1.0) 106 * 107 * @global object $wpdb 108 * 109 * @return boolean 110 */ 46 111 public function save() { 47 112 global $wpdb; … … 63 128 $bp = buddypress(); 64 129 65 if ( $this->id )130 if ( ! empty( $this->id ) ) { 66 131 $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id ); 67 else132 } else { 68 133 $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description ); 69 70 if ( is_wp_error( $wpdb->query( $sql ) ) ) 134 } 135 136 // Bail if query fails 137 if ( is_wp_error( $wpdb->query( $sql ) ) ) { 71 138 return false; 139 } 72 140 73 141 // If not set, update the ID in the group object 74 if ( ! $this->id )142 if ( empty( $this->id ) ) { 75 143 $this->id = $wpdb->insert_id; 144 } 76 145 77 146 /** … … 87 156 } 88 157 158 /** 159 * Delete a profile field group 160 * 161 * @since BuddyPress (1.1.0) 162 * 163 * @global object $wpdb 164 * @return boolean 165 */ 89 166 public function delete() { 90 167 global $wpdb; 91 168 92 if ( empty( $this->can_delete ) ) 169 // Bail if field group cannot be deleted 170 if ( empty( $this->can_delete ) ) { 93 171 return false; 172 } 94 173 95 174 /** … … 102 181 do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) ); 103 182 104 $bp = buddypress(); 183 $bp = buddypress(); 184 $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ); 185 $deleted = $wpdb->query( $sql ); 105 186 106 187 // Delete field group 107 if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id )) ) {188 if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 108 189 return false; 109 } else { 110 111 // Remove the group's fields. 112 if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { 113 114 // Remove profile data for the groups fields 115 for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { 116 BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); 117 } 118 } 119 120 /** 121 * Fires after the current group instance gets deleted. 122 * 123 * @since BuddyPress (2.0.0) 124 * 125 * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference. 126 */ 127 do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) ); 128 129 return true; 130 } 190 } 191 192 // Remove the group's fields. 193 if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { 194 195 // Remove profile data for the groups fields 196 for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { 197 BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); 198 } 199 } 200 201 /** 202 * Fires after the current group instance gets deleted. 203 * 204 * @since BuddyPress (2.0.0) 205 * 206 * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference. 207 */ 208 do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) ); 209 210 return true; 131 211 } 132 212 … … 134 214 135 215 /** 136 * get() 137 * 138 * Populates the BP_XProfile_Group object with profile field groups, fields, and field data 216 * Populates the BP_XProfile_Group object with profile field groups, fields, 217 * and field data 139 218 * 140 219 * @package BuddyPress XProfile 141 220 * 142 221 * @global $wpdb WordPress DB access object. 143 * @global BuddyPress $bp The one true BuddyPress instance144 222 * 145 223 * @param array $args { … … 169 247 global $wpdb; 170 248 171 $defaults = array( 249 // Parse arguments 250 $r = wp_parse_args( $args, array( 172 251 'profile_group_id' => false, 173 252 'user_id' => bp_displayed_user_id(), … … 180 259 'exclude_fields' => false, 181 260 'update_meta_cache' => true, 182 ); 183 184 $r = wp_parse_args( $args, $defaults ); 185 extract( $r, EXTR_SKIP ); 261 ) ); 186 262 187 263 // Keep track of object IDs for cache-priming … … 192 268 ); 193 269 194 $where_sql = ''; 195 196 if ( ! empty( $profile_group_id ) ) { 197 $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id ); 198 } elseif ( $exclude_groups ) { 199 $exclude_groups = join( ',', wp_parse_id_list( $exclude_groups ) ); 200 $where_sql = "WHERE g.id NOT IN ({$exclude_groups})"; 270 // WHERE 271 if ( ! empty( $r['profile_group_id'] ) ) { 272 $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] ); 273 } elseif ( $r['exclude_groups'] ) { 274 $exclude = join( ',', wp_parse_id_list( $r['exclude_groups'] ) ); 275 $where_sql = "WHERE g.id NOT IN ({$exclude})"; 276 } else { 277 $where_sql = ''; 201 278 } 202 279 203 280 $bp = buddypress(); 204 281 205 if ( ! empty( $hide_empty_groups ) ) { 282 // Include or exclude empty groups 283 if ( ! empty( $r['hide_empty_groups'] ) ) { 206 284 $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" ); 207 285 } else { … … 209 287 } 210 288 289 // Get all group data 211 290 $groups = self::get_group_data( $group_ids ); 212 291 213 if ( empty( $fetch_fields ) ) 292 // Bail if not also getting fields 293 if ( empty( $r['fetch_fields'] ) ) { 214 294 return $groups; 215 216 // Get the group ids 217 $group_ids = array(); 218 foreach( (array) $groups as $group ) { 219 $group_ids[] = $group->id; 220 } 295 } 296 297 // Get the group ids from the groups we found 298 $group_ids = wp_list_pluck( $groups, 'id' ); 221 299 222 300 // Store for meta cache priming 223 301 $object_ids['group'] = $group_ids; 224 302 225 $group_ids = implode( ',', (array) $group_ids ); 226 227 if ( empty( $group_ids ) ) 303 // Bail if no groups foundS 304 if ( empty( $group_ids ) ) { 228 305 return $groups; 306 } 307 308 // Setup IN query from group IDs 309 $group_ids_in = implode( ',', (array) $group_ids ); 229 310 230 311 // Support arrays and comma-separated strings 231 $exclude_fields_cs = wp_parse_id_list( $ exclude_fields);312 $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] ); 232 313 233 314 // Visibility - Handled here so as not to be overridden by sloppy use of the 234 315 // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user() 235 $exclude_fields_cs = array_merge( $exclude_fields_cs, bp_xprofile_get_hidden_fields_for_user( $user_id ) ); 236 $exclude_fields_cs = implode( ',', $exclude_fields_cs ); 237 238 if ( !empty( $exclude_fields_cs ) ) { 316 $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] ); 317 $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields ); 318 $exclude_fields_cs = implode( ',', $exclude_fields_cs ); 319 320 // Setup IN query for field IDs 321 if ( ! empty( $exclude_fields_cs ) ) { 239 322 $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})"; 240 323 } else { … … 243 326 244 327 // Fetch the fields 245 $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" ); 328 $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" ); 329 $field_ids = wp_list_pluck( $fields, 'id' ); 246 330 247 331 // Store field IDs for meta cache priming 248 $object_ids['field'] = wp_list_pluck( $fields, 'id' ); 249 250 if ( empty( $fields ) ) 332 $object_ids['field'] = $field_ids; 333 334 // Bail if no fields 335 if ( empty( $fields ) ) { 251 336 return $groups; 337 } 252 338 253 339 // Maybe fetch field data 254 if ( ! empty( $fetch_field_data ) ) { 255 256 // Fetch the field data for the user. 257 foreach( (array) $fields as $field ) { 258 $field_ids[] = $field->id; 259 } 260 261 $field_ids_sql = implode( ',', (array) $field_ids ); 262 263 if ( ! empty( $field_ids ) && ! empty( $user_id ) ) { 264 $field_data = BP_XProfile_ProfileData::get_data_for_user( $user_id, $field_ids ); 340 if ( ! empty( $r['fetch_field_data'] ) ) { 341 342 // Get field data for user ID 343 if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) { 344 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids ); 265 345 } 266 346 267 347 // Remove data-less fields, if necessary 268 if ( ! empty( $hide_empty_fields) && ! empty( $field_ids ) && ! empty( $field_data ) ) {348 if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) { 269 349 270 350 // Loop through the results and find the fields that have data. … … 278 358 279 359 // Fields that have data get removed from the list 280 unset( $field_ids[ $key] );360 unset( $field_ids[ $key ] ); 281 361 } 282 362 } … … 285 365 foreach( $fields as $field_key => $field ) { 286 366 if ( in_array( $field->id, $field_ids ) ) { 287 unset( $fields[ $field_key] );367 unset( $fields[ $field_key ] ); 288 368 } 289 369 } … … 294 374 295 375 // Field data was found 296 if ( ! empty( $fields ) && ! empty( $field_data ) && !is_wp_error( $field_data ) ) {376 if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) { 297 377 298 378 // Loop through fields … … 304 384 // Assign correct data value to the field 305 385 if ( $field->id == $data->field_id ) { 306 $fields[ $field_key]->data = new stdClass;307 $fields[ $field_key]->data->value = $data->value;308 $fields[ $field_key]->data->id = $data->id;386 $fields[ $field_key ]->data = new stdClass; 387 $fields[ $field_key ]->data->value = $data->value; 388 $fields[ $field_key ]->data->id = $data->id; 309 389 } 310 390 … … 317 397 318 398 // Prime the meta cache, if necessary 319 if ( $update_meta_cache) {399 if ( ! empty( $r['update_meta_cache'] ) ) { 320 400 bp_xprofile_update_meta_cache( $object_ids ); 321 401 } 322 402 323 403 // Maybe fetch visibility levels 324 if ( ! empty( $fetch_visibility_level) ) {325 $fields = self::fetch_visibility_level( $ user_id, $fields );404 if ( ! empty( $r['fetch_visibility_level'] ) ) { 405 $fields = self::fetch_visibility_level( $r['user_id'], $fields ); 326 406 } 327 407 … … 334 414 335 415 foreach( (array) $fields as $field ) { 336 if ( $group->id == $field->group_id ) {337 $groups[ $index]->fields[] = $field;416 if ( $group->id === $field->group_id ) { 417 $groups[ $index ]->fields[] = $field; 338 418 } 339 419 } … … 341 421 // When we unset fields above, we may have created empty groups. 342 422 // Remove them, if necessary. 343 if ( empty( $group->fields ) && $hide_empty_groups) {344 unset( $groups[ $index] );423 if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) { 424 unset( $groups[ $index ] ); 345 425 } 346 426 … … 368 448 } 369 449 450 // Setup empty arrays 370 451 $groups = array(); 371 452 $uncached_gids = array(); 372 453 454 // Loop through groups and look for cached & uncached data 373 455 foreach ( $group_ids as $group_id ) { 374 456 375 457 // If cached data is found, use it 376 if ( $group_data = wp_cache_get( 'xprofile_group_' . $group_id, 'bp' ) ) { 458 $cache_key = 'xprofile_group_' . $group_id; 459 $group_data = wp_cache_get( $cache_key, 'bp' ); 460 if ( false !== $group_data ) { 377 461 $groups[ $group_id ] = $group_data; 378 462 … … 388 472 // Fetch uncached data from the DB if necessary 389 473 if ( ! empty( $uncached_gids ) ) { 474 475 // Setup IN query for uncached group data 390 476 $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) ); 391 477 392 $bp = buddypress(); 478 // Get table name to query 479 $table_name = buddypress()->profile->table_name_groups; 393 480 394 481 // Fetch data, preserving order 395 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )"); 396 397 // Put queried data into the placeholders created earlier, 398 // and add it to the cache 399 foreach ( (array) $queried_gdata as $gdata ) { 400 $groups[ $gdata->id ] = $gdata; 401 wp_cache_set( 'xprofile_group_' . $gdata->id, $gdata, 'bp' ); 402 } 403 } 404 405 // Reset indexes 406 $groups = array_values( $groups ); 407 408 return $groups; 409 } 410 482 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )"); 483 484 // Make sure query returned valid data 485 if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) { 486 487 // Put queried data into the placeholders created earlier, 488 // and add it to the cache 489 foreach ( (array) $queried_gdata as $gdata ) { 490 491 // Add group to groups array 492 $groups[ $gdata->id ] = $gdata; 493 494 // Cache previously uncached group data 495 $cache_key = 'xprofile_group_' . $gdata->id; 496 wp_cache_set( $cache_key, $gdata, 'bp' ); 497 } 498 } 499 } 500 501 // Reset indexes & return 502 return array_values( $groups ); 503 } 504 505 /** 506 * Validate field group when form submitted 507 * 508 * @since BuddyPress (1.0.0) 509 * 510 * @global string $message 511 * 512 * @return boolean 513 */ 411 514 public static function admin_validate() { 412 515 global $message; 413 516 414 / * Validate Form */517 // Validate Form 415 518 if ( empty( $_POST['group_name'] ) ) { 416 519 $message = __( 'Please make sure you give the group a name.', 'buddypress' ); … … 421 524 } 422 525 526 /** 527 * Update field group position 528 * 529 * @since BuddyPress (1.5.0) 530 * 531 * @global $wpdb $wpdb 532 * @param int $field_group_id 533 * @param int $position 534 * 535 * @return boolean 536 */ 423 537 public static function update_position( $field_group_id, $position ) { 424 538 global $wpdb; 425 539 426 if ( ! is_numeric( $position ) ) {540 if ( ! is_numeric( $position ) ) { 427 541 return false; 428 542 } 429 543 430 // purge profile field group cache544 // Purge profile field group cache 431 545 wp_cache_delete( 'xprofile_groups_inc_empty', 'bp' ); 432 546 … … 453 567 454 568 // Does the admin allow this field to be customized? 455 $allow_custom = 'disabled' !== bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' ); 569 $visibility = bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' ); 570 $allow_custom = (bool) ( 'disabled' !== $visibility ); 456 571 457 572 // Look to see if the user has set the visibility for this field 458 if ( $allow_custom && isset( $visibility_levels[$field->id] ) ) {459 $field_visibility = $visibility_levels[ $field->id];573 if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) { 574 $field_visibility = $visibility_levels[ $field->id ]; 460 575 461 576 // If no admin-set default is saved, fall back on a global default … … 470 585 * @param string $value Default visibility value. 471 586 */ 472 $field_visibility = ! empty( $fallback_visibility ) ? $fallback_visibility : apply_filters( 'bp_xprofile_default_visibility_level', 'public' ); 473 } 474 475 $fields[$key]->visibility_level = $field_visibility; 587 $field_visibility = ! empty( $fallback_visibility ) 588 ? $fallback_visibility 589 : apply_filters( 'bp_xprofile_default_visibility_level', 'public' ); 590 } 591 592 $fields[ $key ]->visibility_level = $field_visibility; 476 593 } 477 594 … … 501 618 $default_visibility_levels = array(); 502 619 foreach ( $levels as $level ) { 503 if ( 'default_visibility' == $level->meta_key ) { 504 $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value; 505 } elseif ( 'allow_custom_visibility' == $level->meta_key ) { 506 $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value; 620 switch ( $level->meta_key ) { 621 case 'default_visibility' : 622 $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value; 623 break; 624 case 'allow_custom_visibility' : 625 $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value; 626 break; 507 627 } 508 628 } … … 514 634 } 515 635 636 /** Admin Output **********************************************************/ 637 638 /** 639 * Output the admin area field group form 640 * 641 * @since BuddyPress (1.0.0) 642 * 643 * @global string $message 644 */ 516 645 public function render_admin_form() { 517 646 global $message; 518 647 648 // New field group 519 649 if ( empty( $this->id ) ) { 520 650 $title = __( 'Add New Field Group', 'buddypress' ); 521 $action = "users.php?page=bp-profile-setup&mode=add_group";651 $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'add_group' ), 'users.php' ); 522 652 $button = __( 'Save', 'buddypress' ); 653 654 // Existing field group 523 655 } else { 524 656 $title = __( 'Edit Field Group', 'buddypress' ); 525 $action = "users.php?page=bp-profile-setup&mode=edit_group&group_id=" . $this->id;657 $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'edit_group', 'group_id' => $this->id ), 'users.php' ); 526 658 $button = __( 'Update', 'buddypress' ); 527 659 } ?> … … 533 665 <h2><?php echo esc_html( $title ); ?></h2> 534 666 535 <?php if ( ! empty( $message ) ) : ?>667 <?php if ( ! empty( $message ) ) : ?> 536 668 537 669 <div id="message" class="error fade"> … … 570 702 * @param BP_XProfile_Group $this Current XProfile group. 571 703 */ 572 do_action( 'xprofile_group_before_submitbox', $this ); 573 ?> 704 do_action( 'xprofile_group_before_submitbox', $this ); ?> 574 705 575 706 <div id="submitdiv" class="postbox"> … … 588 719 * @param BP_XProfile_Group $this Current XProfile group. 589 720 */ 590 do_action( 'xprofile_group_submitbox_start', $this ); 591 ?> 721 do_action( 'xprofile_group_submitbox_start', $this ); ?> 592 722 593 723 <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" /> … … 613 743 * @param BP_XProfile_Group $this Current XProfile group. 614 744 */ 615 do_action( 'xprofile_group_after_submitbox', $this ); 616 ?> 745 do_action( 'xprofile_group_after_submitbox', $this ); ?> 746 617 747 </div> 618 748 </div> … … 621 751 </div> 622 752 623 <?php753 <?php 624 754 } 625 755 }
Note: See TracChangeset
for help on using the changeset viewer.