author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Rewrite API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Rewrite |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
18 | 10 |
* Endpoint mask that matches nothing. |
0 | 11 |
* |
12 |
* @since 2.1.0 |
|
13 |
*/ |
|
9 | 14 |
define( 'EP_NONE', 0 ); |
0 | 15 |
|
16 |
/** |
|
18 | 17 |
* Endpoint mask that matches post permalinks. |
0 | 18 |
* |
19 |
* @since 2.1.0 |
|
20 |
*/ |
|
9 | 21 |
define( 'EP_PERMALINK', 1 ); |
0 | 22 |
|
23 |
/** |
|
18 | 24 |
* Endpoint mask that matches attachment permalinks. |
0 | 25 |
* |
26 |
* @since 2.1.0 |
|
27 |
*/ |
|
9 | 28 |
define( 'EP_ATTACHMENT', 2 ); |
0 | 29 |
|
30 |
/** |
|
18 | 31 |
* Endpoint mask that matches any date archives. |
0 | 32 |
* |
33 |
* @since 2.1.0 |
|
34 |
*/ |
|
9 | 35 |
define( 'EP_DATE', 4 ); |
0 | 36 |
|
37 |
/** |
|
18 | 38 |
* Endpoint mask that matches yearly archives. |
0 | 39 |
* |
40 |
* @since 2.1.0 |
|
41 |
*/ |
|
9 | 42 |
define( 'EP_YEAR', 8 ); |
0 | 43 |
|
44 |
/** |
|
18 | 45 |
* Endpoint mask that matches monthly archives. |
0 | 46 |
* |
47 |
* @since 2.1.0 |
|
48 |
*/ |
|
9 | 49 |
define( 'EP_MONTH', 16 ); |
0 | 50 |
|
51 |
/** |
|
18 | 52 |
* Endpoint mask that matches daily archives. |
0 | 53 |
* |
54 |
* @since 2.1.0 |
|
55 |
*/ |
|
9 | 56 |
define( 'EP_DAY', 32 ); |
0 | 57 |
|
58 |
/** |
|
18 | 59 |
* Endpoint mask that matches the site root. |
0 | 60 |
* |
61 |
* @since 2.1.0 |
|
62 |
*/ |
|
9 | 63 |
define( 'EP_ROOT', 64 ); |
0 | 64 |
|
65 |
/** |
|
18 | 66 |
* Endpoint mask that matches comment feeds. |
0 | 67 |
* |
68 |
* @since 2.1.0 |
|
69 |
*/ |
|
9 | 70 |
define( 'EP_COMMENTS', 128 ); |
0 | 71 |
|
72 |
/** |
|
18 | 73 |
* Endpoint mask that matches searches. |
74 |
* |
|
75 |
* Note that this only matches a search at a "pretty" URL such as |
|
76 |
* `/search/my-search-term`, not `?s=my-search-term`. |
|
0 | 77 |
* |
78 |
* @since 2.1.0 |
|
79 |
*/ |
|
9 | 80 |
define( 'EP_SEARCH', 256 ); |
0 | 81 |
|
82 |
/** |
|
18 | 83 |
* Endpoint mask that matches category archives. |
0 | 84 |
* |
85 |
* @since 2.1.0 |
|
86 |
*/ |
|
9 | 87 |
define( 'EP_CATEGORIES', 512 ); |
0 | 88 |
|
89 |
/** |
|
18 | 90 |
* Endpoint mask that matches tag archives. |
0 | 91 |
* |
92 |
* @since 2.3.0 |
|
93 |
*/ |
|
9 | 94 |
define( 'EP_TAGS', 1024 ); |
0 | 95 |
|
96 |
/** |
|
18 | 97 |
* Endpoint mask that matches author archives. |
0 | 98 |
* |
99 |
* @since 2.1.0 |
|
100 |
*/ |
|
9 | 101 |
define( 'EP_AUTHORS', 2048 ); |
0 | 102 |
|
103 |
/** |
|
18 | 104 |
* Endpoint mask that matches pages. |
0 | 105 |
* |
106 |
* @since 2.1.0 |
|
107 |
*/ |
|
9 | 108 |
define( 'EP_PAGES', 4096 ); |
0 | 109 |
|
110 |
/** |
|
18 | 111 |
* Endpoint mask that matches all archive views. |
0 | 112 |
* |
113 |
* @since 3.7.0 |
|
114 |
*/ |
|
115 |
define( 'EP_ALL_ARCHIVES', EP_DATE | EP_YEAR | EP_MONTH | EP_DAY | EP_CATEGORIES | EP_TAGS | EP_AUTHORS ); |
|
116 |
||
117 |
/** |
|
18 | 118 |
* Endpoint mask that matches everything. |
0 | 119 |
* |
120 |
* @since 2.1.0 |
|
121 |
*/ |
|
122 |
define( 'EP_ALL', EP_PERMALINK | EP_ATTACHMENT | EP_ROOT | EP_COMMENTS | EP_SEARCH | EP_PAGES | EP_ALL_ARCHIVES ); |
|
123 |
||
124 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* Adds a rewrite rule that transforms a URL structure to a set of query vars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* Any value in the $after parameter that isn't 'bottom' will result in the rule |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* being placed at the top of the rewrite rules. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
* @since 4.4.0 Array support was added to the `$query` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* |
16 | 133 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* @param string $regex Regular expression to match request against. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* @param string|array $query The corresponding query vars for this rewrite rule. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* @param string $after Optional. Priority of the new rule. Accepts 'top' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* or 'bottom'. Default 'bottom'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
function add_rewrite_rule( $regex, $query, $after = 'bottom' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
$wp_rewrite->add_rule( $regex, $query, $after ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
/** |
19 | 147 |
* Adds a new rewrite tag (like %postname%). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
* |
18 | 149 |
* The `$query` parameter is optional. If it is omitted you must ensure that you call |
150 |
* this on, or before, the {@see 'init'} hook. This is because `$query` defaults to |
|
151 |
* `$tag=`, and for this to work a new query var has to be added. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
* |
16 | 155 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
156 |
* @global WP $wp Current WordPress environment instance. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
* @param string $tag Name of the new rewrite tag. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* @param string $regex Regular expression to substitute the tag for in rewrite rules. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* @param string $query Optional. String to append to the rewritten query. Must end in '='. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
function add_rewrite_tag( $tag, $regex, $query = '' ) { |
16 | 163 |
// Validate the tag's name. |
164 |
if ( strlen( $tag ) < 3 || '%' !== $tag[0] || '%' !== $tag[ strlen( $tag ) - 1 ] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
return; |
9 | 166 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
global $wp_rewrite, $wp; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
if ( empty( $query ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
$qv = trim( $tag, '%' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
172 |
$wp->add_query_var( $qv ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
$query = $qv . '='; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
$wp_rewrite->add_rewrite_tag( $tag, $regex, $query ); |
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 |
|
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 |
* Removes an existing rewrite tag (like %postname%). |
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 |
* @since 4.5.0 |
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 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* @param string $tag Name of the rewrite tag. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
function remove_rewrite_tag( $tag ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
$wp_rewrite->remove_rewrite_tag( $tag ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
/** |
19 | 194 |
* Adds a permalink structure. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
* @see WP_Rewrite::add_permastruct() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* @param string $name Name for permalink structure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
* @param string $struct Permalink structure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* @param array $args Optional. Arguments for building the rules from the permalink structure, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* see WP_Rewrite::add_permastruct() for full details. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
function add_permastruct( $name, $struct, $args = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
// Back-compat for the old parameters: $with_front and $ep_mask. |
9 | 210 |
if ( ! is_array( $args ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
$args = array( 'with_front' => $args ); |
9 | 212 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
213 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
214 |
if ( func_num_args() === 4 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
$args['ep_mask'] = func_get_arg( 3 ); |
9 | 216 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
$wp_rewrite->add_permastruct( $name, $struct, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
* Removes a permalink structure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
* Can only be used to remove permastructs that were added using add_permastruct(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* Built-in permastructs cannot be removed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* @see WP_Rewrite::remove_permastruct() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* @param string $name Name for permalink structure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
function remove_permastruct( $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
$wp_rewrite->remove_permastruct( $name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
/** |
19 | 241 |
* Adds a new feed type like /atom1/. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
* |
16 | 245 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
* |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
247 |
* @param string $feedname Feed name. Should not start with '_'. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
248 |
* @param callable $callback Callback to run on feed display. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
* @return string Feed action name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
251 |
function add_feed( $feedname, $callback ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
global $wp_rewrite; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
|
16 | 254 |
if ( ! in_array( $feedname, $wp_rewrite->feeds, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
$wp_rewrite->feeds[] = $feedname; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
$hook = 'do_feed_' . $feedname; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
|
16 | 260 |
// Remove default function hook. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
remove_action( $hook, $hook ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
263 |
add_action( $hook, $callback, 10, 2 ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
return $hook; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
/** |
19 | 269 |
* Removes rewrite rules and then recreate rewrite rules. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
* |
16 | 273 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
* @param bool $hard Whether to update .htaccess (hard flush) or just update |
16 | 276 |
* rewrite_rules option (soft flush). Default is true (hard). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
function flush_rewrite_rules( $hard = true ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
global $wp_rewrite; |
16 | 280 |
|
281 |
if ( is_callable( array( $wp_rewrite, 'flush_rules' ) ) ) { |
|
282 |
$wp_rewrite->flush_rules( $hard ); |
|
283 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
285 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
/** |
19 | 287 |
* Adds an endpoint, like /trackback/. |
0 | 288 |
* |
289 |
* Adding an endpoint creates extra rewrite rules for each of the matching |
|
290 |
* places specified by the provided bitmask. For example: |
|
291 |
* |
|
5 | 292 |
* add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES ); |
0 | 293 |
* |
294 |
* will add a new rewrite rule ending with "json(/(.*))?/?$" for every permastruct |
|
295 |
* that describes a permalink (post) or page. This is rewritten to "json=$match" |
|
296 |
* where $match is the part of the URL matched by the endpoint regex (e.g. "foo" in |
|
5 | 297 |
* "[permalink]/json/foo/"). |
0 | 298 |
* |
299 |
* A new query var with the same name as the endpoint will also be created. |
|
300 |
* |
|
301 |
* When specifying $places ensure that you are using the EP_* constants (or a |
|
302 |
* combination of them using the bitwise OR operator) as their values are not |
|
5 | 303 |
* guaranteed to remain static (especially `EP_ALL`). |
0 | 304 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
* Be sure to flush the rewrite rules - see flush_rewrite_rules() - when your plugin gets |
0 | 306 |
* activated and deactivated. |
307 |
* |
|
308 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* @since 4.3.0 Added support for skipping query var registration by passing `false` to `$query_var`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* |
16 | 311 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
0 | 312 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
* @param string $name Name of the endpoint. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
* @param int $places Endpoint mask describing the places the endpoint should be added. |
18 | 315 |
* Accepts a mask of: |
316 |
* - `EP_ALL` |
|
317 |
* - `EP_NONE` |
|
318 |
* - `EP_ALL_ARCHIVES` |
|
319 |
* - `EP_ATTACHMENT` |
|
320 |
* - `EP_AUTHORS` |
|
321 |
* - `EP_CATEGORIES` |
|
322 |
* - `EP_COMMENTS` |
|
323 |
* - `EP_DATE` |
|
324 |
* - `EP_DAY` |
|
325 |
* - `EP_MONTH` |
|
326 |
* - `EP_PAGES` |
|
327 |
* - `EP_PERMALINK` |
|
328 |
* - `EP_ROOT` |
|
329 |
* - `EP_SEARCH` |
|
330 |
* - `EP_TAGS` |
|
331 |
* - `EP_YEAR` |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* @param string|bool $query_var Name of the corresponding query variable. Pass `false` to skip registering a query_var |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
* for this endpoint. Defaults to the value of `$name`. |
0 | 334 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
function add_rewrite_endpoint( $name, $places, $query_var = true ) { |
0 | 336 |
global $wp_rewrite; |
5 | 337 |
$wp_rewrite->add_endpoint( $name, $places, $query_var ); |
0 | 338 |
} |
339 |
||
340 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* Filters the URL base for taxonomies. |
0 | 342 |
* |
343 |
* To remove any manually prepended /index.php/. |
|
344 |
* |
|
345 |
* @access private |
|
346 |
* @since 2.6.0 |
|
347 |
* |
|
348 |
* @param string $base The taxonomy base that we're going to filter |
|
349 |
* @return string |
|
350 |
*/ |
|
351 |
function _wp_filter_taxonomy_base( $base ) { |
|
9 | 352 |
if ( ! empty( $base ) ) { |
0 | 353 |
$base = preg_replace( '|^/index\.php/|', '', $base ); |
354 |
$base = trim( $base, '/' ); |
|
355 |
} |
|
356 |
return $base; |
|
357 |
} |
|
358 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
|
0 | 360 |
/** |
19 | 361 |
* Resolves numeric slugs that collide with date permalinks. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* Permalinks of posts with numeric slugs can sometimes look to WP_Query::parse_query() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* like a date archive, as when your permalink structure is `/%year%/%postname%/` and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* a post with post_name '05' has the URL `/2015/05/`. |
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 |
* This function detects conflicts of this type and resolves them in favor of the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* post permalink. |
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 |
* Note that, since 4.3.0, wp_unique_post_slug() prevents the creation of post slugs |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
* that would result in a date archive conflict. The resolution performed in this |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* function is primarily for legacy content, as well as cases when the admin has changed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
* the site's permalink structure in a way that introduces URL conflicts. |
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 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* @param array $query_vars Optional. Query variables for setting up the loop, as determined in |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
378 |
* WP::parse_request(). Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
* @return array Returns the original array of query vars, with date/post conflicts resolved. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
381 |
function wp_resolve_numeric_slug_conflicts( $query_vars = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
if ( ! isset( $query_vars['year'] ) && ! isset( $query_vars['monthnum'] ) && ! isset( $query_vars['day'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
// Identify the 'postname' position in the permastruct array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
16 | 388 |
$postname_index = array_search( '%postname%', $permastructs, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
if ( false === $postname_index ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
* A numeric slug could be confused with a year, month, or day, depending on position. To account for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* the possibility of post pagination (eg 2015/2 for the second page of a post called '2015'), our |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
* `is_*` checks are generous: check for year-slug clashes when `is_year` *or* `is_month`, and check |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* for month-slug clashes when `is_month` *or* `is_day`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
400 |
$compare = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
402 |
$compare = 'year'; |
9 | 403 |
} elseif ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
$compare = 'monthnum'; |
9 | 405 |
} elseif ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && isset( $query_vars['day'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
406 |
$compare = 'day'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
408 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
409 |
if ( ! $compare ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
// This is the potentially clashing slug. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
414 |
$value = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
415 |
if ( $compare && array_key_exists( $compare, $query_vars ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
416 |
$value = $query_vars[ $compare ]; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
417 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
$post = get_page_by_path( $value, OBJECT, 'post' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
if ( ! ( $post instanceof WP_Post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
// If the date of the post doesn't match the date specified in the URL, resolve to the date archive. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
if ( preg_match( '/^([0-9]{4})\-([0-9]{2})/', $post->post_date, $matches ) && isset( $query_vars['year'] ) && ( 'monthnum' === $compare || 'day' === $compare ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
// $matches[1] is the year the post was published. |
18 | 427 |
if ( (int) $query_vars['year'] !== (int) $matches[1] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
// $matches[2] is the month the post was published. |
18 | 432 |
if ( 'day' === $compare && isset( $query_vars['monthnum'] ) && (int) $query_vars['monthnum'] !== (int) $matches[2] ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
* If the located post contains nextpage pagination, then the URL chunk following postname may be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* intended as the page number. Verify that it's a valid page before resolving to it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
$maybe_page = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
if ( 'year' === $compare && isset( $query_vars['monthnum'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
$maybe_page = $query_vars['monthnum']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
} elseif ( 'monthnum' === $compare && isset( $query_vars['day'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
$maybe_page = $query_vars['day']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
} |
16 | 447 |
// Bug found in #11694 - 'page' was returning '/4'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
448 |
$maybe_page = (int) trim( $maybe_page, '/' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
$post_page_count = substr_count( $post->post_content, '<!--nextpage-->' ) + 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
// If the post doesn't have multiple pages, but a 'page' candidate is found, resolve to the date archive. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
if ( 1 === $post_page_count && $maybe_page ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
457 |
// If the post has multiple pages and the 'page' number isn't valid, resolve to the date archive. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
if ( $post_page_count > 1 && $maybe_page > $post_page_count ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
return $query_vars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
// If we've gotten to this point, we have a slug/date clash. First, adjust for nextpage. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
if ( '' !== $maybe_page ) { |
18 | 464 |
$query_vars['page'] = (int) $maybe_page; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
// Next, unset autodetected date-related query vars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
unset( $query_vars['year'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
unset( $query_vars['monthnum'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
unset( $query_vars['day'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
// Then, set the identified post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
$query_vars['name'] = $post->post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
// Finally, return the modified query vars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
return $query_vars; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
479 |
/** |
19 | 480 |
* Examines a URL and try to determine the post ID it represents. |
0 | 481 |
* |
482 |
* Checks are supposedly from the hosted site blog. |
|
483 |
* |
|
484 |
* @since 1.0.0 |
|
485 |
* |
|
16 | 486 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
487 |
* @global WP $wp Current WordPress environment instance. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
488 |
* |
0 | 489 |
* @param string $url Permalink to check. |
490 |
* @return int Post ID, or 0 on failure. |
|
491 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
492 |
function url_to_postid( $url ) { |
0 | 493 |
global $wp_rewrite; |
494 |
||
5 | 495 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
496 |
* Filters the URL to derive the post ID from. |
5 | 497 |
* |
498 |
* @since 2.2.0 |
|
499 |
* |
|
500 |
* @param string $url The URL to derive the post ID from. |
|
501 |
*/ |
|
502 |
$url = apply_filters( 'url_to_postid', $url ); |
|
0 | 503 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
504 |
$url_host = parse_url( $url, PHP_URL_HOST ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
505 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
506 |
if ( is_string( $url_host ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
507 |
$url_host = str_replace( 'www.', '', $url_host ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
508 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
509 |
$url_host = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
510 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
511 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
512 |
$home_url_host = parse_url( home_url(), PHP_URL_HOST ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
513 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
514 |
if ( is_string( $home_url_host ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
515 |
$home_url_host = str_replace( 'www.', '', $home_url_host ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
516 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
517 |
$home_url_host = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
518 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
520 |
// Bail early if the URL does not belong to this site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
if ( $url_host && $url_host !== $home_url_host ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
522 |
return 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
|
16 | 525 |
// First, check to see if there is a 'p=N' or 'page_id=N' to match against. |
9 | 526 |
if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) { |
527 |
$id = absint( $values[2] ); |
|
528 |
if ( $id ) { |
|
0 | 529 |
return $id; |
9 | 530 |
} |
0 | 531 |
} |
532 |
||
16 | 533 |
// Get rid of the #anchor. |
9 | 534 |
$url_split = explode( '#', $url ); |
535 |
$url = $url_split[0]; |
|
0 | 536 |
|
16 | 537 |
// Get rid of URL ?query=string. |
9 | 538 |
$url_split = explode( '?', $url ); |
539 |
$url = $url_split[0]; |
|
0 | 540 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
// Set the correct URL scheme. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
$scheme = parse_url( home_url(), PHP_URL_SCHEME ); |
9 | 543 |
$url = set_url_scheme( $url, $scheme ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
544 |
|
16 | 545 |
// Add 'www.' if it is absent and should be there. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) { |
9 | 547 |
$url = str_replace( '://', '://www.', $url ); |
548 |
} |
|
0 | 549 |
|
16 | 550 |
// Strip 'www.' if it is present and shouldn't be. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
551 |
if ( ! str_contains( home_url(), '://www.' ) ) { |
9 | 552 |
$url = str_replace( '://www.', '://', $url ); |
553 |
} |
|
0 | 554 |
|
16 | 555 |
if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
$page_on_front = get_option( 'page_on_front' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
559 |
return (int) $page_on_front; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
561 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
562 |
|
16 | 563 |
// Check to see if we are using rewrite rules. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
|
16 | 566 |
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options. |
9 | 567 |
if ( empty( $rewrite ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
return 0; |
9 | 569 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
|
16 | 571 |
// Strip 'index.php/' if we're not using path info permalinks. |
9 | 572 |
if ( ! $wp_rewrite->using_index_permalinks() ) { |
0 | 573 |
$url = str_replace( $wp_rewrite->index . '/', '', $url ); |
9 | 574 |
} |
0 | 575 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
576 |
if ( str_contains( trailingslashit( $url ), home_url( '/' ) ) ) { |
16 | 577 |
// Chop off http://domain.com/[path]. |
9 | 578 |
$url = str_replace( home_url(), '', $url ); |
0 | 579 |
} else { |
16 | 580 |
// Chop off /path/to/blog. |
0 | 581 |
$home_path = parse_url( home_url( '/' ) ); |
9 | 582 |
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : ''; |
583 |
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) ); |
|
0 | 584 |
} |
585 |
||
16 | 586 |
// Trim leading and lagging slashes. |
9 | 587 |
$url = trim( $url, '/' ); |
0 | 588 |
|
9 | 589 |
$request = $url; |
0 | 590 |
$post_type_query_vars = array(); |
591 |
||
9 | 592 |
foreach ( get_post_types( array(), 'objects' ) as $post_type => $t ) { |
593 |
if ( ! empty( $t->query_var ) ) { |
|
0 | 594 |
$post_type_query_vars[ $t->query_var ] = $post_type; |
9 | 595 |
} |
0 | 596 |
} |
597 |
||
598 |
// Look for matches. |
|
599 |
$request_match = $request; |
|
9 | 600 |
foreach ( (array) $rewrite as $match => $query ) { |
0 | 601 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
602 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
603 |
* If the requesting file is the anchor of the match, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
604 |
* prepend it to the path info. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
605 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
606 |
if ( ! empty( $url ) && ( $url !== $request ) && str_starts_with( $match, $url ) ) { |
0 | 607 |
$request_match = $url . '/' . $request; |
9 | 608 |
} |
0 | 609 |
|
9 | 610 |
if ( preg_match( "#^$match#", $request_match, $matches ) ) { |
0 | 611 |
|
612 |
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) { |
|
5 | 613 |
// 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
|
614 |
$page = get_page_by_path( $matches[ $varmatch[1] ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
615 |
if ( ! $page ) { |
0 | 616 |
continue; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
617 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
619 |
$post_status_obj = get_post_status_object( $page->post_status ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
620 |
if ( ! $post_status_obj->public && ! $post_status_obj->protected |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
621 |
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
} |
0 | 624 |
} |
625 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
626 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
627 |
* Got a match. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
628 |
* Trim the query of everything up to the '?'. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
629 |
*/ |
9 | 630 |
$query = preg_replace( '!^.+\?!', '', $query ); |
0 | 631 |
|
632 |
// Substitute the substring matches into the query. |
|
9 | 633 |
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) ); |
0 | 634 |
|
16 | 635 |
// Filter out non-public query vars. |
0 | 636 |
global $wp; |
637 |
parse_str( $query, $query_vars ); |
|
638 |
$query = array(); |
|
639 |
foreach ( (array) $query_vars as $key => $value ) { |
|
16 | 640 |
if ( in_array( (string) $key, $wp->public_query_vars, true ) ) { |
9 | 641 |
$query[ $key ] = $value; |
642 |
if ( isset( $post_type_query_vars[ $key ] ) ) { |
|
643 |
$query['post_type'] = $post_type_query_vars[ $key ]; |
|
644 |
$query['name'] = $value; |
|
0 | 645 |
} |
646 |
} |
|
647 |
} |
|
648 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
// 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
|
650 |
$query = wp_resolve_numeric_slug_conflicts( $query ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
|
16 | 652 |
// Do the query. |
0 | 653 |
$query = new WP_Query( $query ); |
9 | 654 |
if ( ! empty( $query->posts ) && $query->is_singular ) { |
0 | 655 |
return $query->post->ID; |
9 | 656 |
} else { |
0 | 657 |
return 0; |
9 | 658 |
} |
0 | 659 |
} |
660 |
} |
|
661 |
return 0; |
|
662 |
} |