/**
  * Updates the affiliate entry.
  * 
  * @param int $user_id
  * @param array $old_userdata
  */
 public static function profile_update($user_id, $old_userdata)
 {
     global $wpdb;
     // update affiliate entry
     $affiliate_ids = affiliates_get_user_affiliate($user_id);
     if (!empty($affiliate_ids)) {
         if ($affiliate_id = array_shift($affiliate_ids)) {
             if ($user = get_userdata($user_id)) {
                 $affiliates_table = _affiliates_get_tablename('affiliates');
                 $query = $wpdb->prepare("UPDATE {$affiliates_table} SET name = %s, email = %s WHERE affiliate_id = %d", $user->first_name . ' ' . $user->last_name, $user->user_email, intval($affiliate_id));
                 if ($wpdb->query($query)) {
                     do_action('affiliates_updated_affiliate', $affiliate_id);
                 }
             }
         }
     }
 }
 /**
  * Performs coupon checks after checkout validation.
  *
  * @param array $posted posted form data
  */
 public static function woocommerce_after_checkout_validation($posted)
 {
     global $woocommerce;
     if (isset($woocommerce->cart)) {
         $cart = $woocommerce->cart;
         if (!empty($cart->applied_coupons)) {
             if (method_exists('Affiliates_Attributes_WordPress', 'get_affiliate_for_coupon')) {
                 $valid = true;
                 $emails = array($posted['billing_email']);
                 if (is_user_logged_in()) {
                     $current_user = wp_get_current_user();
                     $emails[] = $current_user->user_email;
                 }
                 $emails = array_map('sanitize_email', array_map('strtolower', $emails));
                 self::remove_filters();
                 foreach ($cart->applied_coupons as $key => $code) {
                     $coupon = new WC_Coupon($code);
                     if (!is_wp_error($coupon->is_valid())) {
                         if ($affiliate_id = Affiliates_Attributes_WordPress::get_affiliate_for_coupon($coupon->code)) {
                             if ($user_id = get_current_user_id()) {
                                 if ($affiliate_ids = affiliates_get_user_affiliate($user_id)) {
                                     if (in_array($affiliate_id, $affiliate_ids)) {
                                         $valid = false;
                                         break;
                                     }
                                 }
                             }
                             if ($affiliate = affiliates_get_affiliate($affiliate_id)) {
                                 if (isset($affiliate['email']) && in_array(strtolower($affiliate['email']), $emails)) {
                                     $valid = false;
                                     break;
                                 }
                             }
                         }
                     }
                 }
                 self::add_filters();
                 if (!$valid) {
                     $coupon->add_coupon_message(WC_Coupon::E_WC_COUPON_INVALID_REMOVED);
                     unset($cart->applied_coupons[$key]);
                     $woocommerce->session->coupon_codes = $cart->applied_coupons;
                     $woocommerce->session->refresh_totals = true;
                 }
             }
         }
     }
 }
