/**
  * Add pending referral
  *
  * @function add_pending_referral
  * @access public
  */
 public function add_pending_referral($order_id = 0)
 {
     if ($this->was_referred()) {
         $this->order = apply_filters('affwp_get_jigoshop_order', new jigoshop_order($order_id));
         // Fetch order
         if ($this->is_affiliate_email($this->order->billing_email)) {
             return;
             // Customers cannot refer themselves
         }
         $description = '';
         $items = $this->order->items;
         foreach ($items as $key => $item) {
             $description .= $item['name'];
             if ($key + 1 < count($items)) {
                 $description .= ', ';
             }
         }
         $amount = $this->order->order_total;
         if (affiliate_wp()->settings->get('exclude_tax')) {
             $amount -= $this->order->get_total_tax();
         }
         if (affiliate_wp()->settings->get('exclude_shipping')) {
             $amount -= $this->order->order_shipping;
         }
         $referral_total = $this->calculate_referral_amount($amount, $order_id);
         $this->insert_pending_referral($referral_total, $order_id, $description);
         $referral = affiliate_wp()->referrals->get_by('reference', $order_id, $this->context);
         $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
         $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
         $this->order->add_order_note(sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral->referral_id, $amount, $name));
     }
 }
 /**
  * Revoke a referral when a payment is refunded
  *
  * @access  public
  * @since   1.6
  */
 public function revoke_referral_on_refund($payment_id = 0)
 {
     if (!affiliate_wp()->settings->get('revoke_on_refund')) {
         return;
     }
     $this->reject_referral($payment_id);
     $referral = affiliate_wp()->referrals->get_by('reference', $payment_id, $this->context);
     $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
     $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
     $note = sprintf(__('Referral #%d for %s for %s rejected', 'affiliate-wp'), $referral->referral_id, $amount, $name);
     $payment = SI_Payment::get_instance($payment_id);
     $new_data = wp_parse_args($payment->get_data(), array('affwp_notes' => $note));
     $payment->set_data($new_data);
 }
Example #3
0
/**
 *  Load the frontend scripts and styles
 *  
 *  @since 1.0
 *  @return void
 */
function affwp_frontend_scripts_and_styles()
{
    global $post;
    if (!is_object($post)) {
        return;
    }
    if (has_shortcode($post->post_content, 'affiliate_area') || apply_filters('affwp_force_frontend_scripts', false)) {
        $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
        wp_enqueue_script('affwp-frontend', AFFILIATEWP_PLUGIN_URL . 'assets/js/frontend' . $suffix . '.js', array('jquery'), AFFILIATEWP_VERSION);
        wp_localize_script('affwp-frontend', 'affwp_vars', array('affwp_version' => AFFILIATEWP_VERSION, 'permalinks' => get_option('permalink_structure'), 'pretty_affiliate_urls' => affwp_is_pretty_referral_urls(), 'currency_sign' => affwp_currency_filter(''), 'currency_pos' => affiliate_wp()->settings->get('currency_position', 'before')));
        wp_enqueue_style('affwp-forms', AFFILIATEWP_PLUGIN_URL . 'assets/css/forms' . $suffix . '.css', AFFILIATEWP_VERSION);
        wp_enqueue_style('dashicons');
    }
}
/**
 * Plugin Name: AffiliateWP - Email Affiliate When Referral Paid
 * Plugin URI: http://affiliatewp.com
 * Description: Sends an email to the affiliate when a referral has been paid
 * Author: Andrew Munro
 * Author URI: http://affiliatewp.com
 * Version: 1.0
 */
function affwp_eawrp_affiliate_referral_paid_email($referral_id, $new_status, $old_status)
{
    if (!function_exists('affiliate_wp') || ('paid' != $new_status || 'unpaid' != $old_status)) {
        return;
    }
    $referral = affiliate_wp()->referrals->get_by('referral_id', $referral_id);
    $affiliate_id = $referral->affiliate_id;
    $affiliate_email = affwp_get_affiliate_email($affiliate_id);
    $amount = html_entity_decode(affwp_currency_filter($referral->amount), ENT_COMPAT, 'UTF-8');
    $date = date_i18n(get_option('date_format'), strtotime($referral->date));
    // email subject
    $subject = sprintf('Congratulations, your referral for %s has just been paid', $amount);
    // email body
    $message = affwp_eawrp_affiliate_referral_paid_email_body($affiliate_id, $amount, $date);
    // send mail
    affiliate_wp()->emails->send($affiliate_email, $subject, $message);
}
/**
 * Send referral email to admin
 * Requires AffiliateWP v1.6+
 */
