[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field-type-datebox.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 2.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Datebox xprofile field type.
  15   *
  16   * @since 2.0.0
  17   */
  18  class BP_XProfile_Field_Type_Datebox extends BP_XProfile_Field_Type {
  19  
  20      /**
  21       * Constructor for the datebox field type.
  22       *
  23       * @since 2.0.0
  24       */
  25  	public function __construct() {
  26          parent::__construct();
  27  
  28          $this->category = _x( 'Single Fields', 'xprofile field type category', 'buddypress' );
  29          $this->name     = _x( 'Date Selector', 'xprofile field type', 'buddypress' );
  30  
  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;
  34  
  35          /**
  36           * Fires inside __construct() method for BP_XProfile_Field_Type_Datebox class.
  37           *
  38           * @since 2.0.0
  39           *
  40           * @param BP_XProfile_Field_Type_Datebox $this Current instance of
  41           *                                             the field type datebox.
  42           */
  43          do_action( 'bp_xprofile_field_type_datebox', $this );
  44      }
  45  
  46      /**
  47       * Output the edit field HTML for this field type.
  48       *
  49       * Must be used inside the {@link bp_profile_fields()} template loop.
  50       *
  51       * @since 2.0.0
  52       *
  53       * @param array $raw_properties Optional key/value array of
  54       *                              {@link http://dev.w3.org/html5/markup/input.html permitted attributes}
  55       *                              that you want to add.
  56       */
  57  	public function edit_field_html( array $raw_properties = array() ) {
  58  
  59          // User_id is a special optional parameter that we pass to.
  60          // {@link bp_the_profile_field_options()}.
  61          if ( isset( $raw_properties['user_id'] ) ) {
  62              $user_id = (int) $raw_properties['user_id'];
  63              unset( $raw_properties['user_id'] );
  64          } else {
  65              $user_id = bp_displayed_user_id();
  66          }
  67  
  68          $day_r = bp_parse_args( $raw_properties, array(
  69              'id'   => bp_get_the_profile_field_input_name() . '_day',
  70              'name' => bp_get_the_profile_field_input_name() . '_day'
  71          ) );
  72  
  73          $month_r = bp_parse_args( $raw_properties, array(
  74              'id'   => bp_get_the_profile_field_input_name() . '_month',
  75              'name' => bp_get_the_profile_field_input_name() . '_month'
  76          ) );
  77  
  78          $year_r = bp_parse_args( $raw_properties, array(
  79              'id'   => bp_get_the_profile_field_input_name() . '_year',
  80              'name' => bp_get_the_profile_field_input_name() . '_year'
  81          ) ); ?>
  82  
  83              <legend>
  84                  <?php bp_the_profile_field_name(); ?>
  85                  <?php bp_the_profile_field_required_label(); ?>
  86              </legend>
  87  
  88              <?php if ( bp_get_the_profile_field_description() ) : ?>
  89                  <p class="description" tabindex="0"><?php bp_the_profile_field_description(); ?></p>
  90              <?php endif; ?>
  91  
  92              <div class="input-options datebox-selects">
  93  
  94                  <?php
  95  
  96                  /**
  97                   * Fires after field label and displays associated errors for the field.
  98                   *
  99                   * This is a dynamic hook that is dependent on the associated
 100                   * field ID. The hooks will be similar to `bp_field_12_errors`
 101                   * where the 12 is the field ID. Simply replace the 12 with
 102                   * your needed target ID.
 103                   *
 104                   * @since 1.8.0
 105                   */
 106                  do_action( bp_get_the_profile_field_errors_action() ); ?>
 107  
 108                  <label for="<?php bp_the_profile_field_input_name(); ?>_day" class="xprofile-field-label"><?php
 109                      esc_html_e( 'Day', 'buddypress' );
 110                  ?></label>
 111                  <select <?php echo $this->get_edit_field_html_elements( $day_r ); ?>>
 112                      <?php bp_the_profile_field_options( array(
 113                          'type'    => 'day',
 114                          'user_id' => $user_id
 115                      ) ); ?>
 116                  </select>
 117  
 118                  <label for="<?php bp_the_profile_field_input_name(); ?>_month" class="xprofile-field-label"><?php
 119                      esc_html_e( 'Month', 'buddypress' );
 120                  ?></label>
 121                  <select <?php echo $this->get_edit_field_html_elements( $month_r ); ?>>
 122                      <?php bp_the_profile_field_options( array(
 123                          'type'    => 'month',
 124                          'user_id' => $user_id
 125                      ) ); ?>
 126                  </select>
 127  
 128                  <label for="<?php bp_the_profile_field_input_name(); ?>_year" class="xprofile-field-label"><?php
 129                      esc_html_e( 'Year', 'buddypress' );
 130                  ?></label>
 131                  <select <?php echo $this->get_edit_field_html_elements( $year_r ); ?>>
 132                      <?php bp_the_profile_field_options( array(
 133                          'type'    => 'year',
 134                          'user_id' => $user_id
 135                      ) ); ?>
 136                  </select>
 137  
 138              </div>
 139  
 140      <?php
 141      }
 142  
 143      /**
 144       * Output the edit field options HTML for this field type.
 145       *
 146       * BuddyPress considers a field's "options" to be, for example, the items in a selectbox.
 147       * These are stored separately in the database, and their templating is handled separately.
 148       *
 149       * This templating is separate from {@link BP_XProfile_Field_Type::edit_field_html()} because
 150       * it's also used in the wp-admin screens when creating new fields, and for backwards compatibility.
 151       *
 152       * Must be used inside the {@link bp_profile_fields()} template loop.
 153       *
 154       * @since 2.0.0
 155       *
 156       * @param array $args Optional. The arguments passed to {@link bp_the_profile_field_options()}.
 157       */
 158  	public function edit_field_options_html( array $args = array() ) {
 159  
 160          $date       = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] );
 161          $day        = 0;
 162          $month      = 0;
 163          $year       = 0;
 164          $html       = '';
 165          $eng_months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
 166  
 167          // Set day, month, year defaults.
 168          if ( ! empty( $date ) ) {
 169  
 170              // If Unix timestamp.
 171              if ( is_numeric( $date ) ) {
 172                  $day   = date( 'j', $date );
 173                  $month = date( 'F', $date );
 174                  $year  = date( 'Y', $date );
 175  
 176              // If MySQL timestamp.
 177              } else {
 178                  $day   = mysql2date( 'j', $date );
 179                  $month = mysql2date( 'F', $date, false ); // Not localized, so that selected() works below.
 180                  $year  = mysql2date( 'Y', $date );
 181              }
 182          }
 183  
 184          // Check for updated posted values, and errors preventing them from
 185          // being saved first time.
 186          if ( ! empty( $_POST['field_' . $this->field_obj->id . '_day'] ) ) {
 187              $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day'];
 188              $day     = ( $day != $new_day ) ? $new_day : $day;
 189          }
 190  
 191          if ( ! empty( $_POST['field_' . $this->field_obj->id . '_month'] ) ) {
 192              if ( in_array( $_POST['field_' . $this->field_obj->id . '_month'], $eng_months ) ) {
 193                  $new_month = $_POST['field_' . $this->field_obj->id . '_month'];
 194              } else {
 195                  $new_month = $month;
 196              }
 197  
 198              $month = ( $month !== $new_month ) ? $new_month : $month;
 199          }
 200  
 201          if ( ! empty( $_POST['field_' . $this->field_obj->id . '_year'] ) ) {
 202              $new_year = (int) $_POST['field_' . $this->field_obj->id . '_year'];
 203              $year     = ( $year != $new_year ) ? $new_year : $year;
 204          }
 205  
 206          // $type will be passed by calling function when needed.
 207          switch ( $args['type'] ) {
 208              case 'day':
 209                  $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $day, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
 210  
 211                  for ( $i = 1; $i < 32; ++$i ) {
 212                      $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $day, $i, false ), (int) $i );
 213                  }
 214              break;
 215  
 216              case 'month':
 217                  $months = array(
 218                      __( 'January',   'buddypress' ),
 219                      __( 'February',  'buddypress' ),
 220                      __( 'March',     'buddypress' ),
 221                      __( 'April',     'buddypress' ),
 222                      __( 'May',       'buddypress' ),
 223                      __( 'June',      'buddypress' ),
 224                      __( 'July',      'buddypress' ),
 225                      __( 'August',    'buddypress' ),
 226                      __( 'September', 'buddypress' ),
 227                      __( 'October',   'buddypress' ),
 228                      __( 'November',  'buddypress' ),
 229                      __( 'December',  'buddypress' )
 230                  );
 231  
 232                  $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $month, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
 233  
 234                  for ( $i = 0; $i < 12; ++$i ) {
 235                      $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $eng_months[$i] ), selected( $month, $eng_months[$i], false ), $months[$i] );
 236                  }
 237              break;
 238  
 239              case 'year':
 240                  $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $year, 0, false ), /* translators: no option picked in select box */ __( '----', 'buddypress' ) );
 241  
 242                  $settings = $this->get_field_settings( $this->field_obj->id );
 243  
 244                  if ( 'relative' === $settings['range_type'] ) {
 245                      $start = date( 'Y' ) + $settings['range_relative_start'];
 246                      $end   = date( 'Y' ) + $settings['range_relative_end'];
 247                  } else {
 248                      $start = $settings['range_absolute_start'];
 249                      $end   = $settings['range_absolute_end'];
 250                  }
 251  
 252                  for ( $i = $end; $i >= $start; $i-- ) {
 253                      $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i );
 254                  }
 255              break;
 256          }
 257  
 258          /**
 259           * Filters the output for the profile field datebox.
 260           *
 261           * @since 1.1.0
 262           *
 263           * @param string $html  HTML output for the field.
 264           * @param string $value Which date type is being rendered for.
 265           * @param string $day   Date formatted for the current day.
 266           * @param string $month Date formatted for the current month.
 267           * @param string $year  Date formatted for the current year.
 268           * @param int    $id    ID of the field object being rendered.
 269           * @param string $date  Current date.
 270           */
 271          echo apply_filters( 'bp_get_the_profile_field_datebox', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date );
 272      }
 273  
 274      /**
 275       * Output HTML for this field type on the wp-admin Profile Fields screen.
 276       *
 277       * Must be used inside the {@link bp_profile_fields()} template loop.
 278       *
 279       * @since 2.0.0
 280       *
 281       * @param array $raw_properties Optional key/value array of permitted attributes that you want to add.
 282       */
 283  	public function admin_field_html( array $raw_properties = array() ) {
 284  
 285          $day_r = bp_parse_args( $raw_properties, array(
 286              'id'   => bp_get_the_profile_field_input_name() . '_day',
 287              'name' => bp_get_the_profile_field_input_name() . '_day'
 288          ) );
 289  
 290          $month_r = bp_parse_args( $raw_properties, array(
 291              'id'   => bp_get_the_profile_field_input_name() . '_month',
 292              'name' => bp_get_the_profile_field_input_name() . '_month'
 293          ) );
 294  
 295          $year_r = bp_parse_args( $raw_properties, array(
 296              'id'   => bp_get_the_profile_field_input_name() . '_year',
 297              'name' => bp_get_the_profile_field_input_name() . '_year'
 298          ) ); ?>
 299  
 300          <label for="<?php bp_the_profile_field_input_name(); ?>_day" class="xprofile-field-label"><?php
 301              esc_html_e( 'Day', 'buddypress' );
 302          ?></label>
 303          <select <?php echo $this->get_edit_field_html_elements( $day_r ); ?>>
 304              <?php bp_the_profile_field_options( array( 'type' => 'day' ) ); ?>
 305          </select>
 306  
 307          <label for="<?php bp_the_profile_field_input_name(); ?>_month" class="xprofile-field-label"><?php
 308              esc_html_e( 'Month', 'buddypress' );
 309          ?></label>
 310          <select <?php echo $this->get_edit_field_html_elements( $month_r ); ?>>
 311              <?php bp_the_profile_field_options( array( 'type' => 'month' ) ); ?>
 312          </select>
 313  
 314          <label for="<?php bp_the_profile_field_input_name(); ?>_year" class="xprofile-field-label"><?php
 315              esc_html_e( 'Year', 'buddypress' );
 316          ?></label>
 317          <select <?php echo $this->get_edit_field_html_elements( $year_r ); ?>>
 318              <?php bp_the_profile_field_options( array( 'type' => 'year' ) ); ?>
 319          </select>
 320  
 321      <?php
 322      }
 323  
 324      /**
 325       * Get settings for a given date field.
 326       *
 327       * @since 2.7.0
 328       *
 329       * @param int $field_id ID of the field.
 330       * @return array
 331       */
 332  	public static function get_field_settings( $field_id ) {
 333          $defaults = array(
 334              'date_format'          => 'Y-m-d',
 335              'date_format_custom'   => '',
 336              'range_type'           => 'absolute',
 337              'range_absolute_start' => date( 'Y' ) - 60,
 338              'range_absolute_end'   => date( 'Y' ) + 10,
 339              'range_relative_start' => '-10',
 340              'range_relative_end'   => '20',
 341          );
 342  
 343          $settings = array();
 344          foreach ( $defaults as $key => $value ) {
 345              $saved = bp_xprofile_get_meta( $field_id, 'field', $key, true );
 346  
 347              if ( $saved ) {
 348                  $settings[ $key ] = $saved;
 349              } else {
 350                  $settings[ $key ] = $value;
 351              }
 352          }
 353  
 354          $settings = self::validate_settings( $settings );
 355  
 356          return $settings;
 357      }
 358  
 359      /**
 360       * Validate date field settings.
 361       *
 362       * @since 2.7.0
 363       *
 364       * @param array $settings Raw settings.
 365       * @return array Validated settings.
 366       */
 367  	public static function validate_settings( $settings ) {
 368          foreach ( $settings as $key => &$value ) {
 369              switch ( $key ) {
 370                  case 'range_type' :
 371                      if ( $value !== 'absolute' ) {
 372                          $value = 'relative';
 373                      }
 374                  break;
 375  
 376                  // @todo More date restrictions?
 377                  case 'range_absolute_start' :
 378                  case 'range_absolute_end' :
 379                      $value = absint( $value );
 380                  break;
 381  
 382                  case 'range_relative_start' :
 383                  case 'range_relative_end' :
 384                      $value = intval( $value );
 385                  break;
 386              }
 387          }
 388  
 389          return $settings;
 390      }
 391  
 392      /**
 393       * Save settings from the field edit screen in the Dashboard.
 394       *
 395       * @param int   $field_id ID of the field.
 396       * @param array $settings Array of settings.
 397       * @return bool True on success.
 398       */
 399  	public function admin_save_settings( $field_id, $settings ) {
 400          $existing_settings = self::get_field_settings( $field_id );
 401  
 402          $saved_settings = array();
 403          foreach ( array_keys( $existing_settings ) as $setting ) {
 404              switch ( $setting ) {
 405                  case 'range_relative_start' :
 406                  case 'range_relative_end' :
 407                      $op_key = $setting . '_type';
 408                      if ( isset( $settings[ $op_key ] ) && 'past' === $settings[ $op_key ] ) {
 409                          $value = 0 - intval( $settings[ $setting ] );
 410                      } else {
 411                          $value = intval( $settings[ $setting ] );
 412                      }
 413  
 414                      $saved_settings[ $setting ] = $value;
 415                  break;
 416  
 417                  default :
 418                      if ( isset( $settings[ $setting ] ) ) {
 419                          $saved_settings[ $setting ] = $settings[ $setting ];
 420                      }
 421                  break;
 422              }
 423          }
 424  
 425          // Sanitize and validate saved settings.
 426          $saved_settings = self::validate_settings( $saved_settings );
 427  
 428          foreach ( $saved_settings as $setting_key => $setting_value ) {
 429              bp_xprofile_update_meta( $field_id, 'field', $setting_key, $setting_value );
 430          }
 431  
 432          return true;
 433      }
 434  
 435      /**
 436       * Generate the settings markup for Date fields.
 437       *
 438       * @since 2.7.0
 439       *
 440       * @param BP_XProfile_Field $current_field The current profile field on the add/edit screen.
 441       * @param string            $control_type  Optional. HTML input type used to render the current
 442       *                                         field's child options.
 443       */
 444  	public function admin_new_field_html( BP_XProfile_Field $current_field, $control_type = '' ) {
 445          $type = array_search( get_class( $this ), bp_xprofile_get_field_types() );
 446  
 447          if ( false === $type ) {
 448              return;
 449          }
 450  
 451          $class = $current_field->type != $type ? 'display: none;' : '';
 452  
 453          $settings = self::get_field_settings( $current_field->id );
 454          ?>
 455  
 456  <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;">
 457      <table class="form-table bp-date-options">
 458          <tr>
 459              <th scope="row">
 460                  <?php esc_html_e( 'Date format', 'buddypress' ); ?>
 461              </th>
 462  
 463              <td>
 464                  <fieldset>
 465                      <legend class="screen-reader-text">
 466                          <?php esc_html_e( 'Date format', 'buddypress' ); ?>
 467                      </legend>
 468  
 469                      <?php foreach ( $this->get_date_formats() as $format ): ?>
 470                          <div class="bp-date-format-option">
 471                              <label for="date-format-<?php echo esc_attr( $format ); ?>">
 472                                  <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'] ); ?> />
 473                                  <span class="date-format-label"><?php echo date_i18n( $format ); ?></span>
 474                                  <code><?php echo esc_html( $format ); ?></code>
 475                              </label>
 476                          </div>
 477                      <?php endforeach;?>
 478  
 479                      <div class="bp-date-format-option">
 480                          <label for="date-format-elapsed">
 481                              <input type="radio" name="field-settings[date_format]" id="date-format-elapsed" <?php checked( 'elapsed', $settings['date_format'] ); ?> value="elapsed" aria-describedby="date-format-elapsed-setting" />
 482                              <span class="date-format-label" id="date-format-elapsed-setting"><?php esc_html_e( 'Time elapsed', 'buddypress' ); ?></span> <?php _e( '<code>4 years ago</code>, <code>4 years from now</code>', 'buddypress' ); ?>
 483                          </label>
 484                      </div>
 485  
 486                      <div class="bp-date-format-option">
 487                          <label for="date-format-custom">
 488                              <input type="radio" name="field-settings[date_format]" id="date-format-custom" <?php checked( 'custom', $settings['date_format'] ); ?> value="custom" />
 489                              <span class="date-format-label"><?php esc_html_e( 'Custom:', 'buddypress' ); ?></span>
 490                          </label>
 491                          <label for="date-format-custom-value" class="screen-reader-text"><?php esc_html_e( 'Enter custom time format', 'buddypress' ); ?></label>
 492                          <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'] ); ?>" aria-describedby="date-format-custom-example" /> <span class="screen-reader-text"><?php esc_html_e( 'Example:', 'buddypress' ); ?></span><span class="date-format-custom-example" id="date-format-custom-sample"><?php if ( $settings['date_format_custom'] ) : ?><?php echo esc_html( date_i18n( $settings['date_format_custom'] ) ); endif; ?></span><span class="spinner" id="date-format-custom-spinner" aria-hidden="true"></span>
 493  
 494                          <p><a href="https://codex.wordpress.org/Formatting_Date_and_Time"><?php esc_html_e( 'Documentation on date and time formatting', 'buddypress' ); ?></a></p>
 495                      </div>
 496  
 497                  </fieldset>
 498              </td>
 499          </tr>
 500  
 501          <tr>
 502              <th scope="row">
 503                  <?php esc_html_e( 'Range', 'buddypress' ); ?>
 504              </th>
 505  
 506              <td>
 507                  <fieldset class="bp-range-types">
 508                      <legend class="screen-reader-text">
 509                          <?php esc_html_e( 'Range', 'buddypress' ); ?>
 510                      </legend>
 511  
 512                      <div class="bp-date-format-option">
 513                          <div class="bp-date-range-type-label">
 514                              <label for="range_type_absolute">
 515                                  <input type="radio" name="field-settings[range_type]" id="range_type_absolute" value="absolute" <?php checked( 'absolute', $settings['range_type'] ); ?> />
 516                                  <?php esc_html_e( 'Absolute', 'buddypress' ); ?>
 517                              </label>
 518                          </div>
 519  
 520                          <div class="bp-date-range-type-values">
 521                              <label for="field-settings[range_absolute_start]" aria-label="Year"><?php esc_html_e( 'Start:', 'buddypress' ); ?></label>
 522                              <?php printf( '<input class="date-range-numeric" type="text" name="field-settings[range_absolute_start]" id="field-settings[range_absolute_start]" value="%s" />', esc_attr( $settings['range_absolute_start'] ) ); ?>
 523                              <label for="field-settings[range_absolute_end]" aria-label="Year"><?php esc_html_e( 'End:', 'buddypress' ); ?></label>
 524                              <?php printf( '<input class="date-range-numeric" type="text" name="field-settings[range_absolute_end]" id="field-settings[range_absolute_end]" value="%s" />', esc_attr( $settings['range_absolute_end'] ) ); ?>
 525                          </div>
 526                      </div>
 527  
 528                      <div class="bp-date-format-option">
 529                          <div class="bp-date-range-type-label">
 530                              <label for="range_type_relative">
 531                                  <input type="radio" name="field-settings[range_type]" id="range_type_relative" value="relative" <?php checked( 'relative', $settings['range_type'] ); ?> />
 532                                  <?php esc_html_e( 'Relative', 'buddypress' ); ?>
 533                              </label>
 534                          </div>
 535  
 536                          <div class="bp-date-range-type-values">
 537                              <label for="field-settings[range_relative_start]"><?php esc_html_e( 'Start:', 'buddypress' ); ?></label>
 538                              <?php printf( '<input type="text" class="date-range-numeric" name="field-settings[range_relative_start]" id="field-settings[range_relative_start]" value="%s" />',
 539                                  esc_attr( abs( $settings['range_relative_start'] ) )
 540                                  );
 541                              ?>
 542  
 543                              <label class="screen-reader-text" for="field-settings[range_relative_start_type]"><?php esc_html_e( 'Select range', 'buddypress' ); ?></label>
 544                              <?php printf( '<select name="field-settings[range_relative_start_type]" id="field-settings[range_relative_start_type]"><option value="past" %s>%s</option><option value="future" %s>%s</option></select>',
 545                                  selected( true, $settings['range_relative_start'] <= 0, false ),
 546                                  esc_attr__( 'years ago', 'buddypress' ),
 547                                  selected( true, $settings['range_relative_start'] > 0, false ),
 548                                  esc_attr__( 'years from now', 'buddypress' )
 549                                  );
 550                              ?>
 551  
 552                              <label for="field-settings[range_relative_end]"><?php esc_html_e( 'End:', 'buddypress' ); ?></label>
 553                              <?php printf( '<input type="text" class="date-range-numeric" name="field-settings[range_relative_end]" id="field-settings[range_relative_end]" value="%s" />',
 554                                  esc_attr( abs( $settings['range_relative_end'] ) )
 555                                  );
 556                              ?>
 557                              <label class="screen-reader-text" for="field-settings[range_relative_end_type]"><?php esc_html_e( 'Select range', 'buddypress' ); ?></label>
 558                              <?php printf( '<select name="field-settings[range_relative_end_type]" id="field-settings[range_relative_end_type]"><option value="past" %s>%s</option><option value="future" %s>%s</option></select>',
 559                                      selected( true, $settings['range_relative_end'] <= 0, false ),
 560                                      esc_attr__( 'years ago', 'buddypress' ),
 561                                      selected( true, $settings['range_relative_end'] > 0, false ),
 562                                      esc_attr__( 'years from now', 'buddypress' )
 563                                  );
 564                              ?>
 565                          </div>
 566                      </div>
 567  
 568                  </fieldset>
 569              </td>
 570          </tr>
 571      </table>
 572  </div>
 573          <?php
 574      }
 575  
 576      /**
 577       * Format Date values for display.
 578       *
 579       * @since 2.1.0
 580       * @since 2.4.0 Added the `$field_id` parameter.
 581       *
 582       * @param string     $field_value The date value, as saved in the database. Typically, this is a MySQL-formatted
 583       *                                date string (Y-m-d H:i:s).
 584       * @param string|int $field_id    Optional. ID of the field.
 585       * @return string Date formatted by bp_format_time().
 586       */
 587  	public static function display_filter( $field_value, $field_id = '' ) {
 588          if ( ! $field_value ) {
 589              return $field_value;
 590          }
 591  
 592          // If Unix timestamp.
 593          if ( ! is_numeric( $field_value ) ) {
 594              $field_value = strtotime( $field_value );
 595          }
 596  
 597          $settings = self::get_field_settings( $field_id );
 598  
 599          switch ( $settings['date_format'] ) {
 600              case 'elapsed' :
 601                  $formatted = bp_core_time_since( $field_value );
 602              break;
 603  
 604              case 'custom' :
 605                  $formatted = date_i18n( $settings['date_format_custom'], $field_value );
 606              break;
 607  
 608              default :
 609                  $formatted = date_i18n( $settings['date_format'], $field_value );
 610              break;
 611          }
 612  
 613          return $formatted;
 614      }
 615  
 616      /**
 617       * Gets the default date formats available when configuring a Date field.
 618       *
 619       * @since 2.7.0
 620       *
 621       * @return array
 622       */
 623  	public function get_date_formats() {
 624          $date_formats = array_unique( apply_filters( 'date_formats', array( __( 'F j, Y', 'buddypress' ), 'Y-m-d', 'm/d/Y', 'd/m/Y' ) ) );
 625  
 626  
 627          /**
 628           * Filters the available date formats for XProfile date fields.
 629           *
 630           * @since 2.7.0
 631           *
 632           * @param array $date_formats
 633           */
 634          return apply_filters( 'bp_xprofile_date_field_date_formats', $date_formats );
 635      }
 636  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1