/** * Handle bulk editing of users * * @since 1.0.0 */ public function handle_bulk_actions($redirect_to = '', $action = '', $user_ids = array()) { // Get terms $terms = get_terms($this->taxonomy, array('hide_empty' => false)); // Bail if no users or terms to work with if (empty($user_ids) || empty($terms)) { return $redirect_to; } // New actions array $actions = array(); // Compile available actions foreach ($terms as $term) { $key = "{$term->slug}-{$this->taxonomy}"; $actions[] = "add-{$key}"; $actions[] = "remove-{$key}"; } // Bail if not a supported bulk action if (!in_array($action, $actions, true)) { return $redirect_to; } // Type & term $type = strstr($action, '-', true); $term = str_replace("{$type}-", '', $action); $term = str_replace("-{$this->taxonomy}", '', $term); // Loop through users foreach ($user_ids as $user) { // Skip if current user cannot edit this user if (!current_user_can('edit_user', $user)) { continue; } // Get term slugs of user for this taxonomy $terms = wp_get_terms_for_user($user, $this->taxonomy); $update_terms = wp_list_pluck($terms, 'slug'); // Adding if ('add' === $type) { if (!in_array($term, $update_terms)) { $update_terms[] = $term; } // Removing } elseif ('remove' === $type) { $index = array_search($term, $update_terms); if (false !== $index && isset($update_terms[$index])) { unset($update_terms[$index]); } } // Delete all groups if they're empty if (empty($update_terms)) { $update_terms = null; } // Update terms for users if ($update_terms !== $terms) { wp_set_terms_for_user($user, $this->taxonomy, $update_terms, true); } } // Add count to redirection if (!empty($update_terms)) { $redirect_to = add_query_arg(array('user_groups_count' => count($user_ids), 'action_type' => $type), $redirect_to); } // Return redirection return $redirect_to; }
/** * Handle bulk editing of users * * @since 0.1.0 */ public function bulk_edit_action() { // Bail if not a bulk edit request if (!isset($_REQUEST[$this->taxonomy . '-submit']) || empty($_POST[$this->taxonomy])) { return; } check_admin_referer("bulk-edit-{$this->taxonomy}"); // Get an array of users from the string parse_str(urldecode($_POST[$this->taxonomy . '-users']), $users); // Bail if no users to edit if (empty($users['users'])) { return; } $users = $users['users']; $action = strstr($_POST[$this->taxonomy], '-', true); $term = str_replace($action, '', $_POST[$this->taxonomy]); // Loop through users foreach ($users as $user) { // Skip if current user cannot edit this user if (!current_user_can('edit_user', $user)) { continue; } // Get term slugs of user for this taxonomy $terms = wp_get_terms_for_user($user, $this->taxonomy); $update_terms = wp_list_pluck($terms, 'slug'); // Adding if ('add' === $action) { if (!in_array($term, $update_terms)) { $update_terms[] = $term; } // Removing } elseif ('remove' === $action) { $index = array_search($term, $update_terms); if (isset($update_terms[$index])) { unset($update_terms[$index]); } } // Delete all groups if they're empty if (empty($update_terms)) { $update_terms = null; } // Update terms for users if ($update_terms !== $terms) { wp_set_terms_for_user($user, $this->taxonomy, $update_terms, true); } } // Success wp_safe_redirect(admin_url('users.php')); die; }