function affwp_custom_referral_sale_email($add)
{
    $emails = new Affiliate_WP_Emails();
    $referral = affwp_get_referral($add);
    $affiliate_id = $referral->affiliate_id;
    $context = $referral->context;
    $reference = $referral->reference;
    $products = $referral->products;
    switch ($context) {
        case 'edd':
            $link = esc_url(admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $reference));
            break;
        case 'woocommerce':
            $link = esc_url(admin_url('post.php?post=' . $reference . '&action=edit'));
            break;
        default:
            $link = '';
            break;
    }
    $email = apply_filters('affwp_registration_admin_email', get_option('admin_email'));
    $amount = html_entity_decode(affwp_currency_filter($referral->amount), ENT_COMPAT, 'UTF-8');
    $subject = __('New referral sale!', 'affiliate-wp');
    $message = __('Congratulations!', 'affiliate-wp') . "\n\n";
    $message .= __('You have just received a new referral sale:', 'affiliate-wp') . "\n\n";
    if ($link) {
        $message .= sprintf(__('Order: %s ', 'affiliate-wp'), '<a href="' . $link . '">#' . $reference . '</a>') . "\n";
    } else {
        $message .= sprintf(__('Order: #%s ', 'affiliate-wp'), $reference) . "\n";
    }
    $message .= sprintf(__('Affiliate Name: %s ', 'affiliate-wp'), affiliate_wp()->affiliates->get_affiliate_name($affiliate_id)) . "\n";
    $message .= sprintf(__('Referral amount: %s ', 'affiliate-wp'), $amount) . "\n\n";
    $message .= __('Products that earned commission:', 'affiliate-wp') . "\n\n";
    if ($products) {
        foreach ($products as $product) {
            $referral_amount = html_entity_decode(affwp_currency_filter(affwp_format_amount($product['referral_amount'])), ENT_COMPAT, 'UTF-8');
            $message .= '<strong>' . $product['name'] . '</strong>' . "\n";
            $message .= sprintf(__('Referral Amount: %s ', 'affiliate-wp'), $referral_amount) . "\n\n";
        }
    }
    $emails->send($email, $subject, $message);
}
Example #6
0
 public function mark_referral_complete($order)
 {
     if ('success' !== strtolower($order->status)) {
         return;
     }
     $this->complete_referral($order->id);
     $referral = affiliate_wp()->referrals->get_by('reference', $order->id, $this->context);
     $order = new MemberOrder($order->id);
     // Prevent infinite loop
     remove_action('pmpro_updated_order', array($this, 'mark_referral_complete'), 10);
     $order->affiliate_id = $referral->affiliate_id;
     $amount = html_entity_decode(affwp_currency_filter(affwp_format_amount($referral->amount)), ENT_QUOTES, 'UTF-8');
     $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
     $note = sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral->referral_id, $amount, $name);
     if (empty($order->notes)) {
         $order->notes = $note;
     } else {
         $order->notes = $order->notes . "\n\n" . $note;
     }
     $order->saveOrder();
 }
Example #7
0
 public function add_pending_referral($order_id = 0)
 {
     if ($this->was_referred()) {
         $this->order = apply_filters('affwp_get_shopp_order', shopp_order($order_id->order));
         $customer_email = $this->order->email;
         if ($this->is_affiliate_email($customer_email)) {
             return;
             // Customers cannot refer themselves
         }
         $description = '';
         foreach ($this->order->purchased as $key => $item) {
             $description .= $item->name;
             if ($key + 1 < count($this->order->purchased)) {
                 $description .= ', ';
             }
         }
         $amount = $this->order->total;
         if (affiliate_wp()->settings->get('exclude_tax')) {
             $amount -= $this->order->tax;
         }
         if (affiliate_wp()->settings->get('exclude_shipping')) {
             $amount -= $this->order->shipping;
         }
         $referral_total = $this->calculate_referral_amount($amount, $order_id->order);
         $this->insert_pending_referral($referral_total, $order_id->order, $description);
         $referral = affiliate_wp()->referrals->get_by('reference', $order_id->order, 'shopp');
         $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
         $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
         $user = wp_get_current_user();
         $Note = new ShoppMetaObject();
         $Note->parent = $order_id->order;
         $Note->context = 'purchase';
         $Note->type = 'order_note';
         $Note->value = new stdClass();
         $Note->value->author = $user->ID;
         $Note->value->message = sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral->referral_id, $amount, $name);
         $Note->save();
     }
 }
 /**
  * Revoke referral on refund
  *
  * @access public
  * @uses GFFormsModel::add_note()
  *
  * @param array $entry
  * @param array $action
  */
 public function revoke_referral_on_refund($entry, $action)
 {
     $this->reject_referral($entry['id']);
     $referral = affiliate_wp()->referrals->get_by('reference', $entry['id'], $this->context);
     $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
     $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
     $note = sprintf(__('Referral #%d for %s for %s rejected', 'affiliate-wp'), $referral->referral_id, $amount, $name);
     GFFormsModel::add_note($entry["id"], 0, 'AffiliateWP', $note);
 }
