AdditionalOptions.class.php
2 months ago
CampaignInfo.class.php
2 months ago
Campaigns.class.php
2 months ago
ClickTracking.class.php
2 months ago
ContactForm7.class.php
2 months ago
Debugging.class.php
4 weeks ago
EDD.class.php
4 weeks ago
General.class.php
4 weeks ago
Integrations.class.php
2 months ago
LifterLMS.class.php
2 months ago
Marketpress.class.php
4 weeks ago
MemberPress.class.php
2 months ago
Membership2.class.php
2 months ago
PMPro.class.php
2 months ago
PPBuyNowButton.class.php
2 months ago
RestrictContentPro.class.php
2 months ago
S2Member.class.php
2 months ago
Signup.class.php
2 months ago
SimplePayPro.class.php
2 months ago
StripePayments.class.php
4 weeks ago
SureCart.class.php
2 months ago
WPEasyCart.class.php
2 months ago
WPPayForms.class.php
2 months ago
WishListMember.class.php
2 months ago
WooComm.class.php
4 weeks ago
MemberPress.class.php
204 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | |
| 4 | class postaffiliatepro_Form_Settings_MemberPress extends postaffiliatepro_Form_Base { |
| 5 | const MEMBERPRESS_COMMISSION_ENABLED = 'memberpress-commission-enabled'; |
| 6 | const MEMBERPRESS_CONFIG_PAGE = 'memberpress-config-page'; |
| 7 | const MEMBERPRESS_TRACK_RECURRING = 'memberpress-track-refurring'; |
| 8 | const MEMBERPRESS_TRACK_RECURRING_TOTAL = 'memberpress-track-recurring-total'; |
| 9 | const MEMBERPRESS_DATA1 = 'memberpress-data1'; |
| 10 | const MEMBERPRESS_DATA2 = 'memberpress-data2'; |
| 11 | const MEMBERPRESS_DATA3 = 'memberpress-data3'; |
| 12 | const MEMBERPRESS_DATA4 = 'memberpress-data4'; |
| 13 | const MEMBERPRESS_DATA5 = 'memberpress-data5'; |
| 14 | |
| 15 | public function __construct() { |
| 16 | parent::__construct(self::MEMBERPRESS_CONFIG_PAGE, 'options.php'); |
| 17 | } |
| 18 | |
| 19 | protected function getTemplateFile() { |
| 20 | return WP_PLUGIN_DIR . '/postaffiliatepro/Template/MemberPressConfig.xtpl'; |
| 21 | } |
| 22 | |
| 23 | protected function initForm() { |
| 24 | $this->addCheckbox(self::MEMBERPRESS_TRACK_RECURRING); |
| 25 | $this->addCheckbox(self::MEMBERPRESS_TRACK_RECURRING_TOTAL); |
| 26 | |
| 27 | $additionalData = array( |
| 28 | '0' => ' ', |
| 29 | 'u_ID' => 'user ID', |
| 30 | 'u_user_login' => 'user login', |
| 31 | 'u_user_nicename' => 'user nice name', |
| 32 | 'u_display_name' => 'user display name', |
| 33 | 'u_user_email' => 'user email', |
| 34 | 'u_user_url' => 'user URL', |
| 35 | 'u_first_name' => 'user first name', |
| 36 | 'u_last_name' => 'user last name', |
| 37 | 'u_user_ip' => 'user IP address', |
| 38 | 'p_price' => 'membership price', |
| 39 | 'p_period_type' => 'membership period type', |
| 40 | 'p_pricing_title' => 'membership pricing title' |
| 41 | ); |
| 42 | $this->addSelect(self::MEMBERPRESS_DATA1, $additionalData); |
| 43 | $this->addSelect(self::MEMBERPRESS_DATA2, $additionalData); |
| 44 | $this->addSelect(self::MEMBERPRESS_DATA3, $additionalData); |
| 45 | $this->addSelect(self::MEMBERPRESS_DATA4, $additionalData); |
| 46 | $this->addSelect(self::MEMBERPRESS_DATA5, $additionalData); |
| 47 | |
| 48 | $this->addSubmit(); |
| 49 | } |
| 50 | |
| 51 | public function initSettings() { |
| 52 | register_setting(postaffiliatepro::INTEGRATIONS_SETTINGS_PAGE_NAME, self::MEMBERPRESS_COMMISSION_ENABLED); |
| 53 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_TRACK_RECURRING); |
| 54 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_TRACK_RECURRING_TOTAL); |
| 55 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_DATA1); |
| 56 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_DATA2); |
| 57 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_DATA3); |
| 58 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_DATA4); |
| 59 | register_setting(self::MEMBERPRESS_CONFIG_PAGE, self::MEMBERPRESS_DATA5); |
| 60 | } |
| 61 | |
| 62 | public function addPrimaryConfigMenu() { |
| 63 | if (get_option(self::MEMBERPRESS_COMMISSION_ENABLED) === 'true') { |
| 64 | add_submenu_page( |
| 65 | 'integrations-config-page-handle', |
| 66 | 'MemberPress', |
| 67 | 'MemberPress', |
| 68 | 'manage_options', |
| 69 | 'memberpressintegration-settings-page', |
| 70 | array($this, 'printConfigPage') |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public function printConfigPage() { |
| 76 | $this->render(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | public function MemberPressTrackSale($txn) { |
| 81 | if (get_option(self::MEMBERPRESS_COMMISSION_ENABLED) !== 'true') { |
| 82 | return $txn; |
| 83 | } |
| 84 | |
| 85 | // START hacky fix for recurring subscriptions with no free trial comming in as $0 amount and few seconds afterwards triggering MemberPressRecurringSale with correct amount |
| 86 | $price = $txn->amount; |
| 87 | if ($price == '0.00' || $price == 0) { |
| 88 | $product = $txn->product(); |
| 89 | if (isset($product->trial_days) && isset($product->trial_amount)) { |
| 90 | if ($product->trial_days == '0' || $product->trial_amount != '0') { |
| 91 | $this->_log('Subscription product with incorrect amount received, saving cookie to user\'s meta and waiting for recurring notification with actual paid amount.'); |
| 92 | if (isset($_REQUEST['pap_custom']) && (sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])) != '')) { |
| 93 | $wp_user_obj = MeprUtils::get_user_by('id', $txn->user_id); |
| 94 | update_user_meta($wp_user_obj->ID, 'pap_custom', sanitize_text_field(wp_unslash($_REQUEST['pap_custom']))); |
| 95 | } |
| 96 | return $txn; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | // END |
| 101 | |
| 102 | $accountID = ''; |
| 103 | $visitorID = ''; |
| 104 | if (isset($_REQUEST['pap_custom']) && (sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])) != '')) { |
| 105 | $papCookie = sanitize_text_field(wp_unslash($_REQUEST['pap_custom'])); |
| 106 | } else { |
| 107 | $wp_user_obj = MeprUtils::get_user_by('id', $txn->user_id); |
| 108 | $papCookie = get_user_meta($wp_user_obj->ID, 'pap_custom', true); |
| 109 | } |
| 110 | if ($papCookie !== '') { |
| 111 | $visitorID = substr($papCookie, -32); |
| 112 | $accountID = substr($papCookie, 0, 8); |
| 113 | } |
| 114 | |
| 115 | $query = 'AccountId='.$accountID. '&visitorId='.$visitorID.'&TotalCost='.$txn->amount.'&ProductID='.$txn->product_id.'&OrderID='; |
| 116 | |
| 117 | if (isset($txn->subscription_id) && $txn->subscription_id != '0' && $txn->subscription_id != 0) { |
| 118 | $query .= $txn->subscription_id; |
| 119 | } else { |
| 120 | $query .= $txn->id; |
| 121 | } |
| 122 | |
| 123 | $query = $this->addExtraDataToQuery($query, $txn); |
| 124 | |
| 125 | if (isset($_REQUEST['pap_IP']) && ($ip = filter_var(wp_unslash($_REQUEST['pap_IP']), FILTER_VALIDATE_IP))) { |
| 126 | $query .= '&ip='.$ip; |
| 127 | } |
| 128 | |
| 129 | $this->_log('MemberPress integration sending a tracking request with the following details: ' . $query); |
| 130 | self::sendRequest(postaffiliatepro::parseSaleScriptPath(), $query); |
| 131 | return $txn; |
| 132 | } |
| 133 | |
| 134 | public function addHiddenFieldToPaymentForm($productId) { |
| 135 | postaffiliatepro::addHiddenFieldToPaymentForm(); |
| 136 | return $productId; |
| 137 | } |
| 138 | |
| 139 | public function MemberPressRecurringSale(MeprEvent $event) { |
| 140 | $txn = new MeprTransaction($event->evt_id); |
| 141 | if (get_option(self::MEMBERPRESS_TRACK_RECURRING) !== 'true') { |
| 142 | $this->_log('Recurring commissions are not enabled, ending'); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // try to recur a commission with order ID $txn->subscription_id |
| 147 | $session = $this->getApiSession(); |
| 148 | if ($session === null || $session === '0') { |
| 149 | $this->_log('We have no session to PAP installation! Recurring commission failed.'); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $recurringTotal = false; |
| 154 | if (get_option(self::MEMBERPRESS_TRACK_RECURRING_TOTAL) === 'true') { |
| 155 | $recurringTotal = $txn->amount; |
| 156 | } |
| 157 | $this->_log('MemberPress integration trying to trigger a recurring commission for subscription ' . $txn->subscription_id . ' with value ' . $recurringTotal); |
| 158 | if (!$this->fireRecurringCommissions($session, $txn->subscription_id, $recurringTotal)) { |
| 159 | // creating recurring commissions failed, create a new commission instead |
| 160 | $this->_log('Creating a new commission with order ID '.$txn->subscription_id); |
| 161 | $this->MemberPressTrackSale($txn); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private function addExtraDataToQuery($query, $txn) { |
| 166 | for ($i = 1; $i <= 5; $i++) { |
| 167 | if (get_option(constant('self::MEMBERPRESS_DATA'. $i)) === '0') { |
| 168 | continue; |
| 169 | } |
| 170 | if (substr(get_option(constant('self::MEMBERPRESS_DATA'. $i)), 0, 2) === 'u_') { |
| 171 | $data = $this->getUserDetail($txn->user_id, substr(get_option(constant('self::MEMBERPRESS_DATA'. $i)), 2)); |
| 172 | } else { |
| 173 | $data = $this->getProductDetail($txn->product_id, substr(get_option(constant('self::MEMBERPRESS_DATA'. $i)), 2)); |
| 174 | } |
| 175 | $query .= '&data' . $i . '=' . $data; |
| 176 | } |
| 177 | return $query; |
| 178 | } |
| 179 | |
| 180 | private function getUserDetail($id, $detail) { |
| 181 | $user = new MeprUser($id); |
| 182 | if (isset($user->$detail)) { |
| 183 | return urlencode($user->$detail); |
| 184 | } |
| 185 | return ''; |
| 186 | } |
| 187 | |
| 188 | private function getProductDetail($id, $detail) { |
| 189 | $product = new MeprProduct($id); |
| 190 | if (isset($product->$detail)) { |
| 191 | return urlencode($product->$detail); |
| 192 | } |
| 193 | return ''; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | $papSubmenuPriority = 22; |
| 198 | $papIntegrationMemberPress = new postaffiliatepro_Form_Settings_MemberPress(); |
| 199 | add_action('admin_init', array($papIntegrationMemberPress, 'initSettings'), 99); |
| 200 | add_action('admin_menu', array($papIntegrationMemberPress, 'addPrimaryConfigMenu'), $papSubmenuPriority); |
| 201 | |
| 202 | add_action('mepr-signup', array($papIntegrationMemberPress, 'MemberPressTrackSale')); |
| 203 | add_action('mepr-checkout-before-submit', array($papIntegrationMemberPress, 'addHiddenFieldToPaymentForm')); |
| 204 | add_action('mepr-event-recurring-transaction-completed', array($papIntegrationMemberPress, 'MemberPressRecurringSale'), 99, 1); |