-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathclass.clef-invite.php
More file actions
82 lines (71 loc) · 2.71 KB
/
Copy pathclass.clef-invite.php
File metadata and controls
82 lines (71 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class ClefInvite {
public $code;
public $created_at;
private $user_email;
function __construct($user, $is_network_admin=false) {
$this->code = md5(uniqid(mt_rand(), true));
$this->user_email = $user->user_email;
$this->user_id = $user->ID;
$this->created_at = time();
$sites = get_blogs_of_user($user->ID);
if ($is_network_admin || count($sites) == 0) {
$site = array_shift($sites);
switch_to_blog($site->userblog_id);
$this->site_name = get_bloginfo('name');
$this->login_url = wp_login_url();
restore_current_blog();
} else {
$this->site_name = get_bloginfo('name');
$this->login_url = wp_login_url();
}
}
function persist() {
update_user_meta($this->user_id, 'clef_invite_code', $this);
}
function get_link() {
return ($this->login_url .
'?clef_invite_code=' . $this->code .
'&clef_invite_id=' . urlencode(base64_encode($this->user_email)));
}
function send_email() {
if (empty($this->user_email)) return true;
$subject = '['. $this->site_name . '] ' . __('Set up Clef for your account', "wpclef");
$template = 'invite_email.tpl';
$vars = array(
"invite_link" => $this->get_link(),
"site_name" => $this->site_name
);
return ClefUtils::send_email(
$this->user_email,
$subject,
$template,
$vars
);
}
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 = sprintf( __("unable to send emails to the following users: %s.", 'wpclef'), 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;
}
}
}
?>