Changeset 11041
- Timestamp:
- 08/30/2016 03:17:16 PM (8 years ago)
- Location:
- trunk/src/bp-xprofile
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/admin/css/admin.css
r10022 r11041 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 } -
trunk/src/bp-xprofile/admin/js/admin.js
r10557 r11041 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';83 } 84 85 if ( XProfileAdmin. supports_options_field_types.indexOf( forWhat ) >= 0 ) {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 } 84 85 if ( XProfileAdmin.do_settings_section_field_types.indexOf( forWhat ) >= 0 ) { 86 86 document.getElementById( forWhat ).style.display = ''; 87 87 do_autolink = 'on'; … … 270 270 // Handle title placeholder text the WordPress way 271 271 titleHint( 'title' ); 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 } ); 272 313 273 314 // tabs init with a custom tab template and an "add" callback filling in the content -
trunk/src/bp-xprofile/bp-xprofile-admin.php
r11035 r11041 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. -
trunk/src/bp-xprofile/bp-xprofile-cssjs.php
r10613 r11041 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 ); … … 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 } -
trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php
r10855 r11041 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 33 $this->do_settings_section = true; 32 34 33 35 /** … … 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 } … … 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. 320 * 321 * @since 2.0.0 330 * Get settings for a given date field. 331 * 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 ( false !== $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 ] = $settings[ $setting ]; 424 } 425 break; 426 } 427 } 428 429 // Sanitize and validate saved settings. 430 $saved_settings = self::validate_settings( $saved_settings ); 431 432 foreach ( $saved_settings as $setting_key => $setting_value ) { 433 bp_xprofile_update_meta( $field_id, 'field', $setting_key, $setting_value ); 434 } 435 436 return true; 437 } 438 439 /** 440 * Generate the settings markup for Date fields. 441 * 442 * @since 2.7.0 322 443 * 323 444 * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen. … … 325 446 * field's child options. 326 447 */ 327 public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {} 448 public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) { 449 $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 450 451 if ( false === $type ) { 452 return; 453 } 454 455 $class = $current_field->type != $type ? 'display: none;' : ''; 456 $current_type_obj = bp_xprofile_create_field_type( $type ); 457 458 $settings = self::get_field_settings( $current_field->id ); 459 ?> 460 461 <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 462 <table class="form-table bp-date-options"> 463 <tr> 464 <th scope="row"> 465 <?php esc_html_e( 'Date format', 'buddypress' ); ?> 466 </th> 467 468 <td> 469 <fieldset> 470 <legend class="screen-reader-text"> 471 <?php esc_html_e( 'Date format', 'buddypress' ); ?> 472 </legend> 473 474 <?php foreach ( $this->get_date_formats() as $format ): ?> 475 <div class="bp-date-format-option"> 476 <label for="date-format-<?php echo esc_attr( $format ); ?>"> 477 <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'] ); ?> /> 478 <span class="date-format-label"><?php echo date_i18n( $format ); ?></span> 479 <code><?php echo esc_html( $format ); ?></code> 480 </label> 481 </div> 482 <?php endforeach;?> 483 484 <div class="bp-date-format-option"> 485 <label for="date-format-elapsed"> 486 <input type="radio" name="field_settings[date_format]" id="date-format-elapsed" <?php checked( 'elapsed', $settings['date_format'] ); ?> value="elapsed" /> 487 <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' ); ?> 488 </label> 489 </div> 490 491 <div class="bp-date-format-option"> 492 <label for="date-format-custom"> 493 <input type="radio" name="field_settings[date_format]" id="date-format-custom" <?php checked( 'custom', $settings['date_format'] ); ?> value="custom" /> 494 <span class="date-format-label"><?php esc_html_e( 'Custom:', 'buddypress' ); ?></span> 495 <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> 496 </label> 497 </div> 498 </fieldset> 499 500 <p><a href="https://codex.wordpress.org/Formatting_Date_and_Time"><?php esc_html_e( 'Documentation on date and time formatting', 'buddypress' ); ?></a></p> 501 </td> 502 </tr> 503 504 <tr> 505 <th scope="row"> 506 <?php esc_html_e( 'Range', 'buddypress' ); ?> 507 </th> 508 509 <td> 510 <fieldset> 511 <legend class="screen-reader-text"> 512 <?php esc_html_e( 'Range', 'buddypress' ); ?> 513 </legend> 514 515 <div class="bp-date-format-option"> 516 <label> 517 <input type="radio" name="field_settings[range_type]" id="range_type_absolute" value="absolute" <?php checked( 'absolute', $settings['range_type'] ); ?> /> 518 519 <?php 520 printf( 521 /* translators: 1: Start year for date range, 2: End year for date range */ 522 __( 'From %1$s to %2$s', 'buddypress' ), 523 sprintf( '<input class="date-range-numeric" type="text" name="field_settings[range_absolute_start]" value="%s" />', esc_attr( $settings['range_absolute_start'] ) ), 524 sprintf( '<input class="date-range-numeric" type="text" name="field_settings[range_absolute_end]" value="%s" />', esc_attr( $settings['range_absolute_end'] ) ) 525 ); 526 ?> 527 </label> 528 </div> 529 530 <div class="bp-date-format-option"> 531 <label> 532 <input type="radio" name="field_settings[range_type]" id="range_type_relative" value="relative" <?php checked( 'relative', $settings['range_type'] ); ?> /> 533 534 <?php 535 printf( 536 /* translators: 1: "From" year count for date range, 2: "To" year count for date range */ 537 __( 'From %1$s to %2$s', 'buddypress' ), 538 sprintf( 539 '<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>', 540 esc_attr( abs( $settings['range_relative_start'] ) ), 541 selected( true, $settings['range_relative_start'] <= 0, false ), 542 esc_attr__( 'years ago', 'buddypress' ), 543 selected( true, $settings['range_relative_start'] > 0, false ), 544 esc_attr__( 'years from now', 'buddypress' ) 545 ), 546 sprintf( 547 '<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>', 548 esc_attr( abs( $settings['range_relative_end'] ) ), 549 selected( true, $settings['range_relative_end'] <= 0, false ), 550 esc_attr__( 'years ago', 'buddypress' ), 551 selected( true, $settings['range_relative_end'] > 0, false ), 552 esc_attr__( 'years from now', 'buddypress' ) 553 ) 554 ); 555 ?> 556 </label> 557 </div> 558 </fieldset> 559 </td> 560 </tr> 561 </table> 562 </div> 563 <?php 564 } 328 565 329 566 /** … … 345 582 } 346 583 347 return bp_format_time( $field_value, true, false ); 584 $settings = self::get_field_settings( $field_id ); 585 586 switch ( $settings['date_format'] ) { 587 case 'elapsed' : 588 $formatted = bp_core_time_since( $field_value ); 589 break; 590 591 case 'custom' : 592 $formatted = date( $settings['date_format_custom'], $field_value ); 593 break; 594 595 default : 596 $formatted = date( $settings['date_format'], $field_value ); 597 break; 598 } 599 600 return $formatted; 601 } 602 603 public function get_date_formats() { 604 $date_formats = array_unique( apply_filters( 'date_formats', array( __( 'F j, Y', 'buddypress' ), 'Y-m-d', 'm/d/Y', 'd/m/Y' ) ) ); 605 606 return apply_filters( 'bp_xprofile_date_field_date_formats', $date_formats ); 348 607 } 349 608 } -
trunk/src/bp-xprofile/classes/class-bp-xprofile-field-type.php
r10828 r11041 81 81 */ 82 82 public $supports_richtext = false; 83 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|null Boolean if set explicitly by the type object, otherwise null. 89 */ 90 protected $do_settings_section = null; 83 91 84 92 /** … … 227 235 */ 228 236 return (bool) apply_filters( 'bp_xprofile_field_type_is_valid', $validated, $values, $this ); 237 } 238 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; 229 254 } 230 255 … … 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 -
trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php
r11035 r11041 1104 1104 1105 1105 return true; 1106 } 1107 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 ); 1106 1119 } 1107 1120
Note: See TracChangeset
for help on using the changeset viewer.