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
EDD.class.php
272 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class postaffiliatepro_Form_Settings_EDD extends postaffiliatepro_Form_Base { |
| 5 | const EDD_COMMISSION_ENABLED = 'edd-commission-enabled'; |
| 6 | const EDD_CONFIG_PAGE = 'edd-config-page'; |
| 7 | const EDD_PERPRODUCT = 'edd-per-product'; |
| 8 | const EDD_PRODUCT_ID = 'edd-product-id'; |
| 9 | const EDD_TRACK_FEE = 'edd-track-fee'; |
| 10 | const EDD_DATA1 = 'edd-data1'; |
| 11 | const EDD_CAMPAIGN = 'edd-campaign'; |
| 12 | const EDD_STATUS_UPDATE = 'edd-status-update'; |
| 13 | |
| 14 | public function __construct() { |
| 15 | parent::__construct(self::EDD_CONFIG_PAGE, 'options.php'); |
| 16 | } |
| 17 | |
| 18 | protected function getTemplateFile() { |
| 19 | return WP_PLUGIN_DIR . '/postaffiliatepro/Template/EDD.xtpl'; |
| 20 | } |
| 21 | |
| 22 | protected function initForm() { |
| 23 | $this->addCheckbox(self::EDD_PERPRODUCT); |
| 24 | $this->addCheckbox(self::EDD_TRACK_FEE); |
| 25 | $this->addSelect(self::EDD_PRODUCT_ID, array( |
| 26 | '0' => ' ', |
| 27 | 'id' => 'product ID', |
| 28 | 'name' => 'product name' |
| 29 | )); |
| 30 | $this->addSelect(self::EDD_DATA1, array( |
| 31 | '0' => ' ', |
| 32 | 'id' => 'customer ID', |
| 33 | 'email' => 'customer email' |
| 34 | )); |
| 35 | |
| 36 | $campaignHelper = new postaffiliatepro_Util_CampaignHelper(); |
| 37 | $campaignList = $campaignHelper->getCampaignsList(); |
| 38 | |
| 39 | $campaigns = array('0' => ' '); |
| 40 | foreach ($campaignList as $row) { |
| 41 | $campaigns[$row->get('campaignid')] = htmlspecialchars($row->get('name')); |
| 42 | } |
| 43 | $this->addSelect(self::EDD_CAMPAIGN, $campaigns); |
| 44 | |
| 45 | $this->addSubmit(); |
| 46 | } |
| 47 | |
| 48 | public function initSettings() { |
| 49 | register_setting(postaffiliatepro::INTEGRATIONS_SETTINGS_PAGE_NAME, self::EDD_COMMISSION_ENABLED); |
| 50 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_PERPRODUCT); |
| 51 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_TRACK_FEE); |
| 52 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_PRODUCT_ID); |
| 53 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_DATA1); |
| 54 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_STATUS_UPDATE); |
| 55 | register_setting(self::EDD_CONFIG_PAGE, self::EDD_CAMPAIGN); |
| 56 | } |
| 57 | |
| 58 | public function addPrimaryConfigMenu() { |
| 59 | if (get_option(self::EDD_COMMISSION_ENABLED) === 'true') { |
| 60 | add_submenu_page('integrations-config-page-handle', 'Easy Digital Downloads', 'Easy Digital Downloads', 'manage_options', 'eddintegration-settings-page', array( |
| 61 | $this, |
| 62 | 'printConfigPage' |
| 63 | )); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function printConfigPage() { |
| 68 | $this->render(); |
| 69 | } |
| 70 | |
| 71 | public function eddAddThankYouPageTrackSale($payment, $edd_receipt_args) { |
| 72 | if (get_option(self::EDD_COMMISSION_ENABLED) !== 'true') { |
| 73 | echo "<!-- Post Affiliate Pro sale tracking error - tracking not enabled -->\n"; |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $payment = new EDD_Payment($payment->ID); |
| 78 | $cart_details = $payment->cart_details; |
| 79 | |
| 80 | if (is_array($cart_details) && get_option(self::EDD_PERPRODUCT) === 'true') { |
| 81 | $count = count($cart_details); |
| 82 | $i = 1; |
| 83 | $deleteCookiesAfterFee = false; |
| 84 | foreach ($cart_details as $n => $download) { |
| 85 | $deleteCookies = false; |
| 86 | $orderId = ($this->eddIsRecurringProduct($download)) ? $payment->ID . '-' . $download['id'] : $payment->ID.'('.($i).')'; |
| 87 | $order['id'] = $orderId; |
| 88 | $order['total'] = $download['subtotal']; |
| 89 | $order['product'] = $this->getProductID($download); |
| 90 | $order['data1'] = $this->getTrackingData1($payment); |
| 91 | $order['currency'] = $payment->currency; |
| 92 | if ($count == $i) { |
| 93 | $deleteCookies = true; |
| 94 | if (get_option(self::EDD_TRACK_FEE) === 'true') { |
| 95 | $deleteCookies = false; |
| 96 | $deleteCookiesAfterFee = true; |
| 97 | } |
| 98 | } |
| 99 | $this->trackOrder($order, $deleteCookies); |
| 100 | $i++; |
| 101 | if (get_option(self::EDD_TRACK_FEE) === 'true') { |
| 102 | $this->trackSignupFee($payment, $download, $i, $deleteCookiesAfterFee); |
| 103 | $i++; |
| 104 | } |
| 105 | } |
| 106 | } else { |
| 107 | // per order |
| 108 | $order['id'] = $payment->ID; |
| 109 | $totalCost = (get_option(self::EDD_TRACK_FEE) === 'true') ? $payment->subtotal + $this->getSignupFee($payment) : $payment->subtotal; |
| 110 | $order['total'] = $totalCost; |
| 111 | $order['product'] = ''; |
| 112 | $order['data1'] = $this->getTrackingData1($payment); |
| 113 | $order['currency'] = $payment->currency; |
| 114 | $this->trackOrder($order); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public function eddTrackRecurring($payment, $extraDetails) { |
| 119 | if (get_option(self::EDD_COMMISSION_ENABLED) !== 'true') { |
| 120 | return; |
| 121 | } |
| 122 | self::_log('EDD Tracking of recurring order: ' . $payment->parent_payment); |
| 123 | |
| 124 | $session = $this->getApiSession(); |
| 125 | if ($session === null || $session === '0') { |
| 126 | self::_log('We have no session to PAP installation! Recurring commission failed. Trying to create a regular commission.'); |
| 127 | $this->eddTrackRecurringAsRegularSale($payment, $extraDetails); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | if (!$this->fireRecurringCommissions($session, $payment->parent_payment . '-' . $extraDetails->product_id, $payment->total)) { |
| 132 | self::_log('Recurring commission for order ID ' . $payment->parent_payment . ' failed, trying to create a regular commission.'); |
| 133 | $this->eddTrackRecurringAsRegularSale($payment, $extraDetails); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private function trackOrder($order, $deleteCookie = false) { |
| 138 | $result = "<!-- Post Affiliate Pro sale tracking -->\n"; |
| 139 | |
| 140 | $contentToAsync = 'PostAffTracker.setAccountId(\'' . postaffiliatepro::getAccountName() . '\');'; |
| 141 | $contentToAsync .= "var sale = PostAffTracker.createSale();\n"; |
| 142 | $contentToAsync .= "sale.setTotalCost('" . esc_js($order['total']) . "');\n"; |
| 143 | $contentToAsync .= "sale.setOrderID('" . esc_js($order['id']) . "');\n"; |
| 144 | $contentToAsync .= "sale.setProductID('" . esc_js($order['product']) . "');\n"; |
| 145 | $contentToAsync .= "sale.setData1('" . esc_js($order['data1']) . "');\n"; |
| 146 | $contentToAsync .= "sale.setCurrency('" . esc_js($order['currency']) . "');\n"; |
| 147 | |
| 148 | if (!$deleteCookie) { |
| 149 | $contentToAsync .= "if (typeof sale.doNotDeleteCookies === 'function') {sale.doNotDeleteCookies();}\n"; |
| 150 | } |
| 151 | if (get_option(self::EDD_CAMPAIGN) !== '' && get_option(self::EDD_CAMPAIGN) !== null && get_option(self::EDD_CAMPAIGN) !== 0 && get_option(self::EDD_CAMPAIGN) !== '0') { |
| 152 | $contentToAsync .= "sale.setCampaignID('" . get_option(self::EDD_CAMPAIGN) . "');\n"; |
| 153 | } |
| 154 | $contentToAsync .= "PostAffTracker.register();\n"; |
| 155 | |
| 156 | if (get_option(postaffiliatepro::ASYNC_ENABLED) !== 'true') { |
| 157 | $result .= postaffiliatepro::getPAPTrackJSDynamicCode().'<script type="text/javascript">'; |
| 158 | $result .= $contentToAsync . "</script>\n"; |
| 159 | } else { |
| 160 | $result .= postaffiliatepro::getPAPTrackJSAsyncCode($contentToAsync); |
| 161 | } |
| 162 | |
| 163 | echo $result; |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | private function getProductID($order) { |
| 168 | switch (get_option(self::EDD_PRODUCT_ID)) { |
| 169 | case 'id': |
| 170 | return $order['id']; |
| 171 | case 'name': |
| 172 | return $order['name']; |
| 173 | default: return ''; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | private function getTrackingData1($payment) { |
| 178 | switch (get_option(self::EDD_DATA1)) { |
| 179 | case 'id': |
| 180 | if ($payment->user_id != '') { |
| 181 | return $payment->user_id; |
| 182 | } |
| 183 | return crc32($payment->email); |
| 184 | case 'email': return $payment->email; |
| 185 | default: return ''; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | private function getSignupFee($payment) { |
| 190 | if (isset($payment->fees)) { |
| 191 | foreach ($payment->fees as $fee) { |
| 192 | if ($fee['id'] === 'signup_fee') { |
| 193 | return $fee['amount']; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | private function getSignupFeeFromDownload($download) { |
| 201 | if (isset($download['options']['recurring']['signup_fee'])) { |
| 202 | return $download['options']['recurring']['signup_fee']; |
| 203 | } |
| 204 | if (isset($download['item_number']['options']['recurring']['signup_fee'])) { |
| 205 | return $download['item_number']['options']['recurring']['signup_fee']; |
| 206 | } |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | private function trackSignupFee($payment, $download, $i, $deleteCookies) { |
| 211 | $order['id'] = $payment->ID.'('.($i).')'; |
| 212 | $order['total'] = $this->getSignupFeeFromDownload($download); |
| 213 | $order['product'] = $this->getProductID($download) . ' Signup Fee'; |
| 214 | $order['data1'] = $this->getTrackingData1($payment); |
| 215 | $order['currency'] = $payment->currency; |
| 216 | $this->trackOrder($order, $deleteCookies); |
| 217 | } |
| 218 | |
| 219 | private function eddTrackRecurringAsRegularSale($payment, $extraDetails) { |
| 220 | $query = 'AccountId=' . postaffiliatepro::getAccountName(); |
| 221 | $campaignId = get_option(self::EDD_CAMPAIGN); |
| 222 | if ($campaignId != '' && $campaignId !== '0') { |
| 223 | $query .= '&CampaignID=' . $campaignId; |
| 224 | } |
| 225 | $productId = $extraDetails->product_id; |
| 226 | if (get_option(self::EDD_PRODUCT_ID) === 'name' && function_exists('edd_get_payment')) { |
| 227 | $parentPayment = edd_get_payment($payment->parent_payment); |
| 228 | if (isset($parentPayment->cart_details)) { |
| 229 | foreach ( $parentPayment->cart_details as $cart_item ) { |
| 230 | if ( (int) $extraDetails->product_id === (int) $cart_item['id'] ) { |
| 231 | $productId = $cart_item['name']; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | $orderId = $payment->ID . '-' . $extraDetails->product_id; |
| 237 | $query .= "&TotalCost=$payment->total&OrderID=$orderId&ProductID=$productId"; |
| 238 | $query .= "&Currency=$payment->currency"; |
| 239 | $query .= "&Data1=" . urlencode($this->getTrackingData1($payment)); |
| 240 | self::_log('EDD Sending a tracking request with these details: ' . print_r($query, true)); |
| 241 | self::sendRequest(postaffiliatepro::parseSaleScriptPath(), $query); |
| 242 | } |
| 243 | |
| 244 | private function eddIsRecurringProduct($download) { |
| 245 | if(isset($download['options']['recurring'])) { |
| 246 | return true; |
| 247 | } |
| 248 | if(isset($download['item_number']['options']['recurring'])) { |
| 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | $papSubmenuPriority = 12; |
| 256 | $papIntegrationEDD = new postaffiliatepro_Form_Settings_EDD(); |
| 257 | add_action('admin_init', array( |
| 258 | $papIntegrationEDD, |
| 259 | 'initSettings' |
| 260 | ), 99); |
| 261 | add_action('admin_menu', array( |
| 262 | $papIntegrationEDD, |
| 263 | 'addPrimaryConfigMenu' |
| 264 | ), $papSubmenuPriority); |
| 265 | add_action('edd_payment_receipt_after_table', array( |
| 266 | $papIntegrationEDD, |
| 267 | 'eddAddThankYouPageTrackSale' |
| 268 | ), 99, 2); |
| 269 | add_action('edd_recurring_add_subscription_payment', array( |
| 270 | $papIntegrationEDD, |
| 271 | 'eddTrackRecurring' |
| 272 | ), 99, 2); |