<?php
/**
 * MainWP Users Page
 *
 * This page is used to Manage Users on child sites.
 *
 * @package MainWP/User
 */

namespace MainWP\Dashboard;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class MainWP_User
 *
 * @uses page-mainwp-bulk-add::MainWP_Bulk_Add()
 */
class MainWP_User { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.

    /**
     * Get Class Name
     *
     * @return string __CLASS__
     */
    public static function get_class_name() {
        return __CLASS__;
    }

    /**
     * Public static varable to hold Subpages information.
     *
     * @var array $subPages
     */
    public static $subPages;

    /**
     * Method init()
     *
     * Initiate hooks for the users page.
     */
    public static function init() {
        /**
         * This hook allows you to render the User page header via the 'mainwp-pageheader-user' action.
         *
         * @link http://codex.mainwp.com/#mainwp-pageheader-user
         *
         * This hook is normally used in the same context of 'mainwp-getsubpages-user'
         * @link http://codex.mainwp.com/#mainwp-getsubpages-user
         *
         * @see \MainWP_User::render_header
         */
        add_action( 'mainwp-pageheader-user', array( static::get_class_name(), 'render_header' ) );

        /**
         * This hook allows you to render the User page footer via the 'mainwp-pagefooter-user' action.
         *
         * @link http://codex.mainwp.com/#mainwp-pagefooter-user
         *
         * This hook is normally used in the same context of 'mainwp-getsubpages-user'
         * @link http://codex.mainwp.com/#mainwp-getsubpages-user
         *
         * @see \MainWP_User::render_footer
         */
        add_action( 'mainwp-pagefooter-user', array( static::get_class_name(), 'render_footer' ) );

        add_action( 'mainwp_help_sidebar_content', array( static::get_class_name(), 'mainwp_help_content' ) );
    }

    /**
     * Method init_menu()
     *
     * Initiate menu.
     *
     * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
     */
    public static function init_menu() {
        $_page = add_submenu_page(
            'mainwp_tab',
            __( 'Users', 'mainwp' ),
            '<span id="mainwp-Users">' . esc_html__( 'Users', 'mainwp' ) . '</span>',
            'read',
            'UserBulkManage',
            array(
                static::get_class_name(),
                'render',
            )
        );

        add_action( 'load-' . $_page, array( static::get_class_name(), 'on_load_page' ) );

        add_submenu_page(
            'mainwp_tab',
            __( 'Users', 'mainwp' ),
            '<div class="mainwp-hidden">' . esc_html__( 'Add New', 'mainwp' ) . '</div>',
            'read',
            'UserBulkAdd',
            array(
                static::get_class_name(),
                'render_bulk_add',
            )
        );

        add_submenu_page(
            'mainwp_tab',
            __( 'Import Users', 'mainwp' ),
            '<div class="mainwp-hidden">' . esc_html__( 'Import Users', 'mainwp' ) . '</div>',
            'read',
            'BulkImportUsers',
            array(
                static::get_class_name(),
                'render_bulk_import_users',
            )
        );

        /**
         * This hook allows you to add extra sub pages to the User page via the 'mainwp-getsubpages-user' filter.
         *
         * @link http://codex.mainwp.com/#mainwp-getsubpages-user
         */
        $sub_pages        = apply_filters_deprecated( 'mainwp-getsubpages-user', array( array() ), '4.0.7.2', 'mainwp_getsubpages_user' );  // @deprecated Use 'mainwp_getsubpages_user' instead. NOSONAR - not IP.
        static::$subPages = apply_filters( 'mainwp_getsubpages_user', $sub_pages );

        if ( isset( static::$subPages ) && is_array( static::$subPages ) ) {
            foreach ( static::$subPages as $subPage ) {
                if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
                    continue;
                }
                add_submenu_page( 'mainwp_tab', $subPage['title'], '<div class="mainwp-hidden">' . esc_html( $subPage['title'] ) . '</div>', 'read', 'UserBulk' . $subPage['slug'], $subPage['callback'] );
            }
        }