/**
 * Retrieves the total unpaid earnings for an affiliate
 *
 * @since 1.0
 * @return float
 */
function affwp_get_affiliate_unpaid_earnings($affiliate, $formatted = false)
{
    if (is_object($affiliate) && isset($affiliate->affiliate_id)) {
        $affiliate_id = $affiliate->affiliate_id;
    } elseif (is_numeric($affiliate)) {
        $affiliate_id = absint($affiliate);
    } else {
        return false;
    }
    $referrals = affiliate_wp()->referrals->get_referrals(array('affiliate_id' => $affiliate_id, 'status' => 'unpaid', 'number' => -1));
    $earnings = 0;
    if (!empty($referrals)) {
        foreach ($referrals as $referral) {
            $earnings += $referral->amount;
        }
    }
    if ($formatted) {
        $earnings = affwp_currency_filter($earnings);
    }
    return $earnings;
}
Example #10
0
 /**
  * Render the amount column
  *
  * @access public
  * @since 1.0
  * @param array $referral Contains all the data for the checkbox column
  * @return string Displays the referral amount
  */
 public function column_amount($referral)
 {
     return affwp_currency_filter(affwp_format_amount($referral->amount));
 }
do_action('affwp_referrals_dashboard_th');
?>
			</tr>
		</thead>

		<tbody>
			<?php 
