Ticket #2947: 2947.003.patch
File 2947.003.patch, 13.8 KB (added by , 14 years ago) |
---|
-
bp-xprofile/bp-xprofile-templatetags.php
187 187 if ( $class ) 188 188 $css_classes[] = sanitize_title( esc_attr( $class ) ); 189 189 190 / * Set a class with the field ID */190 // Set a class with the field ID 191 191 $css_classes[] = 'field_' . $profile_template->field->id; 192 192 193 / * Set a class with the field name (sanitized) */193 // Set a class with the field name (sanitized) 194 194 $css_classes[] = 'field_' . sanitize_title( $profile_template->field->name ); 195 195 196 196 if ( $profile_template->current_field % 2 == 1 ) … … 359 359 return apply_filters( 'bp_get_the_profile_field_input_name', 'field_' . $field->id . $array_box ); 360 360 } 361 361 362 /** 363 * bp_the_profile_field_options() 364 * 365 * Displays field options HTML for field types of 'selectbox', 'multiselectbox', 366 * 'radio', 'checkbox', and 'datebox'. 367 * 368 * @package BuddyPress Xprofile 369 * @since 1.1 370 * 371 * @uses bp_get_the_profile_field_options() 372 * 373 * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'. 374 */ 362 375 function bp_the_profile_field_options( $args = '' ) { 363 376 echo bp_get_the_profile_field_options( $args ); 364 377 } 378 /** 379 * bp_get_the_profile_field_options() 380 * 381 * Retrieves field options HTML for field types of 'selectbox', 'multiselectbox', 382 * 'radio', 'checkbox', and 'datebox'. 383 * 384 * @package BuddyPress Xprofile 385 * @since 1.1 386 * 387 * @uses BP_XProfile_Field::get_children() 388 * @uses BP_XProfile_ProfileData::get_value_byid() 389 * 390 * @param array $args Specify type for datebox. Allowed 'day', 'month', 'year'. 391 */ 365 392 function bp_get_the_profile_field_options( $args = '' ) { 366 393 global $field; 367 394 … … 377 404 378 405 $options = $field->get_children(); 379 406 407 // Setup some defaults 408 $html = ''; 409 $selected = ''; 410 380 411 switch ( $field->type ) { 381 412 382 413 case 'selectbox': case 'multiselectbox': … … 384 415 $html .= '<option value="">--------</option>'; 385 416 386 417 for ( $k = 0; $k < count($options); $k++ ) { 387 $o ption_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) );388 $option_values = (array) $option_values;418 $original_option_values = maybe_unserialize( BP_XProfile_ProfileData::get_value_byid( $options[$k]->parent_id ) ); 419 $option_values = (array) $original_option_values; 389 420 390 / * Check for updated posted values, but errors preventing them from being saved first time */391 foreach( (array)$option_values as $i => $option_value ) {421 // Check for updated posted values, but errors preventing them from being saved first time 422 foreach( $option_values as $i => $option_value ) { 392 423 if ( isset( $_POST['field_' . $field->id] ) && $_POST['field_' . $field->id] != $option_value ) { 393 424 if ( !empty( $_POST['field_' . $field->id] ) ) 394 425 $option_values[$i] = $_POST['field_' . $field->id]; 395 426 } 396 427 } 397 428 398 if ( in_array( $options[$k]->name, (array)$option_values ) || $options[$k]->is_default_option ) { 429 $selected = ''; 430 431 // First, check to see whether the user-entered value 432 // matches 433 if ( in_array( $options[$k]->name, (array) $option_values ) ) 399 434 $selected = ' selected="selected"'; 400 } else {401 $selected = '';402 }403 435 404 $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '">' . stripslashes( esc_attr( $options[$k]->name ) ) . '</option>', $options[$k] ); 436 // Then, if the user has not provided a value, check for 437 // defaults 438 if ( !is_array( $original_option_values ) && empty( $option_values ) & $options[$k]->is_default_option ) 439 $selected = ' selected="selected"'; 440 441 $html .= apply_filters( 'bp_get_the_profile_field_options_select', '<option' . $selected . ' value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '">' . esc_attr( stripslashes( $options[$k]->name ) ) . '</option>', $options[$k] ); 405 442 } 406 443 break; 407 444 408 445 case 'radio': 409 $html = '<div id="field_' . $field->id . '">';446 $html .= '<div id="field_' . $field->id . '">'; 410 447 411 448 for ( $k = 0; $k < count($options); $k++ ) { 412 449 $option_value = BP_XProfile_ProfileData::get_value_byid($options[$k]->parent_id); 413 450 414 / * Check for updated posted values, but errors preventing them from being saved first time */451 // Check for updated posted values, but errors preventing them from being saved first time 415 452 if ( isset( $_POST['field_' . $field->id] ) && $option_value != $_POST['field_' . $field->id] ) { 416 453 if ( !empty( $_POST['field_' . $field->id] ) ) 417 454 $option_value = $_POST['field_' . $field->id]; 418 455 } 419 420 if ( $option_value == $options[$k]->name || $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) ) { 456 457 $selected = ''; 458 if ( $option_value == $options[$k]->name || !empty( $value ) && $value == $options[$k]->name || ( empty( $option_value ) && $options[$k]->is_default_option ) ) 421 459 $selected = ' checked="checked"'; 422 } else {423 $selected = '';424 }425 460 426 $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] );461 $html .= apply_filters( 'bp_get_the_profile_field_options_radio', '<label><input' . $selected . ' type="radio" name="field_' . $field->id . '" id="option_' . $options[$k]->id . '" value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '"> ' . esc_attr( stripslashes( $options[$k]->name ) ) . '</label>', $options[$k] ); 427 462 } 428 463 429 464 $html .= '</div>'; … … 432 467 case 'checkbox': 433 468 $option_values = BP_XProfile_ProfileData::get_value_byid($options[0]->parent_id); 434 469 435 / * Check for updated posted values, but errors preventing them from being saved first time */470 // Check for updated posted values, but errors preventing them from being saved first time 436 471 if ( isset( $_POST['field_' . $field->id] ) && $option_values != maybe_serialize( $_POST['field_' . $field->id] ) ) { 437 472 if ( !empty( $_POST['field_' . $field->id] ) ) 438 473 $option_values = $_POST['field_' . $field->id]; 439 474 } 440 475 441 476 $option_values = maybe_unserialize($option_values); 442 443 $html = ''; 477 444 478 for ( $k = 0; $k < count($options); $k++ ) { 445 479 $selected = ''; 480 481 // First, check to see whether the user's saved values 482 // match the option 446 483 for ( $j = 0; $j < count($option_values); $j++ ) { 447 if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) || $options[$k]->is_default_option) {484 if ( $option_values[$j] == $options[$k]->name || @in_array( $options[$k]->name, $value ) ) { 448 485 $selected = ' checked="checked"'; 449 486 break; 450 487 } 451 488 } 489 490 // If the user has not yet supplied a value for this field, 491 // check to see whether there is a default value available 492 if ( !is_array( $option_values ) && empty( $option_values ) && !$selected && $options[$k]->is_default_option) { 493 $selected = ' checked="checked"'; 494 } 452 495 453 $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . stripslashes( esc_attr( $options[$k]->name ) ) . '"> ' . stripslashes( esc_attr( $options[$k]->name ) ) . '</label>', $options[$k] ); 454 $selected = ''; 496 $html .= apply_filters( 'bp_get_the_profile_field_options_checkbox', '<label><input' . $selected . ' type="checkbox" name="field_' . $field->id . '[]" id="field_' . $options[$k]->id . '_' . $k . '" value="' . esc_attr( stripslashes( $options[$k]->name ) ) . '"> ' . esc_attr( stripslashes( $options[$k]->name ) ) . '</label>', $options[$k] ); 455 497 } 456 498 break; 457 499 458 500 case 'datebox': 501 $date = BP_XProfile_ProfileData::get_value_byid( $field->id ); 459 502 460 if ( !empty( $field->data->value ) ) { 461 $day = date("j", $field->data->value); 462 $month = date("F", $field->data->value); 463 $year = date("Y", $field->data->value); 464 $default_select = ' selected="selected"'; 503 // Set day, month, year defaults 504 $day = ''; 505 $month = ''; 506 $year = ''; 507 508 if ( !empty( $date ) ) { 509 $day = date( 'j', $date ); 510 $month = date( 'F', $date ); 511 $year = date( 'Y', $date ); 465 512 } 466 513 467 / * Check for updated posted values, but errors preventing them from being saved first time */514 // Check for updated posted values, but errors preventing them from being saved first time 468 515 if ( !empty( $_POST['field_' . $field->id . '_day'] ) ) { 469 516 if ( $day != $_POST['field_' . $field->id . '_day'] ) 470 517 $day = $_POST['field_' . $field->id . '_day']; … … 482 529 483 530 switch ( $type ) { 484 531 case 'day': 485 $html .= '<option value=""' . esc_attr( $default_select) . '>--</option>';532 $html .= '<option value=""' . selected( $day, '', false ) . '>--</option>'; 486 533 487 534 for ( $i = 1; $i < 32; $i++ ) { 488 if ( $day == $i ) { 489 $selected = ' selected = "selected"'; 490 } else { 491 $selected = ''; 492 } 493 $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; 535 $html .= '<option value="' . $i .'"' . selected( $day, $i, false ) . '>' . $i . '</option>'; 494 536 } 495 537 break; 496 538 … … 503 545 __( 'October', 'buddypress' ), __( 'November', 'buddypress' ), __( 'December', 'buddypress' ) 504 546 ); 505 547 506 $html .= '<option value=""' . esc_attr( $default_select) . '>------</option>';548 $html .= '<option value=""' . selected( $month, '', false ) . '>------</option>'; 507 549 508 550 for ( $i = 0; $i < 12; $i++ ) { 509 if ( $month == $eng_months[$i] ) { 510 $selected = ' selected = "selected"'; 511 } else { 512 $selected = ''; 513 } 514 515 $html .= '<option value="' . $eng_months[$i] . '"' . $selected . '>' . $months[$i] . '</option>'; 551 $html .= '<option value="' . $eng_months[$i] . '"' . selected( $month, $eng_months[$i], false ) . '>' . $months[$i] . '</option>'; 516 552 } 517 553 break; 518 554 519 555 case 'year': 520 $html .= '<option value=""' . esc_attr( $default_select) . '>----</option>';556 $html .= '<option value=""' . selected( $year, '', false ) . '>----</option>'; 521 557 522 for ( $i = date( 'Y', time() ); $i > 1899; $i-- ) { 523 if ( $year == $i ) { 524 $selected = ' selected = "selected"'; 525 } else { 526 $selected = ''; 527 } 528 529 $html .= '<option value="' . $i .'"' . $selected . '>' . $i . '</option>'; 558 for ( $i = date( 'Y' ); $i > 1899; $i-- ) { 559 $html .= '<option value="' . $i .'"' . selected( $year, $i, false ) . '>' . $i . '</option>'; 530 560 } 531 561 break; 532 562 } 533 563 534 apply_filters( 'bp_get_the_profile_field_datebox', $html, $day, $month, $year, $default_select);564 $html = apply_filters( 'bp_get_the_profile_field_datebox', $html, $type, $day, $month, $year ); 535 565 536 566 break; 537 567 } -
bp-xprofile.php
225 225 foreach ( (array)$posted_field_ids as $field_id ) { 226 226 if ( !isset( $_POST['field_' . $field_id] ) ) { 227 227 228 if ( is_numeric( $_POST['field_' . $field_id . '_day'] ) ) {228 if ( !empty( $_POST['field_' . $field_id . '_day'] ) && is_numeric( $_POST['field_' . $field_id . '_day'] ) ) { 229 229 /* Concatenate the values. */ 230 230 $date_value = $_POST['field_' . $field_id . '_day'] . ' ' . 231 231 $_POST['field_' . $field_id . '_month'] . ' ' . … … 250 250 251 251 /* Now we've checked for required fields, lets save the values. */ 252 252 foreach ( (array)$posted_field_ids as $field_id ) { 253 if ( !xprofile_set_field_data( $field_id, $bp->displayed_user->id, $_POST['field_' . $field_id], $is_required[$field_id] ) ) 253 254 // Certain types of fields (checkboxes, multiselects) may come through empty. Save them as an empty array so that they don't get overwritten by the default on the next edit. 255 if ( empty( $_POST['field_' . $field_id] ) ) 256 $value = array(); 257 else 258 $value = $_POST['field_' . $field_id]; 259 260 if ( !xprofile_set_field_data( $field_id, $bp->displayed_user->id, $value, $is_required[$field_id] ) ) 254 261 $errors = true; 255 262 else 256 do_action( 'xprofile_profile_field_data_updated', $field_id, $ _POST['field_' . $field_id]);263 do_action( 'xprofile_profile_field_data_updated', $field_id, $value ); 257 264 } 258 265 259 266 do_action( 'xprofile_updated_profile', $bp->displayed_user->id, $posted_field_ids, $errors ); … … 667 674 if ( $is_required && ( empty( $value ) || !is_array( $value ) && !strlen( trim( $value ) ) ) ) 668 675 return false; 669 676 670 /* If the value is empty, then delete any field data that exists */ 671 if ( empty( $value ) ) { 677 $field = new BP_XProfile_Field( $field_id ); 678 679 // If the value is empty, then delete any field data that exists, unless the field is of a 680 // type where null values are semantically meaningful 681 if ( empty( $value ) && 'checkbox' != $field->type && 'multiselectbox' != $field->type ) { 672 682 xprofile_delete_field_data( $field_id, $user_id ); 673 683 return true; 674 684 } 675 685 676 $field = new BP_XProfile_Field( $field_id );677 678 686 /* Check the value is an acceptable value */ 679 687 if ( 'checkbox' == $field->type || 'radio' == $field->type || 'selectbox' == $field->type || 'multiselectbox' == $field->type ) { 680 688 $options = $field->get_children(); … … 688 696 unset( $value[$i] ); 689 697 } 690 698 691 if ( empty( $value ) )692 return false;693 694 699 /* Reset the keys by merging with an empty array */ 695 700 $value = array_merge( array(), $value ); 696 701 } else {