/**
  * @ticket BP6545
  */
 public function test_newly_created_field_should_have_field_id_property_set()
 {
     $field = new BP_XProfile_Field();
     $field->group_id = 1;
     $field->name = 'Foo';
     $new_field_id = $field->save();
     $this->assertSame($new_field_id, $field->id);
 }
/**
 * Insert or update an xprofile field.
 *
 * @param array $args {
 *     Array of arguments.
 *     @type int    $field_id          Optional. Pass the ID of an existing field to edit that field.
 *     @type int    $field_group_id    ID of the associated field group.
 *     @type int    $parent_id         Optional. ID of the parent field.
 *     @type string $type              Field type. Checked against a field_types whitelist.
 *     @type string $name              Name of the new field.
 *     @type string $description       Optional. Descriptive text for the field.
 *     @type bool   $is_required       Optional. Whether users must provide a value for the field. Default: false.
 *     @type bool   $can_delete        Optional. Whether admins can delete this field in the Dashboard interface.
 *                                     Generally this is false only for the Name field, which is required throughout BP.
 *                                     Default: true.
 *     @type string $order_by          Optional. For field types that support options (such as 'radio'), this flag
 *                                     determines whether the sort order of the options will be 'default'
 *                                     (order created) or 'custom'.
 *     @type bool   $is_default_option Optional. For the 'option' field type, setting this value to true means that
 *                                     it'll be the default value for the parent field when the user has not yet
 *                                     overridden. Default: true.
 *     @type int    $option_order      Optional. For the 'option' field type, this determines the order in which the
 *                                     options appear.
 * }
 * @return bool|int False on failure, ID of new field on success.
 */
function xprofile_insert_field($args = '')
{
    $r = wp_parse_args($args, array('field_id' => null, 'field_group_id' => null, 'parent_id' => null, 'type' => '', 'name' => '', 'description' => '', 'is_required' => false, 'can_delete' => true, 'order_by' => '', 'is_default_option' => false, 'option_order' => null, 'field_order' => null));
    // field_group_id is required
    if (empty($r['field_group_id'])) {
        return false;
    }
    // Check this is a non-empty, valid field type.
    if (!in_array($r['type'], (array) buddypress()->profile->field_types)) {
        return false;
    }
    // Instantiate a new field object
    if (!empty($r['field_id'])) {
        $field = new BP_XProfile_Field($r['field_id']);
    } else {
        $field = new BP_XProfile_Field();
    }
    $field->group_id = $r['field_group_id'];
    $field->type = $r['type'];
    // The 'name' field cannot be empty.
    if (!empty($r['name'])) {
        $field->name = $r['name'];
    }
    $field->description = $r['description'];
    $field->order_by = $r['order_by'];
    $field->parent_id = (int) $r['parent_id'];
    $field->field_order = (int) $r['field_order'];
    $field->option_order = (int) $r['option_order'];
    $field->is_required = (bool) $r['is_required'];
    $field->can_delete = (bool) $r['can_delete'];
    $field->is_default_option = (bool) $r['is_default_option'];
    return $field->save();
}
Example #3
0
function xprofile_admin_manage_field($group_id, $field_id = null)
{
    global $bp, $nxtdb, $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 = nxt_filter_kses($_POST['title']);
            $field->description = !empty($_POST['description']) ? nxt_filter_kses($_POST['description']) : '';
            $field->is_required = nxt_filter_kses($_POST['required']);
            $field->type = nxt_filter_kses($_POST['fieldtype']);
            if (!empty($_POST["sort_order_{$field->type}"])) {
                $field->order_by = nxt_filter_kses($_POST["sort_order_{$field->type}"]);
            }
            $field->field_order = $nxtdb->get_var($nxtdb->prepare("SELECT field_order FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id));
            if (!$field->field_order) {
                $field->field_order = (int) $nxtdb->get_var($nxtdb->prepare("SELECT max(field_order) FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id));
                $field->field_order++;
            }
            if (!$field->save()) {
                $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);
                }
                unset($_GET['mode']);
                do_action('xprofile_fields_saved_field', $field);
                $groups = BP_XProfile_Group::get();
                xprofile_admin($message, $type);
            }
        } else {
            $field->render_admin_form($message);
        }
    } else {
        $field->render_admin_form();
    }
}
function xprofile_insert_field($args = '')
{
    global $bp;
    extract($args);
    /**
     * Possible parameters (pass as assoc array):
     *	'field_id'
     *	'field_group_id'
     *	'parent_id'
     *	'type'
     *	'name'
     *	'description'
     *	'is_required'
     *	'can_delete'
     *	'field_order'
     *	'order_by'
     *	'is_default_option'
     *	'option_order'
     */
    // Check we have the minimum details
    if (!$field_group_id) {
        return false;
    }
    // Check this is a valid field type
    if (!in_array($type, (array) $bp->profile->field_types)) {
        return false;
    }
    // Instantiate a new field object
    if ($field_id) {
        $field = new BP_XProfile_Field($field_id);
    } else {
        $field = new BP_XProfile_Field();
    }
    $field->group_id = $field_group_id;
    if (!empty($parent_id)) {
        $field->parent_id = $parent_id;
    }
    if (!empty($type)) {
        $field->type = $type;
    }
    if (!empty($name)) {
        $field->name = $name;
    }
    if (!empty($description)) {
        $field->description = $description;
    }
    if (!empty($is_required)) {
        $field->is_required = $is_required;
    }
    if (!empty($can_delete)) {
        $field->can_delete = $can_delete;
    }
    if (!empty($field_order)) {
        $field->field_order = $field_order;
    }
    if (!empty($order_by)) {
        $field->order_by = $order_by;
    }
    if (!empty($is_default_option)) {
        $field->is_default_option = $is_default_option;
    }
    if (!empty($option_order)) {
        $field->option_order = $option_order;
    }
    return $field->save();
}
Example #5
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();
    }
}
Example #6
0
/**
 * 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);
                }
                // 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();
    }
}
function xprofile_admin_manage_field($group_id, $field_id = null)
{
    global $message, $groups;
    $field = new BP_XProfile_Field($field_id);
    $field->group_id = $group_id;
    if (isset($_POST['saveField'])) {
        if (BP_XProfile_Field::admin_validate($_POST)) {
            $field->name = wp_filter_kses($_POST['title']);
            $field->desc = wp_filter_kses($_POST['description']);
            $field->is_required = wp_filter_kses($_POST['required']);
            $field->is_public = wp_filter_kses($_POST['public']);
            $field->type = wp_filter_kses($_POST['fieldtype']);
            $field->order_by = wp_filter_kses($_POST["sort_order_{$field->type}"]);
            if (!$field->save()) {
                $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';
                unset($_GET['mode']);
                do_action('xprofile_fields_saved_field', $field);
                $groups = BP_XProfile_Group::get_all();
                xprofile_admin($message, $type);
            }
        } else {
            $field->render_admin_form($message);
        }
    } else {
        $field->render_admin_form();
    }
}
Example #8
0
/**
 * Insert an xprofile field.
 *
 * @param array $args {
 *     Array of arguments.
 *     @type int $field_id Optional. Pass the ID of an existing field to edit
 *           that field.
 *     @type int $field_group_id ID of the associated field group.
 *     @type int $parent_id Optional. ID of the parent field.
 *     @type string $type Field type. Checked against a field_types whitelist.
 *     @type string $name Name of the new field.
 *     @type string $description Optional. Descriptive text for the field.
 *     @type bool $is_required Optional. Whether users must provide a value for
 *           the field. Default: false.
 *     @type bool $can_delete Optional. Whether admins can delete this field in
 *           the Dashboard interface. Generally this is true only for the Name
 *           field, which is required throughout BP. Default: true.
 *     @type string $order_by Optional. For field types that support options
 *           (such as 'radio'), this flag determines whether the sort order of
 *           the options will be 'default' (order created) or 'custom'.
 *     @type bool $is_default_option Optional. For the 'option' field type,
 *           setting this value to true means that it'll be the default value
 *           for the parent field when the user has not yet overridden.
 *     @type int $option_order Optional. For the 'option' field type, this
 *           determines the order in which the options appear.
 * }
 * @return bool|int False on failure, ID of new field on success.
 */
