-
Notifications
You must be signed in to change notification settings - Fork 15
/
class-participad-user.php
107 lines (90 loc) · 2.89 KB
/
class-participad-user.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* User
*
* @since 1.0
*/
class Participad_User {
var $wp_user_id;
var $ep_user_id;
var $ep_session_id;
public function __construct( $args = array() ) {
$defaults = array(
'wp_user_id' => 0,
'ep_user_id' => 0,
);
$r = wp_parse_args( $args, $defaults );
// WP user id always takes precedence
if ( $r['wp_user_id'] ) {
$this->wp_user_id = $r['wp_user_id'];
$this->setup_userdata_from_wp_user_id();
} else if ( $r['ep_user_id'] ) {
$this->ep_user_id = $r['ep_user_id'];
$this->setup_userdata_from_ep_user_id();
}
}
/**
* Given WP user id, set up the user
*
* We store this locally for efficiency's sake. When not found, query
* the EP instance.
*
* @todo Need to create the function that goes in the other direction
*/
protected function setup_userdata_from_wp_user_id() {
$this->ep_user_id = get_user_meta( $this->wp_user_id, 'ep_user_id', true );
if ( ! $this->ep_user_id ) {
$this->ep_user_id = self::create_ep_user( $this->wp_user_id );
}
}
/**
* Create a session between this user and a given EP group
*
* @param int $wp_post_id This is used for setting the session key
* @param string $ep_group_id
*/
public function create_session( $wp_post_id, $ep_group_id ) {
// Sessions are user-post specific
$session_key = 'ep_group_session_id-post_' . $wp_post_id;
$this->ep_session_id = get_user_meta( $this->wp_user_id, $session_key, true );
if ( empty( $this->ep_session_id ) ) {
$ep_session_id = Participad_Post::create_ep_group_session( $ep_group_id, $this->ep_user_id );
if ( ! is_wp_error( $ep_session_id ) ) {
$this->ep_session_id = $ep_session_id;
update_user_meta( $this->wp_user_id, $session_key, $this->ep_session_id );
}
}
if ( ! empty( $this->ep_session_id ) ) {
// @todo Better expiration?
// It's not possible to set cross-domain cookies. But
// this filter will let you adjust for EP on a separate
// subdomain
$cookie_domain = apply_filters( 'participad_cookie_domain', '' );
setcookie( "sessionID", $this->ep_session_id, time() + ( 60*60*24*365*100 ), '/', $cookie_domain );
}
}
/////////////////////
// STATIC METHODS //
/////////////////////
/**
* Given a WP user ID, create a new EP user
*
* @param int $wp_user_id
* @return string $ep_user_id
*/
public static function create_ep_user( $wp_user_id ) {
$ep_user_id = '';
// Use display_name for the WP user
$wp_user = new WP_User( $wp_user_id );
if ( is_a( $wp_user, 'WP_User' ) ) {
try {
$ep_user = participad_client()->createAuthorIfNotExistsFor( $wp_user->ID, $wp_user->display_name );
$ep_user_id = $ep_user->authorID;
update_user_meta( $wp_user_id, 'ep_user_id', $ep_user_id );
return $ep_user_id;
} catch ( Exception $e ) {
return new WP_Error( 'create_ep_user', __( 'Could not create the Etherpad Lite user.', 'participad' ) );
}
}
}
}