function affiliate_referrer_info($attr = array(), $content = null)
{
    global $wpdb;
    $affiliate_referrer = 1;
    $active_plugins = get_option('active_plugins', array());
    $affiliates_pro_is_active = in_array('affiliates-pro/affiliates-pro.php', $active_plugins);
    $affiliates_entr_is_active = in_array('affiliates-pro/affiliates-enterprise.php', $active_plugins);
    $options = shortcode_atts(array('direct' => false, 'display' => 'user_login'), $attr);
    extract($options);
    $output = '';
    $user_id = get_current_user_id();
    if ($affiliates_entr_is_active) {
        $relations_table = _affiliates_get_tablename('affiliates_relations');
        if ($user_id && affiliates_user_is_affiliate($user_id)) {
            if ($affiliate_ids = affiliates_get_user_affiliate($user_id)) {
                foreach ($affiliate_ids as $affiliate_id) {
                    if ($affiliate_referrer = $wpdb->get_var($wpdb->prepare("SELECT from_affiliate_id FROM {$relations_table} WHERE to_affiliate_id=%d ", $affiliate_id))) {
                        continue;
                    }
                }
            }
        }
    } else {
        if ($affiliates_pro_is_active) {
            $affiliate_referrers = get_option('affiliate_referrers');
            $relations = count($affiliate_referrers);
            if ($user_id && affiliates_user_is_affiliate($user_id)) {
                if ($affiliate_ids = affiliates_get_user_affiliate($user_id)) {
                    $affiliate_id = $affiliate_ids[0];
                    for ($i = 0; $i <= $relations; $i++) {
                        foreach ($affiliate_referrers[$i] as $key => $value) {
                            if ($affiliate_id == $value) {
                                $affiliate_referrer = $key;
                            }
                        }
                    }
                }
            }
        } else {
            echo "<div class='error'>The <strong>Affiliates Referrer Info</strong> plugin requires on of the Affiliates plugins by <a href='http://itthinx.com'>Itthinx</a> to be installed and activated.</div>";
        }
    }
    if ($user_id = affiliates_get_affiliate_user($affiliate_referrer)) {
        if ($user = get_user_by('id', $user_id)) {
            switch ($display) {
                case 'user_login':
                    $output .= $user->user_login;
                    break;
                case 'user_nicename':
                    $output .= $user->user_nicename;
                    break;
                case 'user_email':
                    $output .= $user->user_email;
                    break;
                case 'user_url':
                    $output .= $user->user_url;
                    break;
                case 'display_name':
                    $output .= $user->display_name;
                    break;
                default:
                    $output .= $user->user_login;
            }
            $output = wp_strip_all_tags($output);
        }
    }
    return $output;
}
 /**
  * Shows monthly earnings.
  * 
  * Note that we don't do any s2u or u2s date adjustments here.
  * 
  * @param array $atts not used; this shortcode does not accept any arguments
  * @param string $content not used
  */
 public static function affiliates_earnings($atts, $content = null)
 {
     global $wpdb;
     $output = '';
     $user_id = get_current_user_id();
     if ($user_id && affiliates_user_is_affiliate($user_id)) {
         if ($affiliate_ids = affiliates_get_user_affiliate($user_id)) {
             $output .= '<table class="affiliates-earnings">';
             $output .= '<thead>';
             $output .= '<tr>';
             $output .= '<th>';
             $output .= __('Month', AFFILIATES_PLUGIN_DOMAIN);
             $output .= '</th>';
             $output .= '<th>';
             $output .= __('Earnings', AFFILIATES_PLUGIN_DOMAIN);
             $output .= '</th>';
             $output .= '</tr>';
             $output .= '</thead>';
             $output .= '<tbody>';
             $referrals_table = _affiliates_get_tablename('referrals');
             if ($range = $wpdb->get_row("SELECT MIN(datetime) from_datetime, MAX(datetime) thru_datetime FROM {$referrals_table} WHERE affiliate_id IN (" . implode(',', $affiliate_ids) . ")")) {
                 if (!empty($range->from_datetime)) {
                     // Covers for NULL when no referrals recorded yet, too.
                     $t = strtotime($range->from_datetime);
                     $eom = strtotime(date('Y-m-t 23:59:59', time()));
                     while ($t < $eom) {
                         $from = date('Y-m', $t) . '-01 00:00:00';
                         $thru = date('Y-m-t', strtotime($from));
                         $sums = array();
                         foreach ($affiliate_ids as $affiliate_id) {
                             if ($totals = self::get_total($affiliate_id, $from, $thru)) {
                                 if (count($totals) > 0) {
                                     foreach ($totals as $currency_id => $total) {
                                         $sums[$currency_id] = isset($sums[$currency_id]) ? bcadd($sums[$currency_id], $total, AFFILIATES_REFERRAL_AMOUNT_DECIMALS) : $total;
                                     }
                                 }
                             }
                         }
                         $output .= '<tr>';
                         // month & year
                         $output .= '<td>';
                         $output .= date_i18n(__('F Y', AFFILIATES_PLUGIN_DOMAIN), strtotime($from));
                         // translators: date format; month and year for earnings display
                         $output .= '</td>';
                         // earnings
                         $output .= '<td>';
                         if (count($sums) > 1) {
                             $output .= '<ul>';
                             foreach ($sums as $currency_id => $total) {
                                 $output .= '<li>';
                                 $output .= apply_filters('affiliates_earnings_display_currency', $currency_id);
                                 $output .= '&nbsp;';
                                 $output .= apply_filters('affiliates_earnings_display_total', number_format_i18n($total, apply_filters('affiliates_earnings_decimals', 2)), $total, $currency_id);
                                 $output .= '</li>';
                             }
                             $output .= '</ul>';
                         } else {
                             if (count($sums) > 0) {
                                 $output .= apply_filters('affiliates_earnings_display_currency', $currency_id);
                                 $output .= '&nbsp;';
                                 $output .= apply_filters('affiliates_earnings_display_total', number_format_i18n($total, apply_filters('affiliates_earnings_decimals', 2)), $total, $currency_id);
                             } else {
                                 $output .= apply_filters('affiliates_earnings_display_total_none', __('None', AFFILIATES_PLUGIN_DOMAIN));
                             }
                         }
                         $output .= '</td>';
                         $output .= '</tr>';
                         $t = strtotime('+1 month', $t);
                     }
                 } else {
                     $output .= '<td colspan="2">';
                     $output .= apply_filters('affiliates_earnings_display_total_no_earnings', __('There are no earnings yet.', AFFILIATES_PLUGIN_DOMAIN));
                     $output .= '</td>';
                 }
             }
             $output .= '</tbody>';
             $output .= '</table>';
         }
     }
     return $output;
 }
 /**
  * Updates an affiliate user.
  *
  * @access private
  * @param array $userdata
  * @return int|WP_Error Either user's ID or error on failure.
  */
 public static function update_affiliate_user($user_id, $userdata)
 {
     global $wpdb, $affiliates_update_affiliate_user;
     if (!isset($affiliates_update_affiliate_user)) {
         $affiliates_update_affiliate_user = true;
     } else {
         return;
     }
     $errors = new WP_Error();
     if (($user = get_user_by('id', $user_id)) && affiliates_user_is_affiliate($user_id)) {
         $new_password = false;
         $user_email = apply_filters('user_registration_email', $userdata['user_email']);
         // Check the e-mail address
         if ($user_email == '') {
             $errors->add('empty_email', __('<strong>ERROR</strong>: Please type your e-mail address.', AFFILIATES_PLUGIN_DOMAIN));
         } elseif (!is_email($user_email)) {
             $errors->add('invalid_email', __('<strong>ERROR</strong>: The email address isn&#8217;t correct.', AFFILIATES_PLUGIN_DOMAIN));
             $user_email = '';
         } elseif ($other_user_id = email_exists($user_email)) {
             if ($other_user_id != $user_id) {
                 $errors->add('email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.', AFFILIATES_PLUGIN_DOMAIN));
             }
         }
         if ($errors->get_error_code()) {
             return $errors;
         }
         $userdata['first_name'] = sanitize_text_field($userdata['first_name']);
         $userdata['last_name'] = sanitize_text_field($userdata['last_name']);
         $userdata['user_email'] = $user_email;
         if (!empty($userdata['user_url'])) {
             $userdata['user_url'] = esc_url_raw($userdata['user_url']);
             $userdata['user_url'] = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $userdata['user_url']) ? $userdata['user_url'] : 'http://' . $userdata['user_url'];
         }
         // update affiliate user and affiliate entry
         $_userdata = array('ID' => $user_id, 'first_name' => esc_sql($userdata['first_name']), 'last_name' => esc_sql($userdata['last_name']), 'user_email' => esc_sql($userdata['user_email']));
         if (!empty($userdata['password'])) {
             // Don't pass the new password through wp_update_user as we risk
             // sending headers again when wp_update_user() clears the cookies while
             // the new password is set. Instead, we set the new password here and
             // only update the other user data below.
             $new_password = true;
             wp_set_password($userdata['password'], $user_id);
         }
         if (isset($userdata['user_url'])) {
             $_userdata['user_url'] = esc_sql($userdata['user_url']);
         }
         // update user and affiliate entry
         $user_id = wp_update_user($_userdata);
         // if WP_Error it's returned below
         if (!is_wp_error($user_id)) {
             // add user meta from remaining fields
             foreach ($userdata as $meta_key => $meta_value) {
                 if (!key_exists($meta_key, $_userdata) && !in_array($meta_key, self::$skip_meta_fields)) {
                     update_user_meta($user_id, $meta_key, maybe_unserialize($meta_value));
                 }
             }
             // update affiliate entry
             $affiliate_ids = affiliates_get_user_affiliate($user_id);
             if ($affiliate_id = array_shift($affiliate_ids)) {
                 $affiliates_table = _affiliates_get_tablename('affiliates');
                 $query = $wpdb->prepare("UPDATE {$affiliates_table} SET name = %s, email = %s WHERE affiliate_id = %d", $_userdata['first_name'] . ' ' . $_userdata['last_name'], $_userdata['user_email'], intval($affiliate_id));
                 if ($wpdb->query($query)) {
                     do_action('affiliates_updated_affiliate', $affiliate_id);
                 }
             }
             // @todo headers already sent notices with WC when password is changed ... for example
             // wp_woocommerce_session_... cookie cannot be set - headers already sent by .../wp-content/themes/twentytwelve/header.php on line 13 in .../wp-content/plugins/woocommerce/includes/wc-core-functions.php on line 469
             // below doesn't solve this and we don't want to force logout/login on password change anyway
             // 				if ( $new_password ) {
             // 					$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
             // 					wp_redirect( wp_login_url( $current_url ) );
             // 					exit;
             // 				}
         }
     }
     return $user_id;
 }