Bxcft_Field_Type_CheckboxAcceptance

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

Defined (1)

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

/classes/Bxcft_Field_Type_CheckboxAcceptance.php  
  1. class Bxcft_Field_Type_CheckboxAcceptance extends BP_XProfile_Field_Type 
  2. public function __construct() { 
  3. parent::__construct(); 
  4.  
  5. $this->name = _x( 'Checkbox Acceptance', 'xprofile field type', 'bxcft' ); 
  6.  
  7. $this->accepts_null_value = true; 
  8. $this->supports_options = true; 
  9.  
  10. $this->set_format( '/^.+$/', 'replace' ); 
  11. do_action( 'bp_xprofile_field_type_checkbox_acceptance', $this ); 
  12.  
  13. public function admin_field_html (array $raw_properties = array ()) 
  14. global $field; 
  15.  
  16. $options = $field->get_children( true ); 
  17. $text = ''; 
  18. foreach ($options as $option) { 
  19. $text .= rawurldecode($option->name); 
  20.  
  21. $html = $this->get_edit_field_html_elements( array_merge( 
  22. array( 'type' => 'checkbox' ),  
  23. $raw_properties 
  24. ) ); 
  25. ?> 
  26. <label for="<?php bp_the_profile_field_input_name(); ?>"> 
  27. <input <?php echo $html; ?>> 
  28. <?php echo $text; ?> 
  29. </label> 
  30. <?php 
  31.  
  32. public function edit_field_html (array $raw_properties = array ()) 
  33. $user_id = bp_displayed_user_id(); 
  34.  
  35. if ( isset( $raw_properties['user_id'] ) ) { 
  36. $user_id = (int) $raw_properties['user_id']; 
  37. unset( $raw_properties['user_id'] ); 
  38.  
  39. // HTML5 required attribute. 
  40. if ( bp_get_the_profile_field_is_required() ) { 
  41. $raw_properties['required'] = 'required'; 
  42. $required = true; 
  43. } else { 
  44. $required = false; 
  45. ?> 
  46. <label for="<?php bp_the_profile_field_input_name(); ?>"><?php bp_the_profile_field_name(); ?> <?php if ( bp_get_the_profile_field_is_required() ) : ?><?php esc_html_e( '(required)', 'buddypress' ); ?><?php endif; ?></label> 
  47. <?php do_action( bp_get_the_profile_field_errors_action() ); ?> 
  48. <?php bp_the_profile_field_options( "user_id={$user_id}&required={$required}" ); ?> 
  49. <?php 
  50.  
  51. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  52. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  53. if ( false === $type ) { 
  54. return; 
  55.  
  56. $class = $current_field->type != $type ? 'display: none;' : ''; 
  57. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  58.  
  59. $text = ''; 
  60. $options = $current_field->get_children( true ); 
  61. if ( ! $options ) { 
  62. $options = array(); 
  63. $i = 1; 
  64. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  65. if ( $current_type_obj->supports_options && ! $current_type_obj->supports_multiple_defaults && isset( $_POST["isDefault_{$type}_option"][$i] ) && (int) $_POST["isDefault_{$type}_option"] === $i ) { 
  66. $is_default_option = true; 
  67. } elseif ( isset( $_POST["isDefault_{$type}_option"][$i] ) ) { 
  68. $is_default_option = (bool) $_POST["isDefault_{$type}_option"][$i]; 
  69. } else { 
  70. $is_default_option = false; 
  71.  
  72. $options[] = (object) array( 
  73. 'id' => 0,  
  74. 'is_default_option' => $is_default_option,  
  75. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  76. ); 
  77.  
  78. $text .= sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ); 
  79. ++$i; 
  80.  
  81. if ( ! $options ) { 
  82. $options[] = (object) array( 
  83. 'id' => 0,  
  84. 'is_default_option' => false,  
  85. 'name' => '',  
  86. ); 
  87. } else { 
  88. foreach ($options as $option) { 
  89. $text .= rawurldecode($option->name); 
  90. ?> 
  91. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  92. <h3><?php esc_html_e( 'Use this field to write a text that should be displayed beside the checkbox:', 'bxcft' ); ?></h3> 
  93. <div class="inside"> 
  94. <p> 
  95. <textarea name="<?php echo esc_attr( "{$type}_text" ); ?>" 
  96. id="<?php echo esc_attr( "{$type}_text" ); ?>" rows="5" cols="60"><?php echo $text; ?></textarea> 
  97. </p> 
  98. </div> 
  99. <?php if ($options):$i=1; ?> 
  100. <?php foreach ($options as $option): ?> 
  101. <input type="hidden" name="<?php echo esc_attr( "{$type}_option[{$i}]"); ?>" 
  102. id ="<?php echo esc_attr( "{$type}_option{$i}"); ?>" value="<?php echo $option->name; ?>" /> 
  103. <?php $i++; endforeach; ?> 
  104. <?php endif; ?> 
  105. </div> 
  106. <?php 
  107.  
  108. public function edit_field_options_html( array $args = array() ) 
  109. $options = $this->field_obj->get_children(); 
  110. $checkbox_acceptance = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] )); 
  111.  
  112. if ( !empty($_POST['field_' . $this->field_obj->id]) ) { 
  113. $new_checkbox_acceptance = $_POST['field_' . $this->field_obj->id]; 
  114. $checkbox_acceptance = ( $checkbox_acceptance != $new_checkbox_acceptance ) ? $new_checkbox_acceptance : $checkbox_acceptance; 
  115.  
  116. $html = '<input type="checkbox" name="check_acc_'.bp_get_the_profile_field_input_name().'" id="check_acc_'.bp_get_the_profile_field_input_name().'"'; 
  117. if ($checkbox_acceptance == 1) { 
  118. $html .= ' checked="checked"'; 
  119. if (isset($args['required']) && $args['required']) { 
  120. $html .= ' required="required" aria-required="true"'; 
  121. $html .= ' value="1" /> '; 
  122.  
  123. $html .= '<input type="hidden" name="'.bp_get_the_profile_field_input_name().'" id="'.bp_get_the_profile_field_input_name().'"'; 
  124. if ($checkbox_acceptance == 1) { 
  125. $html .= ' value="1" /> '; 
  126. } else { 
  127. $html .= ' value="0" /> '; 
  128. if ($options) { 
  129. foreach ($options as $option) { 
  130. $html .= rawurldecode($option->name); 
  131.  
  132. // Javascript. 
  133. $html .= ' 
  134. <script> 
  135. jQuery(document).ready(function() { 
  136. jQuery("#check_acc_'.bp_get_the_profile_field_input_name().'").click(function() { 
  137. if (jQuery(this).is(":checked")) { 
  138. jQuery("#'.bp_get_the_profile_field_input_name().'").val("1"); 
  139. } else { 
  140. jQuery("#'.bp_get_the_profile_field_input_name().'").val("0"); 
  141. }); 
  142. }); 
  143. </script> 
  144. '; 
  145.  
  146. echo apply_filters( 'bp_get_the_profile_field_checkbox_acceptance', $html, $args['type'], $this->field_obj->id, $checkbox_acceptance ); 
  147.  
  148. public function is_valid( $values ) { 
  149. $this->validation_whitelist = null; 
  150. return parent::is_valid($values); 
  151.  
  152. /** 
  153. * Modify the appearance of value. Apply autolink if enabled. 
  154. * @param string $value Original value of field 
  155. * @param int $field_id Id of field 
  156. * @return string Value formatted 
  157. */ 
  158. public static function display_filter($field_value, $field_id = '') { 
  159.  
  160. $new_field_value = $field_value; 
  161.  
  162. if ($field_value !== '' && !empty($field_id)) { 
  163. $field = BP_XProfile_Field::get_instance($field_id); 
  164.  
  165. if ($field) { 
  166. $new_field_value = ((int)$field_value == 1) ? 
  167. __('yes', 'bxcft') : __('no', 'bxcft'); 
  168.  
  169. $do_autolink = apply_filters('bxcft_do_autolink',  
  170. $field->get_do_autolink()); 
  171.  
  172. if ($do_autolink) { 
  173. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  174. $search_url = add_query_arg( array( $query_arg => urlencode( $field_value ) ),  
  175. bp_get_members_directory_permalink() ); 
  176. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  177. '" rel="nofollow">' . $new_field_value . '</a>'; 
  178.  
  179. /** 
  180. * bxcft_checkbox_acceptance_display_filter 
  181. * Use this filter to modify the appearance of Checkbox Acceptance 
  182. * field value. 
  183. * @param $new_field_value Value of field 
  184. * @param $field_id Id of field. 
  185. * @return Filtered value of field. 
  186. */ 
  187. return apply_filters('bxcft_checkbox_acceptance_display_filter',  
  188. $new_field_value, $field_id);