BP_XProfile_Field_Type_Datebox

Datebox xprofile field type.

Defined (1)

The class is defined in the following location(s).

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