Bxcft_Field_Type_MultiSelectCustomTaxonomy

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

Defined (1)

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

/classes/Bxcft_Field_Type_MultiSelectCustomTaxonomy.php  
  1. class Bxcft_Field_Type_MultiSelectCustomTaxonomy extends BP_XProfile_Field_Type 
  2. const ALLOW_NEW_TAGS = "allow_new_tags"; 
  3.  
  4. public function __construct() { 
  5. parent::__construct(); 
  6.  
  7. $this->name = _x( 'Custom Taxonomy Multiselector', 'xprofile field type', 'bxcft' ); 
  8.  
  9. $this->supports_multiple_defaults = true; 
  10. $this->supports_options = true; 
  11.  
  12. $this->set_format( '/^.+$/', 'replace' ); 
  13. do_action( 'bp_xprofile_field_type_multiselect_custom_taxonomy', $this ); 
  14.  
  15. public function admin_field_html( array $raw_properties = array() ) { 
  16. $html = $this->get_edit_field_html_elements( array_merge( 
  17. array( 
  18. 'multiple' => 'multiple',  
  19. 'id' => bp_get_the_profile_field_input_name() . '[]',  
  20. 'name' => bp_get_the_profile_field_input_name() . '[]',  
  21. ),  
  22. $raw_properties 
  23. ) ); 
  24. ?> 
  25. <select <?php echo $html; ?>> 
  26. <?php bp_the_profile_field_options(); ?> 
  27. </select> 
  28. <?php 
  29.  
  30. public function admin_new_field_html (\BP_XProfile_Field $current_field, $control_type = '') 
  31. $type = array_search( get_class( $this ), bp_xprofile_get_field_types() ); 
  32. if ( false === $type ) { 
  33. return; 
  34.  
  35. $class = $current_field->type != $type ? 'display: none;' : ''; 
  36. $current_type_obj = bp_xprofile_create_field_type( $type ); 
  37.  
  38. $options = $current_field->get_children( true ); 
  39. if ( ! $options ) { 
  40. $options = array(); 
  41. $i = 1; 
  42. while ( isset( $_POST[$type . '_option'][$i] ) ) { 
  43. if ( $current_type_obj->supports_options && ! $current_type_obj->supports_multiple_defaults && isset( $_POST["isDefault_{$type}_option"][$i] ) && (int) $_POST["isDefault_{$type}_option"] === $i ) { 
  44. $is_default_option = true; 
  45. } elseif ( isset( $_POST["isDefault_{$type}_option"][$i] ) ) { 
  46. $is_default_option = (bool) $_POST["isDefault_{$type}_option"][$i]; 
  47. } else { 
  48. $is_default_option = false; 
  49.  
  50. $options[] = (object) array( 
  51. 'id' => -1,  
  52. 'is_default_option' => $is_default_option,  
  53. 'name' => sanitize_text_field( stripslashes( $_POST[$type . '_option'][$i] ) ),  
  54. ); 
  55.  
  56. ++$i; 
  57.  
  58. if ( ! $options ) { 
  59. $options[] = (object) array( 
  60. 'id' => -1,  
  61. 'is_default_option' => false,  
  62. 'name' => '',  
  63. ); 
  64.  
  65. $taxonomies = get_taxonomies(array( 
  66. 'public' => true,  
  67. '_builtin' => false,  
  68. )); 
  69. ?> 
  70. <div id="<?php echo esc_attr( $type ); ?>" class="postbox bp-options-box" style="<?php echo esc_attr( $class ); ?> margin-top: 15px;"> 
  71. <?php 
  72. if (!$taxonomies): 
  73. ?> 
  74. <h3><?php _e('There is no custom taxonomy. You need to create at least one to use this field.', 'bxcft'); ?></h3> 
  75. <?php else : ?> 
  76. <h3><?php esc_html_e( 'Select a custom taxonomy:', 'bxcft' ); ?></h3> 
  77. <div class="inside"> 
  78. <p> 
  79. <?php _e('Select a custom taxonomy:', 'bxcft'); ?> 
  80. <select name="<?php echo esc_attr( "{$type}_option[1]" ); ?>" id="<?php echo esc_attr( "{$type}_option[1]" ); ?>"> 
  81. <option value=""><?php _e('Select...', 'bxcft'); ?></option> 
  82. <?php foreach($taxonomies as $k=>$v): ?> 
  83. <option value="<?php echo $k; ?>"<?php if ($options[0]->name == $k): ?> selected="selected"<?php endif; ?>><?php echo $v; ?></option> 
  84. <?php endforeach; ?> 
  85. </select> 
  86. </p> 
  87. <p> 
  88. <?php _e('Allow users to add new tags:', 'bxcft'); ?> 
  89. <input type="checkbox" name="<?php echo esc_attr( "{$type}_option[2]" ); ?>" 
  90. id="<?php echo esc_attr( "{$type}_option[2]" ); ?>" value="<?php echo Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS; ?>" 
  91. <?php if (is_array($options) && isset($options[1]) && $options[1]->name === Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS): ?> checked="checked"<?php endif; ?> /> 
  92. </p> 
  93. </div> 
  94. <?php endif; ?> 
  95. </div> 
  96. <?php 
  97.  
  98. public function edit_field_html (array $raw_properties = array ()) 
  99. $user_id = bp_displayed_user_id(); 
  100.  
  101. if ( isset( $raw_properties['user_id'] ) ) { 
  102. $user_id = (int) $raw_properties['user_id']; 
  103. unset( $raw_properties['user_id'] ); 
  104.  
  105. // HTML5 required attribute. 
  106. if ( bp_get_the_profile_field_is_required() ) { 
  107. $raw_properties['required'] = 'required'; 
  108.  
  109. $html = $this->get_edit_field_html_elements( array_merge( 
  110. array( 
  111. 'multiple' => 'multiple',  
  112. 'id' => bp_get_the_profile_field_input_name() . '[]',  
  113. 'name' => bp_get_the_profile_field_input_name() . '[]',  
  114. ),  
  115. $raw_properties 
  116. ) ); 
  117. ?> 
  118. <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> 
  119. <?php do_action( bp_get_the_profile_field_errors_action() ); ?> 
  120. <select <?php echo $html; ?>> 
  121. <?php bp_the_profile_field_options( "user_id={$user_id}" ); ?> 
  122. </select> 
  123. <?php 
  124.  
  125. public function edit_field_options_html( array $args = array() ) { 
  126. $options = $this->field_obj->get_children(); 
  127. $terms_selected = maybe_unserialize(BP_XProfile_ProfileData::get_value_byid( $this->field_obj->id, $args['user_id'] )); 
  128.  
  129. $html = ''; 
  130. if ($options) { 
  131. foreach ($options as $option) { 
  132. if (Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS !== $option->name) { 
  133. $taxonomy_selected = $option->name; 
  134. break; 
  135. if ( !empty($_POST['field_' . $this->field_obj->id]) ) { 
  136. $new_terms_selected = $_POST['field_' . $this->field_obj->id]; 
  137. $terms_selected = ( $terms_selected != $new_terms_selected ) ? $new_terms_selected : $terms_selected; 
  138. // Get terms of custom taxonomy selected. 
  139. $terms = get_terms($taxonomy_selected, array( 
  140. 'hide_empty' => false 
  141. )); 
  142. if ($terms) { 
  143. foreach ($terms as $term) { 
  144. $html .= sprintf('<option value="%s"%s>%s</option>',  
  145. $term->term_id,  
  146. (!empty($terms_selected) && (in_array($term->term_id, $terms_selected)))?'selected="selected"':'',  
  147. $term->name); 
  148.  
  149. echo apply_filters( 'bp_get_the_profile_field_multiselect_custom_taxonomy', $html, $args['type'], $terms_selected, $this->field_obj->id ); 
  150.  
  151. public function set_whitelist_values( $values ) { 
  152. foreach ($values as $value) { 
  153. if (Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS !== $value) { 
  154. $taxonomy_to_check = $value; 
  155. break; 
  156.  
  157. $terms = get_terms( array( 
  158. 'taxonomy' => $taxonomy_to_check,  
  159. 'hide_empty' => false,  
  160. 'fields' => 'id=>name',  
  161. ) ); 
  162.  
  163. if ( $terms ) { 
  164. $this->validation_whitelist = array_merge( 
  165. // Convert ids to strings for validation. 
  166. array_map( function($v) { 
  167. return "$v"; 
  168. }, array_keys( $terms ) ),  
  169. array_values( $terms ) 
  170. ); 
  171.  
  172. return $this; 
  173.  
  174. /** 
  175. * Modify the appearance of value. Apply autolink if enabled. 
  176. * @param string $value Original value of field 
  177. * @param int $field_id Id of field 
  178. * @return string Value formatted 
  179. */ 
  180. public static function display_filter($field_value, $field_id = '') { 
  181.  
  182. $new_field_value = $field_value; 
  183.  
  184. if (!empty($field_value) && !empty($field_id)) { 
  185. $field = BP_XProfile_Field::get_instance($field_id); 
  186. if ($field) { 
  187. $do_autolink = apply_filters('bxcft_do_autolink',  
  188. $field->get_do_autolink()); 
  189. if ($do_autolink) { 
  190. $query_arg = bp_core_get_component_search_query_arg( 'members' ); 
  191. $childs = $field->get_children(); 
  192. if (!empty($childs) && isset($childs[0])) { 
  193. $taxonomy_selected = $childs[0]->name; 
  194. $aux = ''; 
  195. $term_ids = explode(', ', $field_value); 
  196. foreach ($term_ids as $tid) { 
  197. $tid = trim($tid); 
  198. $term = get_term_by('id', $tid, $taxonomy_selected); 
  199. if ($term && $term->taxonomy == $taxonomy_selected) { 
  200. if (empty($aux)) { 
  201. $aux .= '<ul class="list_custom_taxonomy">'; 
  202. $aux .= '<li>'; 
  203. if ($do_autolink) { 
  204. $search_url = add_query_arg( array( 
  205. $query_arg => urlencode( $tid ) 
  206. ), bp_get_members_directory_permalink() ); 
  207. $aux .= '<a href="' . esc_url( $search_url ) . 
  208. '" rel="nofollow">' . $term->name . '</a>'; 
  209. } else { 
  210. $aux .= $term->name; 
  211. $aux .= '</li>'; 
  212. if (!empty($aux)) { 
  213. $aux .= '</ul>'; 
  214. $new_field_value = $aux; 
  215.  
  216. /** 
  217. * bxcft_multiselect_custom_taxonomy_display_filter 
  218. * Use this filter to modify the appearance of Multiselector 
  219. * Custom Taxonomy field value. 
  220. * @param $new_field_value Value of field 
  221. * @param $field_id Id of field. 
  222. * @return Filtered value of field. 
  223. */ 
  224. return apply_filters('bxcft_multiselect_custom_taxonomy_display_filter',  
  225. $new_field_value, $field_id);