Bxcft_Field_Type_SelectCustomTaxonomy

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

Defined (1)

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

/classes/Bxcft_Field_Type_SelectCustomTaxonomy.php  
  1. class Bxcft_Field_Type_SelectCustomTaxonomy extends BP_XProfile_Field_Type 
  2. public function __construct() { 
  3. parent::__construct(); 
  4.  
  5. $this->name = _x( 'Custom Taxonomy Selector', 'xprofile field type', 'bxcft' ); 
  6.  
  7. $this->supports_options = true; 
  8.  
  9. $this->set_format( '/^.+$/', 'replace' ); 
  10. do_action( 'bp_xprofile_field_type_select_custom_taxonomy', $this ); 
  11.  
  12. public function admin_field_html( array $raw_properties = array() ) { 
  13. $html = $this->get_edit_field_html_elements( $raw_properties ); 
  14. ?> 
  15. <select <?php echo $html; ?>> 
  16. <?php bp_the_profile_field_options(); ?> 
  17. </select> 
  18. <?php 
  19.  
  20. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  21. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  22. if ( false === $type ) { 
  23. return; 
  24.  
  25. $class = $current_field->type != $type ? 'display: none;' : ''; 
  26. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  27.  
  28. $options = $current_field->get_children( true ); 
  29. if ( ! $options ) { 
  30. $options = array(); 
  31. $i = 1; 
  32. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  33. if ( $current_type_obj->supports_options && ! $current_type_obj->supports_multiple_defaults && isset( $_POST["isDefault_{$type}_option"][$i] ) && (int) $_POST["isDefault_{$type}_option"] === $i ) { 
  34. $is_default_option = true; 
  35. } elseif ( isset( $_POST["isDefault_{$type}_option"][$i] ) ) { 
  36. $is_default_option = (bool) $_POST["isDefault_{$type}_option"][$i]; 
  37. } else { 
  38. $is_default_option = false; 
  39.  
  40. $options[] = (object) array( 
  41. 'id' => -1,  
  42. 'is_default_option' => $is_default_option,  
  43. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  44. ); 
  45.  
  46. ++$i; 
  47.  
  48. if ( ! $options ) { 
  49. $options[] = (object) array( 
  50. 'id' => -1,  
  51. 'is_default_option' => false,  
  52. 'name' => '',  
  53. ); 
  54.  
  55. $taxonomies = get_taxonomies(array( 
  56. 'public' => true,  
  57. '_builtin' => false,  
  58. )); 
  59. ?> 
  60. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  61. <?php 
  62. if (!$taxonomies): 
  63. ?> 
  64. <h3><?php _e('There is no custom taxonomy. You need to create at least one to use this field.', 'bxcft'); ?></h3> 
  65. <?php else : ?> 
  66. <h3><?php esc_html_e( 'Select a custom taxonomy:', 'bxcft' ); ?></h3> 
  67. <div class="inside"> 
  68. <p> 
  69. <?php _e('Select a custom taxonomy:', 'bxcft'); ?> 
  70. <select name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" id="<?php echo esc_attr( "{$type}_option[1]" ); ?>"> 
  71. <option value=""><?php _e('Select...', 'bxcft'); ?></option> 
  72. <?php foreach($taxonomies as $k=>$v): ?> 
  73. <option value="<?php echo $k; ?>"<?php if ($options[0]->name == $k): ?> selected="selected"<?php endif; ?>><?php echo $v; ?></option> 
  74. <?php endforeach; ?> 
  75. </select> 
  76. </p> 
  77. </div> 
  78. <?php endif; ?> 
  79. </div> 
  80. <?php 
  81.  
  82. public function edit_field_html (array $raw_properties = array ()) 
  83. $user_id = bp_displayed_user_id(); 
  84.  
  85. if ( isset( $raw_properties['user_id'] ) ) { 
  86. $user_id = (int) $raw_properties['user_id']; 
  87. unset( $raw_properties['user_id'] ); 
  88.  
  89. // HTML5 required attribute. 
  90. if ( bp_get_the_profile_field_is_required() ) { 
  91. $raw_properties['required'] = 'required'; 
  92.  
  93. $html = $this->get_edit_field_html_elements( $raw_properties ); 
  94. ?> 
  95. <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> 
  96. <?php do_action( bp_get_the_profile_field_errors_action() ); ?> 
  97. <select <?php echo $html; ?>> 
  98. <option value=""><?php _e('Select...', 'bxcft'); ?></option> 
  99. <?php bp_the_profile_field_options( "user_id={$user_id}" ); ?> 
  100. </select> 
  101. <?php 
  102.  
  103. public function edit_field_options_html( array $args = array() ) { 
  104. $options = $this->field_obj->get_children(); 
  105. $term_selected = BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] ); 
  106.  
  107. $html = ''; 
  108. if ($options) { 
  109. $taxonomy_selected = $options[0]->name; 
  110. if ( !empty($_POST['field_' . $this->field_obj->id]) ) { 
  111. $new_term_selected = (int) $_POST['field_' . $this->field_obj->id]; 
  112. $term_selected = ( $term_selected != $new_term_selected ) ? $new_term_selected : $term_selected; 
  113. // Get terms of custom taxonomy selected. 
  114. $terms = get_terms($taxonomy_selected, array( 
  115. 'hide_empty' => false 
  116. )); 
  117. if ($terms) { 
  118. foreach ($terms as $term) { 
  119. $html .= sprintf('<option value="%s"%s>%s</option>',  
  120. $term->term_id,  
  121. ($term_selected==$term->term_id)?' selected="selected"':'',  
  122. $term->name); 
  123.  
  124. echo apply_filters( 'bp_get_the_profile_field_select_custom_taxonomy', $html, $args['type'], $term_selected, $this->field_obj->id ); 
  125.  
  126. /** 
  127. * Overriden, we cannot validate against the whitelist. 
  128. * @param type $values 
  129. * @return type 
  130. */ 
  131. public function is_valid( $values ) { 
  132. $validated = false; 
  133.  
  134. // Some types of field (e.g. multi-selectbox) may have multiple values to check 
  135. foreach ( (array) $values as $value ) { 
  136.  
  137. // Validate the $value against the type's accepted format(s). 
  138. foreach ( $this->validation_regex as $format ) { 
  139. if ( 1 === preg_match( $format, $value ) ) { 
  140. $validated = true; 
  141. continue; 
  142.  
  143. } else { 
  144. $validated = false; 
  145.  
  146. // Handle field types with accepts_null_value set if $values is an empty array 
  147. if ( ! $validated && is_array( $values ) && empty( $values ) && $this->accepts_null_value ) { 
  148. $validated = true; 
  149.  
  150. return (bool) apply_filters( 'bp_xprofile_field_type_is_valid', $validated, $values, $this ); 
  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 (!empty($field_value) && !empty($field_id)) { 
  163. $field = BP_XProfile_Field::get_instance($field_id); 
  164. if ($field) { 
  165. $childs = $field->get_children(); 
  166. if (!empty($childs) && isset($childs[0])) { 
  167. $taxonomy_selected = $childs[0]->name; 
  168. $field_value = trim($field_value); 
  169. $term = get_term_by('id', $field_value, $taxonomy_selected); 
  170. if ($term && $term->taxonomy == $taxonomy_selected) { 
  171. $new_field_value = $term->name; 
  172. } else { 
  173. $new_field_value = __('--', 'bxcft'); 
  174.  
  175. $do_autolink = apply_filters('bxcft_do_autolink',  
  176. $field->get_do_autolink()); 
  177.  
  178. if ($do_autolink) { 
  179. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  180. $search_url = add_query_arg( array( 
  181. $query_arg => urlencode( $field_value ) 
  182. ), bp_get_members_directory_permalink() ); 
  183. $new_field_value = '<a href="' . esc_url( $search_url ) . 
  184. '" rel="nofollow">' . $new_field_value . '</a>'; 
  185.  
  186. /** 
  187. * bxcft_select_custom_taxonomy_display_filter 
  188. * Use this filter to modify the appearance of Selector 
  189. * Custom Taxonomy field value. 
  190. * @param $new_field_value Value of field 
  191. * @param $field_id Id of field. 
  192. * @return Filtered value of field. 
  193. */ 
  194. return apply_filters('bxcft_select_custom_taxonomy_display_filter',  
  195. $new_field_value, $field_id);