General: Account for Sec-CH-UA-Mobile client hint request header in wp_is_mobile().

Add missing test coverage for `wp_is_mobile()`.

Fixes #59370.
Props westonruter, flixos90.


git-svn-id: https://develop.svn.wordpress.org/trunk@56638 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2023-09-20 20:49:12 +00:00
parent 5b79a4cd49
commit e40a1180cb
2 changed files with 153 additions and 1 deletions

View File

@@ -144,11 +144,16 @@ $is_iis7 = $is_IIS && (int) substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVE
* Test if the current browser runs on a mobile device (smart phone, tablet, etc.)
*
* @since 3.4.0
* @since 6.4.0 Added checking for the Sec-CH-UA-Mobile request header.
*
* @return bool
*/
function wp_is_mobile() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) {
// This is the `Sec-CH-UA-Mobile` user agent client hint HTTP request header.
// See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile>.
$is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] );
} elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
$is_mobile = false;
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.)
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )