/**
  * Class initialization.
  */
 static function init()
 {
     add_shortcode('affiliates_registration', array('Affiliates_Registration', 'add_shortcode'));
     add_action('wp_print_styles', array('Affiliates_Registration', 'print_styles'));
     self::$submit_button_label = __('Sign Up', AFFILIATES_PLUGIN_DOMAIN);
     // delete affiliate when user is deleted
     add_action('deleted_user', array('Affiliates_Registration', 'deleted_user'));
 }
 /**
  * Registration form.
  * 
  * @see Affiliates_Registration::$defaults for accepted parameters
  * 
  * @param array $options form options
  * @return string rendered registration form
  */
 public static function render_form($options = array())
 {
     wp_enqueue_style('affiliates');
     self::$submit_button_label = __('Sign Up', AFFILIATES_PLUGIN_DOMAIN);
     $output = '';
     //
     // Existing affiliate
     //
     if ($is_affiliate = affiliates_user_is_affiliate()) {
         $output .= '<div class="affiliates-registration registered">';
         $output .= '<p>';
         $output .= __('You are already registered as an affiliate.', AFFILIATES_PLUGIN_DOMAIN);
         $output .= '</p>';
         if (isset($options['registered_profile_link_url'])) {
             $output .= '<p>';
             $output .= '<a href="' . esc_url($options['registered_profile_link_url']) . '">';
             if (isset($options['registered_profile_link_text'])) {
                 $output .= wp_filter_kses($options['registered_profile_link_text']);
             } else {
                 $output .= __('Access your profile', AFFILIATES_PLUGIN_DOMAIN);
             }
             $output .= '</a>';
             $output .= '</p>';
         }
         $output .= '</div>';
         return $output;
     }
     //
     // Registration closed
     //
     if (!get_option('aff_registration', get_option('users_can_register', false))) {
         $output .= '<p>' . __('Registration is currently closed.', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
         return $output;
     }
     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();
     //
     // Gather user info
     //
     $user = null;
     if ($is_logged_in = is_user_logged_in()) {
         $user = wp_get_current_user();
         if (isset($registration_fields['first_name']) && $registration_fields['first_name']['enabled']) {
             $first_name = $user->first_name;
             $first_name = sanitize_user_field('first_name', $first_name, $user->ID, 'display');
             $registration_fields['first_name']['value'] = $first_name;
         }
         if (isset($registration_fields['last_name']) && $registration_fields['last_name']['enabled']) {
             $last_name = $user->last_name;
             $last_name = sanitize_user_field('last_name', $last_name, $user->ID, 'display');
             $registration_fields['last_name']['value'] = $last_name;
         }
         if (isset($registration_fields['user_login']) && $registration_fields['user_login']['enabled']) {
             $user_login = $user->user_login;
             $user_login = sanitize_user_field('user_login', $user_login, $user->ID, 'display');
             $registration_fields['user_login']['value'] = $user_login;
         }
         if (isset($registration_fields['user_email']) && $registration_fields['user_email']['enabled']) {
             $user_email = $user->user_email;
             $user_email = sanitize_user_field('email', $user_email, $user->ID, 'display');
             $registration_fields['user_email']['value'] = $user_email;
         }
         if (isset($registration_fields['user_url']) && $registration_fields['user_url']['enabled']) {
             $url = $user->user_url;
             $url = sanitize_user_field('user_url', $url, $user->ID, 'display');
             $registration_fields['user_url']['value'] = $url;
         }
     }
     $submit_name = 'affiliates-registration-submit';
     $nonce = 'affiliates-registration-nonce';
     $nonce_action = 'affiliates-registration';
     $send = false;
     $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 = !empty($_POST[Affiliates_Utility::get_captcha_field_id()]) ? $_POST[Affiliates_Utility::get_captcha_field_id()] : null;
         if (!Affiliates_Utility::captcha_validates($captcha)) {
             $error = true;
             // dumbot
         }
         // gather field values
         foreach ($registration_fields as $name => $field) {
             if ($field['enabled']) {
                 $value = isset($_POST[$name]) ? $_POST[$name] : '';
                 $value = Affiliates_Utility::filter($value);
                 if ($field['required'] && empty($value)) {
                     $error = true;
                     $output .= '<div class="error">';
                     $output .= __('<strong>ERROR</strong>', AFFILIATES_PLUGIN_DOMAIN);
                     $output .= ' : ';
                     $output .= sprintf(__('Please fill out the field <em>%s</em>.', AFFILIATES_PLUGIN_DOMAIN), $field['label']);
                     $output .= '</div>';
                 }
                 $registration_fields[$name]['value'] = $value;
             }
         }
         $error = apply_filters('affiliates_registration_error_validate', $error);
         if (!$error) {
             $userdata = array();
             foreach ($registration_fields as $name => $field) {
                 if ($registration_fields[$name]['enabled']) {
                     $userdata[$name] = $registration_fields[$name]['value'];
                 }
             }
             // don't try to create a new user on multiple renderings
             global $affiliate_user_id, $new_affiliate_registered, $stored_affiliate;
             if (!isset($affiliate_user_id)) {
                 if (!$is_logged_in) {
                     // allow plugins to be aware of new user account being created
                     do_action('affiliates_before_register_affiliate', $userdata);
                     // create the affiliate user account
                     $affiliate_user_id = self::register_affiliate($userdata);
                     $new_affiliate_registered = true;
                     do_action('affiliates_after_register_affiliate', $userdata);
                 } else {
                     $affiliate_user_id = $user->ID;
                     $new_affiliate_registered = true;
                 }
             }
             // register as affiliate
             if (!is_wp_error($affiliate_user_id)) {
                 // add affiliate entry
                 $send = true;
                 if (!isset($stored_affiliate)) {
                     if ($new_affiliate_registered) {
                         $affiliate_id = self::store_affiliate($affiliate_user_id, $userdata);
                         // update user including meta
                         self::update_affiliate_user($affiliate_user_id, $userdata);
                         do_action('affiliates_stored_affiliate', $affiliate_id, $affiliate_user_id);
                     }
                     $stored_affiliate = true;
                 }
                 $is_widget = isset($options['is_widget']) && ($options['is_widget'] === true || $options['is_widget'] == 'true');
                 $redirect = isset($options['redirect']) && ($options['redirect'] === true || $options['redirect'] == 'true');
                 $redirect_url = empty($_REQUEST['redirect_to']) ? apply_filters('affiliates_registration_login_redirect_url', get_site_url(get_current_blog_id(), 'wp-login.php?checkemail=confirm')) : $_REQUEST['redirect_to'];
                 if ($redirect && !$is_widget && !headers_sent()) {
                     wp_safe_redirect($redirect_url);
                     exit;
                 } else {
                     $output .= '<p>' . __('Thanks for signing up!', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
                     if (!$is_logged_in) {
                         $output .= '<p>' . __('Please check your email for the confirmation link.', AFFILIATES_PLUGIN_DOMAIN) . '</p>';
                         if ($redirect && !$is_widget) {
                             $output .= '<script type="text/javascript">window.location="' . esc_url($redirect_url) . '";</script>';
                         } else {
                             $output .= '<p>';
                             $output .= sprintf(__('Log in <a href="%s">here</a>.', AFFILIATES_PLUGIN_DOMAIN), esc_url(apply_filters('affiliates_registration_login_redirect_url', get_site_url(get_current_blog_id(), 'wp-login.php?checkemail=confirm'))));
                             $output .= '</p>';
                         }
                     } else {
                         if (isset($options['registered_profile_link_url'])) {
                             $output .= '<p>';
                             $output .= '<a href="' . esc_url($options['registered_profile_link_url']) . '">';
                             if (isset($options['registered_profile_link_text'])) {
                                 $output .= wp_filter_kses($options['registered_profile_link_text']);
                             } else {
                                 $output .= __('Access your profile', AFFILIATES_PLUGIN_DOMAIN);
                             }
                             $output .= '</a>';
                             $output .= '</p>';
                         }
                     }
                 }
             } else {
                 // is_wp_error( $affiliate_user_id ), user registration failed
                 $error = true;
                 $wp_error = $affiliate_user_id;
                 if ($wp_error->get_error_code()) {
                     $errors = array();
                     $messages = array();
                     foreach ($wp_error->get_error_codes() as $code) {
                         $severity = $wp_error->get_error_data($code);
                         foreach ($wp_error->get_error_messages($code) as $error) {
                             if ('message' == $severity) {
                                 $messages[] = $error;
                             } else {
                                 $errors[] = $error;
                             }
                         }
                     }
                     if (!empty($errors)) {
                         $output .= '<div class="error">';
                         $output .= apply_filters('login_errors', implode('<br />', $errors));
                         $output .= '</div>';
                     }
                     if (!empty($messages)) {
                         $output .= '<div class="message">';
                         $output .= apply_filters('login_messages', implode('<br />', $messages));
                         $output .= '</div>';
                     }
                 }
             }
         }
     }
     // Registration form
     if (!$send) {
         if (isset($options['terms_post_id'])) {
             $terms_post = get_post($options['terms_post_id']);
             if ($terms_post) {
                 $terms_post_link = '<a target="_blank" href="' . esc_url(get_permalink($terms_post->ID)) . '">' . get_the_title($terms_post->ID) . '</a>';
                 $terms = sprintf(apply_filters('affiliates_terms_post_link_text', __('By signing up, you indicate that you have read and agree to the %s.', AFFILIATES_PLUGIN_DOMAIN)), $terms_post_link);
             }
         }
         $output .= '<div class="affiliates-registration" id="affiliates-registration">';
         $output .= '<img id="affiliates-registration-throbber" src="' . AFFILIATES_PLUGIN_URL . 'images/affiliates-throbber.gif" style="display:none" />';
         $output .= '<form id="affiliates-registration-form" method="post">';
         $output .= '<div>';
         $output .= apply_filters('affiliates_registration_before_fields', '');
         $output .= self::render_fields($registration_fields);
         $output .= apply_filters('affiliates_registration_after_fields', '');
         if (isset($terms)) {
             $output .= '<div class="terms">' . $terms . '</div>';
         }
         $output .= Affiliates_Utility::captcha_get($captcha);
         $output .= wp_nonce_field($nonce_action, $nonce, true, false);
         if (isset($options['redirect_to'])) {
             $output .= '<input type="hidden" name="redirect_to" value="' . esc_url($options['redirect_to']) . '" />';
         }
         $output .= '<div class="sign-up">';
         $output .= '<input type="submit" name="' . $submit_name . '" value="' . self::$submit_button_label . '" />';
         $output .= '</div>';
         $output .= '</div>';
         $output .= '</form>';
         $output .= '</div>';
     }
     return $output;
 }