Skip to content

Commit

Permalink
No redirect or cookies on GraphQL requests
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey authored Aug 7, 2022
1 parent 784556f commit f9556d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions qtranslate_core.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ function qtranxf_detect_language( &$url_info ) {

$url_info['language'] = $lang;

// REST calls should be deterministic (stateless), no special language detection e.g. based on cookie
$url_info['set_cookie'] = ! wp_doing_ajax() && ! qtranxf_is_rest_request_expected();
// REST and GraphQL API calls should be deterministic (stateless), no special language detection e.g. based on cookie
$url_info['set_cookie'] = ! wp_doing_ajax() && ! qtranxf_is_rest_request_expected() && ! qtranxf_is_graphql_request_expected();

/**
* Hook for possible other methods
Expand Down Expand Up @@ -435,6 +435,7 @@ function qtranxf_detect_language_front( &$url_info ) {

if ( ! isset( $url_info['doredirect'] )
&& ( ! qtranxf_is_rest_request_expected() ) // fallback case where language can be read from cookie with REST
&& ( ! qtranxf_is_graphql_request_expected() )
&& ( ! $q_config['hide_default_language'] || $lang != $q_config['default_language'] )
&& ( ! qtranxf_language_neutral_path( $url_info['wp-path'] ) )
) {
Expand Down
11 changes: 11 additions & 0 deletions qtranslate_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,16 @@ function qtranxf_is_rest_request_expected() {
return stripos( $_SERVER['REQUEST_URI'], '/' . rest_get_url_prefix() . '/' ) !== false;
}

/**
* Evaluate if the request URI leads to a GraphQL API call.
*
* @return bool
* @see is_graphql_http_request in https://github.com/wp-graphql/wp-graphql/blob/develop/src/Router.php
*/
function qtranxf_is_graphql_request_expected() {
return function_exists( 'is_graphql_http_request' ) && is_graphql_http_request();
}

/**
* Evaluate if the current request allows HTTP redirection.
* Admin requests (WP_ADMIN, DOING_AJAX, WP_CLI, DOING_CRON) or REST calls should not be redirected.
Expand All @@ -584,6 +594,7 @@ function qtranxf_is_rest_request_expected() {
function qtranxf_can_redirect() {
return ! is_admin() && ! wp_doing_ajax() && ! ( defined( 'WP_CLI' ) && WP_CLI ) && ! wp_doing_cron() && empty( $_POST )
&& ( ! qtranxf_is_rest_request_expected() )
&& ( ! qtranxf_is_graphql_request_expected() )
// TODO clarify: 'REDIRECT_*' needs more testing --> && !isset($_SERVER['REDIRECT_URL'])
&& ( ! isset( $_SERVER['REDIRECT_STATUS'] ) || $_SERVER['REDIRECT_STATUS'] == '200' );
}
Expand Down

0 comments on commit f9556d5

Please sign in to comment.