if ($referrals) {
    ?>

				<?php 
    foreach ($referrals as $referral) {
        ?>
					<tr>
						<td class="referral-amount"><?php 
        echo affwp_currency_filter(affwp_format_amount($referral->amount));
        ?>
</td>
						<td class="referral-description"><?php 
        echo wp_kses_post(nl2br($referral->description));
        ?>
</td>
						<td class="referral-status <?php 
        echo $referral->status;
        ?>
"><?php 
        echo affwp_get_referral_status_label($referral);
        ?>
</td>
						<td class="referral-date"><?php 
        echo date_i18n(get_option('date_format'), strtotime($referral->date));
Example #12
0
/**
 * Send email on new referrals
 *
 * @since 1.6
 * @param int $affiliate_id The ID of the registered affiliate
 * @param array $referral
 */
function affwp_notify_on_new_referral($affiliate_id = 0, $referral)
{
    $user_id = affwp_get_affiliate_user_id($affiliate_id);
    if (!get_user_meta($user_id, 'affwp_referral_notifications', true)) {
        return;
    }
    if (empty($affiliate_id)) {
        return;
    }
    if (empty($referral)) {
        return;
    }
    $emails = new Affiliate_WP_Emails();
    $emails->__set('affiliate_id', $affiliate_id);
    $emails->__set('referral', $referral);
    $email = affwp_get_affiliate_email($affiliate_id);
    $subject = affiliate_wp()->settings->get('referral_subject', __('Referral Awarded!', 'affiliate-wp'));
    $message = affiliate_wp()->settings->get('referral_email', false);
    $amount = html_entity_decode(affwp_currency_filter($referral->amount), ENT_COMPAT, 'UTF-8');
    if (!$message) {
        $message = sprintf(__('Congratulations %s!', 'affiliate-wp'), affiliate_wp()->affiliates->get_affiliate_name($affiliate_id)) . "\n\n";
        $message .= sprintf(__('You have been awarded a new referral of %s on %s!', 'affiliate-wp'), $amount, home_url()) . "\n\n";
        $message .= sprintf(__('log into your affiliate area to view your earnings or disable these notifications: %s', 'affiliate-wp'), affiliate_wp()->login->get_login_url()) . "\n\n";
    }
    // $args is setup for backwards compatibility with < 1.6
    $args = array('affiliate_id' => $affiliate_id, 'amount' => $referral->amount, 'referral' => $referral);
    $subject = apply_filters('affwp_new_referral_subject', $subject, $args);
    $message = apply_filters('affwp_new_referral_email', $message, $args);
    if (apply_filters('affwp_notify_on_new_referral', true, $referral)) {
        $emails->send($email, $subject, $message);
    }
}
Example #13
0
 /**
  * Insert payment note
  *
  * @access  public
  * @since   1.3.1
  */
 public function insert_payment_note($payment_id = 0)
 {
     $referral = affiliate_wp()->referrals->get_by('reference', $payment_id, $this->context);
     if (empty($referral)) {
         return;
     }
     $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
     $affiliate_id = $referral->affiliate_id;
     $name = affiliate_wp()->affiliates->get_affiliate_name($affiliate_id);
     edd_insert_payment_note($payment_id, sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral->referral_id, $amount, $name));
 }
Example #14
0
				<th><?php 
_e('Total visits', 'affiliate-wp');
?>
</th>
				<?php 
do_action('affwp_view_affiliate_report_table_header', $affiliate_id);
?>
			</tr>

		</thead>

		<tbody>

			<tr>
				<td><?php 
echo affwp_currency_filter(affwp_get_affiliate_earnings($affiliate_id));
?>
</td>
				<td><?php 
echo affwp_get_affiliate_unpaid_earnings($affiliate_id, true);
?>
</td>
				<td><?php 
echo affwp_get_affiliate_referral_count($affiliate_id);
?>
</td>
				<td><?php 
echo affiliate_wp()->referrals->count(array('affiliate_id' => $affiliate_id, 'status' => 'unpaid'));
?>
</td>
				<td><?php 
Example #15
0
 /**
  * Render the amount column
  *
  * @access public
  * @since 1.0
  * @param array $referral Contains all the data for the checkbox column
  * @return string Displays the referral amount
  */
 public function column_amount($referral)
 {
     $value = affwp_currency_filter(affwp_format_amount($referral->amount));
     return apply_filters('affwp_referral_table_amount', $value, $referral);
 }
Example #16
0
 /**
  * Render the earnings column
  *
  * @access public
  * @since 1.0
  * @param array $affiliate Contains all the data for the earnings column
  * @return string earnings link
  */
 function column_earnings($affiliate)
 {
     return affwp_currency_filter(affwp_format_amount(affwp_get_affiliate_earnings($affiliate->affiliate_id)));
 }
 /**
  * Store a pending referral when a new order is created
  *
  * @access  public
  * @since   1.0
  */
 public function add_pending_referral($order_id = 0, $posted)
 {
     $this->order = apply_filters('affwp_get_woocommerce_order', new WC_Order($order_id));
     // Check if an affiliate coupon was used
     $coupon_affiliate_id = $this->get_coupon_affiliate_id();
     if ($this->was_referred() || $coupon_affiliate_id) {
         // get affiliate ID
         $affiliate_id = $this->get_affiliate_id($order_id);
         if (false !== $coupon_affiliate_id) {
             $affiliate_id = $coupon_affiliate_id;
         }
         // Customers cannot refer themselves
         if ($this->is_affiliate_email($this->order->billing_email, $affiliate_id)) {
             return false;
         }
         // Check for an existing referral
         $existing = affiliate_wp()->referrals->get_by('reference', $order_id, $this->context);
         // If an existing referral exists and it is paid or unpaid exit.
         if ($existing && ('paid' == $existing->status || 'unpaid' == $existing->status)) {
             return false;
             // Completed Referral already created for this reference
         }
         $cart_shipping = $this->order->get_total_shipping();
         $items = $this->order->get_items();
         // Calculate the referral amount based on product prices
         $amount = 0.0;
         foreach ($items as $product) {
             if (get_post_meta($product['product_id'], '_affwp_' . $this->context . '_referrals_disabled', true)) {
                 continue;
                 // Referrals are disabled on this product
             }
             // The order discount has to be divided across the items
             $product_total = $product['line_total'];
             $shipping = 0;
             if ($cart_shipping > 0 && !affiliate_wp()->settings->get('exclude_shipping')) {
                 $shipping = $cart_shipping / count($items);
                 $product_total += $shipping;
             }
             if (!affiliate_wp()->settings->get('exclude_tax')) {
                 $product_total += $product['line_tax'];
             }
             if ($product_total <= 0) {
                 continue;
             }
             $amount += $this->calculate_referral_amount($product_total, $order_id, $product['product_id'], $affiliate_id);
         }
         if (0 == $amount && affiliate_wp()->settings->get('ignore_zero_referrals')) {
             return false;
             // Ignore a zero amount referral
         }
         $description = $this->get_referral_description();
         $visit_id = affiliate_wp()->tracking->get_visit_id();
         if ($existing) {
             // Update the previously created referral
             affiliate_wp()->referrals->update_referral($existing->referral_id, array('amount' => $amount, 'reference' => $order_id, 'description' => $description, 'campaign' => affiliate_wp()->tracking->get_campaign(), 'affiliate_id' => $affiliate_id, 'visit_id' => $visit_id, 'products' => $this->get_products(), 'context' => $this->context));
         } else {
             // Create a new referral
             $referral_id = affiliate_wp()->referrals->add(apply_filters('affwp_insert_pending_referral', array('amount' => $amount, 'reference' => $order_id, 'description' => $description, 'campaign' => affiliate_wp()->tracking->get_campaign(), 'affiliate_id' => $affiliate_id, 'visit_id' => $visit_id, 'products' => $this->get_products(), 'context' => $this->context), $amount, $order_id, $description, $affiliate_id, $visit_id, array(), $this->context));
             if ($referral_id) {
                 $amount = affwp_currency_filter(affwp_format_amount($amount));
                 $name = affiliate_wp()->affiliates->get_affiliate_name($affiliate_id);
                 $this->order->add_order_note(sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral_id, $amount, $name));
             }
         }
     }
 }
 /**
  * Update referral status and add note to Formidable Pro entry
  *
  * @since 1.6
  *
  * @author Naomi C. Bush <*****@*****.**>
  *
  * @param int $entry_id
  * @param int $form_id
  */
 public function revoke_referral_on_refund($entry_id, $form_id)
 {
     global $frm_entry_meta;
     $this->reject_referral($entry_id);
     $referral = affiliate_wp()->referrals->get_by('reference', $entry_id, $this->context);
     $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
     $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
     $note = sprintf(__('AffiliateWP: Referral #%d for %s for %s rejected', 'affiliate-wp'), $referral->referral_id, $amount, $name);
     $frm_entry_meta->add_entry_meta($entry_id, 0, '', array('comment' => $note, 'user_id' => 0));
 }
 /**
  * Mark referral as complete when payment is completed
  *
  * @access  public
  * @since   1.7
  */
 public function mark_referral_complete(Zippy_Event $event)
 {
     if ($event->new_status == 'complete' && $event->old_status != 'complete') {
         $order = $event->order;
         $referral = affiliate_wp()->referrals->get_by('reference', $order->getId(), $this->context);
         if (!$referral) {
             return;
         }
         $this->complete_referral($order->getId());
         $amount = affwp_currency_filter(affwp_format_amount($referral->amount));
         $name = affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
         $note = sprintf(__('Referral #%d for %s recorded for %s', 'affiliate-wp'), $referral->referral_id, $amount, $name);
         $order->addNote(array('content' => $note, 'timestamp' => time()));
         $order->saveNotes();
     }
 }
