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


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1