class-affiliates-users.php
3.55 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* class-affiliates-users-registered.php
*
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com
*
* This code is provided subject to the license granted.
* Unauthorized use and distribution is prohibited.
* See COPYRIGHT.txt and LICENSE.txt
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates-users
* @since affiliates-users 1.0.0
*/
/**
* Auto-add affiliates for new users.
*/
class Affiliates_Users {
/**
* Initialize hooks that handle addition and removal of users and blogs.
*/
public static function init() {
add_action( 'affiliates_before_register_affiliate', array( __CLASS__, 'affiliates_before_register_affiliate' ) );
add_action( 'user_register', array( __CLASS__, 'user_register' ) );
add_action( 'add_user_to_blog', array( __CLASS__, 'add_user_to_blog' ), 10, 3 );
}
/**
* Set flag.
* @param array $userdata
*/
public static function affiliates_before_register_affiliate( $userdata ) {
global $affiliates_users_skip;
$affiliates_users_skip = true;
}
/**
* Create an affiliate account for the user if it doesn't have one.
* @param WP_User $user
* @return new affiliate's ID or false
*/
public static function maybe_create_affiliate( $user ) {
global $affiliates_users_skip;
if ( isset( $affiliates_users_skip ) && $affiliates_users_skip ) {
return;
}
if ( function_exists( 'affiliates_get_user_affiliate' ) ) {
$affiliates = affiliates_get_user_affiliate( $user->ID );
if ( empty( $affiliates ) ) {
$first_name = get_user_meta( $user->ID, 'first_name', true );
$last_name = get_user_meta( $user->ID, 'last_name', true );
if ( empty( $first_name ) && empty( $last_name ) ) {
if ( !empty( $user->display_name ) ) {
$first_name = $user->display_name;
} else {
$first_name = $user->user_login;
}
}
$userdata = array(
'first_name' => $first_name,
'last_name' => $last_name,
'user_email' => $user->user_email,
);
return Affiliates_Registration::store_affiliate( $user->ID, $userdata );
}
}
return false;
}
/**
* Creates affiliate accounts for users who don't have one.
* @return array of affiliate IDs for newly created affiliate accounts
*/
public static function all() {
$affiliate_ids = array();
$users = get_users();
foreach( $users as $user ) {
$affiliate_id = self::maybe_create_affiliate( $user );
if ( $affiliate_id ) {
$affiliate_ids[] = $affiliate_id;
}
}
return $affiliate_ids;
}
/**
* Add affiliate account for new user.
* @param int $user_id
*/
public static function user_register( $user_id ) {
// normal site or if multisite must belong to blog
if ( !is_multisite() || is_user_member_of_blog( $user_id ) ) {
if ( $user = get_user_by( 'id', $user_id ) ) {
self::maybe_create_affiliate( $user );
}
}
}
/**
* Add affiliate account for user on blog.
* @param int $user_id
* @param WP_string $role
*/
function add_user_to_blog( $user_id, $role, $blog_id ) {
if ( is_multisite() ) {
if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
wp_cache_switch_to_blog( $blog_id );
} else {
switch_to_blog( $blog_id );
wp_cache_reset();
}
}
if ( $user = get_user_by( 'id', $user_id ) ) {
self::maybe_create_affiliate( $user );
}
if ( is_multisite() ) {
restore_current_blog();
}
}
}
Affiliates_Users::init();