> Menus page for the login logout menu items
*
* @since 3.4.0
*/
public function add_login_logout_metabox() {
add_meta_box(
'add-login-logout',
'Log In / Log Out',
array( $this, 'add_login_logout_menu_items' ),
'nav-menus',
'side',
'default'
);
}
/**
* Add menu items for the login logout metabox
*
* @since 3.4.0
*/
public function add_login_logout_menu_items() {
// The ID of the currently selected menu
global $nav_menu_selected_id;
$menu_items = array(
'asenha-login' => array(
'title' => 'Log In',
'url' => '#asenha-login',
'classes' => array( 'asenha-login-menu-item' ),
),
'asenha-logout' => array(
'title' => 'Log Out',
'url' => '#asenha-logout',
'classes' => array( 'asenha-logout-menu-item' ),
),
'asenha-login-logout' => array(
'title' => 'Log In / Log Out',
'url' => '#asenha-login-logout',
'classes' => array( 'asenha-login-logout-menu-item' ),
),
);
$item_details = array(
'db_id' => 0,
'object' => 'asenha',
'object_id' => '',
'menu_item_parent' => 0,
'type' => 'custom',
'title' => '',
'url' => '',
'target' => '',
'attr_title' => '',
'classes' => array(),
'xfn' => '',
);
$menu_items_object = array();
foreach ( $menu_items as $item_id => $details ) {
$menu_items_object[ $details['title'] ] = (object) $item_details;
$menu_items_object[ $details['title'] ]->object_id = $item_id;
$menu_items_object[ $details['title'] ]->title = $details['title'];
$menu_items_object[ $details['title'] ]->classes = $details['classes'];
$menu_items_object[ $details['title'] ]->url = $details['url'];
}
$walker = new Walker_Nav_Menu_Checklist( array() );
?>
url ) && false !== strpos( $menu_item->url, 'asenha' ) ) {
// Define login URL based on whether
if ( array_key_exists( 'change_login_url', $options ) && $options['change_login_url'] ) {
if ( array_key_exists( 'custom_login_slug', $options ) && ! empty( $options['custom_login_slug'] ) ) {
$login_page_url = get_site_url() . '/' . $options['custom_login_slug'];
}
} else {
$login_page_url = wp_login_url();
}
$logout_redirect_url = home_url();
switch( $menu_item->url ) {
case '#asenha-login';
$menu_item->url = $login_page_url;
break;
case '#asenha-logout';
$menu_item->url = wp_logout_url();
break;
case '#asenha-login-logout';
$menu_item->url = ( is_user_logged_in() ) ? wp_logout_url() : $login_page_url;
$menu_item->title = ( is_user_logged_in() ) ? 'Log Out' : 'Log In';
break;
}
}
return $menu_item;
}
/**
* Conditionally remove login or logout menu item based on is_user_logged_in()
*
* @since 3.4.0
*/
public function maybe_remove_login_or_logout_menu_item( $sorted_menu_items ) {
foreach( $sorted_menu_items as $menu => $item ) {
$item_classes = $item->classes;
// Maybe remove Log In menu item
if ( in_array( 'asenha-login-menu-item', $item_classes ) ) {
if ( is_user_logged_in() ) {
unset( $sorted_menu_items[$menu] );
}
}
// Maybe remove Log Out menu item
if ( in_array( 'asenha-logout-menu-item', $item_classes ) ) {
if ( ! is_user_logged_in() ) {
unset( $sorted_menu_items[$menu] );
}
}
}
return $sorted_menu_items;
}
/**
* Redirect to custom internal URL after login for user roles
*
* @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
* @param string $origin_url URL the user is coming from.
* @param object $user logged-in user's data.
* @since 1.5.0
*/
public function redirect_for_roles_after_login( $username, $user ) {
$options = get_option( ASENHA_SLUG_U, array() );
$redirect_after_login_to_slug = $options['redirect_after_login_to_slug'];
$redirect_after_login_for = $options['redirect_after_login_for'];
if ( isset( $redirect_after_login_for ) && ( count( $redirect_after_login_for ) > 0 ) ) {
// Assemble single-dimensional array of roles for which custom URL redirection should happen
$roles_for_custom_redirect = array();
foreach( $redirect_after_login_for as $role_slug => $custom_redirect ) {
if ( $custom_redirect ) {
$roles_for_custom_redirect[] = $role_slug;
}
}
// Does the user have roles data in array form?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
$current_user_roles = $user->roles;
}
// Set custom redirect URL for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
foreach ( $current_user_roles as $role ) {
if ( in_array( $role, $roles_for_custom_redirect ) ) {
wp_safe_redirect( home_url( $redirect_after_login_to_slug . '/' ) );
exit();
}
}
}
}
/**
* Redirect to custom internal URL after login for user roles
*
* @param string $redirect_to_url URL to redirect to. Default is admin dashboard URL.
* @param string $origin_url URL the user is coming from.
* @param object $user logged-in user's data.
* @since 1.6.0
*/
public function redirect_after_logout( $user_id ) {
$options = get_option( ASENHA_SLUG_U, array() );
$redirect_after_logout_to_slug = $options['redirect_after_logout_to_slug'];
$redirect_after_logout_for = $options['redirect_after_logout_for'];
$user = get_userdata( $user_id );
if ( isset( $redirect_after_logout_for ) && ( count( $redirect_after_logout_for ) > 0 ) ) {
// Assemble single-dimensional array of roles for which custom URL redirection should happen
$roles_for_custom_redirect = array();
foreach( $redirect_after_logout_for as $role_slug => $custom_redirect ) {
if ( $custom_redirect ) {
$roles_for_custom_redirect[] = $role_slug;
}
}
// Does the user have roles data in array form?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
$current_user_roles = $user->roles;
}
// Redirect for roles set in the settings. Otherwise, leave redirect URL to the default, i.e. admin dashboard.
foreach ( $current_user_roles as $role ) {
if ( in_array( $role, $roles_for_custom_redirect ) ) {
wp_safe_redirect( home_url( $redirect_after_logout_to_slug . '/' ) );
exit();
}
}
}
}
/**
* Log date time when a user last logged in successfully
*
* @since 3.6.0
*/
public function log_login_datetime( $user_login ) {
$user = get_user_by( 'login', $user_login ); // by username
update_user_meta( $user->ID, 'asenha_last_login_on', time() );
}
/**
* Add Last Login column to users list table
*
* @since 3.6.0
*/
public function add_last_login_column( $columns ) {
$columns['asenha_last_login'] = 'Last Login';
return $columns;
}
/**
* Show last login info in the last login column
*
* @since 3.6.0
*/
public function show_last_login_info( $output, $column_name, $user_id ) {
if ( 'asenha_last_login' === $column_name ) {
if ( ! empty( get_user_meta( $user_id, 'asenha_last_login_on', true ) ) ) {
$last_login_unixtime = (int) get_user_meta( $user_id, 'asenha_last_login_on', true );
if ( function_exists( 'wp_date' ) ) {
$output = wp_date( 'M j, Y H:i', $last_login_unixtime );
} else {
$output = date_i18n( 'M j, Y H:i', $last_login_unixtime );
}
} else {
$output = 'No data yet';
}
}
return $output;
}
/**
* Add custom CSS for the Last Login column
*
* @since 3.6.0
*/
public function add_column_style() {
?>