id = 'sp_xprofile';
if( bp_is_active( 'xprofile' ) ) {
add_action('xprofile_field_after_contentbox', array( $this, 'add_content_box' ), 12, 1);
add_action('xprofile_fields_saved_field', array( $this, 'save_options' ), 12, 1);
add_filter('bp_get_the_profile_field_name', array( $this, 'sp_replace_labels' ), 12, 1);
add_filter('bp_has_profile', array( $this, 'sp_make_noneditable' ), 12, 2);
add_filter('bp_has_profile', array( $this, 'sp_hide_registration_fields' ), 12, 2);
add_action('bp_after_profile_edit_content', array( $this, 'add_validation' ));
add_action('bp_after_register_page', array( $this, 'add_validation' ));
add_filter( 'manage_users_columns', array( $this, 'add_bp_field_colums' ) );
add_action( 'manage_users_custom_column', array( $this, 'bp_field_column_content' ), 10, 3);
}
}
/**
* [field_value description]
* @param [type] $value [description]
* @return [type] [description]
*/
public function field_value( $value )
{
if ( ! empty( $value ) ) {
return sanitize_text_field( $value );
}
}
/**
* [add_content_box description]
* @param [type] $field [description]
*/
public function add_content_box( $field )
{
do_action('sp_before_advanced_xprofile_fields', $field);
$labels = bp_xprofile_get_meta($field->id, 'field', $this->id.'_labels');
$r = wp_parse_args($labels, array(
'registration' => '',
'self' => '',
'user' => '',
'edit' => '',
'admin' => ''
));
$validation_methods = bp_xprofile_get_meta($field->id, 'field', $this->id.'_validation');
$v = wp_parse_args($validation_methods, array(
'enable' => array(
'char_limit' => 0,
'min_chars' => 0,
'text_format' => 0
),
'char_limit' => '',
'min_chars' => '',
'text_format' => ''
));
$option_values = bp_xprofile_get_meta($field->id, 'field', $this->id.'_options');
$options = wp_parse_args($option_values, array(
'hide_registration' => '',
'admin_approval' => '',
'admin_column' => '',
'non_editable' => ''
));
?>
id, $this->id.'_labels', $_POST['advanced_xprofile']);
bp_xprofile_update_field_meta($field->id, $this->id.'_options', $_POST['advanced_xprofile_options']);
bp_xprofile_update_field_meta($field->id, $this->id.'_validation', $_POST['advanced_xprofile_validation']);
//Save Admin Columns
$user_columns = get_option($this->id.'user_columns');
if (empty($user_columns)) {
$user_columns = array();
}
if (!empty($_POST['advanced_xprofile_options']['admin_column'])) :
$user_columns[] = $field->id;
else :
if (in_array($field->id, $user_columns)) {
if (($key = array_search($field->id, $user_columns)) !== false) {
unset($user_columns[$key]);
}
}
endif;
update_option($this->id.'user_columns', $user_columns);
}
public function sp_replace_labels($name)
{
global $field;
global $bp;
$labels = bp_xprofile_get_meta($field->id, 'field', $this->id.'_labels');
$r = wp_parse_args($labels, array(
'registration' => $field->name,
'self' => $field->name,
'user' => $field->name,
'edit' => $field->name
));
if ($bp->current_component=='profile' && $bp->current_action=='edit') :
$name = ($r['edit'] ? $r['edit'] : $name);
elseif ($bp->current_component=='profile' && $bp->current_action=='public') :
if ($bp->displayed_user->id == $bp->loggedin_user->id) :
$name = ($r['self'] ? $r['self'] : $name);
else :
$name = ($r['user'] ? $r['user'] : $name);
endif;
elseif ($bp->current_component=='register') :
$name = ($r['registration'] ? $r['registration'] : $name);
endif;
return $name;
}
public function sp_hide_registration_fields($has_groups, $profile_template)
{
global $bp;
$user_id = $profile_template->user_id;
if ($bp->current_component == 'register') {
if (!empty($profile_template->groups)) {
$keep_group = array();
$group_inc = 0;
foreach ($profile_template->groups as $group) {
if (!empty($group->fields)) {
$keep_field = array();
foreach ($group->fields as $field) {
$option_values = bp_xprofile_get_meta($field->id, 'field', $this->id.'_options');
if (empty($option_values['hide_registration']) || $option_values['hide_registration']!=1) :
$keep_field[] = $field;
endif;
}
if (!empty($keep_field)) :
$profile_template->groups[$group_inc]->fields = $keep_field;
else :
unset($profile_template->groups[$group_inc]);
endif;
}
$group_inc++;
}
}
}
return $profile_template;
}
public function sp_make_noneditable($has_groups, $profile_template)
{
global $bp;
if (current_user_can('edit_users')) {
return $profile_template;
}
$user_id = $profile_template->user_id;
if ($bp->current_component == 'profile') {
if (!empty($profile_template->groups)) {
$keep_group = array();
$group_inc = 0;
foreach ($profile_template->groups as $group) {
if (!empty($group->fields)) {
$keep_field = array();
foreach ($group->fields as $field) {
$option_values = bp_xprofile_get_meta($field->id, 'field', $this->id.'_options');
if (empty($option_values['non_editable']) || $option_values['non_editable']!=1 && !empty($field->value)) :
$keep_field[] = $field;
endif;
}
if (!empty($keep_field)) :
$profile_template->groups[$group_inc]->fields = $keep_field;
else :
unset($profile_template->groups[$group_inc]);
endif;
}
$group_inc++;
}
}
}
return $profile_template;
}
/**
* Add Validation
*/
public function add_validation()
{
global $wpdb;
global $bp;
$field_ids = $wpdb->get_col("SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id = ". bp_get_current_profile_group_id());
if (!empty($field_ids)) :
?>
id.'user_columns');
if (!empty($user_columns)) :
foreach ($user_columns as $key => $field_id) :
$labels = bp_xprofile_get_meta($field_id, 'field', $this->id.'_labels');
$field = xprofile_get_field($field_id);
$name = (!empty($labels['admin']) ? $labels['admin'] : $field->name );
$id = sanitize_title($name);
$id = str_replace('-', '_', $id);
$columns['field_'.$field->id] = $name;
endforeach;
endif;
return $columns;
}
/**
* Get field value for table column
* @param string $value [description]
* @param string $column_name [description]
* @param integer $user_id [description]
* @return string
*/
public function bp_field_column_content( $value = '', $column_name = '', $user_id = 0 ) {
$user_columns = get_option($this->id . 'user_columns');
if ( !empty( $user_columns ) ) :
$field = str_replace('field_', '', $column_name);
if ( in_array( $field, $user_columns ) ) {
$field_data = xprofile_get_field_data( $field, $user_id, 'comma' );
return maybe_unserialize( $field_data );
}
endif;
return $value;
}
}
endif;
add_action('bp_init', 'sp_advanced_xprofile_initiate');
function sp_advanced_xprofile_initiate(){
SP_Advanced_XProfile::instance();
}
/**
* Load language
*/
add_action('plugins_loaded', 'bp_advanced_xprofile_language');
function bp_advanced_xprofile_language(){
load_plugin_textdomain('sp-advanced-xprofile', false, dirname(plugin_basename(__FILE__)) . '/languages');
}