Example #1
0
 public function test_passing_empty_member_type_to_get_fields_for_member_type_should_return_unrestricted_fields()
 {
     $f2 = $this->factory->xprofile_field->create(array('field_group_id' => $this->field_group_id));
     $field_2 = new BP_XProfile_Field($f2);
     $field_2->set_member_types(array('foo'));
     $found = BP_XProfile_Field::get_fields_for_member_type('');
     $this->assertEqualSets(array(1, $this->field_id), array_keys($found));
 }
 /**
  * @group member_types
  * @ticket BP5192
  */
 public function test_member_type_null_should_be_respected()
 {
     $g = $this->factory->xprofile_group->create();
     $f1 = $this->factory->xprofile_field->create(array('field_group_id' => $g));
     $f2 = $this->factory->xprofile_field->create(array('field_group_id' => $g));
     bp_register_member_type('foo');
     bp_register_member_type('bar');
     $field1 = new BP_XProfile_Field($f1);
     $field1->set_member_types(array('foo'));
     $found_groups = BP_XProfile_Group::get(array('member_type' => array('null'), 'fetch_fields' => true));
     // The groups aren't indexed, so we have to go looking for it.
     foreach ($found_groups as $fg) {
         if ($g == $fg->id) {
             $the_group = $fg;
         }
     }
     $this->assertNotContains($f1, wp_list_pluck($the_group->fields, 'id'));
     $this->assertContains($f2, wp_list_pluck($the_group->fields, 'id'));
 }
/**
 * Handles the adding or editing of profile field data for a user.
 */
function xprofile_admin_manage_field($group_id, $field_id = null)
{
    global $wpdb, $message, $groups;
    $bp = buddypress();
    $field = new BP_XProfile_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 BuddyPress (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();
    }
}