BP_XProfile_Field_Type_Datebox

Datebox xprofile field type.

Description

Source

File: bp-xprofile/classes/class-bp-xprofile-field-type-datebox.php

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

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.