rest_cookie_check_errors

The timeline below displays how wordpress function rest_cookie_check_errors has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 3.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 6.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 2.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 6.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 1.4

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 6.1

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 9.8

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 5.9

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 8.8

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 5.8

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie check failed'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 7.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .10

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 6.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .12

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 5.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .13

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 4.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .14

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 5.4

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 3.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .16

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 2.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .20

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 2.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .19

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 1.2

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .17

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 0.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .20

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 9.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .24

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 4.9

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    rest_get_server()->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 6.3

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 * @global WP_REST_Server $wp_rest_server      REST server instance.
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie, $wp_rest_server;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    $wp_rest_server->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: .27

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 * @global WP_REST_Server $wp_rest_server      REST server instance.
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie, $wp_rest_server;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    $wp_rest_server->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 4.6

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed          $wp_rest_auth_cookie
 * @global WP_REST_Server $wp_rest_server      REST server instance.
 *
 * @param WP_Error|mixed $result Error from another authentication handler,
 *                               null if we should handle it, or another value
 *                               if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie, $wp_rest_server;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    // Send a refreshed nonce in header.
    $wp_rest_server->send_header('X-WP-Nonce', wp_create_nonce('wp_rest'));
    return true;
}

WordPress Version: 5.4

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
 *                               or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    return true;
}

WordPress Version: .30

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
 *                               or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    return true;
}

WordPress Version: 4.4

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
 *                               or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    return true;
}

WordPress Version: .31

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
 *                               or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        add_filter('rest_send_nocache_headers', '__return_true', 20);
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    return true;
}

WordPress Version: 4.4

/**
 * Checks for errors when using cookie-based authentication.
 *
 * WordPress' built-in cookie authentication is always active
 * for logged in users. However, the API has to check nonces
 * for each request to ensure users are not vulnerable to CSRF.
 *
 * @since 4.4.0
 *
 * @global mixed $wp_rest_auth_cookie
 *
 * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it,
 *                               or another value if not.
 * @return WP_Error|mixed|bool WP_Error if the cookie is invalid, the $result, otherwise true.
 */
function rest_cookie_check_errors($result)
{
    if (!empty($result)) {
        return $result;
    }
    global $wp_rest_auth_cookie;
    /*
     * Is cookie authentication being used? (If we get an auth
     * error, but we're still logged in, another authentication
     * must have been used).
     */
    if (true !== $wp_rest_auth_cookie && is_user_logged_in()) {
        return $result;
    }
    // Determine if there is a nonce.
    $nonce = null;
    if (isset($_REQUEST['_wpnonce'])) {
        $nonce = $_REQUEST['_wpnonce'];
    } elseif (isset($_SERVER['HTTP_X_WP_NONCE'])) {
        $nonce = $_SERVER['HTTP_X_WP_NONCE'];
    }
    if (null === $nonce) {
        // No nonce at all, so act as if it's an unauthenticated request.
        wp_set_current_user(0);
        return true;
    }
    // Check the nonce.
    $result = wp_verify_nonce($nonce, 'wp_rest');
    if (!$result) {
        return new WP_Error('rest_cookie_invalid_nonce', __('Cookie nonce is invalid'), array('status' => 403));
    }
    return true;
}