PS_HIDE_LOGIN_BASENAME ) ) {
$out .= '
' . sprintf( __( 'To set a networkwide default, go to Network Settings .', 'wps-hide-login' ), network_admin_url( 'settings.php#whl_settings' ) ) . '
';
}
echo $out;
}
public function whl_page_input() {
if ( get_option( 'permalink_structure' ) ) {
echo '' . trailingslashit( home_url() ) . '
' . ( $this->use_trailing_slashes() ? ' /
' : '' );
} else {
echo '' . trailingslashit( home_url() ) . '?
';
}
echo '' . __( 'Protect your website by changing the login URL and preventing access to the wp-login.php page and the wp-admin directory to non-connected people.', 'wps-hide-login' ) . '
';
}
public function whl_redirect_admin_input() {
if ( get_option( 'permalink_structure' ) ) {
echo '' . trailingslashit( home_url() ) . '
' . ( $this->use_trailing_slashes() ? ' /
' : '' );
} else {
echo '' . trailingslashit( home_url() ) . '?
';
}
echo '' . __( 'Redirect URL when someone tries to access the wp-login.php page and the wp-admin directory while not logged in.', 'wps-hide-login' ) . '
';
}
public function admin_notices() {
global $pagenow;
$out = '';
if ( ! is_network_admin()
&& $pagenow === 'options-general.php'
&& isset( $_GET['settings-updated'] )
&& ! isset( $_GET['page'] ) ) {
echo '' . sprintf( __( 'Your login page is now here: %2$s . Bookmark this page!', 'wps-hide-login' ), $this->new_login_url(), $this->new_login_url() ) . '
';
}
}
public function plugin_action_links( $links ) {
if ( is_network_admin()
&& is_plugin_active_for_network( WPS_HIDE_LOGIN_BASENAME ) ) {
array_unshift( $links, '' . __( 'Settings', 'wps-hide-login' ) . ' ' );
} elseif ( ! is_network_admin() ) {
array_unshift( $links, '' . __( 'Settings', 'wps-hide-login' ) . ' ' );
}
return $links;
}
public function redirect_export_data() {
if ( ! empty( $_GET ) && isset( $_GET['action'] ) && 'confirmaction' === $_GET['action'] && isset( $_GET['request_id'] ) && isset( $_GET['confirm_key'] ) ) {
$request_id = (int) $_GET['request_id'];
$key = sanitize_text_field( wp_unslash( $_GET['confirm_key'] ) );
$result = wp_validate_user_request_key( $request_id, $key );
if ( ! is_wp_error( $result ) ) {
wp_redirect( add_query_arg( array(
'action' => 'confirmaction',
'request_id' => $_GET['request_id'],
'confirm_key' => $_GET['confirm_key']
), $this->new_login_url()
) );
exit();
}
}
}
public function plugins_loaded() {
global $pagenow;
if ( ! is_multisite()
&& ( strpos( rawurldecode( $_SERVER['REQUEST_URI'] ), 'wp-signup' ) !== false
|| strpos( rawurldecode( $_SERVER['REQUEST_URI'] ), 'wp-activate' ) !== false ) && apply_filters( 'wps_hide_login_signup_enable', false ) === false ) {
wp_die( __( 'This feature is not enabled.', 'wps-hide-login' ) );
}
$request = parse_url( rawurldecode( $_SERVER['REQUEST_URI'] ) );
if ( ( strpos( rawurldecode( $_SERVER['REQUEST_URI'] ), 'wp-login.php' ) !== false
|| ( isset( $request['path'] ) && untrailingslashit( $request['path'] ) === site_url( 'wp-login', 'relative' ) ) )
&& ! is_admin() ) {
$this->wp_login_php = true;
$_SERVER['REQUEST_URI'] = $this->user_trailingslashit( '/' . str_repeat( '-/', 10 ) );
$pagenow = 'index.php';
} elseif ( ( isset( $request['path'] ) && untrailingslashit( $request['path'] ) === home_url( $this->new_login_slug(), 'relative' ) )
|| ( ! get_option( 'permalink_structure' )
&& isset( $_GET[ $this->new_login_slug() ] )
&& empty( $_GET[ $this->new_login_slug() ] ) ) ) {
$pagenow = 'wp-login.php';
} elseif ( ( strpos( rawurldecode( $_SERVER['REQUEST_URI'] ), 'wp-register.php' ) !== false
|| ( isset( $request['path'] ) && untrailingslashit( $request['path'] ) === site_url( 'wp-register', 'relative' ) ) )
&& ! is_admin() ) {
$this->wp_login_php = true;
$_SERVER['REQUEST_URI'] = $this->user_trailingslashit( '/' . str_repeat( '-/', 10 ) );
$pagenow = 'index.php';
}
}
public function setup_theme() {
global $pagenow;
if ( ! is_user_logged_in() && 'customize.php' === $pagenow ) {
wp_die( __( 'This has been disabled', 'wps-hide-login' ), 403 );
}
}
public function wp_loaded() {
global $pagenow;
$request = parse_url( rawurldecode( $_SERVER['REQUEST_URI'] ) );
do_action( 'wps_hide_login_before_redirect', $request );
if ( ! ( isset( $_GET['action'] ) && $_GET['action'] === 'postpass' && isset( $_POST['post_password'] ) ) ) {
if ( is_admin() && ! is_user_logged_in() && ! defined( 'WP_CLI' ) && ! defined( 'DOING_AJAX' ) && ! defined( 'DOING_CRON' ) && $pagenow !== 'admin-post.php' && $request['path'] !== '/wp-admin/options.php' ) {
wp_safe_redirect( $this->new_redirect_url() );
die();
}
if ( ! is_user_logged_in() && isset( $_GET['wc-ajax'] ) && $pagenow === 'profile.php' ) {
wp_safe_redirect( $this->new_redirect_url() );
die();
}
if ( ! is_user_logged_in() && isset( $request['path'] ) && $request['path'] === '/wp-admin/options.php' ) {
header('Location: ' . $this->new_redirect_url() );
die;
}
if ( $pagenow === 'wp-login.php' && isset( $request['path'] ) && $request['path'] !== $this->user_trailingslashit( $request['path'] ) && get_option( 'permalink_structure' ) ) {
wp_safe_redirect( $this->user_trailingslashit( $this->new_login_url() )
. ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
die;
} elseif ( $this->wp_login_php ) {
if ( ( $referer = wp_get_referer() )
&& strpos( $referer, 'wp-activate.php' ) !== false
&& ( $referer = parse_url( $referer ) )
&& ! empty( $referer['query'] ) ) {
parse_str( $referer['query'], $referer );
@require_once WPINC . '/ms-functions.php';
if ( ! empty( $referer['key'] )
&& ( $result = wpmu_activate_signup( $referer['key'] ) )
&& is_wp_error( $result )
&& ( $result->get_error_code() === 'already_active'
|| $result->get_error_code() === 'blog_taken' ) ) {
wp_safe_redirect( $this->new_login_url()
. ( ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . $_SERVER['QUERY_STRING'] : '' ) );
die;
}
}
$this->wp_template_loader();
} elseif ( $pagenow === 'wp-login.php' ) {
global $error, $interim_login, $action, $user_login;
$redirect_to = admin_url();
$requested_redirect_to = '';
if ( isset( $_REQUEST['redirect_to'] ) ) {
$requested_redirect_to = $_REQUEST['redirect_to'];
}
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( ! isset( $_REQUEST['action'] ) ) {
$logged_in_redirect = apply_filters( 'whl_logged_in_redirect', $redirect_to, $requested_redirect_to, $user );
wp_safe_redirect( $logged_in_redirect );
die();
}
}
@require_once ABSPATH . 'wp-login.php';
die;
}
}
}
public function site_url( $url, $path, $scheme, $blog_id ) {
return $this->filter_wp_login_php( $url, $scheme );
}
public function network_site_url( $url, $path, $scheme ) {
return $this->filter_wp_login_php( $url, $scheme );
}
public function wp_redirect( $location, $status ) {
if ( strpos( $location, 'https://wordpress.com/wp-login.php' ) !== false ) {
return $location;
}
return $this->filter_wp_login_php( $location );
}
public function filter_wp_login_php( $url, $scheme = null ) {
if ( strpos( $url, 'wp-login.php?action=postpass' ) !== false ) {
return $url;
}
if ( strpos( $url, 'wp-login.php' ) !== false && strpos( wp_get_referer(), 'wp-login.php' ) === false ) {
if ( is_ssl() ) {
$scheme = 'https';
}
$args = explode( '?', $url );
if ( isset( $args[1] ) ) {
parse_str( $args[1], $args );
if ( isset( $args['login'] ) ) {
$args['login'] = rawurlencode( $args['login'] );
}
$url = add_query_arg( $args, $this->new_login_url( $scheme ) );
} else {
$url = $this->new_login_url( $scheme );
}
}
return $url;
}
public function welcome_email( $value ) {
return $value = str_replace( 'wp-login.php', trailingslashit( get_site_option( 'whl_page', 'login' ) ), $value );
}
public function forbidden_slugs() {
$wp = new \WP;
return array_merge( $wp->public_query_vars, $wp->private_query_vars );
}
/**
* Load scripts
*/
public function admin_enqueue_scripts( $hook ) {
if ( 'options-general.php' != $hook ) {
return false;
}
wp_enqueue_style( 'plugin-install' );
wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
add_thickbox();
}
public function wps_hide_login_menu_page() {
$title = __( 'WPS Hide Login' );
add_options_page( $title, $title, 'manage_options', 'whl_settings', array(
$this,
'settings_page'
) );
}
public function settings_page() {
_e( 'WPS Hide Login' );
}
public function whl_template_redirect() {
if ( ! empty( $_GET ) && isset( $_GET['page'] ) && 'whl_settings' === $_GET['page'] ) {
wp_redirect( admin_url( 'options-general.php#whl_settings' ) );
exit();
}
}
/**
*
* Update url redirect : wp-admin/options.php
*
* @param $login_url
* @param $redirect
* @param $force_reauth
*
* @return string
*/
public function login_url( $login_url, $redirect, $force_reauth ) {
if ( is_404() ) {
return '#';
}
if ( $force_reauth === false ) {
return $login_url;
}
if ( empty( $redirect ) ) {
return $login_url;
}
$redirect = explode( '?', $redirect );
if ( $redirect[0] === admin_url( 'options.php' ) ) {
$login_url = admin_url();
}
return $login_url;
}
}