options = $options; $this->init(); } /** * Start the engine running * * @return void */ private function init() { add_filter( 'adminoptimizer_settings_navtab', [ $this, 'add_settings_navtab' ] ); add_filter( 'adminoptimizer_settings_sections', [ $this, 'settings_fields' ] ); if ( ! empty( $this->options['display_user_registration_date'] ) || ! empty( $this->options['enable_user_login_tracking'] ) ) { add_filter( 'manage_users_columns', [ $this, 'add_user_custom_columns' ] ); add_action( 'manage_users_custom_column', [ $this, 'render_user_custom_columns' ], 10, 3 ); add_filter( 'request', [ $this, 'user_custom_column_orderby' ] ); } if ( ! empty( $this->options['display_user_registration_date'] ) ) { add_filter( 'manage_users_sortable_columns', [ $this, 'add_user_custom_column_sortable' ] ); } if ( ! empty( $this->options['enable_user_login_tracking'] ) ) { add_action( 'wp_login', [ $this, 'user_last_login' ], 10, 2 ); } if ( ! empty( $this->options['disable_user_signup_notification'] ) ) { add_action( 'init', function () { remove_action( 'register_new_user', 'wp_send_new_user_notifications' ); remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' ); } ); } if ( ! empty( $this->options['hide_update_notices'] ) ) { add_action( 'admin_head', [ $this, 'hide_update_notice_to_all_but_admin_users' ], 1 ); } if ( ! empty( $this->options['disable_user_account'] ) ) { $this->modules['disable_user_login'] = new Disable_User_Account(); } $this->migrate_legacy_hide_admin_toolbar_toggle(); $this->migrate_legacy_block_admin_access_toggle(); if ( ! empty( $this->options['enable_user_access_manager'] ) ) { $this->modules['user_access_manager'] = new User_Access_Manager(); } } /** * Move the legacy Hide Admin Toolbar toggle into per-role User Access Manager settings. * * @return void */ private function migrate_legacy_hide_admin_toolbar_toggle() { if ( empty( $this->options['hide_admin_toolbar'] ) ) { return; } $uam_options = get_option( User_Access_Manager::OPTION_NAME, [] ); $uam_options = is_array( $uam_options ) ? $uam_options : []; if ( empty( $uam_options['role_settings'] ) || ! is_array( $uam_options['role_settings'] ) ) { $uam_options['role_settings'] = []; } $wp_roles = wp_roles(); $roles = isset( $wp_roles->roles ) && is_array( $wp_roles->roles ) ? array_keys( $wp_roles->roles ) : []; foreach ( $roles as $role ) { $role = sanitize_key( (string) $role ); if ( empty( $role ) ) { continue; } if ( empty( $uam_options['role_settings'][ $role ] ) || ! is_array( $uam_options['role_settings'][ $role ] ) ) { $uam_options['role_settings'][ $role ] = []; } $uam_options['role_settings'][ $role ]['show_admin_bar'] = 0; } update_option( User_Access_Manager::OPTION_NAME, $uam_options ); $this->options['enable_user_access_manager'] = 1; unset( $this->options['hide_admin_toolbar'] ); update_option( MODULES_OPTION, $this->options ); } /** * Move the legacy Block Admin Access module toggle into User Access Manager. * * @return void */ private function migrate_legacy_block_admin_access_toggle() { if ( empty( $this->options['block_admin_access'] ) ) { return; } $this->options['enable_user_access_manager'] = 1; unset( $this->options['block_admin_access'] ); update_option( MODULES_OPTION, $this->options ); } /** * Add settings navtab * * @param array $nav_tab List of nav tabs. * * @return array */ public function add_settings_navtab( $nav_tab ) { if ( empty( $nav_tab['users'] ) ) { $nav_tab['users'] = [ 'title' => __( 'Users', 'admin-optimizer' ), 'slug' => 'adminoptim-users-settings', ]; } return $nav_tab; } /** * List of Settings fields * * @param array $fields Settings fields. * * @return array */ public function settings_fields( $fields ) { if ( empty( $fields['users-module'] ) ) { $fields['users-module'] = [ 'id' => 'adminoptimizer-users-module-settings', 'title' => __( 'Modules', 'admin-optimizer' ), 'description' => '', 'menu_slug' => 'adminoptim-users-settings', 'option_name' => MODULES_OPTION, 'args' => [ 'before_section' => '
', 'after_section' => '
', 'section_class' => 'module', ], 'fields' => [ [ 'type' => 'slider-checkbox', 'title' => __( 'User Access Manager', 'admin-optimizer' ), 'id' => 'enable-user-access-manager', 'name' => 'enable_user_access_manager', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'View and manage WordPress roles, custom roles, custom capabilities and configure which user roles can access the admin area%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], [ 'type' => 'slider-checkbox', 'title' => __( 'User Switching (Pro only)', 'admin-optimizer' ), 'id' => 'enable-user-switching', 'name' => 'enable_user_switching', 'disabled' => 'disabled', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Temporarily switch into non-privileged users to verify role capabilities and access restrictions.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], [ 'type' => 'slider-checkbox', 'title' => __( 'Disable User Account', 'admin-optimizer' ), 'id' => 'disable-user-account', 'name' => 'disable_user_account', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Disable user accounts and prevent them from logging in.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], ], ]; } if ( empty( $fields['users-tweaks'] ) ) { $fields['users-tweaks'] = [ 'id' => 'adminoptimizer-users-tweaks-settings', 'title' => __( 'Tweaks', 'admin-optimizer' ), 'description' => __( 'Activate with a single click. No additional configuration required.', 'admin-optimizer' ), 'menu_slug' => 'adminoptim-users-settings', 'option_name' => MODULES_OPTION, 'args' => [ 'before_section' => '
', 'after_section' => '
', 'section_class' => 'tweaks', ], 'fields' => [ [ 'type' => 'slider-checkbox', 'title' => __( 'Hide Update notice to all users, except for Administrators', 'admin-optimizer' ), 'id' => 'hide-update-notices', 'name' => 'hide_update_notices', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Only show update notices to Administrators and those with update capabilities.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], [ 'type' => 'slider-checkbox', 'title' => __( 'Track User\'s last login date', 'admin-optimizer' ), 'id' => 'enable-user-login-tracking', 'name' => 'enable_user_login_tracking', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Track and display the user last login date.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], [ 'type' => 'slider-checkbox', 'title' => __( 'Display User registration date', 'admin-optimizer' ), 'id' => 'display-user-registration-date', 'name' => 'display_user_registration_date', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Track and show the user registration date.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], [ 'type' => 'slider-checkbox', 'title' => __( 'Disable new user signup notification', 'admin-optimizer' ), 'id' => 'disable-user-signup-notification', 'name' => 'disable_user_signup_notification', // translators: %1$s is the line break tag. %2$s is the anchor tag. %3$s is the anchor closure tag. 'label' => sprintf( __( 'Disable the option to send the new user an email when their account was created.%1$s%2$sLearn more%3$s', 'admin-optimizer' ), '
', '', '' ), ], ], ]; } return $fields; } /** * Check user last login * * @param string $user_login User's username. * @param \WP_User $user WP_User class. * * @return void */ public function user_last_login( $user_login, $user ) { update_user_meta( $user->ID, 'last_login', time() ); } /** * Render Custom columns in User page * * @param string $value Custom column output. * @param string $column_name Column name. * @param int $user_id ID of the currently-listed user. * * @return false|string */ public function render_user_custom_columns( $value, $column_name, $user_id ) { if ( 'registerdate' === $column_name ) { $user = get_userdata( $user_id ); $registered = strtotime( get_date_from_gmt( $user->user_registered ) ); return '' . esc_html( wp_date( get_option( 'date_format' ), $registered ) ) . '
' . esc_html( wp_date( get_option( 'time_format' ), $registered ) ) . '
'; } elseif ( 'lastlogin' === $column_name ) { $last_login = (int) get_user_meta( $user_id, 'last_login', true ); $the_login_date = wp_date( 'd M Y, H:i:s', $last_login ); if ( empty( $the_login_date ) || empty( $last_login ) ) { $the_login_date = 'Never'; } return $the_login_date; } else { return $value; } } /** * Add custom colums to Users page. * * @param array $columns Columns. * * @return array */ public function add_user_custom_column_sortable( $columns ): array { $custom = []; if ( ! empty( $this->options['display_user_registration_date'] ) ) { $custom['registerdate'] = 'registered'; } if ( ! empty( $custom ) ) { return wp_parse_args( $custom, $columns ); } else { return $columns; } } /** * Define custom column orderby * * @param array $vars Vars. * * @return mixed */ public function user_custom_column_orderby( $vars ) { if ( isset( $vars['orderby'] ) && 'registerdate' === $vars['orderby'] ) { $vars = array_merge( $vars, [ 'meta_key' => 'registerdate', //phpcs:ignore 'orderby' => 'meta_value', ] ); } return $vars; } /** * Add custom columns to Users page. * * @param array $columns All columns. * * @return array */ public function add_user_custom_columns( $columns ) { if ( ! empty( $this->options['display_user_registration_date'] ) ) { $columns['registerdate'] = __( 'Registered', 'admin-optimizer' ); } if ( ! empty( $this->options['enable_user_login_tracking'] ) ) { $columns['lastlogin'] = __( 'Last Login', 'admin-optimizer' ); } return $columns; } /** * Hide update notice * * @return void */ public function hide_update_notice_to_all_but_admin_users() { if ( ! current_user_can( 'update_core' ) ) { remove_action( 'admin_notices', 'update_nag', 3 ); } } }