Opened 10 years ago
Last modified 21 months ago
#39295 new enhancement
Prevent infinite loop when calling get_user_locale() in a 'locale' filter
| Reported by: | yoavf | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | I18N | Version: | 4.7 |
| Severity: | major | Keywords: | has-patch 2nd-opinion |
| Cc: | Focuses: |
Description
Using get_user_locale() in a locale filter (for get_locale()) might sound like a good idea, for example to change the site locale depending on our user.
<?php if ( $condition) { add_filter( 'locale', 'get_user_locale' ); }
However, if one does that and a non-logged in user (or a user without a locale value) visits the site, a infinite loop will be triggered, since get_user_locale() will itself call get_locale() when no user->locale value exists.
Since this only affects logged out users, it feels like an easy trap to miss, and we should prevent that.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
39295.2.diff is a refresh that follows the approach of 39295.2.diff with some improvements:
get_locale().39295.2.alt.diff is an alternative patch that just makes
add_filter( 'locale', 'get_user_locale' )work as expected, without the_doing_it_wrong()message, by using the existing$localeglobal as a fallback to avoid the infinite loop. Perhaps that could be the preferred fix here?