/**
  * Record a referral when a new user has been referred by an affiliate.
  * 
  * @param int $user_id
  * @param array $params registration parameters
  */
 public static function user_register($user_id, $params = array())
 {
     extract($params);
     if (!isset($force)) {
         $force = false;
     }
     if (!isset($type)) {
         $type = null;
     }
     if (!$force && is_admin()) {
         if (!apply_filters('affiliates_user_registration_on_admin', false)) {
             return;
         }
     }
     if ($user = get_user_by('id', $user_id)) {
         $post_id = null;
         if ($post = get_post()) {
             $post_id = $post->ID;
         }
         switch ($type) {
             case 'customer':
                 $description = sprintf('Customer Registration %s', esc_html($user->user_login));
                 break;
             default:
                 $description = sprintf('User Registration %s', esc_html($user->user_login));
         }
         $base_amount = null;
         if (AFFILIATES_PLUGIN_NAME != 'affiliates') {
             $base_amount = get_option('aff_user_registration_base_amount', null);
         }
         $amount = null;
         if (empty($base_amount)) {
             $amount = get_option('aff_user_registration_amount', '0');
         }
         $currency = get_option('aff_user_registration_currency', Affiliates::DEFAULT_CURRENCY);
         $user_registration_referral_status = get_option('aff_user_registration_referral_status', get_option('aff_default_referral_status', AFFILIATES_REFERRAL_STATUS_ACCEPTED));
         $data = array('user_login' => array('title' => 'Username', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $user->user_login), 'user_email' => array('title' => 'Email', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $user->user_email), 'first_name' => array('title' => 'First Name', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $user->first_name), 'last_name' => array('title' => 'Last Name', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $user->last_name), 'base_amount' => array('title' => 'Base Amount', 'domain' => AFFILIATES_PLUGIN_DOMAIN, 'value' => $base_amount));
         if (class_exists('Affiliates_Referral_WordPress')) {
             $r = new Affiliates_Referral_WordPress();
             $affiliate_id = $r->evaluate($post_id, $description, $data, $base_amount, $amount, $currency, $user_registration_referral_status, self::REFERRAL_TYPE);
         } else {
             $affiliate_id = affiliates_suggest_referral($post_id, $description, $data, $amount, $currency, $user_registration_referral_status, self::REFERRAL_TYPE);
         }
     }
 }
 /**
  * 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>';
     }
 }
 /**
  * Record a referral when a new order has been processed.
  * 
  * Note that we can't hook into the order process before(*), because
  * the meta data would not have been added.
  * 
  * (*) We could hook into woocommerce_checkout_update_order_meta but the
  * 'coupons' meta data would not be there, so if we want to use it here at
  * some point, woocommerce_checkout_order_processed is a better choice.
  * @param int $order_id the post id of the order
  */
 public static function woocommerce_checkout_order_processed($order_id)
 {
     $order_subtotal = null;
     $currency = get_option('woocommerce_currency');
     if (function_exists('wc_get_order')) {
         if ($order = wc_get_order($order_id)) {
             if (method_exists($order, 'get_subtotal')) {
                 $order_subtotal = $order->get_subtotal();
             }
             if (method_exists($order, 'get_total_discount')) {
                 $order_subtotal -= $order->get_total_discount();
                 // excluding tax
                 if ($order_subtotal < 0) {
                     $order_subtotal = 0;
                 }
             }
             if (method_exists($order, 'get_order_currency')) {
                 $currency = $order->get_order_currency();
             }
         }
     }
     if ($order_subtotal === null) {
         $order_total = get_post_meta($order_id, '_order_total', true);
         $order_tax = get_post_meta($order_id, '_order_tax', true);
         $order_shipping = get_post_meta($order_id, '_order_shipping', true);
         $order_shipping_tax = get_post_meta($order_id, '_order_shipping_tax', true);
         $order_subtotal = $order_total - $order_tax - $order_shipping - $order_shipping_tax;
     }
     $order_link = '<a href="' . admin_url('post.php?post=' . $order_id . '&action=edit') . '">';
     $order_link .= sprintf(__('Order #%s', AFF_WOOCOMMERCE_LIGHT_PLUGIN_DOMAIN), $order_id);
     $order_link .= "</a>";
     $data = array('order_id' => array('title' => 'Order #', 'domain' => AFF_WOOCOMMERCE_LIGHT_PLUGIN_DOMAIN, 'value' => esc_sql($order_id)), 'order_total' => array('title' => 'Total', 'domain' => AFF_WOOCOMMERCE_LIGHT_PLUGIN_DOMAIN, 'value' => esc_sql($order_subtotal)), 'order_currency' => array('title' => 'Currency', 'domain' => AFF_WOOCOMMERCE_LIGHT_PLUGIN_DOMAIN, 'value' => esc_sql($currency)), 'order_link' => array('title' => 'Order', 'domain' => AFF_WOOCOMMERCE_LIGHT_PLUGIN_DOMAIN, 'value' => esc_sql($order_link)));
     $options = get_option(self::PLUGIN_OPTIONS, array());
     $referral_rate = isset($options[self::REFERRAL_RATE]) ? $options[self::REFERRAL_RATE] : self::REFERRAL_RATE_DEFAULT;
     $amount = round(floatval($referral_rate) * floatval($order_subtotal), AFFILIATES_REFERRAL_AMOUNT_DECIMALS);
     $description = sprintf('Order #%s', $order_id);
     affiliates_suggest_referral($order_id, $description, $data, $amount, $currency);
 }