public static function invite_users($users, $is_network_admin) { $errors = array(); foreach ($users as &$user) { if (!ClefUtils::user_has_clef($user)) { $invite = new ClefInvite($user, $is_network_admin); $invite->persist(); $success = $invite->send_email(); if (!$success) { $errors[] = $user->user_email; } } } if (count($errors) > 0) { if (count($errors) == count($filtered_users)) { $message = __("there was an error sending the invite email to all users. Copy and paste the preview email to your users and they'll be walked through a tutorial to connect with Clef", 'wpclef'); } else { $message = __("unable to send emails to the following users: ", 'wpclef'); $message .= join(", ", $errors); $message .= __(". Copy and paste the preview email to your users and they'll be walked through a tutorial to connect with Clef", 'wpclef'); } throw new Exception($message); } else { return true; } }
public function ajax_invite_users() { $role = strtolower(ClefUtils::isset_POST('roles')); $is_network_admin = filter_var(ClefUtils::isset_POST('networkAdmin'), FILTER_VALIDATE_BOOLEAN); if (!$role) { return new WP_Error('invalid_role', __('invalid role', 'wpclef')); } $opts = array('exclude' => array(get_current_user_id()), 'meta_query' => array(array('key' => 'clef_id', 'value' => '', 'compare' => 'NOT EXISTS'))); # if we are on the network admin page, don't filter users by # blog ID. if ($is_network_admin) { $opts['blog_id'] = false; } $other_users = get_users($opts); $filtered_users = $this->filter_users_by_role($other_users, $role); if (empty($filtered_users)) { return new WP_Error('no_users', __("there are no other users without Clef with this role or greater", "wpclef")); } $errors = array(); foreach ($filtered_users as &$user) { $invite = new ClefInvite($user, $is_network_admin); $invite->persist(); $success = $invite->send_email($from_email); if (!$success) { $errors[] = $user->user_email; } } if (count($errors) > 0) { if (count($errors) == count($filtered_users)) { $message = __("there was an error sending the invite email to all users. Copy and paste the preview email to your users and they'll be walked through a tutorial to connect with Clef", 'wpclef'); } else { $message = __("unable to send emails to the following users: ", 'wpclef'); $message .= join(", ", $errors); $message .= __(". Copy and paste the preview email to your users and they'll be walked through a tutorial to connect with Clef", 'wpclef'); } return new WP_Error('clef_mail_error', $message); } else { return array("success" => true); } }