/**
  * Handles login redirections.
  *
  * @package s2Member\Login_Redirects
  * @since 3.5
  *
  * @attaches-to ``add_action('wp_login');``
  *
  * @param string  $username Expects Username.
  * @param WP_User $user Expects a WP_User object instance.
  *
  * @return null Or exits script execution after a redirection takes place.
  */
 public static function login_redirect($username = '', $user = NULL)
 {
     foreach (array_keys(get_defined_vars()) as $__v) {
         $__refs[$__v] =& ${$__v};
     }
     do_action('ws_plugin__s2member_before_login_redirect', get_defined_vars());
     unset($__refs, $__v);
     // Housekeeping.
     $ci = $GLOBALS['WS_PLUGIN__']['s2member']['o']['ruris_case_sensitive'] ? '' : 'i';
     if (is_string($username) && $username && is_object($user) && !empty($user->ID) && ($user_id = $user->ID)) {
         update_user_option($user_id, 's2member_last_login_time', time());
         $logins = (int) get_user_option('s2member_login_counter', $user_id) + 1;
         update_user_option($user_id, 's2member_login_counter', $logins);
         if (!get_user_option('s2member_registration_ip', $user_id)) {
             update_user_option($user_id, 's2member_registration_ip', $_SERVER['REMOTE_ADDR']);
         }
         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password']) {
             delete_user_setting('default_password_nag');
             update_user_option($user_id, 'default_password_nag', FALSE, TRUE);
         }
         $ok = TRUE;
         // Initialize IP restriction being OK here. This is for filters.
         if ($username !== 'demo' && !is_super_admin($user_id) && !apply_filters('ws_plugin__s2member_disable_login_ip_restrictions', $user->has_cap('edit_posts') ? TRUE : FALSE, get_defined_vars())) {
             $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok($_SERVER['REMOTE_ADDR'], strtolower($username));
         }
         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_redirection_always_http']) {
             // Alter value of `redirect_to`?
             if (!empty($_REQUEST['redirect_to']) && is_string($_REQUEST['redirect_to']) && strpos($_REQUEST['redirect_to'], 'wp-admin') === FALSE) {
                 $_REQUEST['redirect_to'] = preg_replace('/^https\\:\\/\\//i', 'http://', $_REQUEST['redirect_to']);
                 if (stripos($_REQUEST['redirect_to'], 'http://') !== 0) {
                     $redirect_uri = $_REQUEST['redirect_to'];
                     // e.g., `/path/with/?query=args`
                     $home_path = trim((string) @parse_url(home_url('/'), PHP_URL_PATH), '/');
                     $http_home_base = trim(preg_replace('/\\/' . preg_quote($home_path, '/') . '\\/$/' . $ci, '', home_url('/', 'http')), '/');
                     $_REQUEST['redirect_to'] = $http_home_base . '/' . ltrim($redirect_uri, '/');
                 }
             }
         }
         if ($redirect = apply_filters('ws_plugin__s2member_login_redirect', $user->has_cap('edit_posts') ? FALSE : TRUE, get_defined_vars())) {
             $obey_redirect_to = apply_filters('ws_plugin__s2member_obey_login_redirect_to', TRUE, get_defined_vars());
             if ($obey_redirect_to && (empty($_REQUEST['redirect_to']) || !is_string($_REQUEST['redirect_to']) || $_REQUEST['redirect_to'] === admin_url() || preg_match('/^\\/?wp-admin\\/?$/' . $ci, $_REQUEST['redirect_to']))) {
                 $obey_redirect_to = FALSE;
             } else {
                 if ($obey_redirect_to && !empty($_REQUEST['redirect_to_automatic']) && is_string($redirect)) {
                     $obey_redirect_to = FALSE;
                 }
             }
             // Do not obey automatic redirects when a custom redirection filter applies.
             // ↑ NOTE: this will apply to s2Member Pro's One-Time-Offers (Upon Login) also.
             if (!$obey_redirect_to) {
                 foreach (array_keys(get_defined_vars()) as $__v) {
                     $__refs[$__v] =& ${$__v};
                 }
                 do_action('ws_plugin__s2member_during_login_redirect', get_defined_vars());
                 unset($__refs, $__v);
                 // Housekeeping.
                 $is_lwp = FALSE;
                 // Initialize LWP detection flag.
                 if ($redirect && is_string($redirect)) {
                     $redirect = $redirect;
                 } else {
                     if ($login_redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user)) {
                         $is_lwp = TRUE;
                         // Flag as being a hard-coded LWP URL in this case.
                         $redirect = $login_redirection_url;
                         // Special redirection URL.
                     } else {
                         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']) {
                             $is_lwp = TRUE;
                             // Flag as being a hard-coded LWP URL in this case.
                             $redirect = get_page_link($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']);
                         } else {
                             $redirect = home_url('/');
                         }
                     }
                 }
                 // Default to the home page.
                 if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_redirection_always_http']) {
                     $redirect = preg_replace('/^https\\:\\/\\//i', 'http://', $redirect);
                     if (stripos($redirect, 'http://') !== 0) {
                         $redirect_uri = $redirect;
                         // e.g., `/path/with/?query=args`
                         $home_path = trim((string) @parse_url(home_url('/'), PHP_URL_PATH), '/');
                         $http_home_base = trim(preg_replace('/\\/' . preg_quote($home_path, '/') . '\\/$/' . $ci, '', home_url('/', 'http')), '/');
                         $redirect = $http_home_base . '/' . ltrim($redirect_uri, '/');
                     }
                 }
                 if ($is_lwp) {
                     // Allow offsite redirection?
                     wp_redirect($redirect);
                 } else {
                     wp_safe_redirect($redirect);
                 }
                 // Default behavior.
                 exit;
                 // Stop here; redirecting now.
             }
         }
     }
     do_action('ws_plugin__s2member_after_login_redirect', get_defined_vars());
 }
 /**
  * Handles login redirections.
  *
  * @package s2Member\Login_Redirects
  * @since 3.5
  *
  * @attaches-to ``add_action("wp_login");``
  *
  * @param str $username Expects Username.
  * @param WP_User $user Expects a WP_User object instance.
  * @return null Or exits script execution after a redirection takes place.
  */
 public static function login_redirect($username = FALSE, $user = FALSE)
 {
     foreach (array_keys(get_defined_vars()) as $__v) {
         $__refs[$__v] =& ${$__v};
     }
     do_action("ws_plugin__s2member_before_login_redirect", get_defined_vars());
     unset($__refs, $__v);
     if (is_string($username) && $username && is_object($user) && !empty($user->ID) && ($user_id = $user->ID)) {
         update_user_option($user_id, "s2member_last_login_time", time());
         if (!get_user_option("s2member_registration_ip", $user_id)) {
             update_user_option($user_id, "s2member_registration_ip", $_SERVER["REMOTE_ADDR"]);
         }
         if (($logins = (int) get_user_option("s2member_login_counter", $user_id) + 1) >= 1 || ($logins = 1)) {
             update_user_option($user_id, "s2member_login_counter", $logins);
         }
         if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"]) {
             delete_user_setting("default_password_nag") . update_user_option($user_id, "default_password_nag", false, true);
         }
         $disable_login_ip_restrictions = apply_filters("ws_plugin__s2member_disable_login_ip_restrictions", false, get_defined_vars());
         if (($ok = true) && !is_super_admin($user_id) && $username !== "demo" && !$disable_login_ip_restrictions) {
             $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok($_SERVER["REMOTE_ADDR"], $username);
         }
         if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_redirection_always_http"]) {
             // Alter value of `redirect_to`?
             if (!empty($_REQUEST["redirect_to"]) && is_string($_REQUEST["redirect_to"]) && strpos($_REQUEST["redirect_to"], "wp-admin") === FALSE) {
                 $_REQUEST["redirect_to"] = preg_replace("/^https\\:\\/\\//i", "http://", $_REQUEST["redirect_to"]);
                 if (stripos($_REQUEST["redirect_to"], "http://") !== 0) {
                     // Force an absolute URL in this case.
                     $_REQUEST["redirect_to"] = home_url($_REQUEST["redirect_to"], "http");
                 }
             }
         }
         if ($redirect = apply_filters("ws_plugin__s2member_login_redirect", $user->has_cap("edit_posts") ? false : true, get_defined_vars())) {
             $obey_redirect_to = apply_filters("ws_plugin__s2member_obey_login_redirect_to", true, get_defined_vars());
             if (!$obey_redirect_to || empty($_REQUEST["redirect_to"]) || !is_string($_REQUEST["redirect_to"]) || $_REQUEST["redirect_to"] === admin_url() || preg_match("/^\\/?wp-admin\\/?\$/", $_REQUEST["redirect_to"])) {
                 foreach (array_keys(get_defined_vars()) as $__v) {
                     $__refs[$__v] =& ${$__v};
                 }
                 do_action("ws_plugin__s2member_during_login_redirect", get_defined_vars());
                 unset($__refs, $__v);
                 if ($redirect && is_string($redirect)) {
                     $redirect = $redirect;
                 } else {
                     if ($redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user)) {
                         $redirect = $redirection_url;
                     } else {
                         // Else we use the Login Welcome Page configured for s2Member.
                         $redirect = get_page_link($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_welcome_page"]);
                     }
                 }
                 if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_redirection_always_http"]) {
                     $redirect = preg_replace("/^https\\:\\/\\//i", "http://", $redirect);
                     if (stripos($redirect, "http://") !== 0) {
                         // Force an absolute URL in this case.
                         $redirect = home_url($redirect, "http");
                     }
                 }
                 wp_redirect($redirect) . exit;
             }
         }
     }
     foreach (array_keys(get_defined_vars()) as $__v) {
         $__refs[$__v] =& ${$__v};
     }
     do_action("ws_plugin__s2member_after_login_redirect", get_defined_vars());
     unset($__refs, $__v);
     return;
 }
 /**
  * Handles login redirections.
  *
  * @package s2Member\Login_Redirects
  * @since 3.5
  *
  * @attaches-to ``add_action("wp_login");``
  *
  * @param str $username Expects Username to be passed in by the Action Hook.
  * @return null Or exits script execution after a redirection takes place.
  */
 public static function login_redirect($username = FALSE)
 {
     eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
     do_action("ws_plugin__s2member_before_login_redirect", get_defined_vars());
     unset($__refs, $__v);
     /* Unset defined __refs, __v. */
     /**/
     $username = !$username && is_object($user = wp_get_current_user()) && !empty($user->user_login) ? strtolower($user->user_login) : strtolower($username);
     /**/
     if ($username && (isset($user) && is_object($user) || is_object($user = new WP_User($username))) && !empty($user->ID) && ($user_id = $user->ID)) {
         if (!get_user_option("s2member_registration_ip", $user_id)) {
             /* Have we got this yet? */
             update_user_option($user_id, "s2member_registration_ip", $_SERVER["REMOTE_ADDR"]);
         }
         /**/
         if (($logins = (int) get_user_option("s2member_login_counter", $user_id) + 1) >= 1 || ($logins = 1)) {
             update_user_option($user_id, "s2member_login_counter", $logins);
         }
         /**/
         if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_password"]) {
             /* Nag em? */
             delete_user_setting("default_password_nag") . update_user_option($user_id, "default_password_nag", false, true);
         }
         /**/
         $disable_login_ip_restrictions = apply_filters("ws_plugin__s2member_disable_login_ip_restrictions", false, get_defined_vars());
         /**/
         if (($ok = true) && !is_super_admin($user_id) && $username !== "demo" && !$disable_login_ip_restrictions) {
             $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok($_SERVER["REMOTE_ADDR"], $username);
         }
         /**/
         if ($redirect = apply_filters("ws_plugin__s2member_login_redirect", $user->has_cap("edit_posts") ? false : true, get_defined_vars())) {
             $obey_redirect_to = apply_filters("ws_plugin__s2member_obey_login_redirect_to", true, get_defined_vars());
             /**/
             if (!$obey_redirect_to || empty($_REQUEST["redirect_to"]) || !is_string($_REQUEST["redirect_to"]) || $_REQUEST["redirect_to"] === admin_url() || preg_match("/^\\/?wp-admin\\/?\$/", $_REQUEST["redirect_to"])) {
                 eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
                 do_action("ws_plugin__s2member_during_login_redirect", get_defined_vars());
                 unset($__refs, $__v);
                 /* Unset defined __refs, __v. */
                 /**/
                 if ($redirect && is_string($redirect)) {
                     /* Is this a string? */
                     wp_redirect($redirect);
                 } else {
                     if ($redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user)) {
                         wp_redirect($redirection_url);
                     } else {
                         /* Else we use the Login Welcome Page configured for s2Member. */
                         wp_redirect(get_page_link($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["login_welcome_page"]));
                     }
                 }
                 /**/
                 exit;
                 /* Clean exit. */
             }
         }
     }
     /**/
     eval('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
     do_action("ws_plugin__s2member_after_login_redirect", get_defined_vars());
     unset($__refs, $__v);
     /* Unset defined __refs, __v. */
     /**/
     return;
     /* Return for uniformity. */
 }
 /**
  * Handles Specific Post/Page Access authentication.
  *
  * @package s2Member\SP_Access
  * @since 3.5
  *
  * @param int|string $sp_id Numeric Post/Page ID in WordPress.
  * @param bool $read_only Optional. Defaults to false. If ``$read_only = true``,
  * 	no session cookies are set, no IP Restrictions are checked, and script execution is not exited on Link failure.
  * 	In other words, with ``$read_only = true``, this function will simply return true or false.
  * @return null|bool Always returns `true` if access is indeed allowed in one way or another.
  * 	If access is denied with ``$read_only = true`` simply return false. If access is denied with ``$read_only = false``, return false; but if a Specific Post/Page Access Link is currently being used, we exit with a warning about Access Link expiration here.
  */
 public static function sp_access($sp_id = FALSE, $read_only = FALSE)
 {
     do_action("ws_plugin__s2member_before_sp_access", get_defined_vars());
     $excluded = apply_filters("ws_plugin__s2member_sp_access_excluded", false, get_defined_vars());
     if ($excluded || current_user_can(apply_filters("ws_plugin__s2member_sp_access_excluded_cap", "edit_posts", get_defined_vars()))) {
         return apply_filters("ws_plugin__s2member_sp_access", true, get_defined_vars(), "auth-via-exclusion");
     } else {
         if ($sp_id && is_numeric($sp_id) && (!empty($_GET["s2member_sp_access"]) && ($_g["s2member_sp_access"] = trim(stripslashes((string) $_GET["s2member_sp_access"]))) && is_array($sp_access_values = array($_g["s2member_sp_access"])) || is_array($sp_access_values = c_ws_plugin__s2member_sp_access::sp_access_session())) && !empty($sp_access_values)) {
             foreach ($sp_access_values as $sp_access_value) {
                 if (is_array($sp_access = preg_split("/\\:\\.\\:\\|\\:\\.\\:/", c_ws_plugin__s2member_utils_encryption::decrypt($sp_access_value)))) {
                     if (count($sp_access) === 4 && $sp_access[0] === "sp_time_hours" && in_array($sp_id, preg_split("/[\r\n\t\\s;,]+/", $sp_access[1]))) {
                         if (is_numeric($sp_access[2]) && is_numeric($sp_access[3]) && $sp_access[2] <= strtotime("now") && $sp_access[2] + $sp_access[3] * 3600 >= strtotime("now")) {
                             if (!$read_only && !empty($_g["s2member_sp_access"])) {
                                 // Add to session?
                                 c_ws_plugin__s2member_sp_access::sp_access_session($_g["s2member_sp_access"]);
                             }
                             if ($read_only || c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok($_SERVER["REMOTE_ADDR"], $sp_access_value)) {
                                 return apply_filters("ws_plugin__s2member_sp_access", true, get_defined_vars(), "auth-via-link-session");
                             }
                         }
                     }
                 }
             }
             // Otherwise, authentication was NOT possible via link or session.
             if (!$read_only && !empty($_g["s2member_sp_access"])) {
                 status_header(503);
                 header("Content-Type: text/html; charset=UTF-8");
                 while (@ob_end_clean()) {
                 }
                 // Clean any existing output buffers.
                 exit(_x('<strong>Your Link Expired:</strong><br />Please contact Support if you need assistance.', "s2member-front", "s2member"));
             } else {
                 // Else return false here.
                 return apply_filters("ws_plugin__s2member_sp_access", false, get_defined_vars(), "no-auth-via-link-session");
             }
         } else {
             // Else return false here.
             return apply_filters("ws_plugin__s2member_sp_access", false, get_defined_vars(), "no-auth-no-link-session");
         }
     }
 }
 /**
  * Handles login redirections.
  *
  * @package s2Member\Login_Redirects
  * @since 3.5
  *
  * @attaches-to ``add_action('wp_login');``
  *
  * @param string  $username Expects Username.
  * @param WP_User $user Expects a WP_User object instance.
  *
  * @return null Or exits script execution after a redirection takes place.
  */
 public static function login_redirect($username = '', $user = NULL)
 {
     foreach (array_keys(get_defined_vars()) as $__v) {
         $__refs[$__v] =& ${$__v};
     }
     do_action('ws_plugin__s2member_before_login_redirect', get_defined_vars());
     unset($__refs, $__v);
     // Housekeeping.
     if (is_string($username) && $username && is_object($user) && !empty($user->ID) && ($user_id = $user->ID)) {
         update_user_option($user_id, 's2member_last_login_time', time());
         if (!get_user_option('s2member_registration_ip', $user_id)) {
             update_user_option($user_id, 's2member_registration_ip', $_SERVER['REMOTE_ADDR']);
         }
         if (($logins = (int) get_user_option('s2member_login_counter', $user_id) + 1) >= 1 || ($logins = 1)) {
             update_user_option($user_id, 's2member_login_counter', $logins);
         }
         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_password']) {
             delete_user_setting('default_password_nag') . update_user_option($user_id, 'default_password_nag', FALSE, TRUE);
         }
         if (($ok = TRUE) && !is_super_admin($user_id) && $username !== 'demo' && !apply_filters('ws_plugin__s2member_disable_login_ip_restrictions', $user->has_cap('edit_posts') ? TRUE : FALSE, get_defined_vars())) {
             $ok = c_ws_plugin__s2member_ip_restrictions::ip_restrictions_ok($_SERVER['REMOTE_ADDR'], strtolower($username));
         }
         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_redirection_always_http']) {
             // Alter value of `redirect_to`?
             if (!empty($_REQUEST['redirect_to']) && is_string($_REQUEST['redirect_to']) && strpos($_REQUEST['redirect_to'], 'wp-admin') === FALSE) {
                 $_REQUEST['redirect_to'] = preg_replace('/^https\\:\\/\\//i', 'http://', $_REQUEST['redirect_to']);
                 if (stripos($_REQUEST['redirect_to'], 'http://') !== 0) {
                     $home_path = trim((string) @parse_url(home_url('/'), PHP_URL_PATH), '/');
                     $http_home_base = trim(preg_replace('/\\/' . preg_quote($home_path, '/') . '\\/$/', '', home_url('/', 'http')), '/');
                     $_REQUEST['redirect_to'] = $http_home_base . '/' . ltrim($_REQUEST['redirect_to'], '/');
                 }
             }
         }
         if ($redirect = apply_filters('ws_plugin__s2member_login_redirect', $user->has_cap('edit_posts') ? FALSE : TRUE, get_defined_vars())) {
             $obey_redirect_to = apply_filters('ws_plugin__s2member_obey_login_redirect_to', TRUE, get_defined_vars());
             if ($obey_redirect_to && (empty($_REQUEST['redirect_to']) || !is_string($_REQUEST['redirect_to']) || $_REQUEST['redirect_to'] === admin_url() || preg_match('/^\\/?wp-admin\\/?$/', $_REQUEST['redirect_to']))) {
                 $obey_redirect_to = FALSE;
             } else {
                 if ($obey_redirect_to && !empty($_REQUEST['redirect_to_automatic']) && is_string($redirect)) {
                     $obey_redirect_to = FALSE;
                 }
             }
             // Do not obey automatic redirects when a custom redirection filter applies.
             // ↑ NOTE: this will apply to s2Member Pro's One-Time-Offers (Upon Login) also.
             if (!$obey_redirect_to) {
                 foreach (array_keys(get_defined_vars()) as $__v) {
                     $__refs[$__v] =& ${$__v};
                 }
                 do_action('ws_plugin__s2member_during_login_redirect', get_defined_vars());
                 unset($__refs, $__v);
                 // Housekeeping.
                 if ($redirect && is_string($redirect)) {
                     $redirect = $redirect;
                 } else {
                     if ($redirection_url = c_ws_plugin__s2member_login_redirects::login_redirection_url($user)) {
                         $redirect = $redirection_url;
                     } else {
                         if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']) {
                             // Else we use the Login Welcome Page configured for s2Member.
                             $redirect = get_page_link($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_welcome_page']);
                         } else {
                             $redirect = home_url('/');
                         }
                     }
                 }
                 // Default to the home page.
                 if ($GLOBALS['WS_PLUGIN__']['s2member']['o']['login_redirection_always_http']) {
                     $redirect = preg_replace('/^https\\:\\/\\//i', 'http://', $redirect);
                     if (stripos($redirect, 'http://') !== 0) {
                         $home_path = trim((string) @parse_url(home_url('/'), PHP_URL_PATH), '/');
                         $http_home_base = trim(preg_replace('/\\/' . preg_quote($home_path, '/') . '\\/$/', '', home_url('/', 'http')), '/');
                         $redirect = $http_home_base . '/' . ltrim($redirect, '/');
                     }
                 }
                 wp_redirect($redirect) . exit;
             }
         }
     }
     foreach (array_keys(get_defined_vars()) as $__v) {
         $__refs[$__v] =& ${$__v};
     }
     do_action('ws_plugin__s2member_after_login_redirect', get_defined_vars());
     unset($__refs, $__v);
     // Housekeeping.
 }