/**
 * Check Rublon protection for displayed users.
 *
 * The function is executed during any query to the users
 * table. It retrieves the user list that is to be
 * displayed on the admin's user list page and checks the
 * status of the Rublon protection for each user, then
 * stores the result in the prerender data for further use.
 *
 * @param WP_User_Query $query Current "users" query
 */
function rublon2factor_pre_user_query(&$query)
{
    global $pagenow;
    // Check if plugin is active and we're on the user list page
    if (RublonHelper::isSiteRegistered() && $pagenow == 'users.php') {
        $query_vars = $query->query_vars;
        // Check whether the query was manually executed by the plugin
        // in order to avoid an infinite loop.
        if (empty($query_vars['exclude']) || !in_array('rublon_user_query', $query_vars['exclude'])) {
            // This is not the plugin's query - prepare one.
            array_push($query_vars['exclude'], 'rublon_user_query');
            $users_query = new WP_User_Query($query_vars);
            $users = $users_query->get_results();
            if (!empty($users)) {
                $check = null;
                // Prepare the CheckProtection request via Rublon API.
                require_once dirname(__FILE__) . '/libs/RublonImplemented/RublonAPICheckProtection.php';
                $rublon_mobile_users = array();
                $requested_users = array();
                foreach ($users as $user) {
                    if ($user instanceof WP_User) {
                        $user_id = RublonHelper::getUserId($user);
                        $mobile_user_status = RublonHelper::getMobileUserStatus($user);
                        if ($mobile_user_status === false) {
                            array_push($requested_users, $user);
                            if (empty($check)) {
                                $check = new RublonAPICheckProtection(RublonHelper::getRublon(), $user_id, RublonHelper::getUserEmail($user));
                            } else {
                                $check->appendUser($user_id, RublonHelper::getUserEmail($user));
                            }
                        } else {
                            $rublon_mobile_users[$user_id] = $mobile_user_status;
                        }
                    }
                }
                $user_id = null;
                if (!empty($requested_users)) {
                    try {
                        $check->perform();
                    } catch (RublonException $e) {
                        // Assume no info available.
                        $check = null;
                    }
                }
                foreach ($requested_users as $user) {
                    $user_id = RublonHelper::getUserId($user);
                    if (!empty($check) && $check->isProtectionEnabled($user_id)) {
                        $rublon_mobile_users[$user_id] = true;
                        RublonHelper::setMobileUserStatus($user, RublonHelper::YES);
                    } else {
                        RublonHelper::setMobileUserStatus($user, RublonHelper::NO);
                    }
                }
                // Store the request's result in the prerender data.
                RublonHelper::setPrerenderData(RublonHelper::PRERENDER_KEY_MOBILE_USERS, $rublon_mobile_users);
            }
        }
    }
}