Example #20
0
/**
 * Email template tag: amount
 * The amount of an affiliate transaction
 *
 * @return string amount
 */
function affwp_email_tag_amount($affiliate_id = 0, $referral)
{
    return html_entity_decode(affwp_currency_filter($referral->amount), ENT_COMPAT, 'UTF-8');
}
<?php

$user_id = affwp_get_affiliate_user_id($user);
$user_credit_balance = get_user_meta($user_id, 'affwp_wc_credit_balance', true);
if (empty($user_credit_balance)) {
    $user_credit_balance = 0;
}
?>

<table class="affwp-table">
	<thead>
	<tr>
		<th><?php 
_e('Store Credits', 'affwp-scd');
?>
</th>
	</tr>
	</thead>
	<tbody>
	<tr>
		<td><?php 
echo affwp_currency_filter($user_credit_balance);
?>
</td>
	</tr>
	</tbody>
</table>
Example #22
0
 /**
  * Get the total unpaid earnings
  *
  * @access  public
  * @since   1.0
  */
 public function unpaid_earnings($date = '', $affiliate_id = 0, $format = true)
 {
     $args = array();
     $args['status'] = 'unpaid';
     $args['affiliate_id'] = $affiliate_id;
     $args['number'] = '-1';
     if (!empty($date)) {
         switch ($date) {
             case 'month':
                 $date = array('start' => date('Y-m-01 00:00:00', current_time('timestamp')), 'end' => date('Y-m-' . cal_days_in_month(CAL_GREGORIAN, date('n'), date('Y')) . ' 00:00:00', current_time('timestamp')));
                 break;
         }
         $args['date'] = $date;
     }
     $referrals = $this->get_referrals($args);
     $earnings = array_sum(wp_list_pluck($referrals, 'amount'));
     if ($format) {
         $earnings = affwp_currency_filter(affwp_format_amount($earnings));
     }
     return $earnings;
 }
