author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress environment setup class. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @since 2.0.0 |
|
7 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
8 |
#[AllowDynamicProperties] |
0 | 9 |
class WP { |
10 |
/** |
|
11 |
* Public query variables. |
|
12 |
* |
|
13 |
* Long list of public query variables. |
|
14 |
* |
|
15 |
* @since 2.0.0 |
|
9 | 16 |
* @var string[] |
0 | 17 |
*/ |
16 | 18 |
public $public_query_vars = array( 'm', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 's', 'search', 'exact', 'sentence', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'pagename', 'page_id', 'error', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'favicon', 'taxonomy', 'term', 'cpage', 'post_type', 'embed' ); |
0 | 19 |
|
20 |
/** |
|
21 |
* Private query variables. |
|
22 |
* |
|
23 |
* Long list of private query variables. |
|
24 |
* |
|
25 |
* @since 2.0.0 |
|
9 | 26 |
* @var string[] |
0 | 27 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
public $private_query_vars = array( 'offset', 'posts_per_page', 'posts_per_archive_page', 'showposts', 'nopaging', 'post_type', 'post_status', 'category__in', 'category__not_in', 'category__and', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'tag_id', 'post_mime_type', 'perm', 'comments_per_page', 'post__in', 'post__not_in', 'post_parent', 'post_parent__in', 'post_parent__not_in', 'title', 'fields' ); |
0 | 29 |
|
30 |
/** |
|
31 |
* Extra query variables set by the user. |
|
32 |
* |
|
33 |
* @since 2.1.0 |
|
34 |
* @var array |
|
35 |
*/ |
|
5 | 36 |
public $extra_query_vars = array(); |
0 | 37 |
|
38 |
/** |
|
39 |
* Query variables for setting up the WordPress Query Loop. |
|
40 |
* |
|
41 |
* @since 2.0.0 |
|
42 |
* @var array |
|
43 |
*/ |
|
19 | 44 |
public $query_vars = array(); |
0 | 45 |
|
46 |
/** |
|
47 |
* String parsed to set the query variables. |
|
48 |
* |
|
49 |
* @since 2.0.0 |
|
50 |
* @var string |
|
51 |
*/ |
|
19 | 52 |
public $query_string = ''; |
0 | 53 |
|
54 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* The request path, e.g. 2015/05/06. |
0 | 56 |
* |
57 |
* @since 2.0.0 |
|
58 |
* @var string |
|
59 |
*/ |
|
19 | 60 |
public $request = ''; |
0 | 61 |
|
62 |
/** |
|
63 |
* Rewrite rule the request matched. |
|
64 |
* |
|
65 |
* @since 2.0.0 |
|
66 |
* @var string |
|
67 |
*/ |
|
19 | 68 |
public $matched_rule = ''; |
0 | 69 |
|
70 |
/** |
|
71 |
* Rewrite query the request matched. |
|
72 |
* |
|
73 |
* @since 2.0.0 |
|
74 |
* @var string |
|
75 |
*/ |
|
19 | 76 |
public $matched_query = ''; |
0 | 77 |
|
78 |
/** |
|
79 |
* Whether already did the permalink. |
|
80 |
* |
|
81 |
* @since 2.0.0 |
|
82 |
* @var bool |
|
83 |
*/ |
|
5 | 84 |
public $did_permalink = false; |
0 | 85 |
|
86 |
/** |
|
18 | 87 |
* Adds a query variable to the list of public query variables. |
0 | 88 |
* |
89 |
* @since 2.1.0 |
|
90 |
* |
|
91 |
* @param string $qv Query variable name. |
|
92 |
*/ |
|
9 | 93 |
public function add_query_var( $qv ) { |
16 | 94 |
if ( ! in_array( $qv, $this->public_query_vars, true ) ) { |
0 | 95 |
$this->public_query_vars[] = $qv; |
9 | 96 |
} |
0 | 97 |
} |
98 |
||
99 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
* Removes a query variable from a list of public query variables. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* @param string $name Query variable name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
public function remove_query_var( $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
$this->public_query_vars = array_diff( $this->public_query_vars, array( $name ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
/** |
18 | 111 |
* Sets the value of a query variable. |
0 | 112 |
* |
113 |
* @since 2.3.0 |
|
114 |
* |
|
16 | 115 |
* @param string $key Query variable name. |
116 |
* @param mixed $value Query variable value. |
|
0 | 117 |
*/ |
9 | 118 |
public function set_query_var( $key, $value ) { |
119 |
$this->query_vars[ $key ] = $value; |
|
0 | 120 |
} |
121 |
||
122 |
/** |
|
18 | 123 |
* Parses the request to find the correct WordPress query. |
0 | 124 |
* |
125 |
* Sets up the query variables based on the request. There are also many |
|
126 |
* filters and actions that can be used to further manipulate the result. |
|
127 |
* |
|
128 |
* @since 2.0.0 |
|
19 | 129 |
* @since 6.0.0 A return value was added. |
0 | 130 |
* |
16 | 131 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* |
0 | 133 |
* @param array|string $extra_query_vars Set the extra query variables. |
19 | 134 |
* @return bool Whether the request was parsed. |
0 | 135 |
*/ |
9 | 136 |
public function parse_request( $extra_query_vars = '' ) { |
0 | 137 |
global $wp_rewrite; |
138 |
||
5 | 139 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* Filters whether to parse the request. |
5 | 141 |
* |
142 |
* @since 3.5.0 |
|
143 |
* |
|
144 |
* @param bool $bool Whether or not to parse the request. Default true. |
|
19 | 145 |
* @param WP $wp Current WordPress environment instance. |
5 | 146 |
* @param array|string $extra_query_vars Extra passed query variables. |
147 |
*/ |
|
9 | 148 |
if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) { |
19 | 149 |
return false; |
9 | 150 |
} |
0 | 151 |
|
9 | 152 |
$this->query_vars = array(); |
0 | 153 |
$post_type_query_vars = array(); |
154 |
||
5 | 155 |
if ( is_array( $extra_query_vars ) ) { |
0 | 156 |
$this->extra_query_vars = & $extra_query_vars; |
5 | 157 |
} elseif ( ! empty( $extra_query_vars ) ) { |
158 |
parse_str( $extra_query_vars, $this->extra_query_vars ); |
|
159 |
} |
|
0 | 160 |
// Process PATH_INFO, REQUEST_URI, and 404 for permalinks. |
161 |
||
162 |
// Fetch the rewrite rules. |
|
163 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
|
164 |
||
9 | 165 |
if ( ! empty( $rewrite ) ) { |
0 | 166 |
// If we match a rewrite rule, this will be cleared. |
9 | 167 |
$error = '404'; |
0 | 168 |
$this->did_permalink = true; |
169 |
||
9 | 170 |
$pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; |
0 | 171 |
list( $pathinfo ) = explode( '?', $pathinfo ); |
9 | 172 |
$pathinfo = str_replace( '%', '%25', $pathinfo ); |
0 | 173 |
|
174 |
list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] ); |
|
9 | 175 |
$self = $_SERVER['PHP_SELF']; |
19 | 176 |
|
177 |
$home_path = parse_url( home_url(), PHP_URL_PATH ); |
|
178 |
$home_path_regex = ''; |
|
179 |
if ( is_string( $home_path ) && '' !== $home_path ) { |
|
180 |
$home_path = trim( $home_path, '/' ); |
|
181 |
$home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); |
|
182 |
} |
|
0 | 183 |
|
16 | 184 |
/* |
185 |
* Trim path info from the end and the leading home path from the front. |
|
186 |
* For path info requests, this leaves us with the requesting filename, if any. |
|
187 |
* For 404 requests, this leaves us with the requested permalink. |
|
188 |
*/ |
|
9 | 189 |
$req_uri = str_replace( $pathinfo, '', $req_uri ); |
190 |
$req_uri = trim( $req_uri, '/' ); |
|
191 |
$pathinfo = trim( $pathinfo, '/' ); |
|
192 |
$self = trim( $self, '/' ); |
|
19 | 193 |
|
194 |
if ( ! empty( $home_path_regex ) ) { |
|
195 |
$req_uri = preg_replace( $home_path_regex, '', $req_uri ); |
|
196 |
$req_uri = trim( $req_uri, '/' ); |
|
197 |
$pathinfo = preg_replace( $home_path_regex, '', $pathinfo ); |
|
198 |
$pathinfo = trim( $pathinfo, '/' ); |
|
199 |
$self = preg_replace( $home_path_regex, '', $self ); |
|
200 |
$self = trim( $self, '/' ); |
|
201 |
} |
|
0 | 202 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
203 |
// The requested permalink is in $pathinfo for path info requests and $req_uri for other requests. |
9 | 204 |
if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
$requested_path = $pathinfo; |
0 | 206 |
} else { |
207 |
// If the request uri is the index, blank it out so that we don't try to match it against a rule. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
208 |
if ( $req_uri === $wp_rewrite->index ) { |
0 | 209 |
$req_uri = ''; |
9 | 210 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
211 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
$requested_path = $req_uri; |
0 | 213 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
214 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
$requested_file = $req_uri; |
0 | 216 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
$this->request = $requested_path; |
0 | 218 |
|
219 |
// Look for matches. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
$request_match = $requested_path; |
0 | 221 |
if ( empty( $request_match ) ) { |
16 | 222 |
// An empty request could only match against ^$ regex. |
0 | 223 |
if ( isset( $rewrite['$'] ) ) { |
224 |
$this->matched_rule = '$'; |
|
9 | 225 |
$query = $rewrite['$']; |
226 |
$matches = array( '' ); |
|
0 | 227 |
} |
228 |
} else { |
|
229 |
foreach ( (array) $rewrite as $match => $query ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
// If the requested file is the anchor of the match, prepend it to the path info. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
231 |
if ( ! empty( $requested_file ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
232 |
&& str_starts_with( $match, $requested_file ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
233 |
&& $requested_file !== $requested_path |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
234 |
) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
$request_match = $requested_file . '/' . $requested_path; |
9 | 236 |
} |
0 | 237 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
238 |
if ( preg_match( "#^$match#", $request_match, $matches ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
239 |
|| preg_match( "#^$match#", urldecode( $request_match ), $matches ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
240 |
) { |
0 | 241 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
242 |
if ( $wp_rewrite->use_verbose_page_rules |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
243 |
&& preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
244 |
) { |
5 | 245 |
// This is a verbose page match, let's check to be sure about it. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
$page = get_page_by_path( $matches[ $varmatch[1] ] ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
247 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
if ( ! $page ) { |
9 | 249 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
$post_status_obj = get_post_status_object( $page->post_status ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
253 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
if ( ! $post_status_obj->public && ! $post_status_obj->protected |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
255 |
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
256 |
) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
} |
0 | 259 |
} |
260 |
||
261 |
// Got a match. |
|
262 |
$this->matched_rule = $match; |
|
263 |
break; |
|
264 |
} |
|
265 |
} |
|
266 |
} |
|
267 |
||
19 | 268 |
if ( ! empty( $this->matched_rule ) ) { |
0 | 269 |
// Trim the query of everything up to the '?'. |
9 | 270 |
$query = preg_replace( '!^.+\?!', '', $query ); |
0 | 271 |
|
272 |
// Substitute the substring matches into the query. |
|
9 | 273 |
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
0 | 274 |
|
275 |
$this->matched_query = $query; |
|
276 |
||
277 |
// Parse the query. |
|
9 | 278 |
parse_str( $query, $perma_query_vars ); |
0 | 279 |
|
280 |
// If we're processing a 404 request, clear the error var since we found something. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
281 |
if ( '404' === $error ) { |
0 | 282 |
unset( $error, $_GET['error'] ); |
9 | 283 |
} |
0 | 284 |
} |
285 |
||
286 |
// If req_uri is empty or if it is a request for ourself, unset error. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
287 |
if ( empty( $requested_path ) || $requested_file === $self |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
288 |
|| str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
289 |
) { |
0 | 290 |
unset( $error, $_GET['error'] ); |
291 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
292 |
if ( isset( $perma_query_vars ) && str_contains( $_SERVER['PHP_SELF'], 'wp-admin/' ) ) { |
0 | 293 |
unset( $perma_query_vars ); |
9 | 294 |
} |
0 | 295 |
|
296 |
$this->did_permalink = false; |
|
297 |
} |
|
298 |
} |
|
299 |
||
5 | 300 |
/** |
16 | 301 |
* Filters the query variables allowed before processing. |
5 | 302 |
* |
303 |
* Allows (publicly allowed) query vars to be added, removed, or changed prior |
|
304 |
* to executing the query. Needed to allow custom rewrite rules using your own arguments |
|
305 |
* to work, or any other custom query variables you want to be publicly available. |
|
306 |
* |
|
307 |
* @since 1.5.0 |
|
308 |
* |
|
16 | 309 |
* @param string[] $public_query_vars The array of allowed query variable names. |
5 | 310 |
*/ |
311 |
$this->public_query_vars = apply_filters( 'query_vars', $this->public_query_vars ); |
|
0 | 312 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
if ( is_post_type_viewable( $t ) && $t->query_var ) { |
9 | 315 |
$post_type_query_vars[ $t->query_var ] = $post_type; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
} |
0 | 318 |
|
319 |
foreach ( $this->public_query_vars as $wpvar ) { |
|
9 | 320 |
if ( isset( $this->extra_query_vars[ $wpvar ] ) ) { |
321 |
$this->query_vars[ $wpvar ] = $this->extra_query_vars[ $wpvar ]; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
322 |
} elseif ( isset( $_GET[ $wpvar ] ) && isset( $_POST[ $wpvar ] ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
323 |
&& $_GET[ $wpvar ] !== $_POST[ $wpvar ] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
324 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
325 |
wp_die( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
326 |
__( 'A variable mismatch has been detected.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
327 |
__( 'Sorry, you are not allowed to view this item.' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
328 |
400 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
329 |
); |
9 | 330 |
} elseif ( isset( $_POST[ $wpvar ] ) ) { |
331 |
$this->query_vars[ $wpvar ] = $_POST[ $wpvar ]; |
|
332 |
} elseif ( isset( $_GET[ $wpvar ] ) ) { |
|
333 |
$this->query_vars[ $wpvar ] = $_GET[ $wpvar ]; |
|
334 |
} elseif ( isset( $perma_query_vars[ $wpvar ] ) ) { |
|
335 |
$this->query_vars[ $wpvar ] = $perma_query_vars[ $wpvar ]; |
|
336 |
} |
|
0 | 337 |
|
9 | 338 |
if ( ! empty( $this->query_vars[ $wpvar ] ) ) { |
339 |
if ( ! is_array( $this->query_vars[ $wpvar ] ) ) { |
|
340 |
$this->query_vars[ $wpvar ] = (string) $this->query_vars[ $wpvar ]; |
|
0 | 341 |
} else { |
9 | 342 |
foreach ( $this->query_vars[ $wpvar ] as $vkey => $v ) { |
343 |
if ( is_scalar( $v ) ) { |
|
344 |
$this->query_vars[ $wpvar ][ $vkey ] = (string) $v; |
|
0 | 345 |
} |
346 |
} |
|
347 |
} |
|
348 |
||
9 | 349 |
if ( isset( $post_type_query_vars[ $wpvar ] ) ) { |
350 |
$this->query_vars['post_type'] = $post_type_query_vars[ $wpvar ]; |
|
351 |
$this->query_vars['name'] = $this->query_vars[ $wpvar ]; |
|
0 | 352 |
} |
353 |
} |
|
354 |
} |
|
355 |
||
16 | 356 |
// Convert urldecoded spaces back into '+'. |
9 | 357 |
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { |
358 |
if ( $t->query_var && isset( $this->query_vars[ $t->query_var ] ) ) { |
|
359 |
$this->query_vars[ $t->query_var ] = str_replace( ' ', '+', $this->query_vars[ $t->query_var ] ); |
|
360 |
} |
|
361 |
} |
|
0 | 362 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
// Don't allow non-publicly queryable taxonomies to be queried from the front end. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
if ( ! is_admin() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
foreach ( get_taxonomies( array( 'publicly_queryable' => false ), 'objects' ) as $taxonomy => $t ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
* Disallow when set to the 'taxonomy' query var. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* Non-publicly queryable taxonomies cannot register custom query vars. See register_taxonomy(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
if ( isset( $this->query_vars['taxonomy'] ) && $taxonomy === $this->query_vars['taxonomy'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
unset( $this->query_vars['taxonomy'], $this->query_vars['term'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
|
16 | 376 |
// Limit publicly queried post_types to those that are 'publicly_queryable'. |
9 | 377 |
if ( isset( $this->query_vars['post_type'] ) ) { |
378 |
$queryable_post_types = get_post_types( array( 'publicly_queryable' => true ) ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
379 |
|
0 | 380 |
if ( ! is_array( $this->query_vars['post_type'] ) ) { |
16 | 381 |
if ( ! in_array( $this->query_vars['post_type'], $queryable_post_types, true ) ) { |
0 | 382 |
unset( $this->query_vars['post_type'] ); |
9 | 383 |
} |
0 | 384 |
} else { |
385 |
$this->query_vars['post_type'] = array_intersect( $this->query_vars['post_type'], $queryable_post_types ); |
|
386 |
} |
|
387 |
} |
|
388 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
// Resolve conflicts between posts with numeric slugs and date archive queries. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
$this->query_vars = wp_resolve_numeric_slug_conflicts( $this->query_vars ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
|
9 | 392 |
foreach ( (array) $this->private_query_vars as $var ) { |
393 |
if ( isset( $this->extra_query_vars[ $var ] ) ) { |
|
394 |
$this->query_vars[ $var ] = $this->extra_query_vars[ $var ]; |
|
395 |
} |
|
0 | 396 |
} |
397 |
||
9 | 398 |
if ( isset( $error ) ) { |
0 | 399 |
$this->query_vars['error'] = $error; |
9 | 400 |
} |
0 | 401 |
|
5 | 402 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* Filters the array of parsed query variables. |
5 | 404 |
* |
405 |
* @since 2.1.0 |
|
406 |
* |
|
407 |
* @param array $query_vars The array of requested query variables. |
|
408 |
*/ |
|
409 |
$this->query_vars = apply_filters( 'request', $this->query_vars ); |
|
0 | 410 |
|
5 | 411 |
/** |
412 |
* Fires once all query variables for the current request have been parsed. |
|
413 |
* |
|
414 |
* @since 2.1.0 |
|
415 |
* |
|
19 | 416 |
* @param WP $wp Current WordPress environment instance (passed by reference). |
5 | 417 |
*/ |
418 |
do_action_ref_array( 'parse_request', array( &$this ) ); |
|
19 | 419 |
|
420 |
return true; |
|
0 | 421 |
} |
422 |
||
423 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* Sends additional HTTP headers for caching, content type, etc. |
0 | 425 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
* Sets the Content-Type header. Sets the 'error' status (if passed) and optionally exits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed. |
0 | 428 |
* |
429 |
* @since 2.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
430 |
* @since 4.4.0 `X-Pingback` header is added conditionally for single posts that allow pings. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
431 |
* @since 6.1.0 Runs after posts have been queried. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
432 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
* @global WP_Query $wp_query WordPress Query object. |
0 | 434 |
*/ |
5 | 435 |
public function send_headers() { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
436 |
global $wp_query; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
437 |
|
9 | 438 |
$headers = array(); |
439 |
$status = null; |
|
0 | 440 |
$exit_required = false; |
19 | 441 |
$date_format = 'D, d M Y H:i:s'; |
0 | 442 |
|
9 | 443 |
if ( is_user_logged_in() ) { |
444 |
$headers = array_merge( $headers, wp_get_nocache_headers() ); |
|
16 | 445 |
} elseif ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) { |
18 | 446 |
// Unmoderated comments are only visible for 10 minutes via the moderation hash. |
447 |
$expires = 10 * MINUTE_IN_SECONDS; |
|
448 |
||
19 | 449 |
$headers['Expires'] = gmdate( $date_format, time() + $expires ); |
18 | 450 |
$headers['Cache-Control'] = sprintf( |
451 |
'max-age=%d, must-revalidate', |
|
452 |
$expires |
|
453 |
); |
|
9 | 454 |
} |
0 | 455 |
if ( ! empty( $this->query_vars['error'] ) ) { |
456 |
$status = (int) $this->query_vars['error']; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
457 |
|
0 | 458 |
if ( 404 === $status ) { |
9 | 459 |
if ( ! is_user_logged_in() ) { |
460 |
$headers = array_merge( $headers, wp_get_nocache_headers() ); |
|
461 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
|
9 | 463 |
$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ); |
16 | 464 |
} elseif ( in_array( $status, array( 403, 500, 502, 503 ), true ) ) { |
0 | 465 |
$exit_required = true; |
466 |
} |
|
5 | 467 |
} elseif ( empty( $this->query_vars['feed'] ) ) { |
9 | 468 |
$headers['Content-Type'] = get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ); |
0 | 469 |
} else { |
16 | 470 |
// Set the correct content type for feeds. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
$type = $this->query_vars['feed']; |
16 | 472 |
if ( 'feed' === $this->query_vars['feed'] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
$type = get_default_feed(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
475 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
$headers['Content-Type'] = feed_content_type( $type ) . '; charset=' . get_option( 'blog_charset' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
// We're showing a feed, so WP is indeed the only thing that last changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
if ( ! empty( $this->query_vars['withcomments'] ) |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
480 |
|| str_contains( $this->query_vars['feed'], 'comments-' ) |
9 | 481 |
|| ( empty( $this->query_vars['withoutcomments'] ) |
482 |
&& ( ! empty( $this->query_vars['p'] ) |
|
483 |
|| ! empty( $this->query_vars['name'] ) |
|
484 |
|| ! empty( $this->query_vars['page_id'] ) |
|
485 |
|| ! empty( $this->query_vars['pagename'] ) |
|
486 |
|| ! empty( $this->query_vars['attachment'] ) |
|
487 |
|| ! empty( $this->query_vars['attachment_id'] ) |
|
488 |
) |
|
489 |
) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
490 |
) { |
19 | 491 |
$wp_last_modified_post = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false ); |
492 |
$wp_last_modified_comment = mysql2date( $date_format, get_lastcommentmodified( 'GMT' ), false ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
493 |
|
19 | 494 |
if ( strtotime( $wp_last_modified_post ) > strtotime( $wp_last_modified_comment ) ) { |
495 |
$wp_last_modified = $wp_last_modified_post; |
|
496 |
} else { |
|
497 |
$wp_last_modified = $wp_last_modified_comment; |
|
498 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
} else { |
19 | 500 |
$wp_last_modified = mysql2date( $date_format, get_lastpostmodified( 'GMT' ), false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
501 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
502 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
503 |
if ( ! $wp_last_modified ) { |
19 | 504 |
$wp_last_modified = gmdate( $date_format ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
505 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
506 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
$wp_last_modified .= ' GMT'; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
508 |
$wp_etag = '"' . md5( $wp_last_modified ) . '"'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
|
0 | 510 |
$headers['Last-Modified'] = $wp_last_modified; |
9 | 511 |
$headers['ETag'] = $wp_etag; |
0 | 512 |
|
16 | 513 |
// Support for conditional GET. |
9 | 514 |
if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) { |
0 | 515 |
$client_etag = wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ); |
9 | 516 |
} else { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
517 |
$client_etag = ''; |
9 | 518 |
} |
0 | 519 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
520 |
if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
521 |
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
$client_last_modified = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
524 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
|
16 | 526 |
// If string is empty, return 0. If not, attempt to parse into a timestamp. |
9 | 527 |
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0; |
0 | 528 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
529 |
// Make a timestamp for our most recent modification. |
9 | 530 |
$wp_modified_timestamp = strtotime( $wp_last_modified ); |
0 | 531 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
532 |
if ( ( $client_last_modified && $client_etag ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
533 |
? ( ( $client_modified_timestamp >= $wp_modified_timestamp ) && ( $client_etag === $wp_etag ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
534 |
: ( ( $client_modified_timestamp >= $wp_modified_timestamp ) || ( $client_etag === $wp_etag ) ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
535 |
) { |
9 | 536 |
$status = 304; |
0 | 537 |
$exit_required = true; |
538 |
} |
|
539 |
} |
|
540 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
541 |
if ( is_singular() ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
542 |
$post = isset( $wp_query->post ) ? $wp_query->post : null; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
543 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
544 |
// Only set X-Pingback for single posts that allow pings. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
if ( $post && pings_open( $post ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
$headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
547 |
} |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
548 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
549 |
// Send nocache headers for password protected posts to avoid unwanted caching. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
550 |
if ( ! empty( $post->post_password ) ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
551 |
$headers = array_merge( $headers, wp_get_nocache_headers() ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
552 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
554 |
|
5 | 555 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* Filters the HTTP headers before they're sent to the browser. |
5 | 557 |
* |
558 |
* @since 2.8.0 |
|
559 |
* |
|
9 | 560 |
* @param string[] $headers Associative array of headers to be sent. |
18 | 561 |
* @param WP $wp Current WordPress environment instance. |
5 | 562 |
*/ |
563 |
$headers = apply_filters( 'wp_headers', $headers, $this ); |
|
0 | 564 |
|
9 | 565 |
if ( ! empty( $status ) ) { |
0 | 566 |
status_header( $status ); |
9 | 567 |
} |
0 | 568 |
|
569 |
// If Last-Modified is set to false, it should not be sent (no-cache situation). |
|
570 |
if ( isset( $headers['Last-Modified'] ) && false === $headers['Last-Modified'] ) { |
|
571 |
unset( $headers['Last-Modified'] ); |
|
572 |
||
16 | 573 |
if ( ! headers_sent() ) { |
574 |
header_remove( 'Last-Modified' ); |
|
0 | 575 |
} |
576 |
} |
|
577 |
||
16 | 578 |
if ( ! headers_sent() ) { |
579 |
foreach ( (array) $headers as $name => $field_value ) { |
|
580 |
header( "{$name}: {$field_value}" ); |
|
581 |
} |
|
9 | 582 |
} |
0 | 583 |
|
9 | 584 |
if ( $exit_required ) { |
16 | 585 |
exit; |
9 | 586 |
} |
0 | 587 |
|
5 | 588 |
/** |
589 |
* Fires once the requested HTTP headers for caching, content type, etc. have been sent. |
|
590 |
* |
|
591 |
* @since 2.1.0 |
|
592 |
* |
|
19 | 593 |
* @param WP $wp Current WordPress environment instance (passed by reference). |
5 | 594 |
*/ |
595 |
do_action_ref_array( 'send_headers', array( &$this ) ); |
|
0 | 596 |
} |
597 |
||
598 |
/** |
|
599 |
* Sets the query string property based off of the query variable property. |
|
600 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
601 |
* The {@see 'query_string'} filter is deprecated, but still works. Plugins should |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
* use the {@see 'request'} filter instead. |
0 | 603 |
* |
604 |
* @since 2.0.0 |
|
605 |
*/ |
|
5 | 606 |
public function build_query_string() { |
0 | 607 |
$this->query_string = ''; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
608 |
|
9 | 609 |
foreach ( (array) array_keys( $this->query_vars ) as $wpvar ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
610 |
if ( '' !== $this->query_vars[ $wpvar ] ) { |
9 | 611 |
$this->query_string .= ( strlen( $this->query_string ) < 1 ) ? '' : '&'; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
612 |
|
9 | 613 |
if ( ! is_scalar( $this->query_vars[ $wpvar ] ) ) { // Discard non-scalars. |
0 | 614 |
continue; |
9 | 615 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
616 |
|
9 | 617 |
$this->query_string .= $wpvar . '=' . rawurlencode( $this->query_vars[ $wpvar ] ); |
0 | 618 |
} |
619 |
} |
|
620 |
||
5 | 621 |
if ( has_filter( 'query_string' ) ) { // Don't bother filtering and parsing if no plugins are hooked in. |
622 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
* Filters the query string before parsing. |
5 | 624 |
* |
625 |
* @since 1.5.0 |
|
16 | 626 |
* @deprecated 2.1.0 Use {@see 'query_vars'} or {@see 'request'} filters instead. |
5 | 627 |
* |
628 |
* @param string $query_string The query string to modify. |
|
629 |
*/ |
|
16 | 630 |
$this->query_string = apply_filters_deprecated( |
631 |
'query_string', |
|
632 |
array( $this->query_string ), |
|
633 |
'2.1.0', |
|
634 |
'query_vars, request' |
|
635 |
); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
636 |
|
9 | 637 |
parse_str( $this->query_string, $this->query_vars ); |
0 | 638 |
} |
639 |
} |
|
640 |
||
641 |
/** |
|
642 |
* Set up the WordPress Globals. |
|
643 |
* |
|
644 |
* The query_vars property will be extracted to the GLOBALS. So care should |
|
645 |
* be taken when naming global variables that might interfere with the |
|
646 |
* WordPress environment. |
|
647 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
* @since 2.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
* |
16 | 650 |
* @global WP_Query $wp_query WordPress Query object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* @global string $query_string Query string for the loop. |
16 | 652 |
* @global array $posts The found posts. |
653 |
* @global WP_Post|null $post The current post, if available. |
|
654 |
* @global string $request The SQL statement for the request. |
|
655 |
* @global int $more Only set, if single page or post. |
|
656 |
* @global int $single If single page or post. Only set, if single page or post. |
|
657 |
* @global WP_User $authordata Only set, if author archive. |
|
0 | 658 |
*/ |
5 | 659 |
public function register_globals() { |
0 | 660 |
global $wp_query; |
661 |
||
662 |
// Extract updated query vars back into global namespace. |
|
663 |
foreach ( (array) $wp_query->query_vars as $key => $value ) { |
|
664 |
$GLOBALS[ $key ] = $value; |
|
665 |
} |
|
666 |
||
667 |
$GLOBALS['query_string'] = $this->query_string; |
|
9 | 668 |
$GLOBALS['posts'] = & $wp_query->posts; |
669 |
$GLOBALS['post'] = isset( $wp_query->post ) ? $wp_query->post : null; |
|
670 |
$GLOBALS['request'] = $wp_query->request; |
|
0 | 671 |
|
672 |
if ( $wp_query->is_single() || $wp_query->is_page() ) { |
|
673 |
$GLOBALS['more'] = 1; |
|
674 |
$GLOBALS['single'] = 1; |
|
675 |
} |
|
676 |
||
18 | 677 |
if ( $wp_query->is_author() ) { |
678 |
$GLOBALS['authordata'] = get_userdata( get_queried_object_id() ); |
|
9 | 679 |
} |
0 | 680 |
} |
681 |
||
682 |
/** |
|
683 |
* Set up the current user. |
|
684 |
* |
|
685 |
* @since 2.0.0 |
|
686 |
*/ |
|
5 | 687 |
public function init() { |
0 | 688 |
wp_get_current_user(); |
689 |
} |
|
690 |
||
691 |
/** |
|
692 |
* Set up the Loop based on the query variables. |
|
693 |
* |
|
694 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
695 |
* |
16 | 696 |
* @global WP_Query $wp_the_query WordPress Query object. |
0 | 697 |
*/ |
5 | 698 |
public function query_posts() { |
0 | 699 |
global $wp_the_query; |
700 |
$this->build_query_string(); |
|
9 | 701 |
$wp_the_query->query( $this->query_vars ); |
702 |
} |
|
0 | 703 |
|
9 | 704 |
/** |
5 | 705 |
* Set the Headers for 404, if nothing is found for requested URL. |
0 | 706 |
* |
16 | 707 |
* Issue a 404 if a request doesn't match any posts and doesn't match any object |
708 |
* (e.g. an existing-but-empty category, tag, author) and a 404 was not already issued, |
|
709 |
* and if the request was not a search or the homepage. |
|
0 | 710 |
* |
711 |
* Otherwise, issue a 200. |
|
712 |
* |
|
16 | 713 |
* This sets headers after posts have been queried. handle_404() really means "handle status". |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
* By inspecting the result of querying posts, seemingly successful requests can be switched to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
* a 404 so that canonical redirection logic can kick in. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
* |
0 | 717 |
* @since 2.0.0 |
9 | 718 |
* |
16 | 719 |
* @global WP_Query $wp_query WordPress Query object. |
9 | 720 |
*/ |
5 | 721 |
public function handle_404() { |
0 | 722 |
global $wp_query; |
723 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
* Filters whether to short-circuit default header status handling. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
* Returning a non-false value from the filter will short-circuit the handling |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
* and return early. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
729 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
730 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
731 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
* @param bool $preempt Whether to short-circuit default header status handling. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
* @param WP_Query $wp_query WordPress Query object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
735 |
if ( false !== apply_filters( 'pre_handle_404', false, $wp_query ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
738 |
|
0 | 739 |
// If we've already issued a 404, bail. |
9 | 740 |
if ( is_404() ) { |
0 | 741 |
return; |
9 | 742 |
} |
0 | 743 |
|
16 | 744 |
$set_404 = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
|
16 | 746 |
// Never 404 for the admin, robots, or favicon. |
747 |
if ( is_admin() || is_robots() || is_favicon() ) { |
|
748 |
$set_404 = false; |
|
749 |
||
750 |
// If posts were found, check for paged content. |
|
751 |
} elseif ( $wp_query->posts ) { |
|
752 |
$content_found = true; |
|
753 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
if ( is_singular() ) { |
16 | 755 |
$post = isset( $wp_query->post ) ? $wp_query->post : null; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
756 |
$next = '<!--nextpage-->'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
|
16 | 758 |
// Check for paged content that exceeds the max number of pages. |
759 |
if ( $post && ! empty( $this->query_vars['page'] ) ) { |
|
760 |
// Check if content is actually intended to be paged. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
761 |
if ( str_contains( $post->post_content, $next ) ) { |
16 | 762 |
$page = trim( $this->query_vars['page'], '/' ); |
763 |
$content_found = (int) $page <= ( substr_count( $post->post_content, $next ) + 1 ); |
|
764 |
} else { |
|
765 |
$content_found = false; |
|
766 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
|
16 | 770 |
// The posts page does not support the <!--nextpage--> pagination. |
771 |
if ( $wp_query->is_posts_page && ! empty( $this->query_vars['page'] ) ) { |
|
772 |
$content_found = false; |
|
773 |
} |
|
774 |
||
775 |
if ( $content_found ) { |
|
776 |
$set_404 = false; |
|
777 |
} |
|
778 |
||
779 |
// We will 404 for paged queries, as no posts were found. |
|
780 |
} elseif ( ! is_paged() ) { |
|
781 |
$author = get_query_var( 'author' ); |
|
782 |
||
783 |
// Don't 404 for authors without posts as long as they matched an author on this site. |
|
784 |
if ( is_author() && is_numeric( $author ) && $author > 0 && is_user_member_of_blog( $author ) |
|
785 |
// Don't 404 for these queries if they matched an object. |
|
786 |
|| ( is_tag() || is_category() || is_tax() || is_post_type_archive() ) && get_queried_object() |
|
787 |
// Don't 404 for these queries either. |
|
788 |
|| is_home() || is_search() || is_feed() |
|
789 |
) { |
|
790 |
$set_404 = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
791 |
} |
0 | 792 |
} |
793 |
||
16 | 794 |
if ( $set_404 ) { |
795 |
// Guess it's time to 404. |
|
796 |
$wp_query->set_404(); |
|
797 |
status_header( 404 ); |
|
798 |
nocache_headers(); |
|
799 |
} else { |
|
800 |
status_header( 200 ); |
|
0 | 801 |
} |
802 |
} |
|
803 |
||
804 |
/** |
|
805 |
* Sets up all of the variables required by the WordPress environment. |
|
806 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
807 |
* The action {@see 'wp'} has one parameter that references the WP object. It |
0 | 808 |
* allows for accessing the properties and methods to further manipulate the |
809 |
* object. |
|
810 |
* |
|
811 |
* @since 2.0.0 |
|
812 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
813 |
* @param string|array $query_args Passed to parse_request(). |
0 | 814 |
*/ |
9 | 815 |
public function main( $query_args = '' ) { |
0 | 816 |
$this->init(); |
19 | 817 |
|
818 |
$parsed = $this->parse_request( $query_args ); |
|
819 |
||
820 |
if ( $parsed ) { |
|
821 |
$this->query_posts(); |
|
822 |
$this->handle_404(); |
|
823 |
$this->register_globals(); |
|
824 |
} |
|
5 | 825 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
826 |
$this->send_headers(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
827 |
|
5 | 828 |
/** |
829 |
* Fires once the WordPress environment has been set up. |
|
830 |
* |
|
831 |
* @since 2.1.0 |
|
832 |
* |
|
19 | 833 |
* @param WP $wp Current WordPress environment instance (passed by reference). |
5 | 834 |
*/ |
835 |
do_action_ref_array( 'wp', array( &$this ) ); |
|
0 | 836 |
} |
837 |
} |