public function setUp() { parent::setUp(); $this->old_current_user = get_current_user_id(); $this->set_current_user($this->factory->user->create(array('role' => 'administrator'))); $this->keymaster_id = get_current_user_id(); bbp_set_user_role($this->keymaster_id, bbp_get_keymaster_role()); }
/** * @covers ::bbp_move_topic_handler */ public function test_bbp_move_topic_handler() { $old_current_user = 0; $this->old_current_user = get_current_user_id(); $this->set_current_user($this->factory->user->create(array('role' => 'administrator'))); $this->keymaster_id = get_current_user_id(); bbp_set_user_role($this->keymaster_id, bbp_get_keymaster_role()); $old_forum_id = $this->factory->forum->create(); $topic_id = $this->factory->topic->create(array('post_parent' => $old_forum_id, 'topic_meta' => array('forum_id' => $old_forum_id))); $reply_id = $this->factory->reply->create(array('post_parent' => $topic_id, 'reply_meta' => array('forum_id' => $old_forum_id, 'topic_id' => $topic_id))); // Topic post parent $topic_parent = wp_get_post_parent_id($topic_id); $this->assertSame($old_forum_id, $topic_parent); // Forum meta $this->assertSame(1, bbp_get_forum_topic_count($old_forum_id, true, true)); $this->assertSame(1, bbp_get_forum_reply_count($old_forum_id, true, true)); $this->assertSame($topic_id, bbp_get_forum_last_topic_id($old_forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_reply_id($old_forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_active_id($old_forum_id)); // Topic meta $this->assertSame($old_forum_id, bbp_get_topic_forum_id($topic_id)); $this->assertSame(1, bbp_get_topic_voice_count($topic_id, true)); $this->assertSame(1, bbp_get_topic_reply_count($topic_id, true)); $this->assertSame($reply_id, bbp_get_topic_last_reply_id($topic_id)); $this->assertSame($reply_id, bbp_get_topic_last_active_id($topic_id)); // Reply Meta $this->assertSame($old_forum_id, bbp_get_reply_forum_id($reply_id)); $this->assertSame($topic_id, bbp_get_reply_topic_id($reply_id)); // Create a new forum $new_forum_id = $this->factory->forum->create(); // Move the topic into the new forum bbp_move_topic_handler($topic_id, $old_forum_id, $new_forum_id); // Topic post parent $topic_parent = wp_get_post_parent_id($topic_id); $this->assertSame($new_forum_id, $topic_parent); // Forum meta $this->assertSame(1, bbp_get_forum_topic_count($new_forum_id, true, true)); $this->assertSame(1, bbp_get_forum_reply_count($new_forum_id, true, true)); $this->assertSame($topic_id, bbp_get_forum_last_topic_id($new_forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_reply_id($new_forum_id)); $this->assertSame($reply_id, bbp_get_forum_last_active_id($new_forum_id)); // Topic meta $this->assertSame($new_forum_id, bbp_get_topic_forum_id($topic_id)); $this->assertSame(1, bbp_get_topic_voice_count($topic_id, true)); $this->assertSame(1, bbp_get_topic_reply_count($topic_id, true)); $this->assertSame($reply_id, bbp_get_topic_last_reply_id($topic_id)); $this->assertSame($reply_id, bbp_get_topic_last_active_id($topic_id)); // Reply Meta $this->assertSame($new_forum_id, bbp_get_reply_forum_id($reply_id)); $this->assertSame($topic_id, bbp_get_reply_topic_id($reply_id)); // Old Topic/Reply Counts $this->assertSame(0, bbp_get_forum_topic_count($old_forum_id, true, true)); $this->assertSame(0, bbp_get_forum_reply_count($old_forum_id, true, true)); // Retore the user $this->set_current_user($this->old_current_user); }
public function setUp() { parent::setUp(); $this->old_current_user = get_current_user_id(); $this->set_current_user($this->factory->user->create(array('role' => 'administrator'))); $this->keymaster_id = get_current_user_id(); bbp_set_user_role($this->keymaster_id, bbp_get_keymaster_role()); if (!function_exists('bbp_admin_repair')) { require_once BBP_PLUGIN_DIR . 'includes/admin/tools.php'; } }
public function addUser($userID, $product) { $wpdb = $this->db->wpdb(); $wpdb->insert($this->db->getDbName(), array('order_id' => $product['order_id'], 'item_id' => $product['id'], 'user_id' => $userID, 'purchased_at' => date('Y-m-d H:i:s', strtotime($product['purchased_at'])), 'product_name' => $product['name'], 'market_name' => $product['market_name']), array('%s', '%s', '%d', '%s', '%s')); if (function_exists('bbp_set_user_role')) { bbp_set_user_role($userID, 'participant'); } $items = array($product['id']); // add a hook to allow adding extra user meta (or whatever) do_action('marketcheck/registered_user', $userID, $product, $wpdb->insert_id); update_user_meta($userID, 'marketcheck_purchased_items', $items); }
/** * This repair tool will map each user of the current site to their respective * forums role. By default, Admins will be Key Masters, and every other role * will be the default role defined in Settings > Forums (Participant). * * @since 2.2.0 bbPress (r4340) * * @uses bbp_get_user_role_map() To get the map of user roles * @uses bbp_get_default_role() To get the default bbPress user role * @uses bbp_get_blog_roles() To get the current WordPress roles * @uses get_users() To get the users of each role (limited to ID field) * @uses bbp_set_user_role() To set each user's forums role */ function bbp_admin_repair_user_roles() { $statement = __('Remapping forum role for each user on this site… %s', 'bbpress'); $changed = 0; $role_map = bbp_get_user_role_map(); $default_role = bbp_get_default_role(); // Bail if no role map exists if (empty($role_map)) { return array(1, sprintf($statement, __('Failed!', 'bbpress'))); } // Iterate through each role... foreach (array_keys(bbp_get_blog_roles()) as $role) { // Reset the offset $offset = 0; // If no role map exists, give the default forum role (bbp-participant) $new_role = isset($role_map[$role]) ? $role_map[$role] : $default_role; // Get users of this site, limited to 1000 while ($users = get_users(array('role' => $role, 'fields' => 'ID', 'number' => 1000, 'offset' => $offset))) { // Iterate through each user of $role and try to set it foreach ((array) $users as $user_id) { if (bbp_set_user_role($user_id, $new_role)) { ++$changed; // Keep a count to display at the end } } // Bump the offset for the next query iteration $offset = $offset + 1000; } } $result = sprintf(__('Complete! %s users updated.', 'bbpress'), bbp_number_format($changed)); return array(0, sprintf($statement, $result)); }
/** * Process bulk dropdown form submission from the WordPress Users * Table * * @uses current_user_can() to check for 'promote users' capability * @uses bbp_get_dynamic_roles() to get forum roles * @uses bbp_get_user_role() to get a user's current forums role * @uses bbp_set_user_role() to set the user's new forums role * @return bool Always false */ public function user_role_bulk_change() { // Bail if no users specified if (empty($_REQUEST['users'])) { return; } // Bail if this isn't a bbPress action if (empty($_REQUEST['bbp-new-role']) || empty($_REQUEST['bbp-change-role'])) { return; } // Check that the new role exists $dynamic_roles = bbp_get_dynamic_roles(); if (empty($dynamic_roles[$_REQUEST['bbp-new-role']])) { return; } // Bail if nonce check fails check_admin_referer('bbp-bulk-users', 'bbp-bulk-users-nonce'); // Bail if current user cannot promote users if (!current_user_can('promote_users')) { return; } // Get the current user ID $current_user_id = (int) bbp_get_current_user_id(); // Run through user ids foreach ((array) $_REQUEST['users'] as $user_id) { $user_id = (int) $user_id; // Don't let a user change their own role if ($user_id === $current_user_id) { continue; } // Set up user and role data $user_role = bbp_get_user_role($user_id); $new_role = sanitize_text_field($_REQUEST['bbp-new-role']); // Only keymasters can set other keymasters if (in_array(bbp_get_keymaster_role(), array($user_role, $new_role)) && !bbp_is_user_keymaster()) { continue; } // Set the new forums role if ($new_role !== $user_role) { bbp_set_user_role($user_id, $new_role); } } }
/** * @covers ::bbp_check_for_blacklist */ public function test_should_return_true_for_keymasterss_to_bypass_blacklist_check() { // Create a keymaster user. $old_current_user = 0; $this->old_current_user = get_current_user_id(); $this->set_current_user($this->factory->user->create(array('role' => 'subscriber'))); $this->keymaster_id = get_current_user_id(); bbp_set_user_role($this->keymaster_id, bbp_get_keymaster_role()); $t = $this->factory->topic->create(array('post_author' => bbp_get_current_user_id(), 'post_title' => 'Sting', 'post_content' => 'Beware, there maybe bees hibernating.')); $anonymous_data = false; $author_id = bbp_get_topic_author_id($t); $title = bbp_get_topic_title($t); $content = bbp_get_topic_content($t); update_option('blacklist_keys', "hibernating\nfoo"); $result = bbp_check_for_blacklist($anonymous_data, $author_id, $title, $content); $this->assertTrue($result); // Retore the original user. $this->set_current_user($this->old_current_user); }
/** * Helper function hooked to 'bbp_profile_update' action to save or * update user roles and capabilities. * * @since bbPress (r4235) * * @param int $user_id * @uses bbp_reset_user_caps() to reset caps * @usse bbp_save_user_caps() to save caps */ function bbp_profile_update_role($user_id = 0) { // Bail if no user ID was passed if (empty($user_id)) { return; } // Bail if no role if (!isset($_POST['bbp-forums-role'])) { return; } // Fromus role we want the user to have $new_role = sanitize_text_field($_POST['bbp-forums-role']); $forums_role = bbp_get_user_role($user_id); // Bail if no role change if ($new_role === $forums_role) { return; } // Bail if trying to set their own role if (bbp_is_user_home_edit()) { return; } // Bail if current user cannot promote the passing user if (!current_user_can('promote_user', $user_id)) { return; } // Set the new forums role bbp_set_user_role($user_id, $new_role); }
/** * Hooked to the 'bbp_activate' action, this helper function automatically makes * the current user a Key Master in the forums if they just activated bbPress, * regardless of the bbp_allow_global_access() setting. * * @since bbPress (r4910) * * @internal Used to internally make the current user a keymaster on activation * * @uses current_user_can() to bail if user cannot activate plugins * @uses get_current_user_id() to get the current user ID * @uses get_current_blog_id() to get the current blog ID * @uses is_user_member_of_blog() to bail if the current user does not have a role * @uses bbp_is_user_keymaster() to bail if the user is already a keymaster * @uses bbp_set_user_role() to make the current user a keymaster * * @return If user can't activate plugins or is already a keymaster */ function bbp_make_current_user_keymaster() { // Bail if the current user can't activate plugins since previous pageload if (!current_user_can('activate_plugins')) { return; } // Get the current user ID $user_id = get_current_user_id(); $blog_id = get_current_blog_id(); // Bail if user is not actually a member of this site if (!is_user_member_of_blog($user_id, $blog_id)) { return; } // Bail if the current user already has a forum role to prevent // unexpected role and capability escalation. if (bbp_get_user_role($user_id)) { return; } // Make the current user a keymaster bbp_set_user_role($user_id, bbp_get_keymaster_role()); // Reload the current user so caps apply immediately wp_get_current_user(); }
function etheme_bb_user_role() { if (!function_exists('bbp_is_deactivation')) { return; } // Bail if deactivating bbPress if (bbp_is_deactivation()) { return; } // Catch all, to prevent premature user initialization if (!did_action('set_current_user')) { return; } // Bail if not logged in or already a member of this site if (!is_user_logged_in()) { return; } // Get the current user ID $user_id = get_current_user_id(); // Bail if user already has a forums role if (bbp_get_user_role($user_id)) { return; } // Bail if user is marked as spam or is deleted if (bbp_is_user_inactive($user_id)) { return; } /** Ready *****************************************************************/ // Load up bbPress once $bbp = bbpress(); // Get whether or not to add a role to the user account $add_to_site = bbp_allow_global_access(); // Get the current user's WordPress role. Set to empty string if none found. $user_role = bbp_get_user_blog_role($user_id); // Get the role map $role_map = bbp_get_user_role_map(); /** Forum Role ************************************************************/ // Use a mapped role if (isset($role_map[$user_role])) { $new_role = $role_map[$user_role]; // Use the default role } else { $new_role = bbp_get_default_role(); } /** Add or Map ************************************************************/ // Add the user to the site if (true === $add_to_site) { // Make sure bbPress roles are added bbp_add_forums_roles(); $bbp->current_user->add_role($new_role); // Don't add the user, but still give them the correct caps dynamically } else { $bbp->current_user->caps[$new_role] = true; $bbp->current_user->get_role_caps(); } $new_role = bbp_get_default_role(); bbp_set_user_role($user_id, $new_role); }
public static function import_user($userdata, $dry_run) { global $wpdb; $report = ''; $map = array("user_login" => "email", "first_name" => "first_name", "last_name" => "surname", "user_email" => "email", "user_registered" => "joined", "user_url" => "personal_web_page"); $args = array(); foreach ($map as $wp_key => $old_key) { if ($old_key == 'joined') { $args[$wp_key] = date('Y-m-d H:i:s', $userdata[$old_key]); } elseif ($wp_key == "user_login") { $args[$wp_key] = substr(str_replace("'", '', $userdata[$old_key]), 0, 60); } else { $args[$wp_key] = $userdata[$old_key]; } } $args["user_pass"] = NULL; $args["display_name"] = $userdata['first_name'] . " " . $userdata['surname']; $args["nickname"] = $args["display_name"]; $args["user_nicename"] = preg_replace('/[^a-z]/', '', strtolower($args["display_name"])); $report .= '<h3>Inserting new wordpress user:</h3><table class="widefat"><thead><tr><th>Wordpress key</th><th>Old key</th><th>Value</th></tr></thead><tbody>'; foreach ($map as $wp_key => $old_key) { $report .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $wp_key, $old_key, $args[$wp_key]); } $report .= '</tbody></table>'; if ($dry_run) { $inserted_id = 'test'; } else { $inserted_id = wp_insert_user($args); } if (!is_wp_error($inserted_id)) { $report .= '<h3>Adding profile information</h3><table class="widefat"><thead><tr><th>Wordpress key</th><th>Old key</th><th>Value</th></tr></thead><tbody>'; /* add extra data to the user */ $data_fields = array('position' => 'institutional_position', 'research_centre' => 'research_centregrouping', 'organisation' => 'institution', 'department' => 'departmentschool', 'networks' => 'membership_of_policing_related_networks', 'user_tel' => 'telephone', '_um_last_login' => 'lastlogin'); $thematic_areas = array('antisocial_behaviour' => 'Antisocial behaviour', 'cbrne' => 'CBRNE (chemical, biological, etc)', 'child_abuse_and_exploitation' => 'Child abuse and exploitation', 'community_engagement' => 'Community engagement', 'community_safety' => 'Community Safety', 'counter_terrorism' => 'Counter terrorism', 'covert_techniques' => 'Covert techniques', 'crime_analysis' => 'Crime analysis', 'crime_prevention___reduction' => 'Crime Prevention and Reduction', 'criminal_investigation' => 'Criminal investigation', 'criminal_justice' => 'Criminal justice', 'critical_incidents' => 'Critical incidents', 'cyber_crime' => 'Cyber crime', 'domestic_violence' => 'Domestic violence', 'drugs_and_alcohol' => 'Drugs and alcohol', 'equality___diversity' => 'Equality and diversity', 'firearms' => 'Firearms', 'forensics' => 'Forensics', 'fraud' => 'Fraud', 'horizon_scanning' => 'Horizon scanning', 'leadership' => 'Leadership', 'mental_health' => 'Mental health', 'missing_people' => 'Missing people', 'offenders' => 'Offenders', 'police_governance___accountability' => 'Police governance and accountability', 'police_organisational_culture' => 'Police organisational culture', 'local_policing_approaches' => 'Local Policing approaches', 'policing_partnerships' => 'Policing partnerships', 'ports___border_policing' => 'Ports and border policing', 'private_policing' => 'Private policing', 'prostitution' => 'Prostitution', 'public_order' => 'Public order', 'rural__environmental_crime' => 'Rural/environmental crime', 'serious_organised_crime' => 'Serious and organised crime', 'socio_legal_studies' => 'Socio legal studies', 'technology___information_management' => 'Technology and Information Management', 'victims___witnesses' => 'Victims and witnesses', 'violent___sexual_crime' => 'Violent and sexual crime', 'workforce_development' => 'Workforce development', 'young_people' => 'Young people'); $usermeta = array(); $usermeta['expertise'] = array(); foreach ($thematic_areas as $key => $val) { if ($userdata[$key] == "y") { $usermeta['expertise'][] = $val; } } foreach ($data_fields as $wp_key => $old_key) { if ($old_key == 'areas_of_policing_research_expertise' || $old_key == 'membership_of_policing_related_networks') { $usermeta[$wp_key] = str_replace('\\r\\n', "; ", $userdata[$old_key]); } else { $usermeta[$wp_key] = $userdata[$old_key]; } } $usermeta['role'] = 'member'; $usermeta['account_status'] = 'approved'; foreach ($usermeta as $key => $value) { if (!$dry_run) { update_user_meta($inserted_id, $key, $value); } $print_value = is_array($value) ? implode("; ", $value) : $value; $oldkey = isset($data_fields[$key]) ? $data_fields[$key] : '-'; $report .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $key, $oldkey, $print_value); } $report .= '</tbody></table>'; $result = $wpdb->update($wpdb->users, array('user_pass' => $userdata["password"]), array('ID' => $inserted_id), array('%s'), array('%d')); if (false !== $result) { $report .= '<p>Updated user password</p>'; } else { $report = '<p><strong>Password could not be set</strong></p>'; } bbp_set_user_role($inserted_id, 'bbp_participant'); $report .= '<p>Assigned additional forum role</p>'; } else { $report = '<div class="error notice"><p>Failed to insert user into Wordpress users table</p></div>'; } return $report; }
/** * Helper function hooked to 'bbp_edit_user_profile_update' action to save or * update user roles and capabilities. * * @since bbPress (r4235) * * @param int $user_id * @uses bbp_reset_user_caps() to reset caps * @usse bbp_save_user_caps() to save caps */ function bbp_profile_update_role($user_id = 0) { // Bail if no user ID was passed if (empty($user_id)) { return; } // Bail if no role if (!isset($_POST['bbp-forums-role'])) { return; } // Fromus role we want the user to have $new_role = sanitize_text_field($_POST['bbp-forums-role']); $forums_role = bbp_get_user_role($user_id); // Set the new forums role if ($new_role != $forums_role) { bbp_set_user_role($user_id, $new_role); } }