get_error_code() ) { return $result; } if ( ! self::isPublicRoute( self::currentRoute() ) ) { return $result; } wp_set_current_user( 0 ); return true; } /** * Whether a REST route is one visitors call. * * @param string $route REST route, for example /subtleforms/v1/submit. */ public static function isPublicRoute( string $route ): bool { if ( 1 === preg_match( self::PATTERN, $route ) ) { return true; } /** * More REST routes visitors call while filling in a form, for * example an add-on's own public routes. Exact routes such as * `/subtleforms/v1/example`. * * @param string[] $routes Routes. * @since 2.7.2 */ $extra = function_exists( 'apply_filters' ) ? (array) apply_filters( 'subtleforms/rest/public_routes', array() ) : array(); $route = rtrim( $route, '/' ); foreach ( $extra as $candidate ) { if ( is_string( $candidate ) && '' !== $candidate && rtrim( $candidate, '/' ) === $route ) { return true; } } return false; } /** * The REST route of the current request. */ private static function currentRoute(): string { global $wp; if ( isset( $wp->query_vars['rest_route'] ) && is_string( $wp->query_vars['rest_route'] ) ) { return '/' . ltrim( $wp->query_vars['rest_route'], '/' ); } $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['REQUEST_URI'] ) ) : ''; return self::routeFromUri( $uri ); } /** * Extract the REST route from a raw REQUEST_URI value. * * Sanitizes then parses the path; used when `$wp->query_vars['rest_route']` * is not set. Empty string when the URI is not under the REST prefix. * * @param string $uri Raw request URI (may be slash-magic encoded). */ public static function routeFromUri( string $uri ): string { $uri = sanitize_text_field( wp_unslash( $uri ) ); $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); if ( '' === $path ) { return ''; } $prefix = '/' . trim( rest_get_url_prefix(), '/' ); $pos = strpos( $path, $prefix . '/' ); return false === $pos ? '' : substr( $path, $pos + strlen( $prefix ) ); } }