/**
  * Save condition information when a Field is added/edited
  * 
  * @param type $field
  * @return type
  */
 public function save_field_condition($field)
 {
     if (isset($_POST['xprofile-condition-display'])) {
         if (empty($_POST['xprofile-condition-display'])) {
             $this->delete_condition($field->id);
             return;
         }
         if (!wp_verify_nonce($_POST['xprofile-condition-edit-nonce'], 'xprofile-condition-edit-action')) {
             return;
         }
         //if we are here, we need to set the condition
         $visibility = $_POST['xprofile-condition-display'];
         //no need to worry about it, we will explicitly check visibility after .000001ms from here
         $other_field_id = absint($_POST['xprofile-condition-other-field']);
         //field id must be an integer
         $operator = $this->validate_operator($_POST['xprofile-condition-operator'], $other_field_id);
         //check for valid operator
         //sanitize the field value
         $value = $_POST['xprofile-condition-other-field-value'];
         //$value = $this->sanitize_value( $value, $other_field_id );
         if (in_array($visibility, array('show', 'hide')) && $other_field_id && $operator) {
             //make sure that all the fields are set
             //what about empty value?
             //let us update it then
             bp_xprofile_update_field_meta($field->id, 'xprofile_condition_display', $visibility);
             bp_xprofile_update_field_meta($field->id, 'xprofile_condition_other_field', $other_field_id);
             bp_xprofile_update_field_meta($field->id, 'xprofile_condition_operator', $operator);
             bp_xprofile_update_field_meta($field->id, 'xprofile_condition_other_field_value', $value);
         }
     }
     //we need to check if the condition was save, if yes, let us keep that condition in the meta
 }
Esempio n. 2
0
/**
 * Handles the adding or editing of profile field data for a user.
 */