        static::init_left_menu( static::$subPages );
    }

    /**
     * Initiates sub pages menu.
     *
     * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
     */
    public static function init_subpages_menu() {
        ?>
        <div id="menu-mainwp-Users" class="mainwp-submenu-wrapper">
            <div class="wp-submenu sub-open" style="">
                <div class="mainwp_boxout">
                    <div class="mainwp_boxoutin"></div>
                    <?php if ( \mainwp_current_user_can( 'dashboard', 'manage_users' ) ) { ?>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulkManage' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Manage Users', 'mainwp' ); ?></a>
                    <?php } ?>
                    <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UserBulkAdd' ) ) { ?>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulkAdd' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Add New', 'mainwp' ); ?></a>
                    <?php } ?>
                    <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'BulkImportUsers' ) ) { ?>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=BulkImportUsers' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Import Users', 'mainwp' ); ?></a>
                    <?php } ?>
                    <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UpdateAdminPasswords' ) ) { ?>
                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=UpdateAdminPasswords' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Admin Passwords', 'mainwp' ); ?></a>
                    <?php } ?>
                    <?php if ( ! MainWP_Menu::is_disable_menu_item( 3, 'PasswordPolicy' ) ) { ?>
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=PasswordPolicy' ) ); ?>" class="mainwp-submenu"><?php esc_html_e( 'Password Policy', 'mainwp' ); ?></a>
                    <?php } ?>
                    <?php
                    if ( isset( static::$subPages ) && is_array( static::$subPages ) ) {
                        foreach ( static::$subPages as $subPage ) {
                            if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
                                continue;
                            }
                            ?>
                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=UserBulk' . $subPage['slug'] ) ); ?>" class="mainwp-submenu"><?php echo esc_html( $subPage['title'] ); ?></a>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Method get_manage_columns()
     *
     * Get columns to display.
     *
     * @return array $colums Array of columns to display on the page.
     *
     * @uses \MainWP\Dashboard\MainWP_Utility::enabled_wp_seo()
     */
    public static function get_manage_columns() {
        return array(
            'name'                 => esc_html__( 'Name', 'mainwp' ),
            'username'             => esc_html__( 'Username', 'mainwp' ),
            'email'                => esc_html__( 'E-mail', 'mainwp' ),
            'last_password_change' => esc_html__( 'Last Password Change', 'mainwp' ),
            'password_status'      => esc_html__( 'Password Status', 'mainwp' ),
            'role'                 => esc_html__( 'Role', 'mainwp' ),
            'posts'                => esc_html__( 'Posts', 'mainwp' ),
            'website'              => esc_html__( 'Website', 'mainwp' ),
        );
    }

    /**
     * Method on_load_page()
     *
     * Used during init_menu() to get the class names of,
     * admin_head and get_hidden_columns.
     *
     * @return void
     */
    public static function on_load_page() {
        add_action( 'mainwp_screen_options_modal_bottom', array( static::get_class_name(), 'hook_screen_options_modal_bottom' ), 10, 2 );
    }

    /**
     * Method hook_screen_options_modal_bottom()
     *
     * Render screen options modal bottom.
     */
    public static function hook_screen_options_modal_bottom() {
        $page = isset( $_GET['page'] ) ? wp_unslash( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        if ( 'UserBulkManage' === $page ) {

            $show_columns = get_user_option( 'mainwp_manageusers_show_columns' );

            if ( ! is_array( $show_columns ) ) {
                $show_columns = array();
            }

            $cols = static::get_manage_columns();
            MainWP_UI::render_showhide_columns_settings( $cols, $show_columns, 'user' );
        }
    }

    /**
     * Initiates Users menu.
     *
     * @param array $subPages Sub pages array.
     *
     * @uses \MainWP\Dashboard\MainWP_Menu::add_left_menu()
     * @uses \MainWP\Dashboard\MainWP_Menu::init_subpages_left_menu()
     * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
     */
    public static function init_left_menu( $subPages = array() ) {
        MainWP_Menu::add_left_menu(
            array(
                'title'         => esc_html__( 'Users', 'mainwp' ),
                'parent_key'    => 'managesites',
                'slug'          => 'UserBulkManage',
                'href'          => 'admin.php?page=UserBulkManage',
                'icon'          => '<i class="user icon"></i>',
                'desc'          => 'Manage users on your child sites',
                'leftsub_order' => 7,
            ),
            1
        );

        $init_sub_subleftmenu = array(
            array(
                'title'      => esc_html__( 'Manage Users', 'mainwp' ),
                'parent_key' => 'UserBulkManage',
                'href'       => 'admin.php?page=UserBulkManage',
                'slug'       => 'UserBulkManage',
                'right'      => 'manage_users',
            ),
            array(
                'title'      => esc_html__( 'Add New', 'mainwp' ),
                'parent_key' => 'UserBulkManage',
                'href'       => 'admin.php?page=UserBulkAdd',
                'slug'       => 'UserBulkAdd',
                'right'      => '',
            ),
            array(
                'title'      => esc_html__( 'Import Users', 'mainwp' ),
                'parent_key' => 'UserBulkManage',
                'href'       => 'admin.php?page=BulkImportUsers',
                'slug'       => 'BulkImportUsers',
                'right'      => '',
            ),
            array(
                'title'      => esc_html__( 'Admin Passwords', 'mainwp' ),
                'parent_key' => 'UserBulkManage',
                'href'       => 'admin.php?page=UpdateAdminPasswords',
                'slug'       => 'UpdateAdminPasswords',
                'right'      => '',
            ),
            array(
                'title'      => esc_html__( 'Password Policy', 'mainwp' ),
                'parent_key' => 'UserBulkManage',
                'href'       => 'admin.php?page=PasswordPolicy',
                'slug'       => 'PasswordPolicy',
                'right'      => '',
            ),
        );

        MainWP_Menu::init_subpages_left_menu( $subPages, $init_sub_subleftmenu, 'UserBulkManage', 'UserBulk' );

        foreach ( $init_sub_subleftmenu as $item ) {
            if ( MainWP_Menu::is_disable_menu_item( 3, $item['slug'] ) ) {
                continue;
            }
            MainWP_Menu::add_left_menu( $item, 2 );
        }
    }

    /**
     * Method render_header()
     *
     * Render Users page header.
     *
     * @param string $shownPage The page slug shown at this moment.
     *
     * @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
     * @uses \MainWP\Dashboard\MainWP_UI::render_top_header()
     * @uses \MainWP\Dashboard\MainWP_UI::render_page_navigation()
     */
    public static function render_header( $shownPage = '' ) {  //phpcs:ignore -- NOSONAR - complex.
        $params = array(
            'title' => esc_html__( 'Users', 'mainwp' ),
        );
        MainWP_UI::render_top_header( $params );

        $renderItems = array();

        if ( \mainwp_current_user_can( 'dashboard', 'manage_users' ) ) {
            $renderItems[] = array(
                'title'  => esc_html__( 'Manage Users', 'mainwp' ),
                'href'   => 'admin.php?page=UserBulkManage',
                'active' => ( '' === $shownPage ) ? true : false,
            );
        }

        if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UserBulkAdd' ) ) {
            $renderItems[] = array(
                'title'  => esc_html__( 'Add New', 'mainwp' ),
                'href'   => 'admin.php?page=UserBulkAdd',
                'active' => ( 'Add' === $shownPage ) ? true : false,
            );
        }

        if ( ! MainWP_Menu::is_disable_menu_item( 3, 'BulkImportUsers' ) ) {
            $renderItems[] = array(
                'title'  => esc_html__( 'Import Users', 'mainwp' ),
                'href'   => 'admin.php?page=BulkImportUsers',
                'active' => ( 'Import' === $shownPage ) ? true : false,
            );
        }

        if ( ! MainWP_Menu::is_disable_menu_item( 3, 'UpdateAdminPasswords' ) ) {
            $renderItems[] = array(
                'title'  => esc_html__( 'Admin Passwords', 'mainwp' ),
                'href'   => 'admin.php?page=UpdateAdminPasswords',
                'active' => ( 'UpdateAdminPasswords' === $shownPage ) ? true : false,
            );
        }

        if ( ! MainWP_Menu::is_disable_menu_item( 3, 'PasswordPolicy' ) ) {
            $renderItems[] = array(
                'title'  => esc_html__( 'Password Policy', 'mainwp' ),
                'href'   => 'admin.php?page=PasswordPolicy',
                'active' => ( 'PasswordPolicy' === $shownPage ) ? true : false,
            );
        }

        if ( isset( static::$subPages ) && is_array( static::$subPages ) ) {
            foreach ( static::$subPages as $subPage ) {
                if ( MainWP_Menu::is_disable_menu_item( 3, 'UserBulk' . $subPage['slug'] ) ) {
                    continue;
                }

                $item           = array();
                $item['title']  = $subPage['title'];
                $item['href']   = 'admin.php?page=UserBulk' . $subPage['slug'];
                $item['active'] = ( $subPage['slug'] === $shownPage ) ? true : false;
                $renderItems[]  = $item;
            }
        }

        MainWP_UI::render_page_navigation( $renderItems );
    }

    /**
     * Method render_footer()
     *
     * Render Users page footer. Closes the page container.
     */
    public static function render_footer() {
        echo '</div>';
    }

    /**
     * Renders manage users dashboard.
     *
     * @return void
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::get_cached_context()
     */
    public static function render() {
        if ( ! \mainwp_current_user_can( 'dashboard', 'manage_users' ) ) {
            \mainwp_do_not_have_permissions( esc_html__( 'manage users', 'mainwp' ) );

            return;
        }

        $cachedSearch = MainWP_Cache::get_cached_context( 'Users' );

        $selected_sites   = array();
        $selected_groups  = array();
        $selected_clients = array();

        if ( null !== $cachedSearch ) {
            if ( is_array( $cachedSearch['sites'] ) ) {
                $selected_sites = $cachedSearch['sites'];
            } elseif ( is_array( $cachedSearch['groups'] ) ) {
                $selected_groups = $cachedSearch['groups'];
            } elseif ( is_array( $cachedSearch['clients'] ) ) {
                $selected_clients = $cachedSearch['clients'];
            }
        }

        static::render_header( '' );
        ?>
        <div id="mainwp-manage-users">
            <div class="mainwp-main-content">
                <div class="mainwp-actions-bar ui mini form">
                    <div class="ui grid">
                        <div class="ui two column row">
                            <div class="column">
                                <select class="ui mini dropdown disabled" id="mainwp-bulk-actions">
                                    <option value=""><?php esc_html_e( 'Bulk Actions', 'mainwp' ); ?></option>
                                    <option value="edit"><?php esc_html_e( 'Edit', 'mainwp' ); ?></option>
                                    <option value="delete"><?php esc_html_e( 'Delete', 'mainwp' ); ?></option>
                                    <?php
                                    /**
                                     * Action: mainwp_users_bulk_action
                                     *
                                     * Adds new Bulk Actions option under on Manage Users.
                                     *
                                     * Suggested HTML Markup:
                                     * <option value="Your custom value">Your custom label</option>
                                     *
                                     * @since 4.1
                                     */
                                    do_action( 'mainwp_users_bulk_action' );
                                    ?>
                                </select>
                                <button class="ui mini button disabled" id="mainwp-do-users-bulk-actions"><?php esc_html_e( 'Apply', 'mainwp' ); ?></button>
                                <?php
                                /**
                                 * Users actions bar (left)
                                 *
                                 * Fires at the left side of the actions bar on the Users screen, after the Bulk Actions menu.
                                 *
                                 * @since 4.0
                                 */
                                do_action( 'mainwp_users_actions_bar_left' );
                                ?>
                            </div>
                            <div class="right aligned column">
                                <?php
                                /**
                                 * Users actions bar (right)
                                 *
                                 * Fires at the right side of the actions bar on the Users screen.
                                 *
                                 * @since 4.0
                                 */
                                do_action( 'mainwp_users_actions_bar_right' );
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="mainwp-loading-users-row" style="display: none;">
                    <div class="ui active page dimmer">
                        <div class="ui double text loader">
                            <?php esc_html_e( 'Loading...', 'mainwp' ); ?>
                            <span id="mainwp_users_loading_info" class="mainwp-grabbing-info-note"><br /><?php esc_html_e( 'Automatically refreshing to get up to date information.', 'mainwp' ); ?></span>
                        </div>
                    </div>
                </div>
                <div class="ui padded segment" id="mainwp_users_wrap_table">
                    <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-manage-users-info-message' ) ) : ?>
                        <div class="ui info message">
                            <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-manage-users-info-message"></i>
                            <?php /* translators: 1: opening anchor tag, 2: closing anchor tag */ printf( esc_html__( 'Manage existing users on your child sites.  Here you can Delete, Edit or Change Role for existing users.  For additional help, please check this %1$shelp documentation%2$s.', 'mainwp' ), '<a href="https://docs.mainwp.com/sites/users/manage-users/" target="_blank">', '</a>' ); ?>
                        </div>
                    <?php endif; ?>
                    <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-password-policy-feature-info-message' ) ) : ?>
                        <div class="ui info message">
                            <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-password-policy-feature-info-message"></i>
                            <div><?php esc_html_e( 'MainWP shows the Last password change and Status for each user. WordPress doesn\'t store a reliable password-change date by default, so tracking starts after this version of MainWP Child is installed/updated. Until a user changes their password, the date may appear as Unknown.', 'mainwp' ); ?></div><br/>
                            <div><?php esc_html_e( 'If your password policy is set to Never, MainWP will still record password changes, but no due/overdue notices are shown and status may remain OK/Disabled. When a policy is enabled, users with an Unknown date can still become Due/Overdue based on no password change recorded since the policy was enabled.', 'mainwp' ); ?></div>
                        </div>
                    <?php endif; ?>
                    <div class="ui message" id="mainwp-message-zone" style="display:none"></div>
                    <?php static::render_table( true ); ?>
                </div>
                <div id="mainwp-update-users-box" class="ui segment">
                    <?php static::render_update_users(); ?>
                </div>
            </div>
            <div class="mainwp-side-content mainwp-no-padding">
                <?php
                /**
                 * Action: mainwp_manage_users_sidebar_top
                 *
                 * Fires on top of the sidebar on Manage Users page.
                 *
                 * @since 4.1
                 */
                do_action( 'mainwp_manage_users_sidebar_top' );
                ?>
                <div class="mainwp-select-sites ui accordion mainwp-sidebar-accordion">
                    <?php
                    /**
                     * Action: mainwp_manage_users_before_select_sites
                     *
                     * Fires before the Select Sites section on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_before_select_sites' );
                    ?>
                    <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div>
                    <div class="content active">
                        <?php
                        $sel_params = array(
                            'selected_sites'   => $selected_sites,
                            'selected_groups'  => $selected_groups,
                            'selected_clients' => $selected_clients,
                            'class'            => 'mainwp_select_sites_box_left',
                            'show_client'      => true,
                        );
                        MainWP_UI_Select_Sites::select_sites_box( $sel_params );
                        ?>
                        </div>
                    <?php
                    /**
                     * Action: mainwp_manage_users_after_select_sites
                     *
                     * Fires after the Select Sites section on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_after_select_sites' );
                    ?>
                </div>

                <?php
                $user_roles = array(
                    'subscriber'    => esc_html__( 'Subscriber', 'mainwp' ),
                    'administrator' => esc_html__( 'Administrator', 'mainwp' ),
                    'editor'        => esc_html__( 'Editor', 'mainwp' ),
                    'author'        => esc_html__( 'Author', 'mainwp' ),
                    'contributor'   => esc_html__( 'Contributor', 'mainwp' ),
                );

                $user_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $user_roles ), '4.0.7.2', 'mainwp_users_manage_roles' );  // @deprecated Use 'mainwp_users_manage_roles' instead. NOSONAR - not IP.
                $user_roles = apply_filters( 'mainwp_users_manage_roles', $user_roles );

                ?>

                <div class="ui fitted divider"></div>
                <div class="mainwp-search-options ui accordion mainwp-sidebar-accordion">
                    <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Roles', 'mainwp' ); ?></div>
                    <div class="content active">
                    <?php
                    /**
                     * Action: mainwp_manage_users_before_search_options
                     *
                     * Fires before the Search Options section on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_before_search_options' );
                    ?>
                    <div class="ui mini form">
                        <div class="field">
                            <select multiple="" class="ui fluid mini dropdown" id="mainwp_user_roles">
                                <option value=""><?php esc_html_e( 'Select wanted role(s)', 'mainwp' ); ?></option>
                                <?php
                                foreach ( $user_roles as $r => $n ) {
                                    if ( empty( $r ) ) {
                                        continue;
                                    }
                                    ?>
                                    <option value="<?php echo esc_html( $r ); ?>"><?php echo esc_html( $n ); ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <?php
                    /**
                     * Action: mainwp_manage_users_after_search_options
                     *
                     * Fires after the Search Options section on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_after_search_options' );
                    ?>
                </div>
                </div>
                <div class="ui fitted divider"></div>
                <div class="mainwp-search-options ui accordion mainwp-sidebar-accordion">
                    <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Search Options', 'mainwp' ); ?></div>
                    <div class="content active"><?php static::render_search_options(); ?></div>
                </div>
                <div class="ui fitted divider"></div>
                <div class="mainwp-search-submit">
                    <?php
                    /**
                     * Action: mainwp_manage_users_before_submit_button
                     *
                     * Fires before the Submit Button on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_before_submit_button' );
                    ?>
                    <input type="button" name="mainwp_show_users" id="mainwp_show_users" class="ui green big fluid button" value="<?php esc_attr_e( 'Show Users', 'mainwp' ); ?>"/>
                    <?php
                    /**
                     * Action: mainwp_manage_users_after_submit_button
                     *
                     * Fires after the Submit Button on Manage Users page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_manage_users_after_submit_button' );
                    ?>
                </div>
                <?php
                /**
                 * Action: mainwp_manage_users_sidebar_bottom
                 *
                 * Fires at the bottom of the sidebar on Manage Users page.
                 *
                 * @since 4.1
                 */
                do_action( 'mainwp_manage_users_sidebar_bottom' );
                ?>
            </div>
            <div class="ui hidden clearing divider"></div>
        </div>
        <?php
        static::render_footer( '' );
    }

    /**
     * Method render_search_options()
     *
     * Render User page search.
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::get_cached_context()
     */
    public static function render_search_options() {
        $cachedSearch = MainWP_Cache::get_cached_context( 'Users' );
        $statuses     = isset( $cachedSearch['status'] ) ? $cachedSearch['status'] : array();
        if ( $cachedSearch && isset( $cachedSearch['keyword'] ) ) {
            $cachedSearch['keyword'] = trim( $cachedSearch['keyword'] );
        }
        ?>

        <div class="ui mini form">
            <div class="field">
                <div class="ui input fluid">
                    <input type="text" placeholder="<?php esc_attr_e( 'Username', 'mainwp' ); ?>" id="mainwp_search_users" class="text" value="<?php echo ( null !== $cachedSearch ) ? esc_attr( $cachedSearch['keyword'] ) : ''; ?>" />
                </div>
            </div>
        </div>
        <?php
        if ( is_array( $statuses ) && ! empty( $statuses ) ) {
            $status = '';
            foreach ( $statuses as $st ) {
                $status .= "'" . esc_html( $st ) . "',";
            }
            $status = rtrim( $status, ',' );
            ?>
            <script type="text/javascript">
            jQuery( document ).ready( function () {
                jQuery( '#mainwp_user_roles' ).dropdown( 'set selected', [<?php echo $status; //phpcs:ignore -- safe output. ?>] );
            } );
            </script>
            <?php
        }
    }

    /**
     * Renders Edit Users Modal window.
     */
    public static function render_update_users() {

        $editable_roles = array(
            'donotupdate'   => esc_html__( 'Do not update', 'mainwp' ),
            'administrator' => esc_html__( 'Administrator', 'mainwp' ),
            'subscriber'    => esc_html__( 'Subscriber', 'mainwp' ),
            'contributor'   => esc_html__( 'Contributor', 'mainwp' ),
            'author'        => esc_html__( 'Author', 'mainwp' ),
            'editor'        => esc_html__( 'Editor', 'mainwp' ),
        );

        $editable_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $editable_roles ), '4.0.7.2', 'mainwp_users_manage_roles' );  // @deprecated Use 'mainwp_users_manage_roles' instead. NOSONAR - not IP.
        $editable_roles = apply_filters( 'mainwp_users_manage_roles', $editable_roles );

        $editable_roles[''] = esc_html__( '&mdash; No role for this site &mdash;', 'mainwp' );

        ?>
        <div id="mainwp-edit-users-modal" class="ui modal">
            <i class="close icon"></i>
            <div class="header"><?php esc_html_e( 'Edit User', 'mainwp' ); ?></div>
            <div class="scrolling content">
                <div class="ui info message"><?php esc_html_e( 'Empty fields will not be passed to child sites.', 'mainwp' ); ?></div>
                <form id="update_user_profile" class="ui form">
                    <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>

                    <h3 class="ui header"><?php esc_html_e( 'Name', 'mainwp' ); ?></h3>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Role', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <select name="role" id="role" class="ui selection dropdown">
                                <?php
                                foreach ( $editable_roles as $role_id => $role_name ) {
                                    echo '<option value="' . esc_attr( $role_id ) . '" ' . ( 'donotupdate' === $role_id ? 'selected="selected"' : '' ) . '>' . esc_html( $role_name ) . '</option>';
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'First Name', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <input type="text" name="first_name" id="first_name" value="" />
                            </div>
                        </div>
                    </div>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Last Name', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <input type="text" name="last_name" id="last_name" value="" />
                            </div>
                        </div>
                    </div>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Nickname', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <input type="text" name="nickname" id="nickname" value="" />
                            </div>
                        </div>
                    </div>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Display name publicly as', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <select name="display_name" id="display_name" class="ui selection dropdown"></select>
                            </div>
                        </div>
                    </div>

                    <h3 class="ui header"><?php esc_html_e( 'Contact Info', 'mainwp' ); ?></h3>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Email', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <input type="email" name="email" id="email" value="" />
                            </div>
                        </div>
                    </div>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Website', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <input type="url" name="url" id="url" value="" />
                            </div>
                        </div>
                    </div>

                    <h3 class="ui header"><?php esc_html_e( 'About the user', 'mainwp' ); ?></h3>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Biographical Info', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui input">
                                <textarea name="description" id="description" rows="5" cols="30" style="width:100%"></textarea>
                            </div>
                        </div>
                    </div>

                    <h3 class="ui header"><?php esc_html_e( 'Account Management', 'mainwp' ); ?></h3>

                    <div class="ui grid field">
                        <label class="six wide column middle aligned"><?php esc_html_e( 'Password', 'mainwp' ); ?></label>
                        <div class="ui six wide column">
                            <div class="ui action input">
                                <input class="hidden" value=" "/>
                                <input type="text" id="password" name="password" autocomplete="off" value="">
                            </div>
                        </div>
                    </div>

                </form>
            </div>

            <div class="actions">
                <div id="mainwp_update_password_error" style="display: none"></div>
                <span id="mainwp_users_updating"><i class="ui active inline loader tiny"></i></span>
                <input type="button" class="ui green button" id="mainwp_btn_update_user" value="<?php esc_attr_e( 'Update', 'mainwp' ); ?>">
            </div>
        </div>
        <?php
    }

    /**
     * Renders Users Table.
     *
     * @param bool   $cached true|false Cached or not.
     * @param string $role Current user role.
     * @param string $groups Current user groups.
     * @param string $sites Current Child Sites the user is on.
     * @param null   $search Search field.
     * @param mixed  $clients Current Clients's Sites the user is on.
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::echo_body()
     */
    public static function render_table( $cached = true, $role = '', $groups = '', $sites = '', $search = null, $clients = '' ) {

        $has_cached_results = $cached && isset( $_SESSION['MainWPUsersSearch'] ) && ! empty( $_SESSION['MainWPUsersSearch'] );

        /**
         * Action: mainwp_before_users_table
         *
         * Fires before the User table.
         *
         * @since 4.1
         */
        do_action( 'mainwp_before_users_table' );

        if ( ! $cached || $has_cached_results ) {
            ?>
        <table id="mainwp-users-table" class="ui unstackable single line table" style="width:100%">
            <thead>
                <tr>
                    <th  class="no-sort collapsing check-column"><span class="ui checkbox"><input id="cb-select-all-top" type="checkbox" /></span></th>
                    <?php do_action( 'mainwp_users_table_header' ); ?>
                    <th id="name"><?php esc_html_e( 'Name', 'mainwp' ); ?></th>
                    <th id="username"><?php esc_html_e( 'Username', 'mainwp' ); ?></th>
                    <th id="email"><?php esc_html_e( 'E-mail', 'mainwp' ); ?></th>
                    <th id="last_password_change" class="right aligned"><?php esc_html_e( 'Last Password Change', 'mainwp' ); ?></th>
                    <th id="password_status" class="center aligned"><?php esc_html_e( 'Password Status', 'mainwp' ); ?></th>
                    <th id="role"><?php esc_html_e( 'Role', 'mainwp' ); ?></th>
                    <th id="posts"><?php esc_html_e( 'Posts', 'mainwp' ); ?></th>
                    <th id="website"><?php esc_html_e( 'Website', 'mainwp' ); ?></th>
                    <th id="mainwp-users-actions" class="no-sort collapsing"></th>
                </tr>
            </thead>
            <tbody id="mainwp-users-list">
            <?php
            if ( $cached ) {
                MainWP_Cache::echo_body( 'Users' );
            } else {
                MainWP_Cache::init_cache( 'Users' );
                static::render_table_body( $role, $groups, $sites, $search, $clients );
            }
            ?>
            </tbody>
        </table>
            <?php
        } else {
            MainWP_UI::render_empty_page_placeholder(
                esc_html__( 'Find Users', 'mainwp' ),
                esc_html__( 'Select the Child Sites you want, choose any filters, then click Show Users.', 'mainwp' ),
                '<em data-emoji=":busts_in_silhouette:" class="big"></em>'
            );
        }
        /**
         * Action: mainwp_after_users_table
         *
         * Fires after the User table.
         *
         * @since 4.1
         */
        do_action( 'mainwp_after_users_table' );

        /**
         * Filter: mainwp_users_table_fatures
         *
         * Filters the Manage Users table features.
         *
         * @since 4.1
         */
        $table_features = array(
            'searching'  => 'true',
            'paging'     => 'true',
            'info'       => 'true',
            'stateSave'  => 'true',
            'scrollX'    => 'true',
            'responsive' => 'true',
            'colReorder' => '{columns:":not(.check-column):not(:last-child)"}',
            'order'      => '[]',
        );
        $table_features = apply_filters( 'mainwp_users_table_fatures', $table_features );
        ?>
        <script type="text/javascript">
        if (typeof responsive === 'undefined') {
            let responsive = false; // to fix redeclaration for ajax request.
        }
        responsive = <?php echo esc_html( $table_features['responsive'] ); ?>;
        if( jQuery( window ).width() > 1140 ) {
            responsive = false;
        }
        jQuery( document ).ready( function () {
            try {
                jQuery( "#mainwp-users-table" ).DataTable().destroy(); // to fix re-init database issue.
                $manage_users_table = jQuery( "#mainwp-users-table" ).DataTable( {
                    "responsive" : responsive,
                    "searching" : <?php echo esc_html( $table_features['searching'] ); ?>,
                    "colReorder" : <?php echo $table_features['colReorder']; // phpcs:ignore -- specical chars. ?>,
                    "stateSave":  <?php echo esc_html( $table_features['stateSave'] ); ?>,
                    "paging": <?php echo esc_html( $table_features['paging'] ); ?>,
                    "info": <?php echo esc_html( $table_features['info'] ); ?>,
                    "order": <?php echo esc_html( $table_features['order'] ); ?>,
                    "scrollX" : <?php echo esc_html( $table_features['scrollX'] ); ?>,
                    "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
                    "language" : { "emptyTable": "<?php esc_html_e( 'Please use the search options to find wanted users.', 'mainwp' ); ?>" },
                    "columnDefs": [
                        {
                            "targets": 'no-sort',
                            "orderable": false
                        },
                        {
                            "targets": '#last_password_change',
                            "type": 'string'
                        },
                        {
                            "targets": '#password_status',
                            "type": 'string'
                        }
                    ],
                    "drawCallback": function() {
                        setTimeout(() => {
                            jQuery('#mainwp-users-table .ui.dropdown').dropdown();
                            jQuery('#mainwp-users-table .ui.checkbox').checkbox();
                            mainwp_datatable_fix_menu_overflow('#mainwp-users-table');
                            mainwp_table_check_columns_init(); // ajax: to fix checkbox all.
                            updateUsersBulkActionsState();
                        }, 1000);
                    },
                    select: {
                        items: 'row',
                        style: 'multi+shift',
                        selector: 'tr>td:not(.not-selectable)'
                    }
                } ).on('select', function (e, dt, type, indexes) {
                    if( 'row' == type ){
                        dt.rows(indexes)
                        .nodes()
                        .to$().find('td.check-column .ui.checkbox' ).checkbox('set checked');
                        updateUsersBulkActionsState();
                    }
                }).on('deselect', function (e, dt, type, indexes) {
                    if( 'row' == type ){
                        dt.rows(indexes)
                        .nodes()
                        .to$().find('td.check-column .ui.checkbox' ).checkbox('set unchecked');
                        updateUsersBulkActionsState();
                    }
                }).on( 'columns-reordered', function () {
                    setTimeout(() => {
                        jQuery( '#mainwp-users-table .ui.dropdown' ).dropdown();
                        jQuery( '#mainwp-users-table .ui.checkbox' ).checkbox();
                        mainwp_datatable_fix_menu_overflow('#mainwp-users-table');
                        mainwp_table_check_columns_init(); // ajax: to fix checkbox all.
                    }, 1000);
                });
            } catch ( err ) {
                // to fix js error.
            }

            _init_manage_sites_screen = function() {
                jQuery( '#mainwp-overview-screen-options-modal input[type=checkbox][id^="mainwp_show_column_"]' ).each( function() {
                    let col_id = jQuery( this ).attr( 'id' );
                    col_id = col_id.replace( "mainwp_show_column_", "" );
                    try {
                        $manage_users_table.column( '#' + col_id ).visible( jQuery(this).is( ':checked' ) );
                    } catch(err) {
                        // to fix js error.
                    }
                } );
            };
            _init_manage_sites_screen();

            updateUsersBulkActionsState();

            jQuery(document).on('change', '#mainwp-users-table .check-column INPUT:checkbox', function() {
                updateUsersBulkActionsState();
            });

        } );
        </script>
        <?php
    }

    /**
     * Renders the table body.
     *
     * @param string $role   User Role.
     * @param string $groups Usr Group.
     * @param string $sites  Users Sites.
     * @param string $search Search field.
     * @param mixed  $clients Current Clients's Sites the user is on.
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::init_cache()
     * @uses \MainWP\Dashboard\MainWP_Cache::add_context()
     * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
     * @uses \MainWP\Dashboard\MainWP_DB::query()
     * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
     * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_by_group_id()
     * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
     * @uses \MainWP\Dashboard\MainWP_DB::free_result()
     * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
     * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
     */
    public static function render_table_body( $role = '', $groups = '', $sites = '', $search = '', $clients = '' ) { // phpcs:ignore -- NOSONAR - current complexity required to achieve desired results. Pull request solutions appreciated.
        $output         = new \stdClass();
        $output->errors = array();
        $output->users  = 0;

        $data_fields   = MainWP_System_Utility::get_default_map_site_fields();
        $data_fields[] = 'users';

        if ( 1 === (int) get_option( 'mainwp_optimize', 1 ) || MainWP_Demo_Handle::is_demo_mode() ) {

            $check_users_role = false;

            if ( ! empty( $role ) ) {
                $roles = explode( ',', $role );
                if ( is_array( $roles ) ) {
                    $check_users_role = true;
                }
            }

            $dbwebsites = array();
            if ( ! empty( $sites ) ) {
                foreach ( $sites as $v ) {
                    if ( MainWP_Utility::ctype_digit( $v ) ) {
                        $website = MainWP_DB::instance()->get_website_by_id( $v );
                        if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
                            $dbwebsites[ $website->id ] = $website;
                        }
                    }
                }
            }
            if ( ! empty( $groups ) ) {
                foreach ( $groups as $v ) {
                    if ( MainWP_Utility::ctype_digit( $v ) ) {
                        $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
                        while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
                            if ( ! empty( $website->sync_errors ) || MainWP_System_Utility::is_suspended_site( $website ) ) {
                                continue;
                            }
                            $dbwebsites[ $website->id ] = $website;
                        }
                        MainWP_DB::free_result( $websites );
                    }
                }
            }

            if ( ! empty( $clients ) && is_array( $clients ) ) {
                $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
                    $clients,
                    array(
                        'select_data' => $data_fields,
                    )
                );

                foreach ( $websites as $website ) {
                    if ( ! empty( $website->sync_errors ) || MainWP_System_Utility::is_suspended_site( $website ) ) {
                        continue;
                    }
                    $dbwebsites[ $website->id ] = $website;
                }
            }

            if ( $dbwebsites ) {
                foreach ( $dbwebsites as $website ) {
                    $allUsers         = json_decode( $website->users, true );
                    $allUsersCount    = count( $allUsers );
                    $search_user_role = array();
                    if ( $check_users_role ) {
                        for ( $i = 0; $i < $allUsersCount; $i++ ) {
                            $user = $allUsers[ $i ];
                            foreach ( $roles as $_role ) {
                                if ( stristr( $user['role'], $_role ) ) {
                                    if ( ! in_array( $user['id'], $search_user_role ) ) {
                                        $search_user_role[] = $user['id'];
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    for ( $i = 0; $i < $allUsersCount; $i++ ) {
                        $user = $allUsers[ $i ];
                        if ( ! empty( $search ) && ! stristr( $user['login'], trim( $search ) ) && ! stristr( $user['display_name'], trim( $search ) ) && ! stristr( $user['email'], trim( $search ) ) ) {
                            continue;
                        }

                        if ( $check_users_role && ! in_array( $user['id'], $search_user_role ) ) {
                            continue;
                        }

                        $tmpUsers       = array( $user );
                        $output->users += static::users_search_handler_renderer( $tmpUsers, $website );
                    }
                }
            }
        } else {
            $dbwebsites = array();
            if ( '' !== $sites ) {
                foreach ( $sites as $v ) {
                    if ( MainWP_Utility::ctype_digit( $v ) ) {
                        $website = MainWP_DB::instance()->get_website_by_id( $v );
                        if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
                            $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                                $website,
                                $data_fields
                            );
                        }
                    }
                }
            }
            if ( '' !== $groups ) {
                foreach ( $groups as $v ) {
                    if ( MainWP_Utility::ctype_digit( $v ) ) {
                        $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
                        while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
                            if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                                continue;
                            }
                            $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                                $website,
                                $data_fields
                            );
                        }
                        MainWP_DB::free_result( $websites );
                    }
                }
            }

            if ( '' !== $clients && is_array( $clients ) ) {
                $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
                    $clients,
                    array(
                        'select_data' => $data_fields,
                    )
                );
                if ( $websites ) {
                    foreach ( $websites as $website ) {
                        if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                            continue;
                        }
                        $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                            $website,
                            $data_fields
                        );
                    }
                }
            }

            $post_data = array(
                'role'           => $role,
                'search'         => '*' . trim( $search ) . '*',
                'search_columns' => 'user_login,display_name,user_email',
            );

            MainWP_Connect::fetch_urls_authed(
                $dbwebsites,
                'search_users',
                $post_data,
                array(
                    static::get_class_name(),
                    'users_search_handler',
                ),
                $output
            );
        }

        MainWP_Cache::add_context(
            'Users',
            array(
                'count'   => $output->users,
                'keyword' => $search,
                'status'  => $role,
                'sites'   => '' !== $sites ? $sites : '',
                'groups'  => '' !== $groups ? $groups : '',
                'clients' => ( '' !== $clients ) ? $clients : '',
            )
        );

        // Sort if required.

        if ( empty( $output->users ) ) {
            static::render_cache_not_found();
        }
    }

    /**
     * Renders when cache is not found.
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::add_body()
     */
    public static function render_cache_not_found() {
        ob_start();
        $newOutput = ob_get_clean();
        echo $newOutput; // phpcs:ignore WordPress.Security.EscapeOutput
        MainWP_Cache::add_body( 'Users', $newOutput );
    }

    /**
     * Gets the selected users current role.
     *
     * @param string $role Selected Users Role.
     */
    private static function get_role( $role ) {
        if ( is_array( $role ) ) {
            $allowed_roles = array( 'subscriber', 'administrator', 'editor', 'author', 'contributor' );
            $ret           = '';
            foreach ( $role as $ro ) {
                if ( in_array( $ro, $allowed_roles ) ) {
                    $ret .= ucfirst( $ro ) . ', ';
                }
            }
            $ret = rtrim( $ret, ', ' );
            if ( '' === $ret ) {
                $ret = 'None';
            }

            return $ret;
        }

        return ucfirst( $role );
    }

    /**
     * Renders Search results.
     *
     * @param array  $users Users array.
     * @param object $website Object containing the child site info.
     *
     * @return mixed Search results table.
     *
     * @uses \MainWP\Dashboard\MainWP_Cache::add_body()
     */
    protected static function users_search_handler_renderer( $users, $website ) { // phpcs:ignore -- NOSONAR -complex.
        $return  = 0;
        $is_demo = MainWP_Demo_Handle::is_demo_mode();

        foreach ( $users as $user ) {
            if ( ! is_array( $user ) ) {
                continue;
            }
            ob_start();
            ?>
            <tr>
                <td class="check-column"><span class="ui checkbox"><input type="checkbox" name="user[]" value="1"></span></td>
                <?php do_action( 'mainwp_users_table_column', $user, $website ); ?>
                <td class="name column-name not-selectable">
                    <?php echo ! empty( $user['display_name'] ) ? esc_html( $user['display_name'] ) : '&nbsp;'; ?>
                </td>
                <td class="username column-username not-selectable"><strong><abbr title="<?php echo esc_attr( $user['login'] ); ?>"><?php echo esc_html( $user['login'] ); ?></abbr></strong></td>
                <td class="email column-email not-selectable"><a href="mailto:<?php echo esc_attr( $user['email'] ); ?>"><?php echo esc_html( $user['email'] ); ?></a></td>
                <td class="last_password_change column-last_password_change not-selectable right aligned" data-order="<?php echo ! empty( $user['last_password_change'] ) ? intval( $user['last_password_change'] ) : 0; ?>">
                    <?php
                    if ( ! empty( $user['last_password_change'] ) ) {
                        $timestamp = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $user['last_password_change'] );
                        echo '<span data-tooltip="' . esc_attr( $timestamp ) . '" data-inverted="" data-position="left center">' . esc_html( MainWP_Utility::time_elapsed_string( $user['last_password_change'] ) ) . '</span>';
                    } else {
                        echo '<span class="ui grey text" data-tooltip="' . esc_attr__( 'Password change hasn\'t been tracked yet.', 'mainwp' ) . '" data-inverted="" data-position="left center">' . esc_html__( 'Unknown', 'mainwp' ) . '</span>';
                    }
                    ?>
                </td>
                <?php
                $status       = isset( $user['password_status'] ) ? $user['password_status'] : '';
                $status_order = 0;
                if ( 'OVERDUE' === $status ) {
                    $status_order = 3;
                } elseif ( 'DUE' === $status ) {
                    $status_order = 2;
                } elseif ( 'OK' === $status ) {
                    $status_order = 1;
                }
                ?>
                <td class="password_status column-password_status not-selectable center aligned" data-order="<?php echo intval( $status_order ); ?>">
                    <?php
                    if ( 'OK' === $status && empty( $user['last_password_change'] ) ) {
                        echo '<span class="ui grey mini label" data-tooltip="' . esc_attr__( 'Password change hasn\'t been tracked yet.', 'mainwp' ) . '" data-inverted="" data-position="left center">' . esc_html__( 'Unknown', 'mainwp' ) . '</span>';
                    } elseif ( 'OK' === $status ) {
                        echo '<span class="ui green mini label">' . esc_html__( 'Fresh', 'mainwp' ) . '</span>';
                    } elseif ( 'DUE' === $status ) {
                        if ( ! empty( $user['password_due_time'] ) ) {
                            $due_date = MainWP_Utility::format_timestamp( $user['password_due_time'] );
                            echo '<span class="ui yellow mini label" data-tooltip="' . esc_attr__( 'Due: ', 'mainwp' ) . esc_attr( $due_date ) . '" data-inverted="" data-position="left center">' . esc_html__( 'Due Soon', 'mainwp' ) . '</span>';
                        } else {
                            echo '<span class="ui yellow mini label">' . esc_html__( 'Due Soon', 'mainwp' ) . '</span>';
                        }
                    } elseif ( 'OVERDUE' === $status ) {
                        if ( ! empty( $user['password_due_time'] ) ) {
                            $overdue_date = MainWP_Utility::format_timestamp( $user['password_due_time'] );
                            echo '<span class="ui red mini label" data-tooltip="' . esc_attr__( 'Was due: ', 'mainwp' ) . esc_attr( $overdue_date ) . '" data-inverted="" data-position="left center">' . esc_html__( 'Overdue', 'mainwp' ) . '</span>';
                        } else {
                            echo '<span class="ui red mini label">' . esc_html__( 'Overdue', 'mainwp' ) . '</span>';
                        }
                    } elseif ( 'DISABLED' === $status || empty( $status ) ) {
                        echo '<span class="ui grey mini label">' . esc_html__( 'Disabled', 'mainwp' ) . '</span>';
                    } else {
                        echo '<span class="ui grey text">' . esc_html( $status ) . '</span>';
                    }
                    ?>
                </td>
                <td class="role column-role not-selectable"><?php echo static::get_role( $user['role'] ); // phpcs:ignore WordPress.Security.EscapeOutput ?></td>
                <td class="posts column-posts not-selectable"><a href="<?php echo esc_url( admin_url( 'admin.php?page=PostBulkManage&siteid=' . intval( $website->id ) ) . '&userid=' . $user['id'] ); ?>"><?php echo esc_html( $user['post_count'] ); ?></a></td>
                <td class="website column-website not-selectable"><a href="<?php echo esc_url( $website->url ); ?>" target="_blank"><?php echo esc_html( $website->url ); ?></a></td>
                <td class="right aligned not-selectable">
                    <input class="userId" type="hidden" name="id" value="<?php echo esc_attr( $user['id'] ); ?>" />
                    <input class="userName" type="hidden" name="name" value="<?php echo esc_attr( $user['login'] ); ?>" />
                    <input class="websiteId" type="hidden" name="id" value="<?php echo intval( $website->id ); ?>" />
                    <div class="ui right pointing dropdown" style="z-index: 999">
                        <a href="javascript:void(0)"><i class="ellipsis vertical icon"></i></a>
                        <div class="menu">
                            <a class="item user_getedit" href="#"><?php esc_html_e( 'Edit', 'mainwp' ); ?></a>
                            <?php if ( ( 1 !== (int) $user['id'] ) && ( $user['login'] !== $website->adminname ) ) { ?>
                            <a class="item user_submitdelete" href="#"><?php esc_html_e( 'Delete', 'mainwp' ); ?></a>
                            <?php } elseif ( ( 1 === (int) $user['id'] ) || ( $user['login'] === $website->adminname ) ) { ?>
                            <a href="javascript:void(0)" class="item" data-tooltip="This user is used for our secure link, it can not be deleted." data-inverted="" data-position="left center"><?php esc_html_e( 'Delete', 'mainwp' ); ?></a>
                            <?php } ?>
                            <?php if ( ! $is_demo ) : ?>
                            <a class="item" href="<?php MainWP_Site_Open::get_open_site_admin_link( $website->id, true ); ?>" data-tooltip="<?php esc_attr_e( 'Jump to the site WP Admin', 'mainwp' ); ?>"  data-position="bottom right"  data-inverted="" class="open_newwindow_wpadmin ui green basic icon button" target="_blank"><?php esc_html_e( 'Go to WP Admin', 'mainwp' ); ?></a>
                            <?php else : ?>
                                <a class="item" href="<?php echo esc_url( $website->url ) . 'wp-admin.html'; ?>" data-tooltip="<?php esc_attr_e( 'Jump to the site WP Admin', 'mainwp' ); ?>"  data-position="bottom right"  data-inverted="" class="open_newwindow_wpadmin ui green basic icon button" target="_blank"><?php esc_html_e( 'Go to WP Admin', 'mainwp' ); ?></a>
                            <?php endif; ?>
                            <?php
                            /**
                             * Action: mainwp_users_table_action
                             *
                             * Adds a new item in the Actions menu in Manage Users table.
                             *
                             * Suggested HTML markup:
                             * <a class="item" href="Your custom URL">Your custom label</a>
                             *
                             * @param array $user    Array containing the user data.
                             * @param array $website Object containing the website data.
                             *
                             * @since 4.1
                             */
                            do_action( 'mainwp_users_table_action', $user, $website );
                            ?>
                        </div>
                    </div>
                </td>
            </tr>
            <?php
            $newOutput = ob_get_clean();
            echo $newOutput; // phpcs:ignore WordPress.Security.EscapeOutput
            MainWP_Cache::add_body( 'Users', $newOutput );
            ++$return;
        }

        return $return;
    }

    /**
     * Handles user search.
     *
     * @param mixed $data Search data.
     * @param mixed $website Child Site.
     * @param mixed $output Output to pass to static::users_search_handler_renderer().
     *
     * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
     * @uses \MainWP\Dashboard\MainWP_Exception
     * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response()
     */
    public static function users_search_handler( $data, $website, &$output ) {
        if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $website ) ) {
            return;
        }
        if ( 0 < preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) ) {
            $result = $results[1];
            $users  = MainWP_System_Utility::get_child_response( base64_decode( $result ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
            unset( $results );
            $output->users += static::users_search_handler_renderer( $users, $website );
            unset( $users );
        } else {
            $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( 'NOMAINWP', $website->url ) );
        }
    }

    /**
     * Deletes user.
     */
    public static function delete() {
        static::action( 'delete' );
        die( wp_json_encode( array( 'result' => esc_html__( 'User has been deleted', 'mainwp' ) ) ) );
    }

    /**
     * Edits user.
     */
    public static function edit() {
        $information = static::action( 'edit' );
        wp_send_json( $information );
    }

    /**
     * Updates user.
     */
    public static function update_user() {
        static::action( 'update_user' );
        die( wp_json_encode( array( 'result' => esc_html__( 'User has been updated', 'mainwp' ) ) ) );
    }

    /**
     * Updates users password.
     */
    public static function update_password() {
        static::action( 'update_password' );
        die( wp_json_encode( array( 'result' => esc_html__( 'User password has been updated', 'mainwp' ) ) ) );
    }

    /**
     * Users actions.
     *
     * @param mixed  $pAction Action to perform delete|update_user|update_password.
     * @param string $extra   Additional Roles to add if any.
     *
     * @return mixed $information User update info that is returned.
     * @throws \MainWP_Exception Error message.
     *
     * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
     * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
     * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
     * @uses \MainWP\Dashboard\MainWP_Exception
     * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
     * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
     */
    public static function action( $pAction, $extra = '' ) { // phpcs:ignore -- NOSONAR - current complexity required to achieve desired results. Pull request solutions appreciated.

        // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        $userId    = isset( $_POST['userId'] ) ? sanitize_text_field( wp_unslash( $_POST['userId'] ) ) : false;
        $userName  = isset( $_POST['userName'] ) ? sanitize_text_field( wp_unslash( $_POST['userName'] ) ) : '';
        $websiteId = isset( $_POST['websiteId'] ) ? sanitize_text_field( wp_unslash( $_POST['websiteId'] ) ) : false;
        $pass      = isset( $_POST['update_password'] ) ? rawurldecode( wp_unslash( $_POST['update_password'] ) ) : '';

        if ( function_exists( '\mb_convert_encoding' ) ) {
            $pass = \mb_convert_encoding( $pass, 'ISO-8859-1', 'UTF-8' );
        } else {
            $pass = utf8_decode( $pass ); // to compatible.
        }

        if ( empty( $userId ) || empty( $websiteId ) ) {
            die( wp_json_encode( array( 'error' => esc_html__( 'Site ID or user ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
        }

        $website = MainWP_DB::instance()->get_website_by_id( $websiteId );

        if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
            die(
                wp_json_encode(
                    array(
                        'error'     => esc_html__( 'Suspended site.', 'mainwp' ),
                        'errorCode' => 'SUSPENDED_SITE',
                    )
                )
            );
        }

        if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
            die( wp_json_encode( array( 'error' => esc_html__( 'You can not edit this website!', 'mainwp' ) ) ) );
        }

        if ( ( 'delete' === $pAction ) && ( $website->adminname === $userName ) ) {
            die( wp_json_encode( array( 'error' => esc_html__( 'This user is used for our secure link, it can not be deleted.', 'mainwp' ) ) ) );
        }

        if ( 'update_user' === $pAction ) {
            $user_data = isset( $_POST['user_data'] ) ? wp_unslash( $_POST['user_data'] ) : '';
            parse_str( $user_data, $extra );
            if ( $website->adminname === $userName && is_array( $extra ) && isset( $extra['role'] ) ) {
                unset( $extra['role'] );
            }

            if ( ! empty( $pass ) ) {
                $extra['pass1'] = $pass;
                $extra['pass2'] = $pass;
            }
        }
        // phpcs:enable

        $optimize = ( 1 === (int) get_option( 'mainwp_optimize', 1 ) ) ? 1 : 0;

        /**
        * Action: mainwp_before_user_action
        *
        * Fires before user edit/delete/update_user/update_password actions.
        *
        * @since 4.1
        */
        do_action( 'mainwp_before_user_action', $pAction, $userId, $extra, $pass, $optimize, $website );

        try {
            $information = MainWP_Connect::fetch_url_authed(
                $website,
                'user_action',
                array(
                    'action'    => $pAction,
                    'id'        => $userId,
                    'extra'     => $extra,
                    'user_pass' => $pass,
                    'optimize'  => $optimize,
                )
            );

            if ( is_array( $information ) && isset( $information['status'] ) && ( 'SUCCESS' === $information['status'] ) ) {
                $data = isset( $information['other_data']['users_data'] ) ? $information['other_data']['users_data'] : array();  // user actions data.

                /**
                 * Fires immediately after user action.
                 *
                 * @since 4.5.1.1
                 */
                do_action( 'mainwp_user_action', $website, $pAction, $data, $extra, $optimize );
            }
        } catch ( MainWP_Exception $e ) {
            die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) );
        }

        /**
        * Action: mainwp_after_user_action
        *
        * Fires after user edit/delete/update_user/update_password actions.
        *
        * @since 4.1
        */
        do_action( 'mainwp_after_user_action', $information, $pAction, $userId, $extra, $pass, $optimize, $website );

        if ( is_array( $information ) && isset( $information['error'] ) ) {
            wp_send_json( array( 'error' => esc_html( $information['error'] ) ) );
        }

        if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) {
            die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error.', 'mainwp' ) ) ) );
        } elseif ( 'update_user' === $pAction ) {
            if ( $optimize && isset( $information['users'] ) ) {
                $websiteValues['users'] = wp_json_encode( $information['users'] );
                MainWP_DB::instance()->update_website_values( $websiteId, $websiteValues );
            }
        }

        // This user is used for our secure link, you can not change the role.
        if ( 'edit' === $pAction && $website->adminname === $userName && is_array( $information ) && isset( $information['user_data'] ) ) {
            $information['is_secure_admin'] = 1;
        }

        return $information;
    }

    /**
     * Renders the Add New user form.
     */
    public static function render_bulk_add() {

        /**
         * Filter: mainwp_new_user_password_complexity
         *
         * Filters the Password lenght for the Add New user, Password field.
         *
         * Since 4.1
         */
        $pass_complexity = apply_filters( 'mainwp_new_user_password_complexity', '24' );
        static::render_header( 'Add' );
        // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        ?>
        <div id="mainwp-add-users">
            <?php
            /**
             * Action: mainwp_before_new_user_form
             *
             * Fires before the Add New user form.
             *
             * @since 4.1
             */
            do_action( 'mainwp_before_new_user_form' );
            ?>
            <form action="" method="post" name="createuser" id="createuser" class="add:users: validate">
                <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
                <div class="mainwp-main-content ui padded segment">
                    <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-add-user-info-message' ) ) : ?>
                    <div class="ui info message">
                        <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-add-user-info-message"></i>
                        <div class="header"><?php esc_html_e( 'Create users on all selected Child Sites.', 'mainwp' ); ?></div>
                        <?php esc_html_e( 'Select one or more sites, tags or clients before submitting.', 'mainwp' ); ?>
                    </div>
                <?php endif; ?>
                    <?php if ( isset( $_GET['success'] ) && 1 === (int) $_GET['success'] ) : ?>
                        <div class="ui green message">
                            <i class="close icon"></i>
                            <?php esc_html_e( 'User created successfully. You can now manage this user from the Manage Users page, or create another one here.', 'mainwp' ); ?>
                        </div>
                    <?php endif; ?>
                    <div class="ui message" id="mainwp-message-zone" style="display:none;"></div>
                    <div id="mainwp-add-new-user-form" >
                        <?php
                        /**
                         * Action: mainwp_before_new_user_form_fields
                         *
                         * Fires before the Add New user form fields.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_before_new_user_form_fields' );
                        ?>
                        <div class="ui form">
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Username', 'mainwp' ); ?> <span class="ui small red text"><?php esc_html_e( '(Required)', 'mainwp' ); ?></span></label>
                                <div class="ui six wide column">
                                    <div class="ui input">
                                        <input type="text" id="user_login" name="user_login" value="<?php echo ( isset( $_POST['user_login'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) ) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'E-mail', 'mainwp' ); ?> <span class="ui small red text"><?php esc_html_e( '(Required)', 'mainwp' ); ?></span></label>
                                <div class="ui six wide column">
                                    <div class="ui input">
                                        <input type="text" id="email" name="email" value="<?php echo ( isset( $_POST['email'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['email'] ) ) ) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'First Name', 'mainwp' ); ?></label>
                                <div class="ui six wide column">
                                    <div class="ui input">
                                        <input type="text" id="first_name" name="first_name" value="<?php echo ( isset( $_POST['first_name'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) ) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Last Name', 'mainwp' ); ?></label>
                                <div class="ui six wide column">
                                    <div class="ui input">
                                        <input type="text" id="last_name" name="last_name" value="<?php echo ( isset( $_POST['last_name'] ) ) ? esc_html( sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) ) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Website', 'mainwp' ); ?></label>
                                <div class="ui six wide column">
                                    <div class="ui input">
                                        <input type="text" id="url" name="url" value="<?php echo ( isset( $_POST['url'] ) ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : ''; ?>">
                                    </div>
                                </div>
                            </div>
                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Password', 'mainwp' ); ?> <span class="ui small red text"><?php esc_html_e( '(Required)', 'mainwp' ); ?></span></label>
                                <div class="ui six wide column">
                                    <input class="hidden" value=" "/>
                                    <div class="ui icon input">
                                        <input type="text" id="password" name="password" autocomplete="off" value="<?php echo esc_attr( wp_generate_password( $pass_complexity ) ); ?>">
                                        <i class="sync alternate link icon mainwp-generate-password-button"></i>
                                    </div>
                                </div>
                            </div>

                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Send Password?', 'mainwp' ); ?></label>
                                <div class="one wide column ui toggle checkbox">
                                    <input type="checkbox" name="send_password" id="send_password" <?php echo ( isset( $_POST['send_password'] ) ) ? esc_html( 'checked' ) : ''; ?> >
                                </div>
                                <div class="nine wide column">
                                    <span class="ui small text"><?php esc_html_e( 'If enabled, the new user will receive their login credentials via email.', 'mainwp' ); ?></span>
                                </div>
                            </div>

                            <?php
                            $user_roles = array(
                                'subscriber'    => esc_html__( 'Subscriber', 'mainwp' ),
                                'administrator' => esc_html__( 'Administrator', 'mainwp' ),
                                'editor'        => esc_html__( 'Editor', 'mainwp' ),
                                'author'        => esc_html__( 'Author', 'mainwp' ),
                                'contributor'   => esc_html__( 'Contributor', 'mainwp' ),
                            );
                            $user_roles = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $user_roles ), '4.0.7.2', 'mainwp_users_manage_roles' );  // @deprecated Use 'mainwp_users_manage_roles' instead. NOSONAR - not IP.
                            $user_roles = apply_filters( 'mainwp_users_manage_roles', $user_roles );
                            ?>

                            <div class="ui grid field">
                                <label class="six wide column middle aligned"><?php esc_html_e( 'Role', 'mainwp' ); ?></label>
                                <div class="six wide column">
                                    <select class="ui dropdown" name="role" id="role">
                                        <?php
                                        foreach ( $user_roles as $r => $n ) {
                                            if ( empty( $r ) ) {
                                                continue;
                                            }
                                            ?>
                                            <option value="<?php echo esc_html( $r ); ?>" <?php echo ( isset( $_POST['role'] ) && $_POST['role'] === $r ) ? esc_html( 'selected' ) : ''; ?>><?php echo esc_html( $n ); ?></option>
                                            <?php
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>

                        </div>
                        <?php
                        /**
                         * Action: mainwp_after_new_user_form_fields
                         *
                         * Fires after the Add New user form fields.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_after_new_user_form_fields' );
                        ?>
                    </div>
                </div>
                <div class="mainwp-side-content mainwp-no-padding">
                    <?php
                    /**
                     * Action: mainwp_add_new_user_sidebar_top
                     *
                     * Fires at the top of the sidebar on Add New user page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_add_new_user_sidebar_top' );
                    ?>
                    <div class="mainwp-select-sites ui accordion mainwp-sidebar-accordion">
                        <?php
                        /**
                         * Action: mainwp_add_new_user_before_select_sites
                         *
                         * Fires before the Select Sites section on the Add New user page.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_add_new_user_before_select_sites' );
                        ?>
                        <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div>
                        <div class="content active">
                            <?php
                            $sel_params = array(
                                'show_client' => true,
                            );
                            MainWP_UI_Select_Sites::select_sites_box( $sel_params );
                            ?>
                            </div>
                        <?php
                        /**
                         * Action: mainwp_add_new_user_after_select_sites
                         *
                         * Fires after the Select Sites section on the Add New user page.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_add_new_user_after_select_sites' );
                        ?>
                    </div>
                    <div class="ui fitted divider"></div>
                    <div class="mainwp-search-submit">
                        <?php
                        /**
                         * Action: mainwp_add_new_user_before_submit_button
                         *
                         * Fires before the Submit button on the Add New user page.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_add_new_user_before_submit_button' );
                        ?>
                        <input type="button" name="createuser" id="bulk_add_createuser" class="ui big green fluid button" value="<?php esc_attr_e( 'Add New User', 'mainwp' ); ?> "/>
                        <?php
                        /**
                         * Action: mainwp_add_new_user_after_submit_button
                         *
                         * Fires after the Submit button on the Add New user page.
                         *
                         * @since 4.1
                         */
                        do_action( 'mainwp_add_new_user_after_submit_button' );
                        ?>
                    </div>
                    <?php
                    /**
                     * Action: mainwp_add_new_user_sidebar_bottom
                     *
                     * Fires at the bottom of the sidebar on Add New user page.
                     *
                     * @since 4.1
                     */
                    do_action( 'mainwp_add_new_user_sidebar_bottom' );
                    ?>
                </div>
                <div style="clear:both"></div>
            </form>
            <script type="text/javascript">
                jQuery( document ).ready( function () {
                    // Initialize button state based on site selection
                    mainwp_init_button_site_selection_dependency( 'bulk_add_createuser' );
                } );
            </script>
            <?php
            /**
             * Action: mainwp_after_new_user_form
             *
             * Fires after the Add New user form.
             *
             * @since 4.1
             */
            do_action( 'mainwp_after_new_user_form' );
            ?>
        </div>
        <?php
        // phpcs:enable

        static::render_footer( 'Add' );
    }

    /**
     * Renders bulk Import Users form.
     *
     * @return void
     */
    public static function render_bulk_import_users() {
        if ( isset( $_FILES['import_user_file_bulkupload'] ) && isset( $_FILES['import_user_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['import_user_file_bulkupload']['error'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing
            static::render_bulk_upload();
            return;
        }
        ?>
        <?php static::render_header( 'Import' ); ?>
        <div id="MainWP_Bulk_AddUser">
            <form action="" method="post" name="createuser" id="createuser" class="add:users: validate" enctype="multipart/form-data">
                <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
                <?php static::render_import_users(); ?>
            </form>
        </div>
        <?php
        static::render_footer( 'Import' );
    }

    /**
     * Method render_import_users()
     *
     * Render Import Users page.
     */
    public static function render_import_users() {
        ?>
        <div class="ui padded segment" id="mainwp-import-sites">
            <?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-import-users-info-message' ) ) : ?>
                <div class="ui info message">
                    <i class="close icon mainwp-notice-dismiss" notice-id="mainwp-import-users-info-message"></i>
                    <?php /* translators: 1: opening anchor tag, 2: closing anchor tag */ printf( esc_html__( 'Use the form to bulk import users.  You can download the sample CSV file to see how to fomat the import file properly.  For additional help, please check this %1$shelp documentation%2$s.', 'mainwp' ), '<a href="https://docs.mainwp.com/sites/users/manage-users#import-users-via-csv/" target="_blank">', '</a>' ); ?>
                </div>
            <?php endif; ?>

            <div id="mainwp-message-zone" class="ui message" style="display:none"></div>
            <?php
            /**
             * Action: mainwp_before_import_users
             *
             * Fires above the Import Users section.
             *
             * @since 4.1
             */
            do_action( 'mainwp_before_import_users' );
            ?>
            <div id="mainwp-message-zone" class="ui message" style="display:none"></div>
            <div class="ui form">
                    <form method="POST" action="" enctype="multipart/form-data" id="mainwp_managesites_bulkadd_form">
                        <?php MainWP_UI::generate_wp_nonce( 'mainwp-admin-nonce' ); ?>
                        <div class="ui grid field">
                            <label class="six wide column middle aligned"><?php esc_html_e( 'Upload the CSV file', 'mainwp' ); ?> (<a href="<?php echo esc_url( MAINWP_PLUGIN_URL . 'assets/csv/sample_users.csv' ); ?>" ><?php esc_html_e( 'Download Sample CSV file', 'mainwp' ); ?></a>)</label>
                            <div class="ten wide column" data-tooltip="<?php esc_attr_e( 'Click to upload the import file.', 'mainwp' ); ?>" data-inverted="" data-position="left center">
                                <div class="ui file input">
                                <input type="file" name="import_user_file_bulkupload" id="import_user_file_bulkupload" accept="text/comma-separated-values" />
                                </div>
                            </div>
                        </div>
                        <div class="ui grid field">
                            <label class="six wide column middle aligned"><?php esc_html_e( 'CSV file contains a header', 'mainwp' ); ?></label>
                        <div class="ui toggle checkbox" data-tooltip="<?php esc_attr_e( 'Enable if the import file contains a header.', 'mainwp' ); ?>" data-inverted="" data-position="left center">
                            <input type="checkbox" name="import_user_chk_header_first" checked="checked" id="import_user_chk_header_first" value="1" />
                            </div>
                        </div>
                        <div class="ui divider"></div>
                        <input type="button" name="createuser" id="bulk_import_createuser" class="ui big green button" value="<?php esc_attr_e( 'Import Users', 'mainwp' ); ?>" />
                    </form>
                </div>
            <?php
            /**
             * Action: mainwp_after_import_users
             *
             * Fires under the Import Users section.
             *
             * @since 4.1
             */
            do_action( 'mainwp_after_import_users' );
            ?>
            </div>
        <?php
    }

    /**
     * Method do_bulk_add()
     *
     * Bulk User addition $_POST Handler.
     *
     * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
     * @uses \MainWP\Dashboard\MainWP_DB::query()
     * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
     * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
     * @uses \MainWP\Dashboard\MainWP_DB::free_result()
     * @uses \MainWP\Dashboard\MainWP_Bulk_Add::get_class_name()
     * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
     * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
     */
    public static function do_bulk_add() { // phpcs:ignore -- NOSONAR -Current complexity is required to achieve desired results. Pull request solutions appreciated.
        $errors      = array();
        $errorFields = array();

        // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        if ( isset( $_POST['select_by'] ) ) {
            $selected_sites   = ( isset( $_POST['selected_sites'] ) && is_array( $_POST['selected_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_sites'] ) ) : array();
            $selected_groups  = ( isset( $_POST['selected_groups'] ) && is_array( $_POST['selected_groups'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_groups'] ) ) : array();
            $selected_clients = ( isset( $_POST['selected_clients'] ) && is_array( $_POST['selected_clients'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['selected_clients'] ) ) : array();

            if ( ( 'group' === $_POST['select_by'] && empty( $selected_groups ) ) || ( 'site' === $_POST['select_by'] && empty( $selected_sites ) ) || ( 'client' === $_POST['select_by'] && empty( $selected_clients ) ) ) {
                $errors[] = esc_html__( 'Please select at least one website or group or client.', 'mainwp' );
            }
        } else {
            $errors[] = esc_html__( 'Please select at least one website or group or client.', 'mainwp' );
        }

        if ( ! isset( $_POST['user_login'] ) || '' === $_POST['user_login'] ) {
            $errorFields[] = 'user_login';
        }

        if ( ! isset( $_POST['email'] ) || '' === $_POST['email'] ) {
            $errorFields[] = 'email';
        }

        $allowed_roles = array( 'subscriber', 'administrator', 'editor', 'author', 'contributor' );
        $cus_roles     = array();
        $cus_roles     = apply_filters_deprecated( 'mainwp-users-manage-roles', array( $cus_roles ), '4.0.7.2', 'mainwp_users_manage_roles' );  // @deprecated Use 'mainwp_users_manage_roles' instead. NOSONAR - not IP.
        $cus_roles     = apply_filters( 'mainwp_users_manage_roles', $cus_roles );

        if ( is_array( $cus_roles ) && ! empty( $cus_roles ) ) {
            $cus_roles     = array_keys( $cus_roles );
            $allowed_roles = array_merge( $allowed_roles, $cus_roles );
        }

        if ( ! isset( $_POST['role'] ) || ! in_array( $_POST['role'], $allowed_roles ) ) {
            $errorFields[] = 'role';
        }

        $data_fields = MainWP_System_Utility::get_default_map_site_fields();

        if ( empty( $errors ) && empty( $errorFields ) ) {
            $user_to_add = array(
                'user_pass'  => isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '',
                'user_login' => isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '',
                'user_url'   => isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : '',
                'user_email' => isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '',
                'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
                'last_name'  => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
                'role'       => isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '',
            );

            $dbwebsites = array();

            if ( isset( $_POST['select_by'] ) && 'site' === $_POST['select_by'] ) {
                foreach ( $selected_sites as $k ) {
                    if ( MainWP_Utility::ctype_digit( $k ) ) {
                        $website = MainWP_DB::instance()->get_website_by_id( $k );
                        if ( empty( $website->sync_errors ) && ! MainWP_System_Utility::is_suspended_site( $website ) ) {
                            $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                                $website,
                                $data_fields
                            );
                        }
                    }
                }
            } elseif ( isset( $_POST['select_by'] ) && 'client' === $_POST['select_by'] ) {
                $websites = MainWP_DB_Client::instance()->get_websites_by_client_ids(
                    $selected_clients,
                    array(
                        'select_data' => $data_fields,
                    )
                );

                if ( $websites ) {
                    foreach ( $websites as $website ) {
                        if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                            continue;
                        }
                        $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                            $website,
                            $data_fields
                        );
                    }
                }
            } else {
                foreach ( $selected_groups as $k ) {
                    if ( MainWP_Utility::ctype_digit( $k ) ) {
                        $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $k ) );
                        while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
                            if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                                continue;
                            }
                            $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                                $website,
                                $data_fields
                            );
                        }
                        MainWP_DB::free_result( $websites );
                    }
                }
            }

            if ( ! empty( $dbwebsites ) ) {
                $post_data      = array(
                    'new_user'      => base64_encode( wp_json_encode( $user_to_add ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
                    'send_password' => ( isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : '' ),
                );
                $output         = new \stdClass();
                $output->ok     = array();
                $output->errors = array();

                /**
                * Action: mainwp_before_user_create
                *
                * Fires before user create.
                *
                * @since 4.1
                */
                do_action( 'mainwp_before_user_create', $post_data, $dbwebsites );

                MainWP_Connect::fetch_urls_authed(
                    $dbwebsites,
                    'newuser',
                    $post_data,
                    array(
                        MainWP_Bulk_Add::get_class_name(),
                        'posting_bulk_handler',
                    ),
                    $output
                );

                /**
                * Action: mainwp_after_user_create
                *
                * Fires after user create.
                *
                * @since 4.1
                */
                do_action( 'mainwp_after_user_create', $output, $post_data, $dbwebsites );
            }

            $countSites     = 0;
            $countRealItems = 0;
            foreach ( $dbwebsites as $website ) {
                if ( isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ) {
                    ++$countSites;
                    ++$countRealItems;
                }
            }
            static::render_bulk_add_modal( $dbwebsites, $output );
        } else {
            echo wp_json_encode( array( $errorFields, $errors ) );
        }
        // phpcs:enable
    }

    /**
     * Renders Bulk User addition Modal window.
     *
     * @param mixed $dbwebsites Child sites list.
     * @param mixed $output Modal window content.
     */
    public static function render_bulk_add_modal( $dbwebsites, $output ) {
        // phpcs:disable WordPress.Security.EscapeOutput
        ?>
        <div id="mainwp-creating-new-user-modal" class="ui modal">
            <i class="close mainwp-reload icon"></i>
            <div class="header"><?php esc_html_e( 'New User', 'mainwp' ); ?></div>
            <div class="content">
                <div class="ui middle aligned divided list">
                    <?php foreach ( $dbwebsites as $website ) : ?>
                    <div class="item ui grid">
                        <span class="content"><a href="<?php echo esc_url( admin_url( 'admin.php?page=managesites&dashboard=' . $website->id ) ); ?>"><?php echo esc_html( stripslashes( $website->name ) ); ?></a></span>
                        <span class="right floated content"><?php echo isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ? '<i class="check green icon"></i> ' : '<span data-tooltip="' . esc_attr( $output->errors[ $website->id ] ) . '" data-position="left center" data-inverted=""><i class="times red icon"></i></span>'; ?></span>
                    </div>
                    <?php endforeach; ?>
                </div>
                <script type="text/javascript">
                    setTimeout(() => {
                        // Check if all items have green check icons (no red times icons)
                        let allSuccess = true;
                        jQuery('#mainwp-creating-new-user-modal .ui.list .item').each(function() {
                            // Check if this item has a red times icon
                            if (jQuery(this).find('.times.red.icon').length > 0) {
                                allSuccess = false;
                                return false; // Break the loop
                            }
                        });

                        // If all items are successful, redirect to success page
                        if (allSuccess) {
                            jQuery('#createuser')[0].reset();
                            location.href = 'admin.php?page=UserBulkAdd&success=1';
                        }
                    }, 3000);
                </script>
            </div>
        </div>
        <?php
        // phpcs:enable
    }

    /**
     * Renders Import Users Modal window.
     *
     * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
     * @uses \MainWP\Dashboard\MainWP_Utility::starts_with()
     */
    public static function render_bulk_upload() {  //phpcs:ignore -- NOSONAR - complex.
        static::render_header( 'Import' );
        // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        $errors = array();
        if ( isset( $_FILES['import_user_file_bulkupload']['error'] ) && UPLOAD_ERR_OK === $_FILES['import_user_file_bulkupload']['error'] ) {
            if ( isset( $_FILES['import_user_file_bulkupload']['tmp_name'] ) && is_uploaded_file( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ) {
                $tmp_path = isset( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ? sanitize_text_field( wp_unslash( $_FILES['import_user_file_bulkupload']['tmp_name'] ) ) : '';
                MainWP_System_Utility::get_wp_file_system();
                // phpcs:enable
                /**
                 * WordPress files system object.
                 *
                 * @global object
                 */
                global $wp_filesystem;

                $content = $wp_filesystem->get_contents( $tmp_path );
                $lines   = explode( "\r\n", $content );

                if ( is_array( $lines ) && ! empty( $lines ) ) {
                    $i = 0;
                    // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
                    if ( ! empty( $_POST['import_user_chk_header_first'] ) ) {
                        $header_line = trim( $lines[0] ) . "\n";
                        unset( $lines[0] );
                    }
                    // phpcs:enable

                    foreach ( $lines as $originalLine ) {

                        $line = trim( $originalLine );

                        if ( MainWP_Utility::starts_with( $line, '#' ) ) {
                            continue;
                        }

                        $items = str_getcsv( $line, ',' );

                        if ( 3 > count( $items ) ) {
                            continue;
                        }

                        $import_data = array(
                            'user_login'    => sanitize_text_field( wp_unslash( $items[0] ) ),
                            'email'         => sanitize_text_field( wp_unslash( $items[1] ) ),
                            'first_name'    => sanitize_text_field( wp_unslash( $items[2] ) ),
                            'last_name'     => sanitize_text_field( wp_unslash( $items[3] ) ),
                            'url'           => sanitize_text_field( wp_unslash( $items[4] ) ),
                            'pass1'         => sanitize_text_field( wp_unslash( $items[5] ) ),
                            'send_password' => intval( $items[6] ),
                            'role'          => sanitize_text_field( wp_unslash( strtolower( $items[7] ) ) ),
                            'select_sites'  => sanitize_text_field( wp_unslash( $items[8] ) ),
                            'select_groups' => sanitize_text_field( wp_unslash( $items[9] ) ),
                        );
                        $encoded     = wp_json_encode( $import_data );
                        ?>
                        <input type="hidden" id="user_import_csv_line_<?php echo intval( $i + 1 ); ?>" original-line="<?php echo esc_attr( sanitize_text_field( $line ) ); ?>" encoded-data="<?php echo esc_html( $encoded ); ?>" />
                        <?php
                        ++$i;
                    }
                    $header_line = trim( $header_line );
                    ?>
                    <div class="ui modal" id="mainwp-import-users-modal">
                    <i class="close icon"></i>
                        <div class="header"><?php esc_html_e( 'Importing new users and add them to your sites.', 'mainwp' ); ?></div>
                        <div class="scrolling content">
                            <?php
                            /**
                             * Action: mainwp_import_users_modal_top
                             *
                             * Fires on the top of the Import Users modal.
                             *
                             * @since 4.1
                             */
                            do_action( 'mainwp_import_users_modal_top' );
                            ?>
                            <div id="MainWPBulkUploadUserLoading" style="display: none;"><i class="ui active inline loader tiny"></i> <?php esc_html_e( 'Importing Users', 'mainwp' ); ?></div>
                            <input type="hidden" id="import_user_do_import" value="1"/>
                            <input type="hidden" id="import_user_total_import" value="<?php echo esc_attr( $i ); ?>"/>
                            <p>
                                <div class="import_user_import_listing" id="import_user_import_logging">
                                    <pre class="log"><?php echo esc_html( $header_line ) . "\n"; ?></pre>
                                </div>
                            </p>
                            <div id="import_user_import_failed_rows" style="display: none;">
                                <span><?php echo esc_html( $header_line ); ?></span>
                            </div>
                            <?php
                            /**
                             * Action: mainwp_import_users_modal_bottom
                             *
                             * Fires on the bottom of the Import Users modal.
                             *
                             * @since 4.1
                             */
                            do_action( 'mainwp_import_users_modal_bottom' );
                            ?>
                        </div>
                        <div class="actions">
                            <input type="button" name="import_user_btn_import" id="import_user_btn_import" class="ui basic button" value="<?php esc_attr_e( 'Pause', 'mainwp' ); ?>"/>
                            <input type="button" name="import_user_btn_save_csv" id="import_user_btn_save_csv" style="display:none;" class="ui basic green button" value="<?php esc_attr_e( 'Save failed', 'mainwp' ); ?>"/>
                        </div>
                    </div>
                        <script type="text/javascript">
                            jQuery( document ).ready( function () {
                                jQuery( "#mainwp-import-users-modal" ).modal( {
                                    closable: false,
                                    onHide: function() {
                                        mainwp_forceReload('admin.php?page=BulkImportUsers');
                                    }
                                } ).modal( 'show' );
                            } );
                        </script>

                    <?php

                } else {
                    $errors[] = esc_html__( 'Invalid data. Please make sure that the Import file has been formated properly.', 'mainwp' );
                }
            } else {
                $errors[] = esc_html__( 'File could not be uploaded. Temporary file cold not be created. Please make sure that the tmpfile() PHP function is enabled on your server.', 'mainwp' );
            }
        } else {
            $errors[] = esc_html__( 'File could not be uploaded. Please try again. If process keeps failing, please review MainWP Knowledgebase, and if you still have issues, please let us know in the MainWP Community.', 'mainwp' );
        }

        if ( ! empty( $errors ) ) {
            ?>
                <?php foreach ( $errors as $error ) { ?>
                <div class="ui error message"><?php echo esc_html( $error ); ?></div>
                <?php } ?>

            <a href="<?php echo esc_url( get_admin_url() ); ?>admin.php?page=UserBulkAdd" class="ui button"><?php esc_html_e( 'Add New', 'mainwp' ); ?></a>
            <a href="<?php echo esc_url( get_admin_url() ); ?>admin.php?page=mainwp_tab" class="ui button"><?php esc_html_e( 'Return to Overview', 'mainwp' ); ?></a>
            <?php
        }

        static::render_footer( 'Import' );
    }

    /**
     * User Import $_POST handler.
     *
     * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
     * @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_name()
     * @uses \MainWP\Dashboard\MainWP_DB::query()
     * @uses \MainWP\Dashboard\MainWP_DB::get_websites_by_url()
     * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_by_group_id()
     * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
     * @uses \MainWP\Dashboard\MainWP_DB::free_result()
     * @uses \MainWP\Dashboard\MainWP_Bulk_Add::get_class_name()
     * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
     */
    public static function do_import() { // phpcs:ignore -- NOSONAR -Current complexity is required to achieve desired results. Pull request solutions appreciated.

        // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
        $selected_sites  = ( isset( $_POST['select_sites'] ) && is_array( $_POST['select_sites'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['select_sites'] ) ) : array();
        $selected_groups = ( isset( $_POST['select_groups'] ) && is_array( $_POST['select_groups'] ) ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['select_groups'] ) ) : array();

        $data_fields   = MainWP_System_Utility::get_default_map_site_fields();
        $data_fields[] = 'users';

        $user_to_add = array(
            'user_pass'  => isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '',
            'user_login' => isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '',
            'user_url'   => isset( $_POST['url'] ) ? wp_unslash( $_POST['url'] ) : '',
            'user_email' => isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '',
            'first_name' => isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '',
            'last_name'  => isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '',
            'role'       => isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '',
        );

        $ret         = array();
        $dbwebsites  = array();
        $not_valid   = array();
        $error_sites = '';
        if ( isset( $_POST['select_by'] ) && 'site' === $_POST['select_by'] ) {
            foreach ( $selected_sites as $url ) {
                if ( ! empty( $url ) ) {
                    $website = MainWP_DB::instance()->get_websites_by_url( $url );
                    if ( ! empty( $website ) ) {
                        $website = current( $website );
                    }
                    if ( $website ) {
                        if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                            if ( '' !== $website->sync_errors ) {
                                $err_imp = esc_html__( 'Site disconnected:', 'mainwp' );
                            } else {
                                $err_imp = esc_html__( 'Site suspended:', 'mainwp' );
                            }
                            $not_valid[]  = $err_imp . ' ' . $website->url;
                            $error_sites .= $website->url . ';';
                            continue;
                        }
                        $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                            $website,
                            $data_fields
                        );
                    } else {
                        $not_valid[]  = esc_html__( 'Unexisting website. Please try again.', 'mainwp' ) . ' ' . $url;
                        $error_sites .= $url . ';';
                    }
                }
            }
        } else {
            foreach ( $selected_groups as $group ) {
                if ( MainWP_DB_Common::instance()->get_group_by_name( $group ) ) {
                    $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_name( $group ) );
                    if ( $websites ) {
                        while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
                            if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) {
                                if ( '' !== $website->sync_errors ) {
                                    $err_imp = esc_html__( 'Site disconnected:', 'mainwp' );
                                } else {
                                    $err_imp = esc_html__( 'Site suspended:', 'mainwp' );
                                }
                                $not_valid[]  = $err_imp . ' ' . $website->url;
                                $error_sites .= $website->url . ';';
                                continue;
                            }
                            $dbwebsites[ $website->id ] = MainWP_Utility::map_site(
                                $website,
                                $data_fields
                            );
                        }
                        MainWP_DB::free_result( $websites );
                    } else {
                        $not_valid[]  = esc_html__( 'No websites assigned to the selected group.', 'mainwp' ) . ' ' . $group;
                        $error_sites .= $group . ';';
                    }
                } else {
                    $not_valid[]  = esc_html__( 'Unexisting group selected. Please try again.', 'mainwp' ) . ' ' . $group;
                    $error_sites .= $group . ';';
                }
            }
        }

        if ( ! empty( $dbwebsites ) ) {
            $post_data      = array(
                'new_user'      => base64_encode( wp_json_encode( $user_to_add ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
                'send_password' => ( isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : '' ),
            );
            $output         = new \stdClass();
            $output->ok     = array();
            $output->errors = array();

            /**
            * Action: mainwp_before_user_create
            *
            * Fires before user create.
            *
            * @since 4.1
            */
            do_action( 'mainwp_before_user_create', $post_data, $dbwebsites );

            MainWP_Connect::fetch_urls_authed(
                $dbwebsites,
                'newuser',
                $post_data,
                array(
                    MainWP_Bulk_Add::get_class_name(),
                    'posting_bulk_handler',
                ),
                $output
            );

            /**
            * Action: mainwp_after_user_create
            *
            * Fires after user create.
            *
            * @since 4.1
            */
            do_action( 'mainwp_after_user_create', $output, $post_data, $dbwebsites );
        }

        $ret['ok_list']    = array();
        $ret['error_list'] = array();
        foreach ( $dbwebsites as $website ) {
            if ( isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ) {
                $ret['ok_list'][] = 'New user(s) created: ' . esc_html( stripslashes( $website->name ) );
            } else {
                $ret['error_list'][] = esc_html( $output->errors[ $website->id ] . ' ' . stripslashes( $website->name ) );
                $error_sites        .= $website->url . ';';
            }
        }

        foreach ( $not_valid as $val ) {
            $ret['error_list'][] = $val;
        }

        $ret['failed_logging'] = '';
        if ( ! empty( $error_sites ) ) {
            $error_sites = rtrim( $error_sites, ';' );

            $user_login    = isset( $_POST['user_login'] ) ? sanitize_text_field( wp_unslash( $_POST['user_login'] ) ) : '';
            $email         = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
            $first_name    = isset( $_POST['first_name'] ) ? sanitize_text_field( wp_unslash( $_POST['first_name'] ) ) : '';
            $last_name     = isset( $_POST['last_name'] ) ? sanitize_text_field( wp_unslash( $_POST['last_name'] ) ) : '';
            $url           = isset( $_POST['url'] ) ? sanitize_text_field( wp_unslash( $_POST['url'] ) ) : '';
            $pass1         = isset( $_POST['pass1'] ) ? wp_unslash( $_POST['pass1'] ) : '';
            $send_password = isset( $_POST['send_password'] ) ? intval( $_POST['send_password'] ) : 0;
            $role          = isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '';

            $ret['failed_logging'] = esc_html( $user_login . ',' . $email . ',' . $first_name . ',' . $last_name . ',' . $url . ',' . $pass1 . ',' . $send_password . ',' . $role . ',' . $error_sites . ',' );
        }
        $ret['line_number'] = isset( $_POST['line_number'] ) ? intval( $_POST['line_number'] ) : 0;
        // phpcs:enable
        die( wp_json_encode( $ret ) );
    }

    /**
     * Method mainwp_help_content()
     *
     * Creates the MainWP Help Documentation List for the help component in the sidebar.
     */
    public static function mainwp_help_content() {
        if ( isset( $_GET['page'] ) && ( 'UserBulkManage' === $_GET['page'] || 'UserBulkAdd' === $_GET['page'] || 'UpdateAdminPasswords' === $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
            ?>
            <p><?php esc_html_e( 'If you need help with managing users, please review following help documents', 'mainwp' ); ?></p>
            <div class="ui list">
                <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/sites/users/manage-users" target="_blank">Manage Users</a></div>
                <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/sites/users/manage-users#create-a-new-user" target="_blank">Create a New User</a></div>
                <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/sites/users/manage-users#edit-existing-users" target="_blank">Edit an Existing User</a></div>
                <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/sites/users/manage-users#bulk-update-admin-passwords" target="_blank">Bulk Update Administrator Passwords</a></div>
                <div class="item"><i class="external alternate icon"></i> <a href="https://docs.mainwp.com/sites/users/manage-users#delete-users" target="_blank">Delete User(s)</a></div>
                <?php
                /**
                 * Action: mainwp_users_help_item
                 *
                 * Fires at the bottom of the help articles list in the Help sidebar on the Users page.
                 *
                 * Suggested HTML markup:
                 *
                 * <div class="item"><a href="Your custom URL">Your custom text</a></div>
                 *
                 * @since 4.1
                 */
                do_action( 'mainwp_users_help_item' );
                ?>
            </div>
            <?php
        }
    }
}
