author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Creates common globals for the rest of WordPress |
|
4 |
* |
|
19 | 5 |
* Sets $pagenow global which is the filename of the current screen. |
6 |
* Checks for the browser to set which one is currently being used. |
|
0 | 7 |
* |
8 |
* Detects which user environment WordPress is being used on. |
|
5 | 9 |
* Only attempts to check for Apache, Nginx and IIS -- three web |
10 |
* servers with known pretty permalink capability. |
|
11 |
* |
|
12 |
* Note: Though Nginx is detected, WordPress does not currently |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
13 |
* generate rewrite rules for it. See https://developer.wordpress.org/advanced-administration/server/web-server/nginx/ |
0 | 14 |
* |
15 |
* @package WordPress |
|
16 |
*/ |
|
17 |
||
18 |
global $pagenow, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
$is_lynx, $is_gecko, $is_winIE, $is_macIE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone, $is_IE, $is_edge, |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
$is_apache, $is_IIS, $is_iis7, $is_nginx, $is_caddy; |
0 | 21 |
|
16 | 22 |
// On which page are we? |
0 | 23 |
if ( is_admin() ) { |
16 | 24 |
// wp-admin pages are checked more carefully. |
9 | 25 |
if ( is_network_admin() ) { |
26 |
preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches ); |
|
27 |
} elseif ( is_user_admin() ) { |
|
28 |
preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches ); |
|
29 |
} else { |
|
30 |
preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches ); |
|
31 |
} |
|
19 | 32 |
|
33 |
$pagenow = ! empty( $self_matches[1] ) ? $self_matches[1] : ''; |
|
9 | 34 |
$pagenow = trim( $pagenow, '/' ); |
35 |
$pagenow = preg_replace( '#\?.*?$#', '', $pagenow ); |
|
19 | 36 |
|
0 | 37 |
if ( '' === $pagenow || 'index' === $pagenow || 'index.php' === $pagenow ) { |
38 |
$pagenow = 'index.php'; |
|
39 |
} else { |
|
9 | 40 |
preg_match( '#(.*?)(/|$)#', $pagenow, $self_matches ); |
41 |
$pagenow = strtolower( $self_matches[1] ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
42 |
if ( ! str_ends_with( $pagenow, '.php' ) ) { |
16 | 43 |
$pagenow .= '.php'; // For `Options +Multiviews`: /wp-admin/themes/index.php (themes.php is queried). |
9 | 44 |
} |
0 | 45 |
} |
46 |
} else { |
|
9 | 47 |
if ( preg_match( '#([^/]+\.php)([?/].*?)?$#i', $_SERVER['PHP_SELF'], $self_matches ) ) { |
48 |
$pagenow = strtolower( $self_matches[1] ); |
|
49 |
} else { |
|
0 | 50 |
$pagenow = 'index.php'; |
9 | 51 |
} |
0 | 52 |
} |
9 | 53 |
unset( $self_matches ); |
0 | 54 |
|
16 | 55 |
// Simple browser detection. |
56 |
$is_lynx = false; |
|
57 |
$is_gecko = false; |
|
58 |
$is_winIE = false; |
|
59 |
$is_macIE = false; |
|
60 |
$is_opera = false; |
|
61 |
$is_NS4 = false; |
|
62 |
$is_safari = false; |
|
63 |
$is_chrome = false; |
|
64 |
$is_iphone = false; |
|
65 |
$is_edge = false; |
|
0 | 66 |
|
9 | 67 |
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
68 |
if ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Lynx' ) ) { |
0 | 69 |
$is_lynx = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
70 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Edg' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
$is_edge = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'OPR/' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
73 |
$is_opera = true; |
9 | 74 |
} elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chrome' ) !== false ) { |
0 | 75 |
if ( stripos( $_SERVER['HTTP_USER_AGENT'], 'chromeframe' ) !== false ) { |
76 |
$is_admin = is_admin(); |
|
77 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* Filters whether Google Chrome Frame should be used, if available. |
0 | 79 |
* |
80 |
* @since 3.2.0 |
|
81 |
* |
|
82 |
* @param bool $is_admin Whether to use the Google Chrome Frame. Default is the value of is_admin(). |
|
83 |
*/ |
|
16 | 84 |
$is_chrome = apply_filters( 'use_google_chrome_frame', $is_admin ); |
85 |
if ( $is_chrome ) { |
|
0 | 86 |
header( 'X-UA-Compatible: chrome=1' ); |
9 | 87 |
} |
0 | 88 |
$is_winIE = ! $is_chrome; |
89 |
} else { |
|
90 |
$is_chrome = true; |
|
91 |
} |
|
9 | 92 |
} elseif ( stripos( $_SERVER['HTTP_USER_AGENT'], 'safari' ) !== false ) { |
0 | 93 |
$is_safari = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
} elseif ( ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) || str_contains( $_SERVER['HTTP_USER_AGENT'], 'Trident' ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'Win' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
) { |
0 | 97 |
$is_winIE = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) && str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mac' ) ) { |
0 | 99 |
$is_macIE = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Gecko' ) ) { |
0 | 101 |
$is_gecko = true; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Nav' ) && str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.' ) ) { |
0 | 103 |
$is_NS4 = true; |
104 |
} |
|
105 |
} |
|
106 |
||
9 | 107 |
if ( $is_safari && stripos( $_SERVER['HTTP_USER_AGENT'], 'mobile' ) !== false ) { |
0 | 108 |
$is_iphone = true; |
9 | 109 |
} |
0 | 110 |
|
111 |
$is_IE = ( $is_macIE || $is_winIE ); |
|
112 |
||
16 | 113 |
// Server detection. |
0 | 114 |
|
115 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
* Whether the server software is Apache or something else. |
9 | 117 |
* |
0 | 118 |
* @global bool $is_apache |
119 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
120 |
$is_apache = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) ); |
0 | 121 |
|
122 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
123 |
* Whether the server software is Nginx or something else. |
9 | 124 |
* |
0 | 125 |
* @global bool $is_nginx |
126 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
127 |
$is_nginx = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'nginx' ) ); |
0 | 128 |
|
129 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
130 |
* Whether the server software is Caddy / FrankenPHP or something else. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
131 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
132 |
* @global bool $is_caddy |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
133 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
$is_caddy = ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Caddy' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'FrankenPHP' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
* Whether the server software is IIS or something else. |
9 | 138 |
* |
0 | 139 |
* @global bool $is_IIS |
140 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
$is_IIS = ! $is_apache && ( str_contains( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) || str_contains( $_SERVER['SERVER_SOFTWARE'], 'ExpressionDevServer' ) ); |
0 | 142 |
|
143 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
* Whether the server software is IIS 7.X or greater. |
9 | 145 |
* |
0 | 146 |
* @global bool $is_iis7 |
147 |
*/ |
|
18 | 148 |
$is_iis7 = $is_IIS && (int) substr( $_SERVER['SERVER_SOFTWARE'], strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/' ) + 14 ) >= 7; |
0 | 149 |
|
150 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
* Test if the current browser runs on a mobile device (smart phone, tablet, etc.). |
0 | 152 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @since 3.4.0 |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
* @since 6.4.0 Added checking for the Sec-CH-UA-Mobile request header. |
9 | 155 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* @return bool |
0 | 157 |
*/ |
158 |
function wp_is_mobile() { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
if ( isset( $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
// This is the `Sec-CH-UA-Mobile` user agent client hint HTTP request header. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
// See <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Mobile>. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
$is_mobile = ( '?1' === $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
} elseif ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
0 | 164 |
$is_mobile = false; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
} elseif ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) // Many mobile devices (all iPhone, iPad, etc.) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
171 |
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) { |
0 | 172 |
$is_mobile = true; |
173 |
} else { |
|
174 |
$is_mobile = false; |
|
175 |
} |
|
176 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* Filters whether the request should be treated as coming from a mobile device or not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* @param bool $is_mobile Whether the request is from a mobile device or not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
return apply_filters( 'wp_is_mobile', $is_mobile ); |
0 | 185 |
} |