function xprofile_admin_manage_field($group_id, $field_id = null)
{
    global $bp, $wpdb, $message, $groups;
    $field = new BP_XProfile_Field($field_id);
    $field->group_id = $group_id;
    if (isset($_POST['saveField'])) {
        if (BP_XProfile_Field::admin_validate()) {
            $field->name = wp_filter_kses($_POST['title']);
            $field->description = !empty($_POST['description']) ? wp_filter_kses($_POST['description']) : '';
            $field->is_required = wp_filter_kses($_POST['required']);
            $field->type = wp_filter_kses($_POST['fieldtype']);
            if (!empty($_POST["sort_order_{$field->type}"])) {
                $field->order_by = wp_filter_kses($_POST["sort_order_{$field->type}"]);
            }
            $field->field_order = $wpdb->get_var($wpdb->prepare("SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id));
            if (!$field->field_order) {
                $field->field_order = (int) $wpdb->get_var($wpdb->prepare("SELECT max(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id));
                $field->field_order++;
            }
            // For new profile fields, set the $field_id. For existing profile fields,
            // this will overwrite $field_id with the same value.
            $field_id = $field->save();
            if (!$field_id) {
                $message = __('There was an error saving the field. Please try again', 'buddypress');
                $type = 'error';
                unset($_GET['mode']);
                xprofile_admin($message, $type);
            } else {
                $message = __('The field was saved successfully.', 'buddypress');
                $type = 'success';
                if (1 == $field_id) {
                    bp_update_option('bp-xprofile-fullname-field-name', $field->name);
                }
                if (!empty($_POST['default-visibility'])) {
                    bp_xprofile_update_field_meta($field_id, 'default_visibility', $_POST['default-visibility']);
                }
                if (!empty($_POST['allow-custom-visibility'])) {
                    bp_xprofile_update_field_meta($field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility']);
                }
                unset($_GET['mode']);
                do_action('xprofile_fields_saved_field', $field);
                $groups = bp_xprofile_get_groups();
                xprofile_admin($message, $type);
            }
        } else {
            $field->render_admin_form($message);
        }
    } else {
        $field->render_admin_form();
    }
}
/**
 * Handles the adding or editing of profile field data for a user.
 *
 * @param int      $group_id ID of the group.
 * @param int|null $field_id ID of the field being managed.
 */
function xprofile_admin_manage_field($group_id, $field_id = null)
{
    global $wpdb, $message, $groups;
    $bp = buddypress();
    if (is_null($field_id)) {
        $field = new BP_XProfile_Field();
    } else {
        $field = xprofile_get_field($field_id);
    }
    $field->group_id = $group_id;
    if (isset($_POST['saveField'])) {
        if (BP_XProfile_Field::admin_validate()) {
            $field->is_required = $_POST['required'];
            $field->type = $_POST['fieldtype'];
            $field->name = $_POST['title'];
            if (!empty($_POST['description'])) {
                $field->description = $_POST['description'];
            } else {
                $field->description = '';
            }
            if (!empty($_POST["sort_order_{$field->type}"])) {
                $field->order_by = $_POST["sort_order_{$field->type}"];
            }
            $field->field_order = $wpdb->get_var($wpdb->prepare("SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id));
            if (empty($field->field_order) || is_wp_error($field->field_order)) {
                $field->field_order = (int) $wpdb->get_var($wpdb->prepare("SELECT max(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id));
                $field->field_order++;
            }
            // For new profile fields, set the $field_id. For existing profile
            // fields, this will overwrite $field_id with the same value.
            $field_id = $field->save();
            if (empty($field_id)) {
                $message = __('There was an error saving the field. Please try again.', 'buddypress');
                $type = 'error';
            } else {
                $message = __('The field was saved successfully.', 'buddypress');
                $type = 'success';
                // @todo remove these old options
                if (1 == $field_id) {
                    bp_update_option('bp-xprofile-fullname-field-name', $field->name);
                }
                // Set member types.
                if (isset($_POST['has-member-types'])) {
                    $member_types = array();
                    if (isset($_POST['member-types'])) {
                        $member_types = stripslashes_deep($_POST['member-types']);
                    }
                    $field->set_member_types($member_types);
                }
                // Validate default visibility.
                if (!empty($_POST['default-visibility']) && in_array($_POST['default-visibility'], wp_list_pluck(bp_xprofile_get_visibility_levels(), 'id'))) {
                    bp_xprofile_update_field_meta($field_id, 'default_visibility', $_POST['default-visibility']);
                }
                // Validate custom visibility.
                if (!empty($_POST['allow-custom-visibility']) && in_array($_POST['allow-custom-visibility'], array('allowed', 'disabled'))) {
                    bp_xprofile_update_field_meta($field_id, 'allow_custom_visibility', $_POST['allow-custom-visibility']);
                }
                // Validate signup.
                if (!empty($_POST['signup-position'])) {
                    bp_xprofile_update_field_meta($field_id, 'signup_position', (int) $_POST['signup-position']);
                } else {
                    bp_xprofile_delete_meta($field_id, 'field', 'signup_position');
                }
                /**
                 * Fires at the end of the process to save a field for a user, if successful.
                 *
                 * @since 1.0.0
                 *
                 * @param BP_XProfile_Field $field Current BP_XProfile_Field object.
                 */
                do_action('xprofile_fields_saved_field', $field);
                $groups = bp_xprofile_get_groups();
            }
            unset($_GET['mode']);
            xprofile_admin($message, $type);
        } else {
            $field->render_admin_form($message);
        }
    } else {
        $field->render_admin_form();
    }
}
function bpdd_import_users_profile()
{
    /** @var $wpdb WPDB */
    global $bp, $wpdb;
    $data = array();
    $xprofile_structure = (require_once dirname(__FILE__) . '/data/xprofile_structure.php');
    // first import profile groups
    foreach ($xprofile_structure as $group_type => $group_data) {
        $group_id = xprofile_insert_field_group(array('name' => $group_data['name'], 'description' => $group_data['desc']));
        // then import fields
        foreach ($group_data['fields'] as $field_type => $field_data) {
            $field_id = xprofile_insert_field(array('field_group_id' => $group_id, 'parent_id' => 0, 'type' => $field_type, 'name' => $field_data['name'], 'description' => $field_data['desc'], 'is_required' => $field_data['required'], 'order_by' => 'custom'));
            if ($field_id) {
                bp_xprofile_update_field_meta($field_id, 'default_visibility', $field_data['default-visibility']);
                bp_xprofile_update_field_meta($field_id, 'allow_custom_visibility', $field_data['allow-custom-visibility']);
                $data[$field_id]['type'] = $field_type;
                // finally import options
                if (!empty($field_data['options'])) {
                    foreach ($field_data['options'] as $option) {
                        $option_id = xprofile_insert_field(array('field_group_id' => $group_id, 'parent_id' => $field_id, 'type' => 'option', 'name' => $option['name'], 'can_delete' => true, 'is_default_option' => $option['is_default_option'], 'option_order' => $option['option_order']));
                        $data[$field_id]['options'][$option_id] = $option['name'];
                    }
                } else {
                    $data[$field_id]['options'] = array();
                }
            }
        }
    }
    $xprofile_data = (require_once dirname(__FILE__) . '/data/xprofile_data.php');
    $users = bpdd_get_random_users_ids(0);
    // now import profile fields data for all fields for each user
    $count = 0;
    foreach ($users as $user_id) {
        foreach ($data as $field_id => $field_data) {
            switch ($field_data['type']) {
                case 'datebox':
                case 'textarea':
                case 'number':
                case 'textbox':
                case 'url':
                case 'selectbox':
                case 'radio':
                    if (xprofile_set_field_data($field_id, $user_id, $xprofile_data[$field_data['type']][array_rand($xprofile_data[$field_data['type']])])) {
                        $count++;
                    }
                    break;
                case 'checkbox':
                case 'multiselectbox':
                    if (xprofile_set_field_data($field_id, $user_id, explode(',', $xprofile_data[$field_data['type']][array_rand($xprofile_data[$field_data['type']])]))) {
                        $count++;
                    }
                    break;
            }
        }
    }
    return $count;
}
 function save_admin_field($field)
 {
     if (isset($_POST['bp_smp'])) {
         // When creating a new field, no field_id will have been set yet. We'll
         // look it up based on the $field object passed to the hook
         if (empty($this->field_id)) {
             $this->field_id = BP_XProfile_Field::get_id_from_name($field->name);
         }
         $new_smp_data = $_POST['bp_smp'];
         bp_xprofile_update_field_meta($this->field_id, 'bp_smp_data', $new_smp_data);
     }
 }