class-affiliates-exclusion.php
6.11 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* class-affiliates-exclusion.php
*
* Copyright (c) 2010 - 2015 "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 affiliates 2.8.0
*/
class Affiliates_Exclusion {
private static $ap_priority = false;
public static function init() {
if ( get_option( 'aff_allow_auto', 'no' ) == 'no' ) {
add_filter( 'affiliates_service_affiliate_id', array( __CLASS__, 'service' ), 999, 2 );
// add_filter( 'affiliates_coupon_affiliate_id', array( __CLASS__, 'coupon' ), 999, 2 );
}
if ( get_option( 'aff_allow_auto_coupons', 'no' ) == 'no' ) {
add_filter( 'woocommerce_coupon_is_valid', array( __CLASS__, 'woocommerce_coupon_is_valid' ), 999, 2 );
add_action( 'woocommerce_after_checkout_validation', array( __CLASS__, 'woocommerce_after_checkout_validation' ), 999 );
}
}
/**
* Service hook.
* @param int $affiliate_id
* @param string $service
* @return int affiliate id or null
*/
public static function service( $affiliate_id, $service = null ) {
return self::validate( $affiliate_id );
}
/**
* Coupon hook.
* @param int $affiliate_id
* @param string $coupon
* @return int affiliate id or null
*/
public static function coupon( $affiliate_id, $coupon ) {
return self::validate( $affiliate_id );
}
/**
* Returns null if the user of the affiliate is the same as the current user.
* @param int $affiliate_id
* @return int affiliate id or null
*/
public static function validate( $affiliate_id ) {
$result = $affiliate_id;
if ( $affiliate_id !== null ) {
if ( $affiliate = affiliates_get_affiliate( $affiliate_id ) ) {
if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
if ( (int) $user_id === (int) get_current_user_id() ) {
$result = null;
} else {
if ( !empty( $affiliate['email'] ) ) {
if ( $user = get_user_by( 'email', $affiliate['email'] ) ) {
if ( (int) $user->ID === (int) get_current_user_id() ) {
$result = null;
}
}
}
}
}
}
}
return $result;
}
/**
* Invalidate affiliate's own coupons.
*
* @param boolean $valid
* @param WC_Coupons $coupon
* @return boolean
*/
public static function woocommerce_coupon_is_valid( $valid, $coupon ) {
global $woocommerce;
// Only perform checks if the coupon is valid at this stage.
if ( $valid && !empty( $coupon ) && !empty( $coupon->id ) && !empty( $coupon->code ) ) {
if ( method_exists( 'Affiliates_Attributes_WordPress', 'get_affiliate_for_coupon' ) ) {
self::remove_filters();
if ( $affiliate_id = Affiliates_Attributes_WordPress::get_affiliate_for_coupon( $coupon->code ) ) {
if ( $user_id = get_current_user_id() ) {
if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
if ( in_array( $affiliate_id, $affiliate_ids ) ) {
$valid = false;
}
}
}
}
self::add_filters();
}
}
return $valid;
}
/**
* Performs coupon checks after checkout validation.
*
* @param array $posted posted form data
*/
public static function woocommerce_after_checkout_validation( $posted ) {
global $woocommerce;
if ( isset( $woocommerce->cart ) ) {
$cart = $woocommerce->cart;
if ( ! empty( $cart->applied_coupons ) ) {
if ( method_exists( 'Affiliates_Attributes_WordPress', 'get_affiliate_for_coupon' ) ) {
$valid = true;
$emails = array( $posted['billing_email'] );
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$emails[] = $current_user->user_email;
}
$emails = array_map( 'sanitize_email', array_map( 'strtolower', $emails ) );
self::remove_filters();
foreach ( $cart->applied_coupons as $key => $code ) {
$coupon = new WC_Coupon( $code );
if ( ! is_wp_error( $coupon->is_valid() ) ) {
if ( $affiliate_id = Affiliates_Attributes_WordPress::get_affiliate_for_coupon( $coupon->code ) ) {
if ( $user_id = get_current_user_id() ) {
if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
if ( in_array( $affiliate_id, $affiliate_ids ) ) {
$valid = false;
break;
}
}
}
if ( $affiliate = affiliates_get_affiliate( $affiliate_id ) ) {
if ( isset( $affiliate['email'] ) && in_array( strtolower( $affiliate['email'] ), $emails ) ) {
$valid = false;
break;
}
}
}
}
}
self::add_filters();
if ( !$valid ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_INVALID_REMOVED );
unset( $cart->applied_coupons[ $key ] );
$woocommerce->session->coupon_codes = $cart->applied_coupons;
$woocommerce->session->refresh_totals = true;
}
}
}
}
}
/**
* These filters must be removed so we can get the affiliate id without
* their methods interfering when using get_affiliate_for_coupon().
*/
private static function remove_filters() {
self::$ap_priority = has_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ) );
if ( self::$ap_priority !== false ) {
remove_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ), self::$ap_priority, 2 );
}
remove_filter( 'affiliates_coupon_affiliate_id', array( __CLASS__, 'coupon' ), 999 );
}
/**
* Add the filters back.
*/
private static function add_filters() {
if ( self::$ap_priority !== false ) {
add_filter( 'affiliates_coupon_affiliate_id', array( 'Affiliates_Permanent', 'affiliates_coupon_affiliate_id' ), self::$ap_priority, 2 );
}
self::$ap_priority = false;
add_filter( 'affiliates_coupon_affiliate_id', array( __CLASS__, 'coupon' ), 999, 2 );
}
}
Affiliates_Exclusion::init();