function cfbgr_migrate_xprofile_as_member_types() { global $wpdb; $buddypress = buddypress(); // Description of this tool, displayed to the user $statement = __('Migrating/Resetting xProfile data as member types: %s', 'buddypress-group-restrictions'); // Default to failure text $result = __('No xProfile data needs to be migrated or reset.', 'buddypress-group-restrictions'); // Default to unrepaired $repair = 0; $field = (int) bp_get_option('cfbgr_xfield_id', 0); if (empty($field)) { return array(0, sprintf($statement, $result)); } $member_types = bp_get_member_types(); // Walk through all users on the site $user_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->users}"); foreach ($user_ids as $user_id) { $value = sanitize_key(xprofile_get_field_data($field, $user_id)); // Do we have a matching member type ? if (isset($member_types[$value])) { // Set member types if empty or different if ($value !== bp_get_member_type($user_id)) { bp_set_member_type($user_id, $value); $repair += 1; } } } $result = sprintf(__('%d migrated or reset', 'buddypress-group-restrictions'), $repair); // All done! return array(0, sprintf($statement, $result)); }
/** * @ticket BP6996 */ public function test_should_contain_member_type_classes() { buddypress()->members->types = array(); bp_register_member_type('foo'); bp_register_member_type('bar'); $u = $this->factory->user->create(); bp_set_member_type($u, 'bar'); if (bp_has_members(array('include' => array($u)))) { while (bp_members()) { bp_the_member(); $found = bp_get_member_class(); } } global $members_template; unset($members_template); buddypress()->members->types = array(); $this->assertContains('member-type-bar', $found); $this->assertNotContains('member-type-foo', $found); }
/** * Process bulk member type change submission from the WP admin users list table. * * @since 2.7.0 */ public function users_table_process_bulk_type_change() { // Output the admin notice. $this->users_type_change_notice(); // Bail if no users are specified or if this isn't a BuddyPress action. if (empty($_REQUEST['users']) || empty($_REQUEST['bp_change_type']) && empty($_REQUEST['bp_change_type2']) || empty($_REQUEST['bp_change_member_type'])) { return; } // Bail if nonce check fails. check_admin_referer('bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce'); // Bail if current user cannot promote users. if (!bp_current_user_can('promote_users')) { return; } $new_type = ''; if (!empty($_REQUEST['bp_change_type2'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type2']); } elseif (!empty($_REQUEST['bp_change_type'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type']); } // Check that the selected type actually exists. if ('remove_member_type' != $new_type && null === bp_get_member_type_object($new_type)) { $error = true; } else { // Run through user ids. $error = false; foreach ((array) $_REQUEST['users'] as $user_id) { $user_id = (int) $user_id; // Get the old member type to check against. $member_type = bp_get_member_type($user_id); if ('remove_member_type' === $new_type) { // Remove the current member type, if there's one to remove. if ($member_type) { $removed = bp_remove_member_type($user_id, $member_type); if (false === $removed || is_wp_error($removed)) { $error = true; } } } else { // Set the new member type. if ($new_type !== $member_type) { $set = bp_set_member_type($user_id, $new_type); if (false === $set || is_wp_error($set)) { $error = true; } } } } } // If there were any errors, show the error message. if ($error) { $redirect = add_query_arg(array('updated' => 'member-type-change-error'), wp_get_referer()); } else { $redirect = add_query_arg(array('updated' => 'member-type-change-success'), wp_get_referer()); } wp_redirect($redirect); exit; }
/** * Update User member type association * * @return type */ public function update_member_type() { //none of our actions are set if (empty($_REQUEST['new_member_type_top']) && empty($_REQUEST['new_member_type_bottom'])) { return; } //only admin/super admin if (!current_user_can('edit_users')) { return; } $member_type = ''; if (!empty($_REQUEST['new_member_type_top'])) { $member_type = trim($_REQUEST['new_member_type_top']); } else { $member_type = trim($_REQUEST['new_member_type_bottom']); } $input_name = 'users'; if (is_multisite() && is_network_admin()) { $input_name = 'allusers'; } $users = isset($_REQUEST[$input_name]) ? $_REQUEST[$input_name] : array(); if (empty($users)) { return; //no user selected } // $users = wp_parse_id_list($users); $member_type = sanitize_key($member_type); $member_type_object = bp_get_member_type_object($member_type); if (empty($member_type_object)) { return; //the member type does not seem to be registered } $updated = 0; foreach ($users as $user_id) { bp_set_member_type($user_id, $member_type); } $updated = 1; $this->message = sprintf(__('Updated member type for %d user(s) to %s. '), count($users), $member_type_object->labels['singular_name']); if (is_network_admin()) { $url = network_admin_url('users.php'); } else { $url = admin_url('users.php'); } $redirect = add_query_arg(array('updated' => $updated, 'bp-member-type-message' => urlencode($this->message)), $url); wp_safe_redirect($redirect); exit(0); }
/** * Process changes from the Member Type metabox. * * @since 2.2.0 */ public function process_member_type_update() { if (!isset($_POST['bp-member-type-nonce']) || !isset($_POST['bp-members-profile-member-type'])) { return; } $user_id = $this->get_user_id(); check_admin_referer('bp-member-type-change-' . $user_id, 'bp-member-type-nonce'); // Permission check. if (!current_user_can('bp_moderate') && $user_id != bp_loggedin_user_id()) { return; } // Member type string must either reference a valid member type, or be empty. $member_type = stripslashes($_POST['bp-members-profile-member-type']); if (!empty($member_type) && !bp_get_member_type_object($member_type)) { return; } /* * If an invalid member type is passed, someone's doing something * fishy with the POST request, so we can fail silently. */ if (bp_set_member_type($user_id, $member_type)) { // @todo Success messages can't be posted because other stuff happens on the page load. } }
/** * Delete a user's member type when the user when the user is deleted. * * @since 2.2.0 * * @param int $user_id ID of the user. * @return array $value See {@see bp_set_member_type()}. */ function bp_remove_member_type_on_user_delete($user_id) { return bp_set_member_type($user_id, ''); }
/** * @group member_types * @ticket BP5192 */ public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() { $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')); $field2 = new BP_XProfile_Field($f2); $field2->set_member_types(array('bar')); $u = $this->factory->user->create(); bp_set_member_type($u, 'foo'); $found_groups = BP_XProfile_Group::get(array('user_id' => $u, 'member_type' => 'bar', '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->assertContains($f1, wp_list_pluck($the_group->fields, 'id')); }
/** * @group cache * @group member_types */ public function test_member_type_should_be_prefetched_into_cache_during_user_query() { bp_register_member_type('foo'); bp_register_member_type('bar'); $users = $this->factory->user->create_many(4); bp_set_member_type($users[0], 'foo'); bp_set_member_type($users[1], 'bar'); bp_set_member_type($users[2], 'foo'); $q = new BP_User_Query(array('include' => $users)); $this->assertSame(array('foo'), wp_cache_get($users[0], 'bp_member_member_type')); $this->assertSame(array('bar'), wp_cache_get($users[1], 'bp_member_member_type')); $this->assertSame(array('foo'), wp_cache_get($users[2], 'bp_member_member_type')); $this->assertSame('', wp_cache_get($users[3], 'bp_member_member_type')); }
/** * Each time a field will be saved we need to check if it's * not our member type field type... * * @param BP_XProfile_Field $field */ function cfbgr_save_xprofile_as_member_type($field = null) { if (!empty($field->user_id) && !empty($field->field_id)) { $saved_option = (int) bp_get_option('cfbgr_xfield_id', 0); if (!empty($saved_option) && $saved_option === (int) $field->field_id) { $member_type = maybe_unserialize($field->value); if (is_array($member_type)) { $member_type = array_pop($member_type); } // Save the member type for the user. bp_set_member_type($field->user_id, $member_type); } } }
/** * @group BP6138 */ public function test_bp_has_member_type_should_return_true_on_success() { $u1 = $this->factory->user->create(); bp_register_member_type('foo'); bp_register_member_type('bar'); bp_set_member_type($u1, 'foo'); bp_set_member_type($u1, 'bar', true); $this->assertTrue(bp_has_member_type($u1, 'foo')); $types = bp_get_member_type($u1, false); $this->assertEqualSets(array('bar', 'foo'), $types); }
/** * @group bp_has_members * @ticket BP6286 */ public function test_bp_has_members_should_infer_member_type_from_get_param_comma_sep() { bp_register_member_type('foo'); bp_register_member_type('bar'); bp_register_member_type('baz'); $users = $this->factory->user->create_many(4); bp_set_member_type($users[0], 'foo'); bp_set_member_type($users[1], 'bar'); bp_set_member_type($users[2], 'baz'); global $members_template; $old_members_template = $members_template; $old_get = $_GET; $_GET['member_type'] = 'foo,bar'; bp_has_members(); $members = is_array($members_template->members) ? array_values($members_template->members) : array(); $member_ids = wp_list_pluck($members, 'ID'); $this->assertEqualSets(array($users[0], $users[1]), $member_ids); $GLOBALS['members_template'] = $old_members_template; }
/** * Update the member type of a user when member type field is updated * * @param type $data_field * @return type */ public function update_member_type($data_field) { $field = xprofile_get_field($data_field->field_id); //we only need to worry about member type field if ($field->type != 'membertype') { return; } $user_id = $data_field->user_id; $member_type = maybe_unserialize($data_field->value); //validate too? if (empty($member_type)) { //remove all member type? bp_set_member_type($user_id, ''); return; } //should we validate member type here? I don't think as only validated data will be passed here bp_set_member_type($user_id, $member_type); }
public function test_bp_get_member_type_should_not_return_unregistered_types() { $u1 = $this->factory->user->create(); bp_register_member_type('foo'); bp_set_member_type($u1, 'foo'); // Directly set a type that hasn't been registered. bp_set_object_terms($u1, 'ugh', bp_get_member_type_tax_name(), true); $type = bp_get_member_type($u1, false); $this->assertEquals(array('foo'), $type); }
/** * Update member type on multisite * * @param type $user_id * @param type $key * @param type $user */ public function bmt_member_type_on_registration_multisite($user_id, $key, $user) { //Set default member type if user has not selected any $bmt_member_type = !empty($user['meta']['bmt_member_type']) ? $user['meta']['bmt_member_type'] : buddyboss_bmt()->option('default_member_type'); if (!empty($bmt_member_type)) { if (!empty($user_id)) { bp_set_member_type($user_id, $bmt_member_type); } } }