Example #23
0
 /**
  * Render the earnings column
  *
  * @access public
  * @since 1.0
  * @param array $affiliate Contains all the data for the earnings column
  * @return string earnings link
  */
 function column_earnings($affiliate)
 {
     $value = affwp_currency_filter(affwp_format_amount(affwp_get_affiliate_earnings($affiliate->affiliate_id)));
     return apply_filters('affwp_affiliate_table_earnings', $value, $affiliate);
 }
Example #24
0
function affwp_affiliates_dashboard()
{
    ?>
	<div class="wrap">
		<h2><?php 
    _e('Overview', 'affiliate-wp');
    ?>
</h2>
		<?php 
    do_action('affwp_overview_top');
    ?>
		<div id="affwp-dashboard-widgets-wrap">
			<div id="dashboard-widgets" class="metabox-holder">
				<div id="postbox-container-1" class="postbox-container">
					<?php 
    do_action('affwp_overview_tleft_op');
    ?>
					<div class="postbox">
						<h3><?php 
    _e('Totals', 'affiliate-wp');
    ?>
</h3>
						<div class="inside">
							
							<table class="affwp_table">

								<thead>

									<tr>

										<th><?php 
    _e('Paid Earnings', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Paid Earnings This Month', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Paid Earnings Today', 'affiliate-wp');
    ?>
</th>

									</tr>

								</thead>

								<tbody>

									<tr>
										<td><?php 
    echo affiliate_wp()->referrals->paid_earnings();
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->paid_earnings('month');
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->paid_earnings('today');
    ?>
</td>
									</tr>

								</tbody>

							</table>

							<table class="affwp_table">

								<thead>

									<tr>

										<th><?php 
    _e('Unpaid Referrals', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Unpaid Referrals This Month', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Unpaid Referrals Today', 'affiliate-wp');
    ?>
</th>

									</tr>

								</thead>

								<tbody>

									<tr>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_count();
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_count('month');
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_count('today');
    ?>
</td>
									</tr>

								</tbody>

							</table>
							<table class="affwp_table">

								<thead>

									<tr>

										<th><?php 
    _e('Unpaid Earnings', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Unpaid Earnings This Month', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Unpaid Earnings Today', 'affiliate-wp');
    ?>
</th>

									</tr>

								</thead>

								<tbody>

									<tr>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_earnings();
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_earnings('month');
    ?>
</td>
										<td><?php 
    echo affiliate_wp()->referrals->unpaid_earnings('today');
    ?>
</td>
									</tr>

								</tbody>

							</table>
						</div>
					</div>
					<div class="postbox">
						<h3><?php 
    _e('Latest Affiliate Registrations', 'affiliate-wp');
    ?>
</h3>
						<div class="inside">
							<?php 
    $affiliates = affiliate_wp()->affiliates->get_affiliates(apply_filters('affwp_overview_latest_affiliate_registrations', array('number' => 5)));
    ?>
							<table class="affwp_table">

								<thead>

									<tr>
										<th><?php 
    _e('Affiliate', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Status', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Actions', 'affiliate-wp');
    ?>
</th>
									</tr>

								</thead>

								<tbody>
									<?php 
    if ($affiliates) {
        ?>
										<?php 
        foreach ($affiliates as $affiliate) {
            ?>
	
											<tr>
												<td><?php 
            echo affiliate_wp()->affiliates->get_affiliate_name($affiliate->affiliate_id);
            ?>
</td>
												<td><?php 
            echo $affiliate->status;
            ?>
</td>
												<td>
													<?php 
            if ('pending' == $affiliate->status) {
                $review_url = admin_url('admin.php?page=affiliate-wp-affiliates&action=review_affiliate&affiliate_id=' . $affiliate->affiliate_id);
                echo '<a href="' . esc_url($review_url) . '">' . __('Review', 'affiliate-wp') . '</a>';
            } else {
                $affiliate_report_url = admin_url('admin.php?page=affiliate-wp-affiliates&action=view_affiliate&affiliate_id=' . $affiliate->affiliate_id);
                echo '<a href="' . esc_url($affiliate_report_url) . '">' . __('View Report', 'affiliate-wp') . '</a>';
            }
            ?>
												</td>
											</tr>
										<?php 
        }
        ?>
									<?php 
    } else {
        ?>
										<tr>
											<td colspan="3"><?php 
        _e('No affiliate registrations yet', 'affiliate-wp');
        ?>
</td>
										</tr>
									<?php 
    }
    ?>
								</tbody>

							</table>
		
						</div>
					</div>
					<?php 
    do_action('affwp_overview_left_bottom');
    ?>
				</div>
				<div id="postbox-container-2" class="postbox-container">
					<?php 
    do_action('affwp_overview_right_top');
    ?>
					
					<div class="postbox">
						<h3><?php 
    _e('Most Valuable Affiliates', 'affiliate-wp');
    ?>
</h3>
						<div class="inside">
							<?php 
    $affiliates = affiliate_wp()->affiliates->get_affiliates(apply_filters('affwp_overview_most_valuable_affiliates', array('number' => 5, 'orderby' => 'earnings', 'order' => 'DESC')));
    ?>
							<table class="affwp_table">

								<thead>

									<tr>
										<th><?php 
    _e('Affiliate', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Earnings', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Referrals', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Visits', 'affiliate-wp');
    ?>
</th>
									</tr>

								</thead>

								<tbody>
								<?php 
    if ($affiliates) {
        ?>
									<?php 
        foreach ($affiliates as $affiliate) {
            ?>
	
										<tr>
											<td><?php 
            echo affiliate_wp()->affiliates->get_affiliate_name($affiliate->affiliate_id);
            ?>
</td>
											<td><?php 
            echo affwp_currency_filter($affiliate->earnings);
            ?>
</td>
											<td><?php 
            echo absint($affiliate->referrals);
            ?>
</td>
											<td><?php 
            echo absint($affiliate->visits);
            ?>
</td>
										</tr>
									<?php 
        }
        ?>
								<?php 
    } else {
        ?>
									<tr>
										<td colspan="4"><?php 
        _e('No registered affiliates', 'affiliate-wp');
        ?>
</td>
									</tr>
								<?php 
    }
    ?>
								</tbody>

							</table>
						</div>
					</div>

					<div class="postbox">
						<h3><?php 
    _e('Recent Referrals', 'affiliate-wp');
    ?>
</h3>
						<div class="inside">
							<?php 
    $referrals = affiliate_wp()->referrals->get_referrals(apply_filters('affwp_overview_recent_referrals', array('number' => 5, 'status' => 'unpaid')));
    ?>
							<table class="affwp_table">

								<thead>

									<tr>
										<th><?php 
    _e('Affiliate', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Amount', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Description', 'affiliate-wp');
    ?>
</th>
									</tr>

								</thead>

								<tbody>
								<?php 
    if ($referrals) {
        ?>
									<?php 
        foreach ($referrals as $referral) {
            ?>
	
										<tr>
											<td><?php 
            echo affiliate_wp()->affiliates->get_affiliate_name($referral->affiliate_id);
            ?>
</td>
											<td><?php 
            echo affwp_currency_filter($referral->amount);
            ?>
</td>
											<td><?php 
            echo !empty($referral->description) ? esc_html($referral->description) : '';
            ?>
</td>
										</tr>
									<?php 
        }
        ?>
								<?php 
    } else {
        ?>
									<tr>
										<td colspan="3"><?php 
        _e('No referrals recorded yet', 'affiliate-wp');
        ?>
</td>
									</tr>
								<?php 
    }
    ?>
								</tbody>

							</table>
						</div>
					</div>
					<div class="postbox">
						<h3><?php 
    _e('Recent Referral Visits', 'affiliate-wp');
    ?>
</h3>
						<div class="inside">
							<?php 
    $visits = affiliate_wp()->visits->get_visits(apply_filters('affwp_overview_recent_referral_visits', array('number' => 8)));
    ?>
							<table class="affwp_table">

								<thead>

									<tr>
										<th><?php 
    _e('Affiliate', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('URL', 'affiliate-wp');
    ?>
</th>
										<th><?php 
    _e('Converted', 'affiliate-wp');
    ?>
</th>
									</tr>

								</thead>

								<tbody>
									<?php 
    if ($visits) {
        ?>
										<?php 
        foreach ($visits as $visit) {
            ?>
	
											<tr>
												<td><?php 
            echo affiliate_wp()->affiliates->get_affiliate_name($visit->affiliate_id);
            ?>
</td>
												<td><a href="<?php 
            echo esc_url($visit->url);
            ?>
"><?php 
            echo esc_html($visit->url);
            ?>
</a></td>
												<td>
													<?php 
            $converted = !empty($visit->referral_id) ? 'yes' : 'no';
            ?>
													<span class="visit-converted <?php 
            echo $converted;
            ?>
"><i></i></span>
												</td>
											</tr>
										<?php 
        }
        ?>
									<?php 
    } else {
        ?>
										<tr>
											<td colspan="3"><?php 
        _e('No referral visits recorded yet', 'affiliate-wp');
        ?>
</td>
										</tr>
									<?php 
    }
    ?>
								</tbody>

							</table>
						</div>
					</div>
					<?php 
    do_action('affwp_overview_right_bottom');
    ?>
				</div>
			</div>
		</div>
		<?php 
    do_action('affwp_overview_bottom');
    ?>
	</div>
<?php 
}
    /**
     * Get referrals
     *
     * @since  1.0
     * @return string
     */
    public function show_leaderboard($args = array())
    {
        $defaults = apply_filters('affwp_leaderboard_defaults', array('number' => isset($args['number']) ? $args['number'] : 10, 'orderby' => isset($args['orderby']) ? $args['orderby'] : 'referrals'));
        $args = wp_parse_args($args, $defaults);
        // show an affiliate's earnings
        $show_earnings = isset($args['earnings']) && ('yes' == $args['earnings'] || 'on' == $args['earnings']) ? true : false;
        // show an affiliate's referrals
        $show_referrals = isset($args['referrals']) && ('yes' == $args['referrals'] || 'on' == $args['referrals']) ? true : false;
        // show an affiliate's visits
        $show_visits = isset($args['visits']) && ('yes' == $args['visits'] || 'on' == $args['visits']) ? true : false;
        // get affiliates
        $affiliates = affiliate_wp()->affiliates->get_affiliates($defaults);
        ob_start();
        if ($affiliates) {
            ?>

		<ol class="affwp-leaderboard">
		<?php 
            foreach ($affiliates as $affiliate) {
                ?>
			<li><?php 
                // affiliate name
                echo affiliate_wp()->affiliates->get_affiliate_name($affiliate->affiliate_id);
                $to_show = apply_filters('affwp_leaderboard_to_show', array('referrals' => $show_referrals, 'earnings' => $show_earnings, 'visits' => $show_visits));
                $output = array();
                if ($to_show) {
                    foreach ($to_show as $key => $value) {
                        if ($value && $key == 'referrals') {
                            $output[] = absint($affiliate->referrals) . ' ' . __('referrals', 'affiliatewp-leaderboard');
                        }
                        if ($value && $key == 'earnings') {
                            $output[] = affwp_currency_filter(affwp_format_amount($affiliate->earnings)) . ' ' . __('earnings', 'affiliatewp-leaderboard');
                        }
                        if ($value && $key == 'visits') {
                            $output[] = absint($affiliate->visits) . ' ' . __('visits', 'affiliatewp-leaderboard');
                        }
                    }
                }
                $output = implode('&nbsp;&nbsp;<span class="divider">|</span>&nbsp;&nbsp;', $output);
                if ($output) {
                    echo '<p>' . $output . '</p>';
                }
                ?>
</li>
		<?php 
            }
            ?>
		</ol>
		<?php 
        } else {
            ?>
			<?php 
            _e('No registered affiliates', 'affiliatewp-leaderboard');
            ?>
		<?php 
        }
        ?>


		<?php 
        $html = ob_get_clean();
        return apply_filters('affwp_show_leaderboard', $html, $affiliates, $show_referrals, $show_earnings, $show_visits);
    }