AdditionalOptions.class.php
3 months ago
CampaignInfo.class.php
3 months ago
Campaigns.class.php
3 months ago
ClickTracking.class.php
3 months ago
ContactForm7.class.php
3 months ago
Debugging.class.php
2 months ago
EDD.class.php
2 months ago
General.class.php
2 months ago
Integrations.class.php
3 months ago
LifterLMS.class.php
3 months ago
Marketpress.class.php
2 months ago
MemberPress.class.php
3 months ago
Membership2.class.php
3 months ago
PMPro.class.php
3 months ago
PPBuyNowButton.class.php
3 months ago
RestrictContentPro.class.php
3 months ago
S2Member.class.php
3 months ago
Signup.class.php
3 months ago
SimplePayPro.class.php
3 months ago
StripePayments.class.php
2 months ago
SureCart.class.php
3 months ago
WPEasyCart.class.php
3 months ago
WPPayForms.class.php
3 months ago
WishListMember.class.php
3 months ago
WooComm.class.php
2 months ago
SureCart.class.php
259 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class postaffiliatepro_Form_Settings_SureCart extends postaffiliatepro_Form_Base { |
| 5 | public const SURECART_COMMISSION_ENABLED = 'surecart-commission-enabled'; |
| 6 | private const SURECART_CONFIG_PAGE = 'surecart-config-page'; |
| 7 | private const SURECART_COUPONS_TRACKING = 'surecart-coupons-tracking'; |
| 8 | private const SURECART_PRODUCT_ID = 'surecart-product-id'; |
| 9 | private const SURECART_DATA1 = 'surecart-data1'; |
| 10 | private const SURECART_DATA2 = 'surecart-data2'; |
| 11 | private const SURECART_DATA3 = 'surecart-data3'; |
| 12 | private const SURECART_DATA4 = 'surecart-data4'; |
| 13 | private const SURECART_DATA5 = 'surecart-data5'; |
| 14 | private const SURECART_CAMPAIGN = 'surecart-campaign'; |
| 15 | |
| 16 | public function __construct() { |
| 17 | parent::__construct(self::SURECART_CONFIG_PAGE, 'options.php'); |
| 18 | } |
| 19 | |
| 20 | protected function getTemplateFile() { |
| 21 | return WP_PLUGIN_DIR . '/postaffiliatepro/Template/SureCartConfig.xtpl'; |
| 22 | } |
| 23 | |
| 24 | protected function initForm() { |
| 25 | $this->addCheckbox(self::SURECART_COUPONS_TRACKING); |
| 26 | $this->addSelect(self::SURECART_PRODUCT_ID, array( |
| 27 | '0' => ' ', |
| 28 | 'product_id' => __('product ID','postaffiliatepro'), |
| 29 | 'product_name' => __('product name','postaffiliatepro'), |
| 30 | 'product_sku' => __('product SKU','postaffiliatepro'), |
| 31 | 'product_slug' => __('product slug','postaffiliatepro') |
| 32 | )); |
| 33 | |
| 34 | $dataOptions = array( |
| 35 | '0' => ' ', |
| 36 | 'customer_id' => __('customer ID','postaffiliatepro'), |
| 37 | 'customer_email' => __('customer email','postaffiliatepro'), |
| 38 | 'customer_name' => __('customer name','postaffiliatepro'), |
| 39 | 'product_id' => __('product ID','postaffiliatepro'), |
| 40 | 'product_name' => __('product name','postaffiliatepro'), |
| 41 | 'product_sku' => __('product SKU','postaffiliatepro'), |
| 42 | 'product_slug' => __('product slug','postaffiliatepro') |
| 43 | ); |
| 44 | $this->addSelect(self::SURECART_DATA1, $dataOptions); |
| 45 | $this->addSelect(self::SURECART_DATA2, $dataOptions); |
| 46 | $this->addSelect(self::SURECART_DATA3, $dataOptions); |
| 47 | $this->addSelect(self::SURECART_DATA4, $dataOptions); |
| 48 | $this->addSelect(self::SURECART_DATA5, $dataOptions); |
| 49 | |
| 50 | $campaignHelper = new postaffiliatepro_Util_CampaignHelper(); |
| 51 | $campaignList = $campaignHelper->getCampaignsList(); |
| 52 | |
| 53 | $campaigns = array( |
| 54 | '0' => ' ' |
| 55 | ); |
| 56 | foreach ($campaignList as $row) { |
| 57 | $campaigns[$row->get('campaignid')] = htmlspecialchars($row->get('name')); |
| 58 | } |
| 59 | $this->addSelect(self::SURECART_CAMPAIGN, $campaigns); |
| 60 | |
| 61 | $this->addSubmit(); |
| 62 | } |
| 63 | |
| 64 | public function initSettings() { |
| 65 | register_setting(postaffiliatepro::INTEGRATIONS_SETTINGS_PAGE_NAME, self::SURECART_COMMISSION_ENABLED); |
| 66 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_COUPONS_TRACKING); |
| 67 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_PRODUCT_ID); |
| 68 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_DATA1); |
| 69 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_DATA2); |
| 70 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_DATA3); |
| 71 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_DATA4); |
| 72 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_DATA5); |
| 73 | register_setting(self::SURECART_CONFIG_PAGE, self::SURECART_CAMPAIGN); |
| 74 | } |
| 75 | |
| 76 | public function addPrimaryConfigMenu() { |
| 77 | if (get_option(self::SURECART_COMMISSION_ENABLED) === 'true') { |
| 78 | add_submenu_page('integrations-config-page-handle', 'SureCart', 'SureCart', 'manage_options', 'surecartintegration-settings-page', array( |
| 79 | $this, |
| 80 | 'printConfigPage' |
| 81 | )); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | public function printConfigPage() { |
| 86 | $this->render(); |
| 87 | } |
| 88 | |
| 89 | public function addCssToHead() { |
| 90 | if (get_option(self::SURECART_COMMISSION_ENABLED) === 'true') { |
| 91 | echo " |
| 92 | <style> |
| 93 | .paphideme { |
| 94 | display: none; |
| 95 | } |
| 96 | </style> |
| 97 | "; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public function integrateSureCartShortcodeForm($content) { |
| 102 | if (get_option(self::SURECART_COMMISSION_ENABLED) !== 'true') { |
| 103 | return $content; |
| 104 | } |
| 105 | $writeCookieCode = $this->getWriteCookieCode(); |
| 106 | return '<style>.paphideme {display: none;}</style>'.$content . $writeCookieCode; |
| 107 | } |
| 108 | |
| 109 | public function writeCookieToHiddenField() { |
| 110 | if (is_feed()) { |
| 111 | return; |
| 112 | } |
| 113 | if (get_option(self::SURECART_COMMISSION_ENABLED) !== 'true') { |
| 114 | return; |
| 115 | } |
| 116 | echo $this->getWriteCookieCode(); |
| 117 | } |
| 118 | |
| 119 | public function trackInitialPurchase($purchase, $isRecurringFallback = false) { |
| 120 | if (get_option(self::SURECART_COMMISSION_ENABLED) !== 'true') { |
| 121 | return; |
| 122 | } |
| 123 | if (!class_exists('SureCart\Models\Purchase') || !($purchase instanceof \SureCart\Models\Purchase)) { |
| 124 | postaffiliatepro_Base::_log('SureCart is not active or the tracking function received incorrect object. Tracking is stopped.'); |
| 125 | return; |
| 126 | } |
| 127 | $orderInstance = new \SureCart\Models\Order(); |
| 128 | $orderObject = $orderInstance::find($purchase->initial_order); |
| 129 | $checkoutInstance = new \SureCart\Models\Checkout(); |
| 130 | $checkout = $checkoutInstance::find($orderObject->checkout); |
| 131 | $cookie = trim($checkout->metadata->papvisitorid ?? ''); |
| 132 | $ip = $checkout->ip_address ?? ''; |
| 133 | $query = 'AccountId=' . postaffiliatepro::getAccountName() . '&visitorId=' . substr($cookie, -32); |
| 134 | $campaignId = get_option(self::SURECART_CAMPAIGN); |
| 135 | if ($campaignId && $campaignId !== '0') { |
| 136 | $query .= '&CampaignID=' . $campaignId; |
| 137 | } |
| 138 | $totalCost = $checkout->net_paid_amount ?? 0; |
| 139 | $totalCost /= 100; |
| 140 | $productId = $this->getExtraData($purchase, 'productID'); |
| 141 | $coupon = ''; |
| 142 | if ($checkout->discount && get_option(self::SURECART_COUPONS_TRACKING) === 'true') { |
| 143 | $discountInstance = new \SureCart\Models\Discount(); |
| 144 | $discount = $discountInstance::find($checkout->discount); |
| 145 | $couponInstance = new \SureCart\Models\Promotion(); |
| 146 | $coupon = $couponInstance::find($discount->promotion)->code; |
| 147 | } |
| 148 | $orderId = $purchase->subscription ?? $purchase->id; |
| 149 | if ($isRecurringFallback) { |
| 150 | $orderId = $purchase->id; |
| 151 | } |
| 152 | $query .= "&TotalCost=$totalCost&OrderID=$orderId&ProductID=$productId"; |
| 153 | $query .= "&Currency=$checkout->currency&ip=$ip&Coupon=$coupon"; |
| 154 | for ($d = 1; $d <=5; $d++) { |
| 155 | $query .= "&Data$d=" . urlencode($this->getExtraData($purchase, $d)); |
| 156 | } |
| 157 | self::_log('Sending a tracking request with these details: ' . print_r($query, true)); |
| 158 | self::sendRequest(postaffiliatepro::parseSaleScriptPath(), $query); |
| 159 | } |
| 160 | |
| 161 | public function trackSubscriptionRenewed($subscription) { |
| 162 | if (get_option(self::SURECART_COMMISSION_ENABLED) !== 'true') { |
| 163 | return; |
| 164 | } |
| 165 | if (!class_exists('SureCart\Models\Subscription') || !($subscription instanceof \SureCart\Models\Subscription)) { |
| 166 | postaffiliatepro_Base::_log('SureCart is not active or the tracking function received incorrect object. Tracking is stopped.'); |
| 167 | return; |
| 168 | } |
| 169 | postaffiliatepro_Base::_log('Tracking renewal of subscription: ' . $subscription->id); |
| 170 | $session = $this->getApiSession(); |
| 171 | if ($session === null || $session === '0') { |
| 172 | $session = new Pap_Api_Session($this->getApiSessionUrl()); |
| 173 | } |
| 174 | |
| 175 | if (!$this->fireRecurringCommissions($session, $subscription->id, $subscription->subtotal_amount / 100, $subscription->currency)) { |
| 176 | self::_log('Recurring commission for order ID ' . $subscription->id . ' failed, trying to create a regular commission.'); |
| 177 | $purchase = new \SureCart\Models\Purchase(); |
| 178 | $purchase = $purchase::find($subscription->purchase); |
| 179 | $this->trackInitialPurchase($purchase, true); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | private function getExtraData($purchase, $field) { |
| 184 | if ($field === 'productID') { |
| 185 | $data = get_option(self::SURECART_PRODUCT_ID); |
| 186 | } else { |
| 187 | $data = get_option(constant('self::SURECART_DATA'.$field)); |
| 188 | } |
| 189 | if ($data === '0') { |
| 190 | return ''; |
| 191 | } |
| 192 | $dataArray = explode('_', $data); |
| 193 | if ($dataArray[0] === 'customer') { |
| 194 | $customerInstance = new \SureCart\Models\Customer(); |
| 195 | return urlencode($customerInstance::find($purchase->customer)->{$dataArray[1]}); |
| 196 | } |
| 197 | $productInstance = new \SureCart\Models\Product(); |
| 198 | return urlencode($productInstance::find($purchase->product)->{$dataArray[1]}); |
| 199 | } |
| 200 | |
| 201 | private function getWriteCookieCode() { |
| 202 | return '<script type="text/javascript"> |
| 203 | window.papSureCartTrackingIdAdded = false; |
| 204 | try { |
| 205 | let papsurecartinput = document.querySelectorAll("sc-input[name=\'papvisitorid\']"); |
| 206 | if (papsurecartinput.length) { |
| 207 | let papInterval = setInterval(function() { |
| 208 | if (!window.papSureCartTrackingIdAdded && papsurecartinput[0].shadowRoot && papsurecartinput[0].shadowRoot.querySelectorAll(\'input[name="papvisitorid"]\').length) { |
| 209 | if (typeof PostAffTracker !== "undefined") { |
| 210 | papsurecartinput[0].shadowRoot.querySelectorAll(\'input[name="papvisitorid"]\')[0].value = PostAffTracker.getVisitorId(); |
| 211 | } else { |
| 212 | papsurecartinput[0].shadowRoot.querySelectorAll(\'input[name="papvisitorid"]\')[0].value = window.localStorage.getItem("PAPVisitorId") || (document.cookie.match(/PAPVisitorId=([^;]+)/) ? document.cookie.match(/PAPVisitorId=([^;]+)/)[1] : ""); |
| 213 | } |
| 214 | window.papSureCartTrackingIdAdded = true; |
| 215 | clearInterval(papInterval); |
| 216 | } |
| 217 | }, 500); |
| 218 | } |
| 219 | } catch (e) { |
| 220 | console.error("'.__('Error while trying to write PAP visitor ID to SureCart input field','postaffiliatepro').': " + e); |
| 221 | } |
| 222 | </script>'; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | $papSubmenuPriority = 48; |
| 227 | $appIntegrationSureCart = new postaffiliatepro_Form_Settings_SureCart(); |
| 228 | add_action('admin_init', array( |
| 229 | $appIntegrationSureCart, |
| 230 | 'initSettings' |
| 231 | ), 99); |
| 232 | add_action('admin_menu', array( |
| 233 | $appIntegrationSureCart, |
| 234 | 'addPrimaryConfigMenu' |
| 235 | ), $papSubmenuPriority); |
| 236 | |
| 237 | add_action('wp_head', array( |
| 238 | $appIntegrationSureCart, |
| 239 | 'addCssToHead' |
| 240 | ), 99); |
| 241 | |
| 242 | add_filter('surecart/shortcode/render', array( |
| 243 | $appIntegrationSureCart, |
| 244 | 'integrateSureCartShortcodeForm' |
| 245 | ), 100); |
| 246 | |
| 247 | add_action('wp_footer', array( |
| 248 | $appIntegrationSureCart, |
| 249 | 'writeCookieToHiddenField' |
| 250 | ), 100); |
| 251 | |
| 252 | add_action('surecart/purchase_created', array( |
| 253 | $appIntegrationSureCart, |
| 254 | 'trackInitialPurchase' |
| 255 | )); |
| 256 | add_action('surecart/subscription_renewed', array( |
| 257 | $appIntegrationSureCart, |
| 258 | 'trackSubscriptionRenewed' |
| 259 | )); |