/**
 * Show edit affiliate form.
 * @param int $affiliate_id affiliate id
 */
function affiliates_admin_affiliates_edit($affiliate_id)
{
    global $wpdb;
    if (!current_user_can(AFFILIATES_ADMINISTER_AFFILIATES)) {
        wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliate = affiliates_get_affiliate(intval($affiliate_id));
    if (empty($affiliate)) {
        wp_die(__('No such affiliate.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliates_users_table = _affiliates_get_tablename('affiliates_users');
    $affiliate_user = null;
    $affiliate_user_edit = '';
    $affiliate_user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$affiliates_users_table} WHERE affiliate_id = %d", intval($affiliate_id)));
    if ($affiliate_user_id !== null) {
        $affiliate_user = get_user_by('id', intval($affiliate_user_id));
        if ($affiliate_user) {
            if (current_user_can('edit_user', $affiliate_user->ID)) {
                $affiliate_user_edit = sprintf(__('Edit %s', AFFILIATES_PLUGIN_DOMAIN), '<a target="_blank" href="' . esc_url("user-edit.php?user_id={$affiliate_user->ID}") . '">' . $affiliate_user->user_login . '</a>');
            }
        }
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('affiliate_id', $current_url);
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : $affiliate['name'];
    $email = isset($_POST['email-field']) ? $_POST['email-field'] : $affiliate['email'];
    $user_login = isset($_POST['user-field']) ? $_POST['user-field'] : ($affiliate_user != null ? $affiliate_user->user_login : '');
    $from_date = isset($_POST['from-date-field']) ? $_POST['from-date-field'] : $affiliate['from_date'];
    $thru_date = isset($_POST['thru-date-field']) ? $_POST['thru-date-field'] : $affiliate['thru_date'];
    $output = '<div class="manage-affiliates">' . '<div>' . '<h2>' . __('Edit an affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</h2>' . '</div>' . '<form id="edit-affiliate" action="' . $current_url . '" method="post">' . '<div class="affiliate edit">' . '<input id="affiliate-id-field" name="affiliate-id-field" type="hidden" value="' . esc_attr(intval($affiliate_id)) . '"/>' . '<div class="field">' . '<label for="name-field" class="field-label first required">' . __('Name', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr($name) . '"/>' . '</div>' . '<div class="field">' . '<label for="email-field" class="field-label">' . __('Email', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input id="email-field" name="email-field" class="emailfield" type="text" value="' . esc_attr($email) . '"/>' . '<span class="description">' . __("If a valid <strong>Username</strong> is specified and no email is given, the user's email address will be used automatically.", AFFILIATES_PLUGIN_DOMAIN) . '</span>' . '</div>' . '<div class="field">' . '<label for="user-field" class="field-label">' . __('Username', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input id="user-field" name="user-field" class="userfield" type="text" value="' . esc_attr($user_login) . '"/>' . $affiliate_user_edit . '</div>' . '<div class="field">' . '<label for="from-date-field" class="field-label first">' . __('From', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input id="from-date-field" name="from-date-field" class="datefield" type="text" value="' . esc_attr($from_date) . '"/>' . '</div>' . '<div class="field">' . '<label for="thru-date-field" class="field-label">' . __('Until', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input id="thru-date-field" name="thru-date-field" class="datefield" type="text" value="' . esc_attr($thru_date) . '"/>' . '</div>' . '<div class="field">' . wp_nonce_field('affiliates-edit', AFFILIATES_ADMIN_AFFILIATES_NONCE, true, false) . '<input class="button" type="submit" value="' . __('Save', AFFILIATES_PLUGIN_DOMAIN) . '"/>' . '<input type="hidden" value="edit" name="action"/>' . '<a class="cancel" href="' . $current_url . '">' . __('Cancel', AFFILIATES_PLUGIN_DOMAIN) . '</a>' . '</div>' . '</div>' . '</form>' . '</div>';
    // .manage-affiliates
    echo $output;
    affiliates_footer();
}
/**
 * Show form to remove an affiliate.
 * @param int $affiliate_id affiliate id
 */
function affiliates_admin_affiliates_remove($affiliate_id)
{
    global $wpdb;
    if (!current_user_can(AFFILIATES_ADMINISTER_AFFILIATES)) {
        wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliate = affiliates_get_affiliate(intval($affiliate_id));
    if (empty($affiliate)) {
        wp_die(__('No such affiliate.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliates_users_table = _affiliates_get_tablename('affiliates_users');
    $affiliate_user = null;
    $affiliate_user_edit = '';
    $affiliate_user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$affiliates_users_table} WHERE affiliate_id = %d", intval($affiliate_id)));
    if ($affiliate_user_id !== null) {
        $affiliate_user = get_user_by('id', intval($affiliate_user_id));
        if ($affiliate_user) {
            if (current_user_can('edit_user', $affiliate_user->ID)) {
                $affiliate_user_edit = sprintf(__('Edit %s', AFFILIATES_PLUGIN_DOMAIN), '<a target="_blank" href="' . esc_url("user-edit.php?user_id={$affiliate_user->ID}") . '">' . $affiliate_user->user_login . '</a>');
            } else {
                $affiliate_user_edit = $affiliate_user->user_login;
            }
        }
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('affiliate_id', $current_url);
    $output = '<div class="manage-affiliates">' . '<div>' . '<h2>' . __('Remove an affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</h2>' . '</div>' . '<form id="remove-affiliate" action="' . $current_url . '" method="post">' . '<div class="affiliate remove">' . '<input id="affiliate-id-field" name="affiliate-id-field" type="hidden" value="' . esc_attr(intval($affiliate_id)) . '"/>' . '<ul>' . '<li>' . sprintf(__('Name : %s', AFFILIATES_PLUGIN_DOMAIN), wp_filter_kses($affiliate['name'])) . '</li>' . '<li>' . sprintf(__('Email : %s', AFFILIATES_PLUGIN_DOMAIN), wp_filter_kses($affiliate['email'])) . '</li>' . '<li>' . sprintf(__('Username : %s', AFFILIATES_PLUGIN_DOMAIN), wp_filter_kses($affiliate_user_edit)) . '</li>' . '<li>' . sprintf(__('From : %s', AFFILIATES_PLUGIN_DOMAIN), wp_filter_kses($affiliate['from_date'])) . '</li>' . '<li>' . sprintf(__('Until : %s', AFFILIATES_PLUGIN_DOMAIN), wp_filter_kses($affiliate['from_date'])) . '</li>' . '</ul> ' . wp_nonce_field('affiliates-remove', AFFILIATES_ADMIN_AFFILIATES_NONCE, true, false) . '<input class="button" type="submit" value="' . __('Remove', AFFILIATES_PLUGIN_DOMAIN) . '"/>' . '<input type="hidden" value="remove" name="action"/>' . '<a class="cancel" href="' . $current_url . '">' . __('Cancel', AFFILIATES_PLUGIN_DOMAIN) . '</a>' . '</div>' . '</div>' . '</form>' . '</div>';
    // .manage-affiliates
    echo $output;
    affiliates_footer();
}
 /**
  * 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 affiliates_admin_hits_affiliate()
{
    global $wpdb, $affiliates_options;
    $output = '';
    if (!current_user_can(AFFILIATES_ACCESS_AFFILIATES)) {
        wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
    }
    if (isset($_POST['from_date']) || isset($_POST['thru_date']) || isset($_POST['clear_filters']) || isset($_POST['affiliate_id']) || isset($_POST['expanded']) || isset($_POST['expanded_hits']) || isset($_POST['expanded_referrals']) || isset($_POST['show_inoperative'])) {
        if (!wp_verify_nonce($_POST[AFFILIATES_ADMIN_HITS_AFF_FILTER_NONCE], 'admin')) {
            wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
        }
    }
    // filters
    $from_date = $affiliates_options->get_option('hits_affiliate_from_date', null);
    $thru_date = $affiliates_options->get_option('hits_affiliate_thru_date', null);
    $affiliate_id = $affiliates_options->get_option('hits_affiliate_affiliate_id', null);
    $expanded = $affiliates_options->get_option('hits_affiliate_expanded', null);
    // @todo input ist not shown, eventually remove unless ...
    $expanded_referrals = $affiliates_options->get_option('hits_affiliate_expanded_referrals', null);
    $expanded_hits = $affiliates_options->get_option('hits_affiliate_expanded_hits', null);
    $show_inoperative = $affiliates_options->get_option('hits_affiliate_show_inoperative', null);
    if (isset($_POST['clear_filters'])) {
        $affiliates_options->delete_option('hits_affiliate_from_date');
        $affiliates_options->delete_option('hits_affiliate_thru_date');
        $affiliates_options->delete_option('hits_affiliate_affiliate_id');
        $affiliates_options->delete_option('hits_affiliate_expanded');
        $affiliates_options->delete_option('hits_affiliate_expanded_referrals');
        $affiliates_options->delete_option('hits_affiliate_expanded_hits');
        $affiliates_options->delete_option('hits_affiliate_show_inoperative');
        $from_date = null;
        $thru_date = null;
        $affiliate_id = null;
        $expanded = null;
        $expanded_hits = null;
        $expanded_referrals = null;
        $show_inoperative = null;
    } else {
        if (isset($_POST['submitted'])) {
            // filter by date(s)
            if (!empty($_POST['from_date'])) {
                $from_date = date('Y-m-d', strtotime($_POST['from_date']));
                $affiliates_options->update_option('hits_affiliate_from_date', $from_date);
            } else {
                $from_date = null;
                $affiliates_options->delete_option('hits_affiliate_from_date');
            }
            if (!empty($_POST['thru_date'])) {
                $thru_date = date('Y-m-d', strtotime($_POST['thru_date']));
                $affiliates_options->update_option('hits_affiliate_thru_date', $thru_date);
            } else {
                $thru_date = null;
                $affiliates_options->delete_option('hits_affiliate_thru_date');
            }
            if ($from_date && $thru_date) {
                if (strtotime($from_date) > strtotime($thru_date)) {
                    $thru_date = null;
                    $affiliates_options->delete_option('hits_affiliate_thru_date');
                }
            }
            // filter by affiliate id
            if (!empty($_POST['affiliate_id'])) {
                $affiliate_id = affiliates_check_affiliate_id($_POST['affiliate_id']);
                if ($affiliate_id) {
                    $affiliates_options->update_option('hits_affiliate_affiliate_id', $affiliate_id);
                }
            } else {
                if (isset($_POST['affiliate_id'])) {
                    // empty && isset => '' => all
                    $affiliate_id = null;
                    $affiliates_options->delete_option('hits_affiliate_affiliate_id');
                }
            }
            // expanded details?
            if (!empty($_POST['expanded'])) {
                $expanded = true;
                $affiliates_options->update_option('hits_affiliate_expanded', true);
            } else {
                $expanded = false;
                $affiliates_options->delete_option('hits_affiliate_expanded');
            }
            if (!empty($_POST['expanded_hits'])) {
                $expanded_hits = true;
                $affiliates_options->update_option('hits_affiliate_expanded_hits', true);
            } else {
                $expanded_hits = false;
                $affiliates_options->delete_option('hits_affiliate_expanded_hits');
            }
            if (!empty($_POST['expanded_referrals'])) {
                $expanded_referrals = true;
                $affiliates_options->update_option('hits_affiliate_expanded_referrals', true);
            } else {
                $expanded_referrals = false;
                $affiliates_options->delete_option('hits_affiliate_expanded_referrals');
            }
            if (!empty($_POST['show_inoperative'])) {
                $show_inoperative = true;
                $affiliates_options->update_option('hits_affiliate_show_inoperative', true);
            } else {
                $show_inoperative = false;
                $affiliates_options->delete_option('hits_affiliate_show_inoperative');
            }
        }
    }
    if (isset($_POST['row_count'])) {
        if (!wp_verify_nonce($_POST[AFFILIATES_ADMIN_HITS_AFF_NONCE_1], 'admin')) {
            wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
        }
    }
    if (isset($_POST['paged'])) {
        if (!wp_verify_nonce($_POST[AFFILIATES_ADMIN_HITS_AFF_NONCE_2], 'admin')) {
            wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
        }
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('paged', $current_url);
    $affiliates_table = _affiliates_get_tablename('affiliates');
    $referrals_table = _affiliates_get_tablename('referrals');
    $hits_table = _affiliates_get_tablename('hits');
    $output .= '<div>' . '<h2>' . __('Affiliates & Referrals', AFFILIATES_PLUGIN_DOMAIN) . '</h2>' . '</div>';
    $row_count = isset($_POST['row_count']) ? intval($_POST['row_count']) : 0;
    if ($row_count <= 0) {
        $row_count = $affiliates_options->get_option('hits_affiliate_per_page', AFFILIATES_HITS_AFFILIATE_PER_PAGE);
    } else {
        $affiliates_options->update_option('hits_affiliate_per_page', $row_count);
    }
    $offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
    if ($offset < 0) {
        $offset = 0;
    }
    $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 0;
    if ($paged < 0) {
        $paged = 0;
    }
    $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : null;
    switch ($orderby) {
        case 'date':
        case 'visits':
        case 'hits':
        case 'referrals':
        case 'ratio':
        case 'name':
            break;
        default:
            $orderby = 'name';
    }
    $order = isset($_GET['order']) ? $_GET['order'] : null;
    switch ($order) {
        case 'asc':
        case 'ASC':
            $switch_order = 'DESC';
            break;
        case 'desc':
        case 'DESC':
            $switch_order = 'ASC';
            break;
        default:
            $order = 'ASC';
            $switch_order = 'DESC';
    }
    if ($from_date || $thru_date || $affiliate_id) {
        $filters = " WHERE ";
    } else {
        $filters = '';
    }
    $filter_params = array();
    // We now have the desired dates from the user's point of view, i.e. in her timezone.
    // If supported, adjust the dates for the site's timezone:
    if ($from_date) {
        $from_datetime = DateHelper::u2s($from_date);
    }
    if ($thru_date) {
        $thru_datetime = DateHelper::u2s($thru_date, 24 * 3600);
    }
    if ($from_date && $thru_date) {
        $filters .= " datetime >= %s AND datetime < %s ";
        $filter_params[] = $from_datetime;
        $filter_params[] = $thru_datetime;
    } else {
        if ($from_date) {
            $filters .= " datetime >= %s ";
            $filter_params[] = $from_datetime;
        } else {
            if ($thru_date) {
                $filters .= " datetime < %s ";
                $filter_params[] = $thru_datetime;
            }
        }
    }
    if ($affiliate_id) {
        if ($from_date || $thru_date) {
            $filters .= " AND ";
        }
        $filters .= " h.affiliate_id = %d ";
        $filter_params[] = $affiliate_id;
    }
    // how many are there ?
    $count_query = $wpdb->prepare("SELECT affiliate_id FROM {$hits_table} h\n\t\t{$filters}\n\t\tGROUP BY affiliate_id\n\t\t", $filter_params);
    $wpdb->query($count_query);
    $count = $wpdb->num_rows;
    if ($count > $row_count) {
        $paginate = true;
    } else {
        $paginate = false;
    }
    $pages = ceil($count / $row_count);
    if ($paged > $pages) {
        $paged = $pages;
    }
    if ($paged != 0) {
        $offset = ($paged - 1) * $row_count;
    }
    // Get the summarized results, these are grouped by date.
    // Note: Referrals on dates without a hit will not be included.
    // @see notes about this in affiliates_admin_hits()
    $date_condition = "";
    if ($from_date && $thru_date) {
        $date_condition = " AND datetime >= '" . $from_datetime . "' AND datetime < '" . $thru_datetime . "' ";
    } else {
        if ($from_date) {
            $date_condition = " AND datetime >= '" . $from_datetime . "' ";
        } else {
            if ($thru_date) {
                $date_condition = " AND datetime < '" . $thru_datetime . "' ";
            }
        }
    }
    $query = $wpdb->prepare("\n\t\t\tSELECT\n\t\t\t\t*,\n\t\t\t\tcount(distinct ip) visits,\n\t\t\t\tsum(count) hits,\n\t\t\t\t(select count(*) from {$referrals_table} where affiliate_id = h.affiliate_id {$date_condition} ) referrals,\n\t\t\t\t((select count(*) from {$referrals_table} where affiliate_id = h.affiliate_id {$date_condition} )/count(distinct ip)) ratio\n\t\t\tFROM {$hits_table} h\n\t\t\tLEFT JOIN {$affiliates_table} a ON h.affiliate_id = a.affiliate_id\n\t\t\t{$filters}\n\t\t\tGROUP BY h.affiliate_id\n\t\t\tORDER BY {$orderby} {$order}\n\t\t\tLIMIT {$row_count} OFFSET {$offset}\n\t\t\t", $filter_params);
    $results = $wpdb->get_results($query, OBJECT);
    $column_display_names = array('name' => __('Affiliate', AFFILIATES_PLUGIN_DOMAIN), 'visits' => __('Visitors', AFFILIATES_PLUGIN_DOMAIN), 'hits' => __('Hits', AFFILIATES_PLUGIN_DOMAIN), 'referrals' => __('Referrals', AFFILIATES_PLUGIN_DOMAIN), 'ratio' => __('Ratio', AFFILIATES_PLUGIN_DOMAIN));
    $output .= '<div id="" class="hits-affiliates-overview">';
    $affiliates = affiliates_get_affiliates(true, !$show_inoperative);
    $affiliates_select = '';
    if (!empty($affiliates)) {
        $affiliates_select .= '<label class="affiliate-id-filter" for="affiliate_id">' . __('Affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</label>';
        $affiliates_select .= '<select class="affiliate-id-filter" name="affiliate_id">';
        $affiliates_select .= '<option value="">--</option>';
        foreach ($affiliates as $affiliate) {
            if ($affiliate_id == $affiliate['affiliate_id']) {
                $selected = ' selected="selected" ';
            } else {
                $selected = '';
            }
            $affiliates_select .= '<option ' . $selected . ' value="' . esc_attr($affiliate['affiliate_id']) . '">' . esc_attr(stripslashes($affiliate['name'])) . '</option>';
        }
        $affiliates_select .= '</select>';
    }
    $output .= '<div class="filters">' . '<label class="description" for="setfilters">' . __('Filters', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<form id="setfilters" action="" method="post">' . '<p>' . $affiliates_select . '</p>
				<p>' . '<label class="from-date-filter" for="from_date">' . __('From', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input class="datefield from-date-filter" name="from_date" type="text" value="' . esc_attr($from_date) . '"/>' . '<label class="thru-date-filter" for="thru_date">' . __('Until', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input class="datefield thru-date-filter" name="thru_date" type="text" class="datefield" value="' . esc_attr($thru_date) . '"/>' . '</p>
				<p>' . wp_nonce_field('admin', AFFILIATES_ADMIN_HITS_AFF_FILTER_NONCE, true, false) . '<input class="button" type="submit" value="' . __('Apply', AFFILIATES_PLUGIN_DOMAIN) . '"/>' . '<label class="expanded-filter">' . '<input class="expanded-filter" name="expanded_referrals" type="checkbox" ' . ($expanded_referrals ? 'checked="checked"' : '') . '/>' . ' ' . __('Expand referrals', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<label class="expanded-filter">' . '<input class="expanded-filter" name="expanded_hits" type="checkbox" ' . ($expanded_hits ? 'checked="checked"' : '') . '/>' . ' ' . __('Expand hits', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<label class="show-inoperative-filter">' . '<input class="show-inoperative-filter" name="show_inoperative" type="checkbox" ' . ($show_inoperative ? 'checked="checked"' : '') . '/>' . ' ' . __('Include inoperative affiliates', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input class="button" type="submit" name="clear_filters" value="' . __('Clear', AFFILIATES_PLUGIN_DOMAIN) . '"/>' . '<input type="hidden" value="submitted" name="submitted"/>' . '</p>' . '</form>' . '</div>';
    $output .= '
		<div class="page-options">
			<form id="setrowcount" action="" method="post">
				<div>
					<label for="row_count">' . __('Results per page', AFFILIATES_PLUGIN_DOMAIN) . '</label>' . '<input name="row_count" type="text" size="2" value="' . esc_attr($row_count) . '" />
					' . wp_nonce_field('admin', AFFILIATES_ADMIN_HITS_AFF_NONCE_1, true, false) . '
					<input class="button" type="submit" value="' . __('Apply', AFFILIATES_PLUGIN_DOMAIN) . '"/>
				</div>
			</form>
		</div>
		';
    if ($paginate) {
        require_once AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php';
        $pagination = new Affiliates_Pagination($count, null, $row_count);
        $output .= '<form id="posts-filter" method="post" action="">';
        $output .= '<div>';
        $output .= wp_nonce_field('admin', AFFILIATES_ADMIN_HITS_AFF_NONCE_2, true, false);
        $output .= '</div>';
        $output .= '<div class="tablenav top">';
        $output .= $pagination->pagination('top');
        $output .= '</div>';
        $output .= '</form>';
    }
    $output .= '
		<table id="" class="wp-list-table widefat fixed" cellspacing="0">
		<thead>
			<tr>
			';
    foreach ($column_display_names as $key => $column_display_name) {
        $options = array('orderby' => $key, 'order' => $switch_order);
        $class = "";
        if (strcmp($key, $orderby) == 0) {
            $lorder = strtolower($order);
            $class = "{$key} manage-column sorted {$lorder}";
        } else {
            $class = "{$key} manage-column sortable";
        }
        $column_display_name = '<a href="' . esc_url(add_query_arg($options, $current_url)) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
        $output .= "<th scope='col' class='{$class}'>{$column_display_name}</th>";
    }
    $output .= '</tr>
		</thead>
		<tbody>
		';
    if (count($results) > 0) {
        for ($i = 0; $i < count($results); $i++) {
            $result = $results[$i];
            $output .= '<tr class=" ' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
            $affiliate = affiliates_get_affiliate($result->affiliate_id);
            $output .= "<td class='affiliate-name'>" . stripslashes(wp_filter_nohtml_kses($affiliate['name'])) . "</td>";
            $output .= "<td class='visits'>{$result->visits}</td>";
            $output .= "<td class='hits'>{$result->hits}</td>";
            $output .= "<td class='referrals'>{$result->referrals}</td>";
            $output .= "<td class='ratio'>{$result->ratio}</td>";
            $output .= '</tr>';
            if ($expanded || $expanded_referrals || $expanded_hits) {
                //
                // expanded : referrals ----------------------------------------
                //
                if ($expanded_referrals) {
                    // get the detailed results for referrals
                    $referrals_filters = " WHERE r.affiliate_id = %d ";
                    $referrals_filter_params = array($result->affiliate_id);
                    if ($from_date && $thru_date) {
                        $referrals_filters .= " AND datetime >= %s AND datetime < %s ";
                        $referrals_filter_params[] = $from_datetime;
                        $referrals_filter_params[] = $thru_datetime;
                    } else {
                        if ($from_date) {
                            $referrals_filters .= " AND datetime >= %s ";
                            $referrals_filter_params[] = $from_datetime;
                        } else {
                            if ($thru_date) {
                                $referrals_filters .= " datetime < %s ";
                                $referrals_filter_params[] = $thru_datetime;
                            }
                        }
                    }
                    $referrals_orderby = "datetime {$order}";
                    $referrals_query = $wpdb->prepare("SELECT *\n\t\t\t\t\t\tFROM {$referrals_table} r\n\t\t\t\t\t\tLEFT JOIN {$affiliates_table} a ON r.affiliate_id = a.affiliate_id\n\t\t\t\t\t\t{$referrals_filters}\n\t\t\t\t\t\tORDER BY {$referrals_orderby}\n\t\t\t\t\t\t", $referrals_filter_params);
                    $referrals = $wpdb->get_results($referrals_query, OBJECT);
                    if (count($referrals) > 0) {
                        $output .= '<tr class=" ' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
                        $output .= '<td colspan="5">';
                        $output .= '<div class="details-referrals">';
                        $output .= '<p class="description">' . __('Referrals', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
                        $output .= '
							<table id="details-referrals-' . esc_attr($result->date) . '" class="details-referrals" cellspacing="0">
							<thead>
							<tr>
							<th scope="col" class="datetime">' . __('Time', AFFILIATES_PLUGIN_DOMAIN) . '</th>
							<th scope="col" class="post-id">' . __('Post', AFFILIATES_PLUGIN_DOMAIN) . '</th>
							<th scope="col" class="affiliate-id">' . __('Affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</th>
							</tr>
							</thead>
							<tbody>
							';
                        foreach ($referrals as $referral) {
                            $output .= '<tr class="details-referrals ' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
                            $output .= "<td class='datetime'>" . DateHelper::s2u($referral->datetime) . "</td>";
                            $link = get_permalink($referral->post_id);
                            $title = get_the_title($referral->post_id);
                            $output .= '<td class="post-id"><a href="' . esc_attr($link) . '" target="_blank">' . stripslashes(wp_filter_nohtml_kses($title)) . '</a></td>';
                            $output .= "<td class='affiliate-id'>" . stripslashes(wp_filter_nohtml_kses($referral->name)) . "</td>";
                            $output .= '</tr>';
                        }
                        $output .= '</tbody></table>';
                        $output .= '</div>';
                        // .details-referrals
                        $output .= '</td></tr>';
                    }
                }
                // if $expanded_referrals
                //
                // expanded : hits ----------------------------------------
                //
                if ($expanded_hits) {
                    // get the detailed results for hits
                    $details_orderby = "date {$order}, time {$order}";
                    $details_filters = " WHERE h.affiliate_id = %d ";
                    $details_filter_params = array($result->affiliate_id);
                    if ($from_date && $thru_date) {
                        $details_filters .= " AND datetime >= %s AND datetime < %s ";
                        $details_filter_params[] = $from_datetime;
                        $details_filter_params[] = $thru_datetime;
                    } else {
                        if ($from_date) {
                            $details_filters .= " AND datetime >= %s ";
                            $details_filter_params[] = $from_datetime;
                        } else {
                            if ($thru_date) {
                                $details_filters .= " datetime < %s ";
                                $details_filter_params[] = $thru_datetime;
                            }
                        }
                    }
                    $details_query = $wpdb->prepare("SELECT *\n\t\t\t\t\t\tFROM {$hits_table} h\n\t\t\t\t\t\tLEFT JOIN {$affiliates_table} a ON h.affiliate_id = a.affiliate_id\n\t\t\t\t\t\t{$details_filters}\n\t\t\t\t\t\tORDER BY {$details_orderby}\n\t\t\t\t\t\t", $details_filter_params);
                    $hits = $wpdb->get_results($details_query, OBJECT);
                    $output .= '<tr class=" ' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
                    $output .= '<td colspan="5">';
                    $output .= '<div class="details-hits">';
                    $output .= '<p class="description">' . __('Hits', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
                    $output .= '
						<table id="details-hits-' . esc_attr($result->date) . '" class="details-hits" cellspacing="0">
						<thead>
						<tr>
						<th scope="col" class="date">' . __('Date', AFFILIATES_PLUGIN_DOMAIN) . '</th>
						<th scope="col" class="time">' . __('Time', AFFILIATES_PLUGIN_DOMAIN) . '</th>
						<th scope="col" class="ip">' . __('IP', AFFILIATES_PLUGIN_DOMAIN) . '</th>
						<th scope="col" class="count">' . __('Count', AFFILIATES_PLUGIN_DOMAIN) . '</th>
						<th scope="col" class="affiliate-id">' . __('Affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</th>
						</tr>
						</thead>
						<tbody>
						';
                    foreach ($hits as $hit) {
                        $output .= '<tr class="details ' . ($i % 2 == 0 ? 'even' : 'odd') . '">';
                        //						$output .= "<td class='date'>$hit->date</td>";
                        $output .= '<td class="date">' . DateHelper::formatDate(DateHelper::s2u($hit->datetime)) . '</td>';
                        //						$output .= "<td class='time'>$hit->time</td>";
                        $output .= '<td class="time">' . DateHelper::formatTime(DateHelper::s2u($hit->datetime)) . '</td>';
                        $output .= "<td class='ip'>" . long2ip($hit->ip) . "</td>";
                        $output .= "<td class='count'>{$hit->count}</td>";
                        $output .= "<td class='affiliate-id'>" . stripslashes(wp_filter_nohtml_kses($hit->name)) . "</td>";
                        $output .= '</tr>';
                    }
                    $output .= '</tbody></table>';
                    $output .= '</div>';
                    // .details-hits
                    $output .= '</td></tr>';
                }
                // if $expanded_hits
            }
            // expanded
        }
    } else {
        $output .= '<tr><td colspan="5">' . __('There are no results.', AFFILIATES_PLUGIN_DOMAIN) . '</td></tr>';
    }
    $output .= '</tbody>';
    $output .= '</table>';
    if ($paginate) {
        require_once AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php';
        $pagination = new Affiliates_Pagination($count, null, $row_count);
        $output .= '<div class="tablenav bottom">';
        $output .= $pagination->pagination('bottom');
        $output .= '</div>';
    }
    $output .= '</div>';
    // .visits-overview
    echo $output;
    affiliates_footer();
}
/**
 * Delete a referral.
 */
function affiliates_admin_referral_remove($referral_id = null)
{
    global $wpdb;
    $output = '';
    if (!current_user_can(AFFILIATES_ADMINISTER_AFFILIATES)) {
        wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $cancel_url = remove_query_arg('referral_id', remove_query_arg('action', $current_url));
    $current_url = remove_query_arg('paged', $current_url);
    $output .= '<div class="referral remove">';
    $output .= '<h2>';
    $output .= __('Remove a Referral', AFFILIATES_PLUGIN_DOMAIN);
    $output .= '</h2>';
    if (isset($_POST['submit'])) {
        if (!wp_verify_nonce($_POST['referral-nonce'], 'remove')) {
            wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
        } else {
            if (!empty($_POST['referral_id'])) {
                // remove the referral
                $referrals_table = _affiliates_get_tablename('referrals');
                if ($wpdb->query($wpdb->prepare("DELETE FROM {$referrals_table} WHERE referral_id = %d", intval($_POST['referral_id'])))) {
                    $output .= '<br/>';
                    $output .= '<div class="info">';
                    $output .= __('The referral has been removed.', AFFILIATES_PLUGIN_DOMAIN);
                    $output .= ' ';
                    $output .= sprintf('<a href="%s">%s</a>', $cancel_url, __('Return', AFFILIATES_PLUGIN_DOMAIN));
                    $output .= '</div>';
                    $output .= '<br/>';
                } else {
                    $output .= '<div class="error">' . __('I do not know how to delete what does not exist.', AFFILIATES_PLUGIN_DOMAIN) . '</div>';
                }
            }
        }
    } else {
        if ($referral_id !== null) {
            $referrals_table = _affiliates_get_tablename('referrals');
            if ($referrals = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$referrals_table} WHERE referral_id = %d", $referral_id))) {
                if (count($referrals) > 0) {
                    $referral = $referrals[0];
                    $affiliate_id = $referral->affiliate_id;
                    $datetime = $referral->datetime;
                    $description = wp_strip_all_tags($referral->description);
                    $amount = $referral->amount;
                    $currency_id = $referral->currency_id;
                    $status = $referral->status;
                    $reference = wp_strip_all_tags($referral->reference);
                    $output .= '<form id="referral" action="' . $current_url . '" method="post">';
                    $output .= '<div>';
                    $output .= sprintf('<input type="hidden" name="referral_id" value="%d" />', intval($referral_id));
                    $output .= '<input type="hidden" name="action" value="edit" />';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $affiliate = affiliates_get_affiliate($affiliate_id);
                    $output .= stripslashes($affiliate['name']);
                    $output .= '</p>';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Date & Time', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $datetime;
                    $output .= '</p>';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Description', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $description;
                    $output .= '</p>';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Amount', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $amount;
                    $output .= '</p>';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Currency ID', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $currency_id;
                    $output .= '</p>';
                    $status_descriptions = array(AFFILIATES_REFERRAL_STATUS_ACCEPTED => __('Accepted', AFFILIATES_PLUGIN_DOMAIN), AFFILIATES_REFERRAL_STATUS_CLOSED => __('Closed', AFFILIATES_PLUGIN_DOMAIN), AFFILIATES_REFERRAL_STATUS_PENDING => __('Pending', AFFILIATES_PLUGIN_DOMAIN), AFFILIATES_REFERRAL_STATUS_REJECTED => __('Rejected', AFFILIATES_PLUGIN_DOMAIN));
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Status', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $status_descriptions[$status];
                    $output .= '</p>';
                    $output .= '<p>';
                    $output .= '<span class="title">' . __('Reference', AFFILIATES_PLUGIN_DOMAIN) . '</span>';
                    $output .= ' ';
                    $output .= $reference;
                    $output .= '</p>';
                    $output .= wp_nonce_field('remove', 'referral-nonce', true, false);
                    $output .= '<p class="description">';
                    $output .= __('Remove this referral? This action can not be undone.', AFFILIATES_PLUGIN_DOMAIN);
                    $output .= '</p>';
                    $output .= sprintf('<input class="button" type="submit" name="submit" value="%s"/>', __('Remove', AFFILIATES_PLUGIN_DOMAIN));
                    $output .= ' ';
                    $output .= sprintf('<a class="cancel" href="%s">%s</a>', $cancel_url, __('Cancel', AFFILIATES_PLUGIN_DOMAIN));
                    $output .= '</div>';
                    $output .= '</form>';
                } else {
                    $output .= '<div class="error">' . __('This referral does not exist.', AFFILIATES_PLUGIN_DOMAIN) . '</div>';
                }
            } else {
                $output .= '<div class="error">' . __('This referral does not exist.', AFFILIATES_PLUGIN_DOMAIN) . '</div>';
            }
        } else {
            $output .= '<div class="error">' . __('Pretty pointless ...', AFFILIATES_PLUGIN_DOMAIN) . '</div>';
        }
    }
    $output .= '</div>';
    echo $output;
    affiliates_footer();
}
 /**
  * Renders the contact form.
  * Remember NOT to use any form input elements named 'name', 'year', ... 
  * @static
  */
 static function render_form($widget_id = '', $amount = null, $currency_id = null)
 {
     $method = 'post';
     $action = "";
     if (!empty($widget_id)) {
         $ext = '-' . $widget_id;
     } else {
         $ext = '';
     }
     $submit_name = 'affiliates-contact-submit';
     $nonce = 'affiliates-contact-nonce';
     $nonce_action = 'affiliates-contact';
     $send = false;
     $sender_class = '';
     $email_class = '';
     $message_class = '';
     $captcha = '';
     $error = false;
     if (!empty($_POST[$submit_name])) {
         if (!wp_verify_nonce($_POST[$nonce], $nonce_action)) {
             $error = true;
             // fail but don't give clues
         }
         $captcha = $_POST[Affiliates_Contact::$captcha_field_id];
         if (!Affiliates_Contact::captcha_validates($captcha)) {
             $error = true;
             // dumbot
         }
         $sender = Affiliates_Contact::filter($_POST['sender']);
         $email = Affiliates_Contact::filter($_POST['email']);
         $message = Affiliates_Contact::filter($_POST['message']);
         if (empty($sender)) {
             $sender_class .= ' class="missing" ';
             $error = true;
         }
         if (empty($email) || !is_email($email)) {
             $email_class .= ' class="missing" ';
             $error = true;
         }
         if (empty($message)) {
             $message_class .= ' class="missing" ';
             $error = true;
         }
         if (!$error) {
             $send = true;
             $description = __('Affiliates contact form submission', AFFILIATES_PLUGIN_DOMAIN);
             $data = array('name' => array('title' => 'Name', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $sender), 'email' => array('title' => 'Email', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $email), 'message' => array('title' => 'Message', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $message));
             // request a referral
             $affiliate = null;
             if (function_exists('affiliates_suggest_referral')) {
                 $post_id = get_the_ID();
                 $affiliate_id = affiliates_suggest_referral($post_id, $description, $data, $amount, $currency_id, null, null, 'ACF' . md5(time()));
                 if ($affiliate_id) {
                     $affiliate = affiliates_get_affiliate($affiliate_id);
                     // Now you could send an email to the affiliate ...
                 }
             }
         }
     } else {
         $sender = '';
         $email = '';
         $message = '';
     }
     if (!$send) {
         echo '<div class="affiliates-contact" id="affiliates-contact' . $ext . '">';
         echo '<img id="affiliates-contact-throbber' . $ext . '" src="' . AFFILIATES_PLUGIN_URL . 'images/affiliates-throbber.gif" style="display:none" />';
         echo '<form id="affiliates-contact-form' . $ext . '" action="' . $action . '" method="' . $method . '">';
         echo '<div>';
         echo '<label ' . $sender_class . ' id="affiliates-contact-form' . $ext . '-sender-label" for="sender">' . __('Name', AFFILIATES_PLUGIN_DOMAIN) . '</label>';
         echo '<input id="affiliates-contact-form' . $ext . '-sender" name="sender" type="text" value="' . esc_attr($sender) . '"/>';
         echo '<label ' . $email_class . ' id="affiliates-contact-form' . $ext . '-email-label" for="email">' . __('Email', AFFILIATES_PLUGIN_DOMAIN) . '</label>';
         echo '<input id="affiliates-contact-form' . $ext . '-email" name="email" type="text" value="' . esc_attr($email) . '"/>';
         echo '<label ' . $message_class . 'id="affiliates-contact-form' . $ext . '-message-label" for="message">' . __('Message', AFFILIATES_PLUGIN_DOMAIN) . '</label>';
         echo '<textarea id="affiliates-contact-form' . $ext . '-message" name="message">' . $message . '</textarea>';
         echo Affiliates_Contact::captcha_get($captcha);
         echo wp_nonce_field($nonce_action, $nonce, true, false);
         echo '<input type="submit" name="' . $submit_name . '" value="' . __('Send', AFFILIATES_PLUGIN_DOMAIN) . '" />';
         echo '</div>';
         echo '</form>';
         echo '</div>';
     } else {
         echo '<p>' . __('Thanks!', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
     }
 }
/**
 * Show edit affiliate form.
 * @param int $affiliate_id affiliate id
 */
function affiliates_admin_affiliates_edit($affiliate_id)
{
    global $wpdb;
    if (!current_user_can(AFFILIATES_ADMINISTER_AFFILIATES)) {
        wp_die(__('Access denied.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliate = affiliates_get_affiliate(intval($affiliate_id));
    if (empty($affiliate)) {
        wp_die(__('No such affiliate.', AFFILIATES_PLUGIN_DOMAIN));
    }
    $affiliates_users_table = _affiliates_get_tablename('affiliates_users');
    $affiliate_user = null;
    $affiliate_user_edit = '';
    $affiliate_user_fields = '';
    $affiliate_user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$affiliates_users_table} WHERE affiliate_id = %d", intval($affiliate_id)));
    if ($affiliate_user_id !== null) {
        $affiliate_user = get_user_by('id', intval($affiliate_user_id));
        if ($affiliate_user) {
            // user edit link
            if (current_user_can('edit_user', $affiliate_user->ID)) {
                $affiliate_user_edit = sprintf(__('Edit %s', AFFILIATES_PLUGIN_DOMAIN), '<a target="_blank" href="' . esc_url("user-edit.php?user_id={$affiliate_user->ID}") . '">' . $affiliate_user->user_login . '</a>');
            }
            // user meta fields
            require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
            require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
            $registration_fields = Affiliates_Settings_Registration::get_fields();
            // remove fields not stored as user meta
            foreach (Affiliates_Registration::get_skip_meta_fields() as $key) {
                unset($registration_fields[$key]);
            }
            // render user meta
            foreach ($registration_fields as $name => $field) {
                if ($field['enabled']) {
                    $affiliate_user_fields .= '<div class="field">';
                    $affiliate_user_fields .= '<label>';
                    $affiliate_user_fields .= esc_html(stripslashes($field['label']));
                    // @todo i18n
                    $affiliate_user_fields .= ' ';
                    $type = isset($field['type']) ? $field['type'] : 'text';
                    $value = get_user_meta($affiliate_user->ID, $name, true);
                    $affiliate_user_fields .= sprintf('<input type="text" value="%s" readonly="readonly" />', esc_attr(stripslashes($value)));
                    $affiliate_user_fields .= '</label>';
                    $affiliate_user_fields .= '</div>';
                }
            }
        }
    }
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $current_url = remove_query_arg('action', $current_url);
    $current_url = remove_query_arg('affiliate_id', $current_url);
    $name = isset($_POST['name-field']) ? $_POST['name-field'] : $affiliate['name'];
    $email = isset($_POST['email-field']) ? $_POST['email-field'] : $affiliate['email'];
    $user_login = isset($_POST['user-field']) ? $_POST['user-field'] : ($affiliate_user != null ? $affiliate_user->user_login : '');
    $from_date = isset($_POST['from-date-field']) ? $_POST['from-date-field'] : $affiliate['from_date'];
    $thru_date = isset($_POST['thru-date-field']) ? $_POST['thru-date-field'] : $affiliate['thru_date'];
    $output = '<div class="manage-affiliates">' . '<div>' . '<h1>' . __('Edit an affiliate', AFFILIATES_PLUGIN_DOMAIN) . '</h1>' . '</div>' . '<form id="edit-affiliate" action="' . esc_url($current_url) . '" method="post">' . '<div class="affiliate edit">' . '<input id="affiliate-id-field" name="affiliate-id-field" type="hidden" value="' . esc_attr(intval($affiliate_id)) . '"/>' . '<div class="field">' . '<label class="field-label first required">' . '<span class="label">' . __('Name', AFFILIATES_PLUGIN_DOMAIN) . '</span>' . ' ' . '<input id="name-field" name="name-field" class="namefield" type="text" value="' . esc_attr(stripslashes($name)) . '"/>' . '</label>' . '</div>' . '<div class="field">' . '<label class="field-label">' . '<span class="label">' . __('Email', AFFILIATES_PLUGIN_DOMAIN) . '</span>' . ' ' . '<input id="email-field" name="email-field" class="emailfield" type="text" value="' . esc_attr($email) . '"/>' . '</label>' . ' ' . '<span class="description">' . __("If a valid <strong>Username</strong> is specified and no email is given, the user's email address will be used automatically.", AFFILIATES_PLUGIN_DOMAIN) . '</span>' . '</div>' . '<div class="field">' . '<label class="field-label">' . '<span class="label">' . __('Username', AFFILIATES_PLUGIN_DOMAIN) . '</span>' . ' ' . '<input id="user-field" name="user-field" class="userfield" type="text" autocomplete="off" value="' . esc_attr(stripslashes($user_login)) . '"/>' . '</label>' . ' ' . $affiliate_user_edit . '</div>' . $affiliate_user_fields . '<div class="field">' . '<label class="field-label">' . '<span class="label">' . __('From', AFFILIATES_PLUGIN_DOMAIN) . '</span>' . ' ' . '<input id="from-date-field" name="from-date-field" class="datefield" type="text" value="' . esc_attr($from_date) . '"/>' . '</label>' . '</div>' . '<div class="field">' . '<label class="field-label">' . '<span class="label">' . __('Until', AFFILIATES_PLUGIN_DOMAIN) . '</span>' . ' ' . '<input id="thru-date-field" name="thru-date-field" class="datefield" type="text" value="' . esc_attr($thru_date) . '"/>' . '</label>' . '</div>';
    $output .= '<div class="field">' . wp_nonce_field('affiliates-edit', AFFILIATES_ADMIN_AFFILIATES_NONCE, true, false) . '<input class="button button-primary" type="submit" value="' . __('Save', AFFILIATES_PLUGIN_DOMAIN) . '"/>' . '<input type="hidden" value="edit" name="action"/>' . ' ' . '<a class="cancel button" href="' . esc_url($current_url) . '">' . __('Cancel', AFFILIATES_PLUGIN_DOMAIN) . '</a>' . '</div>' . '</div>' . '</form>' . '</div>';
    // .manage-affiliates
    echo $output;
    affiliates_footer();
}