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); |