Ticket #5500: 5500.3.diff
| File 5500.3.diff, 18.1 KB (added by , 10 years ago) |
|---|
-
src/bp-xprofile/admin/css/admin.css
diff --git src/bp-xprofile/admin/css/admin.css src/bp-xprofile/admin/css/admin.css index 6dd7a20..4795078 100644
p.member-type-none-notice { 284 284 display: none; 285 285 visibility: hidden; 286 286 } 287 288 .form-table.bp-date-options { 289 margin: 0 8px; 290 } 291 292 .form-table.bp-date-options th { 293 width: 150px; 294 } 295 296 .date-format-label { 297 display: inline-block; 298 width: 150px; 299 } 300 301 .date-format-custom-value, 302 .date-range-numeric { 303 width: 56px; 304 } -
src/bp-xprofile/admin/js/admin.js
diff --git src/bp-xprofile/admin/js/admin.js src/bp-xprofile/admin/js/admin.js index 53f6b1c..1cafcd9 100644
function add_option(forWhat) { 78 78 function show_options( forWhat ) { 79 79 var do_autolink; 80 80 81 for ( var i = 0; i < XProfileAdmin. supports_options_field_types.length; i++ ) {82 document.getElementById( XProfileAdmin. supports_options_field_types[i] ).style.display = 'none';81 for ( var i = 0; i < XProfileAdmin.do_settings_section_field_types.length; i++ ) { 82 document.getElementById( XProfileAdmin.do_settings_section_field_types[i] ).style.display = 'none'; 83 83 } 84 84 85 if ( XProfileAdmin. supports_options_field_types.indexOf( forWhat ) >= 0 ) {85 if ( XProfileAdmin.do_settings_section_field_types.indexOf( forWhat ) >= 0 ) { 86 86 document.getElementById( forWhat ).style.display = ''; 87 87 do_autolink = 'on'; 88 88 } else { … … jQuery( document ).ready( function() { 270 270 // Handle title placeholder text the WordPress way 271 271 titleHint( 'title' ); 272 272 273 // On Date fields, selecting a date_format radio button should change the Custom value. 274 var $date_format = jQuery( 'input[name="date_format"]' ); 275 var $date_format_custom_value = jQuery( '#date-format-custom-value' ); 276 var $date_format_sample = jQuery( '#date-format-custom-sample' ); 277 $date_format.click( function( e ) { 278 switch ( e.target.value ) { 279 case 'elapsed' : 280 $date_format_custom_value.val( '' ); 281 $date_format_sample.html( '' ); 282 break; 283 284 case 'custom' : 285 break; 286 287 default : 288 $date_format_custom_value.val( e.target.value ); 289 $date_format_sample.html( jQuery( e.target ).siblings( '.date-format-label' ).html() ); 290 break; 291 } 292 } ); 293 294 // Clicking into the custom date format field should select the Custom radio button. 295 var $date_format_custom = jQuery( '#date-format-custom' ); 296 $date_format_custom_value.focus( function() { 297 $date_format_custom.prop( 'checked', 'checked' ); 298 } ); 299 300 // Validate custom date field. 301 var $date_format_spinner = jQuery( '#date-format-custom-spinner' ); 302 $date_format_custom_value.change( function( e ) { 303 $date_format_spinner.addClass( 'is-active' ); 304 jQuery.post( ajaxurl, { 305 action: 'date_format', 306 date: e.target.value 307 }, 308 function( response ) { 309 $date_format_spinner.removeClass( 'is-active' ); 310 $date_format_sample.html( response ); 311 } ); 312 } ); 313 273 314 // tabs init with a custom tab template and an "add" callback filling in the content 274 315 var $tab_items, 275 316 $tabs = jQuery( '#tabs' ).tabs(); -
src/bp-xprofile/bp-xprofile-admin.php
diff --git src/bp-xprofile/bp-xprofile-admin.php src/bp-xprofile/bp-xprofile-admin.php index cadca7b..5eae1ac 100644
function xprofile_admin_manage_field( $group_id, $field_id = null ) { 432 432 bp_xprofile_update_field_meta( $field_id, 'do_autolink', 'off' ); 433 433 } 434 434 435 if ( $field->type_obj->do_settings_section() ) { 436 $settings = isset( $_POST['field_settings'] ) ? wp_unslash( $_POST['field_settings'] ) : array(); 437 $field->admin_save_settings( $settings ); 438 } 439 435 440 /** 436 441 * Fires at the end of the process to save a field for a user, if successful. 437 442 * -
src/bp-xprofile/bp-xprofile-cssjs.php
diff --git src/bp-xprofile/bp-xprofile-cssjs.php src/bp-xprofile/bp-xprofile-cssjs.php index 221cfaa..419f5c7 100644
function xprofile_add_admin_js() { 51 51 // types that support options, for use in showing/hiding the 52 52 // "please enter options for this field" section. 53 53 $strings = array( 54 ' supports_options_field_types' => array(),54 'do_settings_section_field_types' => array(), 55 55 'do_autolink' => '', 56 56 ); 57 57 58 58 foreach ( bp_xprofile_get_field_types() as $field_type => $field_type_class ) { 59 59 $field = new $field_type_class(); 60 if ( $field-> supports_options) {61 $strings[' supports_options_field_types'][] = $field_type;60 if ( $field->do_settings_section() ) { 61 $strings['do_settings_section_field_types'][] = $field_type; 62 62 } 63 63 } 64 64 -
src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php index 6074c5b..8e8decf 100644
class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type { 30 30 31 31 $this->set_format( '/^\d{4}-\d{1,2}-\d{1,2} 00:00:00$/', 'replace' ); // "Y-m-d 00:00:00" 32 32 33 $this->do_settings_section = true; 34 33 35 /** 34 36 * Fires inside __construct() method for BP_XProfile_Field_Type_Datebox class. 35 37 * … … class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type { 239 241 case 'year': 240 242 $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $year, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) ); 241 243 242 for ( $i = 2037; $i > 1901; $i-- ) { 244 $settings = $this->get_field_settings( $this->field_obj->id ); 245 246 if ( 'relative' === $settings['range_type'] ) { 247 $start = date( 'Y' ) + $settings['range_relative_start']; 248 $end = date( 'Y' ) + $settings['range_relative_end']; 249 } else { 250 $start = $settings['range_absolute_start']; 251 $end = $settings['range_absolute_end']; 252 } 253 254 for ( $i = $end; $i >= $start; $i-- ) { 243 255 $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i ); 244 256 } 245 257 break; … … class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type { 315 327 } 316 328 317 329 /** 318 * This method usually outputs HTML for this field type's children options on the wp-admin Profile Fields 319 * "Add Field" and "Edit Field" screens, but for this field type, we don't want it, so it's stubbed out. 330 * Get settings for a given date field. 320 331 * 321 * @since 2.0.0 332 * @since 2.7.0 333 * 334 * @param int $field_id ID of the field. 335 * @return array 336 */ 337 public static function get_field_settings( $field_id ) { 338 $defaults = array( 339 'date_format' => 'Y-m-d', 340 'date_format_custom' => '', 341 'range_type' => 'absolute', 342 'range_absolute_start' => date( 'Y' ) - 60, 343 'range_absolute_end' => date( 'Y' ) + 10, 344 'range_relative_start' => '-10', 345 'range_relative_end' => '20', 346 ); 347 348 $settings = array(); 349 foreach ( $defaults as $key => $value ) { 350 $saved = bp_xprofile_get_meta( $field_id, 'field', $key, true ); 351 if ( '' !== $saved ) { 352 $settings[ $key ] = $saved; 353 } else { 354 $settings[ $key ] = $value; 355 } 356 } 357 358 $settings = self::validate_settings( $settings ); 359 360 return $settings; 361 } 362 363 /** 364 * Validate date field settings. 365 * 366 * @since 2.7.0 367 * 368 * @param array $settings Raw settings. 369 * @return array Validated settings. 370 */ 371 public static function validate_settings( $settings ) { 372 foreach ( $settings as $key => &$value ) { 373 switch ( $key ) { 374 case 'range_type' : 375 if ( $value !== 'absolute' ) { 376 $value = 'relative'; 377 } 378 break; 379 380 // @todo More date restrictions? 381 case 'range_absolute_start' : 382 case 'range_absolute_end' : 383 $value = absint( $value ); 384 break; 385 386 case 'range_relative_start' : 387 case 'range_relative_end' : 388 $value = intval( $value ); 389 break; 390 } 391 } 392 393 return $settings; 394 } 395 396 /** 397 * Save settings from the field edit screen in the Dashboard. 398 * 399 * @param int $field_id ID of the field. 400 * @param array $settings Array of settings. 401 * @return bool True on success. 402 */ 403 public function admin_save_settings( $field_id, $settings ) { 404 $existing_settings = self::get_field_settings( $field_id ); 405 406 $saved_settings = array(); 407 foreach ( array_keys( $existing_settings ) as $setting ) { 408 switch ( $setting ) { 409 case 'range_relative_start' : 410 case 'range_relative_end' : 411 $op_key = $setting . '_type'; 412 if ( isset( $settings[ $op_key ] ) && 'past' === $settings[ $op_key ] ) { 413 $value = 0 - intval( $settings[ $setting ] ); 414 } else { 415 $value = intval( $settings[ $setting ] ); 416 } 417 418 $saved_settings[ $setting ] = $value; 419 break; 420 421 default : 422 if ( isset( $settings[ $setting ] ) ) { 423 $saved_settings[ $setting ] = wp_unslash( $settings[ $setting ] ); 424 } 425 break; 426 } 427 } 428 429 $saved_settings = self::validate_settings( $saved_settings ); 430 431 foreach ( $saved_settings as $setting_key => $setting_value ) { 432 bp_xprofile_update_meta( $field_id, 'field', $setting_key, $setting_value ); 433 } 434 435 return true; 436 } 437 438 /** 439 * Generate the settings markup for Date fields. 440 * 441 * @since 2.7.0 322 442 * 323 443 * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. 324 444 * @param string $control_type Optional. HTML input type used to render the current 325 445 * field's child options. 326 446 */ 327 public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {} 447 public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { 448 $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 449 450 if ( false === $type ) { 451 return; 452 } 453 454 $class = $current_field->type != $type ? 'display: none;' : ''; 455 $current_type_obj = bp_xprofile_create_field_type( $type ); 456 457 $settings = self::get_field_settings( $current_field->id ); 458 ?> 459 460 <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 461 <table class="form-table bp-date-options"> 462 <tr> 463 <th scope="row"> 464 <?php esc_html_e( 'Date format', 'buddypress' ); ?> 465 </th> 466 467 <td> 468 <fieldset> 469 <legend class="screen-reader-text"> 470 <?php esc_html_e( 'Date format', 'buddypress' ); ?> 471 </legend> 472 473 <?php foreach ( $this->get_date_formats() as $format ): ?> 474 <label for="date-format-<?php echo esc_attr( $format ); ?>"> 475 <input type="radio" name="field_settings[date_format]" id="date-format-<?php echo esc_attr( $format ); ?>" value="<?php echo esc_attr( $format ); ?>" <?php checked( $format, $settings['date_format'] ); ?> /> 476 <span class="date-format-label"><?php echo date_i18n( $format ); ?></span> 477 <code><?php echo esc_html( $format ); ?></code> 478 </label> 479 <br /> 480 <?php endforeach;?> 481 482 <label for="date-format-elapsed"> 483 <input type="radio" name="field_settings[date_format]" id="date-format-elapsed" <?php checked( 'elapsed', $settings['date_format'] ); ?> value="elapsed" /> 484 <span class="date-format-label"><?php esc_html_e( 'Time elapsed', 'buddypress' ); ?></span> <?php _e( '<code>4 years ago</code>, <code>4 years from now</code>', 'buddypress' ); ?> 485 </label> 486 <br /> 487 488 <label for="date-format-custom"> 489 <input type="radio" name="field_settings[date_format]" id="date-format-custom" <?php checked( 'custom', $settings['date_format'] ); ?> value="custom" /> 490 <span class="date-format-label"><?php esc_html_e( 'Custom:', 'buddypress' ); ?></span> 491 <input type="text" name="field_settings[date_format_custom]" id="date-format-custom-value" class="date-format-custom-value" value="<?php echo esc_attr( $settings['date_format_custom'] ); ?>" /> <span class="screen-reader-text"><?php esc_html_e( 'Example:', 'buddypress' ); ?></span><span class="date-format-custom-sample" id="date-format-custom-sample"><?php if ( $settings['date_format_custom'] ) : ?><?php echo esc_html( date( $settings['date_format_custom'] ) ); endif; ?></span><span class="spinner" id="date-format-custom-spinner"></span> 492 </label> 493 </fieldset> 494 495 <p><a href="https://codex.wordpress.org/Formatting_Date_and_Time"><?php esc_html_e( 'Documentation on date and time formatting', 'buddypress' ); ?></a></p> 496 </td> 497 </tr> 498 499 <tr> 500 <th scope="row"> 501 <?php esc_html_e( 'Range', 'buddypress' ); ?> 502 </th> 503 504 <td> 505 <fieldset> 506 <legend class="screen-reader-text"> 507 <?php esc_html_e( 'Range', 'buddypress' ); ?> 508 </legend> 509 510 <label> 511 <input type="radio" name="field_settings[range_type]" id="range_type_absolute" value="absolute" <?php checked( 'absolute', $settings['range_type'] ); ?> /> 512 513 <?php 514 printf( 515 __( 'From %s to %s', 'buddypress' ), 516 sprintf( '<input class="date-range-numeric" type="text" name="field_settings[range_absolute_start]" value="%s" />', esc_attr( $settings['range_absolute_start'] ) ), 517 sprintf( '<input class="date-range-numeric" type="text" name="field_settings[range_absolute_end]" value="%s" />', esc_attr( $settings['range_absolute_end'] ) ) 518 ); 519 ?> 520 </label> 521 <br /> 522 523 <label> 524 <input type="radio" name="field_settings[range_type]" id="range_type_relative" value="relative" <?php checked( 'relative', $settings['range_type'] ); ?> /> 525 526 <?php 527 printf( 528 __( 'From %s to %s', 'buddypress' ), 529 sprintf( 530 '<input type="text" class="date-range-numeric" name="field_settings[range_relative_start]" value="%s" /> <select name="field_settings[range_relative_start_type]"><option value="past" %s>%s</option><option value="future" %s>%s</option></select>', 531 esc_attr( abs( $settings['range_relative_start'] ) ), 532 selected( true, $settings['range_relative_start'] <= 0, false ), 533 esc_attr__( 'years ago', 'buddypress' ), 534 selected( true, $settings['range_relative_start'] > 0, false ), 535 esc_attr__( 'years from now', 'buddypress' ) 536 ), 537 sprintf( 538 '<input type="text" class="date-range-numeric" name="field_settings[range_relative_end]" value="%s" /> <select name="field_settings[range_relative_end_type]"><option value="past" %s>%s</option><option value="future" %s>%s</option></select>', 539 esc_attr( abs( $settings['range_relative_end'] ) ), 540 selected( true, $settings['range_relative_end'] <= 0, false ), 541 esc_attr__( 'years ago', 'buddypress' ), 542 selected( true, $settings['range_relative_end'] > 0, false ), 543 esc_attr__( 'years from now', 'buddypress' ) 544 ) 545 ); 546 ?> 547 </label> 548 </fieldset> 549 </td> 550 </tr> 551 </table> 552 </div> 553 <?php 554 } 328 555 329 556 /** 330 557 * Format Date values for display. … … class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type { 344 571 $field_value = strtotime( $field_value ); 345 572 } 346 573 347 return bp_format_time( $field_value, true, false ); 574 $settings = self::get_field_settings( $field_id ); 575 576 switch ( $settings['date_format'] ) { 577 case 'elapsed' : 578 $formatted = bp_core_time_since( $field_value ); 579 break; 580 581 case 'custom' : 582 $formatted = date( $settings['date_format_custom'], $field_value ); 583 break; 584 585 default : 586 $formatted = date( $settings['date_format'], $field_value ); 587 break; 588 } 589 590 return $formatted; 591 } 592 593 public function get_date_formats() { 594 $date_formats = array_unique( apply_filters( 'date_formats', array( __( 'F j, Y' ), 'Y-m-d', 'm/d/Y', 'd/m/Y' ) ) ); 595 596 return apply_filters( 'bp_xprofile_date_field_date_formats', $date_formats ); 348 597 } 349 598 } -
src/bp-xprofile/classes/class-bp-xprofile-field-type.php
diff --git src/bp-xprofile/classes/class-bp-xprofile-field-type.php src/bp-xprofile/classes/class-bp-xprofile-field-type.php index 7fd1940..a0ee88e 100644
abstract class BP_XProfile_Field_Type { 82 82 public $supports_richtext = false; 83 83 84 84 /** 85 * If the field type has a type-specific settings section on the Edit Field panel. 86 * 87 * @since 2.7.0 88 * @var bool 89 */ 90 protected $do_settings_section; 91 92 /** 85 93 * If object is created by an BP_XProfile_Field object. 86 94 * 87 95 * @since 2.0.0 … … abstract class BP_XProfile_Field_Type { 229 237 } 230 238 231 239 /** 240 * Check whether the current field type should have a settings ("options") section on the Edit Field panel. 241 * 242 * Falls back on `supports_options` if no value is set by the field type. 243 * 244 * @since 2.7.0 245 * 246 * @return bool 247 */ 248 public function do_settings_section() { 249 if ( null === $this->do_settings_section ) { 250 $this->do_settings_section = $this->supports_options; 251 } 252 253 return (bool) $this->do_settings_section; 254 } 255 256 /** 232 257 * Output the edit field HTML for this field type. 233 258 * 234 259 * Must be used inside the {@link bp_profile_fields()} template loop. … … abstract class BP_XProfile_Field_Type { 451 476 return $field_value; 452 477 } 453 478 479 /** 480 * Save miscellaneous settings related to this field type. 481 * 482 * Override in a specific field type if it requires an admin save routine. 483 * 484 * @since 2.7.0 485 * 486 * @param int $field_id Field ID. 487 * @param array $settings Array of settings. 488 */ 489 public function admin_save_settings( $field_id, $settings ) {} 490 454 491 /** Protected *************************************************************/ 455 492 456 493 /** -
src/bp-xprofile/classes/class-bp-xprofile-field.php
diff --git src/bp-xprofile/classes/class-bp-xprofile-field.php src/bp-xprofile/classes/class-bp-xprofile-field.php index 801ca1a..87608cd 100644
class BP_XProfile_Field { 1106 1106 } 1107 1107 1108 1108 /** 1109 * Save miscellaneous settings for this field. 1110 * 1111 * Some field types have type-specific settings, which are saved here. 1112 * 1113 * @since 2.7.0 1114 * 1115 * @param array $settings Array of settings. 1116 */ 1117 public function admin_save_settings( $settings ) { 1118 return $this->type_obj->admin_save_settings( $this->id, $settings ); 1119 } 1120 1121 /** 1109 1122 * Populates the items for radio buttons, checkboxes, and dropdown boxes. 1110 1123 */ 1111 1124 public function render_admin_form_children() {
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)