Bxcft_Plugin

The BuddyPress Xprofile Custom Fields Type Bxcft Plugin class.

Defined (1)

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

/bp-xprofile-custom-fields-type.php  
  1. class Bxcft_Plugin 
  2. CONST BXCFT_MAX_FILESIZE = 8; 
  3.  
  4. private $version; 
  5. private $user_id = null; 
  6. private $images_ext_allowed; 
  7. private $images_max_filesize; 
  8. private $files_ext_allowed; 
  9. private $files_max_filesize; 
  10. private $fields_type_with_select2; 
  11. private $fields_type_multiple; 
  12.  
  13. private $autolink_filter_removed = false; 
  14.  
  15. private $bxcft_field_types = array( 
  16. 'birthdate' => 'Bxcft_Field_Type_Birthdate',  
  17. 'email' => 'Bxcft_Field_Type_Email',  
  18. 'web' => 'Bxcft_Field_Type_Web',  
  19. 'datepicker' => 'Bxcft_Field_Type_Datepicker',  
  20. 'select_custom_post_type' => 'Bxcft_Field_Type_SelectCustomPostType',  
  21. 'multiselect_custom_post_type' => 'Bxcft_Field_Type_MultiSelectCustomPostType',  
  22. 'select_custom_taxonomy' => 'Bxcft_Field_Type_SelectCustomTaxonomy',  
  23. 'multiselect_custom_taxonomy' => 'Bxcft_Field_Type_MultiSelectCustomTaxonomy',  
  24. 'checkbox_acceptance' => 'Bxcft_Field_Type_CheckboxAcceptance',  
  25. 'image' => 'Bxcft_Field_Type_Image',  
  26. 'file' => 'Bxcft_Field_Type_File',  
  27. 'color' => 'Bxcft_Field_Type_Color',  
  28. 'decimal_number' => 'Bxcft_Field_Type_DecimalNumber',  
  29. 'number_minmax' => 'Bxcft_Field_Type_NumberMinMax',  
  30. 'slider' => 'Bxcft_Field_Type_Slider',  
  31. ); 
  32.  
  33. public function __construct () 
  34. $this->version = "2.6"; 
  35.  
  36. /** Main hooks **/ 
  37. add_action( 'plugins_loaded', array($this, 'bxcft_update') ); 
  38.  
  39. /** Admin hooks **/ 
  40. add_action( 'admin_init', array($this, 'admin_init') ); 
  41. add_action( 'admin_notices', array($this, 'admin_notices') ); 
  42.  
  43. /** Buddypress hook **/ 
  44. add_action( 'bp_init', array($this, 'init') ); 
  45. add_action( 'bp_signup_validate', array($this, 'bxcft_signup_validate') ); 
  46. add_action( 'xprofile_data_before_save', array($this, 'bxcft_xprofile_data_before_save') ); 
  47. add_action( 'xprofile_data_after_delete', array($this, 'bxcft_xprofile_data_after_delete') ); 
  48.  
  49. /** Select2 fields */ 
  50. add_action( 'xprofile_field_after_submitbox', array($this, 'bxcft_show_select2_box') ); 
  51. add_action( 'xprofile_fields_saved_field', array($this, 'bxcft_save_do_select2') ); 
  52. add_action( 'bp_custom_profile_edit_fields_pre_visibility', array($this, 'bxcft_enable_select2_field') ); 
  53.  
  54. /** Filters **/ 
  55. add_filter( 'bp_xprofile_get_field_types', array($this, 'bxcft_get_field_types'), 10, 1 ); 
  56. add_filter( 'xprofile_get_field_data', array($this, 'bxcft_get_field_data'), 10, 2 ); 
  57. add_filter( 'bp_get_the_profile_field_value', array($this, 'bxcft_remove_autolink_filter_from_buddypress'), 7, 2); 
  58. add_filter( 'bp_get_the_profile_field_value', array($this, 'bxcft_get_field_value'), 10, 3); 
  59. add_filter( 'bp_get_the_profile_field_value', array($this, 'bxcft_restore_autolink_filter_from_buddypress'), 11, 2); 
  60. add_filter( 'bxcft_do_autolink', array($this, 'bxcft_enabled_autolink'), 10, 1 ); 
  61. /** BP Profile Search Filters **/ 
  62. add_filter( 'bps_field_validation_type', array($this, 'bxcft_standard_fields_validation_type' ) ); 
  63. add_filter( 'bps_field_type_for_search_form', array($this, 'bxcft_standard_fields_type_for_search_form' ) ); 
  64. add_filter( 'bps_field_type_for_query', array($this, 'bxcft_standard_fields_for_query' ) ); 
  65. // Special fields. 
  66. add_filter( 'bps_field_validation', array($this, 'bxcft_special_fields_search_validation' ), 10, 2); 
  67. add_filter( 'bps_field_data_for_search_form', array($this, 'bxcft_special_fields_data_for_search_form' ) ); 
  68. add_filter( 'bps_field_query', array($this, 'bxcft_special_fields_query' ), 10, 4); 
  69. // Pre validate multiselect custom taxonomy. 
  70. add_filter( 'bp_xprofile_set_field_data_pre_validate', array( $this, 'bxcft_xprofile_set_field_data_pre_validate' ), 10, 3 ); 
  71.  
  72. public function init() 
  73. $this->images_ext_allowed = apply_filters('bxcft_images_ext_allowed', array( 
  74. 'jpg', 'jpeg', 'gif', 'png' 
  75. )); 
  76. $this->images_max_filesize = apply_filters('bxcft_images_max_filesize', Bxcft_Plugin::BXCFT_MAX_FILESIZE); 
  77. $this->files_ext_allowed = apply_filters('bxcft_files_ext_allowed', array( 
  78. 'doc', 'docx', 'pdf' 
  79. )); 
  80. $this->files_max_filesize = apply_filters('bxcft_files_max_filesize', Bxcft_Plugin::BXCFT_MAX_FILESIZE); 
  81. $this->fields_type_with_select2 = apply_filters('bxcft_field_types_with_select2', array( 
  82. 'selectbox', 'multiselectbox', 'select_custom_post_type',  
  83. 'multiselect_custom_post_type', 'select_custom_taxonomy',  
  84. 'multiselect_custom_taxonomy' 
  85. )); 
  86. $this->fields_type_multiple = apply_filters('bxcft_field_types_multiple', array( 
  87. 'multiselectbox', 'multiselect_custom_post_type',  
  88. 'multiselect_custom_taxonomy' 
  89. )); 
  90.  
  91. /** Includes **/ 
  92. if ( bp_is_active( 'xprofile' ) ) { 
  93. require_once( 'classes/Bxcft_Field_Type_Birthdate.php' ); 
  94. require_once( 'classes/Bxcft_Field_Type_Email.php' ); 
  95. require_once( 'classes/Bxcft_Field_Type_Web.php' ); 
  96. require_once( 'classes/Bxcft_Field_Type_Datepicker.php' ); 
  97. require_once( 'classes/Bxcft_Field_Type_SelectCustomPostType.php' ); 
  98. require_once( 'classes/Bxcft_Field_Type_MultiSelectCustomPostType.php' ); 
  99. require_once( 'classes/Bxcft_Field_Type_SelectCustomTaxonomy.php' ); 
  100. require_once( 'classes/Bxcft_Field_Type_MultiSelectCustomTaxonomy.php' ); 
  101. require_once( 'classes/Bxcft_Field_Type_CheckboxAcceptance.php' ); 
  102. require_once( 'classes/Bxcft_Field_Type_Image.php' ); 
  103. require_once( 'classes/Bxcft_Field_Type_File.php' ); 
  104. require_once( 'classes/Bxcft_Field_Type_Color.php' ); 
  105. require_once( 'classes/Bxcft_Field_Type_DecimalNumber.php' ); 
  106. require_once( 'classes/Bxcft_Field_Type_NumberMinMax.php' ); 
  107. require_once( 'classes/Bxcft_Field_Type_Slider.php' ); 
  108.  
  109. if (bp_is_user_profile_edit() || bp_is_register_page()) { 
  110. $this->load_js(); 
  111.  
  112. public function load_js() { 
  113. wp_enqueue_script('bxcft-modernizr', plugin_dir_url(__FILE__) . 'js/modernizr.js', array(), '2.6.2', false); 
  114. wp_enqueue_script('bxcft-jscolor', plugin_dir_url(__FILE__) . 'js/jscolor/jscolor.js', array(), '1.4.1', true); 
  115. wp_enqueue_script('bxcft-public', plugin_dir_url(__FILE__) . 'js/public.js', array('jquery'), $this->version, true); 
  116.  
  117. // Select 2. 
  118. wp_enqueue_script('bxcft-select2', plugin_dir_url(__FILE__) . 'js/select2/select2.min.js', array('jquery'), '4.0.2', true); 
  119. $locale = get_locale(); 
  120. if (file_exists(plugin_dir_path(__FILE__) . 'js/select2/i18n/' . $locale . '.js')) { 
  121. wp_enqueue_script('bxcft-select2-i18n', plugin_dir_url(__FILE__) . 'js/select2/i18n/' . get_locale() . '.js', array('bxcft-select2'), '4.0.2', true); 
  122. wp_enqueue_style('bxcft-select2', plugin_dir_url(__FILE__) . 'css/select2/select2.min.css', array(), '4.0.2'); 
  123.  
  124. public function admin_init() 
  125. if (is_admin() && get_option('bxcft_activated') == 1) { 
  126. // Check if BuddyPress 2.5 is installed. 
  127. $version_bp = array( '0' ); 
  128. if (function_exists('is_plugin_active') && is_plugin_active('buddypress/bp-loader.php')) { 
  129. // BuddyPress loaded. 
  130. $version_bp = get_file_data( WP_PLUGIN_DIR . '/buddypress/bp-loader.php', array( 'Version' ) ); 
  131. if ( ! $this->compare_versions( array_pop( $version_bp ), '2.5' ) ) { 
  132. $notices = get_option('bxcft_notices'); 
  133. $notices[] = __('BuddyPress Xprofile Custom Fields Type plugin needs <b>BuddyPress 2.5</b>, please install or upgrade BuddyPress.', 'bxcft'); 
  134. update_option('bxcft_notices', $notices); 
  135. delete_option('bxcft_activated'); 
  136. else if ( ! bp_is_active( 'xprofile' ) ) { 
  137. $notices = get_option('bxcft_notices'); 
  138. $notices[] = __('BuddyPress Xprofile Custom Fields Type plugin needs Buddypress Xprofile Component. Please enable Xprofile first.', 'bxcft'); 
  139. update_option('bxcft_notices', $notices); 
  140. delete_option('bxcft_activated'); 
  141.  
  142. // Enqueue javascript. 
  143. wp_enqueue_script('bxcft-js', plugin_dir_url(__FILE__) . 'js/admin.js', array(), $this->version, false); 
  144. wp_localize_script('bxcft-js', 'fields_type_with_select2', array('types' => $this->fields_type_with_select2)); 
  145.  
  146. if (isset($_GET['page']) && $_GET['page'] === 'bp-profile-edit') { 
  147. $this->load_js(); 
  148.  
  149. /** 
  150. * Compare buddypress version with needed version. 
  151. * @since 2.6 
  152. * @param Array $version_actual Actual version. 
  153. * @param Array $version_needed Needed version. 
  154. * @return boolean 
  155. */ 
  156. private function compare_versions( $version_actual, $version_needed ) { 
  157. $components_version_actual = explode( '.', $version_actual ); 
  158. $components_version_needed = explode( '.', $version_needed ); 
  159.  
  160. foreach ( $components_version_needed as $key => $element ) { 
  161. if ( isset( $components_version_actual[ $key ] ) && 
  162. (int) $components_version_actual[ $key ] < (int) $element ) { 
  163. return false; 
  164.  
  165. return true; 
  166.  
  167. public function admin_notices() 
  168. $notices = get_option('bxcft_notices'); 
  169. if ($notices) { 
  170. foreach ($notices as $notice) 
  171. echo "<div class='error'><p>$notice</p></div>"; 
  172. delete_option('bxcft_notices'); 
  173.  
  174. public function bxcft_get_field_types($fields) 
  175. $fields = array_merge($fields, $this->bxcft_field_types); 
  176. return $fields; 
  177.  
  178. /** 
  179. * Remove `xprofile_filter_link_profile_data` on `bp_get_the_profile_field_value` 
  180. * @since 2.4.6 
  181. * @param mixed $value Value of field 
  182. * @return mixed Same value of field 
  183. */ 
  184. public function bxcft_remove_autolink_filter_from_buddypress($value, $type='') { 
  185. if (in_array($type, array_keys($this->bxcft_field_types)) 
  186. && !Bxcft_Plugin::is_autolink_filter_removed()) { 
  187. $this->autolink_filter_removed = true; 
  188. remove_filter( 'bp_get_the_profile_field_value',  
  189. 'xprofile_filter_link_profile_data', 9 ); 
  190.  
  191. return $value; 
  192.  
  193. /** 
  194. * Restore `xprofile_filter_link_profile_data` on `bp_get_the_profile_field_value` 
  195. * @since 2.4.6 
  196. * @param mixed $value Value of field 
  197. * @return mixed Same value of field 
  198. */ 
  199. public function bxcft_restore_autolink_filter_from_buddypress($value, $type='') { 
  200. if ($this->autolink_filter_removed) { 
  201. add_filter( 'bp_get_the_profile_field_value',  
  202. 'xprofile_filter_link_profile_data', 9, 3 ); 
  203.  
  204. return $value; 
  205.  
  206. /** 
  207. * Check if autolink is enabled. If it is enabled for this type of field and 
  208. * if it was removed or it isn't removed by the user in functions.php. 
  209. * @since 2.4.6 
  210. * @param boolean $do_autolink Actual value of do_autolink 
  211. * @return boolean Filtered value of do_autolink 
  212. */ 
  213. public function bxcft_enabled_autolink($do_autolink) { 
  214. return ($do_autolink && 
  215. ($this->autolink_filter_removed 
  216. || !Bxcft_Plugin::is_autolink_filter_removed())); 
  217.  
  218. /** 
  219. * This method is now useless, it will be removed on version 3.0. 
  220. * @since 2.4.6 
  221. * @param mixed $value Value of field 
  222. * @param int $field_id Id of field 
  223. * @return mixed Same value of field 
  224. */ 
  225. public function bxcft_get_field_data($value, $field_id) 
  226. $field = BP_XProfile_Field::get_instance($field_id); 
  227. if (!$field) { 
  228. return; 
  229.  
  230. /** 
  231. * @deprecated 3.0 This filter will be removed in version 3.0. Please 
  232. * stop using it, use instead 'bxcft_TYPENAME_display_filter' 
  233. */ 
  234. return apply_filters('bxcft_show_field_value', $value, $field->type,  
  235. $field_id, $value); 
  236.  
  237. /** 
  238. * This method is now useless, it will be removed on version 3.0. 
  239. * @since 2.4.6 
  240. * @param mixed $value Value of field 
  241. * @param int $field_id Id of field 
  242. * @param string $type Type of field 
  243. * @return mixed Same value of field 
  244. */ 
  245. public function bxcft_get_field_value($value='', $type='', $id='') 
  246. /** 
  247. * @deprecated 3.0 This filter will be removed in version 3.0. Please 
  248. * stop using it, use instead 'bxcft_TYPENAME_display_filter' 
  249. */ 
  250. return apply_filters('bxcft_show_field_value', $value, $type, $id, $value); 
  251.  
  252. /** 
  253. * Returns the folder where files and images will be saved. 
  254. * @since 2.4.6 
  255. * @param integer $user_id Id of current displayed user. 
  256. * @return array array of upload_dir 
  257. */ 
  258. public function bxcft_profile_upload_dir( $user_id = 0 ) 
  259. if ($user_id == 0 && empty($this->user_id)) 
  260. $this->user_id = bp_displayed_user_id(); 
  261.  
  262. /** 
  263. * bxcft_upload_dir 
  264. * Use this filter if you want to change the folder 
  265. * where files and images are saved. 
  266. * @since 2.4.6 
  267. * @var string 
  268. */ 
  269. $profile_subdir = apply_filters( 'bxcft_upload_dir', '/profiles/' . $this->user_id,  
  270. $this->user_id); 
  271.  
  272. $upload_dir = array( 
  273. 'path' => bp_core_get_upload_dir() . $profile_subdir,  
  274. 'url' => bp_core_get_upload_dir('url') . $profile_subdir,  
  275. 'subdir' => bp_core_get_upload_dir() . $profile_subdir,  
  276. 'basedir' => bp_core_get_upload_dir() . $profile_subdir,  
  277. 'baseurl' => bp_core_get_upload_dir('url') . $profile_subdir,  
  278. 'error' => false,  
  279. ); 
  280.  
  281. return apply_filters( 'bxcft_profile_upload_dir', $upload_dir ); 
  282.  
  283. public function bxcft_signup_validate() 
  284. global $bp; 
  285. if ( bp_is_active( 'xprofile' ) ) 
  286. if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) 
  287. $profile_field_ids = explode(', ', $_POST['signup_profile_field_ids']); 
  288. foreach ($profile_field_ids as $field_id) 
  289. $field = new BP_XProfile_Field($field_id); 
  290. if ( ($field->type == 'image' || $field->type == 'file') && 
  291. isset($_FILES['field_'.$field_id])) { 
  292. // Delete required field error. 
  293. unset($bp->signup->errors['field_'.$field_id]); 
  294.  
  295. $filesize = round($_FILES['field_'.$field_id]['size'] / (1024 * 1024), 2); 
  296. if ($field->is_required && $filesize <= 0) { 
  297. $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' ); 
  298. } elseif ($filesize > 0) { 
  299. // Check extensions. 
  300. $ext = strtolower(substr($_FILES['field_'.$field_id]['name'], strrpos($_FILES['field_'.$field_id]['name'], '.')+1)); 
  301. if ($field->type == 'image') { 
  302. if (!in_array($ext, $this->images_ext_allowed)) { 
  303. $bp->signup->errors['field_'.$field_id] = sprintf(__('Image type not allowed: (%s).', 'bxcft'), implode(', ', $this->images_ext_allowed)); 
  304. elseif ($filesize > $this->images_max_filesize) { 
  305. $bp->signup->errors['field_'.$field_id] = sprintf(__('Max image upload size: %s MB.', 'bxcft'), $this->images_max_filesize); 
  306. } elseif ($field->type == 'file') { 
  307. if (!in_array($ext, $this->files_ext_allowed)) { 
  308. $bp->signup->errors['field_'.$field_id] = sprintf(__('File type not allowed: (%s).', 'bxcft'), implode(', ', $this->files_ext_allowed)); 
  309. elseif ($filesize > $this->files_max_filesize) { 
  310. $bp->signup->errors['field_'.$field_id] = sprintf(__('Max file upload size: %s MB.', 'bxcft'), $this->files_max_filesize); 
  311. elseif ($field->type == 'checkbox_acceptance' && $field->is_required) { 
  312. if (isset($_POST['field_' . $field_id]) 
  313. && $_POST['field_' . $field_id] != 1) { 
  314. $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' ); 
  315. elseif ($field->type == 'birthdate') { 
  316. $max_age = 0; 
  317. $options = $field->get_children(); 
  318. foreach ($options as $o) { 
  319. if ($o->name != 'show_age' && $o->name != 'show_birthdate') { 
  320. $max_age = (int)$o->name; 
  321. break; 
  322.  
  323. if ($max_age > 0) { 
  324. // Check birthdate. 
  325. if (class_exists(DateTime)) { 
  326. $now = new DateTime(); 
  327. $birthdate = new DateTime(sprintf("%s-%s-%s",  
  328. $_POST['field_'.$field_id.'_year'],  
  329. $_POST['field_'.$field_id.'_month'],  
  330. $_POST['field_'.$field_id.'_day'])); 
  331. $age = $now->diff($birthdate); 
  332. if ($age->y < $max_age) { 
  333. $bp->signup->errors['field_' . $field_id] = sprintf(__( 'You have to be at least %s years old.', 'bxcft' ), $max_age); 
  334. } // End foreach... 
  335. } // End if ( isset... 
  336. } // End if ( bp_is_active(... 
  337.  
  338. function bxcft_xprofile_data_before_save($data) 
  339. global $bp; 
  340.  
  341. $field_id = $data->field_id; 
  342. $field = new BP_XProfile_Field($field_id); 
  343.  
  344. if ($field->type == 'image' || $field->type == 'file' && isset($_FILES['field_'.$field_id])) 
  345. $uploads = wp_upload_dir(); 
  346. $filesize = round($_FILES['field_'.$field_id]['size'] / (1024 * 1024), 2); 
  347. if (isset($_FILES['field_'.$field_id]) && $filesize > 0) 
  348. $ext = strtolower(substr($_FILES['field_'.$field_id]['name'], strrpos($_FILES['field_'.$field_id]['name'], '.')+1)); 
  349. if ($field->type == 'image') 
  350. $ext_allowed = $this->images_ext_allowed; 
  351. if (!in_array($ext, $ext_allowed)) 
  352. bp_core_add_message( sprintf(__('Image type not allowed: (%s).', 'bxcft'), implode(', ', $this->images_ext_allowed)), 'error' ); 
  353. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  354. elseif ($filesize > $this->images_max_filesize) { 
  355. bp_core_add_message( sprintf(__('Max image upload size: %s MB.', 'bxcft'), $this->images_max_filesize), 'error' ); 
  356. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  357. } else { 
  358. // Delete previous image. 
  359. if (isset($_POST['field_'.$field_id.'_hiddenimg']) && 
  360. !empty($_POST['field_'.$field_id.'_hiddenimg']) && 
  361. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg'])) 
  362. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg']); 
  363. elseif ($field->type == 'file') 
  364. $ext_allowed = $this->files_ext_allowed; 
  365. if (!in_array($ext, $ext_allowed)) { 
  366. bp_core_add_message( sprintf(__('File type not allowed: (%s).', 'bxcft'), implode(', ', $this->files_ext_allowed)), 'error' ); 
  367. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  368. elseif ($filesize > $this->files_max_filesize) { 
  369. bp_core_add_message( sprintf(__('Max file upload size: %s MB.', 'bxcft'), $this->files_max_filesize), 'error' ); 
  370. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  371. } else { 
  372. // Delete previous file. 
  373. if (isset($_POST['field_'.$field_id.'_hiddenfile']) && 
  374. !empty($_POST['field_'.$field_id.'_hiddenfile']) && 
  375. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile'])) 
  376. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile']); 
  377.  
  378. if (in_array($ext, $ext_allowed)) 
  379. require_once( ABSPATH . '/wp-admin/includes/file.php' ); 
  380. $this->user_id = $data->user_id; 
  381. add_filter( 'upload_dir', array($this, 'bxcft_profile_upload_dir'), 10, 0 ); 
  382. $_POST['action'] = 'wp_handle_upload'; 
  383. $uploaded_file = wp_handle_upload( $_FILES['field_'.$field_id] ); 
  384. remove_filter('upload_dir', array($this, 'bxcft_profile_upload_dir'), 10 ); 
  385. $value = str_replace($uploads['baseurl'], '', $uploaded_file['url']); 
  386. } else { 
  387. // Handles delete checkbox. 
  388. if ($field->type == 'image' && isset($_POST['field_'.$field_id.'_deleteimg']) && 
  389. $_POST['field_'.$field_id.'_deleteimg']) 
  390. if (isset($_POST['field_'.$field_id.'_hiddenimg']) && 
  391. !empty($_POST['field_'.$field_id.'_hiddenimg']) && 
  392. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg'])) 
  393. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg']); 
  394. $value = array(); 
  395. elseif ($field->type == 'image') 
  396. $value = (isset($_POST['field_'.$field_id.'_hiddenimg'])) ? 
  397. $_POST['field_'.$field_id.'_hiddenimg'] : ''; 
  398.  
  399. if ($field->type == 'file' && isset($_POST['field_'.$field_id.'_deletefile']) && 
  400. $_POST['field_'.$field_id.'_deletefile']) 
  401. if (isset($_POST['field_'.$field_id.'_hiddenfile']) && 
  402. !empty($_POST['field_'.$field_id.'_hiddenfile']) && 
  403. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile'])) 
  404. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile']); 
  405. $value = array(); 
  406. elseif ($field->type == 'file') 
  407. $value = isset($_POST['field_'.$field_id.'_hiddenfile']) ? 
  408. $_POST['field_'.$field_id.'_hiddenfile'] : ''; 
  409.  
  410. $data->value = (isset($value))?$value:''; 
  411. elseif ($field->type == 'birthdate') { 
  412. $max_age = 0; 
  413. $options = $field->get_children(); 
  414. foreach ($options as $o) { 
  415. if ($o->name != 'show_age' && $o->name != 'show_birthdate') { 
  416. $max_age = (int)$o->name; 
  417. break; 
  418.  
  419. if ($max_age > 0) { 
  420. // Check birthdate. 
  421. if (class_exists(DateTime)) { 
  422. $now = new DateTime(); 
  423. $birthdate = new DateTime(sprintf("%s-%s-%s",  
  424. $_POST['field_'.$field_id.'_year'],  
  425. $_POST['field_'.$field_id.'_month'],  
  426. $_POST['field_'.$field_id.'_day'])); 
  427. $age = $now->diff($birthdate); 
  428. if ($age->y < $max_age) { 
  429. bp_core_add_message( sprintf(__( 'You have to be at least %s years old.', 'bxcft' ), $max_age), 'error' ); 
  430. bp_core_redirect( trailingslashit( bp_displayed_user_domain() . $bp->profile->slug . '/edit/group/' . bp_action_variable( 1 ) ) ); 
  431.  
  432. public function bxcft_xprofile_data_after_delete($data) 
  433. $field_id = $data->field_id; 
  434. $field = new BP_XProfile_Field($field_id); 
  435. $uploads = wp_upload_dir(); 
  436. if ($field->type == 'image' && isset($_POST['field_'.$field_id.'_deleteimg']) && 
  437. $_POST['field_'.$field_id.'_deleteimg']) 
  438. if (isset($_POST['field_'.$field_id.'_hiddenimg']) && 
  439. !empty($_POST['field_'.$field_id.'_hiddenimg']) && 
  440. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg'])) 
  441. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenimg']); 
  442.  
  443. if ($field->type == 'file' && isset($_POST['field_'.$field_id.'_deletefile']) && 
  444. $_POST['field_'.$field_id.'_deletefile']) 
  445. if (isset($_POST['field_'.$field_id.'_hiddenfile']) && 
  446. !empty($_POST['field_'.$field_id.'_hiddenfile']) && 
  447. file_exists($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile'])) 
  448. unlink($uploads['basedir'] . $_POST['field_'.$field_id.'_hiddenfile']); 
  449.  
  450. public function bxcft_xprofile_set_field_data_pre_validate( $value, $field, $field_type_obj ) { 
  451. if ($field->type === 'multiselect_custom_taxonomy') { 
  452. $options = $field->get_children(); 
  453. $allow_new_tags = false; 
  454. $taxonomy_selected = ''; 
  455. foreach ($options as $option) { 
  456. if ( Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS === $option->name) { 
  457. $allow_new_tags = true; 
  458. } else { 
  459. $taxonomy_selected = $option->name; 
  460.  
  461. if ( $allow_new_tags && ! empty( $taxonomy_selected ) ) { 
  462. // Add new tags if needed. 
  463. $value_to_return = array(); 
  464. foreach ( $value as $tag ) { 
  465. if ( ! term_exists( (int) $tag, $taxonomy_selected ) && 
  466. ! term_exists( $tag, $taxonomy_selected ) ) { 
  467. // Create tag. 
  468. $res = wp_insert_term( $tag, $taxonomy_selected ); 
  469. if (is_array($res)) { 
  470. $value_to_return[] = "{$res['term_id']}"; 
  471. } else { 
  472. $value_to_return[] = $tag; 
  473. } else { 
  474. $value_to_return[] = $tag; 
  475. } else { 
  476. $value_to_return = $value; 
  477. } else { 
  478. $value_to_return = $value; 
  479.  
  480. return $value_to_return; 
  481.  
  482. public function bxcft_standard_fields_validation_type($field_type) 
  483. switch($field_type) { 
  484. case 'birthdate': 
  485. // Search by age. 
  486. $field_type = 'datebox'; 
  487. break; 
  488.  
  489. case 'email': 
  490. case 'web': 
  491. $field_type = 'textbox'; 
  492. break; 
  493.  
  494. case 'number_minmax': 
  495. case 'slider': 
  496. $field_type = 'number'; 
  497. break; 
  498.  
  499. return $field_type; 
  500.  
  501. public function bxcft_standard_fields_for_query($field_type) { 
  502. switch($field_type) { 
  503. case 'birthdate': 
  504. case 'datepicker': 
  505. // Search by age. 
  506. $field_type = 'datebox'; 
  507. break; 
  508.  
  509. case 'email': 
  510. case 'web': 
  511. case 'color': 
  512. case 'decimal_number': 
  513. $field_type = 'textbox'; 
  514. break; 
  515.  
  516. case 'number_minmax': 
  517. case 'slider': 
  518. $field_type = 'number'; 
  519. break; 
  520.  
  521. case 'select_custom_post_type': 
  522. case 'select_custom_taxonomy': 
  523. $field_type = 'selectbox'; 
  524. break; 
  525.  
  526. case 'multiselect_custom_post_type': 
  527. case 'multiselect_custom_taxonomy': 
  528. $field_type = 'multiselectbox'; 
  529. break; 
  530.  
  531. case 'checkbox_acceptance': 
  532. $field_type = 'radio'; 
  533. break; 
  534.  
  535. return $field_type; 
  536.  
  537. public function bxcft_standard_fields_type_for_search_form($field_type) { 
  538. switch($field_type) { 
  539. case 'birthdate': 
  540. case 'datepicker': 
  541. // Search by age. 
  542. $field_type = 'datebox'; 
  543. break; 
  544.  
  545. case 'email': 
  546. case 'web': 
  547. case 'color': 
  548. case 'decimal_number': 
  549. $field_type = 'textbox'; 
  550. break; 
  551.  
  552. case 'number_minmax': 
  553. case 'slider': 
  554. $field_type = 'number'; 
  555. break; 
  556.  
  557. return $field_type; 
  558.  
  559. public function bxcft_special_fields_search_validation($settings, $field) { 
  560. list($value, $description, $range) = $settings; 
  561. switch ($field->type) { 
  562. case 'decimal_number': 
  563. $range = true; 
  564. break; 
  565.  
  566. case 'datepicker': 
  567. case 'select_custom_post_type': 
  568. case 'multiselect_custom_post_type': 
  569. case 'select_custom_taxonomy': 
  570. case 'multiselect_custom_taxonomy': 
  571. case 'checkbox_acceptance': 
  572. case 'image': 
  573. case 'file': 
  574. case 'color': 
  575. $range = false; 
  576. break; 
  577.  
  578. return array($value, $description, $range); 
  579.  
  580. public function bxcft_special_fields_data_for_search_form($f) { 
  581. $request = stripslashes_deep ($_REQUEST); 
  582. switch ($f->type) { 
  583. case 'select_custom_post_type': 
  584. case 'multiselect_custom_post_type': 
  585. $f->values = isset ($request[$f->code])? (array)$request[$f->code]: array (); 
  586. $field = new BP_XProfile_Field($f->id); 
  587. $array_options = array(); 
  588. if ($field) { 
  589. $childs = $field->get_children(); 
  590. if (isset($childs) && $childs && count($childs) > 0 
  591. && is_object($childs[0])) { 
  592. $post_type_selected = $childs[0]->name; 
  593. $posts = new WP_Query(array( 
  594. 'posts_per_page' => -1,  
  595. 'post_type' => $post_type_selected,  
  596. 'orderby' => 'title',  
  597. 'order' => 'ASC' 
  598. )); 
  599. if ($posts) { 
  600. foreach ($posts->posts as $p) { 
  601. $array_options[$p->ID] = $p->post_title; 
  602. $f->options = $array_options; 
  603. if ($f->type === 'select_custom_post_type') { 
  604. $f->display = 'selectbox'; 
  605. } else { 
  606. $f->display = 'multiselectbox'; 
  607. break; 
  608.  
  609. case 'select_custom_taxonomy': 
  610. case 'multiselect_custom_taxonomy': 
  611. $f->values = isset ($request[$f->code])? (array)$request[$f->code]: array (); 
  612. $field = new BP_XProfile_Field($f->id); 
  613. $array_options = array(); 
  614. if ($field) { 
  615. $childs = $field->get_children(); 
  616. if (isset($childs) && $childs && count($childs) > 0 
  617. && is_object($childs[0])) { 
  618. $taxonomy_selected = $childs[0]->name; 
  619. $terms = get_terms($taxonomy_selected, array( 
  620. 'hide_empty' => false 
  621. )); 
  622. if ($terms) { 
  623. foreach ($terms as $t) { 
  624. $array_options[$t->term_id] = $t->name; 
  625. $f->options = $array_options; 
  626. if ($f->type === 'select_custom_taxonomy') { 
  627. $f->display = 'selectbox'; 
  628. } else { 
  629. $f->display = 'multiselectbox'; 
  630. break; 
  631.  
  632. case 'checkbox_acceptance': 
  633. $f->values = isset ($request[$f->code])? (array)$request[$f->code]: array (); 
  634. $f->options = array( 
  635. 0 => __('no', 'bxcft'),  
  636. 1 => __('yes', 'bxcft') 
  637. ); 
  638. $f->display = 'radio'; 
  639. break; 
  640.  
  641. case 'image': 
  642. case 'file': 
  643. $f->values = isset ($request[$f->code])? (array)$request[$f->code]: array (); 
  644. $f->options = array( 
  645. 1 => __('Not empty', 'bxcft') 
  646. ); 
  647. $f->display = 'checkbox'; 
  648. break; 
  649.  
  650. return $f; 
  651.  
  652. public function bxcft_special_fields_query($results, $field, $key, $value) { 
  653. global $bp, $wpdb; 
  654. switch ($field->type) { 
  655. case 'image': 
  656. case 'file': 
  657. $sql = $wpdb->prepare ("SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND value != ''", $field->id); 
  658. $results = $wpdb->get_col ($sql); 
  659. break; 
  660.  
  661. return $results; 
  662.  
  663. public function bxcft_show_select2_box($field) { 
  664. $do_select2 = bp_xprofile_get_meta( $field->id, 'field', 'do_select2'); 
  665. $hidden = true; 
  666. if (in_array($field->type, $this->fields_type_with_select2)) { 
  667. $hidden = false; 
  668. ?> 
  669. <div id="select2-box" class="postbox<?php if ($hidden): ?> hidden<?php endif; ?>"> 
  670. <h2><?php esc_html_e('Select2', 'bxft'); ?></h2> 
  671. <div class="inside"> 
  672. <p class="description">Enable select2 javascript code.</p> 
  673.  
  674. <p> 
  675. <label for="do-select2" class="screen-reader-text">Select2 status for this field</label> 
  676. <select name="do_select2" id="do-select2"> 
  677. <option value="on" <?php if ($do_select2 === 'on'): ?> selected="selected"<?php endif; ?>>Enabled</option> 
  678. <option value=""<?php if ($do_select2 !== 'on'): ?> selected="selected"<?php endif; ?>>Disabled</option> 
  679. </select> 
  680. </p> 
  681. </div> 
  682. </div> 
  683. <?php 
  684.  
  685. public function bxcft_save_do_select2($field) { 
  686. $field_id = $field->id; 
  687.  
  688. if (!in_array($field->type, $this->fields_type_with_select2)) { 
  689. bp_xprofile_update_field_meta($field_id, 'do_select2', '' ); 
  690. return; 
  691.  
  692. // Save select2 settings. 
  693. if ( 1 != $field_id ) { 
  694. if ( isset( $_POST['do_select2'] ) && 'on' === wp_unslash( $_POST['do_select2'] ) ) { 
  695. bp_xprofile_update_field_meta( $field_id, 'do_select2', 'on' ); 
  696. } else { 
  697. bp_xprofile_update_field_meta( $field_id, 'do_select2', 'off' ); 
  698.  
  699. public function bxcft_enable_select2_field() { 
  700. global $field; 
  701.  
  702. if (!in_array($field->type, $this->fields_type_with_select2)) { 
  703. return; 
  704.  
  705. $do_select2 = bp_xprofile_get_meta($field->id, 'field', 'do_select2'); 
  706. if ($do_select2 === 'on') { 
  707. $field_name_id = bp_get_the_profile_field_input_name(); 
  708. if (in_array($field->type, $this->fields_type_multiple)) { 
  709. $field_name_id .= '[]'; 
  710.  
  711. $allow_new_tags = false; 
  712. $options = $field->get_children(); 
  713. foreach ($options as $option) { 
  714. if (Bxcft_Field_Type_MultiSelectCustomTaxonomy::ALLOW_NEW_TAGS === $option->name) { 
  715. $allow_new_tags = true; 
  716.  
  717. if ($allow_new_tags) { 
  718. ?> 
  719. <script> 
  720. jQuery(function($) { 
  721. $('select[name="<?php echo $field_name_id; ?>"]').select2({ 
  722. tags: true,  
  723. tokenSeparators: [', '] 
  724. }); 
  725. }); 
  726. </script> 
  727. <?php 
  728. } else { 
  729. ?> 
  730. <script> 
  731. jQuery(function($) { 
  732. $('select[name="<?php echo $field_name_id; ?>"]').select2(); 
  733. }); 
  734. </script> 
  735. <?php 
  736.  
  737. public function bxcft_update() 
  738. $locale = apply_filters( 'bxcft_load_load_textdomain_get_locale', get_locale() ); 
  739. if ( !empty( $locale ) ) { 
  740. $mofile_default = sprintf( '%slang/%s.mo', plugin_dir_path(__FILE__), $locale ); 
  741. $mofile = apply_filters( 'bxcft_load_textdomain_mofile', $mofile_default ); 
  742.  
  743. if ( file_exists( $mofile ) ) { 
  744. load_textdomain( "bxcft", $mofile ); 
  745.  
  746. if (!get_option('bxcft_activated')) { 
  747. add_option('bxcft_activated', 1); 
  748. if (!get_option('bxcft_notices')) { 
  749. add_option('bxcft_notices'); 
  750.  
  751. /** 
  752. * Returns true if autolink filter is removed,  
  753. * false if it exists. 
  754. * @since 2.4.6 
  755. * @return boolean 
  756. */ 
  757. public static function is_autolink_filter_removed() { 
  758. return !(has_filter('bp_get_the_profile_field_value',  
  759. 'xprofile_filter_link_profile_data', 9)); 
  760.  
  761. public static function activate() 
  762. add_option('bxcft_activated', 1); 
  763. add_option('bxcft_notices', array()); 
  764.  
  765. public static function deactivate() 
  766. delete_option('bxcft_activated'); 
  767. delete_option('bxcft_notices');