class-affiliates-user-registration.php
4.24 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* class-affiliates-user-registration.php
*
* Copyright (c) 2010 - 2014 "kento" Karim Rahimpur www.itthinx.com
*
* This code is released under the GNU General Public License.
* 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. See the
* GNU General Public License for more details.
*
* This header and all notices must be kept intact.
*
* @author Karim Rahimpur
* @package affiliates
* @since 2.7.0
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* User registration integration.
*/
class Affiliates_User_Registration {
const REFERRAL_TYPE = 'user';
/**
* Hooks on user_register to record a referral when new users register.
* (only when the built-in User Registration integration is enabled).
*/
public static function init() {
if ( get_option( 'aff_user_registration_enabled', 'no' ) == 'yes' ) {
add_action( 'user_register', array( __CLASS__, 'user_register' ) );
}
if ( get_option( 'aff_customer_registration_enabled', 'no' ) == 'yes' ) {
add_action( 'woocommerce_created_customer', array( __CLASS__, 'woocommerce_created_customer' ), 10, 3 );
}
}
/**
* Hooks on customer creation to record a referral when a new customer is created.
*
* @param int $customer_id
* @param array $new_customer_data
* @param boolean $password_generated
*/
public static function woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
self::user_register( $customer_id, array( 'force' => true, 'type' => 'customer' ) );
}
/**
* Record a referral when a new user has been referred by an affiliate.
*
* @param int $user_id
* @param array $params registration parameters
*/
public static function user_register( $user_id, $params = array() ) {
extract( $params );
if ( !isset( $force ) ) {
$force = false;
}
if ( !isset( $type ) ) {
$type = null;
}
if ( !$force && is_admin() ) {
if ( !apply_filters( 'affiliates_user_registration_on_admin', false ) ) {
return;
}
}
if ( $user = get_user_by( 'id', $user_id ) ) {
$post_id = null;
if ( $post = get_post() ) {
$post_id = $post->ID;
}
switch ( $type ) {
case 'customer' :
$description = sprintf( 'Customer Registration %s', esc_html( $user->user_login ) );
break;
default :
$description = sprintf( 'User Registration %s', esc_html( $user->user_login ) );
}
$base_amount = null;
if ( AFFILIATES_PLUGIN_NAME != 'affiliates' ) {
$base_amount = get_option( 'aff_user_registration_base_amount', null );
}
$amount = null;
if ( empty( $base_amount ) ) {
$amount = get_option( 'aff_user_registration_amount', '0' );
}
$currency = get_option( 'aff_user_registration_currency', Affiliates::DEFAULT_CURRENCY );
$user_registration_referral_status = get_option(
'aff_user_registration_referral_status',
get_option( 'aff_default_referral_status', AFFILIATES_REFERRAL_STATUS_ACCEPTED )
);
$data = array(
'user_login' => array(
'title' => 'Username',
'domain' => AFFILIATES_PLUGIN_DOMAIN,
'value' => $user->user_login,
),
'user_email' => array(
'title' => 'Email',
'domain' => AFFILIATES_PLUGIN_DOMAIN,
'value' => $user->user_email,
),
'first_name' => array(
'title' => 'First Name',
'domain' => AFFILIATES_PLUGIN_DOMAIN,
'value' => $user->first_name,
),
'last_name' => array(
'title' => 'Last Name',
'domain' => AFFILIATES_PLUGIN_DOMAIN,
'value' => $user->last_name,
),
'base_amount' => array(
'title' => 'Base Amount',
'domain' => AFFILIATES_PLUGIN_DOMAIN,
'value' => $base_amount
)
);
if ( class_exists( 'Affiliates_Referral_WordPress' ) ) {
$r = new Affiliates_Referral_WordPress();
$affiliate_id = $r->evaluate( $post_id, $description, $data, $base_amount, $amount, $currency, $user_registration_referral_status, self::REFERRAL_TYPE );
} else {
$affiliate_id = affiliates_suggest_referral( $post_id, $description, $data, $amount, $currency, $user_registration_referral_status, self::REFERRAL_TYPE );
}
}
}
}
Affiliates_User_Registration::init();