PluginProbe ʕ •ᴥ•ʔ
Post Affiliate Pro / trunk
Post Affiliate Pro vtrunk
1.29.5 1.29.4 1.29.3 1.29.0 1.29.1 1.29.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.5.0 1.5.1 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 trunk 1.0.1 1.0.10 1.0.11 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.10.1 1.10.2 1.10.3 1.10.4 1.11.1 1.11.2 1.12.1 1.12.2 1.12.3 1.12.4 1.12.5 1.13.0 1.13.1 1.13.2 1.13.3 1.13.4 1.13.5 1.13.6 1.14.0 1.14.1 1.14.2 1.15.0 1.15.1 1.15.2 1.15.3 1.15.4 1.15.5 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.17.0 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.19.0 1.19.1 1.19.10 1.19.11 1.19.12 1.19.13 1.19.14 1.19.15 1.19.16 1.19.17 1.19.2 1.19.3 1.19.4 1.19.5 1.19.6 1.19.7 1.19.8 1.19.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.28 1.2.3 1.2.31 1.2.32 1.2.33 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.20.0 1.20.1 1.21.0 1.21.1 1.21.2 1.21.3 1.21.4 1.21.5 1.21.6 1.21.7 1.22.0 1.22.1 1.22.2 1.23.0 1.23.1 1.23.2 1.23.3 1.23.4 1.23.5 1.23.6 1.23.7 1.23.8 1.24.1 1.24.2 1.24.3 1.24.4 1.24.5 1.24.6 1.24.7 1.24.8 1.24.9 1.25.0 1.26.0 1.26.1 1.26.10 1.26.11 1.26.2 1.26.3 1.26.4 1.26.5 1.26.6 1.26.7 1.26.8 1.26.9 1.27.0 1.27.1 1.27.10 1.27.11 1.27.12 1.27.13 1.27.14 1.27.15 1.27.2 1.27.3 1.27.5 1.27.6 1.27.7 1.27.8 1.27.9 1.28.0 1.28.1 1.28.2 1.28.3
postaffiliatepro / Shortcode / Affiliate.class.php
postaffiliatepro / Shortcode Last commit date
Affiliate.class.php 1 week ago Cache.class.php 1 month ago
Affiliate.class.php
281 lines
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 class Shortcode_Affiliate extends postaffiliatepro_Base {
5 const SHORTCODES_SETTINGS_PAGE_NAME = 'shortcodes-settings-page';
6 const AFFILAITE_SHORTCODE_CACHE = 'affiliate-shortcode_cache';
7 /**
8 *
9 * @var Shortcode_Cache
10 */
11 private static $cache = null;
12
13 public function __construct() {
14 if (self::$cache === null) {
15 self::$cache = new Shortcode_Cache();
16 }
17 }
18
19 public function getAffiliateShortCode($attr, $content = null) {
20 return $this->getCode($attr, $content);
21 }
22
23 public function getParentAffiliateShortCode($attr, $content = null) {
24 return $this->getCode($attr, $content, true);
25 }
26
27 /**
28 * @return Pap_Api_Affiliate
29 */
30 private function loadAffiliate(Pap_Api_Session $session, $parent = false) {
31 global $current_user;
32 $affiliate = new Pap_Api_Affiliate($session);
33 $affiliate->setRefid($current_user->user_nicename);
34 try {
35 $affiliate->load();
36 } catch (Exception $e) {
37 // try it with notification email as well
38 $this->_log('Unable to load affiliate by referral ID');
39 try {
40 $affiliate->setRefid('');
41 $affiliate->setNotificationEmail($current_user->user_email);
42 $affiliate->load();
43 } catch (Exception $e) {
44 // last try - username
45 $this->_log('Unable to load affiliate by notification email');
46 try {
47 $affiliate->setNotificationEmail('');
48 $affiliate->setUsername($current_user->user_email);
49 $affiliate->load();
50 } catch (Exception $e) {
51 $this->_log('Unable to load affiliate by username');
52 $this->_log('Loading user '.$current_user->nickname.' failed');
53 return null;
54 }
55 }
56 }
57
58 if ($parent) {
59 $parentId = $affiliate->getParentUserId();
60 if ($parentId) {
61 $parentAffiliate = new Pap_Api_Affiliate($session);
62 $parentAffiliate->setUserid($parentId);
63 $parentAffiliate->load();
64 return $parentAffiliate;
65 }
66 return null;
67 }
68 return $affiliate;
69 }
70
71 public function getCode($atts, $content = null, $parent = false) {
72 global $current_user;
73 if ($current_user->ID == 0) {
74 return;
75 }
76 $session = $this->getApiSession();
77 if ($session === null || $session === '0') {
78 $this->_log('Error getting session for login to PAP. Check WP logs for details.');
79 return;
80 }
81 $affiliate = $this->loadAffiliate($session, $parent);
82 if ($affiliate == null) {
83 $this->_log('Error getting affiliate');
84 return;
85 }
86 if (array_key_exists('item', $atts)) {
87 switch ($atts['item']) {
88 case 'name':
89 $n = $affiliate->getFirstname() . ' ' . $affiliate->getLastname();
90 return sanitize_text_field($n);
91 case 'loginurl':
92 case 'loginurl_raw':
93 if ($parent) return '';
94 $caption = 'Affiliate panel';
95 $class = '';
96 if (array_key_exists('caption', $atts)) {
97 $caption = sanitize_text_field($atts['caption']);
98 }
99 if (array_key_exists('class', $atts)) {
100 $class = sanitize_text_field($atts['class']);
101 }
102
103 wp_enqueue_script('jquery');
104 wp_add_inline_script('jquery', "jQuery(function($) {
105 $('#pap-login-url').on('click', function() {
106 $('#loader').show();
107 papGetLoginUrl();
108 });
109
110 function papGetLoginUrl() {
111 $.post(" . wp_json_encode(admin_url('admin-ajax.php')) . ", { action: 'pap_login_redirect_ajax' }, function(response) {
112 if (response.success === true) {
113 window.open(response.data.newUrl, '_parent');
114 } else {
115 console.log(response.data.error);
116 }
117 });
118 }
119 });");
120
121 if ($atts['item'] == 'loginurl') { // return button
122 $loader = '<style>#loader {
123 display: inline-block;
124 width: 1em;
125 height: 1em;
126 border: 3px solid rgba(255,255,255,.3);
127 border-radius: 50%;
128 border-top-color: #000;
129 padding-left: 1em;
130 animation: spin 1s ease infinite;
131 -webkit-animation: spin 1s ease infinite;
132 }</style>';
133 return $loader.'<input id="pap-login-url" type="submit" value="'.esc_attr($caption).'" class="'.esc_attr($class).'"><span id="loader" style="display: none"></span>';
134 } else { // return <a>
135 return '<a href="'.$this->getLoginUrl($affiliate, $session).'" target="_parent" class="'.esc_attr($class).'">'.esc_attr($caption).'</a>';
136 }
137 case 'unpaidCommissions':
138 $timeframe = '';
139 if (array_key_exists('timeframe', $atts)) {
140 $timeframe = $atts['timeframe'];
141 }
142 return $this->getAffiliateCommissionsValue($affiliate->getUserid(), $timeframe, $session);
143 }
144 return $affiliate->getField(sanitize_text_field($atts['item']));
145 }
146 }
147
148 public function loginRedirectAjaxCallback() {
149 // verify logged user again, as this is an Ajax call
150 global $current_user;
151 if ($current_user->ID == 0) {
152 wp_send_json_error(array('error' => __('No authenticated user recognized', 'postaffiliatepro')));
153 wp_die();
154 return;
155 }
156 $session = $this->getApiSession();
157 if ($session === null || $session === '0') {
158 $this->_log('Error getting session for login to PAP. Check WP logs for details.');
159 wp_send_json_error(array('error' => __('Error getting session for login to PAP.', 'postaffiliatepro')));
160 wp_die();
161 return;
162 }
163 $affiliate = $this->loadAffiliate($session);
164 if ($affiliate == null) {
165 $this->_log('Error getting affiliate');
166 wp_send_json_error(array('error' => __('Affiliate account not found in PAP.', 'postaffiliatepro')));
167 wp_die();
168 return;
169 }
170 $newUrl = $this->getLoginUrl($affiliate, $session);
171 wp_send_json_success(array('newUrl' => $newUrl));
172
173 }
174
175 private function getAffiliateCommissionsValue($affiliateId, $timeframe, $session) {
176 $request = new Gpf_Rpc_GridRequest('Pap_Merchants_Payout_PayAffiliatesGrid', 'getRows', $session);
177 $request->setLimit(0, 1);
178 $request->addParam('columns', new Gpf_Rpc_Array(array(array('userid'), array('amounttopay'))));
179 $request->addFilter('rstatus', '=', 'A');
180 $request->addFilter('payoutstatus', '=', 'U');
181 $request->addFilter('userid', '=', $affiliateId);
182
183 $dateRangeValue = $this->getDateRangeValue($timeframe);
184 if ($dateRangeValue != '') {
185 $request->addFilter('dateinserted', Gpf_Data_Filter::DATERANGE_IS, $dateRangeValue);
186 }
187 try {
188 $request->sendNow();
189 } catch(Exception $e) {
190 $this->_log('API call error: '.$e->getMessage());
191 return '0';
192 }
193
194 $grid = $request->getGrid();
195 $recordset = $grid->getRecordset();
196 if ($recordset->getSize() == 0) {
197 return '0';
198 }
199
200 foreach($recordset as $rec) {
201 return $rec->get('amounttopay');
202 }
203 }
204
205 private function getDateRangeValue($timeframe) {
206 switch (strtolower($timeframe)) {
207 case 'yesterday':
208 $dateRangeValue = Gpf_Data_Filter::RANGE_YESTERDAY;
209 break;
210 case 'week':
211 case 'lastweek':
212 case 'last week':
213 case 'w':
214 $dateRangeValue = Gpf_Data_Filter::RANGE_LAST_WEEK;
215 break;
216 case 'month':
217 case 'lastmonth':
218 case 'last month':
219 case 'm':
220 $dateRangeValue = Gpf_Data_Filter::RANGE_LAST_MONTH;
221 break;
222 case 'year':
223 case 'thisyear':
224 case 'this year':
225 case 'y':
226 $dateRangeValue = Gpf_Data_Filter::RANGE_THIS_YEAR;
227 break;
228 case 'lastyear':
229 case 'last year':
230 $dateRangeValue = Gpf_Data_Filter::RANGE_LAST_YEAR;
231 break;
232 case '30':
233 $dateRangeValue = Gpf_Data_Filter::RANGE_LAST_30_DAYS;
234 break;
235 default:
236 $dateRangeValue = '';
237 break;
238 }
239 return $dateRangeValue;
240 }
241 private function getLoginUrl($affiliate, $session) {
242 $request = new Gpf_Rpc_FormRequest('Pap_Auth_LoginKeyService', 'getLoginKey', $session);
243 $request->addParam('userId', $affiliate->getUserid());
244
245 $loginKey = '';
246 try {
247 $request->sendNow();
248 $response = $request->getStdResponse();
249
250 if ($response->success == 'Y') {
251 $loginKey = '?LoginKey='.$request->getForm()->getFieldValue('LoginKey');
252 } else {
253 $this->_log('Error loading "login key": '.$response->message);
254 }
255 } catch(Exception $e) {
256 $this->_log('Loading "login key" failed via API: '.$e->getMessage());
257 }
258
259 if ($loginKey != '') {
260 return rtrim(get_option(postaffiliatepro::PAP_URL_SETTING_NAME), '/') . '/affiliates/login.php' . $loginKey;
261 }
262
263 // a backup method:
264 $email = $affiliate->getUsername();
265 if (strpos($email, '@') === 0) {
266 $email = $affiliate->getNotificationEmail();
267 }
268 return rtrim(get_option(postaffiliatepro::PAP_URL_SETTING_NAME), '/') . '/affiliates/login.php?username=' . urlencode($email);
269 }
270
271 public function initSettings() {
272 register_setting(self::SHORTCODES_SETTINGS_PAGE_NAME, self::AFFILAITE_SHORTCODE_CACHE);
273 }
274 }
275
276 $papShortcodeAffiliate = new Shortcode_Affiliate();
277 add_action('admin_init', array($papShortcodeAffiliate, 'initSettings'), 99);
278 add_shortcode('affiliate', array($papShortcodeAffiliate, 'getAffiliateShortCode'));
279 add_shortcode('parent', array($papShortcodeAffiliate, 'getParentAffiliateShortCode'));
280 // AJAX
281 add_action('wp_ajax_pap_login_redirect_ajax', array($papShortcodeAffiliate, 'loginRedirectAjaxCallback'), 99);