Bxcft_Field_Type_Birthdate

The BuddyPress Xprofile Custom Fields Type Bxcft Field Type Birthdate class.

Defined (1)

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

/classes/Bxcft_Field_Type_Birthdate.php  
  1. class Bxcft_Field_Type_Birthdate extends BP_XProfile_Field_Type 
  2. const OPTION_SHOW_AGE = 'show_age'; 
  3.  
  4. private $_englishMonths = array(); 
  5. private $_months = array(); 
  6.  
  7. public function __construct() { 
  8. parent::__construct(); 
  9.  
  10. $this->name = _x( 'Birthdate Selector', 'xprofile field type', 'bxcft' ); 
  11. $this->supports_options = true; 
  12.  
  13. $this->set_format( '/^\d{4}-\d{1, 2}-\d{1, 2} 00:00:00$/', 'replace' ); // "Y-m-d 00:00:00" 
  14. do_action( 'bp_xprofile_field_type_birthdate', $this ); 
  15.  
  16. private function getEnglishMonths() { 
  17. if (!$this->_englishMonths) { 
  18. $this->_englishMonths = array( 
  19. 'January', 'February', 'March', 'April', 'May', 'June', 'July',  
  20. 'August', 'September', 'October', 'November', 'December' 
  21. ); 
  22.  
  23. return $this->_englishMonths; 
  24.  
  25. private function getMonths() { 
  26. if (!$this->_months) { 
  27. $this->_months = array( 
  28. 0 => __( 'January', 'buddypress' ),  
  29. 1 => __( 'February', 'buddypress' ),  
  30. 2 => __( 'March', 'buddypress' ),  
  31. 3 => __( 'April', 'buddypress' ),  
  32. 4 => __( 'May', 'buddypress' ),  
  33. 5 => __( 'June', 'buddypress' ),  
  34. 6 => __( 'July', 'buddypress' ),  
  35. 7 => __( 'August', 'buddypress' ),  
  36. 8 => __( 'September', 'buddypress' ),  
  37. 9 => __( 'October', 'buddypress' ),  
  38. 10 => __( 'November', 'buddypress' ),  
  39. 11 => __( 'December', 'buddypress' ) 
  40. ); 
  41.  
  42. return $this->_months; 
  43.  
  44. public function admin_field_html (array $raw_properties = array ()) 
  45. $day_html = $this->get_edit_field_html_elements( array_merge( 
  46. array( 
  47. 'id' => bp_get_the_profile_field_input_name() . '_day',  
  48. 'name' => bp_get_the_profile_field_input_name() . '_day',  
  49. 'class' => 'bxcft-birthdate-day' 
  50. ),  
  51. $raw_properties 
  52. ) ); 
  53.  
  54. $month_html = $this->get_edit_field_html_elements( array_merge( 
  55. array( 
  56. 'id' => bp_get_the_profile_field_input_name() . '_month',  
  57. 'name' => bp_get_the_profile_field_input_name() . '_month',  
  58. 'class' => 'bxcft-birthdate-month' 
  59. ),  
  60. $raw_properties 
  61. ) ); 
  62.  
  63. $year_html = $this->get_edit_field_html_elements( array_merge( 
  64. array( 
  65. 'id' => bp_get_the_profile_field_input_name() . '_year',  
  66. 'name' => bp_get_the_profile_field_input_name() . '_year',  
  67. 'class' => 'bxcft-birthdate-year' 
  68. ),  
  69. $raw_properties 
  70. ) ); 
  71. ?> 
  72. <select <?php echo $day_html; ?>> 
  73. <?php bp_the_profile_field_options( 'type=day' ); ?> 
  74. </select> 
  75.  
  76. <select <?php echo $month_html; ?>> 
  77. <?php bp_the_profile_field_options( 'type=month' ); ?> 
  78. </select> 
  79.  
  80. <select <?php echo $year_html; ?>> 
  81. <?php bp_the_profile_field_options( 'type=year' ); ?> 
  82. </select> 
  83. <?php 
  84.  
  85. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  86. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  87. if ( false === $type ) { 
  88. return; 
  89.  
  90. $class = $current_field->type != $type ? 'display: none;' : ''; 
  91. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  92.  
  93. $options = $current_field->get_children( true ); 
  94. if ( ! $options ) { 
  95. $options = array(); 
  96. $i = 1; 
  97. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  98. $is_default_option = true; 
  99.  
  100. $options[] = (object) array( 
  101. 'id' => -1,  
  102. 'is_default_option' => $is_default_option,  
  103. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  104. ); 
  105.  
  106. ++$i; 
  107.  
  108. if ( ! $options ) { 
  109. $options[] = (object) array( 
  110. 'id' => -1,  
  111. 'is_default_option' => false,  
  112. 'name' => '',  
  113. ); 
  114. ?> 
  115. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  116. <h3><?php esc_html_e( 'Show age (hide birthdate):', 'bxcft' ); ?></h3> 
  117. <div class="inside"> 
  118. <p> 
  119. <?php _e('Check this if you want to show age instead of birthdate:', 'bxcft'); ?> 
  120. <input type="hidden" 
  121. name="<?php echo esc_attr( "{$type}_option[0]" ); ?>" 
  122. id="<?php echo esc_attr( "{$type}_option0" ); ?>" 
  123. value="show_birthdate" /> 
  124. <input type="checkbox" 
  125. name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" 
  126. id="<?php echo esc_attr( "{$type}_option1" ); ?>" 
  127. value="<?php echo Bxcft_Field_Type_Birthdate::OPTION_SHOW_AGE; ?>" 
  128. <?php if ($options[0]->name == Bxcft_Field_Type_Birthdate::OPTION_SHOW_AGE) : ?>checked="checked"<?php endif; ?>/> 
  129. </p> 
  130. </div> 
  131.  
  132. <h3><?php esc_html_e( 'Define a minimum age:', 'bxcft' ); ?></h3> 
  133. <div class="inside"> 
  134. <p> 
  135. <?php _e('Minimum age:', 'bxcft'); ?> 
  136. <input type="number" 
  137. name="<?php echo esc_attr( "{$type}_option[2]" ); ?>" 
  138. id="<?php echo esc_attr( "{$type}_option2" ); ?>" 
  139. min="1" max="100" 
  140. value="<?php if (count($options) > 0) { echo $options[1]->name; } ?>" /> 
  141. </p> 
  142. </div> 
  143. </div> 
  144. <?php 
  145.  
  146. public function edit_field_html (array $raw_properties = array ()) 
  147. $user_id = bp_displayed_user_id(); 
  148.  
  149. // user_id is a special optional parameter that we pass to {@link bp_the_profile_field_options()}. 
  150. if ( isset( $raw_properties['user_id'] ) ) { 
  151. $user_id = (int) $raw_properties['user_id']; 
  152. unset( $raw_properties['user_id'] ); 
  153.  
  154. // HTML5 required attribute. 
  155. if ( bp_get_the_profile_field_is_required() ) { 
  156. $raw_properties['required'] = 'required'; 
  157.  
  158. $day_html = $this->get_edit_field_html_elements( array_merge( 
  159. array( 
  160. 'id' => bp_get_the_profile_field_input_name() . '_day',  
  161. 'name' => bp_get_the_profile_field_input_name() . '_day',  
  162. 'class' => 'bxcft-birthdate-day' 
  163. ),  
  164. $raw_properties 
  165. ) ); 
  166.  
  167. $month_html = $this->get_edit_field_html_elements( array_merge( 
  168. array( 
  169. 'id' => bp_get_the_profile_field_input_name() . '_month',  
  170. 'name' => bp_get_the_profile_field_input_name() . '_month',  
  171. 'class' => 'bxcft-birthdate-month' 
  172. ),  
  173. $raw_properties 
  174. ) ); 
  175.  
  176. $year_html = $this->get_edit_field_html_elements( array_merge( 
  177. array( 
  178. 'id' => bp_get_the_profile_field_input_name() . '_year',  
  179. 'name' => bp_get_the_profile_field_input_name() . '_year',  
  180. 'class' => 'bxcft-birthdate-year' 
  181. ),  
  182. $raw_properties 
  183. ) ); 
  184.  
  185. $label = sprintf( 
  186. '<label for="%s_day">%s%s</label>',  
  187. bp_get_the_profile_field_input_name(),  
  188. bp_get_the_profile_field_name(),  
  189. (bp_get_the_profile_field_is_required()) ? 
  190. ' ' . esc_html__( '(required)', 'buddypress') : '' 
  191. ); 
  192. // Label. 
  193. echo apply_filters('bxcft_field_label', $label, bp_get_the_profile_field_id(), bp_get_the_profile_field_type(), bp_get_the_profile_field_input_name(), bp_get_the_profile_field_name(), bp_get_the_profile_field_is_required()); 
  194. // Errors 
  195. do_action( bp_get_the_profile_field_errors_action() ); 
  196. // Input. 
  197. ?> 
  198. <select <?php echo $day_html; ?>> 
  199. <?php bp_the_profile_field_options( array( 'type' => 'day', 'user_id' => $user_id ) ); ?> 
  200. </select> 
  201.  
  202. <select <?php echo $month_html; ?>> 
  203. <?php bp_the_profile_field_options( array( 'type' => 'month', 'user_id' => $user_id ) ); ?> 
  204. </select> 
  205.  
  206. <select <?php echo $year_html; ?>> 
  207. <?php bp_the_profile_field_options( array( 'type' => 'year', 'user_id' => $user_id ) ); ?> 
  208. </select> 
  209. <script> 
  210. if (bxcft_months === undefined) { 
  211. var bxcft_months = []; 
  212. <?php 
  213. $months = $this->getMonths(); 
  214. foreach ($months as $k => $m): 
  215. printf("bxcft_months[%s] = '%s';", $k, $m); 
  216. endforeach; 
  217. ?> 
  218. </script> 
  219. <?php 
  220.  
  221. public function edit_field_options_html( array $args = array() ) { 
  222. $options = $this->field_obj->get_children(); 
  223. $date = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ); 
  224.  
  225. $day = 0; 
  226. $month = ''; 
  227. $year = 0; 
  228. $html = ''; 
  229.  
  230. // Set day, month, year defaults 
  231. if ( ! empty( $date ) ) { 
  232.  
  233. // If Unix timestamp 
  234. if ( is_numeric( $date ) ) { 
  235. $day = date( 'j', $date ); 
  236. $month = date( 'F', $date ); 
  237. $year = date( 'Y', $date ); 
  238.  
  239. // If MySQL timestamp 
  240. } else { 
  241. $day = mysql2date( 'j', $date ); 
  242. $month = mysql2date( 'F', $date, false ); // Not localized, so that selected() works below 
  243. $year = mysql2date( 'Y', $date ); 
  244.  
  245. // Check for updated posted values, and errors preventing them from being saved first time. 
  246. if ( ! empty( $_POST['field_' . $this->field_obj->id . '_day'] ) ) { 
  247. $new_day = (int) $_POST['field_' . $this->field_obj->id . '_day']; 
  248. $day = ( $day != $new_day ) ? $new_day : $day; 
  249.  
  250. if ( ! empty( $_POST['field_' . $this->field_obj->id . '_month'] ) ) { 
  251. $new_month = $_POST['field_' . $this->field_obj->id . '_month']; 
  252. $month = ( $month != $new_month ) ? $new_month : $month; 
  253.  
  254. if ( ! empty( $_POST['field_' . $this->field_obj->id . '_year'] ) ) { 
  255. $new_year = (int) $_POST['field_' . $this->field_obj->id . '_year']; 
  256. $year = ( $year != $new_year ) ? $new_year : $year; 
  257.  
  258. // $type will be passed by calling function when needed 
  259. switch ( $args['type'] ) { 
  260. case 'day': 
  261. $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $day, 0, false ), /** translators: no option picked in select box */ __( '----', 'buddypress' ) ); 
  262.  
  263. for ( $i = 1; $i < 32; ++$i ) { 
  264. $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $day, $i, false ), (int) $i ); 
  265. break; 
  266.  
  267. case 'month': 
  268. $eng_months = $this->getEnglishMonths(); 
  269. $months = $this->getMonths(); 
  270.  
  271. $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $month, 0, false ), /** translators: no option picked in select box */ __( '----', 'buddypress' ) ); 
  272.  
  273. for ( $i = 0; $i < 12; ++$i ) { 
  274. $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', esc_attr( $eng_months[$i] ), selected( $month, $eng_months[$i], false ), $months[$i] ); 
  275. break; 
  276.  
  277. case 'year': 
  278. $html = sprintf( '<option value="" %1$s>%2$s</option>', selected( $year, 0, false ), /** translators: no option picked in select box */ __( '----', 'buddypress' ) ); 
  279.  
  280. for ( $i = date('Y', time()-60*60*24); $i > 1901; $i-- ) { 
  281. $html .= sprintf( '<option value="%1$s" %2$s>%3$s</option>', (int) $i, selected( $year, $i, false ), (int) $i ); 
  282. break; 
  283.  
  284. echo apply_filters( 'bp_get_the_profile_field_birthdate', $html, $args['type'], $day, $month, $year, $this->field_obj->id, $date ); 
  285.  
  286. /** 
  287. * Overriden, we cannot validate against the whitelist. 
  288. * @param type $values 
  289. * @return type 
  290. */ 
  291. public function is_valid( $values ) { 
  292. $validated = false; 
  293.  
  294. foreach ( (array) $values as $value ) { 
  295.  
  296. foreach ( $this->validation_regex as $format ) { 
  297. if ( 1 === preg_match( $format, $value ) ) { 
  298. $validated = true; 
  299. continue; 
  300.  
  301. } else { 
  302. $validated = false; 
  303.  
  304. if ( ! $validated && is_array( $values ) && empty( $values ) && $this->accepts_null_value ) { 
  305. $validated = true; 
  306.  
  307. return (bool) apply_filters( 'bp_xprofile_field_type_is_valid', $validated, $values, $this ); 
  308.  
  309. /** 
  310. * Modify the appearance of value. Apply autolink if enabled. 
  311. * @param string $value Original value of field 
  312. * @param int $field_id Id of field 
  313. * @return string Value formatted 
  314. */ 
  315. public static function display_filter($field_value, $field_id = '') { 
  316.  
  317. $new_field_value = $field_value; 
  318.  
  319. if (!empty($field_value) && !empty($field_id)) { 
  320. $field = BP_XProfile_Field::get_instance($field_id); 
  321.  
  322. if ($field) { 
  323. $show_age = false; 
  324.  
  325. // Looking for "show_age" flag. 
  326. $childs = $field->get_children(); 
  327. if (!empty($childs)) { 
  328. foreach ($childs as $c) { 
  329. if ($c->name == Bxcft_Field_Type_Birthdate::OPTION_SHOW_AGE) { 
  330. $show_age = true; 
  331. break; 
  332.  
  333. if ($show_age) { 
  334. // Calculate age. 
  335. $new_field_value = floor((time() - strtotime($field_value)) / 31556926); 
  336. } else { 
  337. // Display birthdate with WP Settings Date Format. 
  338. $new_field_value = date_i18n( get_option('date_format'),  
  339. strtotime($field_value) ); 
  340.  
  341. $do_autolink = apply_filters('bxcft_do_autolink',  
  342. $field->get_do_autolink()); 
  343.  
  344. if ($do_autolink) { 
  345. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  346. $search_url = add_query_arg( array( $query_arg => urlencode( $field_value ) ),  
  347. bp_get_members_directory_permalink() ); 
  348. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  349. '" rel="nofollow">' . $new_field_value . '</a>'; 
  350.  
  351. /** 
  352. * bxcft_birthdate_display_filter 
  353. * Use this filter to modify the appearance of Birthdate field value. 
  354. * @param $new_field_value Value of field 
  355. * @param $field_id Id of field. 
  356. * @return Filtered value of field. 
  357. */ 
  358. return apply_filters('bxcft_birthdate_display_filter', $new_field_value, $field_id);