function xprofile_insert_field($args = '')
{
    global $bp;
    $r = wp_parse_args($args, array('field_id' => null, 'field_group_id' => null, 'parent_id' => null, 'type' => '', 'name' => '', 'description' => '', 'is_required' => false, 'can_delete' => true, 'order_by' => '', 'is_default_option' => false, 'option_order' => null));
    // field_group_id is required
    if (empty($r['field_group_id'])) {
        return false;
    }
    // Check this is a valid field type
    if (!in_array($r['type'], (array) $bp->profile->field_types)) {
        return false;
    }
    // Instantiate a new field object
    if (!empty($r['field_id'])) {
        $field = new BP_XProfile_Field($r['field_id']);
    } else {
        $field = new BP_XProfile_Field();
    }
    $field->group_id = $r['field_group_id'];
    if (!empty($r['parent_id'])) {
        $field->parent_id = $r['parent_id'];
    }
    if (!empty($r['type'])) {
        $field->type = $r['type'];
    }
    if (!empty($r['name'])) {
        $field->name = $r['name'];
    }
    if (!empty($r['description'])) {
        $field->description = $r['description'];
    }
    if (!empty($r['is_required'])) {
        $field->is_required = $r['is_required'];
    }
    if (!empty($r['can_delete'])) {
        $field->can_delete = $r['can_delete'];
    }
    if (!empty($r['field_order'])) {
        $field->field_order = $r['field_order'];
    }
    if (!empty($r['order_by'])) {
        $field->order_by = $r['order_by'];
    }
    if (!empty($r['is_default_option'])) {
        $field->is_default_option = $r['is_default_option'];
    }
    if (!empty($r['option_order'])) {
        $field->option_order = $r['option_order'];
    }
    return $field->save();
}