';
}
public function wpspcAddCustomValue($custom_field_val) {
if (isset($_COOKIE['wpam_id'])) {
$name = 'wpam_tracking';
$value = $_COOKIE['wpam_id'];
$new_val = $name . '=' . $value;
$custom_field_val = $custom_field_val . '&' . $new_val;
WPAM_Logger::log_debug('Simple WP Cart Integration - Adding custom field value. New value: ' . $custom_field_val);
} else if (isset($_COOKIE[WPAM_PluginConfig::$RefKey])) {
$name = 'wpam_tracking';
$value = $_COOKIE[WPAM_PluginConfig::$RefKey];
$new_val = $name . '=' . $value;
$custom_field_val = $custom_field_val . '&' . $new_val;
WPAM_Logger::log_debug('Simple WP Cart Integration - Adding custom field value. New value: ' . $custom_field_val);
}
else{
if(get_option(WPAM_PluginConfig::$UseIPReferralTrack) == 1){
if(get_option(WPAM_PluginConfig::$AnonymizeIPClickTrack) == 1){
return $custom_field_val;
}
$user_ip = WPAM_Click_Tracking::get_user_ip();
$aff_id = WPAM_Click_Tracking::get_referrer_id_from_ip_address_by_cookie_duration($user_ip);
if (!empty($aff_id)){
$name = 'wpam_tracking';
$value = $aff_id;
$new_val = $name . '=' . $value;
$custom_field_val = $custom_field_val . '&' . $new_val;
WPAM_Logger::log_debug('Simple WP Cart Integration - Adding custom field value using a fallback method. New value: ' . $custom_field_val);
}
}
}
return $custom_field_val;
}
public function wpspcProcessTransaction($ipn_data) {
$custom_data = $ipn_data['custom'];
WPAM_Logger::log_debug('Simple WP Cart Integration - IPN processed hook fired. Custom field value: ' . $custom_data);
$custom_values = array();
parse_str($custom_data, $custom_values);
if (!isset($custom_values['wpam_tracking']) || empty($custom_values['wpam_tracking'])) {
if (isset($_COOKIE['wpam_id']) && !empty($_COOKIE['wpam_id'])) { //useful for onsite option such as smart checkout
$custom_values['wpam_tracking'] = $_COOKIE['wpam_id'];
}
}
if (isset($custom_values['wpam_tracking']) && !empty($custom_values['wpam_tracking'])) {
$tracking_value = $custom_values['wpam_tracking'];
WPAM_Logger::log_debug('Simple WP Cart Integration - Tracking data present. Need to track affiliate commission. Tracking value: ' . $tracking_value);
$purchaseLogId = $ipn_data['txn_id'];
$purchaseAmount = $ipn_data['mc_gross']; //TODO - later calculate sub-total only
$buyer_email = $ipn_data['payer_email'];
$strRefKey = $tracking_value;
$requestTracker = new WPAM_Tracking_RequestTracker();
$requestTracker->handleCheckoutWithRefKey($purchaseLogId, $purchaseAmount, $strRefKey, $buyer_email);
WPAM_Logger::log_debug('Simple WP Cart Integration - Commission tracked for transaction ID: ' . $purchaseLogId . ', Purchase amt: ' . $purchaseAmount . ', Buyer Email: ' . $buyer_email);
}
}
public function onWpscCheckout(array $purchaseInfo) {
if ($purchaseInfo['purchase_log']['processed'] >= 2) {
$purchaseAmount = $purchaseInfo['purchase_log']['totalprice'] - $purchaseInfo['purchase_log']['base_shipping'];
$purchaseLogId = $purchaseInfo['purchase_log']['id'];
$buyer_email = wpsc_get_buyers_email($purchaseLogId);
$requestTracker = new WPAM_Tracking_RequestTracker();
$requestTracker->handleCheckout($purchaseLogId, $purchaseAmount, $buyer_email);
}
}
public function WooCheckoutUpdateOrderMeta($order_id, $posted) {
$wpam_refkey = "";
if (isset($_COOKIE['wpam_id'])) {
$wpam_refkey = $_COOKIE['wpam_id'];
}
if (!empty($wpam_refkey)) { //wpam_id cookie is found and contains affiliate ID.
WPAM_Logger::log_debug("WooCommerce Integration - The user was referred by an affiliate");
$order = wc_get_order($order_id);
$order->update_meta_data('_wpam_id', $wpam_refkey);
$order->save();
$wpam_refkey = $order->get_meta('_wpam_id');
WPAM_Logger::log_debug("WooCommerce Integration - Saving wpam_id (" . $wpam_refkey . ") with order. Order ID: " . $order_id);
}
else{
WPAM_Logger::log_debug("WooCommerce Integration - No affilie cookie found");
WPAM_Logger::log_debug("WooCommerce Integration - Attempting to track using a fallback method");
if(get_option(WPAM_PluginConfig::$UseIPReferralTrack) == 1){
if(get_option(WPAM_PluginConfig::$AnonymizeIPClickTrack) == 1){
WPAM_Logger::log_debug("WooCommerce Integration - The option to anonymize IP addresses is enabled. The fallback method cannot be used.");
return;
}
$user_ip = WPAM_Click_Tracking::get_user_ip();
$aff_id = WPAM_Click_Tracking::get_referrer_id_from_ip_address_by_cookie_duration($user_ip);
if (!empty($aff_id)){
$order = wc_get_order($order_id);
$order->update_meta_data('_wpam_id', $aff_id);
$order->save();
$wpam_refkey = $order->get_meta('_wpam_id');
WPAM_Logger::log_debug("WooCommerce Integration - Saving wpam_id (" . $wpam_refkey . ") with order using the fallback method. Order ID: " . $order_id);
return;
}
else{
WPAM_Logger::log_debug("WooCommerce Integration - Could not track using the fallback method");
}
}
else{
WPAM_Logger::log_debug("WooCommerce Integration - The fallback method is not enabled");
}
WPAM_Logger::log_debug("WooCommerce Integration - The user was not referred by an affiliate. No need to do anything.");
}
}
public function WooCommerceProcessTransaction($order_id) {
//affiliates manager code
WPAM_Logger::log_debug('WooCommerce Integration - Order processed. Order ID: ' . $order_id);
if (wpam_has_purchase_record($order_id)) {
WPAM_Logger::log_debug('WooCommerce Integration - Affiliate commission for this transaction was awarded once. No need to process anything.');
return;
}
WPAM_Logger::log_debug('WooCommerce Integration - Checking if affiliate commission needs to be awarded.');
$order = wc_get_order($order_id);
if(function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order, 'parent')){
WPAM_Logger::log_debug("WooCommerce Integration - This notification is for a new subscription payment");
WPAM_Logger::log_debug("The commission will be calculated via the recurring payemnt api call.", 2);
return;
}
if(function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order, 'renewal')){
WPAM_Logger::log_debug("WooCommerce Integration - This notification is for a recurring subscription payment");
WPAM_Logger::log_debug("The commission will be calculated via the recurring payemnt api call.", 2);
return;
}
if(function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order, 'resubscribe')){
WPAM_Logger::log_debug("WooCommerce Integration - This notification is for a resubscription payment");
WPAM_Logger::log_debug("The commission will be calculated via the recurring payemnt api call.", 2);
return;
}
if(function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($order, 'switch')){
WPAM_Logger::log_debug("WooCommerce Integration - This notification is for a subscription switch");
WPAM_Logger::log_debug("The commission will be calculated via the recurring payemnt api call.", 2);
return;
}
$order_status = $order->get_status();
WPAM_Logger::log_debug("WooCommerce Integration - Order status: " . $order_status);
if (strtolower($order_status) != "completed" && strtolower($order_status) != "processing") {
WPAM_Logger::log_debug("WooCommerce Integration - Order status for this transaction is not in a 'completed' or 'processing' state. Commission will not be awarded at this stage.", 2);
WPAM_Logger::log_debug("WooCommerce Integration - Commission for this transaction will be awarded when you set the order status to completed or processing.", 2);
return;
}
$total = $order->get_total();
$shipping = $order->get_total_shipping();
$tax = $order->get_total_tax();
$fees = wpam_get_total_woocommerce_order_fees($order);
WPAM_Logger::log_debug('WooCommerce Integration - Total amount: ' . $total . ', Total shipping: ' . $shipping . ', Total tax: ' . $tax . ', Fees: '. $fees);
$purchaseAmount = $total - $shipping - $tax - $fees;
$buyer_email = $order->get_billing_email();
$currency = $order->get_currency();
//
$order_currency = $currency;
$order_default_currency = $order->get_meta('_wcpay_multi_currency_order_default_currency');
$order_exchange_rate = $order->get_meta('_wcpay_multi_currency_order_exchange_rate');
if(isset($order_default_currency) && !empty($order_default_currency) && $order_default_currency != $order_currency){
WPAM_Logger::log_debug('WooCommerce Integration - The order is in a different currency. Default currency: '.$order_default_currency.', order currency: '.$order_currency);
if(isset($order_exchange_rate) && is_numeric($order_exchange_rate) && $order_exchange_rate > 0){
$currency = $order_default_currency;
WPAM_Logger::log_debug('WooCommerce Integration - Exchange rate: '.$order_exchange_rate);
$purchaseAmount = $purchaseAmount / $order_exchange_rate;
$purchaseAmount = round($purchaseAmount, 2);
WPAM_Logger::log_debug('WooCommerce Integration - Total amount in default currency: ' . $purchaseAmount);
}
}
//
$wpam_refkey = $order->get_meta('_wpam_refkey');
$wpam_id = $order->get_meta('_wpam_id');
if (!empty($wpam_id)) {
$wpam_refkey = $wpam_id;
}
$wpam_refkey = apply_filters('wpam_woo_override_refkey', $wpam_refkey, $order);
if (empty($wpam_refkey)) {
WPAM_Logger::log_debug("WooCommerce Integration - could not get wpam_id/wpam_refkey from cookie. This is not an affiliate sale", 4);
return;
}
$requestTracker = new WPAM_Tracking_RequestTracker();
WPAM_Logger::log_debug('WooCommerce Integration - awarding commission for order ID: ' . $order_id . ', Purchase amount: ' . $purchaseAmount . ', Affiliate ID: ' . $wpam_refkey . ', Buyer Email: ' . $buyer_email);
$args = array();
$args['txn_id'] = $order_id;
$args['amount'] = $purchaseAmount;
$args['aff_id'] = $wpam_refkey;
if(isset($buyer_email) && !empty($buyer_email)){
$args['email'] = $buyer_email;
}
$args['currency']= $currency;
$args['integration'] = 'woocommerce';
$args['comm_override'] = '1';
WPAM_Commission_Tracking::award_commission($args);
//$requestTracker->handleCheckoutWithRefKey($order_id, $purchaseAmount, $wpam_refkey, $buyer_email);
}
public function WooCommerceRefundTransaction($order_id) {
WPAM_Logger::log_debug('WooCommerce integration - order refunded. Order ID: ' . $order_id);
//$order = new WC_Order($order_id);
$txn_id = $order_id;
WPAM_Commission_Tracking::refund_commission($txn_id);
}
public function SimpleMembershipRefundTransaction($refund_data) {
//The array of Simple Membership's swpm_payment_refunded action (Stripe, PayPal webhooks and PayPal Standard).
if(!is_array($refund_data) || empty($refund_data['txn_id'])){
return;
}
WPAM_Logger::log_debug('Simple Membership integration - payment refunded. Transaction ID: ' . $refund_data['txn_id']);
$total_refunded = null;
$payment_amount = null;
if(empty($refund_data['is_full_refund'])){
//Partial refund: only that part of the commission is cancelled.
$total_refunded = isset($refund_data['total_refunded']) ? $refund_data['total_refunded'] : null;
$payment_amount = isset($refund_data['payment_amount']) ? $refund_data['payment_amount'] : null;
if(!is_numeric($total_refunded) || !is_numeric($payment_amount) || $payment_amount <= 0){
//The amount paid is not known (e.g. no Simple Membership transaction record), so the part to cancel cannot be calculated.
//Leave the commission unchanged rather than cancelling all of it for a partial refund.
WPAM_Logger::log_debug('Simple Membership integration - partial refund but the payment amount is not known. The commission was not changed. Transaction ID: ' . $refund_data['txn_id'], 3);
return;
}
}
//Find the commission by the transaction ID of the refunded payment first.
//checkout_txn_id is only set by Simple Membership for the first payment of a Stripe subscription. The commission of
//that payment was saved with the checkout's transaction ID (the customer ID), not the charge ID of the refund.
global $wpdb;
$query = "
SELECT COUNT(*)
FROM ".WPAM_TRANSACTIONS_TBL."
WHERE referenceId = %s
AND type = 'credit'
";
$txn_id = $refund_data['txn_id'];
if(!$wpdb->get_var($wpdb->prepare($query, $txn_id)) && !empty($refund_data['checkout_txn_id'])){
$count = (int) $wpdb->get_var($wpdb->prepare($query, $refund_data['checkout_txn_id']));
if($count != 1){
WPAM_Logger::log_debug('Simple Membership integration - ' . $count . ' commission records found for the checkout transaction ID: ' . $refund_data['checkout_txn_id'] . ' (expected 1). The commission was not changed. Transaction ID: ' . $txn_id, 2);
return;
}
WPAM_Logger::log_debug('Simple Membership integration - using the checkout transaction ID: ' . $refund_data['checkout_txn_id'] . ' to find the commission. Transaction ID: ' . $txn_id);
$txn_id = $refund_data['checkout_txn_id'];
}
WPAM_Commission_Tracking::refund_commission($txn_id, $total_refunded, $payment_amount);
}
public function jigoshopNewOrder($order_id) {
$order = new jigoshop_order($order_id);
$total = floatval($order->order_subtotal);
if ($order->order_discount) {
$total = $total - floatval($order->order_discount);
}
if ($total < 0) {
$total = 0;
}
$buyer_email = '';
WPAM_Logger::log_debug('JigoShop Integration - new order received. Order ID: ' . order_id . '. Purchase amt: ' . $total);
$requestTracker = new WPAM_Tracking_RequestTracker();
$requestTracker->handleCheckout($order_id, $total, $buyer_email);
}
public function edd_store_custom_fields($payment_meta) {
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - payment_meta filter triggered');
if (isset($_COOKIE['wpam_id'])) {
$strRefKey = $_COOKIE['wpam_id'];
$payment_meta['wpam_refkey'] = $strRefKey;
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - refkey: ' . $strRefKey);
} else if (isset($_COOKIE[WPAM_PluginConfig::$RefKey])) {
$strRefKey = $_COOKIE[WPAM_PluginConfig::$RefKey];
$payment_meta['wpam_refkey'] = $strRefKey;
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - refkey: ' . $strRefKey);
}
else{
if(get_option(WPAM_PluginConfig::$UseIPReferralTrack) == 1){
if(get_option(WPAM_PluginConfig::$AnonymizeIPClickTrack) == 1){
return $payment_meta;
}
$user_ip = WPAM_Click_Tracking::get_user_ip();
$aff_id = WPAM_Click_Tracking::get_referrer_id_from_ip_address_by_cookie_duration($user_ip);
if (!empty($aff_id)){
$payment_meta['wpam_refkey'] = $aff_id;
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - adding refkey: ' . $strRefKey.' using a fallback method');
}
}
}
return $payment_meta;
}
public function edd_on_complete_purchase($payment_id) {
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - complete purchase hook triggered for Order ID: ' . $payment_id . '. Checking if affiliate commission needs to be awarded.');
$payment_meta = edd_get_payment_meta($payment_id);
$strRefKey = "";
if (isset($payment_meta['wpam_refkey']) && !empty($payment_meta['wpam_refkey'])) {
$strRefKey = $payment_meta['wpam_refkey'];
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - This purchase was referred by an affiliate, refkey: ' . $strRefKey);
} else { //checking referral cookie since edd_payment_meta filter seems to be triggering after edd_complete_purchase hook as of version 3.0
if (isset($_COOKIE['wpam_id'])) {
$strRefKey = $_COOKIE['wpam_id'];
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - found refkey: ' . $strRefKey);
} else if (isset($_COOKIE[WPAM_PluginConfig::$RefKey])) {
$strRefKey = $_COOKIE[WPAM_PluginConfig::$RefKey];
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - found refkey: ' . $strRefKey);
}
else {
if(get_option(WPAM_PluginConfig::$UseIPReferralTrack) == 1){
if(get_option(WPAM_PluginConfig::$AnonymizeIPClickTrack) == 1){
return;
}
$user_ip = WPAM_Click_Tracking::get_user_ip();
$aff_id = WPAM_Click_Tracking::get_referrer_id_from_ip_address_by_cookie_duration($user_ip);
if (!empty($aff_id)){
$strRefKey = $aff_id;
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - found refkey: ' . $strRefKey.' using a fallback method');
}
}
}
if(empty($strRefKey)){
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - refkey not found in the payment_meta. This purchase was not referred by an affiliate');
return;
}
}
$purchaseAmount = edd_get_payment_amount($payment_id);
$buyer_email = $payment_meta['email'];
WPAM_Logger::log_debug('Easy Digital Downlaods Integration - Awarding commission for Order ID: ' . $payment_id . '. Purchase amt: ' . $purchaseAmount . ', Buyer Email: ' . $buyer_email);
$requestTracker = new WPAM_Tracking_RequestTracker();
$requestTracker->handleCheckoutWithRefKey($payment_id, $purchaseAmount, $strRefKey, $buyer_email);
}
public function onExchangeCheckout($transaction_id, $method, $method_id, $status, $customer_id, $cart_object, $args) {
$purchaseAmount = it_exchange_get_transaction_subtotal($transaction_id, false);
$buyer_email = '';
$requestTracker = new WPAM_Tracking_RequestTracker();
$requestTracker->handleCheckout($transaction_id, $purchaseAmount, $buyer_email);
return $transaction_id;
}
public function wpam_swpm_after_registration_callback($member_info) {
$create_aff_account = get_option(WPAM_PluginConfig::$AutoAffAccountSWPM);
if(!isset($create_aff_account) || empty($create_aff_account)){
return;
}
WPAM_Logger::log_debug('Simple Membership Auto Affiliate Creation - swpm_front_end_registration_complete_user_data action hook triggered');
WPAM_Logger::log_debug_array($member_info);
if(!isset($member_info['email']) || empty($member_info['email'])){
WPAM_Logger::log_debug("Simple Membership Auto Affiliate Creation - Error, email address is missing. Cannot create affiliate record!", 4);
return;
}
$member_email = sanitize_email($member_info['email']);
$user_info = get_user_by('email', $member_email); //get_userdata($user_id);
if(!$user_info){
WPAM_Logger::log_debug("Simple Membership Auto Affiliate Creation - user could not be found for email: ".$member_email.". Cannot create affiliate record!", 4);
return;
}
$fields = array();
if(get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption) == 1){
$fields['userId'] = $user_info->ID; //only set the user id when auto approval is enabled. Otherwise admin will not be able to approve and the plugin will show ERROR: User already has an account and is already an affiliate
}
$fields['firstName'] = sanitize_text_field($user_info->first_name);
$fields['lastName'] = sanitize_text_field($user_info->last_name);
$fields['email'] = sanitize_email($user_info->user_email);
$country = sanitize_text_field($member_info['country']);
if(isset($country) && !empty($country)){
$fields['addressCountry'] = $country;
}
$address = sanitize_text_field($member_info['address_street']);
if(isset($address) && !empty($address)){
$fields['addressLine1'] = $address;
}
$city = sanitize_text_field($member_info['address_city']);
if(isset($city) && !empty($city)){
$fields['addressCity'] = $city;
}
$state = sanitize_text_field($member_info['address_state']);
if(isset($state) && !empty($state)){
if(isset($country) && $country != "US"){
$state = "OU";
}
$fields['addressState'] = $state;
}
$zipcode = sanitize_text_field($member_info['address_zipcode']);
if(isset($zipcode) && !empty($zipcode)){
$fields['addressZipCode'] = $zipcode;
}
$phone = sanitize_text_field($member_info['phone']);
if(isset($phone) && !empty($phone)){
$fields['phoneNumber'] = $phone;
}
$userhandler = new WPAM_Util_UserHandler();
$userhandler->create_wpam_affiliate_record($fields);
WPAM_Logger::log_debug("Simple Membership Auto Affiliate Creation - affiliate record creation complete.");
}
public function onAdminMenu() {
//let the hackery begin! #63
global $menu;
$menu_parent_slug = 'wpam-affiliates';
//show this to affiliates, but not admins / affiliate managers
if (!current_user_can(WPAM_PluginConfig::$AdminCap) && current_user_can(WPAM_PluginConfig::$AffiliateCap)) {
//$icon_url = esc_url( self::$ICON_URL );
//I won't necessarily guarantee this will work in the future
$new_menu = array(
__('Affiliates', 'affiliates-manager'),
'read',
$this->affiliateHomePage->getLink(),
null,
'menu-top',
null,
'dashicons-groups',
);
$menu[] = $new_menu;
}
//show to non-affiliates
if (!current_user_can(WPAM_PluginConfig::$AffiliateCap) && !current_user_can(WPAM_PluginConfig::$AdminCap)) {
add_menu_page(
__('Affiliates', 'affiliates-manager'), __('Be An Affiliate', 'affiliates-manager'), 'read', 'newaffiliate', array($this, 'becomeAffiliate'), 'dashicons-groups'
);
}
//
$this->adminPages = array(
new WPAM_Pages_Admin_MyAffiliatesPage(
'wpam-affiliates', __('Affiliate Management', 'affiliates-manager'), __('Affiliates', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, array(
new WPAM_Pages_Admin_MyAffiliatesPage(
"wpam-affiliates", __('Affiliates', 'affiliates-manager'), __('My Affiliates', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap
),
new WPAM_Pages_Admin_NewAffiliatePage(
"wpam-newaffiliate", __('New Affiliate', 'affiliates-manager'), __('New Affiliate', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap
),
new WPAM_Pages_Admin_MyCreativesPage(
"wpam-creatives", __('Creatives', 'affiliates-manager'), __('My Creatives', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap
),
new WPAM_Pages_Admin_PaypalPaymentsPage(
"wpam-payments", __('PayPal Mass Pay', 'affiliates-manager'), __('PayPal Mass Pay', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap
)
)
)
);
//Add main affiliates menu object
add_menu_page(__('Affiliate Management', 'affiliates-manager'), __('Affiliates', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-affiliates', array(), 'dashicons-groups', '25.3');
$page = $this->adminPages[0];
//Add the child pages
$children = $page->getChildren();
//Add my affiliates submenu page
$childPage1 = $children[0];
add_submenu_page($page->getId(), $childPage1->getName(), $childPage1->getMenuName(), $childPage1->getRequiredCap(), $childPage1->getId(), array($childPage1, "process"));
//Add new affiliate submenu page
$childPage2 = $children[1];
add_submenu_page($page->getId(), $childPage2->getName(), $childPage2->getMenuName(), $childPage2->getRequiredCap(), $childPage2->getId(), array($childPage2, "process"));
//Add my creatives submenu page
$childPage3 = $children[2];
add_submenu_page($page->getId(), $childPage3->getName(), $childPage3->getMenuName(), $childPage3->getRequiredCap(), $childPage3->getId(), array($childPage3, "process"));
//Add paypal payments submenu page
$childPage4 = $children[3];
add_submenu_page($page->getId(), $childPage4->getName(), $childPage4->getMenuName(), $childPage4->getRequiredCap(), $childPage4->getId(), array($childPage4, "process"));
//Add settings submenu page
$settings_obj = new WPAM_Pages_Admin_SettingsPage();
add_submenu_page($menu_parent_slug, __('Settings', 'affiliates-manager'), __('Settings', 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-settings', array($settings_obj, 'render_settings_page'));
//Add admin functions submenu page
include_once(WPAM_BASE_DIRECTORY . "/source/Admin-menu/wpam-admin-functions-menu.php");
add_submenu_page($menu_parent_slug, __("Affiliates Manager Admin Functions", 'affiliates-manager'), __("Admin Functions", 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-admin-functions', 'wpam_display_admin_functions_menu');
//Add manage payouts submenu page
include_once(WPAM_BASE_DIRECTORY . "/source/Admin-menu/wpam-manage-payouts-menu.php");
add_submenu_page($menu_parent_slug, __("Affiliates Manager Manage Payouts", 'affiliates-manager'), __("Manage Payouts", 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-manage-payouts', 'wpam_display_manage_payouts_menu');
//Add clicks submenu page
include_once(WPAM_BASE_DIRECTORY . "/source/Admin-menu/wpam-clicks-menu.php");
add_submenu_page($menu_parent_slug, __("Affiliates Manager Click Tracking", 'affiliates-manager'), __("Click Tracking", 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-clicktracking', 'wpam_display_clicks_menu');
//Add commission submenu page
include_once(WPAM_BASE_DIRECTORY . "/source/Admin-menu/wpam-commission-menu.php");
add_submenu_page($menu_parent_slug, __("Affiliates Manager Commission Data", 'affiliates-manager'), __("Commissions", 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-commission', 'wpam_display_commission_menu');
//Add addons submenu page
include_once(WPAM_BASE_DIRECTORY . "/source/Admin-menu/wpam-addons-menu.php");
add_submenu_page($menu_parent_slug, __("Affiliates Manager Add-ons", 'affiliates-manager'), __("Add-ons", 'affiliates-manager'), WPAM_PluginConfig::$AdminCap, 'wpam-addons', 'wpam_display_addons_menu');
//Hook for addons to create their menu
do_action('wpam_after_main_admin_menu', $menu_parent_slug);
}
//for public pages
public function onTemplateRedirect() {
if (!is_array(self::$PUBLIC_PAGE_IDS)) {
self::$PUBLIC_PAGE_IDS = array(
$this->publicPages[WPAM_Plugin::PAGE_NAME_HOME]->getPageId(),
$this->publicPages[WPAM_Plugin::PAGE_NAME_REGISTER]->getPageId());
}
//get the current page
$page_id = NULL;
$page = get_page($page_id);
//register front-end scripts
if (isset($page->ID) && in_array($page->ID, self::$PUBLIC_PAGE_IDS)) {
//add jquery dialog + some style
$this->enqueueDialog();
wp_register_style('wpam_jquery_ui_theme', WPAM_URL . '/style/jquery-ui/smoothness/jquery-ui.css');
wp_enqueue_style('wpam_jquery_ui_theme');
wp_register_style('wpam_style', WPAM_URL . "/style/style.css");
wp_enqueue_style('wpam_style');
//#45 add a datepicker
wp_enqueue_script('jquery-ui-datepicker');
wp_register_script('wpam_contact_info', WPAM_URL . '/js/contact_info.js', array('jquery-ui-dialog'));
wp_register_script('wpam_tnc', WPAM_URL . '/js/tnc.js', array('jquery-ui-dialog'));
wp_register_script('wpam_payment_method', WPAM_URL . '/js/payment_method.js');
}
}
/**
* There's an upstream bug with JQuery UI Button that will probably be
* fixed in JQuery UI 1.9, so we need to override the default WP one until
* it's fixed and the fixed version is included in WP.
*
* @see http://bugs.jqueryui.com/ticket/7680
*/
private function enqueueDialog() {
//things seem to be working OK with dialog/button as of WP 3.4, so we'll just use the included version
wp_enqueue_script('jquery-ui-button');
wp_enqueue_script('jquery-ui-dialog');
}
//#79 sync email when it's actually changed
public function filterUserEmail($email) {
$user = wp_get_current_user();
$newEmail = get_option($user->ID . '_new_email');
if (!empty($newEmail) && isset($_GET['newuseremail'])) {
$db = new WPAM_Data_DataAccess();
$affiliate = $db->getAffiliateRepository()->loadByUserId($user->ID);
$affiliate->email = $email;
$db->getAffiliateRepository()->update($affiliate);
}
return $email;
}
/* update the affiliate email when it's updated in WordPress */
public function update_affiliate_email($user_id, $old_user_data) {
global $wpdb;
$table = WPAM_AFFILIATES_TBL;
$user_data = get_user_by('id', $user_id);
//WPAM_Logger::log_debug('profile_update hook fired. current email: '.$user_data->user_email.', old email: '.$old_user_data->user_email);
if(isset($user_data->user_email) && !empty($user_data->user_email) && $user_data->user_email !== $old_user_data->user_email) {
$wpdb->update($table, array('email' => $user_data->user_email), array('userId' => $user_id));
//WPAM_Logger::log_debug('email updated');
}
}
public function onSavePage($page_id, $page) {
if ($page->post_type == 'page') {
if (strpos($page->post_content, WPAM_PluginConfig::$ShortCodeHome) !== false) {
update_option(WPAM_PluginConfig::$HomePageId, $page->ID);
} elseif (strpos($page->post_content, WPAM_PluginConfig::$ShortCodeRegister) !== false) {
update_option(WPAM_PluginConfig::$RegPageId, $page->ID);
}
}
}
public function showAdminMessages() {
/* hide this error since the currency code and symbol comes from the general settings
if ( empty( $this->setloc ) ){
//don't bother showing this warning if they were trying to use 'en_US'
if ( $this->locale == 'en_US' ) {
return;
}
$code = WPAM_MoneyHelper::getCurrencyCode();
$currency = WPAM_MoneyHelper::getDollarSign();
echo '
' . sprintf( __( 'WP Affiliate Manager was unable to load your currency from your WPLANG setting: %s', 'affiliates-manager' ), $this->locale ) . ' ' .
sprintf( __( 'Your currency will be displayed as %s and PayPal payments will be paid in %s', 'affiliates-manager' ), $currency, $code ) . '
';
if ( WPAM_DEBUG ){
echo "';
}
}
*/
}
public function approveApplication() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'approveApplication':
$response = $jsonHandler->approveApplication($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function declineApplication() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'declineApplication':
$response = $jsonHandler->declineApplication($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function blockApplication() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'blockApplication':
$response = $jsonHandler->blockApplication($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function activateAffiliate() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'activateAffiliate':
$response = $jsonHandler->activateApplication($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function deactivateAffiliate() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'deactivateAffiliate':
$response = $jsonHandler->deactivateApplication($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function setCreativeStatus() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'setCreativeStatus':
$response = $jsonHandler->setCreativeStatus($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function addTransaction() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'addTransaction':
$response = $jsonHandler->addTransaction($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
public function deleteCreative() {
$jsonHandler = new WPAM_Util_JsonHandler();
$request = wpam_sanitize_array($_REQUEST);
try {
switch ($request['handler']) {
case 'deleteCreative':
$response = $jsonHandler->deleteCreative($request);
break;
default: throw new Exception(__('Invalid JSON handler.', 'affiliates-manager'));
}
} catch (Exception $e) {
$response = new JsonResponse(JsonResponse::STATUS_ERROR, $e->getMessage());
}
wp_die(json_encode($response)); //required to return a proper result
}
private function output_csv($items, $export_keys, $filename = 'data.csv') {
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=" . $filename);
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen('php://output', 'w'); //open output stream
$export_keys = $this->escape_csv_data_array($export_keys);
fputcsv($output, $export_keys); //let's put column names first
foreach ($items as $item) {
unset($csv_line);
foreach ($export_keys as $key => $value) {
if (isset($item[$key])) {
$csv_line[] = $item[$key];
}
}
$csv_line = $this->escape_csv_data_array($csv_line);
fputcsv($output, $csv_line);
}
}
public function escape_csv_data_array($data_array) {
$esc_data_arr = array('=', '+', '-', '@');
foreach($data_array as $index => $data){
if (in_array(mb_substr($data, 0, 1), $esc_data_arr, true)) {
$data = "'" . $data;
$data_array[$index] = $data;
}
}
return $data_array;
}
public function handle_csv_download() {
if (isset($_POST['wpam-export-affiliates-to-csv'])) {
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'wpam-export-affiliates-to-csv-nonce')) {
die(_e('Nonce check failed for export My Affiliates to CSV!', 'affiliates-manager'));
}
include_once(WPAM_BASE_DIRECTORY . '/classes/ListAffiliatesTable.php');
$affiliates = new WPAM_List_Affiliates_Table();
$affiliates->prepare_items(true);
$export_keys = array(
'affiliateId' => 'Affiliate ID',
'status' => 'Status',
'balance' => 'Balance',
'earnings' => 'Earnings',
'firstName' => 'First Name',
'lastName' => 'Last Name',
'email' => 'Email',
'companyName' => 'Company',
'dateCreated' => 'Date Joined',
'websiteUrl' => 'Website',
'phoneNumber' => 'Phone',
);
$export_keys = apply_filters('wpam_export_columns', $export_keys);
$this->output_csv($affiliates->items, $export_keys, 'MyAffiliates.csv');
die();
}
if (isset($_POST['wpam-export-aff-commissions-to-csv'])) {
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'wpam-export-aff-commissions-to-csv-nonce')) {
die(_e('Nonce check failed for export Affiliate Commissions to CSV!', 'affiliates-manager'));
}
include_once(WPAM_BASE_DIRECTORY . '/classes/ListCommissionTable.php');
$aff_commissions = new WPAM_List_Commission_Table();
$aff_commissions->prepare_items(true);
$export_keys = array(
'dateCreated' => 'Date',
'affiliateId' => 'Affiliate ID',
'amount' => 'Amount',
'referenceId' => 'Transaction ID',
'email' => 'Buyer Email',
);
$export_keys = apply_filters('wpam_export_aff_commission_columns', $export_keys);
$this->output_csv($aff_commissions->items, $export_keys, 'AffiliateCommissions.csv');
die();
}
}
}