author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 13 | d255fe9cd479 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Main WordPress API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
16 | 8 |
require ABSPATH . WPINC . '/option.php'; |
9 |
||
10 |
/** |
|
11 |
* Convert given MySQL date string into a different format. |
|
12 |
* |
|
13 |
* `$format` should be a PHP date format string. |
|
14 |
* 'U' and 'G' formats will return a sum of timestamp with timezone offset. |
|
15 |
* `$date` is expected to be local time in MySQL format (`Y-m-d H:i:s`). |
|
16 |
* |
|
17 |
* Historically UTC time could be passed to the function to produce Unix timestamp. |
|
18 |
* |
|
19 |
* If `$translate` is true then the given date and format string will |
|
20 |
* be passed to `wp_date()` for translation. |
|
0 | 21 |
* |
22 |
* @since 0.71 |
|
23 |
* |
|
5 | 24 |
* @param string $format Format of the date to return. |
25 |
* @param string $date Date string to convert. |
|
26 |
* @param bool $translate Whether the return date should be translated. Default true. |
|
16 | 27 |
* @return string|int|false Formatted date string or sum of Unix timestamp and timezone offset. |
28 |
* False on failure. |
|
0 | 29 |
*/ |
30 |
function mysql2date( $format, $date, $translate = true ) { |
|
9 | 31 |
if ( empty( $date ) ) { |
0 | 32 |
return false; |
9 | 33 |
} |
34 |
||
16 | 35 |
$datetime = date_create( $date, wp_timezone() ); |
36 |
||
37 |
if ( false === $datetime ) { |
|
38 |
return false; |
|
39 |
} |
|
40 |
||
41 |
// Returns a sum of timestamp with timezone offset. Ideally should never be used. |
|
42 |
if ( 'G' === $format || 'U' === $format ) { |
|
43 |
return $datetime->getTimestamp() + $datetime->getOffset(); |
|
9 | 44 |
} |
45 |
||
46 |
if ( $translate ) { |
|
16 | 47 |
return wp_date( $format, $datetime->getTimestamp() ); |
48 |
} |
|
49 |
||
50 |
return $datetime->format( $format ); |
|
51 |
} |
|
52 |
||
53 |
/** |
|
54 |
* Retrieves the current time based on specified type. |
|
0 | 55 |
* |
56 |
* The 'mysql' type will return the time in the format for MySQL DATETIME field. |
|
16 | 57 |
* The 'timestamp' type will return the current timestamp or a sum of timestamp |
58 |
* and timezone offset, depending on `$gmt`. |
|
5 | 59 |
* Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). |
0 | 60 |
* |
61 |
* If $gmt is set to either '1' or 'true', then both types will use GMT time. |
|
62 |
* if $gmt is false, the output is adjusted with the GMT offset in the WordPress option. |
|
63 |
* |
|
64 |
* @since 1.0.0 |
|
65 |
* |
|
16 | 66 |
* @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', |
67 |
* or PHP date format string (e.g. 'Y-m-d'). |
|
5 | 68 |
* @param int|bool $gmt Optional. Whether to use GMT timezone. Default false. |
69 |
* @return int|string Integer if $type is 'timestamp', string otherwise. |
|
0 | 70 |
*/ |
71 |
function current_time( $type, $gmt = 0 ) { |
|
16 | 72 |
// Don't use non-GMT timestamp, unless you know the difference and really need to. |
73 |
if ( 'timestamp' === $type || 'U' === $type ) { |
|
74 |
return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
|
75 |
} |
|
76 |
||
77 |
if ( 'mysql' === $type ) { |
|
78 |
$type = 'Y-m-d H:i:s'; |
|
79 |
} |
|
80 |
||
81 |
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone(); |
|
82 |
$datetime = new DateTime( 'now', $timezone ); |
|
83 |
||
84 |
return $datetime->format( $type ); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Retrieves the current time as an object with the timezone from settings. |
|
89 |
* |
|
90 |
* @since 5.3.0 |
|
91 |
* |
|
92 |
* @return DateTimeImmutable Date and time object. |
|
93 |
*/ |
|
94 |
function current_datetime() { |
|
95 |
return new DateTimeImmutable( 'now', wp_timezone() ); |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Retrieves the timezone from site settings as a string. |
|
100 |
* |
|
101 |
* Uses the `timezone_string` option to get a proper timezone if available, |
|
102 |
* otherwise falls back to an offset. |
|
103 |
* |
|
104 |
* @since 5.3.0 |
|
105 |
* |
|
106 |
* @return string PHP timezone string or a ±HH:MM offset. |
|
107 |
*/ |
|
108 |
function wp_timezone_string() { |
|
109 |
$timezone_string = get_option( 'timezone_string' ); |
|
110 |
||
111 |
if ( $timezone_string ) { |
|
112 |
return $timezone_string; |
|
113 |
} |
|
114 |
||
115 |
$offset = (float) get_option( 'gmt_offset' ); |
|
116 |
$hours = (int) $offset; |
|
117 |
$minutes = ( $offset - $hours ); |
|
118 |
||
119 |
$sign = ( $offset < 0 ) ? '-' : '+'; |
|
120 |
$abs_hour = abs( $hours ); |
|
121 |
$abs_mins = abs( $minutes * 60 ); |
|
122 |
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
|
123 |
||
124 |
return $tz_offset; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Retrieves the timezone from site settings as a `DateTimeZone` object. |
|
129 |
* |
|
130 |
* Timezone can be based on a PHP timezone string or a ±HH:MM offset. |
|
131 |
* |
|
132 |
* @since 5.3.0 |
|
133 |
* |
|
134 |
* @return DateTimeZone Timezone object. |
|
135 |
*/ |
|
136 |
function wp_timezone() { |
|
137 |
return new DateTimeZone( wp_timezone_string() ); |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* Retrieves the date in localized format, based on a sum of Unix timestamp and |
|
9 | 142 |
* timezone offset in seconds. |
0 | 143 |
* |
144 |
* If the locale specifies the locale month and weekday, then the locale will |
|
145 |
* take over the format for the date. If it isn't, then the date format string |
|
146 |
* will be used instead. |
|
147 |
* |
|
16 | 148 |
* Note that due to the way WP typically generates a sum of timestamp and offset |
149 |
* with `strtotime()`, it implies offset added at a _current_ time, not at the time |
|
150 |
* the timestamp represents. Storing such timestamps or calculating them differently |
|
151 |
* will lead to invalid output. |
|
152 |
* |
|
0 | 153 |
* @since 0.71 |
16 | 154 |
* @since 5.3.0 Converted into a wrapper for wp_date(). |
155 |
* |
|
156 |
* @global WP_Locale $wp_locale WordPress date and time locale object. |
|
157 |
* |
|
158 |
* @param string $format Format to display the date. |
|
159 |
* @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset |
|
160 |
* in seconds. Default false. |
|
161 |
* @param bool $gmt Optional. Whether to use GMT timezone. Only applies |
|
162 |
* if timestamp is not provided. Default false. |
|
0 | 163 |
* @return string The date, translated if locale specifies it. |
164 |
*/ |
|
16 | 165 |
function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) { |
166 |
$timestamp = $timestamp_with_offset; |
|
167 |
||
168 |
// If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true). |
|
169 |
if ( ! is_numeric( $timestamp ) ) { |
|
170 |
$timestamp = current_time( 'timestamp', $gmt ); |
|
0 | 171 |
} |
172 |
||
5 | 173 |
/* |
16 | 174 |
* This is a legacy implementation quirk that the returned timestamp is also with offset. |
175 |
* Ideally this function should never be used to produce a timestamp. |
|
5 | 176 |
*/ |
16 | 177 |
if ( 'U' === $format ) { |
178 |
$date = $timestamp; |
|
179 |
} elseif ( $gmt && false === $timestamp_with_offset ) { // Current time in UTC. |
|
180 |
$date = wp_date( $format, null, new DateTimeZone( 'UTC' ) ); |
|
181 |
} elseif ( false === $timestamp_with_offset ) { // Current time in site's timezone. |
|
182 |
$date = wp_date( $format ); |
|
183 |
} else { |
|
184 |
/* |
|
185 |
* Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone. |
|
186 |
* This is the best attempt to reverse that operation into a local time to use. |
|
187 |
*/ |
|
188 |
$local_time = gmdate( 'Y-m-d H:i:s', $timestamp ); |
|
189 |
$timezone = wp_timezone(); |
|
190 |
$datetime = date_create( $local_time, $timezone ); |
|
191 |
$date = wp_date( $format, $datetime->getTimestamp(), $timezone ); |
|
192 |
} |
|
5 | 193 |
|
194 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* Filters the date formatted based on the locale. |
5 | 196 |
* |
197 |
* @since 2.8.0 |
|
198 |
* |
|
16 | 199 |
* @param string $date Formatted date string. |
200 |
* @param string $format Format to display the date. |
|
201 |
* @param int $timestamp A sum of Unix timestamp and timezone offset in seconds. |
|
202 |
* Might be without offset if input omitted timestamp but requested GMT. |
|
203 |
* @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was not provided. |
|
204 |
* Default false. |
|
5 | 205 |
*/ |
16 | 206 |
$date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt ); |
207 |
||
208 |
return $date; |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Retrieves the date, in localized format. |
|
213 |
* |
|
214 |
* This is a newer function, intended to replace `date_i18n()` without legacy quirks in it. |
|
215 |
* |
|
216 |
* Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed |
|
217 |
* with timezone offset. |
|
218 |
* |
|
219 |
* @since 5.3.0 |
|
220 |
* |
|
221 |
* @param string $format PHP date format. |
|
222 |
* @param int $timestamp Optional. Unix timestamp. Defaults to current time. |
|
223 |
* @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone |
|
224 |
* from site settings. |
|
225 |
* @return string|false The date, translated if locale specifies it. False on invalid timestamp input. |
|
226 |
*/ |
|
227 |
function wp_date( $format, $timestamp = null, $timezone = null ) { |
|
228 |
global $wp_locale; |
|
229 |
||
230 |
if ( null === $timestamp ) { |
|
231 |
$timestamp = time(); |
|
232 |
} elseif ( ! is_numeric( $timestamp ) ) { |
|
233 |
return false; |
|
234 |
} |
|
235 |
||
236 |
if ( ! $timezone ) { |
|
237 |
$timezone = wp_timezone(); |
|
238 |
} |
|
239 |
||
240 |
$datetime = date_create( '@' . $timestamp ); |
|
241 |
$datetime->setTimezone( $timezone ); |
|
242 |
||
243 |
if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) { |
|
244 |
$date = $datetime->format( $format ); |
|
245 |
} else { |
|
246 |
// We need to unpack shorthand `r` format because it has parts that might be localized. |
|
247 |
$format = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $format ); |
|
248 |
||
249 |
$new_format = ''; |
|
250 |
$format_length = strlen( $format ); |
|
251 |
$month = $wp_locale->get_month( $datetime->format( 'm' ) ); |
|
252 |
$weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) ); |
|
253 |
||
254 |
for ( $i = 0; $i < $format_length; $i ++ ) { |
|
255 |
switch ( $format[ $i ] ) { |
|
256 |
case 'D': |
|
257 |
$new_format .= addcslashes( $wp_locale->get_weekday_abbrev( $weekday ), '\\A..Za..z' ); |
|
258 |
break; |
|
259 |
case 'F': |
|
260 |
$new_format .= addcslashes( $month, '\\A..Za..z' ); |
|
261 |
break; |
|
262 |
case 'l': |
|
263 |
$new_format .= addcslashes( $weekday, '\\A..Za..z' ); |
|
264 |
break; |
|
265 |
case 'M': |
|
266 |
$new_format .= addcslashes( $wp_locale->get_month_abbrev( $month ), '\\A..Za..z' ); |
|
267 |
break; |
|
268 |
case 'a': |
|
269 |
$new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'a' ) ), '\\A..Za..z' ); |
|
270 |
break; |
|
271 |
case 'A': |
|
272 |
$new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'A' ) ), '\\A..Za..z' ); |
|
273 |
break; |
|
274 |
case '\\': |
|
275 |
$new_format .= $format[ $i ]; |
|
276 |
||
277 |
// If character follows a slash, we add it without translating. |
|
278 |
if ( $i < $format_length ) { |
|
279 |
$new_format .= $format[ ++$i ]; |
|
280 |
} |
|
281 |
break; |
|
282 |
default: |
|
283 |
$new_format .= $format[ $i ]; |
|
284 |
break; |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
$date = $datetime->format( $new_format ); |
|
289 |
$date = wp_maybe_decline_date( $date, $format ); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Filters the date formatted based on the locale. |
|
294 |
* |
|
295 |
* @since 5.3.0 |
|
296 |
* |
|
297 |
* @param string $date Formatted date string. |
|
298 |
* @param string $format Format to display the date. |
|
299 |
* @param int $timestamp Unix timestamp. |
|
300 |
* @param DateTimeZone $timezone Timezone. |
|
301 |
*/ |
|
302 |
$date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone ); |
|
303 |
||
304 |
return $date; |
|
0 | 305 |
} |
306 |
||
307 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* Determines if the date should be declined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
* If the locale specifies that month names require a genitive case in certain |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
* formats (like 'j F Y'), the month name will be replaced with a correct form. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
* @since 4.4.0 |
16 | 314 |
* @since 5.4.0 The `$format` parameter was added. |
315 |
* |
|
316 |
* @global WP_Locale $wp_locale WordPress date and time locale object. |
|
317 |
* |
|
318 |
* @param string $date Formatted date string. |
|
319 |
* @param string $format Optional. Date format to check. Default empty string. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* @return string The date, declined if locale specifies it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
*/ |
16 | 322 |
function wp_maybe_decline_date( $date, $format = '' ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
global $wp_locale; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
|
16 | 325 |
// i18n functions are not available in SHORTINIT mode. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
if ( ! function_exists( '_x' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
|
16 | 330 |
/* |
331 |
* translators: If months in your language require a genitive case, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* translate this to 'on'. Do not translate into your own language. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) { |
16 | 335 |
|
336 |
$months = $wp_locale->month; |
|
337 |
$months_genitive = $wp_locale->month_genitive; |
|
338 |
||
339 |
/* |
|
340 |
* Match a format like 'j F Y' or 'j. F' (day of the month, followed by month name) |
|
341 |
* and decline the month. |
|
342 |
*/ |
|
343 |
if ( $format ) { |
|
344 |
$decline = preg_match( '#[dj]\.? F#', $format ); |
|
345 |
} else { |
|
346 |
// If the format is not passed, try to guess it from the date string. |
|
347 |
$decline = preg_match( '#\b\d{1,2}\.? [^\d ]+\b#u', $date ); |
|
348 |
} |
|
349 |
||
350 |
if ( $decline ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
foreach ( $months as $key => $month ) { |
16 | 352 |
$months[ $key ] = '# ' . preg_quote( $month, '#' ) . '\b#u'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
foreach ( $months_genitive as $key => $month ) { |
16 | 356 |
$months_genitive[ $key ] = ' ' . $month; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
$date = preg_replace( $months, $months_genitive, $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
} |
16 | 361 |
|
362 |
/* |
|
363 |
* Match a format like 'F jS' or 'F j' (month name, followed by day with an optional ordinal suffix) |
|
364 |
* and change it to declined 'j F'. |
|
365 |
*/ |
|
366 |
if ( $format ) { |
|
367 |
$decline = preg_match( '#F [dj]#', $format ); |
|
368 |
} else { |
|
369 |
// If the format is not passed, try to guess it from the date string. |
|
370 |
$decline = preg_match( '#\b[^\d ]+ \d{1,2}(st|nd|rd|th)?\b#u', trim( $date ) ); |
|
371 |
} |
|
372 |
||
373 |
if ( $decline ) { |
|
374 |
foreach ( $months as $key => $month ) { |
|
375 |
$months[ $key ] = '#\b' . preg_quote( $month, '#' ) . ' (\d{1,2})(st|nd|rd|th)?([-–]\d{1,2})?(st|nd|rd|th)?\b#u'; |
|
376 |
} |
|
377 |
||
378 |
foreach ( $months_genitive as $key => $month ) { |
|
379 |
$months_genitive[ $key ] = '$1$3 ' . $month; |
|
380 |
} |
|
381 |
||
382 |
$date = preg_replace( $months, $months_genitive, $date ); |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
// Used for locale-specific rules. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
$locale = get_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
388 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
389 |
if ( 'ca' === $locale ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
// " de abril| de agost| de octubre..." -> " d'abril| d'agost| d'octubre..." |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
$date = preg_replace( '# de ([ao])#i', " d'\\1", $date ); |
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 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* Convert float number to format based on the locale. |
0 | 399 |
* |
400 |
* @since 2.3.0 |
|
401 |
* |
|
16 | 402 |
* @global WP_Locale $wp_locale WordPress date and time locale object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
* @param float $number The number to convert based on locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
* @param int $decimals Optional. Precision of the number of decimal places. Default 0. |
0 | 406 |
* @return string Converted number in string format. |
407 |
*/ |
|
408 |
function number_format_i18n( $number, $decimals = 0 ) { |
|
409 |
global $wp_locale; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
if ( isset( $wp_locale ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
$formatted = number_format( $number, absint( $decimals ), $wp_locale->number_format['decimal_point'], $wp_locale->number_format['thousands_sep'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
$formatted = number_format( $number, absint( $decimals ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
} |
5 | 416 |
|
417 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
* Filters the number formatted based on the locale. |
5 | 419 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
* @since 2.8.0 |
9 | 421 |
* @since 4.9.0 The `$number` and `$decimals` parameters were added. |
5 | 422 |
* |
423 |
* @param string $formatted Converted number in string format. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
* @param float $number The number to convert based on locale. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
* @param int $decimals Precision of the number of decimal places. |
5 | 426 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
return apply_filters( 'number_format_i18n', $formatted, $number, $decimals ); |
0 | 428 |
} |
429 |
||
430 |
/** |
|
431 |
* Convert number of bytes largest unit bytes will fit into. |
|
432 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts |
0 | 434 |
* number of bytes to human readable number by taking the number of that unit |
435 |
* that the bytes will go into it. Supports TB value. |
|
436 |
* |
|
437 |
* Please note that integers in PHP are limited to 32 bits, unless they are on |
|
438 |
* 64 bit architecture, then they have 64 bit size. If you need to place the |
|
439 |
* larger size then what PHP integer type will hold, then use a string. It will |
|
440 |
* be converted to a double, which should always have 64 bit length. |
|
441 |
* |
|
442 |
* Technically the correct unit names for powers of 1024 are KiB, MiB etc. |
|
443 |
* |
|
444 |
* @since 2.3.0 |
|
445 |
* |
|
5 | 446 |
* @param int|string $bytes Number of bytes. Note max integer size for integers. |
447 |
* @param int $decimals Optional. Precision of number of decimal places. Default 0. |
|
16 | 448 |
* @return string|false Number string on success, false on failure. |
0 | 449 |
*/ |
450 |
function size_format( $bytes, $decimals = 0 ) { |
|
451 |
$quant = array( |
|
16 | 452 |
/* translators: Unit symbol for terabyte. */ |
453 |
_x( 'TB', 'unit symbol' ) => TB_IN_BYTES, |
|
454 |
/* translators: Unit symbol for gigabyte. */ |
|
455 |
_x( 'GB', 'unit symbol' ) => GB_IN_BYTES, |
|
456 |
/* translators: Unit symbol for megabyte. */ |
|
457 |
_x( 'MB', 'unit symbol' ) => MB_IN_BYTES, |
|
458 |
/* translators: Unit symbol for kilobyte. */ |
|
459 |
_x( 'KB', 'unit symbol' ) => KB_IN_BYTES, |
|
460 |
/* translators: Unit symbol for byte. */ |
|
461 |
_x( 'B', 'unit symbol' ) => 1, |
|
0 | 462 |
); |
5 | 463 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
if ( 0 === $bytes ) { |
16 | 465 |
/* translators: Unit symbol for byte. */ |
466 |
return number_format_i18n( 0, $decimals ) . ' ' . _x( 'B', 'unit symbol' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
|
5 | 469 |
foreach ( $quant as $unit => $mag ) { |
470 |
if ( doubleval( $bytes ) >= $mag ) { |
|
0 | 471 |
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; |
5 | 472 |
} |
473 |
} |
|
0 | 474 |
|
475 |
return false; |
|
476 |
} |
|
477 |
||
478 |
/** |
|
9 | 479 |
* Convert a duration to human readable format. |
480 |
* |
|
481 |
* @since 5.1.0 |
|
482 |
* |
|
483 |
* @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss), |
|
484 |
* with a possible prepended negative sign (-). |
|
485 |
* @return string|false A human readable duration string, false on failure. |
|
486 |
*/ |
|
487 |
function human_readable_duration( $duration = '' ) { |
|
488 |
if ( ( empty( $duration ) || ! is_string( $duration ) ) ) { |
|
489 |
return false; |
|
490 |
} |
|
491 |
||
492 |
$duration = trim( $duration ); |
|
493 |
||
494 |
// Remove prepended negative sign. |
|
495 |
if ( '-' === substr( $duration, 0, 1 ) ) { |
|
496 |
$duration = substr( $duration, 1 ); |
|
497 |
} |
|
498 |
||
499 |
// Extract duration parts. |
|
500 |
$duration_parts = array_reverse( explode( ':', $duration ) ); |
|
501 |
$duration_count = count( $duration_parts ); |
|
502 |
||
503 |
$hour = null; |
|
504 |
$minute = null; |
|
505 |
$second = null; |
|
506 |
||
507 |
if ( 3 === $duration_count ) { |
|
508 |
// Validate HH:ii:ss duration format. |
|
509 |
if ( ! ( (bool) preg_match( '/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) { |
|
510 |
return false; |
|
511 |
} |
|
512 |
// Three parts: hours, minutes & seconds. |
|
513 |
list( $second, $minute, $hour ) = $duration_parts; |
|
514 |
} elseif ( 2 === $duration_count ) { |
|
515 |
// Validate ii:ss duration format. |
|
516 |
if ( ! ( (bool) preg_match( '/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) { |
|
517 |
return false; |
|
518 |
} |
|
519 |
// Two parts: minutes & seconds. |
|
520 |
list( $second, $minute ) = $duration_parts; |
|
521 |
} else { |
|
522 |
return false; |
|
523 |
} |
|
524 |
||
525 |
$human_readable_duration = array(); |
|
526 |
||
527 |
// Add the hour part to the string. |
|
528 |
if ( is_numeric( $hour ) ) { |
|
16 | 529 |
/* translators: %s: Time duration in hour or hours. */ |
9 | 530 |
$human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour ); |
531 |
} |
|
532 |
||
533 |
// Add the minute part to the string. |
|
534 |
if ( is_numeric( $minute ) ) { |
|
16 | 535 |
/* translators: %s: Time duration in minute or minutes. */ |
9 | 536 |
$human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute ); |
537 |
} |
|
538 |
||
539 |
// Add the second part to the string. |
|
540 |
if ( is_numeric( $second ) ) { |
|
16 | 541 |
/* translators: %s: Time duration in second or seconds. */ |
9 | 542 |
$human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second ); |
543 |
} |
|
544 |
||
545 |
return implode( ', ', $human_readable_duration ); |
|
546 |
} |
|
547 |
||
548 |
/** |
|
5 | 549 |
* Get the week start and end from the datetime or date string from MySQL. |
0 | 550 |
* |
551 |
* @since 0.71 |
|
552 |
* |
|
5 | 553 |
* @param string $mysqlstring Date or datetime field type from MySQL. |
554 |
* @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string. |
|
0 | 555 |
* @return array Keys are 'start' and 'end'. |
556 |
*/ |
|
557 |
function get_weekstartend( $mysqlstring, $start_of_week = '' ) { |
|
5 | 558 |
// MySQL string year. |
559 |
$my = substr( $mysqlstring, 0, 4 ); |
|
560 |
||
561 |
// MySQL string month. |
|
562 |
$mm = substr( $mysqlstring, 8, 2 ); |
|
563 |
||
564 |
// MySQL string day. |
|
565 |
$md = substr( $mysqlstring, 5, 2 ); |
|
566 |
||
567 |
// The timestamp for MySQL string day. |
|
568 |
$day = mktime( 0, 0, 0, $md, $mm, $my ); |
|
569 |
||
570 |
// The day of the week from the timestamp. |
|
16 | 571 |
$weekday = gmdate( 'w', $day ); |
5 | 572 |
|
9 | 573 |
if ( ! is_numeric( $start_of_week ) ) { |
0 | 574 |
$start_of_week = get_option( 'start_of_week' ); |
9 | 575 |
} |
576 |
||
577 |
if ( $weekday < $start_of_week ) { |
|
0 | 578 |
$weekday += 7; |
9 | 579 |
} |
0 | 580 |
|
5 | 581 |
// The most recent week start day on or before $day. |
582 |
$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week ); |
|
583 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
// $start + 1 week - 1 second. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
$end = $start + WEEK_IN_SECONDS - 1; |
0 | 586 |
return compact( 'start', 'end' ); |
587 |
} |
|
588 |
||
589 |
/** |
|
16 | 590 |
* Serialize data, if needed. |
591 |
* |
|
592 |
* @since 2.0.5 |
|
593 |
* |
|
594 |
* @param string|array|object $data Data that might be serialized. |
|
595 |
* @return mixed A scalar data. |
|
596 |
*/ |
|
597 |
function maybe_serialize( $data ) { |
|
598 |
if ( is_array( $data ) || is_object( $data ) ) { |
|
599 |
return serialize( $data ); |
|
600 |
} |
|
601 |
||
602 |
/* |
|
603 |
* Double serialization is required for backward compatibility. |
|
604 |
* See https://core.trac.wordpress.org/ticket/12930 |
|
605 |
* Also the world will end. See WP 3.6.1. |
|
606 |
*/ |
|
607 |
if ( is_serialized( $data, false ) ) { |
|
608 |
return serialize( $data ); |
|
609 |
} |
|
610 |
||
611 |
return $data; |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Unserialize data only if it was serialized. |
|
0 | 616 |
* |
617 |
* @since 2.0.0 |
|
618 |
* |
|
16 | 619 |
* @param string $data Data that might be unserialized. |
0 | 620 |
* @return mixed Unserialized data can be any type. |
621 |
*/ |
|
16 | 622 |
function maybe_unserialize( $data ) { |
623 |
if ( is_serialized( $data ) ) { // Don't attempt to unserialize data that wasn't serialized going in. |
|
624 |
return @unserialize( trim( $data ) ); |
|
625 |
} |
|
626 |
||
627 |
return $data; |
|
0 | 628 |
} |
629 |
||
630 |
/** |
|
631 |
* Check value to find if it was serialized. |
|
632 |
* |
|
633 |
* If $data is not an string, then returned value will always be false. |
|
634 |
* Serialized data is always a string. |
|
635 |
* |
|
636 |
* @since 2.0.5 |
|
637 |
* |
|
5 | 638 |
* @param string $data Value to check to see if was serialized. |
639 |
* @param bool $strict Optional. Whether to be strict about the end of the string. Default true. |
|
0 | 640 |
* @return bool False if not serialized and true if it was. |
641 |
*/ |
|
642 |
function is_serialized( $data, $strict = true ) { |
|
16 | 643 |
// If it isn't a string, it isn't serialized. |
5 | 644 |
if ( ! is_string( $data ) ) { |
0 | 645 |
return false; |
5 | 646 |
} |
0 | 647 |
$data = trim( $data ); |
16 | 648 |
if ( 'N;' === $data ) { |
0 | 649 |
return true; |
5 | 650 |
} |
651 |
if ( strlen( $data ) < 4 ) { |
|
0 | 652 |
return false; |
5 | 653 |
} |
654 |
if ( ':' !== $data[1] ) { |
|
0 | 655 |
return false; |
5 | 656 |
} |
0 | 657 |
if ( $strict ) { |
5 | 658 |
$lastc = substr( $data, -1 ); |
659 |
if ( ';' !== $lastc && '}' !== $lastc ) { |
|
0 | 660 |
return false; |
5 | 661 |
} |
0 | 662 |
} else { |
663 |
$semicolon = strpos( $data, ';' ); |
|
664 |
$brace = strpos( $data, '}' ); |
|
665 |
// Either ; or } must exist. |
|
9 | 666 |
if ( false === $semicolon && false === $brace ) { |
0 | 667 |
return false; |
9 | 668 |
} |
0 | 669 |
// But neither must be in the first X characters. |
9 | 670 |
if ( false !== $semicolon && $semicolon < 3 ) { |
0 | 671 |
return false; |
9 | 672 |
} |
673 |
if ( false !== $brace && $brace < 4 ) { |
|
0 | 674 |
return false; |
9 | 675 |
} |
0 | 676 |
} |
677 |
$token = $data[0]; |
|
678 |
switch ( $token ) { |
|
9 | 679 |
case 's': |
0 | 680 |
if ( $strict ) { |
5 | 681 |
if ( '"' !== substr( $data, -2, 1 ) ) { |
0 | 682 |
return false; |
5 | 683 |
} |
0 | 684 |
} elseif ( false === strpos( $data, '"' ) ) { |
685 |
return false; |
|
686 |
} |
|
16 | 687 |
// Or else fall through. |
9 | 688 |
case 'a': |
689 |
case 'O': |
|
0 | 690 |
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); |
9 | 691 |
case 'b': |
692 |
case 'i': |
|
693 |
case 'd': |
|
0 | 694 |
$end = $strict ? '$' : ''; |
16 | 695 |
return (bool) preg_match( "/^{$token}:[0-9.E+-]+;$end/", $data ); |
0 | 696 |
} |
697 |
return false; |
|
698 |
} |
|
699 |
||
700 |
/** |
|
701 |
* Check whether serialized data is of string type. |
|
702 |
* |
|
703 |
* @since 2.0.5 |
|
704 |
* |
|
5 | 705 |
* @param string $data Serialized data. |
0 | 706 |
* @return bool False if not a serialized string, true if it is. |
707 |
*/ |
|
708 |
function is_serialized_string( $data ) { |
|
5 | 709 |
// if it isn't a string, it isn't a serialized string. |
710 |
if ( ! is_string( $data ) ) { |
|
0 | 711 |
return false; |
5 | 712 |
} |
0 | 713 |
$data = trim( $data ); |
5 | 714 |
if ( strlen( $data ) < 4 ) { |
0 | 715 |
return false; |
5 | 716 |
} elseif ( ':' !== $data[1] ) { |
0 | 717 |
return false; |
5 | 718 |
} elseif ( ';' !== substr( $data, -1 ) ) { |
0 | 719 |
return false; |
16 | 720 |
} elseif ( 's' !== $data[0] ) { |
0 | 721 |
return false; |
5 | 722 |
} elseif ( '"' !== substr( $data, -2, 1 ) ) { |
0 | 723 |
return false; |
5 | 724 |
} else { |
0 | 725 |
return true; |
5 | 726 |
} |
0 | 727 |
} |
728 |
||
729 |
/** |
|
730 |
* Retrieve post title from XMLRPC XML. |
|
731 |
* |
|
732 |
* If the title element is not part of the XML, then the default post title from |
|
733 |
* the $post_default_title will be used instead. |
|
734 |
* |
|
735 |
* @since 0.71 |
|
736 |
* |
|
5 | 737 |
* @global string $post_default_title Default XML-RPC post title. |
0 | 738 |
* |
739 |
* @param string $content XMLRPC XML Request content |
|
740 |
* @return string Post title |
|
741 |
*/ |
|
742 |
function xmlrpc_getposttitle( $content ) { |
|
743 |
global $post_default_title; |
|
744 |
if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) { |
|
745 |
$post_title = $matchtitle[1]; |
|
746 |
} else { |
|
747 |
$post_title = $post_default_title; |
|
748 |
} |
|
749 |
return $post_title; |
|
750 |
} |
|
751 |
||
752 |
/** |
|
753 |
* Retrieve the post category or categories from XMLRPC XML. |
|
754 |
* |
|
755 |
* If the category element is not found, then the default post category will be |
|
756 |
* used. The return type then would be what $post_default_category. If the |
|
757 |
* category is found, then it will always be an array. |
|
758 |
* |
|
759 |
* @since 0.71 |
|
760 |
* |
|
5 | 761 |
* @global string $post_default_category Default XML-RPC post category. |
0 | 762 |
* |
763 |
* @param string $content XMLRPC XML Request content |
|
764 |
* @return string|array List of categories or category name. |
|
765 |
*/ |
|
766 |
function xmlrpc_getpostcategory( $content ) { |
|
767 |
global $post_default_category; |
|
768 |
if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) { |
|
769 |
$post_category = trim( $matchcat[1], ',' ); |
|
770 |
$post_category = explode( ',', $post_category ); |
|
771 |
} else { |
|
772 |
$post_category = $post_default_category; |
|
773 |
} |
|
774 |
return $post_category; |
|
775 |
} |
|
776 |
||
777 |
/** |
|
778 |
* XMLRPC XML content without title and category elements. |
|
779 |
* |
|
780 |
* @since 0.71 |
|
781 |
* |
|
5 | 782 |
* @param string $content XML-RPC XML Request content. |
0 | 783 |
* @return string XMLRPC XML Request content without title and category elements. |
784 |
*/ |
|
785 |
function xmlrpc_removepostdata( $content ) { |
|
786 |
$content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content ); |
|
787 |
$content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content ); |
|
788 |
$content = trim( $content ); |
|
789 |
return $content; |
|
790 |
} |
|
791 |
||
792 |
/** |
|
5 | 793 |
* Use RegEx to extract URLs from arbitrary content. |
0 | 794 |
* |
795 |
* @since 3.7.0 |
|
796 |
* |
|
5 | 797 |
* @param string $content Content to extract URLs from. |
16 | 798 |
* @return string[] Array of URLs found in passed string. |
0 | 799 |
*/ |
800 |
function wp_extract_urls( $content ) { |
|
801 |
preg_match_all( |
|
5 | 802 |
"#([\"']?)(" |
9 | 803 |
. '(?:([\w-]+:)?//?)' |
804 |
. '[^\s()<>]+' |
|
805 |
. '[.]' |
|
806 |
. '(?:' |
|
807 |
. '\([\w\d]+\)|' |
|
808 |
. '(?:' |
|
5 | 809 |
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|" |
9 | 810 |
. '(?:[:]\d+)?/?' |
811 |
. ')+' |
|
812 |
. ')' |
|
5 | 813 |
. ")\\1#", |
0 | 814 |
$content, |
815 |
$post_links |
|
816 |
); |
|
817 |
||
5 | 818 |
$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) ); |
0 | 819 |
|
820 |
return array_values( $post_links ); |
|
821 |
} |
|
822 |
||
823 |
/** |
|
824 |
* Check content for video and audio links to add as enclosures. |
|
825 |
* |
|
826 |
* Will not add enclosures that have already been added and will |
|
827 |
* remove enclosures that are no longer in the post. This is called as |
|
828 |
* pingbacks and trackbacks. |
|
829 |
* |
|
830 |
* @since 1.5.0 |
|
16 | 831 |
* @since 5.3.0 The `$content` parameter was made optional, and the `$post` parameter was |
832 |
* updated to accept a post ID or a WP_Post object. |
|
0 | 833 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
* @global wpdb $wpdb WordPress database abstraction object. |
5 | 835 |
* |
16 | 836 |
* @param string $content Post content. If `null`, the `post_content` field from `$post` is used. |
837 |
* @param int|WP_Post $post Post ID or post object. |
|
838 |
* @return null|bool Returns false if post is not found. |
|
839 |
*/ |
|
840 |
function do_enclose( $content = null, $post ) { |
|
0 | 841 |
global $wpdb; |
842 |
||
16 | 843 |
// @todo Tidy this code and make the debug code optional. |
844 |
include_once ABSPATH . WPINC . '/class-IXR.php'; |
|
845 |
||
846 |
$post = get_post( $post ); |
|
847 |
if ( ! $post ) { |
|
848 |
return false; |
|
849 |
} |
|
850 |
||
851 |
if ( null === $content ) { |
|
852 |
$content = $post->post_content; |
|
853 |
} |
|
0 | 854 |
|
855 |
$post_links = array(); |
|
856 |
||
16 | 857 |
$pung = get_enclosed( $post->ID ); |
0 | 858 |
|
859 |
$post_links_temp = wp_extract_urls( $content ); |
|
860 |
||
861 |
foreach ( $pung as $link_test ) { |
|
16 | 862 |
// Link is no longer in post. |
863 |
if ( ! in_array( $link_test, $post_links_temp, true ) ) { |
|
864 |
$mids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like( $link_test ) . '%' ) ); |
|
9 | 865 |
foreach ( $mids as $mid ) { |
0 | 866 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 867 |
} |
0 | 868 |
} |
869 |
} |
|
870 |
||
871 |
foreach ( (array) $post_links_temp as $link_test ) { |
|
16 | 872 |
// If we haven't pung it already. |
873 |
if ( ! in_array( $link_test, $pung, true ) ) { |
|
874 |
$test = parse_url( $link_test ); |
|
9 | 875 |
if ( false === $test ) { |
0 | 876 |
continue; |
9 | 877 |
} |
878 |
if ( isset( $test['query'] ) ) { |
|
0 | 879 |
$post_links[] = $link_test; |
16 | 880 |
} elseif ( isset( $test['path'] ) && ( '/' !== $test['path'] ) && ( '' !== $test['path'] ) ) { |
0 | 881 |
$post_links[] = $link_test; |
9 | 882 |
} |
0 | 883 |
} |
884 |
} |
|
885 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
886 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
887 |
* Filters the list of enclosure links before querying the database. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
889 |
* Allows for the addition and/or removal of potential enclosures to save |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
* to postmeta before checking the database for existing enclosures. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
892 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
* |
16 | 894 |
* @param string[] $post_links An array of enclosure links. |
895 |
* @param int $post_ID Post ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
*/ |
16 | 897 |
$post_links = apply_filters( 'enclosure_links', $post_links, $post->ID ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
|
0 | 899 |
foreach ( (array) $post_links as $url ) { |
16 | 900 |
if ( '' !== $url && ! $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post->ID, $wpdb->esc_like( $url ) . '%' ) ) ) { |
901 |
||
902 |
$headers = wp_get_http_headers( $url ); |
|
903 |
if ( $headers ) { |
|
9 | 904 |
$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0; |
905 |
$type = isset( $headers['content-type'] ) ? $headers['content-type'] : ''; |
|
0 | 906 |
$allowed_types = array( 'video', 'audio' ); |
907 |
||
16 | 908 |
// Check to see if we can figure out the mime type from the extension. |
909 |
$url_parts = parse_url( $url ); |
|
910 |
if ( false !== $url_parts && ! empty( $url_parts['path'] ) ) { |
|
0 | 911 |
$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION ); |
9 | 912 |
if ( ! empty( $extension ) ) { |
0 | 913 |
foreach ( wp_get_mime_types() as $exts => $mime ) { |
914 |
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { |
|
915 |
$type = $mime; |
|
916 |
break; |
|
917 |
} |
|
918 |
} |
|
919 |
} |
|
920 |
} |
|
921 |
||
16 | 922 |
if ( in_array( substr( $type, 0, strpos( $type, '/' ) ), $allowed_types, true ) ) { |
923 |
add_post_meta( $post->ID, 'enclosure', "$url\n$len\n$mime\n" ); |
|
0 | 924 |
} |
925 |
} |
|
926 |
} |
|
927 |
} |
|
928 |
} |
|
929 |
||
930 |
/** |
|
931 |
* Retrieve HTTP Headers from URL. |
|
932 |
* |
|
933 |
* @since 1.5.1 |
|
934 |
* |
|
5 | 935 |
* @param string $url URL to retrieve HTTP headers from. |
936 |
* @param bool $deprecated Not Used. |
|
0 | 937 |
* @return bool|string False on failure, headers on success. |
938 |
*/ |
|
939 |
function wp_get_http_headers( $url, $deprecated = false ) { |
|
9 | 940 |
if ( ! empty( $deprecated ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
_deprecated_argument( __FUNCTION__, '2.7.0' ); |
9 | 942 |
} |
0 | 943 |
|
944 |
$response = wp_safe_remote_head( $url ); |
|
945 |
||
9 | 946 |
if ( is_wp_error( $response ) ) { |
0 | 947 |
return false; |
9 | 948 |
} |
0 | 949 |
|
950 |
return wp_remote_retrieve_headers( $response ); |
|
951 |
} |
|
952 |
||
953 |
/** |
|
9 | 954 |
* Determines whether the publish date of the current post in the loop is different |
955 |
* from the publish date of the previous post in the loop. |
|
956 |
* |
|
957 |
* For more information on this and similar theme functions, check out |
|
958 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
959 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 960 |
* |
961 |
* @since 0.71 |
|
5 | 962 |
* |
963 |
* @global string $currentday The day of the current post in the loop. |
|
964 |
* @global string $previousday The day of the previous post in the loop. |
|
0 | 965 |
* |
966 |
* @return int 1 when new day, 0 if not a new day. |
|
967 |
*/ |
|
968 |
function is_new_day() { |
|
969 |
global $currentday, $previousday; |
|
16 | 970 |
|
971 |
if ( $currentday !== $previousday ) { |
|
0 | 972 |
return 1; |
9 | 973 |
} else { |
0 | 974 |
return 0; |
9 | 975 |
} |
0 | 976 |
} |
977 |
||
978 |
/** |
|
979 |
* Build URL query based on an associative and, or indexed array. |
|
980 |
* |
|
981 |
* This is a convenient function for easily building url queries. It sets the |
|
982 |
* separator to '&' and uses _http_build_query() function. |
|
983 |
* |
|
5 | 984 |
* @since 2.3.0 |
985 |
* |
|
0 | 986 |
* @see _http_build_query() Used to build the query |
16 | 987 |
* @link https://www.php.net/manual/en/function.http-build-query.php for more on what |
9 | 988 |
* http_build_query() does. |
0 | 989 |
* |
990 |
* @param array $data URL-encode key/value pairs. |
|
5 | 991 |
* @return string URL-encoded string. |
0 | 992 |
*/ |
993 |
function build_query( $data ) { |
|
994 |
return _http_build_query( $data, null, '&', '', false ); |
|
995 |
} |
|
996 |
||
5 | 997 |
/** |
998 |
* From php.net (modified by Mark Jaquith to behave like the native PHP5 function). |
|
999 |
* |
|
1000 |
* @since 3.2.0 |
|
1001 |
* @access private |
|
1002 |
* |
|
16 | 1003 |
* @see https://www.php.net/manual/en/function.http-build-query.php |
1004 |
* |
|
1005 |
* @param array|object $data An array or object of data. Converted to array. |
|
1006 |
* @param string $prefix Optional. Numeric index. If set, start parameter numbering with it. |
|
1007 |
* Default null. |
|
1008 |
* @param string $sep Optional. Argument separator; defaults to 'arg_separator.output'. |
|
1009 |
* Default null. |
|
1010 |
* @param string $key Optional. Used to prefix key name. Default empty. |
|
1011 |
* @param bool $urlencode Optional. Whether to use urlencode() in the result. Default true. |
|
5 | 1012 |
* @return string The query string. |
1013 |
*/ |
|
1014 |
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) { |
|
0 | 1015 |
$ret = array(); |
1016 |
||
1017 |
foreach ( (array) $data as $k => $v ) { |
|
9 | 1018 |
if ( $urlencode ) { |
1019 |
$k = urlencode( $k ); |
|
1020 |
} |
|
16 | 1021 |
if ( is_int( $k ) && null != $prefix ) { |
9 | 1022 |
$k = $prefix . $k; |
1023 |
} |
|
1024 |
if ( ! empty( $key ) ) { |
|
0 | 1025 |
$k = $key . '%5B' . $k . '%5D'; |
9 | 1026 |
} |
16 | 1027 |
if ( null === $v ) { |
0 | 1028 |
continue; |
16 | 1029 |
} elseif ( false === $v ) { |
0 | 1030 |
$v = '0'; |
9 | 1031 |
} |
1032 |
||
1033 |
if ( is_array( $v ) || is_object( $v ) ) { |
|
1034 |
array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) ); |
|
1035 |
} elseif ( $urlencode ) { |
|
1036 |
array_push( $ret, $k . '=' . urlencode( $v ) ); |
|
1037 |
} else { |
|
1038 |
array_push( $ret, $k . '=' . $v ); |
|
1039 |
} |
|
1040 |
} |
|
1041 |
||
1042 |
if ( null === $sep ) { |
|
1043 |
$sep = ini_get( 'arg_separator.output' ); |
|
1044 |
} |
|
1045 |
||
1046 |
return implode( $sep, $ret ); |
|
0 | 1047 |
} |
1048 |
||
1049 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1050 |
* Retrieves a modified URL query string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1051 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1052 |
* You can rebuild the URL and append query variables to the URL query by using this function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1053 |
* There are two ways to use this function; either a single key and value, or an associative array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1055 |
* Using a single key and value: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1056 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1057 |
* add_query_arg( 'key', 'value', 'http://example.com' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1058 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1059 |
* Using an associative array: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1060 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
* add_query_arg( array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1062 |
* 'key1' => 'value1', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1063 |
* 'key2' => 'value2', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1064 |
* ), 'http://example.com' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1065 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
* Omitting the URL from either use results in the current URL being used |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1067 |
* (the value of `$_SERVER['REQUEST_URI']`). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1068 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1069 |
* Values are expected to be encoded appropriately with urlencode() or rawurlencode(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
* Setting any query variable's value to boolean false removes the key (see remove_query_arg()). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
* Important: The return value of add_query_arg() is not escaped by default. Output should be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
* late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
* (XSS) attacks. |
0 | 1076 |
* |
1077 |
* @since 1.5.0 |
|
16 | 1078 |
* @since 5.3.0 Formalized the existing and already documented parameters |
1079 |
* by adding `...$args` to the function signature. |
|
0 | 1080 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1081 |
* @param string|array $key Either a query variable key, or an associative array of query variables. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1082 |
* @param string $value Optional. Either a query variable value, or a URL to act upon. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1083 |
* @param string $url Optional. A URL to act upon. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
* @return string New URL query string (unescaped). |
0 | 1085 |
*/ |
16 | 1086 |
function add_query_arg( ...$args ) { |
0 | 1087 |
if ( is_array( $args[0] ) ) { |
9 | 1088 |
if ( count( $args ) < 2 || false === $args[1] ) { |
0 | 1089 |
$uri = $_SERVER['REQUEST_URI']; |
9 | 1090 |
} else { |
0 | 1091 |
$uri = $args[1]; |
9 | 1092 |
} |
0 | 1093 |
} else { |
9 | 1094 |
if ( count( $args ) < 3 || false === $args[2] ) { |
0 | 1095 |
$uri = $_SERVER['REQUEST_URI']; |
9 | 1096 |
} else { |
0 | 1097 |
$uri = $args[2]; |
9 | 1098 |
} |
1099 |
} |
|
1100 |
||
16 | 1101 |
$frag = strstr( $uri, '#' ); |
1102 |
if ( $frag ) { |
|
0 | 1103 |
$uri = substr( $uri, 0, -strlen( $frag ) ); |
9 | 1104 |
} else { |
0 | 1105 |
$frag = ''; |
9 | 1106 |
} |
0 | 1107 |
|
1108 |
if ( 0 === stripos( $uri, 'http://' ) ) { |
|
1109 |
$protocol = 'http://'; |
|
9 | 1110 |
$uri = substr( $uri, 7 ); |
0 | 1111 |
} elseif ( 0 === stripos( $uri, 'https://' ) ) { |
1112 |
$protocol = 'https://'; |
|
9 | 1113 |
$uri = substr( $uri, 8 ); |
0 | 1114 |
} else { |
1115 |
$protocol = ''; |
|
1116 |
} |
|
1117 |
||
1118 |
if ( strpos( $uri, '?' ) !== false ) { |
|
1119 |
list( $base, $query ) = explode( '?', $uri, 2 ); |
|
9 | 1120 |
$base .= '?'; |
0 | 1121 |
} elseif ( $protocol || strpos( $uri, '=' ) === false ) { |
9 | 1122 |
$base = $uri . '?'; |
0 | 1123 |
$query = ''; |
1124 |
} else { |
|
9 | 1125 |
$base = ''; |
0 | 1126 |
$query = $uri; |
1127 |
} |
|
1128 |
||
1129 |
wp_parse_str( $query, $qs ); |
|
16 | 1130 |
$qs = urlencode_deep( $qs ); // This re-URL-encodes things that were already in the query string. |
0 | 1131 |
if ( is_array( $args[0] ) ) { |
5 | 1132 |
foreach ( $args[0] as $k => $v ) { |
1133 |
$qs[ $k ] = $v; |
|
1134 |
} |
|
0 | 1135 |
} else { |
1136 |
$qs[ $args[0] ] = $args[1]; |
|
1137 |
} |
|
1138 |
||
1139 |
foreach ( $qs as $k => $v ) { |
|
16 | 1140 |
if ( false === $v ) { |
9 | 1141 |
unset( $qs[ $k ] ); |
1142 |
} |
|
0 | 1143 |
} |
1144 |
||
1145 |
$ret = build_query( $qs ); |
|
1146 |
$ret = trim( $ret, '?' ); |
|
1147 |
$ret = preg_replace( '#=(&|$)#', '$1', $ret ); |
|
1148 |
$ret = $protocol . $base . $ret . $frag; |
|
1149 |
$ret = rtrim( $ret, '?' ); |
|
1150 |
return $ret; |
|
1151 |
} |
|
1152 |
||
1153 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
* Removes an item or items from a query string. |
0 | 1155 |
* |
1156 |
* @since 1.5.0 |
|
1157 |
* |
|
5 | 1158 |
* @param string|array $key Query key or keys to remove. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
* @param bool|string $query Optional. When false uses the current URL. Default false. |
0 | 1160 |
* @return string New URL query string. |
1161 |
*/ |
|
5 | 1162 |
function remove_query_arg( $key, $query = false ) { |
16 | 1163 |
if ( is_array( $key ) ) { // Removing multiple keys. |
9 | 1164 |
foreach ( $key as $k ) { |
0 | 1165 |
$query = add_query_arg( $k, false, $query ); |
9 | 1166 |
} |
0 | 1167 |
return $query; |
1168 |
} |
|
1169 |
return add_query_arg( $key, false, $query ); |
|
1170 |
} |
|
1171 |
||
1172 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
* Returns an array of single-use query variable names that can be removed from a URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
* |
16 | 1177 |
* @return string[] An array of parameters to remove from the URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
function wp_removable_query_args() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
$removable_query_args = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
'activate', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
'activated', |
16 | 1183 |
'admin_email_remind_later', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
'approved', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
'deactivate', |
16 | 1186 |
'delete_count', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
'deleted', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
'disabled', |
16 | 1189 |
'doing_wp_cron', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
'enabled', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
'error', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
'hotkeys_highlight_first', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1193 |
'hotkeys_highlight_last', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
'locked', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
'message', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
'same', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
'saved', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
'settings-updated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
'skipped', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
'spammed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
'trashed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
'unspammed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
'untrashed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
'update', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
'updated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
'wp-post-new-reload', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
* Filters the list of query variables to remove. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
* |
16 | 1214 |
* @param string[] $removable_query_args An array of query variables to remove from a URL. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
return apply_filters( 'removable_query_args', $removable_query_args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
/** |
0 | 1220 |
* Walks the array while sanitizing the contents. |
1221 |
* |
|
1222 |
* @since 0.71 |
|
16 | 1223 |
* @since 5.5.0 Non-string values are left untouched. |
0 | 1224 |
* |
1225 |
* @param array $array Array to walk while sanitizing contents. |
|
1226 |
* @return array Sanitized $array. |
|
1227 |
*/ |
|
1228 |
function add_magic_quotes( $array ) { |
|
1229 |
foreach ( (array) $array as $k => $v ) { |
|
1230 |
if ( is_array( $v ) ) { |
|
9 | 1231 |
$array[ $k ] = add_magic_quotes( $v ); |
16 | 1232 |
} elseif ( is_string( $v ) ) { |
9 | 1233 |
$array[ $k ] = addslashes( $v ); |
16 | 1234 |
} else { |
1235 |
continue; |
|
0 | 1236 |
} |
1237 |
} |
|
16 | 1238 |
|
0 | 1239 |
return $array; |
1240 |
} |
|
1241 |
||
1242 |
/** |
|
1243 |
* HTTP request for URI to retrieve content. |
|
1244 |
* |
|
1245 |
* @since 1.5.1 |
|
5 | 1246 |
* |
1247 |
* @see wp_safe_remote_get() |
|
0 | 1248 |
* |
1249 |
* @param string $uri URI/URL of web page to retrieve. |
|
16 | 1250 |
* @return string|false HTTP content. False on failure. |
0 | 1251 |
*/ |
1252 |
function wp_remote_fopen( $uri ) { |
|
16 | 1253 |
$parsed_url = parse_url( $uri ); |
0 | 1254 |
|
9 | 1255 |
if ( ! $parsed_url || ! is_array( $parsed_url ) ) { |
0 | 1256 |
return false; |
9 | 1257 |
} |
1258 |
||
1259 |
$options = array(); |
|
0 | 1260 |
$options['timeout'] = 10; |
1261 |
||
1262 |
$response = wp_safe_remote_get( $uri, $options ); |
|
1263 |
||
9 | 1264 |
if ( is_wp_error( $response ) ) { |
0 | 1265 |
return false; |
9 | 1266 |
} |
0 | 1267 |
|
1268 |
return wp_remote_retrieve_body( $response ); |
|
1269 |
} |
|
1270 |
||
1271 |
/** |
|
1272 |
* Set up the WordPress query. |
|
1273 |
* |
|
1274 |
* @since 2.0.0 |
|
1275 |
* |
|
16 | 1276 |
* @global WP $wp Current WordPress environment instance. |
1277 |
* @global WP_Query $wp_query WordPress Query object. |
|
1278 |
* @global WP_Query $wp_the_query Copy of the WordPress Query object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* |
5 | 1280 |
* @param string|array $query_vars Default WP_Query arguments. |
0 | 1281 |
*/ |
1282 |
function wp( $query_vars = '' ) { |
|
1283 |
global $wp, $wp_query, $wp_the_query; |
|
16 | 1284 |
|
0 | 1285 |
$wp->main( $query_vars ); |
1286 |
||
9 | 1287 |
if ( ! isset( $wp_the_query ) ) { |
0 | 1288 |
$wp_the_query = $wp_query; |
9 | 1289 |
} |
0 | 1290 |
} |
1291 |
||
1292 |
/** |
|
1293 |
* Retrieve the description for the HTTP status. |
|
1294 |
* |
|
1295 |
* @since 2.3.0 |
|
9 | 1296 |
* @since 3.9.0 Added status codes 418, 428, 429, 431, and 511. |
1297 |
* @since 4.5.0 Added status codes 308, 421, and 451. |
|
1298 |
* @since 5.1.0 Added status code 103. |
|
0 | 1299 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
* @global array $wp_header_to_desc |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
* |
0 | 1302 |
* @param int $code HTTP status code. |
16 | 1303 |
* @return string Status description if found, an empty string otherwise. |
0 | 1304 |
*/ |
1305 |
function get_status_header_desc( $code ) { |
|
1306 |
global $wp_header_to_desc; |
|
1307 |
||
1308 |
$code = absint( $code ); |
|
1309 |
||
9 | 1310 |
if ( ! isset( $wp_header_to_desc ) ) { |
0 | 1311 |
$wp_header_to_desc = array( |
1312 |
100 => 'Continue', |
|
1313 |
101 => 'Switching Protocols', |
|
1314 |
102 => 'Processing', |
|
9 | 1315 |
103 => 'Early Hints', |
0 | 1316 |
|
1317 |
200 => 'OK', |
|
1318 |
201 => 'Created', |
|
1319 |
202 => 'Accepted', |
|
1320 |
203 => 'Non-Authoritative Information', |
|
1321 |
204 => 'No Content', |
|
1322 |
205 => 'Reset Content', |
|
1323 |
206 => 'Partial Content', |
|
1324 |
207 => 'Multi-Status', |
|
1325 |
226 => 'IM Used', |
|
1326 |
||
1327 |
300 => 'Multiple Choices', |
|
1328 |
301 => 'Moved Permanently', |
|
1329 |
302 => 'Found', |
|
1330 |
303 => 'See Other', |
|
1331 |
304 => 'Not Modified', |
|
1332 |
305 => 'Use Proxy', |
|
1333 |
306 => 'Reserved', |
|
1334 |
307 => 'Temporary Redirect', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
308 => 'Permanent Redirect', |
0 | 1336 |
|
1337 |
400 => 'Bad Request', |
|
1338 |
401 => 'Unauthorized', |
|
1339 |
402 => 'Payment Required', |
|
1340 |
403 => 'Forbidden', |
|
1341 |
404 => 'Not Found', |
|
1342 |
405 => 'Method Not Allowed', |
|
1343 |
406 => 'Not Acceptable', |
|
1344 |
407 => 'Proxy Authentication Required', |
|
1345 |
408 => 'Request Timeout', |
|
1346 |
409 => 'Conflict', |
|
1347 |
410 => 'Gone', |
|
1348 |
411 => 'Length Required', |
|
1349 |
412 => 'Precondition Failed', |
|
1350 |
413 => 'Request Entity Too Large', |
|
1351 |
414 => 'Request-URI Too Long', |
|
1352 |
415 => 'Unsupported Media Type', |
|
1353 |
416 => 'Requested Range Not Satisfiable', |
|
1354 |
417 => 'Expectation Failed', |
|
5 | 1355 |
418 => 'I\'m a teapot', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
421 => 'Misdirected Request', |
0 | 1357 |
422 => 'Unprocessable Entity', |
1358 |
423 => 'Locked', |
|
1359 |
424 => 'Failed Dependency', |
|
1360 |
426 => 'Upgrade Required', |
|
5 | 1361 |
428 => 'Precondition Required', |
1362 |
429 => 'Too Many Requests', |
|
1363 |
431 => 'Request Header Fields Too Large', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
451 => 'Unavailable For Legal Reasons', |
0 | 1365 |
|
1366 |
500 => 'Internal Server Error', |
|
1367 |
501 => 'Not Implemented', |
|
1368 |
502 => 'Bad Gateway', |
|
1369 |
503 => 'Service Unavailable', |
|
1370 |
504 => 'Gateway Timeout', |
|
1371 |
505 => 'HTTP Version Not Supported', |
|
1372 |
506 => 'Variant Also Negotiates', |
|
1373 |
507 => 'Insufficient Storage', |
|
5 | 1374 |
510 => 'Not Extended', |
1375 |
511 => 'Network Authentication Required', |
|
0 | 1376 |
); |
1377 |
} |
|
1378 |
||
9 | 1379 |
if ( isset( $wp_header_to_desc[ $code ] ) ) { |
1380 |
return $wp_header_to_desc[ $code ]; |
|
1381 |
} else { |
|
0 | 1382 |
return ''; |
9 | 1383 |
} |
0 | 1384 |
} |
1385 |
||
1386 |
/** |
|
1387 |
* Set HTTP status header. |
|
1388 |
* |
|
1389 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
* @since 4.4.0 Added the `$description` parameter. |
5 | 1391 |
* |
1392 |
* @see get_status_header_desc() |
|
1393 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* @param int $code HTTP status code. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
* @param string $description Optional. A custom description for the HTTP status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
function status_header( $code, $description = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
if ( ! $description ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
$description = get_status_header_desc( $code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
if ( empty( $description ) ) { |
5 | 1403 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
|
9 | 1406 |
$protocol = wp_get_server_protocol(); |
5 | 1407 |
$status_header = "$protocol $code $description"; |
9 | 1408 |
if ( function_exists( 'apply_filters' ) ) { |
5 | 1409 |
|
1410 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* Filters an HTTP status header. |
5 | 1412 |
* |
1413 |
* @since 2.2.0 |
|
1414 |
* |
|
1415 |
* @param string $status_header HTTP status header. |
|
1416 |
* @param int $code HTTP status code. |
|
1417 |
* @param string $description Description for the status code. |
|
1418 |
* @param string $protocol Server protocol. |
|
1419 |
*/ |
|
1420 |
$status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol ); |
|
9 | 1421 |
} |
5 | 1422 |
|
16 | 1423 |
if ( ! headers_sent() ) { |
1424 |
header( $status_header, true, $code ); |
|
1425 |
} |
|
0 | 1426 |
} |
1427 |
||
1428 |
/** |
|
5 | 1429 |
* Get the header information to prevent caching. |
1430 |
* |
|
1431 |
* The several different headers cover the different ways cache prevention |
|
1432 |
* is handled by different browsers |
|
0 | 1433 |
* |
1434 |
* @since 2.8.0 |
|
1435 |
* |
|
1436 |
* @return array The associative array of header names and field values. |
|
1437 |
*/ |
|
1438 |
function wp_get_nocache_headers() { |
|
1439 |
$headers = array( |
|
9 | 1440 |
'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT', |
0 | 1441 |
'Cache-Control' => 'no-cache, must-revalidate, max-age=0', |
1442 |
); |
|
1443 |
||
9 | 1444 |
if ( function_exists( 'apply_filters' ) ) { |
5 | 1445 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
* Filters the cache-controlling headers. |
5 | 1447 |
* |
1448 |
* @since 2.8.0 |
|
1449 |
* |
|
1450 |
* @see wp_get_nocache_headers() |
|
1451 |
* |
|
1452 |
* @param array $headers { |
|
1453 |
* Header names and field values. |
|
1454 |
* |
|
1455 |
* @type string $Expires Expires header. |
|
1456 |
* @type string $Cache-Control Cache-Control header. |
|
1457 |
* } |
|
1458 |
*/ |
|
1459 |
$headers = (array) apply_filters( 'nocache_headers', $headers ); |
|
0 | 1460 |
} |
1461 |
$headers['Last-Modified'] = false; |
|
1462 |
return $headers; |
|
1463 |
} |
|
1464 |
||
1465 |
/** |
|
5 | 1466 |
* Set the headers to prevent caching for the different browsers. |
1467 |
* |
|
1468 |
* Different browsers support different nocache headers, so several |
|
1469 |
* headers must be sent so that all of them get the point that no |
|
1470 |
* caching should occur. |
|
0 | 1471 |
* |
1472 |
* @since 2.0.0 |
|
5 | 1473 |
* |
1474 |
* @see wp_get_nocache_headers() |
|
0 | 1475 |
*/ |
1476 |
function nocache_headers() { |
|
16 | 1477 |
if ( headers_sent() ) { |
1478 |
return; |
|
1479 |
} |
|
1480 |
||
0 | 1481 |
$headers = wp_get_nocache_headers(); |
1482 |
||
1483 |
unset( $headers['Last-Modified'] ); |
|
1484 |
||
16 | 1485 |
header_remove( 'Last-Modified' ); |
0 | 1486 |
|
9 | 1487 |
foreach ( $headers as $name => $field_value ) { |
16 | 1488 |
header( "{$name}: {$field_value}" ); |
9 | 1489 |
} |
0 | 1490 |
} |
1491 |
||
1492 |
/** |
|
1493 |
* Set the headers for caching for 10 days with JavaScript content type. |
|
1494 |
* |
|
1495 |
* @since 2.1.0 |
|
1496 |
*/ |
|
1497 |
function cache_javascript_headers() { |
|
1498 |
$expiresOffset = 10 * DAY_IN_SECONDS; |
|
5 | 1499 |
|
9 | 1500 |
header( 'Content-Type: text/javascript; charset=' . get_bloginfo( 'charset' ) ); |
16 | 1501 |
header( 'Vary: Accept-Encoding' ); // Handle proxies. |
9 | 1502 |
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiresOffset ) . ' GMT' ); |
0 | 1503 |
} |
1504 |
||
1505 |
/** |
|
1506 |
* Retrieve the number of database queries during the WordPress execution. |
|
1507 |
* |
|
1508 |
* @since 2.0.0 |
|
1509 |
* |
|
5 | 1510 |
* @global wpdb $wpdb WordPress database abstraction object. |
1511 |
* |
|
1512 |
* @return int Number of database queries. |
|
0 | 1513 |
*/ |
1514 |
function get_num_queries() { |
|
1515 |
global $wpdb; |
|
1516 |
return $wpdb->num_queries; |
|
1517 |
} |
|
1518 |
||
1519 |
/** |
|
5 | 1520 |
* Whether input is yes or no. |
1521 |
* |
|
1522 |
* Must be 'y' to be true. |
|
0 | 1523 |
* |
1524 |
* @since 1.0.0 |
|
1525 |
* |
|
5 | 1526 |
* @param string $yn Character string containing either 'y' (yes) or 'n' (no). |
1527 |
* @return bool True if yes, false on anything else. |
|
0 | 1528 |
*/ |
1529 |
function bool_from_yn( $yn ) { |
|
16 | 1530 |
return ( 'y' === strtolower( $yn ) ); |
0 | 1531 |
} |
1532 |
||
1533 |
/** |
|
5 | 1534 |
* Load the feed template from the use of an action hook. |
0 | 1535 |
* |
1536 |
* If the feed action does not have a hook, then the function will die with a |
|
1537 |
* message telling the visitor that the feed is not valid. |
|
1538 |
* |
|
1539 |
* It is better to only have one hook for each feed. |
|
1540 |
* |
|
1541 |
* @since 2.1.0 |
|
5 | 1542 |
* |
16 | 1543 |
* @global WP_Query $wp_query WordPress Query object. |
0 | 1544 |
*/ |
1545 |
function do_feed() { |
|
1546 |
global $wp_query; |
|
1547 |
||
1548 |
$feed = get_query_var( 'feed' ); |
|
1549 |
||
1550 |
// Remove the pad, if present. |
|
1551 |
$feed = preg_replace( '/^_+/', '', $feed ); |
|
1552 |
||
16 | 1553 |
if ( '' === $feed || 'feed' === $feed ) { |
0 | 1554 |
$feed = get_default_feed(); |
9 | 1555 |
} |
0 | 1556 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
if ( ! has_action( "do_feed_{$feed}" ) ) { |
16 | 1558 |
wp_die( __( 'Error: This is not a valid feed template.' ), '', array( 'response' => 404 ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
} |
0 | 1560 |
|
5 | 1561 |
/** |
1562 |
* Fires once the given feed is loaded. |
|
1563 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
* The dynamic portion of the hook name, `$feed`, refers to the feed template name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1565 |
* Possible values include: 'rdf', 'rss', 'rss2', and 'atom'. |
5 | 1566 |
* |
1567 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1568 |
* @since 4.4.0 The `$feed` parameter was added. |
5 | 1569 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
* @param bool $is_comment_feed Whether the feed is a comment feed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
* @param string $feed The feed name. |
5 | 1572 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed ); |
0 | 1574 |
} |
1575 |
||
1576 |
/** |
|
1577 |
* Load the RDF RSS 0.91 Feed template. |
|
1578 |
* |
|
1579 |
* @since 2.1.0 |
|
5 | 1580 |
* |
1581 |
* @see load_template() |
|
0 | 1582 |
*/ |
1583 |
function do_feed_rdf() { |
|
1584 |
load_template( ABSPATH . WPINC . '/feed-rdf.php' ); |
|
1585 |
} |
|
1586 |
||
1587 |
/** |
|
1588 |
* Load the RSS 1.0 Feed Template. |
|
1589 |
* |
|
1590 |
* @since 2.1.0 |
|
5 | 1591 |
* |
1592 |
* @see load_template() |
|
0 | 1593 |
*/ |
1594 |
function do_feed_rss() { |
|
1595 |
load_template( ABSPATH . WPINC . '/feed-rss.php' ); |
|
1596 |
} |
|
1597 |
||
1598 |
/** |
|
1599 |
* Load either the RSS2 comment feed or the RSS2 posts feed. |
|
1600 |
* |
|
1601 |
* @since 2.1.0 |
|
1602 |
* |
|
5 | 1603 |
* @see load_template() |
1604 |
* |
|
0 | 1605 |
* @param bool $for_comments True for the comment feed, false for normal feed. |
1606 |
*/ |
|
1607 |
function do_feed_rss2( $for_comments ) { |
|
9 | 1608 |
if ( $for_comments ) { |
0 | 1609 |
load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' ); |
9 | 1610 |
} else { |
0 | 1611 |
load_template( ABSPATH . WPINC . '/feed-rss2.php' ); |
9 | 1612 |
} |
0 | 1613 |
} |
1614 |
||
1615 |
/** |
|
1616 |
* Load either Atom comment feed or Atom posts feed. |
|
1617 |
* |
|
1618 |
* @since 2.1.0 |
|
1619 |
* |
|
5 | 1620 |
* @see load_template() |
1621 |
* |
|
0 | 1622 |
* @param bool $for_comments True for the comment feed, false for normal feed. |
1623 |
*/ |
|
1624 |
function do_feed_atom( $for_comments ) { |
|
9 | 1625 |
if ( $for_comments ) { |
1626 |
load_template( ABSPATH . WPINC . '/feed-atom-comments.php' ); |
|
1627 |
} else { |
|
0 | 1628 |
load_template( ABSPATH . WPINC . '/feed-atom.php' ); |
9 | 1629 |
} |
0 | 1630 |
} |
1631 |
||
1632 |
/** |
|
16 | 1633 |
* Displays the default robots.txt file content. |
0 | 1634 |
* |
1635 |
* @since 2.1.0 |
|
16 | 1636 |
* @since 5.3.0 Remove the "Disallow: /" output if search engine visiblity is |
1637 |
* discouraged in favor of robots meta HTML tag in wp_no_robots(). |
|
0 | 1638 |
*/ |
1639 |
function do_robots() { |
|
1640 |
header( 'Content-Type: text/plain; charset=utf-8' ); |
|
1641 |
||
5 | 1642 |
/** |
1643 |
* Fires when displaying the robots.txt file. |
|
1644 |
* |
|
1645 |
* @since 2.1.0 |
|
1646 |
*/ |
|
0 | 1647 |
do_action( 'do_robotstxt' ); |
1648 |
||
1649 |
$output = "User-agent: *\n"; |
|
1650 |
$public = get_option( 'blog_public' ); |
|
16 | 1651 |
|
1652 |
$site_url = parse_url( site_url() ); |
|
1653 |
$path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : ''; |
|
1654 |
$output .= "Disallow: $path/wp-admin/\n"; |
|
1655 |
$output .= "Allow: $path/wp-admin/admin-ajax.php\n"; |
|
0 | 1656 |
|
5 | 1657 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1658 |
* Filters the robots.txt output. |
5 | 1659 |
* |
1660 |
* @since 3.0.0 |
|
1661 |
* |
|
16 | 1662 |
* @param string $output The robots.txt output. |
5 | 1663 |
* @param bool $public Whether the site is considered "public". |
1664 |
*/ |
|
1665 |
echo apply_filters( 'robots_txt', $output, $public ); |
|
0 | 1666 |
} |
1667 |
||
1668 |
/** |
|
16 | 1669 |
* Display the favicon.ico file content. |
1670 |
* |
|
1671 |
* @since 5.4.0 |
|
1672 |
*/ |
|
1673 |
function do_favicon() { |
|
1674 |
/** |
|
1675 |
* Fires when serving the favicon.ico file. |
|
1676 |
* |
|
1677 |
* @since 5.4.0 |
|
1678 |
*/ |
|
1679 |
do_action( 'do_faviconico' ); |
|
1680 |
||
1681 |
wp_redirect( get_site_icon_url( 32, includes_url( 'images/w-logo-blue-white-bg.png' ) ) ); |
|
1682 |
exit; |
|
1683 |
} |
|
1684 |
||
1685 |
/** |
|
9 | 1686 |
* Determines whether WordPress is already installed. |
0 | 1687 |
* |
5 | 1688 |
* The cache will be checked first. If you have a cache plugin, which saves |
1689 |
* the cache values, then this will work. If you use the default WordPress |
|
1690 |
* cache, and the database goes away, then you might have problems. |
|
1691 |
* |
|
1692 |
* Checks for the 'siteurl' option for whether WordPress is installed. |
|
0 | 1693 |
* |
9 | 1694 |
* For more information on this and similar theme functions, check out |
1695 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1696 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
1697 |
* |
|
0 | 1698 |
* @since 2.1.0 |
5 | 1699 |
* |
1700 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
1701 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1702 |
* @return bool Whether the site is already installed. |
0 | 1703 |
*/ |
1704 |
function is_blog_installed() { |
|
1705 |
global $wpdb; |
|
1706 |
||
5 | 1707 |
/* |
1708 |
* Check cache first. If options table goes away and we have true |
|
1709 |
* cached, oh well. |
|
1710 |
*/ |
|
9 | 1711 |
if ( wp_cache_get( 'is_blog_installed' ) ) { |
0 | 1712 |
return true; |
9 | 1713 |
} |
0 | 1714 |
|
1715 |
$suppress = $wpdb->suppress_errors(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
if ( ! wp_installing() ) { |
0 | 1717 |
$alloptions = wp_load_alloptions(); |
1718 |
} |
|
16 | 1719 |
// If siteurl is not set to autoload, check it specifically. |
9 | 1720 |
if ( ! isset( $alloptions['siteurl'] ) ) { |
0 | 1721 |
$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" ); |
9 | 1722 |
} else { |
0 | 1723 |
$installed = $alloptions['siteurl']; |
9 | 1724 |
} |
0 | 1725 |
$wpdb->suppress_errors( $suppress ); |
1726 |
||
9 | 1727 |
$installed = ! empty( $installed ); |
0 | 1728 |
wp_cache_set( 'is_blog_installed', $installed ); |
1729 |
||
9 | 1730 |
if ( $installed ) { |
0 | 1731 |
return true; |
9 | 1732 |
} |
0 | 1733 |
|
1734 |
// If visiting repair.php, return true and let it take over. |
|
9 | 1735 |
if ( defined( 'WP_REPAIRING' ) ) { |
0 | 1736 |
return true; |
9 | 1737 |
} |
0 | 1738 |
|
1739 |
$suppress = $wpdb->suppress_errors(); |
|
1740 |
||
5 | 1741 |
/* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1742 |
* Loop over the WP tables. If none exist, then scratch installation is allowed. |
5 | 1743 |
* If one or more exist, suggest table repair since we got here because the |
1744 |
* options table could not be accessed. |
|
1745 |
*/ |
|
0 | 1746 |
$wp_tables = $wpdb->tables(); |
1747 |
foreach ( $wp_tables as $table ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
// The existence of custom user tables shouldn't suggest an insane state or prevent a clean installation. |
9 | 1749 |
if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table ) { |
0 | 1750 |
continue; |
9 | 1751 |
} |
1752 |
if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table ) { |
|
0 | 1753 |
continue; |
9 | 1754 |
} |
1755 |
||
1756 |
if ( ! $wpdb->get_results( "DESCRIBE $table;" ) ) { |
|
0 | 1757 |
continue; |
9 | 1758 |
} |
0 | 1759 |
|
1760 |
// One or more tables exist. We are insane. |
|
1761 |
||
1762 |
wp_load_translations_early(); |
|
1763 |
||
1764 |
// Die with a DB error. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
$wpdb->error = sprintf( |
16 | 1766 |
/* translators: %s: Database repair URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1767 |
__( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1768 |
'maint/repair.php?referrer=is_blog_installed' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1770 |
|
0 | 1771 |
dead_db(); |
1772 |
} |
|
1773 |
||
1774 |
$wpdb->suppress_errors( $suppress ); |
|
1775 |
||
1776 |
wp_cache_set( 'is_blog_installed', false ); |
|
1777 |
||
1778 |
return false; |
|
1779 |
} |
|
1780 |
||
1781 |
/** |
|
1782 |
* Retrieve URL with nonce added to URL query. |
|
1783 |
* |
|
1784 |
* @since 2.0.4 |
|
1785 |
* |
|
5 | 1786 |
* @param string $actionurl URL to add nonce action. |
1787 |
* @param int|string $action Optional. Nonce action name. Default -1. |
|
1788 |
* @param string $name Optional. Nonce name. Default '_wpnonce'. |
|
1789 |
* @return string Escaped URL with nonce action added. |
|
0 | 1790 |
*/ |
1791 |
function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) { |
|
1792 |
$actionurl = str_replace( '&', '&', $actionurl ); |
|
1793 |
return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) ); |
|
1794 |
} |
|
1795 |
||
1796 |
/** |
|
1797 |
* Retrieve or display nonce hidden field for forms. |
|
1798 |
* |
|
1799 |
* The nonce field is used to validate that the contents of the form came from |
|
1800 |
* the location on the current site and not somewhere else. The nonce does not |
|
1801 |
* offer absolute protection, but should protect against most cases. It is very |
|
1802 |
* important to use nonce field in forms. |
|
1803 |
* |
|
1804 |
* The $action and $name are optional, but if you want to have better security, |
|
1805 |
* it is strongly suggested to set those two parameters. It is easier to just |
|
1806 |
* call the function without any parameters, because validation of the nonce |
|
1807 |
* doesn't require any parameters, but since crackers know what the default is |
|
1808 |
* it won't be difficult for them to find a way around your nonce and cause |
|
1809 |
* damage. |
|
1810 |
* |
|
1811 |
* The input name will be whatever $name value you gave. The input value will be |
|
1812 |
* the nonce creation value. |
|
1813 |
* |
|
1814 |
* @since 2.0.4 |
|
1815 |
* |
|
5 | 1816 |
* @param int|string $action Optional. Action name. Default -1. |
1817 |
* @param string $name Optional. Nonce name. Default '_wpnonce'. |
|
1818 |
* @param bool $referer Optional. Whether to set the referer field for validation. Default true. |
|
1819 |
* @param bool $echo Optional. Whether to display or return hidden form field. Default true. |
|
1820 |
* @return string Nonce field HTML markup. |
|
0 | 1821 |
*/ |
9 | 1822 |
function wp_nonce_field( $action = -1, $name = '_wpnonce', $referer = true, $echo = true ) { |
1823 |
$name = esc_attr( $name ); |
|
0 | 1824 |
$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />'; |
1825 |
||
9 | 1826 |
if ( $referer ) { |
0 | 1827 |
$nonce_field .= wp_referer_field( false ); |
9 | 1828 |
} |
1829 |
||
1830 |
if ( $echo ) { |
|
0 | 1831 |
echo $nonce_field; |
9 | 1832 |
} |
0 | 1833 |
|
1834 |
return $nonce_field; |
|
1835 |
} |
|
1836 |
||
1837 |
/** |
|
1838 |
* Retrieve or display referer hidden field for forms. |
|
1839 |
* |
|
1840 |
* The referer link is the current Request URI from the server super global. The |
|
1841 |
* input name is '_wp_http_referer', in case you wanted to check manually. |
|
1842 |
* |
|
1843 |
* @since 2.0.4 |
|
1844 |
* |
|
5 | 1845 |
* @param bool $echo Optional. Whether to echo or return the referer field. Default true. |
1846 |
* @return string Referer field HTML markup. |
|
0 | 1847 |
*/ |
1848 |
function wp_referer_field( $echo = true ) { |
|
9 | 1849 |
$referer_field = '<input type="hidden" name="_wp_http_referer" value="' . esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />'; |
1850 |
||
1851 |
if ( $echo ) { |
|
0 | 1852 |
echo $referer_field; |
9 | 1853 |
} |
16 | 1854 |
|
0 | 1855 |
return $referer_field; |
1856 |
} |
|
1857 |
||
1858 |
/** |
|
1859 |
* Retrieve or display original referer hidden field for forms. |
|
1860 |
* |
|
1861 |
* The input name is '_wp_original_http_referer' and will be either the same |
|
5 | 1862 |
* value of wp_referer_field(), if that was posted already or it will be the |
1863 |
* current page, if it doesn't exist. |
|
1864 |
* |
|
0 | 1865 |
* @since 2.0.4 |
1866 |
* |
|
5 | 1867 |
* @param bool $echo Optional. Whether to echo the original http referer. Default true. |
1868 |
* @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to. |
|
1869 |
* Default 'current'. |
|
0 | 1870 |
* @return string Original referer field. |
1871 |
*/ |
|
1872 |
function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) { |
|
16 | 1873 |
$ref = wp_get_original_referer(); |
1874 |
||
1875 |
if ( ! $ref ) { |
|
1876 |
$ref = ( 'previous' === $jump_back_to ) ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] ); |
|
1877 |
} |
|
1878 |
||
0 | 1879 |
$orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />'; |
16 | 1880 |
|
9 | 1881 |
if ( $echo ) { |
0 | 1882 |
echo $orig_referer_field; |
9 | 1883 |
} |
16 | 1884 |
|
0 | 1885 |
return $orig_referer_field; |
1886 |
} |
|
1887 |
||
1888 |
/** |
|
5 | 1889 |
* Retrieve referer from '_wp_http_referer' or HTTP referer. |
1890 |
* |
|
1891 |
* If it's the same as the current request URL, will return false. |
|
1892 |
* |
|
0 | 1893 |
* @since 2.0.4 |
1894 |
* |
|
16 | 1895 |
* @return string|false Referer URL on success, false on failure. |
0 | 1896 |
*/ |
1897 |
function wp_get_referer() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1898 |
if ( ! function_exists( 'wp_validate_redirect' ) ) { |
0 | 1899 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1900 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1901 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1902 |
$ref = wp_get_raw_referer(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1903 |
|
16 | 1904 |
if ( $ref && wp_unslash( $_SERVER['REQUEST_URI'] ) !== $ref && home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) !== $ref ) { |
0 | 1905 |
return wp_validate_redirect( $ref, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1906 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1907 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1908 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1909 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1910 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1911 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1912 |
* Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1913 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1914 |
* Do not use for redirects, use wp_get_referer() instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1915 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1916 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1917 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1918 |
* @return string|false Referer URL on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1919 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1920 |
function wp_get_raw_referer() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1921 |
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1922 |
return wp_unslash( $_REQUEST['_wp_http_referer'] ); |
9 | 1923 |
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1924 |
return wp_unslash( $_SERVER['HTTP_REFERER'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1925 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1926 |
|
0 | 1927 |
return false; |
1928 |
} |
|
1929 |
||
1930 |
/** |
|
1931 |
* Retrieve original referer that was posted, if it exists. |
|
1932 |
* |
|
1933 |
* @since 2.0.4 |
|
1934 |
* |
|
16 | 1935 |
* @return string|false Original referer URL on success, false on failure. |
0 | 1936 |
*/ |
1937 |
function wp_get_original_referer() { |
|
9 | 1938 |
if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) ) { |
0 | 1939 |
return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false ); |
9 | 1940 |
} |
16 | 1941 |
|
0 | 1942 |
return false; |
1943 |
} |
|
1944 |
||
1945 |
/** |
|
1946 |
* Recursive directory creation based on full path. |
|
1947 |
* |
|
1948 |
* Will attempt to set permissions on folders. |
|
1949 |
* |
|
1950 |
* @since 2.0.1 |
|
1951 |
* |
|
1952 |
* @param string $target Full path to attempt to create. |
|
1953 |
* @return bool Whether the path was created. True if path already exists. |
|
1954 |
*/ |
|
1955 |
function wp_mkdir_p( $target ) { |
|
1956 |
$wrapper = null; |
|
1957 |
||
5 | 1958 |
// Strip the protocol. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1959 |
if ( wp_is_stream( $target ) ) { |
0 | 1960 |
list( $wrapper, $target ) = explode( '://', $target, 2 ); |
1961 |
} |
|
1962 |
||
5 | 1963 |
// From php.net/mkdir user contributed notes. |
0 | 1964 |
$target = str_replace( '//', '/', $target ); |
1965 |
||
5 | 1966 |
// Put the wrapper back on the target. |
16 | 1967 |
if ( null !== $wrapper ) { |
0 | 1968 |
$target = $wrapper . '://' . $target; |
1969 |
} |
|
1970 |
||
5 | 1971 |
/* |
1972 |
* Safe mode fails with a trailing slash under certain PHP versions. |
|
1973 |
* Use rtrim() instead of untrailingslashit to avoid formatting.php dependency. |
|
1974 |
*/ |
|
9 | 1975 |
$target = rtrim( $target, '/' ); |
1976 |
if ( empty( $target ) ) { |
|
0 | 1977 |
$target = '/'; |
9 | 1978 |
} |
1979 |
||
1980 |
if ( file_exists( $target ) ) { |
|
0 | 1981 |
return @is_dir( $target ); |
9 | 1982 |
} |
0 | 1983 |
|
13 | 1984 |
// Do not allow path traversals. |
1985 |
if ( false !== strpos( $target, '../' ) || false !== strpos( $target, '..' . DIRECTORY_SEPARATOR ) ) { |
|
1986 |
return false; |
|
1987 |
} |
|
1988 |
||
0 | 1989 |
// We need to find the permissions of the parent folder that exists and inherit that. |
1990 |
$target_parent = dirname( $target ); |
|
16 | 1991 |
while ( '.' !== $target_parent && ! is_dir( $target_parent ) && dirname( $target_parent ) !== $target_parent ) { |
0 | 1992 |
$target_parent = dirname( $target_parent ); |
1993 |
} |
|
1994 |
||
1995 |
// Get the permission bits. |
|
16 | 1996 |
$stat = @stat( $target_parent ); |
1997 |
if ( $stat ) { |
|
0 | 1998 |
$dir_perms = $stat['mode'] & 0007777; |
1999 |
} else { |
|
2000 |
$dir_perms = 0777; |
|
2001 |
} |
|
2002 |
||
2003 |
if ( @mkdir( $target, $dir_perms, true ) ) { |
|
5 | 2004 |
|
2005 |
/* |
|
2006 |
* If a umask is set that modifies $dir_perms, we'll have to re-set |
|
2007 |
* the $dir_perms correctly with chmod() |
|
2008 |
*/ |
|
16 | 2009 |
if ( ( $dir_perms & ~umask() ) != $dir_perms ) { |
5 | 2010 |
$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) ); |
2011 |
for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) { |
|
16 | 2012 |
chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms ); |
5 | 2013 |
} |
2014 |
} |
|
2015 |
||
0 | 2016 |
return true; |
2017 |
} |
|
2018 |
||
2019 |
return false; |
|
2020 |
} |
|
2021 |
||
2022 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2023 |
* Test if a given filesystem path is absolute. |
5 | 2024 |
* |
2025 |
* For example, '/foo/bar', or 'c:\windows'. |
|
0 | 2026 |
* |
2027 |
* @since 2.5.0 |
|
2028 |
* |
|
5 | 2029 |
* @param string $path File path. |
0 | 2030 |
* @return bool True if path is absolute, false is not absolute. |
2031 |
*/ |
|
2032 |
function path_is_absolute( $path ) { |
|
5 | 2033 |
/* |
9 | 2034 |
* Check to see if the path is a stream and check to see if its an actual |
2035 |
* path or file as realpath() does not support stream wrappers. |
|
2036 |
*/ |
|
2037 |
if ( wp_is_stream( $path ) && ( is_dir( $path ) || is_file( $path ) ) ) { |
|
2038 |
return true; |
|
2039 |
} |
|
2040 |
||
2041 |
/* |
|
5 | 2042 |
* This is definitive if true but fails if $path does not exist or contains |
2043 |
* a symbolic link. |
|
2044 |
*/ |
|
9 | 2045 |
if ( realpath( $path ) == $path ) { |
0 | 2046 |
return true; |
9 | 2047 |
} |
2048 |
||
16 | 2049 |
if ( strlen( $path ) == 0 || '.' === $path[0] ) { |
0 | 2050 |
return false; |
9 | 2051 |
} |
0 | 2052 |
|
5 | 2053 |
// Windows allows absolute paths like this. |
9 | 2054 |
if ( preg_match( '#^[a-zA-Z]:\\\\#', $path ) ) { |
0 | 2055 |
return true; |
9 | 2056 |
} |
0 | 2057 |
|
5 | 2058 |
// A path starting with / or \ is absolute; anything else is relative. |
16 | 2059 |
return ( '/' === $path[0] || '\\' === $path[0] ); |
0 | 2060 |
} |
2061 |
||
2062 |
/** |
|
5 | 2063 |
* Join two filesystem paths together. |
2064 |
* |
|
2065 |
* For example, 'give me $path relative to $base'. If the $path is absolute, |
|
2066 |
* then it the full path is returned. |
|
0 | 2067 |
* |
2068 |
* @since 2.5.0 |
|
2069 |
* |
|
5 | 2070 |
* @param string $base Base path. |
2071 |
* @param string $path Path relative to $base. |
|
0 | 2072 |
* @return string The path with the base or absolute path. |
2073 |
*/ |
|
2074 |
function path_join( $base, $path ) { |
|
9 | 2075 |
if ( path_is_absolute( $path ) ) { |
0 | 2076 |
return $path; |
9 | 2077 |
} |
2078 |
||
2079 |
return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' ); |
|
0 | 2080 |
} |
2081 |
||
2082 |
/** |
|
5 | 2083 |
* Normalize a filesystem path. |
2084 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2085 |
* On windows systems, replaces backslashes with forward slashes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2086 |
* and forces upper-case drive letters. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2087 |
* Allows for two leading slashes for Windows network shares, but |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2088 |
* ensures that all other duplicate slashes are reduced to a single. |
5 | 2089 |
* |
2090 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2091 |
* @since 4.4.0 Ensures upper-case drive letters on Windows systems. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2092 |
* @since 4.5.0 Allows for Windows network shares. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2093 |
* @since 4.9.7 Allows for PHP file wrappers. |
5 | 2094 |
* |
2095 |
* @param string $path Path to normalize. |
|
2096 |
* @return string Normalized path. |
|
2097 |
*/ |
|
2098 |
function wp_normalize_path( $path ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2099 |
$wrapper = ''; |
16 | 2100 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2101 |
if ( wp_is_stream( $path ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2102 |
list( $wrapper, $path ) = explode( '://', $path, 2 ); |
16 | 2103 |
|
2104 |
$wrapper .= '://'; |
|
2105 |
} |
|
2106 |
||
2107 |
// Standardise all paths to use '/'. |
|
5 | 2108 |
$path = str_replace( '\\', '/', $path ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2109 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2110 |
// Replace multiple slashes down to a singular, allowing for network shares having two slashes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2111 |
$path = preg_replace( '|(?<=.)/+|', '/', $path ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2112 |
|
16 | 2113 |
// Windows paths should uppercase the drive letter. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2114 |
if ( ':' === substr( $path, 1, 1 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2115 |
$path = ucfirst( $path ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2116 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2117 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2118 |
return $wrapper . $path; |
5 | 2119 |
} |
2120 |
||
2121 |
/** |
|
2122 |
* Determine a writable directory for temporary files. |
|
2123 |
* |
|
2124 |
* Function's preference is the return value of sys_get_temp_dir(), |
|
0 | 2125 |
* followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR, |
2126 |
* before finally defaulting to /tmp/ |
|
2127 |
* |
|
2128 |
* In the event that this function does not find a writable location, |
|
5 | 2129 |
* It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file. |
0 | 2130 |
* |
2131 |
* @since 2.5.0 |
|
2132 |
* |
|
5 | 2133 |
* @return string Writable temporary directory. |
0 | 2134 |
*/ |
2135 |
function get_temp_dir() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2136 |
static $temp = ''; |
9 | 2137 |
if ( defined( 'WP_TEMP_DIR' ) ) { |
2138 |
return trailingslashit( WP_TEMP_DIR ); |
|
2139 |
} |
|
2140 |
||
2141 |
if ( $temp ) { |
|
5 | 2142 |
return trailingslashit( $temp ); |
9 | 2143 |
} |
2144 |
||
2145 |
if ( function_exists( 'sys_get_temp_dir' ) ) { |
|
0 | 2146 |
$temp = sys_get_temp_dir(); |
9 | 2147 |
if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { |
5 | 2148 |
return trailingslashit( $temp ); |
9 | 2149 |
} |
2150 |
} |
|
2151 |
||
2152 |
$temp = ini_get( 'upload_tmp_dir' ); |
|
2153 |
if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { |
|
5 | 2154 |
return trailingslashit( $temp ); |
9 | 2155 |
} |
0 | 2156 |
|
2157 |
$temp = WP_CONTENT_DIR . '/'; |
|
9 | 2158 |
if ( is_dir( $temp ) && wp_is_writable( $temp ) ) { |
0 | 2159 |
return $temp; |
9 | 2160 |
} |
0 | 2161 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2162 |
return '/tmp/'; |
0 | 2163 |
} |
2164 |
||
2165 |
/** |
|
2166 |
* Determine if a directory is writable. |
|
2167 |
* |
|
5 | 2168 |
* This function is used to work around certain ACL issues in PHP primarily |
2169 |
* affecting Windows Servers. |
|
2170 |
* |
|
2171 |
* @since 3.6.0 |
|
0 | 2172 |
* |
2173 |
* @see win_is_writable() |
|
2174 |
* |
|
5 | 2175 |
* @param string $path Path to check for write-ability. |
2176 |
* @return bool Whether the path is writable. |
|
0 | 2177 |
*/ |
2178 |
function wp_is_writable( $path ) { |
|
9 | 2179 |
if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { |
0 | 2180 |
return win_is_writable( $path ); |
9 | 2181 |
} else { |
0 | 2182 |
return @is_writable( $path ); |
9 | 2183 |
} |
0 | 2184 |
} |
2185 |
||
2186 |
/** |
|
2187 |
* Workaround for Windows bug in is_writable() function |
|
2188 |
* |
|
2189 |
* PHP has issues with Windows ACL's for determine if a |
|
2190 |
* directory is writable or not, this works around them by |
|
2191 |
* checking the ability to open files rather than relying |
|
2192 |
* upon PHP to interprate the OS ACL. |
|
2193 |
* |
|
2194 |
* @since 2.8.0 |
|
2195 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2196 |
* @see https://bugs.php.net/bug.php?id=27609 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2197 |
* @see https://bugs.php.net/bug.php?id=30931 |
5 | 2198 |
* |
2199 |
* @param string $path Windows path to check for write-ability. |
|
2200 |
* @return bool Whether the path is writable. |
|
0 | 2201 |
*/ |
2202 |
function win_is_writable( $path ) { |
|
16 | 2203 |
if ( '/' === $path[ strlen( $path ) - 1 ] ) { |
2204 |
// If it looks like a directory, check a random file within the directory. |
|
9 | 2205 |
return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp' ); |
16 | 2206 |
} elseif ( is_dir( $path ) ) { |
2207 |
// If it's a directory (and not a file), check a random file within the directory. |
|
0 | 2208 |
return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' ); |
5 | 2209 |
} |
16 | 2210 |
|
2211 |
// Check tmp file for read/write capabilities. |
|
9 | 2212 |
$should_delete_tmp_file = ! file_exists( $path ); |
16 | 2213 |
|
2214 |
$f = @fopen( $path, 'a' ); |
|
2215 |
if ( false === $f ) { |
|
0 | 2216 |
return false; |
9 | 2217 |
} |
0 | 2218 |
fclose( $f ); |
16 | 2219 |
|
9 | 2220 |
if ( $should_delete_tmp_file ) { |
0 | 2221 |
unlink( $path ); |
9 | 2222 |
} |
16 | 2223 |
|
0 | 2224 |
return true; |
2225 |
} |
|
2226 |
||
2227 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2228 |
* Retrieves uploads directory information. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2230 |
* Same as wp_upload_dir() but "light weight" as it doesn't attempt to create the uploads directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2231 |
* Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2232 |
* when not uploading files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2233 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2234 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2235 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2236 |
* @see wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2237 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2238 |
* @return array See wp_upload_dir() for description. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2239 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2240 |
function wp_get_upload_dir() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2241 |
return wp_upload_dir( null, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2242 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2243 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2244 |
/** |
16 | 2245 |
* Returns an array containing the current upload directory's path and URL. |
0 | 2246 |
* |
2247 |
* Checks the 'upload_path' option, which should be from the web root folder, |
|
2248 |
* and if it isn't empty it will be used. If it is empty, then the path will be |
|
2249 |
* 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will |
|
2250 |
* override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path. |
|
2251 |
* |
|
2252 |
* The upload URL path is set either by the 'upload_url_path' option or by using |
|
2253 |
* the 'WP_CONTENT_URL' constant and appending '/uploads' to the path. |
|
2254 |
* |
|
2255 |
* If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in |
|
2256 |
* the administration settings panel), then the time will be used. The format |
|
2257 |
* will be year first and then month. |
|
2258 |
* |
|
2259 |
* If the path couldn't be created, then an error will be returned with the key |
|
2260 |
* 'error' containing the error message. The error suggests that the parent |
|
2261 |
* directory is not writable by the server. |
|
2262 |
* |
|
2263 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2264 |
* @uses _wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2265 |
* |
5 | 2266 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2267 |
* @param bool $create_dir Optional. Whether to check and create the uploads directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2268 |
* Default true for backward compatibility. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2269 |
* @param bool $refresh_cache Optional. Whether to refresh the cache. Default false. |
16 | 2270 |
* @return array { |
2271 |
* Array of information about the upload directory. |
|
2272 |
* |
|
2273 |
* @type string $path Base directory and subdirectory or full path to upload directory. |
|
2274 |
* @type string $url Base URL and subdirectory or absolute URL to upload directory. |
|
2275 |
* @type string $subdir Subdirectory if uploads use year/month folders option is on. |
|
2276 |
* @type string $basedir Path without subdir. |
|
2277 |
* @type string $baseurl URL path without subdir. |
|
2278 |
* @type string|false $error False or error message. |
|
2279 |
* } |
|
0 | 2280 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2281 |
function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2282 |
static $cache = array(), $tested_paths = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2283 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2284 |
$key = sprintf( '%d-%s', get_current_blog_id(), (string) $time ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2285 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2286 |
if ( $refresh_cache || empty( $cache[ $key ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2287 |
$cache[ $key ] = _wp_upload_dir( $time ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2288 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2289 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2290 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2291 |
* Filters the uploads directory data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2292 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
* @since 2.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2294 |
* |
16 | 2295 |
* @param array $uploads { |
2296 |
* Array of information about the upload directory. |
|
2297 |
* |
|
2298 |
* @type string $path Base directory and subdirectory or full path to upload directory. |
|
2299 |
* @type string $url Base URL and subdirectory or absolute URL to upload directory. |
|
2300 |
* @type string $subdir Subdirectory if uploads use year/month folders option is on. |
|
2301 |
* @type string $basedir Path without subdir. |
|
2302 |
* @type string $baseurl URL path without subdir. |
|
2303 |
* @type string|false $error False or error message. |
|
2304 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2305 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2306 |
$uploads = apply_filters( 'upload_dir', $cache[ $key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2307 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
if ( $create_dir ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2309 |
$path = $uploads['path']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2310 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2311 |
if ( array_key_exists( $path, $tested_paths ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2312 |
$uploads['error'] = $tested_paths[ $path ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2313 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2314 |
if ( ! wp_mkdir_p( $path ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2315 |
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2317 |
} else { |
9 | 2318 |
$error_path = wp_basename( $uploads['basedir'] ) . $uploads['subdir']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2319 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2321 |
$uploads['error'] = sprintf( |
16 | 2322 |
/* translators: %s: Directory path. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2323 |
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2324 |
esc_html( $error_path ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2325 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2326 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2327 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2328 |
$tested_paths[ $path ] = $uploads['error']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2329 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2330 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2331 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2332 |
return $uploads; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2333 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2334 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2335 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2336 |
* A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2337 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2338 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2339 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2340 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2341 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2342 |
* @return array See wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2343 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2344 |
function _wp_upload_dir( $time = null ) { |
9 | 2345 |
$siteurl = get_option( 'siteurl' ); |
0 | 2346 |
$upload_path = trim( get_option( 'upload_path' ) ); |
2347 |
||
16 | 2348 |
if ( empty( $upload_path ) || 'wp-content/uploads' === $upload_path ) { |
0 | 2349 |
$dir = WP_CONTENT_DIR . '/uploads'; |
2350 |
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { |
|
16 | 2351 |
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH. |
0 | 2352 |
$dir = path_join( ABSPATH, $upload_path ); |
2353 |
} else { |
|
2354 |
$dir = $upload_path; |
|
2355 |
} |
|
2356 |
||
16 | 2357 |
$url = get_option( 'upload_url_path' ); |
2358 |
if ( ! $url ) { |
|
2359 |
if ( empty( $upload_path ) || ( 'wp-content/uploads' === $upload_path ) || ( $upload_path == $dir ) ) { |
|
0 | 2360 |
$url = WP_CONTENT_URL . '/uploads'; |
9 | 2361 |
} else { |
0 | 2362 |
$url = trailingslashit( $siteurl ) . $upload_path; |
9 | 2363 |
} |
0 | 2364 |
} |
2365 |
||
5 | 2366 |
/* |
2367 |
* Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled. |
|
2368 |
* We also sometimes obey UPLOADS when rewriting is enabled -- see the next block. |
|
2369 |
*/ |
|
0 | 2370 |
if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) { |
2371 |
$dir = ABSPATH . UPLOADS; |
|
2372 |
$url = trailingslashit( $siteurl ) . UPLOADS; |
|
2373 |
} |
|
2374 |
||
16 | 2375 |
// If multisite (and if not the main site in a post-MU network). |
0 | 2376 |
if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) { |
2377 |
||
2378 |
if ( ! get_site_option( 'ms_files_rewriting' ) ) { |
|
5 | 2379 |
/* |
2380 |
* If ms-files rewriting is disabled (networks created post-3.5), it is fairly |
|
2381 |
* straightforward: Append sites/%d if we're not on the main site (for post-MU |
|
2382 |
* networks). (The extra directory prevents a four-digit ID from conflicting with |
|
2383 |
* a year-based directory for the main site. But if a MU-era network has disabled |
|
2384 |
* ms-files rewriting manually, they don't need the extra directory, as they never |
|
2385 |
* had wp-content/uploads for the main site.) |
|
2386 |
*/ |
|
0 | 2387 |
|
9 | 2388 |
if ( defined( 'MULTISITE' ) ) { |
0 | 2389 |
$ms_dir = '/sites/' . get_current_blog_id(); |
9 | 2390 |
} else { |
0 | 2391 |
$ms_dir = '/' . get_current_blog_id(); |
9 | 2392 |
} |
0 | 2393 |
|
2394 |
$dir .= $ms_dir; |
|
2395 |
$url .= $ms_dir; |
|
2396 |
||
2397 |
} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) { |
|
5 | 2398 |
/* |
2399 |
* Handle the old-form ms-files.php rewriting if the network still has that enabled. |
|
2400 |
* When ms-files rewriting is enabled, then we only listen to UPLOADS when: |
|
2401 |
* 1) We are not on the main site in a post-MU network, as wp-content/uploads is used |
|
2402 |
* there, and |
|
2403 |
* 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect |
|
2404 |
* the original blog ID. |
|
2405 |
* |
|
2406 |
* Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. |
|
2407 |
* (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as |
|
2408 |
* as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files |
|
2409 |
* rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) |
|
2410 |
*/ |
|
0 | 2411 |
|
9 | 2412 |
if ( defined( 'BLOGUPLOADDIR' ) ) { |
0 | 2413 |
$dir = untrailingslashit( BLOGUPLOADDIR ); |
9 | 2414 |
} else { |
0 | 2415 |
$dir = ABSPATH . UPLOADS; |
9 | 2416 |
} |
0 | 2417 |
$url = trailingslashit( $siteurl ) . 'files'; |
2418 |
} |
|
2419 |
} |
|
2420 |
||
2421 |
$basedir = $dir; |
|
2422 |
$baseurl = $url; |
|
2423 |
||
2424 |
$subdir = ''; |
|
2425 |
if ( get_option( 'uploads_use_yearmonth_folders' ) ) { |
|
16 | 2426 |
// Generate the yearly and monthly directories. |
9 | 2427 |
if ( ! $time ) { |
0 | 2428 |
$time = current_time( 'mysql' ); |
9 | 2429 |
} |
2430 |
$y = substr( $time, 0, 4 ); |
|
2431 |
$m = substr( $time, 5, 2 ); |
|
0 | 2432 |
$subdir = "/$y/$m"; |
2433 |
} |
|
2434 |
||
2435 |
$dir .= $subdir; |
|
2436 |
$url .= $subdir; |
|
2437 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2438 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2439 |
'path' => $dir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2440 |
'url' => $url, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2441 |
'subdir' => $subdir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2442 |
'basedir' => $basedir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2443 |
'baseurl' => $baseurl, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2444 |
'error' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2445 |
); |
0 | 2446 |
} |
2447 |
||
2448 |
/** |
|
2449 |
* Get a filename that is sanitized and unique for the given directory. |
|
2450 |
* |
|
2451 |
* If the filename is not unique, then a number will be added to the filename |
|
16 | 2452 |
* before the extension, and will continue adding numbers until the filename |
2453 |
* is unique. |
|
2454 |
* |
|
2455 |
* The callback function allows the caller to use their own method to create |
|
2456 |
* unique file names. If defined, the callback should take three arguments: |
|
2457 |
* - directory, base filename, and extension - and return a unique filename. |
|
0 | 2458 |
* |
2459 |
* @since 2.5.0 |
|
2460 |
* |
|
5 | 2461 |
* @param string $dir Directory. |
2462 |
* @param string $filename File name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2463 |
* @param callable $unique_filename_callback Callback. Default null. |
0 | 2464 |
* @return string New filename, if given wasn't unique. |
2465 |
*/ |
|
2466 |
function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) { |
|
5 | 2467 |
// Sanitize the file name before we begin processing. |
9 | 2468 |
$filename = sanitize_file_name( $filename ); |
16 | 2469 |
$ext2 = null; |
0 | 2470 |
|
5 | 2471 |
// Separate the filename into a name and extension. |
9 | 2472 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2473 |
$name = pathinfo( $filename, PATHINFO_BASENAME ); |
16 | 2474 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2475 |
if ( $ext ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2476 |
$ext = '.' . $ext; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2477 |
} |
0 | 2478 |
|
5 | 2479 |
// Edge case: if file is named '.ext', treat as an empty name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2480 |
if ( $name === $ext ) { |
0 | 2481 |
$name = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2482 |
} |
0 | 2483 |
|
5 | 2484 |
/* |
2485 |
* Increment the file number until we have a unique file to save in $dir. |
|
2486 |
* Use callback if supplied. |
|
2487 |
*/ |
|
0 | 2488 |
if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) { |
2489 |
$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext ); |
|
2490 |
} else { |
|
2491 |
$number = ''; |
|
16 | 2492 |
$fname = pathinfo( $filename, PATHINFO_FILENAME ); |
2493 |
||
2494 |
// Always append a number to file names that can potentially match image sub-size file names. |
|
2495 |
if ( $fname && preg_match( '/-(?:\d+x\d+|scaled|rotated)$/', $fname ) ) { |
|
2496 |
$number = 1; |
|
2497 |
||
2498 |
// At this point the file name may not be unique. This is tested below and the $number is incremented. |
|
2499 |
$filename = str_replace( "{$fname}{$ext}", "{$fname}-{$number}{$ext}", $filename ); |
|
2500 |
} |
|
0 | 2501 |
|
5 | 2502 |
// Change '.ext' to lower case. |
9 | 2503 |
if ( $ext && strtolower( $ext ) != $ext ) { |
2504 |
$ext2 = strtolower( $ext ); |
|
2505 |
$filename2 = preg_replace( '|' . preg_quote( $ext ) . '$|', $ext2, $filename ); |
|
0 | 2506 |
|
5 | 2507 |
// Check for both lower and upper case extension or image sub-sizes may be overwritten. |
16 | 2508 |
while ( file_exists( $dir . "/{$filename}" ) || file_exists( $dir . "/{$filename2}" ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2509 |
$new_number = (int) $number + 1; |
16 | 2510 |
$filename = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename ); |
2511 |
$filename2 = str_replace( array( "-{$number}{$ext2}", "{$number}{$ext2}" ), "-{$new_number}{$ext2}", $filename2 ); |
|
9 | 2512 |
$number = $new_number; |
0 | 2513 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2514 |
|
16 | 2515 |
$filename = $filename2; |
2516 |
} else { |
|
2517 |
while ( file_exists( $dir . "/{$filename}" ) ) { |
|
2518 |
$new_number = (int) $number + 1; |
|
2519 |
||
2520 |
if ( '' === "{$number}{$ext}" ) { |
|
2521 |
$filename = "{$filename}-{$new_number}"; |
|
2522 |
} else { |
|
2523 |
$filename = str_replace( array( "-{$number}{$ext}", "{$number}{$ext}" ), "-{$new_number}{$ext}", $filename ); |
|
2524 |
} |
|
2525 |
||
2526 |
$number = $new_number; |
|
2527 |
} |
|
0 | 2528 |
} |
2529 |
||
16 | 2530 |
// Prevent collisions with existing file names that contain dimension-like strings |
2531 |
// (whether they are subsizes or originals uploaded prior to #42437). |
|
2532 |
$upload_dir = wp_get_upload_dir(); |
|
2533 |
||
2534 |
// The (resized) image files would have name and extension, and will be in the uploads dir. |
|
2535 |
if ( $name && $ext && @is_dir( $dir ) && false !== strpos( $dir, $upload_dir['basedir'] ) ) { |
|
2536 |
/** |
|
2537 |
* Filters the file list used for calculating a unique filename for a newly added file. |
|
2538 |
* |
|
2539 |
* Returning an array from the filter will effectively short-circuit retrieval |
|
2540 |
* from the filesystem and return the passed value instead. |
|
2541 |
* |
|
2542 |
* @since 5.5.0 |
|
2543 |
* |
|
2544 |
* @param array|null $files The list of files to use for filename comparisons. |
|
2545 |
* Default null (to retrieve the list from the filesystem). |
|
2546 |
* @param string $dir The directory for the new file. |
|
2547 |
* @param string $filename The proposed filename for the new file. |
|
2548 |
*/ |
|
2549 |
$files = apply_filters( 'pre_wp_unique_filename_file_list', null, $dir, $filename ); |
|
2550 |
||
2551 |
if ( null === $files ) { |
|
2552 |
// List of all files and directories contained in $dir. |
|
2553 |
$files = @scandir( $dir ); |
|
2554 |
} |
|
2555 |
||
2556 |
if ( ! empty( $files ) ) { |
|
2557 |
// Remove "dot" dirs. |
|
2558 |
$files = array_diff( $files, array( '.', '..' ) ); |
|
2559 |
} |
|
2560 |
||
2561 |
if ( ! empty( $files ) ) { |
|
2562 |
// The extension case may have changed above. |
|
2563 |
$new_ext = ! empty( $ext2 ) ? $ext2 : $ext; |
|
2564 |
||
2565 |
// Ensure this never goes into infinite loop |
|
2566 |
// as it uses pathinfo() and regex in the check, but string replacement for the changes. |
|
2567 |
$count = count( $files ); |
|
2568 |
$i = 0; |
|
2569 |
||
2570 |
while ( $i <= $count && _wp_check_existing_file_names( $filename, $files ) ) { |
|
2571 |
$new_number = (int) $number + 1; |
|
2572 |
$filename = str_replace( array( "-{$number}{$new_ext}", "{$number}{$new_ext}" ), "-{$new_number}{$new_ext}", $filename ); |
|
2573 |
$number = $new_number; |
|
2574 |
$i++; |
|
2575 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2576 |
} |
0 | 2577 |
} |
2578 |
} |
|
2579 |
||
16 | 2580 |
/** |
2581 |
* Filters the result when generating a unique file name. |
|
2582 |
* |
|
2583 |
* @since 4.5.0 |
|
2584 |
* |
|
2585 |
* @param string $filename Unique file name. |
|
2586 |
* @param string $ext File extension, eg. ".png". |
|
2587 |
* @param string $dir Directory path. |
|
2588 |
* @param callable|null $unique_filename_callback Callback function that generates the unique file name. |
|
2589 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2590 |
return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback ); |
0 | 2591 |
} |
2592 |
||
2593 |
/** |
|
16 | 2594 |
* Helper function to check if a file name could match an existing image sub-size file name. |
2595 |
* |
|
2596 |
* @since 5.3.1 |
|
2597 |
* @access private |
|
2598 |
* |
|
2599 |
* @param string $filename The file name to check. |
|
2600 |
* @param array $files An array of existing files in the directory. |
|
2601 |
* @return bool True if the tested file name could match an existing file, false otherwise. |
|
2602 |
*/ |
|
2603 |
function _wp_check_existing_file_names( $filename, $files ) { |
|
2604 |
$fname = pathinfo( $filename, PATHINFO_FILENAME ); |
|
2605 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
|
2606 |
||
2607 |
// Edge case, file names like `.ext`. |
|
2608 |
if ( empty( $fname ) ) { |
|
2609 |
return false; |
|
2610 |
} |
|
2611 |
||
2612 |
if ( $ext ) { |
|
2613 |
$ext = ".$ext"; |
|
2614 |
} |
|
2615 |
||
2616 |
$regex = '/^' . preg_quote( $fname ) . '-(?:\d+x\d+|scaled|rotated)' . preg_quote( $ext ) . '$/i'; |
|
2617 |
||
2618 |
foreach ( $files as $file ) { |
|
2619 |
if ( preg_match( $regex, $file ) ) { |
|
2620 |
return true; |
|
2621 |
} |
|
2622 |
} |
|
2623 |
||
2624 |
return false; |
|
2625 |
} |
|
2626 |
||
2627 |
/** |
|
0 | 2628 |
* Create a file in the upload folder with given content. |
2629 |
* |
|
2630 |
* If there is an error, then the key 'error' will exist with the error message. |
|
2631 |
* If success, then the key 'file' will have the unique file path, the 'url' key |
|
2632 |
* will have the link to the new file. and the 'error' key will be set to false. |
|
2633 |
* |
|
2634 |
* This function will not move an uploaded file to the upload folder. It will |
|
2635 |
* create a new file with the content in $bits parameter. If you move the upload |
|
2636 |
* file, read the content of the uploaded file, and then you can give the |
|
2637 |
* filename and content to this function, which will add it to the upload |
|
2638 |
* folder. |
|
2639 |
* |
|
2640 |
* The permissions will be set on the new file automatically by this function. |
|
2641 |
* |
|
2642 |
* @since 2.0.0 |
|
2643 |
* |
|
16 | 2644 |
* @param string $name Filename. |
2645 |
* @param null|string $deprecated Never used. Set to null. |
|
2646 |
* @param string $bits File content |
|
2647 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
|
2648 |
* @return array { |
|
2649 |
* Information about the newly-uploaded file. |
|
2650 |
* |
|
2651 |
* @type string $file Filename of the newly-uploaded file. |
|
2652 |
* @type string $url URL of the uploaded file. |
|
2653 |
* @type string $type File type. |
|
2654 |
* @type string|false $error Error message, if there has been an error. |
|
2655 |
* } |
|
0 | 2656 |
*/ |
2657 |
function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { |
|
9 | 2658 |
if ( ! empty( $deprecated ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2659 |
_deprecated_argument( __FUNCTION__, '2.0.0' ); |
9 | 2660 |
} |
2661 |
||
2662 |
if ( empty( $name ) ) { |
|
0 | 2663 |
return array( 'error' => __( 'Empty filename' ) ); |
9 | 2664 |
} |
0 | 2665 |
|
2666 |
$wp_filetype = wp_check_filetype( $name ); |
|
9 | 2667 |
if ( ! $wp_filetype['ext'] && ! current_user_can( 'unfiltered_upload' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2668 |
return array( 'error' => __( 'Sorry, this file type is not permitted for security reasons.' ) ); |
9 | 2669 |
} |
0 | 2670 |
|
2671 |
$upload = wp_upload_dir( $time ); |
|
2672 |
||
16 | 2673 |
if ( false !== $upload['error'] ) { |
0 | 2674 |
return $upload; |
9 | 2675 |
} |
0 | 2676 |
|
5 | 2677 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2678 |
* Filters whether to treat the upload bits as an error. |
5 | 2679 |
* |
16 | 2680 |
* Returning a non-array from the filter will effectively short-circuit preparing the upload bits |
2681 |
* and return that value instead. An error message should be returned as a string. |
|
5 | 2682 |
* |
2683 |
* @since 3.0.0 |
|
2684 |
* |
|
16 | 2685 |
* @param array|string $upload_bits_error An array of upload bits data, or error message to return. |
5 | 2686 |
*/ |
9 | 2687 |
$upload_bits_error = apply_filters( |
2688 |
'wp_upload_bits', |
|
2689 |
array( |
|
2690 |
'name' => $name, |
|
2691 |
'bits' => $bits, |
|
2692 |
'time' => $time, |
|
2693 |
) |
|
2694 |
); |
|
2695 |
if ( ! is_array( $upload_bits_error ) ) { |
|
2696 |
$upload['error'] = $upload_bits_error; |
|
0 | 2697 |
return $upload; |
2698 |
} |
|
2699 |
||
2700 |
$filename = wp_unique_filename( $upload['path'], $name ); |
|
2701 |
||
2702 |
$new_file = $upload['path'] . "/$filename"; |
|
2703 |
if ( ! wp_mkdir_p( dirname( $new_file ) ) ) { |
|
9 | 2704 |
if ( 0 === strpos( $upload['basedir'], ABSPATH ) ) { |
0 | 2705 |
$error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir']; |
9 | 2706 |
} else { |
2707 |
$error_path = wp_basename( $upload['basedir'] ) . $upload['subdir']; |
|
2708 |
} |
|
0 | 2709 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2710 |
$message = sprintf( |
16 | 2711 |
/* translators: %s: Directory path. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2712 |
__( 'Unable to create directory %s. Is its parent directory writable by the server?' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2713 |
$error_path |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2714 |
); |
0 | 2715 |
return array( 'error' => $message ); |
2716 |
} |
|
2717 |
||
16 | 2718 |
$ifp = @fopen( $new_file, 'wb' ); |
9 | 2719 |
if ( ! $ifp ) { |
16 | 2720 |
return array( |
2721 |
/* translators: %s: File name. */ |
|
2722 |
'error' => sprintf( __( 'Could not write file %s' ), $new_file ), |
|
2723 |
); |
|
2724 |
} |
|
2725 |
||
2726 |
fwrite( $ifp, $bits ); |
|
0 | 2727 |
fclose( $ifp ); |
2728 |
clearstatcache(); |
|
2729 |
||
16 | 2730 |
// Set correct file permissions. |
9 | 2731 |
$stat = @ stat( dirname( $new_file ) ); |
0 | 2732 |
$perms = $stat['mode'] & 0007777; |
2733 |
$perms = $perms & 0000666; |
|
16 | 2734 |
chmod( $new_file, $perms ); |
0 | 2735 |
clearstatcache(); |
2736 |
||
16 | 2737 |
// Compute the URL. |
0 | 2738 |
$url = $upload['url'] . "/$filename"; |
2739 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2740 |
/** This filter is documented in wp-admin/includes/file.php */ |
9 | 2741 |
return apply_filters( |
2742 |
'wp_handle_upload', |
|
2743 |
array( |
|
2744 |
'file' => $new_file, |
|
2745 |
'url' => $url, |
|
2746 |
'type' => $wp_filetype['type'], |
|
2747 |
'error' => false, |
|
2748 |
), |
|
2749 |
'sideload' |
|
2750 |
); |
|
0 | 2751 |
} |
2752 |
||
2753 |
/** |
|
2754 |
* Retrieve the file type based on the extension name. |
|
2755 |
* |
|
2756 |
* @since 2.5.0 |
|
2757 |
* |
|
2758 |
* @param string $ext The extension to search. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2759 |
* @return string|void The file type, example: audio, video, document, spreadsheet, etc. |
0 | 2760 |
*/ |
2761 |
function wp_ext2type( $ext ) { |
|
2762 |
$ext = strtolower( $ext ); |
|
5 | 2763 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2764 |
$ext2type = wp_get_ext_types(); |
9 | 2765 |
foreach ( $ext2type as $type => $exts ) { |
16 | 2766 |
if ( in_array( $ext, $exts, true ) ) { |
0 | 2767 |
return $type; |
9 | 2768 |
} |
2769 |
} |
|
0 | 2770 |
} |
2771 |
||
2772 |
/** |
|
2773 |
* Retrieve the file type from the file name. |
|
2774 |
* |
|
2775 |
* You can optionally define the mime array, if needed. |
|
2776 |
* |
|
2777 |
* @since 2.0.4 |
|
2778 |
* |
|
16 | 2779 |
* @param string $filename File name or path. |
2780 |
* @param string[] $mimes Optional. Array of mime types keyed by their file extension regex. |
|
2781 |
* @return array { |
|
2782 |
* Values for the extension and mime type. |
|
2783 |
* |
|
2784 |
* @type string|false $ext File extension, or false if the file doesn't match a mime type. |
|
2785 |
* @type string|false $type File mime type, or false if the file doesn't match a mime type. |
|
2786 |
* } |
|
0 | 2787 |
*/ |
2788 |
function wp_check_filetype( $filename, $mimes = null ) { |
|
9 | 2789 |
if ( empty( $mimes ) ) { |
0 | 2790 |
$mimes = get_allowed_mime_types(); |
9 | 2791 |
} |
0 | 2792 |
$type = false; |
9 | 2793 |
$ext = false; |
0 | 2794 |
|
2795 |
foreach ( $mimes as $ext_preg => $mime_match ) { |
|
2796 |
$ext_preg = '!\.(' . $ext_preg . ')$!i'; |
|
2797 |
if ( preg_match( $ext_preg, $filename, $ext_matches ) ) { |
|
2798 |
$type = $mime_match; |
|
9 | 2799 |
$ext = $ext_matches[1]; |
0 | 2800 |
break; |
2801 |
} |
|
2802 |
} |
|
2803 |
||
2804 |
return compact( 'ext', 'type' ); |
|
2805 |
} |
|
2806 |
||
2807 |
/** |
|
2808 |
* Attempt to determine the real file type of a file. |
|
5 | 2809 |
* |
0 | 2810 |
* If unable to, the file name extension will be used to determine type. |
2811 |
* |
|
2812 |
* If it's determined that the extension does not match the file's real type, |
|
2813 |
* then the "proper_filename" value will be set with a proper filename and extension. |
|
2814 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2815 |
* Currently this function only supports renaming images validated via wp_get_image_mime(). |
0 | 2816 |
* |
2817 |
* @since 3.0.0 |
|
2818 |
* |
|
16 | 2819 |
* @param string $file Full path to the file. |
2820 |
* @param string $filename The name of the file (may differ from $file due to $file being |
|
2821 |
* in a tmp directory). |
|
2822 |
* @param string[] $mimes Optional. Array of mime types keyed by their file extension regex. |
|
2823 |
* @return array { |
|
2824 |
* Values for the extension, mime type, and corrected filename. |
|
2825 |
* |
|
2826 |
* @type string|false $ext File extension, or false if the file doesn't match a mime type. |
|
2827 |
* @type string|false $type File mime type, or false if the file doesn't match a mime type. |
|
2828 |
* @type string|false $proper_filename File name with its correct extension, or false if it cannot be determined. |
|
2829 |
* } |
|
0 | 2830 |
*/ |
2831 |
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { |
|
2832 |
$proper_filename = false; |
|
2833 |
||
16 | 2834 |
// Do basic extension validation and MIME mapping. |
0 | 2835 |
$wp_filetype = wp_check_filetype( $filename, $mimes ); |
9 | 2836 |
$ext = $wp_filetype['ext']; |
2837 |
$type = $wp_filetype['type']; |
|
0 | 2838 |
|
16 | 2839 |
// We can't do any further validation without a file to work with. |
5 | 2840 |
if ( ! file_exists( $file ) ) { |
0 | 2841 |
return compact( 'ext', 'type', 'proper_filename' ); |
5 | 2842 |
} |
0 | 2843 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2844 |
$real_mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2845 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2846 |
// Validate image types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2847 |
if ( $type && 0 === strpos( $type, 'image/' ) ) { |
0 | 2848 |
|
16 | 2849 |
// Attempt to figure out what type of image it actually is. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2850 |
$real_mime = wp_get_image_mime( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2851 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2852 |
if ( $real_mime && $real_mime != $type ) { |
5 | 2853 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2854 |
* Filters the list mapping image mime types to their respective extensions. |
5 | 2855 |
* |
2856 |
* @since 3.0.0 |
|
2857 |
* |
|
16 | 2858 |
* @param array $mime_to_ext Array of image mime types and their matching extensions. |
5 | 2859 |
*/ |
9 | 2860 |
$mime_to_ext = apply_filters( |
2861 |
'getimagesize_mimes_to_exts', |
|
2862 |
array( |
|
2863 |
'image/jpeg' => 'jpg', |
|
2864 |
'image/png' => 'png', |
|
2865 |
'image/gif' => 'gif', |
|
2866 |
'image/bmp' => 'bmp', |
|
2867 |
'image/tiff' => 'tif', |
|
2868 |
) |
|
2869 |
); |
|
0 | 2870 |
|
16 | 2871 |
// Replace whatever is after the last period in the filename with the correct extension. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2872 |
if ( ! empty( $mime_to_ext[ $real_mime ] ) ) { |
0 | 2873 |
$filename_parts = explode( '.', $filename ); |
2874 |
array_pop( $filename_parts ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2875 |
$filename_parts[] = $mime_to_ext[ $real_mime ]; |
9 | 2876 |
$new_filename = implode( '.', $filename_parts ); |
0 | 2877 |
|
5 | 2878 |
if ( $new_filename != $filename ) { |
16 | 2879 |
$proper_filename = $new_filename; // Mark that it changed. |
5 | 2880 |
} |
16 | 2881 |
// Redefine the extension / MIME. |
0 | 2882 |
$wp_filetype = wp_check_filetype( $new_filename, $mimes ); |
9 | 2883 |
$ext = $wp_filetype['ext']; |
2884 |
$type = $wp_filetype['type']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2885 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2886 |
// Reset $real_mime and try validating again. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2887 |
$real_mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2888 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2889 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2890 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2891 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2892 |
// Validate files that didn't get validated during previous checks. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2893 |
if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) { |
9 | 2894 |
$finfo = finfo_open( FILEINFO_MIME_TYPE ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2895 |
$real_mime = finfo_file( $finfo, $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2896 |
finfo_close( $finfo ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2897 |
|
16 | 2898 |
// fileinfo often misidentifies obscure files as one of these types. |
9 | 2899 |
$nonspecific_types = array( |
2900 |
'application/octet-stream', |
|
2901 |
'application/encrypted', |
|
2902 |
'application/CDFV2-encrypted', |
|
2903 |
'application/zip', |
|
2904 |
); |
|
2905 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2906 |
/* |
9 | 2907 |
* If $real_mime doesn't match the content type we're expecting from the file's extension, |
2908 |
* we need to do some additional vetting. Media types and those listed in $nonspecific_types are |
|
2909 |
* allowed some leeway, but anything else must exactly match the real content type. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2910 |
*/ |
9 | 2911 |
if ( in_array( $real_mime, $nonspecific_types, true ) ) { |
2912 |
// File is a non-specific binary type. That's ok if it's a type that generally tends to be binary. |
|
16 | 2913 |
if ( ! in_array( substr( $type, 0, strcspn( $type, '/' ) ), array( 'application', 'video', 'audio' ), true ) ) { |
2914 |
$type = false; |
|
2915 |
$ext = false; |
|
9 | 2916 |
} |
2917 |
} elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) { |
|
2918 |
/* |
|
2919 |
* For these types, only the major type must match the real value. |
|
2920 |
* This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip, |
|
2921 |
* and some media files are commonly named with the wrong extension (.mov instead of .mp4) |
|
2922 |
*/ |
|
2923 |
if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) { |
|
16 | 2924 |
$type = false; |
2925 |
$ext = false; |
|
0 | 2926 |
} |
9 | 2927 |
} elseif ( 'text/plain' === $real_mime ) { |
2928 |
// A few common file types are occasionally detected as text/plain; allow those. |
|
2929 |
if ( ! in_array( |
|
2930 |
$type, |
|
2931 |
array( |
|
2932 |
'text/plain', |
|
2933 |
'text/csv', |
|
2934 |
'text/richtext', |
|
2935 |
'text/tsv', |
|
2936 |
'text/vtt', |
|
16 | 2937 |
), |
2938 |
true |
|
9 | 2939 |
) |
2940 |
) { |
|
16 | 2941 |
$type = false; |
2942 |
$ext = false; |
|
9 | 2943 |
} |
2944 |
} elseif ( 'text/rtf' === $real_mime ) { |
|
2945 |
// Special casing for RTF files. |
|
2946 |
if ( ! in_array( |
|
2947 |
$type, |
|
2948 |
array( |
|
2949 |
'text/rtf', |
|
2950 |
'text/plain', |
|
2951 |
'application/rtf', |
|
16 | 2952 |
), |
2953 |
true |
|
9 | 2954 |
) |
2955 |
) { |
|
16 | 2956 |
$type = false; |
2957 |
$ext = false; |
|
9 | 2958 |
} |
2959 |
} else { |
|
2960 |
if ( $type !== $real_mime ) { |
|
2961 |
/* |
|
2962 |
* Everything else including image/* and application/*: |
|
2963 |
* If the real content type doesn't match the file extension, assume it's dangerous. |
|
2964 |
*/ |
|
16 | 2965 |
$type = false; |
2966 |
$ext = false; |
|
9 | 2967 |
} |
2968 |
} |
|
2969 |
} |
|
2970 |
||
16 | 2971 |
// The mime type must be allowed. |
9 | 2972 |
if ( $type ) { |
2973 |
$allowed = get_allowed_mime_types(); |
|
2974 |
||
16 | 2975 |
if ( ! in_array( $type, $allowed, true ) ) { |
2976 |
$type = false; |
|
2977 |
$ext = false; |
|
0 | 2978 |
} |
2979 |
} |
|
2980 |
||
5 | 2981 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2982 |
* Filters the "real" file type of the given file. |
5 | 2983 |
* |
2984 |
* @since 3.0.0 |
|
9 | 2985 |
* @since 5.1.0 The $real_mime parameter was added. |
5 | 2986 |
* |
16 | 2987 |
* @param array $wp_check_filetype_and_ext { |
2988 |
* Values for the extension, mime type, and corrected filename. |
|
2989 |
* |
|
2990 |
* @type string|false $ext File extension, or false if the file doesn't match a mime type. |
|
2991 |
* @type string|false $type File mime type, or false if the file doesn't match a mime type. |
|
2992 |
* @type string|false $proper_filename File name with its correct extension, or false if it cannot be determined. |
|
2993 |
* } |
|
9 | 2994 |
* @param string $file Full path to the file. |
2995 |
* @param string $filename The name of the file (may differ from $file due to |
|
2996 |
* $file being in a tmp directory). |
|
16 | 2997 |
* @param string[] $mimes Array of mime types keyed by their file extension regex. |
9 | 2998 |
* @param string|bool $real_mime The actual mime type or false if the type cannot be determined. |
5 | 2999 |
*/ |
9 | 3000 |
return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes, $real_mime ); |
0 | 3001 |
} |
3002 |
||
3003 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3004 |
* Returns the real mime type of an image file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3005 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3006 |
* This depends on exif_imagetype() or getimagesize() to determine real mime types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3007 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3008 |
* @since 4.7.1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3009 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3010 |
* @param string $file Full path to the file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3011 |
* @return string|false The actual mime type or false if the type cannot be determined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3012 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3013 |
function wp_get_image_mime( $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3014 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3015 |
* Use exif_imagetype() to check the mimetype if available or fall back to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3016 |
* getimagesize() if exif isn't avaialbe. If either function throws an Exception |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3017 |
* we assume the file could not be validated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3018 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3019 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3020 |
if ( is_callable( 'exif_imagetype' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3021 |
$imagetype = exif_imagetype( $file ); |
9 | 3022 |
$mime = ( $imagetype ) ? image_type_to_mime_type( $imagetype ) : false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3023 |
} elseif ( function_exists( 'getimagesize' ) ) { |
16 | 3024 |
$imagesize = @getimagesize( $file ); |
9 | 3025 |
$mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3026 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3027 |
$mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3028 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3029 |
} catch ( Exception $e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3030 |
$mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3031 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3032 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3033 |
return $mime; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3034 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3035 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3036 |
/** |
0 | 3037 |
* Retrieve list of mime types and file extensions. |
3038 |
* |
|
3039 |
* @since 3.5.0 |
|
16 | 3040 |
* @since 4.2.0 Support was added for GIMP (.xcf) files. |
3041 |
* @since 4.9.2 Support was added for Flac (.flac) files. |
|
3042 |
* @since 4.9.6 Support was added for AAC (.aac) files. |
|
3043 |
* |
|
3044 |
* @return string[] Array of mime types keyed by the file extension regex corresponding to those types. |
|
0 | 3045 |
*/ |
3046 |
function wp_get_mime_types() { |
|
5 | 3047 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3048 |
* Filters the list of mime types and file extensions. |
5 | 3049 |
* |
3050 |
* This filter should be used to add, not remove, mime types. To remove |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3051 |
* mime types, use the {@see 'upload_mimes'} filter. |
5 | 3052 |
* |
3053 |
* @since 3.5.0 |
|
3054 |
* |
|
16 | 3055 |
* @param string[] $wp_get_mime_types Mime types keyed by the file extension regex |
5 | 3056 |
* corresponding to those types. |
3057 |
*/ |
|
9 | 3058 |
return apply_filters( |
3059 |
'mime_types', |
|
3060 |
array( |
|
3061 |
// Image formats. |
|
3062 |
'jpg|jpeg|jpe' => 'image/jpeg', |
|
3063 |
'gif' => 'image/gif', |
|
3064 |
'png' => 'image/png', |
|
3065 |
'bmp' => 'image/bmp', |
|
3066 |
'tiff|tif' => 'image/tiff', |
|
3067 |
'ico' => 'image/x-icon', |
|
16 | 3068 |
'heic' => 'image/heic', |
9 | 3069 |
// Video formats. |
3070 |
'asf|asx' => 'video/x-ms-asf', |
|
3071 |
'wmv' => 'video/x-ms-wmv', |
|
3072 |
'wmx' => 'video/x-ms-wmx', |
|
3073 |
'wm' => 'video/x-ms-wm', |
|
3074 |
'avi' => 'video/avi', |
|
3075 |
'divx' => 'video/divx', |
|
3076 |
'flv' => 'video/x-flv', |
|
3077 |
'mov|qt' => 'video/quicktime', |
|
3078 |
'mpeg|mpg|mpe' => 'video/mpeg', |
|
3079 |
'mp4|m4v' => 'video/mp4', |
|
3080 |
'ogv' => 'video/ogg', |
|
3081 |
'webm' => 'video/webm', |
|
3082 |
'mkv' => 'video/x-matroska', |
|
16 | 3083 |
'3gp|3gpp' => 'video/3gpp', // Can also be audio. |
3084 |
'3g2|3gp2' => 'video/3gpp2', // Can also be audio. |
|
9 | 3085 |
// Text formats. |
3086 |
'txt|asc|c|cc|h|srt' => 'text/plain', |
|
3087 |
'csv' => 'text/csv', |
|
3088 |
'tsv' => 'text/tab-separated-values', |
|
3089 |
'ics' => 'text/calendar', |
|
3090 |
'rtx' => 'text/richtext', |
|
3091 |
'css' => 'text/css', |
|
3092 |
'htm|html' => 'text/html', |
|
3093 |
'vtt' => 'text/vtt', |
|
3094 |
'dfxp' => 'application/ttaf+xml', |
|
3095 |
// Audio formats. |
|
3096 |
'mp3|m4a|m4b' => 'audio/mpeg', |
|
3097 |
'aac' => 'audio/aac', |
|
3098 |
'ra|ram' => 'audio/x-realaudio', |
|
3099 |
'wav' => 'audio/wav', |
|
3100 |
'ogg|oga' => 'audio/ogg', |
|
3101 |
'flac' => 'audio/flac', |
|
3102 |
'mid|midi' => 'audio/midi', |
|
3103 |
'wma' => 'audio/x-ms-wma', |
|
3104 |
'wax' => 'audio/x-ms-wax', |
|
3105 |
'mka' => 'audio/x-matroska', |
|
3106 |
// Misc application formats. |
|
3107 |
'rtf' => 'application/rtf', |
|
3108 |
'js' => 'application/javascript', |
|
3109 |
'pdf' => 'application/pdf', |
|
3110 |
'swf' => 'application/x-shockwave-flash', |
|
3111 |
'class' => 'application/java', |
|
3112 |
'tar' => 'application/x-tar', |
|
3113 |
'zip' => 'application/zip', |
|
3114 |
'gz|gzip' => 'application/x-gzip', |
|
3115 |
'rar' => 'application/rar', |
|
3116 |
'7z' => 'application/x-7z-compressed', |
|
3117 |
'exe' => 'application/x-msdownload', |
|
3118 |
'psd' => 'application/octet-stream', |
|
3119 |
'xcf' => 'application/octet-stream', |
|
3120 |
// MS Office formats. |
|
3121 |
'doc' => 'application/msword', |
|
3122 |
'pot|pps|ppt' => 'application/vnd.ms-powerpoint', |
|
3123 |
'wri' => 'application/vnd.ms-write', |
|
3124 |
'xla|xls|xlt|xlw' => 'application/vnd.ms-excel', |
|
3125 |
'mdb' => 'application/vnd.ms-access', |
|
3126 |
'mpp' => 'application/vnd.ms-project', |
|
3127 |
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
3128 |
'docm' => 'application/vnd.ms-word.document.macroEnabled.12', |
|
3129 |
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
|
3130 |
'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', |
|
3131 |
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
3132 |
'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', |
|
3133 |
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
|
3134 |
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
|
3135 |
'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', |
|
3136 |
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
|
3137 |
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
3138 |
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', |
|
3139 |
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
3140 |
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', |
|
3141 |
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
|
3142 |
'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', |
|
3143 |
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', |
|
3144 |
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
|
3145 |
'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', |
|
3146 |
'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote', |
|
3147 |
'oxps' => 'application/oxps', |
|
3148 |
'xps' => 'application/vnd.ms-xpsdocument', |
|
3149 |
// OpenOffice formats. |
|
3150 |
'odt' => 'application/vnd.oasis.opendocument.text', |
|
3151 |
'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
3152 |
'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
3153 |
'odg' => 'application/vnd.oasis.opendocument.graphics', |
|
3154 |
'odc' => 'application/vnd.oasis.opendocument.chart', |
|
3155 |
'odb' => 'application/vnd.oasis.opendocument.database', |
|
3156 |
'odf' => 'application/vnd.oasis.opendocument.formula', |
|
3157 |
// WordPerfect formats. |
|
3158 |
'wp|wpd' => 'application/wordperfect', |
|
3159 |
// iWork formats. |
|
3160 |
'key' => 'application/vnd.apple.keynote', |
|
3161 |
'numbers' => 'application/vnd.apple.numbers', |
|
3162 |
'pages' => 'application/vnd.apple.pages', |
|
3163 |
) |
|
3164 |
); |
|
0 | 3165 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3166 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3167 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3168 |
* Retrieves the list of common file extensions and their types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3169 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3170 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3171 |
* |
16 | 3172 |
* @return array[] Multi-dimensional array of file extensions types keyed by the type of file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3173 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3174 |
function wp_get_ext_types() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3176 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3177 |
* Filters file type based on the extension name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3178 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3179 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3180 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3181 |
* @see wp_ext2type() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3182 |
* |
16 | 3183 |
* @param array[] $ext2type Multi-dimensional array of file extensions types keyed by the type of file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3184 |
*/ |
9 | 3185 |
return apply_filters( |
3186 |
'ext2type', |
|
3187 |
array( |
|
16 | 3188 |
'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'heic' ), |
9 | 3189 |
'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), |
3190 |
'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), |
|
3191 |
'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ), |
|
3192 |
'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ), |
|
3193 |
'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ), |
|
3194 |
'text' => array( 'asc', 'csv', 'tsv', 'txt' ), |
|
3195 |
'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ), |
|
3196 |
'code' => array( 'css', 'htm', 'html', 'php', 'js' ), |
|
3197 |
) |
|
3198 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3199 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3200 |
|
0 | 3201 |
/** |
3202 |
* Retrieve list of allowed mime types and file extensions. |
|
3203 |
* |
|
3204 |
* @since 2.8.6 |
|
3205 |
* |
|
3206 |
* @param int|WP_User $user Optional. User to check. Defaults to current user. |
|
16 | 3207 |
* @return string[] Array of mime types keyed by the file extension regex corresponding |
3208 |
* to those types. |
|
0 | 3209 |
*/ |
3210 |
function get_allowed_mime_types( $user = null ) { |
|
3211 |
$t = wp_get_mime_types(); |
|
3212 |
||
3213 |
unset( $t['swf'], $t['exe'] ); |
|
9 | 3214 |
if ( function_exists( 'current_user_can' ) ) { |
0 | 3215 |
$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' ); |
9 | 3216 |
} |
0 | 3217 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3218 |
if ( empty( $unfiltered ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3219 |
unset( $t['htm|html'], $t['js'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3220 |
} |
0 | 3221 |
|
5 | 3222 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3223 |
* Filters list of allowed mime types and file extensions. |
5 | 3224 |
* |
3225 |
* @since 2.0.0 |
|
3226 |
* |
|
16 | 3227 |
* @param array $t Mime types keyed by the file extension regex corresponding to those types. |
5 | 3228 |
* @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user). |
3229 |
*/ |
|
0 | 3230 |
return apply_filters( 'upload_mimes', $t, $user ); |
3231 |
} |
|
3232 |
||
3233 |
/** |
|
3234 |
* Display "Are You Sure" message to confirm the action being taken. |
|
3235 |
* |
|
5 | 3236 |
* If the action has the nonce explain message, then it will be displayed |
3237 |
* along with the "Are you sure?" message. |
|
3238 |
* |
|
0 | 3239 |
* @since 2.0.4 |
3240 |
* |
|
3241 |
* @param string $action The nonce action. |
|
3242 |
*/ |
|
3243 |
function wp_nonce_ays( $action ) { |
|
16 | 3244 |
if ( 'log-out' === $action ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3245 |
$html = sprintf( |
16 | 3246 |
/* translators: %s: Site title. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3247 |
__( 'You are attempting to log out of %s' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3248 |
get_bloginfo( 'name' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3249 |
); |
9 | 3250 |
$html .= '</p><p>'; |
5 | 3251 |
$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; |
9 | 3252 |
$html .= sprintf( |
16 | 3253 |
/* translators: %s: Logout URL. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3254 |
__( 'Do you really want to <a href="%s">log out</a>?' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3255 |
wp_logout_url( $redirect_to ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3256 |
); |
0 | 3257 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3258 |
$html = __( 'The link you followed has expired.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3259 |
if ( wp_get_referer() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3260 |
$html .= '</p><p>'; |
9 | 3261 |
$html .= sprintf( |
3262 |
'<a href="%s">%s</a>', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3263 |
esc_url( remove_query_arg( 'updated', wp_get_referer() ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3264 |
__( 'Please try again.' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3265 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3266 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3267 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3268 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3269 |
wp_die( $html, __( 'Something went wrong.' ), 403 ); |
0 | 3270 |
} |
3271 |
||
3272 |
/** |
|
9 | 3273 |
* Kills WordPress execution and displays HTML page with an error message. |
0 | 3274 |
* |
5 | 3275 |
* This function complements the `die()` PHP function. The difference is that |
0 | 3276 |
* HTML will be displayed to the user. It is recommended to use this function |
5 | 3277 |
* only when the execution should not continue any further. It is not recommended |
3278 |
* to call this function very often, and try to handle as many errors as possible |
|
3279 |
* silently or more gracefully. |
|
3280 |
* |
|
3281 |
* As a shorthand, the desired HTTP response code may be passed as an integer to |
|
3282 |
* the `$title` parameter (the default title would apply) or the `$args` parameter. |
|
0 | 3283 |
* |
3284 |
* @since 2.0.4 |
|
5 | 3285 |
* @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept |
3286 |
* an integer to be used as the response code. |
|
9 | 3287 |
* @since 5.1.0 The `$link_url`, `$link_text`, and `$exit` arguments were added. |
16 | 3288 |
* @since 5.3.0 The `$charset` argument was added. |
3289 |
* @since 5.5.0 The `$text_direction` argument has a priority over get_language_attributes() |
|
3290 |
* in the default handler. |
|
3291 |
* |
|
3292 |
* @global WP_Query $wp_query WordPress Query object. |
|
5 | 3293 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3294 |
* @param string|WP_Error $message Optional. Error message. If this is a WP_Error object, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3295 |
* and not an Ajax or XML-RPC request, the error's messages are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3296 |
* Default empty. |
5 | 3297 |
* @param string|int $title Optional. Error title. If `$message` is a `WP_Error` object, |
3298 |
* error data with the key 'title' may be used to specify the title. |
|
3299 |
* If `$title` is an integer, then it is treated as the response |
|
3300 |
* code. Default empty. |
|
3301 |
* @param string|array|int $args { |
|
3302 |
* Optional. Arguments to control behavior. If `$args` is an integer, then it is treated |
|
3303 |
* as the response code. Default empty array. |
|
3304 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3305 |
* @type int $response The HTTP response code. Default 200 for Ajax requests, 500 otherwise. |
9 | 3306 |
* @type string $link_url A URL to include a link to. Only works in combination with $link_text. |
3307 |
* Default empty string. |
|
3308 |
* @type string $link_text A label for the link to include. Only works in combination with $link_url. |
|
3309 |
* Default empty string. |
|
5 | 3310 |
* @type bool $back_link Whether to include a link to go back. Default false. |
16 | 3311 |
* @type string $text_direction The text direction. This is only useful internally, when WordPress is still |
3312 |
* loading and the site's locale is not set up yet. Accepts 'rtl' and 'ltr'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3313 |
* Default is the value of is_rtl(). |
16 | 3314 |
* @type string $charset Character set of the HTML output. Default 'utf-8'. |
9 | 3315 |
* @type string $code Error code to use. Default is 'wp_die', or the main error code if $message |
3316 |
* is a WP_Error. |
|
3317 |
* @type bool $exit Whether to exit the process after completion. Default true. |
|
5 | 3318 |
* } |
0 | 3319 |
*/ |
3320 |
function wp_die( $message = '', $title = '', $args = array() ) { |
|
9 | 3321 |
global $wp_query; |
5 | 3322 |
|
3323 |
if ( is_int( $args ) ) { |
|
3324 |
$args = array( 'response' => $args ); |
|
3325 |
} elseif ( is_int( $title ) ) { |
|
3326 |
$args = array( 'response' => $title ); |
|
3327 |
$title = ''; |
|
3328 |
} |
|
3329 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3330 |
if ( wp_doing_ajax() ) { |
5 | 3331 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3332 |
* Filters the callback for killing WordPress execution for Ajax requests. |
5 | 3333 |
* |
3334 |
* @since 3.4.0 |
|
3335 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3336 |
* @param callable $function Callback function name. |
5 | 3337 |
*/ |
0 | 3338 |
$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' ); |
9 | 3339 |
} elseif ( wp_is_json_request() ) { |
3340 |
/** |
|
3341 |
* Filters the callback for killing WordPress execution for JSON requests. |
|
3342 |
* |
|
3343 |
* @since 5.1.0 |
|
3344 |
* |
|
3345 |
* @param callable $function Callback function name. |
|
3346 |
*/ |
|
3347 |
$function = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' ); |
|
3348 |
} elseif ( wp_is_jsonp_request() ) { |
|
3349 |
/** |
|
3350 |
* Filters the callback for killing WordPress execution for JSONP requests. |
|
3351 |
* |
|
3352 |
* @since 5.2.0 |
|
3353 |
* |
|
3354 |
* @param callable $function Callback function name. |
|
3355 |
*/ |
|
3356 |
$function = apply_filters( 'wp_die_jsonp_handler', '_jsonp_wp_die_handler' ); |
|
5 | 3357 |
} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { |
3358 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3359 |
* Filters the callback for killing WordPress execution for XML-RPC requests. |
5 | 3360 |
* |
3361 |
* @since 3.4.0 |
|
3362 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3363 |
* @param callable $function Callback function name. |
5 | 3364 |
*/ |
0 | 3365 |
$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' ); |
9 | 3366 |
} elseif ( wp_is_xml_request() |
3367 |
|| isset( $wp_query ) && |
|
3368 |
( function_exists( 'is_feed' ) && is_feed() |
|
3369 |
|| function_exists( 'is_comment_feed' ) && is_comment_feed() |
|
3370 |
|| function_exists( 'is_trackback' ) && is_trackback() ) ) { |
|
3371 |
/** |
|
3372 |
* Filters the callback for killing WordPress execution for XML requests. |
|
3373 |
* |
|
3374 |
* @since 5.2.0 |
|
3375 |
* |
|
3376 |
* @param callable $function Callback function name. |
|
3377 |
*/ |
|
3378 |
$function = apply_filters( 'wp_die_xml_handler', '_xml_wp_die_handler' ); |
|
5 | 3379 |
} else { |
3380 |
/** |
|
9 | 3381 |
* Filters the callback for killing WordPress execution for all non-Ajax, non-JSON, non-XML requests. |
5 | 3382 |
* |
3383 |
* @since 3.0.0 |
|
3384 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3385 |
* @param callable $function Callback function name. |
5 | 3386 |
*/ |
0 | 3387 |
$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' ); |
5 | 3388 |
} |
0 | 3389 |
|
3390 |
call_user_func( $function, $message, $title, $args ); |
|
3391 |
} |
|
3392 |
||
3393 |
/** |
|
9 | 3394 |
* Kills WordPress execution and displays HTML page with an error message. |
3395 |
* |
|
3396 |
* This is the default handler for wp_die(). If you want a custom one, |
|
3397 |
* you can override this using the {@see 'wp_die_handler'} filter in wp_die(). |
|
0 | 3398 |
* |
3399 |
* @since 3.0.0 |
|
3400 |
* @access private |
|
3401 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3402 |
* @param string|WP_Error $message Error message or WP_Error object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3403 |
* @param string $title Optional. Error title. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3404 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
0 | 3405 |
*/ |
3406 |
function _default_wp_die_handler( $message, $title = '', $args = array() ) { |
|
16 | 3407 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3408 |
|
3409 |
if ( is_string( $message ) ) { |
|
16 | 3410 |
if ( ! empty( $parsed_args['additional_errors'] ) ) { |
9 | 3411 |
$message = array_merge( |
3412 |
array( $message ), |
|
16 | 3413 |
wp_list_pluck( $parsed_args['additional_errors'], 'message' ) |
9 | 3414 |
); |
3415 |
$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $message ) . "</li>\n\t</ul>"; |
|
0 | 3416 |
} |
16 | 3417 |
|
3418 |
$message = sprintf( |
|
3419 |
'<div class="wp-die-message">%s</div>', |
|
3420 |
$message |
|
3421 |
); |
|
9 | 3422 |
} |
3423 |
||
3424 |
$have_gettext = function_exists( '__' ); |
|
3425 |
||
16 | 3426 |
if ( ! empty( $parsed_args['link_url'] ) && ! empty( $parsed_args['link_text'] ) ) { |
3427 |
$link_url = $parsed_args['link_url']; |
|
9 | 3428 |
if ( function_exists( 'esc_url' ) ) { |
3429 |
$link_url = esc_url( $link_url ); |
|
5 | 3430 |
} |
16 | 3431 |
$link_text = $parsed_args['link_text']; |
9 | 3432 |
$message .= "\n<p><a href='{$link_url}'>{$link_text}</a></p>"; |
0 | 3433 |
} |
3434 |
||
16 | 3435 |
if ( isset( $parsed_args['back_link'] ) && $parsed_args['back_link'] ) { |
9 | 3436 |
$back_text = $have_gettext ? __( '« Back' ) : '« Back'; |
3437 |
$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>"; |
|
0 | 3438 |
} |
3439 |
||
3440 |
if ( ! did_action( 'admin_head' ) ) : |
|
9 | 3441 |
if ( ! headers_sent() ) { |
16 | 3442 |
header( "Content-Type: text/html; charset={$parsed_args['charset']}" ); |
3443 |
status_header( $parsed_args['response'] ); |
|
0 | 3444 |
nocache_headers(); |
3445 |
} |
|
3446 |
||
16 | 3447 |
$text_direction = $parsed_args['text_direction']; |
3448 |
$dir_attr = "dir='$text_direction'"; |
|
3449 |
||
3450 |
// If `text_direction` was not explicitly passed, |
|
3451 |
// use get_language_attributes() if available. |
|
3452 |
if ( empty( $args['text_direction'] ) |
|
3453 |
&& function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) |
|
3454 |
) { |
|
9 | 3455 |
$dir_attr = get_language_attributes(); |
3456 |
} |
|
3457 |
?> |
|
0 | 3458 |
<!DOCTYPE html> |
16 | 3459 |
<html <?php echo $dir_attr; ?>> |
0 | 3460 |
<head> |
16 | 3461 |
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $parsed_args['charset']; ?>" /> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3462 |
<meta name="viewport" content="width=device-width"> |
9 | 3463 |
<?php |
3464 |
if ( function_exists( 'wp_no_robots' ) ) { |
|
3465 |
wp_no_robots(); |
|
3466 |
} |
|
3467 |
?> |
|
3468 |
<title><?php echo $title; ?></title> |
|
0 | 3469 |
<style type="text/css"> |
3470 |
html { |
|
5 | 3471 |
background: #f1f1f1; |
0 | 3472 |
} |
3473 |
body { |
|
3474 |
background: #fff; |
|
5 | 3475 |
color: #444; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3476 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
0 | 3477 |
margin: 2em auto; |
3478 |
padding: 1em 2em; |
|
3479 |
max-width: 700px; |
|
9 | 3480 |
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.13); |
3481 |
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.13); |
|
0 | 3482 |
} |
3483 |
h1 { |
|
3484 |
border-bottom: 1px solid #dadada; |
|
3485 |
clear: both; |
|
3486 |
color: #666; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3487 |
font-size: 24px; |
0 | 3488 |
margin: 30px 0 0 0; |
3489 |
padding: 0; |
|
3490 |
padding-bottom: 7px; |
|
3491 |
} |
|
3492 |
#error-page { |
|
3493 |
margin-top: 50px; |
|
3494 |
} |
|
16 | 3495 |
#error-page p, |
3496 |
#error-page .wp-die-message { |
|
0 | 3497 |
font-size: 14px; |
3498 |
line-height: 1.5; |
|
3499 |
margin: 25px 0 20px; |
|
3500 |
} |
|
3501 |
#error-page code { |
|
3502 |
font-family: Consolas, Monaco, monospace; |
|
3503 |
} |
|
3504 |
ul li { |
|
3505 |
margin-bottom: 10px; |
|
3506 |
font-size: 14px ; |
|
3507 |
} |
|
3508 |
a { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3509 |
color: #0073aa; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3510 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3511 |
a:hover, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3512 |
a:active { |
16 | 3513 |
color: #006799; |
0 | 3514 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3515 |
a:focus { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3516 |
color: #124964; |
9 | 3517 |
-webkit-box-shadow: |
3518 |
0 0 0 1px #5b9dd9, |
|
3519 |
0 0 2px 1px rgba(30, 140, 190, 0.8); |
|
3520 |
box-shadow: |
|
3521 |
0 0 0 1px #5b9dd9, |
|
3522 |
0 0 2px 1px rgba(30, 140, 190, 0.8); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3523 |
outline: none; |
0 | 3524 |
} |
3525 |
.button { |
|
5 | 3526 |
background: #f7f7f7; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3527 |
border: 1px solid #ccc; |
5 | 3528 |
color: #555; |
0 | 3529 |
display: inline-block; |
3530 |
text-decoration: none; |
|
5 | 3531 |
font-size: 13px; |
16 | 3532 |
line-height: 2; |
5 | 3533 |
height: 28px; |
0 | 3534 |
margin: 0; |
3535 |
padding: 0 10px 1px; |
|
3536 |
cursor: pointer; |
|
3537 |
-webkit-border-radius: 3px; |
|
5 | 3538 |
-webkit-appearance: none; |
0 | 3539 |
border-radius: 3px; |
3540 |
white-space: nowrap; |
|
3541 |
-webkit-box-sizing: border-box; |
|
3542 |
-moz-box-sizing: border-box; |
|
3543 |
box-sizing: border-box; |
|
5 | 3544 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3545 |
-webkit-box-shadow: 0 1px 0 #ccc; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3546 |
box-shadow: 0 1px 0 #ccc; |
16 | 3547 |
vertical-align: top; |
0 | 3548 |
} |
3549 |
||
3550 |
.button.button-large { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3551 |
height: 30px; |
16 | 3552 |
line-height: 2.15384615; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3553 |
padding: 0 12px 2px; |
0 | 3554 |
} |
3555 |
||
3556 |
.button:hover, |
|
3557 |
.button:focus { |
|
5 | 3558 |
background: #fafafa; |
0 | 3559 |
border-color: #999; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3560 |
color: #23282d; |
0 | 3561 |
} |
3562 |
||
9 | 3563 |
.button:focus { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3564 |
border-color: #5b9dd9; |
9 | 3565 |
-webkit-box-shadow: 0 0 3px rgba(0, 115, 170, 0.8); |
3566 |
box-shadow: 0 0 3px rgba(0, 115, 170, 0.8); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3567 |
outline: none; |
0 | 3568 |
} |
3569 |
||
3570 |
.button:active { |
|
3571 |
background: #eee; |
|
3572 |
border-color: #999; |
|
16 | 3573 |
-webkit-box-shadow: inset 0 2px 5px -3px rgba(0, 0, 0, 0.5); |
3574 |
box-shadow: inset 0 2px 5px -3px rgba(0, 0, 0, 0.5); |
|
0 | 3575 |
} |
3576 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3577 |
<?php |
16 | 3578 |
if ( 'rtl' === $text_direction ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3579 |
echo 'body { font-family: Tahoma, Arial; }'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3580 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3581 |
?> |
0 | 3582 |
</style> |
3583 |
</head> |
|
3584 |
<body id="error-page"> |
|
3585 |
<?php endif; // ! did_action( 'admin_head' ) ?> |
|
3586 |
<?php echo $message; ?> |
|
3587 |
</body> |
|
3588 |
</html> |
|
9 | 3589 |
<?php |
16 | 3590 |
if ( $parsed_args['exit'] ) { |
9 | 3591 |
die(); |
3592 |
} |
|
3593 |
} |
|
3594 |
||
3595 |
/** |
|
3596 |
* Kills WordPress execution and displays Ajax response with an error message. |
|
3597 |
* |
|
3598 |
* This is the handler for wp_die() when processing Ajax requests. |
|
3599 |
* |
|
3600 |
* @since 3.4.0 |
|
3601 |
* @access private |
|
3602 |
* |
|
3603 |
* @param string $message Error message. |
|
3604 |
* @param string $title Optional. Error title (unused). Default empty. |
|
3605 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3606 |
*/ |
|
3607 |
function _ajax_wp_die_handler( $message, $title = '', $args = array() ) { |
|
16 | 3608 |
// Set default 'response' to 200 for Ajax requests. |
9 | 3609 |
$args = wp_parse_args( |
3610 |
$args, |
|
3611 |
array( 'response' => 200 ) |
|
3612 |
); |
|
3613 |
||
16 | 3614 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3615 |
|
3616 |
if ( ! headers_sent() ) { |
|
3617 |
// This is intentional. For backward-compatibility, support passing null here. |
|
3618 |
if ( null !== $args['response'] ) { |
|
16 | 3619 |
status_header( $parsed_args['response'] ); |
9 | 3620 |
} |
3621 |
nocache_headers(); |
|
3622 |
} |
|
3623 |
||
3624 |
if ( is_scalar( $message ) ) { |
|
3625 |
$message = (string) $message; |
|
3626 |
} else { |
|
3627 |
$message = '0'; |
|
3628 |
} |
|
3629 |
||
16 | 3630 |
if ( $parsed_args['exit'] ) { |
9 | 3631 |
die( $message ); |
3632 |
} |
|
3633 |
||
3634 |
echo $message; |
|
3635 |
} |
|
3636 |
||
3637 |
/** |
|
3638 |
* Kills WordPress execution and displays JSON response with an error message. |
|
3639 |
* |
|
3640 |
* This is the handler for wp_die() when processing JSON requests. |
|
3641 |
* |
|
3642 |
* @since 5.1.0 |
|
3643 |
* @access private |
|
3644 |
* |
|
3645 |
* @param string $message Error message. |
|
3646 |
* @param string $title Optional. Error title. Default empty. |
|
3647 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3648 |
*/ |
|
3649 |
function _json_wp_die_handler( $message, $title = '', $args = array() ) { |
|
16 | 3650 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3651 |
|
3652 |
$data = array( |
|
16 | 3653 |
'code' => $parsed_args['code'], |
9 | 3654 |
'message' => $message, |
3655 |
'data' => array( |
|
16 | 3656 |
'status' => $parsed_args['response'], |
9 | 3657 |
), |
16 | 3658 |
'additional_errors' => $parsed_args['additional_errors'], |
9 | 3659 |
); |
3660 |
||
3661 |
if ( ! headers_sent() ) { |
|
16 | 3662 |
header( "Content-Type: application/json; charset={$parsed_args['charset']}" ); |
3663 |
if ( null !== $parsed_args['response'] ) { |
|
3664 |
status_header( $parsed_args['response'] ); |
|
9 | 3665 |
} |
3666 |
nocache_headers(); |
|
3667 |
} |
|
3668 |
||
3669 |
echo wp_json_encode( $data ); |
|
16 | 3670 |
if ( $parsed_args['exit'] ) { |
9 | 3671 |
die(); |
3672 |
} |
|
3673 |
} |
|
3674 |
||
3675 |
/** |
|
3676 |
* Kills WordPress execution and displays JSONP response with an error message. |
|
3677 |
* |
|
3678 |
* This is the handler for wp_die() when processing JSONP requests. |
|
3679 |
* |
|
3680 |
* @since 5.2.0 |
|
3681 |
* @access private |
|
3682 |
* |
|
3683 |
* @param string $message Error message. |
|
3684 |
* @param string $title Optional. Error title. Default empty. |
|
3685 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3686 |
*/ |
|
3687 |
function _jsonp_wp_die_handler( $message, $title = '', $args = array() ) { |
|
16 | 3688 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3689 |
|
3690 |
$data = array( |
|
16 | 3691 |
'code' => $parsed_args['code'], |
9 | 3692 |
'message' => $message, |
3693 |
'data' => array( |
|
16 | 3694 |
'status' => $parsed_args['response'], |
9 | 3695 |
), |
16 | 3696 |
'additional_errors' => $parsed_args['additional_errors'], |
9 | 3697 |
); |
3698 |
||
3699 |
if ( ! headers_sent() ) { |
|
16 | 3700 |
header( "Content-Type: application/javascript; charset={$parsed_args['charset']}" ); |
9 | 3701 |
header( 'X-Content-Type-Options: nosniff' ); |
3702 |
header( 'X-Robots-Tag: noindex' ); |
|
16 | 3703 |
if ( null !== $parsed_args['response'] ) { |
3704 |
status_header( $parsed_args['response'] ); |
|
9 | 3705 |
} |
3706 |
nocache_headers(); |
|
3707 |
} |
|
3708 |
||
3709 |
$result = wp_json_encode( $data ); |
|
3710 |
$jsonp_callback = $_GET['_jsonp']; |
|
3711 |
echo '/**/' . $jsonp_callback . '(' . $result . ')'; |
|
16 | 3712 |
if ( $parsed_args['exit'] ) { |
9 | 3713 |
die(); |
3714 |
} |
|
3715 |
} |
|
3716 |
||
3717 |
/** |
|
3718 |
* Kills WordPress execution and displays XML response with an error message. |
|
3719 |
* |
|
3720 |
* This is the handler for wp_die() when processing XMLRPC requests. |
|
0 | 3721 |
* |
3722 |
* @since 3.2.0 |
|
3723 |
* @access private |
|
3724 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3725 |
* @global wp_xmlrpc_server $wp_xmlrpc_server |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3726 |
* |
5 | 3727 |
* @param string $message Error message. |
3728 |
* @param string $title Optional. Error title. Default empty. |
|
3729 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
0 | 3730 |
*/ |
3731 |
function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3732 |
global $wp_xmlrpc_server; |
|
9 | 3733 |
|
16 | 3734 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3735 |
|
3736 |
if ( ! headers_sent() ) { |
|
3737 |
nocache_headers(); |
|
3738 |
} |
|
0 | 3739 |
|
3740 |
if ( $wp_xmlrpc_server ) { |
|
16 | 3741 |
$error = new IXR_Error( $parsed_args['response'], $message ); |
0 | 3742 |
$wp_xmlrpc_server->output( $error->getXml() ); |
3743 |
} |
|
16 | 3744 |
if ( $parsed_args['exit'] ) { |
9 | 3745 |
die(); |
3746 |
} |
|
3747 |
} |
|
3748 |
||
3749 |
/** |
|
3750 |
* Kills WordPress execution and displays XML response with an error message. |
|
3751 |
* |
|
3752 |
* This is the handler for wp_die() when processing XML requests. |
|
3753 |
* |
|
3754 |
* @since 5.2.0 |
|
0 | 3755 |
* @access private |
3756 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3757 |
* @param string $message Error message. |
9 | 3758 |
* @param string $title Optional. Error title. Default empty. |
3759 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3760 |
*/ |
|
3761 |
function _xml_wp_die_handler( $message, $title = '', $args = array() ) { |
|
16 | 3762 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
9 | 3763 |
|
3764 |
$message = htmlspecialchars( $message ); |
|
3765 |
$title = htmlspecialchars( $title ); |
|
3766 |
||
3767 |
$xml = <<<EOD |
|
3768 |
<error> |
|
16 | 3769 |
<code>{$parsed_args['code']}</code> |
9 | 3770 |
<title><![CDATA[{$title}]]></title> |
3771 |
<message><![CDATA[{$message}]]></message> |
|
3772 |
<data> |
|
16 | 3773 |
<status>{$parsed_args['response']}</status> |
9 | 3774 |
</data> |
3775 |
</error> |
|
3776 |
||
3777 |
EOD; |
|
3778 |
||
3779 |
if ( ! headers_sent() ) { |
|
16 | 3780 |
header( "Content-Type: text/xml; charset={$parsed_args['charset']}" ); |
3781 |
if ( null !== $parsed_args['response'] ) { |
|
3782 |
status_header( $parsed_args['response'] ); |
|
9 | 3783 |
} |
3784 |
nocache_headers(); |
|
3785 |
} |
|
3786 |
||
3787 |
echo $xml; |
|
16 | 3788 |
if ( $parsed_args['exit'] ) { |
9 | 3789 |
die(); |
3790 |
} |
|
3791 |
} |
|
3792 |
||
3793 |
/** |
|
3794 |
* Kills WordPress execution and displays an error message. |
|
3795 |
* |
|
3796 |
* This is the handler for wp_die() when processing APP requests. |
|
3797 |
* |
|
3798 |
* @since 3.4.0 |
|
3799 |
* @since 5.1.0 Added the $title and $args parameters. |
|
3800 |
* @access private |
|
3801 |
* |
|
3802 |
* @param string $message Optional. Response to print. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3803 |
* @param string $title Optional. Error title (unused). Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3804 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3805 |
*/ |
9 | 3806 |
function _scalar_wp_die_handler( $message = '', $title = '', $args = array() ) { |
16 | 3807 |
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args ); |
3808 |
||
3809 |
if ( $parsed_args['exit'] ) { |
|
9 | 3810 |
if ( is_scalar( $message ) ) { |
3811 |
die( (string) $message ); |
|
3812 |
} |
|
3813 |
die(); |
|
3814 |
} |
|
3815 |
||
3816 |
if ( is_scalar( $message ) ) { |
|
3817 |
echo (string) $message; |
|
3818 |
} |
|
3819 |
} |
|
3820 |
||
3821 |
/** |
|
3822 |
* Processes arguments passed to wp_die() consistently for its handlers. |
|
3823 |
* |
|
3824 |
* @since 5.1.0 |
|
3825 |
* @access private |
|
3826 |
* |
|
16 | 3827 |
* @param string|WP_Error $message Error message or WP_Error object. |
3828 |
* @param string $title Optional. Error title. Default empty. |
|
3829 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3830 |
* @return array { |
|
3831 |
* Processed arguments. |
|
3832 |
* |
|
3833 |
* @type string $0 Error message. |
|
3834 |
* @type string $1 Error title. |
|
3835 |
* @type array $2 Arguments to control behavior. |
|
3836 |
* } |
|
9 | 3837 |
*/ |
3838 |
function _wp_die_process_input( $message, $title = '', $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3839 |
$defaults = array( |
9 | 3840 |
'response' => 0, |
3841 |
'code' => '', |
|
3842 |
'exit' => true, |
|
3843 |
'back_link' => false, |
|
3844 |
'link_url' => '', |
|
3845 |
'link_text' => '', |
|
3846 |
'text_direction' => '', |
|
16 | 3847 |
'charset' => 'utf-8', |
9 | 3848 |
'additional_errors' => array(), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3849 |
); |
9 | 3850 |
|
3851 |
$args = wp_parse_args( $args, $defaults ); |
|
3852 |
||
3853 |
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) { |
|
3854 |
if ( ! empty( $message->errors ) ) { |
|
3855 |
$errors = array(); |
|
3856 |
foreach ( (array) $message->errors as $error_code => $error_messages ) { |
|
3857 |
foreach ( (array) $error_messages as $error_message ) { |
|
3858 |
$errors[] = array( |
|
3859 |
'code' => $error_code, |
|
3860 |
'message' => $error_message, |
|
3861 |
'data' => $message->get_error_data( $error_code ), |
|
3862 |
); |
|
3863 |
} |
|
3864 |
} |
|
3865 |
||
3866 |
$message = $errors[0]['message']; |
|
3867 |
if ( empty( $args['code'] ) ) { |
|
3868 |
$args['code'] = $errors[0]['code']; |
|
3869 |
} |
|
3870 |
if ( empty( $args['response'] ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['status'] ) ) { |
|
3871 |
$args['response'] = $errors[0]['data']['status']; |
|
3872 |
} |
|
3873 |
if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) { |
|
3874 |
$title = $errors[0]['data']['title']; |
|
3875 |
} |
|
3876 |
||
3877 |
unset( $errors[0] ); |
|
3878 |
$args['additional_errors'] = array_values( $errors ); |
|
3879 |
} else { |
|
3880 |
$message = ''; |
|
3881 |
} |
|
3882 |
} |
|
3883 |
||
3884 |
$have_gettext = function_exists( '__' ); |
|
3885 |
||
3886 |
// The $title and these specific $args must always have a non-empty value. |
|
3887 |
if ( empty( $args['code'] ) ) { |
|
3888 |
$args['code'] = 'wp_die'; |
|
3889 |
} |
|
3890 |
if ( empty( $args['response'] ) ) { |
|
3891 |
$args['response'] = 500; |
|
3892 |
} |
|
3893 |
if ( empty( $title ) ) { |
|
3894 |
$title = $have_gettext ? __( 'WordPress › Error' ) : 'WordPress › Error'; |
|
3895 |
} |
|
3896 |
if ( empty( $args['text_direction'] ) || ! in_array( $args['text_direction'], array( 'ltr', 'rtl' ), true ) ) { |
|
3897 |
$args['text_direction'] = 'ltr'; |
|
3898 |
if ( function_exists( 'is_rtl' ) && is_rtl() ) { |
|
3899 |
$args['text_direction'] = 'rtl'; |
|
3900 |
} |
|
3901 |
} |
|
3902 |
||
16 | 3903 |
if ( ! empty( $args['charset'] ) ) { |
3904 |
$args['charset'] = _canonical_charset( $args['charset'] ); |
|
3905 |
} |
|
3906 |
||
9 | 3907 |
return array( $message, $title, $args ); |
0 | 3908 |
} |
3909 |
||
3910 |
/** |
|
5 | 3911 |
* Encode a variable into JSON, with some sanity checks. |
3912 |
* |
|
3913 |
* @since 4.1.0 |
|
16 | 3914 |
* @since 5.3.0 No longer handles support for PHP < 5.6. |
5 | 3915 |
* |
3916 |
* @param mixed $data Variable (usually an array or object) to encode as JSON. |
|
3917 |
* @param int $options Optional. Options to be passed to json_encode(). Default 0. |
|
3918 |
* @param int $depth Optional. Maximum depth to walk through $data. Must be |
|
3919 |
* greater than 0. Default 512. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3920 |
* @return string|false The JSON encoded string, or false if it cannot be encoded. |
5 | 3921 |
*/ |
3922 |
function wp_json_encode( $data, $options = 0, $depth = 512 ) { |
|
16 | 3923 |
$json = json_encode( $data, $options, $depth ); |
5 | 3924 |
|
3925 |
// If json_encode() was successful, no need to do more sanity checking. |
|
16 | 3926 |
if ( false !== $json ) { |
5 | 3927 |
return $json; |
3928 |
} |
|
3929 |
||
3930 |
try { |
|
16 | 3931 |
$data = _wp_json_sanity_check( $data, $depth ); |
5 | 3932 |
} catch ( Exception $e ) { |
3933 |
return false; |
|
3934 |
} |
|
3935 |
||
16 | 3936 |
return json_encode( $data, $options, $depth ); |
5 | 3937 |
} |
3938 |
||
3939 |
/** |
|
3940 |
* Perform sanity checks on data that shall be encoded to JSON. |
|
3941 |
* |
|
3942 |
* @ignore |
|
3943 |
* @since 4.1.0 |
|
3944 |
* @access private |
|
3945 |
* |
|
3946 |
* @see wp_json_encode() |
|
3947 |
* |
|
16 | 3948 |
* @throws Exception If depth limit is reached. |
3949 |
* |
|
5 | 3950 |
* @param mixed $data Variable (usually an array or object) to encode as JSON. |
3951 |
* @param int $depth Maximum depth to walk through $data. Must be greater than 0. |
|
3952 |
* @return mixed The sanitized data that shall be encoded to JSON. |
|
3953 |
*/ |
|
3954 |
function _wp_json_sanity_check( $data, $depth ) { |
|
3955 |
if ( $depth < 0 ) { |
|
3956 |
throw new Exception( 'Reached depth limit' ); |
|
3957 |
} |
|
3958 |
||
3959 |
if ( is_array( $data ) ) { |
|
3960 |
$output = array(); |
|
3961 |
foreach ( $data as $id => $el ) { |
|
3962 |
// Don't forget to sanitize the ID! |
|
3963 |
if ( is_string( $id ) ) { |
|
3964 |
$clean_id = _wp_json_convert_string( $id ); |
|
3965 |
} else { |
|
3966 |
$clean_id = $id; |
|
3967 |
} |
|
3968 |
||
3969 |
// Check the element type, so that we're only recursing if we really have to. |
|
3970 |
if ( is_array( $el ) || is_object( $el ) ) { |
|
3971 |
$output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 ); |
|
3972 |
} elseif ( is_string( $el ) ) { |
|
3973 |
$output[ $clean_id ] = _wp_json_convert_string( $el ); |
|
3974 |
} else { |
|
3975 |
$output[ $clean_id ] = $el; |
|
3976 |
} |
|
3977 |
} |
|
3978 |
} elseif ( is_object( $data ) ) { |
|
3979 |
$output = new stdClass; |
|
3980 |
foreach ( $data as $id => $el ) { |
|
3981 |
if ( is_string( $id ) ) { |
|
3982 |
$clean_id = _wp_json_convert_string( $id ); |
|
3983 |
} else { |
|
3984 |
$clean_id = $id; |
|
3985 |
} |
|
3986 |
||
3987 |
if ( is_array( $el ) || is_object( $el ) ) { |
|
3988 |
$output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 ); |
|
3989 |
} elseif ( is_string( $el ) ) { |
|
3990 |
$output->$clean_id = _wp_json_convert_string( $el ); |
|
3991 |
} else { |
|
3992 |
$output->$clean_id = $el; |
|
3993 |
} |
|
3994 |
} |
|
3995 |
} elseif ( is_string( $data ) ) { |
|
3996 |
return _wp_json_convert_string( $data ); |
|
3997 |
} else { |
|
3998 |
return $data; |
|
3999 |
} |
|
4000 |
||
4001 |
return $output; |
|
4002 |
} |
|
4003 |
||
4004 |
/** |
|
4005 |
* Convert a string to UTF-8, so that it can be safely encoded to JSON. |
|
4006 |
* |
|
4007 |
* @ignore |
|
4008 |
* @since 4.1.0 |
|
4009 |
* @access private |
|
4010 |
* |
|
4011 |
* @see _wp_json_sanity_check() |
|
4012 |
* |
|
4013 |
* @param string $string The string which is to be converted. |
|
4014 |
* @return string The checked string. |
|
4015 |
*/ |
|
4016 |
function _wp_json_convert_string( $string ) { |
|
4017 |
static $use_mb = null; |
|
4018 |
if ( is_null( $use_mb ) ) { |
|
4019 |
$use_mb = function_exists( 'mb_convert_encoding' ); |
|
4020 |
} |
|
4021 |
||
4022 |
if ( $use_mb ) { |
|
4023 |
$encoding = mb_detect_encoding( $string, mb_detect_order(), true ); |
|
4024 |
if ( $encoding ) { |
|
4025 |
return mb_convert_encoding( $string, 'UTF-8', $encoding ); |
|
4026 |
} else { |
|
4027 |
return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' ); |
|
4028 |
} |
|
4029 |
} else { |
|
4030 |
return wp_check_invalid_utf8( $string, true ); |
|
4031 |
} |
|
4032 |
} |
|
4033 |
||
4034 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4035 |
* Prepares response data to be serialized to JSON. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4036 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4037 |
* This supports the JsonSerializable interface for PHP 5.2-5.3 as well. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4038 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4039 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4040 |
* @since 4.4.0 |
16 | 4041 |
* @deprecated 5.3.0 This function is no longer needed as support for PHP 5.2-5.3 |
4042 |
* has been dropped. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4043 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4044 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4045 |
* @param mixed $data Native representation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4046 |
* @return bool|int|float|null|string|array Data ready for `json_encode()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4047 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4048 |
function _wp_json_prepare_data( $data ) { |
16 | 4049 |
_deprecated_function( __FUNCTION__, '5.3.0' ); |
4050 |
return $data; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4051 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4052 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4053 |
/** |
0 | 4054 |
* Send a JSON response back to an Ajax request. |
4055 |
* |
|
4056 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4057 |
* @since 4.7.0 The `$status_code` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4058 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4059 |
* @param mixed $response Variable (usually an array or object) to encode as JSON, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4060 |
* then print and die. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4061 |
* @param int $status_code The HTTP status code to output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4062 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4063 |
function wp_send_json( $response, $status_code = null ) { |
16 | 4064 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
4065 |
_doing_it_wrong( |
|
4066 |
__FUNCTION__, |
|
4067 |
sprintf( |
|
4068 |
/* translators: 1: WP_REST_Response, 2: WP_Error */ |
|
4069 |
__( 'Return a %1$s or %2$s object from your callback when using the REST API.' ), |
|
4070 |
'WP_REST_Response', |
|
4071 |
'WP_Error' |
|
4072 |
), |
|
4073 |
'5.5.0' |
|
4074 |
); |
|
4075 |
} |
|
4076 |
||
4077 |
if ( ! headers_sent() ) { |
|
4078 |
header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
|
4079 |
if ( null !== $status_code ) { |
|
4080 |
status_header( $status_code ); |
|
4081 |
} |
|
4082 |
} |
|
4083 |
||
5 | 4084 |
echo wp_json_encode( $response ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4085 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4086 |
if ( wp_doing_ajax() ) { |
9 | 4087 |
wp_die( |
4088 |
'', |
|
4089 |
'', |
|
4090 |
array( |
|
4091 |
'response' => null, |
|
4092 |
) |
|
4093 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4094 |
} else { |
0 | 4095 |
die; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4096 |
} |
0 | 4097 |
} |
4098 |
||
4099 |
/** |
|
4100 |
* Send a JSON response back to an Ajax request, indicating success. |
|
4101 |
* |
|
4102 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4103 |
* @since 4.7.0 The `$status_code` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4104 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4105 |
* @param mixed $data Data to encode as JSON, then print and die. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4106 |
* @param int $status_code The HTTP status code to output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4107 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4108 |
function wp_send_json_success( $data = null, $status_code = null ) { |
0 | 4109 |
$response = array( 'success' => true ); |
4110 |
||
9 | 4111 |
if ( isset( $data ) ) { |
0 | 4112 |
$response['data'] = $data; |
9 | 4113 |
} |
0 | 4114 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4115 |
wp_send_json( $response, $status_code ); |
0 | 4116 |
} |
4117 |
||
4118 |
/** |
|
4119 |
* Send a JSON response back to an Ajax request, indicating failure. |
|
4120 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4121 |
* If the `$data` parameter is a WP_Error object, the errors |
5 | 4122 |
* within the object are processed and output as an array of error |
4123 |
* codes and corresponding messages. All other types are output |
|
4124 |
* without further processing. |
|
4125 |
* |
|
0 | 4126 |
* @since 3.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4127 |
* @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4128 |
* @since 4.7.0 The `$status_code` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4129 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4130 |
* @param mixed $data Data to encode as JSON, then print and die. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4131 |
* @param int $status_code The HTTP status code to output. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4132 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4133 |
function wp_send_json_error( $data = null, $status_code = null ) { |
0 | 4134 |
$response = array( 'success' => false ); |
4135 |
||
5 | 4136 |
if ( isset( $data ) ) { |
4137 |
if ( is_wp_error( $data ) ) { |
|
4138 |
$result = array(); |
|
4139 |
foreach ( $data->errors as $code => $messages ) { |
|
4140 |
foreach ( $messages as $message ) { |
|
9 | 4141 |
$result[] = array( |
4142 |
'code' => $code, |
|
4143 |
'message' => $message, |
|
4144 |
); |
|
5 | 4145 |
} |
4146 |
} |
|
4147 |
||
4148 |
$response['data'] = $result; |
|
4149 |
} else { |
|
4150 |
$response['data'] = $data; |
|
4151 |
} |
|
4152 |
} |
|
0 | 4153 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4154 |
wp_send_json( $response, $status_code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4155 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4156 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4157 |
/** |
16 | 4158 |
* Checks that a JSONP callback is a valid JavaScript callback name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4159 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4160 |
* Only allows alphanumeric characters and the dot character in callback |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4161 |
* function names. This helps to mitigate XSS attacks caused by directly |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4162 |
* outputting user input. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4163 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4164 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4165 |
* |
16 | 4166 |
* @param string $callback Supplied JSONP callback function name. |
4167 |
* @return bool Whether the callback function name is valid. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4168 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4169 |
function wp_check_jsonp_callback( $callback ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4170 |
if ( ! is_string( $callback ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4171 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4172 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4173 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4174 |
preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4176 |
return 0 === $illegal_char_count; |
0 | 4177 |
} |
4178 |
||
4179 |
/** |
|
4180 |
* Retrieve the WordPress home page URL. |
|
4181 |
* |
|
5 | 4182 |
* If the constant named 'WP_HOME' exists, then it will be used and returned |
4183 |
* by the function. This can be used to counter the redirection on your local |
|
0 | 4184 |
* development environment. |
4185 |
* |
|
5 | 4186 |
* @since 2.2.0 |
0 | 4187 |
* @access private |
5 | 4188 |
* |
4189 |
* @see WP_HOME |
|
4190 |
* |
|
4191 |
* @param string $url URL for the home location. |
|
0 | 4192 |
* @return string Homepage location. |
4193 |
*/ |
|
4194 |
function _config_wp_home( $url = '' ) { |
|
9 | 4195 |
if ( defined( 'WP_HOME' ) ) { |
0 | 4196 |
return untrailingslashit( WP_HOME ); |
9 | 4197 |
} |
0 | 4198 |
return $url; |
4199 |
} |
|
4200 |
||
4201 |
/** |
|
4202 |
* Retrieve the WordPress site URL. |
|
4203 |
* |
|
4204 |
* If the constant named 'WP_SITEURL' is defined, then the value in that |
|
5 | 4205 |
* constant will always be returned. This can be used for debugging a site |
4206 |
* on your localhost while not having to change the database to your URL. |
|
4207 |
* |
|
4208 |
* @since 2.2.0 |
|
0 | 4209 |
* @access private |
5 | 4210 |
* |
4211 |
* @see WP_SITEURL |
|
0 | 4212 |
* |
4213 |
* @param string $url URL to set the WordPress site location. |
|
5 | 4214 |
* @return string The WordPress Site URL. |
0 | 4215 |
*/ |
4216 |
function _config_wp_siteurl( $url = '' ) { |
|
9 | 4217 |
if ( defined( 'WP_SITEURL' ) ) { |
0 | 4218 |
return untrailingslashit( WP_SITEURL ); |
9 | 4219 |
} |
0 | 4220 |
return $url; |
4221 |
} |
|
4222 |
||
4223 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4224 |
* Delete the fresh site option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4225 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4226 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4227 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4228 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4229 |
function _delete_option_fresh_site() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4230 |
update_option( 'fresh_site', '0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4231 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4232 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4233 |
/** |
0 | 4234 |
* Set the localized direction for MCE plugin. |
4235 |
* |
|
5 | 4236 |
* Will only set the direction to 'rtl', if the WordPress locale has |
4237 |
* the text direction set to 'rtl'. |
|
4238 |
* |
|
4239 |
* Fills in the 'directionality' setting, enables the 'directionality' |
|
4240 |
* plugin, and adds the 'ltr' button to 'toolbar1', formerly |
|
4241 |
* 'theme_advanced_buttons1' array keys. These keys are then returned |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4242 |
* in the $mce_init (TinyMCE settings) array. |
5 | 4243 |
* |
4244 |
* @since 2.1.0 |
|
0 | 4245 |
* @access private |
5 | 4246 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4247 |
* @param array $mce_init MCE settings array. |
0 | 4248 |
* @return array Direction set for 'rtl', if needed by locale. |
4249 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4250 |
function _mce_set_direction( $mce_init ) { |
0 | 4251 |
if ( is_rtl() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4252 |
$mce_init['directionality'] = 'rtl'; |
9 | 4253 |
$mce_init['rtl_ui'] = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4254 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4255 |
if ( ! empty( $mce_init['plugins'] ) && strpos( $mce_init['plugins'], 'directionality' ) === false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4256 |
$mce_init['plugins'] .= ',directionality'; |
5 | 4257 |
} |
4258 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4259 |
if ( ! empty( $mce_init['toolbar1'] ) && ! preg_match( '/\bltr\b/', $mce_init['toolbar1'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4260 |
$mce_init['toolbar1'] .= ',ltr'; |
5 | 4261 |
} |
0 | 4262 |
} |
4263 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4264 |
return $mce_init; |
0 | 4265 |
} |
4266 |
||
5 | 4267 |
|
0 | 4268 |
/** |
4269 |
* Convert smiley code to the icon graphic file equivalent. |
|
4270 |
* |
|
4271 |
* You can turn off smilies, by going to the write setting screen and unchecking |
|
4272 |
* the box, or by setting 'use_smilies' option to false or removing the option. |
|
4273 |
* |
|
4274 |
* Plugins may override the default smiley list by setting the $wpsmiliestrans |
|
4275 |
* to an array, with the key the code the blogger types in and the value the |
|
4276 |
* image file. |
|
4277 |
* |
|
4278 |
* The $wp_smiliessearch global is for the regular expression and is set each |
|
4279 |
* time the function is called. |
|
4280 |
* |
|
4281 |
* The full list of smilies can be found in the function and won't be listed in |
|
4282 |
* the description. Probably should create a Codex page for it, so that it is |
|
4283 |
* available. |
|
4284 |
* |
|
4285 |
* @global array $wpsmiliestrans |
|
4286 |
* @global array $wp_smiliessearch |
|
5 | 4287 |
* |
0 | 4288 |
* @since 2.2.0 |
4289 |
*/ |
|
4290 |
function smilies_init() { |
|
4291 |
global $wpsmiliestrans, $wp_smiliessearch; |
|
4292 |
||
16 | 4293 |
// Don't bother setting up smilies if they are disabled. |
9 | 4294 |
if ( ! get_option( 'use_smilies' ) ) { |
0 | 4295 |
return; |
9 | 4296 |
} |
4297 |
||
4298 |
if ( ! isset( $wpsmiliestrans ) ) { |
|
0 | 4299 |
$wpsmiliestrans = array( |
9 | 4300 |
':mrgreen:' => 'mrgreen.png', |
4301 |
':neutral:' => "\xf0\x9f\x98\x90", |
|
4302 |
':twisted:' => "\xf0\x9f\x98\x88", |
|
4303 |
':arrow:' => "\xe2\x9e\xa1", |
|
4304 |
':shock:' => "\xf0\x9f\x98\xaf", |
|
4305 |
':smile:' => "\xf0\x9f\x99\x82", |
|
4306 |
':???:' => "\xf0\x9f\x98\x95", |
|
4307 |
':cool:' => "\xf0\x9f\x98\x8e", |
|
4308 |
':evil:' => "\xf0\x9f\x91\xbf", |
|
4309 |
':grin:' => "\xf0\x9f\x98\x80", |
|
4310 |
':idea:' => "\xf0\x9f\x92\xa1", |
|
4311 |
':oops:' => "\xf0\x9f\x98\xb3", |
|
4312 |
':razz:' => "\xf0\x9f\x98\x9b", |
|
4313 |
':roll:' => "\xf0\x9f\x99\x84", |
|
4314 |
':wink:' => "\xf0\x9f\x98\x89", |
|
4315 |
':cry:' => "\xf0\x9f\x98\xa5", |
|
4316 |
':eek:' => "\xf0\x9f\x98\xae", |
|
4317 |
':lol:' => "\xf0\x9f\x98\x86", |
|
4318 |
':mad:' => "\xf0\x9f\x98\xa1", |
|
4319 |
':sad:' => "\xf0\x9f\x99\x81", |
|
4320 |
'8-)' => "\xf0\x9f\x98\x8e", |
|
4321 |
'8-O' => "\xf0\x9f\x98\xaf", |
|
4322 |
':-(' => "\xf0\x9f\x99\x81", |
|
4323 |
':-)' => "\xf0\x9f\x99\x82", |
|
4324 |
':-?' => "\xf0\x9f\x98\x95", |
|
4325 |
':-D' => "\xf0\x9f\x98\x80", |
|
4326 |
':-P' => "\xf0\x9f\x98\x9b", |
|
4327 |
':-o' => "\xf0\x9f\x98\xae", |
|
4328 |
':-x' => "\xf0\x9f\x98\xa1", |
|
4329 |
':-|' => "\xf0\x9f\x98\x90", |
|
4330 |
';-)' => "\xf0\x9f\x98\x89", |
|
4331 |
// This one transformation breaks regular text with frequency. |
|
4332 |
// '8)' => "\xf0\x9f\x98\x8e", |
|
4333 |
'8O' => "\xf0\x9f\x98\xaf", |
|
4334 |
':(' => "\xf0\x9f\x99\x81", |
|
4335 |
':)' => "\xf0\x9f\x99\x82", |
|
4336 |
':?' => "\xf0\x9f\x98\x95", |
|
4337 |
':D' => "\xf0\x9f\x98\x80", |
|
4338 |
':P' => "\xf0\x9f\x98\x9b", |
|
4339 |
':o' => "\xf0\x9f\x98\xae", |
|
4340 |
':x' => "\xf0\x9f\x98\xa1", |
|
4341 |
':|' => "\xf0\x9f\x98\x90", |
|
4342 |
';)' => "\xf0\x9f\x98\x89", |
|
4343 |
':!:' => "\xe2\x9d\x97", |
|
4344 |
':?:' => "\xe2\x9d\x93", |
|
0 | 4345 |
); |
4346 |
} |
|
4347 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4348 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4349 |
* Filters all the smilies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4350 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4351 |
* This filter must be added before `smilies_init` is run, as |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4352 |
* it is normally only run once to setup the smilies regex. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4353 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4354 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4355 |
* |
16 | 4356 |
* @param string[] $wpsmiliestrans List of the smilies' hexadecimal representations, keyed by their smily code. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4357 |
*/ |
9 | 4358 |
$wpsmiliestrans = apply_filters( 'smilies', $wpsmiliestrans ); |
4359 |
||
4360 |
if ( count( $wpsmiliestrans ) == 0 ) { |
|
0 | 4361 |
return; |
4362 |
} |
|
4363 |
||
4364 |
/* |
|
4365 |
* NOTE: we sort the smilies in reverse key order. This is to make sure |
|
4366 |
* we match the longest possible smilie (:???: vs :?) as the regular |
|
4367 |
* expression used below is first-match |
|
4368 |
*/ |
|
9 | 4369 |
krsort( $wpsmiliestrans ); |
0 | 4370 |
|
5 | 4371 |
$spaces = wp_spaces_regexp(); |
4372 |
||
16 | 4373 |
// Begin first "subpattern". |
5 | 4374 |
$wp_smiliessearch = '/(?<=' . $spaces . '|^)'; |
0 | 4375 |
|
4376 |
$subchar = ''; |
|
4377 |
foreach ( (array) $wpsmiliestrans as $smiley => $img ) { |
|
9 | 4378 |
$firstchar = substr( $smiley, 0, 1 ); |
4379 |
$rest = substr( $smiley, 1 ); |
|
0 | 4380 |
|
16 | 4381 |
// New subpattern? |
9 | 4382 |
if ( $firstchar != $subchar ) { |
16 | 4383 |
if ( '' !== $subchar ) { |
4384 |
$wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern". |
|
4385 |
$wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern". |
|
0 | 4386 |
} |
9 | 4387 |
$subchar = $firstchar; |
4388 |
$wp_smiliessearch .= preg_quote( $firstchar, '/' ) . '(?:'; |
|
0 | 4389 |
} else { |
4390 |
$wp_smiliessearch .= '|'; |
|
4391 |
} |
|
9 | 4392 |
$wp_smiliessearch .= preg_quote( $rest, '/' ); |
0 | 4393 |
} |
4394 |
||
5 | 4395 |
$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m'; |
4396 |
||
0 | 4397 |
} |
4398 |
||
4399 |
/** |
|
4400 |
* Merge user defined arguments into defaults array. |
|
4401 |
* |
|
4402 |
* This function is used throughout WordPress to allow for both string or array |
|
4403 |
* to be merged into another array. |
|
4404 |
* |
|
4405 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4406 |
* @since 2.3.0 `$args` can now also be an object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4407 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4408 |
* @param string|array|object $args Value to merge with $defaults. |
16 | 4409 |
* @param array $defaults Optional. Array that serves as the defaults. |
4410 |
* Default empty array. |
|
0 | 4411 |
* @return array Merged user defined values with defaults. |
4412 |
*/ |
|
16 | 4413 |
function wp_parse_args( $args, $defaults = array() ) { |
9 | 4414 |
if ( is_object( $args ) ) { |
16 | 4415 |
$parsed_args = get_object_vars( $args ); |
9 | 4416 |
} elseif ( is_array( $args ) ) { |
16 | 4417 |
$parsed_args =& $args; |
9 | 4418 |
} else { |
16 | 4419 |
wp_parse_str( $args, $parsed_args ); |
4420 |
} |
|
4421 |
||
4422 |
if ( is_array( $defaults ) && $defaults ) { |
|
4423 |
return array_merge( $defaults, $parsed_args ); |
|
4424 |
} |
|
4425 |
return $parsed_args; |
|
0 | 4426 |
} |
4427 |
||
4428 |
/** |
|
9 | 4429 |
* Cleans up an array, comma- or space-separated list of scalar values. |
4430 |
* |
|
4431 |
* @since 5.1.0 |
|
4432 |
* |
|
4433 |
* @param array|string $list List of values. |
|
4434 |
* @return array Sanitized array of values. |
|
4435 |
*/ |
|
4436 |
function wp_parse_list( $list ) { |
|
4437 |
if ( ! is_array( $list ) ) { |
|
4438 |
return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
4439 |
} |
|
4440 |
||
4441 |
return $list; |
|
4442 |
} |
|
4443 |
||
4444 |
/** |
|
0 | 4445 |
* Clean up an array, comma- or space-separated list of IDs. |
4446 |
* |
|
4447 |
* @since 3.0.0 |
|
4448 |
* |
|
16 | 4449 |
* @param array|string $list List of IDs. |
4450 |
* @return int[] Sanitized array of IDs. |
|
0 | 4451 |
*/ |
4452 |
function wp_parse_id_list( $list ) { |
|
9 | 4453 |
$list = wp_parse_list( $list ); |
4454 |
||
4455 |
return array_unique( array_map( 'absint', $list ) ); |
|
0 | 4456 |
} |
4457 |
||
4458 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4459 |
* Clean up an array, comma- or space-separated list of slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4460 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4461 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4462 |
* |
16 | 4463 |
* @param array|string $list List of slugs. |
4464 |
* @return string[] Sanitized array of slugs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4465 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4466 |
function wp_parse_slug_list( $list ) { |
9 | 4467 |
$list = wp_parse_list( $list ); |
4468 |
||
4469 |
return array_unique( array_map( 'sanitize_title', $list ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4470 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4471 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4472 |
/** |
0 | 4473 |
* Extract a slice of an array, given a list of keys. |
4474 |
* |
|
4475 |
* @since 3.1.0 |
|
4476 |
* |
|
5 | 4477 |
* @param array $array The original array. |
4478 |
* @param array $keys The list of keys. |
|
4479 |
* @return array The array slice. |
|
0 | 4480 |
*/ |
4481 |
function wp_array_slice_assoc( $array, $keys ) { |
|
4482 |
$slice = array(); |
|
9 | 4483 |
foreach ( $keys as $key ) { |
4484 |
if ( isset( $array[ $key ] ) ) { |
|
0 | 4485 |
$slice[ $key ] = $array[ $key ]; |
9 | 4486 |
} |
4487 |
} |
|
0 | 4488 |
|
4489 |
return $slice; |
|
4490 |
} |
|
4491 |
||
4492 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4493 |
* Determines if the variable is a numeric-indexed array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4494 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4495 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4496 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4497 |
* @param mixed $data Variable to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4498 |
* @return bool Whether the variable is a list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4499 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4500 |
function wp_is_numeric_array( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4501 |
if ( ! is_array( $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4502 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4504 |
|
9 | 4505 |
$keys = array_keys( $data ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4506 |
$string_keys = array_filter( $keys, 'is_string' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4507 |
return count( $string_keys ) === 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4508 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4509 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4510 |
/** |
0 | 4511 |
* Filters a list of objects, based on a set of key => value arguments. |
4512 |
* |
|
4513 |
* @since 3.0.0 |
|
9 | 4514 |
* @since 4.7.0 Uses `WP_List_Util` class. |
0 | 4515 |
* |
5 | 4516 |
* @param array $list An array of objects to filter |
4517 |
* @param array $args Optional. An array of key => value arguments to match |
|
4518 |
* against each object. Default empty array. |
|
4519 |
* @param string $operator Optional. The logical operation to perform. 'or' means |
|
4520 |
* only one element from the array needs to match; 'and' |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4521 |
* means all elements must match; 'not' means no elements may |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4522 |
* match. Default 'and'. |
5 | 4523 |
* @param bool|string $field A field from the object to place instead of the entire object. |
4524 |
* Default false. |
|
4525 |
* @return array A list of objects or object fields. |
|
0 | 4526 |
*/ |
4527 |
function wp_filter_object_list( $list, $args = array(), $operator = 'and', $field = false ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4528 |
if ( ! is_array( $list ) ) { |
0 | 4529 |
return array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4530 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4531 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4532 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4533 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4534 |
$util->filter( $args, $operator ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4535 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4536 |
if ( $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4537 |
$util->pluck( $field ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4538 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4539 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4540 |
return $util->get_output(); |
0 | 4541 |
} |
4542 |
||
4543 |
/** |
|
4544 |
* Filters a list of objects, based on a set of key => value arguments. |
|
4545 |
* |
|
4546 |
* @since 3.1.0 |
|
9 | 4547 |
* @since 4.7.0 Uses `WP_List_Util` class. |
0 | 4548 |
* |
5 | 4549 |
* @param array $list An array of objects to filter. |
4550 |
* @param array $args Optional. An array of key => value arguments to match |
|
4551 |
* against each object. Default empty array. |
|
4552 |
* @param string $operator Optional. The logical operation to perform. 'AND' means |
|
4553 |
* all elements from the array must match. 'OR' means only |
|
4554 |
* one element needs to match. 'NOT' means no elements may |
|
4555 |
* match. Default 'AND'. |
|
4556 |
* @return array Array of found values. |
|
0 | 4557 |
*/ |
4558 |
function wp_list_filter( $list, $args = array(), $operator = 'AND' ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4559 |
if ( ! is_array( $list ) ) { |
0 | 4560 |
return array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4561 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4562 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4563 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4564 |
return $util->filter( $args, $operator ); |
0 | 4565 |
} |
4566 |
||
4567 |
/** |
|
4568 |
* Pluck a certain field out of each object in a list. |
|
4569 |
* |
|
5 | 4570 |
* This has the same functionality and prototype of |
4571 |
* array_column() (PHP 5.5) but also supports objects. |
|
4572 |
* |
|
0 | 4573 |
* @since 3.1.0 |
5 | 4574 |
* @since 4.0.0 $index_key parameter added. |
9 | 4575 |
* @since 4.7.0 Uses `WP_List_Util` class. |
5 | 4576 |
* |
4577 |
* @param array $list List of objects or arrays |
|
4578 |
* @param int|string $field Field from the object to place instead of the entire object |
|
4579 |
* @param int|string $index_key Optional. Field from the object to use as keys for the new array. |
|
4580 |
* Default null. |
|
4581 |
* @return array Array of found values. If `$index_key` is set, an array of found values with keys |
|
4582 |
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original |
|
4583 |
* `$list` will be preserved in the results. |
|
0 | 4584 |
*/ |
5 | 4585 |
function wp_list_pluck( $list, $field, $index_key = null ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4586 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4587 |
return $util->pluck( $field, $index_key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4588 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4589 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4590 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4591 |
* Sorts a list of objects, based on one or more orderby arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4592 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4593 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4594 |
* |
9 | 4595 |
* @param array $list An array of objects to sort. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4596 |
* @param string|array $orderby Optional. Either the field name to order by or an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4597 |
* of multiple orderby fields as $orderby => $order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4598 |
* @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4599 |
* is a string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4600 |
* @param bool $preserve_keys Optional. Whether to preserve keys. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4601 |
* @return array The sorted array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4602 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4603 |
function wp_list_sort( $list, $orderby = array(), $order = 'ASC', $preserve_keys = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4604 |
if ( ! is_array( $list ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4605 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4606 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4607 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4608 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4609 |
return $util->sort( $orderby, $order, $preserve_keys ); |
0 | 4610 |
} |
4611 |
||
4612 |
/** |
|
4613 |
* Determines if Widgets library should be loaded. |
|
4614 |
* |
|
5 | 4615 |
* Checks to make sure that the widgets library hasn't already been loaded. |
4616 |
* If it hasn't, then it will load the widgets library and run an action hook. |
|
0 | 4617 |
* |
4618 |
* @since 2.2.0 |
|
4619 |
*/ |
|
4620 |
function wp_maybe_load_widgets() { |
|
5 | 4621 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4622 |
* Filters whether to load the Widgets library. |
5 | 4623 |
* |
16 | 4624 |
* Returning a falsey value from the filter will effectively short-circuit |
5 | 4625 |
* the Widgets library from loading. |
4626 |
* |
|
4627 |
* @since 2.8.0 |
|
4628 |
* |
|
4629 |
* @param bool $wp_maybe_load_widgets Whether to load the Widgets library. |
|
4630 |
* Default true. |
|
4631 |
*/ |
|
4632 |
if ( ! apply_filters( 'load_default_widgets', true ) ) { |
|
0 | 4633 |
return; |
5 | 4634 |
} |
4635 |
||
16 | 4636 |
require_once ABSPATH . WPINC . '/default-widgets.php'; |
5 | 4637 |
|
0 | 4638 |
add_action( '_admin_menu', 'wp_widgets_add_menu' ); |
4639 |
} |
|
4640 |
||
4641 |
/** |
|
4642 |
* Append the Widgets menu to the themes main menu. |
|
4643 |
* |
|
4644 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4645 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4646 |
* @global array $submenu |
0 | 4647 |
*/ |
4648 |
function wp_widgets_add_menu() { |
|
4649 |
global $submenu; |
|
4650 |
||
9 | 4651 |
if ( ! current_theme_supports( 'widgets' ) ) { |
0 | 4652 |
return; |
9 | 4653 |
} |
0 | 4654 |
|
4655 |
$submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' ); |
|
4656 |
ksort( $submenu['themes.php'], SORT_NUMERIC ); |
|
4657 |
} |
|
4658 |
||
4659 |
/** |
|
4660 |
* Flush all output buffers for PHP 5.2. |
|
4661 |
* |
|
5 | 4662 |
* Make sure all output buffers are flushed before our singletons are destroyed. |
0 | 4663 |
* |
4664 |
* @since 2.2.0 |
|
4665 |
*/ |
|
4666 |
function wp_ob_end_flush_all() { |
|
4667 |
$levels = ob_get_level(); |
|
9 | 4668 |
for ( $i = 0; $i < $levels; $i++ ) { |
0 | 4669 |
ob_end_flush(); |
9 | 4670 |
} |
0 | 4671 |
} |
4672 |
||
4673 |
/** |
|
4674 |
* Load custom DB error or display WordPress DB error. |
|
4675 |
* |
|
4676 |
* If a file exists in the wp-content directory named db-error.php, then it will |
|
4677 |
* be loaded instead of displaying the WordPress DB error. If it is not found, |
|
4678 |
* then the WordPress DB error will be displayed instead. |
|
4679 |
* |
|
4680 |
* The WordPress DB error sets the HTTP status header to 500 to try to prevent |
|
4681 |
* search engines from caching the message. Custom DB messages should do the |
|
4682 |
* same. |
|
4683 |
* |
|
4684 |
* This function was backported to WordPress 2.3.2, but originally was added |
|
4685 |
* in WordPress 2.5.0. |
|
4686 |
* |
|
4687 |
* @since 2.3.2 |
|
5 | 4688 |
* |
4689 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4690 |
*/ |
4691 |
function dead_db() { |
|
4692 |
global $wpdb; |
|
4693 |
||
5 | 4694 |
wp_load_translations_early(); |
4695 |
||
0 | 4696 |
// Load custom DB error template, if present. |
4697 |
if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
|
16 | 4698 |
require_once WP_CONTENT_DIR . '/db-error.php'; |
0 | 4699 |
die(); |
4700 |
} |
|
4701 |
||
4702 |
// If installing or in the admin, provide the verbose message. |
|
9 | 4703 |
if ( wp_installing() || defined( 'WP_ADMIN' ) ) { |
4704 |
wp_die( $wpdb->error ); |
|
4705 |
} |
|
0 | 4706 |
|
4707 |
// Otherwise, be terse. |
|
9 | 4708 |
wp_die( '<h1>' . __( 'Error establishing a database connection' ) . '</h1>', __( 'Database Error' ) ); |
0 | 4709 |
} |
4710 |
||
4711 |
/** |
|
5 | 4712 |
* Convert a value to non-negative integer. |
0 | 4713 |
* |
4714 |
* @since 2.5.0 |
|
4715 |
* |
|
5 | 4716 |
* @param mixed $maybeint Data you wish to have converted to a non-negative integer. |
4717 |
* @return int A non-negative integer. |
|
0 | 4718 |
*/ |
4719 |
function absint( $maybeint ) { |
|
4720 |
return abs( intval( $maybeint ) ); |
|
4721 |
} |
|
4722 |
||
4723 |
/** |
|
5 | 4724 |
* Mark a function as deprecated and inform when it has been used. |
0 | 4725 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4726 |
* There is a {@see 'hook deprecated_function_run'} that will be called that can be used |
0 | 4727 |
* to get the backtrace up to what file and function called the deprecated |
4728 |
* function. |
|
4729 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4730 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 4731 |
* |
4732 |
* This function is to be used in every function that is deprecated. |
|
4733 |
* |
|
4734 |
* @since 2.5.0 |
|
16 | 4735 |
* @since 5.4.0 This function is no longer marked as "private". |
4736 |
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE). |
|
0 | 4737 |
* |
5 | 4738 |
* @param string $function The function that was called. |
4739 |
* @param string $version The version of WordPress that deprecated the function. |
|
16 | 4740 |
* @param string $replacement Optional. The function that should have been called. Default empty. |
4741 |
*/ |
|
4742 |
function _deprecated_function( $function, $version, $replacement = '' ) { |
|
0 | 4743 |
|
5 | 4744 |
/** |
4745 |
* Fires when a deprecated function is called. |
|
4746 |
* |
|
4747 |
* @since 2.5.0 |
|
4748 |
* |
|
4749 |
* @param string $function The function that was called. |
|
4750 |
* @param string $replacement The function that should have been called. |
|
4751 |
* @param string $version The version of WordPress that deprecated the function. |
|
4752 |
*/ |
|
0 | 4753 |
do_action( 'deprecated_function_run', $function, $replacement, $version ); |
4754 |
||
5 | 4755 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4756 |
* Filters whether to trigger an error for deprecated functions. |
5 | 4757 |
* |
4758 |
* @since 2.5.0 |
|
4759 |
* |
|
4760 |
* @param bool $trigger Whether to trigger the error for deprecated functions. Default true. |
|
4761 |
*/ |
|
0 | 4762 |
if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) { |
4763 |
if ( function_exists( '__' ) ) { |
|
16 | 4764 |
if ( $replacement ) { |
4765 |
trigger_error( |
|
4766 |
sprintf( |
|
4767 |
/* translators: 1: PHP function name, 2: Version number, 3: Alternative function name. */ |
|
4768 |
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), |
|
4769 |
$function, |
|
4770 |
$version, |
|
4771 |
$replacement |
|
4772 |
), |
|
4773 |
E_USER_DEPRECATED |
|
4774 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4775 |
} else { |
16 | 4776 |
trigger_error( |
4777 |
sprintf( |
|
4778 |
/* translators: 1: PHP function name, 2: Version number. */ |
|
4779 |
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), |
|
4780 |
$function, |
|
4781 |
$version |
|
4782 |
), |
|
4783 |
E_USER_DEPRECATED |
|
4784 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4785 |
} |
0 | 4786 |
} else { |
16 | 4787 |
if ( $replacement ) { |
4788 |
trigger_error( |
|
4789 |
sprintf( |
|
4790 |
'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', |
|
4791 |
$function, |
|
4792 |
$version, |
|
4793 |
$replacement |
|
4794 |
), |
|
4795 |
E_USER_DEPRECATED |
|
4796 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4797 |
} else { |
16 | 4798 |
trigger_error( |
4799 |
sprintf( |
|
4800 |
'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', |
|
4801 |
$function, |
|
4802 |
$version |
|
4803 |
), |
|
4804 |
E_USER_DEPRECATED |
|
4805 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4806 |
} |
0 | 4807 |
} |
4808 |
} |
|
4809 |
} |
|
4810 |
||
4811 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4812 |
* Marks a constructor as deprecated and informs when it has been used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4813 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4814 |
* Similar to _deprecated_function(), but with different strings. Used to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4815 |
* remove PHP4 style constructors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4816 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4817 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4818 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4819 |
* This function is to be used in every PHP4 style constructor method that is deprecated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4820 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4821 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4822 |
* @since 4.5.0 Added the `$parent_class` parameter. |
16 | 4823 |
* @since 5.4.0 This function is no longer marked as "private". |
4824 |
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4825 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4826 |
* @param string $class The class containing the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4827 |
* @param string $version The version of WordPress that deprecated the function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4828 |
* @param string $parent_class Optional. The parent class calling the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4829 |
* Default empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4830 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4831 |
function _deprecated_constructor( $class, $version, $parent_class = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4832 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4833 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4834 |
* Fires when a deprecated constructor is called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4835 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4836 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4837 |
* @since 4.5.0 Added the `$parent_class` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4838 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4839 |
* @param string $class The class containing the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4840 |
* @param string $version The version of WordPress that deprecated the function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4841 |
* @param string $parent_class The parent class calling the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4842 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4843 |
do_action( 'deprecated_constructor_run', $class, $version, $parent_class ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4844 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4845 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4846 |
* Filters whether to trigger an error for deprecated functions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4847 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4848 |
* `WP_DEBUG` must be true in addition to the filter evaluating to true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4849 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4850 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4851 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4852 |
* @param bool $trigger Whether to trigger the error for deprecated functions. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4853 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4854 |
if ( WP_DEBUG && apply_filters( 'deprecated_constructor_trigger_error', true ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4855 |
if ( function_exists( '__' ) ) { |
16 | 4856 |
if ( $parent_class ) { |
9 | 4857 |
trigger_error( |
4858 |
sprintf( |
|
16 | 4859 |
/* translators: 1: PHP class name, 2: PHP parent class name, 3: Version number, 4: __construct() method. */ |
9 | 4860 |
__( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.' ), |
4861 |
$class, |
|
4862 |
$parent_class, |
|
4863 |
$version, |
|
16 | 4864 |
'<code>__construct()</code>' |
4865 |
), |
|
4866 |
E_USER_DEPRECATED |
|
9 | 4867 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4868 |
} else { |
9 | 4869 |
trigger_error( |
4870 |
sprintf( |
|
16 | 4871 |
/* translators: 1: PHP class name, 2: Version number, 3: __construct() method. */ |
9 | 4872 |
__( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), |
4873 |
$class, |
|
4874 |
$version, |
|
16 | 4875 |
'<code>__construct()</code>' |
4876 |
), |
|
4877 |
E_USER_DEPRECATED |
|
9 | 4878 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4879 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4880 |
} else { |
16 | 4881 |
if ( $parent_class ) { |
9 | 4882 |
trigger_error( |
4883 |
sprintf( |
|
4884 |
'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.', |
|
4885 |
$class, |
|
4886 |
$parent_class, |
|
4887 |
$version, |
|
16 | 4888 |
'<code>__construct()</code>' |
4889 |
), |
|
4890 |
E_USER_DEPRECATED |
|
9 | 4891 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4892 |
} else { |
9 | 4893 |
trigger_error( |
4894 |
sprintf( |
|
4895 |
'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', |
|
4896 |
$class, |
|
4897 |
$version, |
|
16 | 4898 |
'<code>__construct()</code>' |
4899 |
), |
|
4900 |
E_USER_DEPRECATED |
|
9 | 4901 |
); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4902 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4903 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4904 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4905 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4906 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4907 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4908 |
/** |
5 | 4909 |
* Mark a file as deprecated and inform when it has been used. |
0 | 4910 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4911 |
* There is a hook {@see 'deprecated_file_included'} that will be called that can be used |
0 | 4912 |
* to get the backtrace up to what file and function included the deprecated |
4913 |
* file. |
|
4914 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4915 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 4916 |
* |
4917 |
* This function is to be used in every file that is deprecated. |
|
4918 |
* |
|
4919 |
* @since 2.5.0 |
|
16 | 4920 |
* @since 5.4.0 This function is no longer marked as "private". |
4921 |
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE). |
|
0 | 4922 |
* |
5 | 4923 |
* @param string $file The file that was included. |
4924 |
* @param string $version The version of WordPress that deprecated the file. |
|
4925 |
* @param string $replacement Optional. The file that should have been included based on ABSPATH. |
|
16 | 4926 |
* Default empty. |
5 | 4927 |
* @param string $message Optional. A message regarding the change. Default empty. |
0 | 4928 |
*/ |
16 | 4929 |
function _deprecated_file( $file, $version, $replacement = '', $message = '' ) { |
0 | 4930 |
|
5 | 4931 |
/** |
4932 |
* Fires when a deprecated file is called. |
|
4933 |
* |
|
4934 |
* @since 2.5.0 |
|
4935 |
* |
|
4936 |
* @param string $file The file that was called. |
|
4937 |
* @param string $replacement The file that should have been included based on ABSPATH. |
|
4938 |
* @param string $version The version of WordPress that deprecated the file. |
|
4939 |
* @param string $message A message regarding the change. |
|
4940 |
*/ |
|
0 | 4941 |
do_action( 'deprecated_file_included', $file, $replacement, $version, $message ); |
4942 |
||
5 | 4943 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4944 |
* Filters whether to trigger an error for deprecated files. |
5 | 4945 |
* |
4946 |
* @since 2.5.0 |
|
4947 |
* |
|
4948 |
* @param bool $trigger Whether to trigger the error for deprecated files. Default true. |
|
4949 |
*/ |
|
0 | 4950 |
if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) { |
4951 |
$message = empty( $message ) ? '' : ' ' . $message; |
|
16 | 4952 |
|
0 | 4953 |
if ( function_exists( '__' ) ) { |
16 | 4954 |
if ( $replacement ) { |
4955 |
trigger_error( |
|
4956 |
sprintf( |
|
4957 |
/* translators: 1: PHP file name, 2: Version number, 3: Alternative file name. */ |
|
4958 |
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), |
|
4959 |
$file, |
|
4960 |
$version, |
|
4961 |
$replacement |
|
4962 |
) . $message, |
|
4963 |
E_USER_DEPRECATED |
|
4964 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4965 |
} else { |
16 | 4966 |
trigger_error( |
4967 |
sprintf( |
|
4968 |
/* translators: 1: PHP file name, 2: Version number. */ |
|
4969 |
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), |
|
4970 |
$file, |
|
4971 |
$version |
|
4972 |
) . $message, |
|
4973 |
E_USER_DEPRECATED |
|
4974 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4975 |
} |
0 | 4976 |
} else { |
16 | 4977 |
if ( $replacement ) { |
4978 |
trigger_error( |
|
4979 |
sprintf( |
|
4980 |
'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', |
|
4981 |
$file, |
|
4982 |
$version, |
|
4983 |
$replacement |
|
4984 |
) . $message, |
|
4985 |
E_USER_DEPRECATED |
|
4986 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4987 |
} else { |
16 | 4988 |
trigger_error( |
4989 |
sprintf( |
|
4990 |
'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', |
|
4991 |
$file, |
|
4992 |
$version |
|
4993 |
) . $message, |
|
4994 |
E_USER_DEPRECATED |
|
4995 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4996 |
} |
0 | 4997 |
} |
4998 |
} |
|
4999 |
} |
|
5000 |
/** |
|
5 | 5001 |
* Mark a function argument as deprecated and inform when it has been used. |
0 | 5002 |
* |
5003 |
* This function is to be used whenever a deprecated function argument is used. |
|
5004 |
* Before this function is called, the argument must be checked for whether it was |
|
5005 |
* used by comparing it to its default value or evaluating whether it is empty. |
|
5006 |
* For example: |
|
5 | 5007 |
* |
5008 |
* if ( ! empty( $deprecated ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5009 |
* _deprecated_argument( __FUNCTION__, '3.0.0' ); |
5 | 5010 |
* } |
5011 |
* |
|
0 | 5012 |
* There is a hook deprecated_argument_run that will be called that can be used |
5013 |
* to get the backtrace up to what file and function used the deprecated |
|
5014 |
* argument. |
|
5015 |
* |
|
5016 |
* The current behavior is to trigger a user error if WP_DEBUG is true. |
|
5017 |
* |
|
5018 |
* @since 3.0.0 |
|
16 | 5019 |
* @since 5.4.0 This function is no longer marked as "private". |
5020 |
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE). |
|
0 | 5021 |
* |
5 | 5022 |
* @param string $function The function that was called. |
5023 |
* @param string $version The version of WordPress that deprecated the argument used. |
|
16 | 5024 |
* @param string $message Optional. A message regarding the change. Default empty. |
5025 |
*/ |
|
5026 |
function _deprecated_argument( $function, $version, $message = '' ) { |
|
0 | 5027 |
|
5 | 5028 |
/** |
5029 |
* Fires when a deprecated argument is called. |
|
5030 |
* |
|
5031 |
* @since 3.0.0 |
|
5032 |
* |
|
5033 |
* @param string $function The function that was called. |
|
5034 |
* @param string $message A message regarding the change. |
|
5035 |
* @param string $version The version of WordPress that deprecated the argument used. |
|
5036 |
*/ |
|
0 | 5037 |
do_action( 'deprecated_argument_run', $function, $message, $version ); |
5038 |
||
5 | 5039 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5040 |
* Filters whether to trigger an error for deprecated arguments. |
5 | 5041 |
* |
5042 |
* @since 3.0.0 |
|
5043 |
* |
|
5044 |
* @param bool $trigger Whether to trigger the error for deprecated arguments. Default true. |
|
5045 |
*/ |
|
0 | 5046 |
if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) { |
5047 |
if ( function_exists( '__' ) ) { |
|
16 | 5048 |
if ( $message ) { |
5049 |
trigger_error( |
|
5050 |
sprintf( |
|
5051 |
/* translators: 1: PHP function name, 2: Version number, 3: Optional message regarding the change. */ |
|
5052 |
__( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s' ), |
|
5053 |
$function, |
|
5054 |
$version, |
|
5055 |
$message |
|
5056 |
), |
|
5057 |
E_USER_DEPRECATED |
|
5058 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5059 |
} else { |
16 | 5060 |
trigger_error( |
5061 |
sprintf( |
|
5062 |
/* translators: 1: PHP function name, 2: Version number. */ |
|
5063 |
__( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.' ), |
|
5064 |
$function, |
|
5065 |
$version |
|
5066 |
), |
|
5067 |
E_USER_DEPRECATED |
|
5068 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5069 |
} |
0 | 5070 |
} else { |
16 | 5071 |
if ( $message ) { |
5072 |
trigger_error( |
|
5073 |
sprintf( |
|
5074 |
'%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', |
|
5075 |
$function, |
|
5076 |
$version, |
|
5077 |
$message |
|
5078 |
), |
|
5079 |
E_USER_DEPRECATED |
|
5080 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5081 |
} else { |
16 | 5082 |
trigger_error( |
5083 |
sprintf( |
|
5084 |
'%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.', |
|
5085 |
$function, |
|
5086 |
$version |
|
5087 |
), |
|
5088 |
E_USER_DEPRECATED |
|
5089 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5090 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5091 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5092 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5094 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5095 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5096 |
* Marks a deprecated action or filter hook as deprecated and throws a notice. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5097 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5098 |
* Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5099 |
* the deprecated hook was called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5101 |
* Default behavior is to trigger a user error if `WP_DEBUG` is true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5102 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5103 |
* This function is called by the do_action_deprecated() and apply_filters_deprecated() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5104 |
* functions, and so generally does not need to be called directly. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5105 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5106 |
* @since 4.6.0 |
16 | 5107 |
* @since 5.4.0 The error type is now classified as E_USER_DEPRECATED (used to default to E_USER_NOTICE). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5108 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5109 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5110 |
* @param string $hook The hook that was used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5111 |
* @param string $version The version of WordPress that deprecated the hook. |
16 | 5112 |
* @param string $replacement Optional. The hook that should have been used. Default empty. |
5113 |
* @param string $message Optional. A message regarding the change. Default empty. |
|
5114 |
*/ |
|
5115 |
function _deprecated_hook( $hook, $version, $replacement = '', $message = '' ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5116 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5117 |
* Fires when a deprecated hook is called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5118 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5119 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5120 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5121 |
* @param string $hook The hook that was called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5122 |
* @param string $replacement The hook that should be used as a replacement. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5123 |
* @param string $version The version of WordPress that deprecated the argument used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5124 |
* @param string $message A message regarding the change. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5125 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5126 |
do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5127 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5128 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5129 |
* Filters whether to trigger deprecated hook errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5130 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5131 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5132 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5133 |
* @param bool $trigger Whether to trigger deprecated hook errors. Requires |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5134 |
* `WP_DEBUG` to be defined true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5135 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5136 |
if ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5137 |
$message = empty( $message ) ? '' : ' ' . $message; |
16 | 5138 |
|
5139 |
if ( $replacement ) { |
|
5140 |
trigger_error( |
|
5141 |
sprintf( |
|
5142 |
/* translators: 1: WordPress hook name, 2: Version number, 3: Alternative hook name. */ |
|
5143 |
__( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), |
|
5144 |
$hook, |
|
5145 |
$version, |
|
5146 |
$replacement |
|
5147 |
) . $message, |
|
5148 |
E_USER_DEPRECATED |
|
5149 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5150 |
} else { |
16 | 5151 |
trigger_error( |
5152 |
sprintf( |
|
5153 |
/* translators: 1: WordPress hook name, 2: Version number. */ |
|
5154 |
__( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), |
|
5155 |
$hook, |
|
5156 |
$version |
|
5157 |
) . $message, |
|
5158 |
E_USER_DEPRECATED |
|
5159 |
); |
|
0 | 5160 |
} |
5161 |
} |
|
5162 |
} |
|
5163 |
||
5164 |
/** |
|
5 | 5165 |
* Mark something as being incorrectly called. |
0 | 5166 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5167 |
* There is a hook {@see 'doing_it_wrong_run'} that will be called that can be used |
0 | 5168 |
* to get the backtrace up to what file and function called the deprecated |
5169 |
* function. |
|
5170 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5171 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 5172 |
* |
5173 |
* @since 3.1.0 |
|
16 | 5174 |
* @since 5.4.0 This function is no longer marked as "private". |
0 | 5175 |
* |
5176 |
* @param string $function The function that was called. |
|
5 | 5177 |
* @param string $message A message explaining what has been done incorrectly. |
5178 |
* @param string $version The version of WordPress where the message was added. |
|
0 | 5179 |
*/ |
5180 |
function _doing_it_wrong( $function, $message, $version ) { |
|
5181 |
||
5 | 5182 |
/** |
5183 |
* Fires when the given function is being used incorrectly. |
|
5184 |
* |
|
5185 |
* @since 3.1.0 |
|
5186 |
* |
|
5187 |
* @param string $function The function that was called. |
|
5188 |
* @param string $message A message explaining what has been done incorrectly. |
|
5189 |
* @param string $version The version of WordPress where the message was added. |
|
5190 |
*/ |
|
0 | 5191 |
do_action( 'doing_it_wrong_run', $function, $message, $version ); |
5192 |
||
5 | 5193 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5194 |
* Filters whether to trigger an error for _doing_it_wrong() calls. |
5 | 5195 |
* |
5196 |
* @since 3.1.0 |
|
9 | 5197 |
* @since 5.1.0 Added the $function, $message and $version parameters. |
5 | 5198 |
* |
9 | 5199 |
* @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true. |
5200 |
* @param string $function The function that was called. |
|
5201 |
* @param string $message A message explaining what has been done incorrectly. |
|
5202 |
* @param string $version The version of WordPress where the message was added. |
|
5 | 5203 |
*/ |
9 | 5204 |
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function, $message, $version ) ) { |
0 | 5205 |
if ( function_exists( '__' ) ) { |
16 | 5206 |
if ( $version ) { |
5207 |
/* translators: %s: Version number. */ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5208 |
$version = sprintf( __( '(This message was added in version %s.)' ), $version ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5209 |
} |
16 | 5210 |
|
9 | 5211 |
$message .= ' ' . sprintf( |
16 | 5212 |
/* translators: %s: Documentation URL. */ |
9 | 5213 |
__( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ), |
16 | 5214 |
__( 'https://wordpress.org/support/article/debugging-in-wordpress/' ) |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5215 |
); |
16 | 5216 |
|
5217 |
trigger_error( |
|
5218 |
sprintf( |
|
5219 |
/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: WordPress version number. */ |
|
5220 |
__( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), |
|
5221 |
$function, |
|
5222 |
$message, |
|
5223 |
$version |
|
5224 |
), |
|
5225 |
E_USER_NOTICE |
|
5226 |
); |
|
0 | 5227 |
} else { |
16 | 5228 |
if ( $version ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5229 |
$version = sprintf( '(This message was added in version %s.)', $version ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5230 |
} |
16 | 5231 |
|
9 | 5232 |
$message .= sprintf( |
5233 |
' Please see <a href="%s">Debugging in WordPress</a> for more information.', |
|
16 | 5234 |
'https://wordpress.org/support/article/debugging-in-wordpress/' |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5235 |
); |
16 | 5236 |
|
5237 |
trigger_error( |
|
5238 |
sprintf( |
|
5239 |
'%1$s was called <strong>incorrectly</strong>. %2$s %3$s', |
|
5240 |
$function, |
|
5241 |
$message, |
|
5242 |
$version |
|
5243 |
), |
|
5244 |
E_USER_NOTICE |
|
5245 |
); |
|
0 | 5246 |
} |
5247 |
} |
|
5248 |
} |
|
5249 |
||
5250 |
/** |
|
5251 |
* Is the server running earlier than 1.5.0 version of lighttpd? |
|
5252 |
* |
|
5253 |
* @since 2.5.0 |
|
5254 |
* |
|
5 | 5255 |
* @return bool Whether the server is running lighttpd < 1.5.0. |
0 | 5256 |
*/ |
5257 |
function is_lighttpd_before_150() { |
|
9 | 5258 |
$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '' ); |
5259 |
$server_parts[1] = isset( $server_parts[1] ) ? $server_parts[1] : ''; |
|
16 | 5260 |
|
5261 |
return ( 'lighttpd' === $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' ) ); |
|
0 | 5262 |
} |
5263 |
||
5264 |
/** |
|
5265 |
* Does the specified module exist in the Apache config? |
|
5266 |
* |
|
5267 |
* @since 2.5.0 |
|
5268 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5269 |
* @global bool $is_apache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5270 |
* |
5 | 5271 |
* @param string $mod The module, e.g. mod_rewrite. |
5272 |
* @param bool $default Optional. The default return value if the module is not found. Default false. |
|
5273 |
* @return bool Whether the specified module is loaded. |
|
0 | 5274 |
*/ |
9 | 5275 |
function apache_mod_loaded( $mod, $default = false ) { |
0 | 5276 |
global $is_apache; |
5277 |
||
9 | 5278 |
if ( ! $is_apache ) { |
0 | 5279 |
return false; |
9 | 5280 |
} |
0 | 5281 |
|
5 | 5282 |
if ( function_exists( 'apache_get_modules' ) ) { |
0 | 5283 |
$mods = apache_get_modules(); |
16 | 5284 |
if ( in_array( $mod, $mods, true ) ) { |
0 | 5285 |
return true; |
9 | 5286 |
} |
5 | 5287 |
} elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) { |
0 | 5288 |
ob_start(); |
9 | 5289 |
phpinfo( 8 ); |
0 | 5290 |
$phpinfo = ob_get_clean(); |
9 | 5291 |
if ( false !== strpos( $phpinfo, $mod ) ) { |
5292 |
return true; |
|
5293 |
} |
|
0 | 5294 |
} |
16 | 5295 |
|
0 | 5296 |
return $default; |
5297 |
} |
|
5298 |
||
5299 |
/** |
|
5300 |
* Check if IIS 7+ supports pretty permalinks. |
|
5301 |
* |
|
5302 |
* @since 2.8.0 |
|
5303 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5304 |
* @global bool $is_iis7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5305 |
* |
5 | 5306 |
* @return bool Whether IIS7 supports permalinks. |
0 | 5307 |
*/ |
5308 |
function iis7_supports_permalinks() { |
|
5309 |
global $is_iis7; |
|
5310 |
||
5311 |
$supports_permalinks = false; |
|
5312 |
if ( $is_iis7 ) { |
|
5313 |
/* First we check if the DOMDocument class exists. If it does not exist, then we cannot |
|
5314 |
* easily update the xml configuration file, hence we just bail out and tell user that |
|
5315 |
* pretty permalinks cannot be used. |
|
5316 |
* |
|
5317 |
* Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When |
|
5318 |
* URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'. |
|
5319 |
* Lastly we make sure that PHP is running via FastCGI. This is important because if it runs |
|
5320 |
* via ISAPI then pretty permalinks will not work. |
|
5321 |
*/ |
|
16 | 5322 |
$supports_permalinks = class_exists( 'DOMDocument', false ) && isset( $_SERVER['IIS_UrlRewriteModule'] ) && ( 'cgi-fcgi' === PHP_SAPI ); |
0 | 5323 |
} |
5324 |
||
5 | 5325 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5326 |
* Filters whether IIS 7+ supports pretty permalinks. |
5 | 5327 |
* |
5328 |
* @since 2.8.0 |
|
5329 |
* |
|
5330 |
* @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false. |
|
5331 |
*/ |
|
5332 |
return apply_filters( 'iis7_supports_permalinks', $supports_permalinks ); |
|
0 | 5333 |
} |
5334 |
||
5335 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5336 |
* Validates a file name and path against an allowed set of rules. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5337 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5338 |
* A return value of `1` means the file path contains directory traversal. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5339 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5340 |
* A return value of `2` means the file path contains a Windows drive path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5341 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5342 |
* A return value of `3` means the file is not in the allowed files list. |
0 | 5343 |
* |
5344 |
* @since 1.2.0 |
|
5345 |
* |
|
16 | 5346 |
* @param string $file File path. |
5347 |
* @param string[] $allowed_files Optional. Array of allowed files. |
|
0 | 5348 |
* @return int 0 means nothing is wrong, greater than 0 means something was wrong. |
5349 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5350 |
function validate_file( $file, $allowed_files = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5351 |
// `../` on its own is not allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5352 |
if ( '../' === $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5353 |
return 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5354 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5355 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5356 |
// More than one occurence of `../` is not allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5357 |
if ( preg_match_all( '#\.\./#', $file, $matches, PREG_SET_ORDER ) && ( count( $matches ) > 1 ) ) { |
0 | 5358 |
return 1; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5359 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5360 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5361 |
// `../` which does not occur at the end of the path is not allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5362 |
if ( false !== strpos( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) { |
0 | 5363 |
return 1; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5364 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5365 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5366 |
// Files not in the allowed file list are not allowed: |
16 | 5367 |
if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files, true ) ) { |
0 | 5368 |
return 3; |
9 | 5369 |
} |
0 | 5370 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5371 |
// Absolute Windows drive paths are not allowed: |
16 | 5372 |
if ( ':' === substr( $file, 1, 1 ) ) { |
0 | 5373 |
return 2; |
9 | 5374 |
} |
0 | 5375 |
|
5376 |
return 0; |
|
5377 |
} |
|
5378 |
||
5379 |
/** |
|
5380 |
* Whether to force SSL used for the Administration Screens. |
|
5381 |
* |
|
5382 |
* @since 2.6.0 |
|
5383 |
* |
|
5 | 5384 |
* @param string|bool $force Optional. Whether to force SSL in admin screens. Default null. |
0 | 5385 |
* @return bool True if forced, false if not forced. |
5386 |
*/ |
|
5387 |
function force_ssl_admin( $force = null ) { |
|
5388 |
static $forced = false; |
|
5389 |
||
9 | 5390 |
if ( ! is_null( $force ) ) { |
0 | 5391 |
$old_forced = $forced; |
9 | 5392 |
$forced = $force; |
0 | 5393 |
return $old_forced; |
5394 |
} |
|
5395 |
||
5396 |
return $forced; |
|
5397 |
} |
|
5398 |
||
5399 |
/** |
|
5400 |
* Guess the URL for the site. |
|
5401 |
* |
|
5402 |
* Will remove wp-admin links to retrieve only return URLs not in the wp-admin |
|
5403 |
* directory. |
|
5404 |
* |
|
5405 |
* @since 2.6.0 |
|
5406 |
* |
|
5 | 5407 |
* @return string The guessed URL. |
0 | 5408 |
*/ |
5409 |
function wp_guess_url() { |
|
16 | 5410 |
if ( defined( 'WP_SITEURL' ) && '' !== WP_SITEURL ) { |
0 | 5411 |
$url = WP_SITEURL; |
5412 |
} else { |
|
9 | 5413 |
$abspath_fix = str_replace( '\\', '/', ABSPATH ); |
0 | 5414 |
$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ); |
5415 |
||
16 | 5416 |
// The request is for the admin. |
0 | 5417 |
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) { |
5418 |
$path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] ); |
|
5419 |
||
16 | 5420 |
// The request is for a file in ABSPATH. |
5421 |
} elseif ( $script_filename_dir . '/' === $abspath_fix ) { |
|
5422 |
// Strip off any file/query params in the path. |
|
0 | 5423 |
$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] ); |
5424 |
||
5425 |
} else { |
|
5426 |
if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) { |
|
16 | 5427 |
// Request is hitting a file inside ABSPATH. |
0 | 5428 |
$directory = str_replace( ABSPATH, '', $script_filename_dir ); |
16 | 5429 |
// Strip off the subdirectory, and any file/query params. |
9 | 5430 |
$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ); |
0 | 5431 |
} elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) { |
16 | 5432 |
// Request is hitting a file above ABSPATH. |
5 | 5433 |
$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) ); |
16 | 5434 |
// Strip off any file/query params from the path, appending the subdirectory to the installation. |
9 | 5435 |
$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory; |
0 | 5436 |
} else { |
5437 |
$path = $_SERVER['REQUEST_URI']; |
|
5438 |
} |
|
5439 |
} |
|
5440 |
||
16 | 5441 |
$schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet. |
9 | 5442 |
$url = $schema . $_SERVER['HTTP_HOST'] . $path; |
5443 |
} |
|
5444 |
||
5445 |
return rtrim( $url, '/' ); |
|
0 | 5446 |
} |
5447 |
||
5448 |
/** |
|
5449 |
* Temporarily suspend cache additions. |
|
5450 |
* |
|
5451 |
* Stops more data being added to the cache, but still allows cache retrieval. |
|
5452 |
* This is useful for actions, such as imports, when a lot of data would otherwise |
|
5453 |
* be almost uselessly added to the cache. |
|
5454 |
* |
|
5455 |
* Suspension lasts for a single page load at most. Remember to call this |
|
5456 |
* function again if you wish to re-enable cache adds earlier. |
|
5457 |
* |
|
5458 |
* @since 3.3.0 |
|
5459 |
* |
|
5460 |
* @param bool $suspend Optional. Suspends additions if true, re-enables them if false. |
|
5461 |
* @return bool The current suspend setting |
|
5462 |
*/ |
|
5463 |
function wp_suspend_cache_addition( $suspend = null ) { |
|
5464 |
static $_suspend = false; |
|
5465 |
||
9 | 5466 |
if ( is_bool( $suspend ) ) { |
0 | 5467 |
$_suspend = $suspend; |
9 | 5468 |
} |
0 | 5469 |
|
5470 |
return $_suspend; |
|
5471 |
} |
|
5472 |
||
5473 |
/** |
|
5474 |
* Suspend cache invalidation. |
|
5475 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5476 |
* Turns cache invalidation on and off. Useful during imports where you don't want to do |
5 | 5477 |
* invalidations every time a post is inserted. Callers must be sure that what they are |
5478 |
* doing won't lead to an inconsistent cache when invalidation is suspended. |
|
0 | 5479 |
* |
5480 |
* @since 2.7.0 |
|
5481 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5482 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5483 |
* |
5 | 5484 |
* @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true. |
5485 |
* @return bool The current suspend setting. |
|
0 | 5486 |
*/ |
5 | 5487 |
function wp_suspend_cache_invalidation( $suspend = true ) { |
0 | 5488 |
global $_wp_suspend_cache_invalidation; |
5489 |
||
9 | 5490 |
$current_suspend = $_wp_suspend_cache_invalidation; |
0 | 5491 |
$_wp_suspend_cache_invalidation = $suspend; |
5492 |
return $current_suspend; |
|
5493 |
} |
|
5494 |
||
5495 |
/** |
|
5 | 5496 |
* Determine whether a site is the main site of the current network. |
0 | 5497 |
* |
5498 |
* @since 3.0.0 |
|
9 | 5499 |
* @since 4.9.0 The `$network_id` parameter was added. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5500 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5501 |
* @param int $site_id Optional. Site ID to test. Defaults to current site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5502 |
* @param int $network_id Optional. Network ID of the network to check for. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5503 |
* Defaults to current network. |
5 | 5504 |
* @return bool True if $site_id is the main site of the network, or if not |
5505 |
* running Multisite. |
|
0 | 5506 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5507 |
function is_main_site( $site_id = null, $network_id = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5508 |
if ( ! is_multisite() ) { |
0 | 5509 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5510 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5511 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5512 |
if ( ! $site_id ) { |
0 | 5513 |
$site_id = get_current_blog_id(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5514 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5515 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5516 |
$site_id = (int) $site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5517 |
|
16 | 5518 |
return get_main_site_id( $network_id ) === $site_id; |
0 | 5519 |
} |
5520 |
||
5521 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5522 |
* Gets the main site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5523 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5524 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5525 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5526 |
* @param int $network_id Optional. The ID of the network for which to get the main site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5527 |
* Defaults to the current network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5528 |
* @return int The ID of the main site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5529 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5530 |
function get_main_site_id( $network_id = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5531 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5532 |
return get_current_blog_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5533 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5534 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5535 |
$network = get_network( $network_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5536 |
if ( ! $network ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5537 |
return 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5538 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5539 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5540 |
return $network->site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5541 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5542 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5543 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5544 |
* Determine whether a network is the main network of the Multisite installation. |
0 | 5545 |
* |
5546 |
* @since 3.7.0 |
|
5547 |
* |
|
5548 |
* @param int $network_id Optional. Network ID to test. Defaults to current network. |
|
5 | 5549 |
* @return bool True if $network_id is the main network, or if not running Multisite. |
0 | 5550 |
*/ |
5551 |
function is_main_network( $network_id = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5552 |
if ( ! is_multisite() ) { |
0 | 5553 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5554 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5555 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5556 |
if ( null === $network_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5557 |
$network_id = get_current_network_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5558 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5559 |
|
0 | 5560 |
$network_id = (int) $network_id; |
5561 |
||
16 | 5562 |
return ( get_main_network_id() === $network_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5563 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5564 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5565 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5566 |
* Get the main network ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5567 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5568 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5569 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5570 |
* @return int The ID of the main network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5571 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5572 |
function get_main_network_id() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5573 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5574 |
return 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5575 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5576 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5577 |
$current_network = get_network(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5578 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5579 |
if ( defined( 'PRIMARY_NETWORK_ID' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5580 |
$main_network_id = PRIMARY_NETWORK_ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5581 |
} elseif ( isset( $current_network->id ) && 1 === (int) $current_network->id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5582 |
// If the current network has an ID of 1, assume it is the main network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5583 |
$main_network_id = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5584 |
} else { |
9 | 5585 |
$_networks = get_networks( |
5586 |
array( |
|
5587 |
'fields' => 'ids', |
|
5588 |
'number' => 1, |
|
5589 |
) |
|
5590 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5591 |
$main_network_id = array_shift( $_networks ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5592 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5593 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5594 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5595 |
* Filters the main network ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5596 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5597 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5598 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5599 |
* @param int $main_network_id The ID of the main network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5600 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5601 |
return (int) apply_filters( 'get_main_network_id', $main_network_id ); |
0 | 5602 |
} |
5603 |
||
5604 |
/** |
|
5 | 5605 |
* Determine whether global terms are enabled. |
0 | 5606 |
* |
5607 |
* @since 3.0.0 |
|
5 | 5608 |
* |
5609 |
* @return bool True if multisite and global terms enabled. |
|
0 | 5610 |
*/ |
5611 |
function global_terms_enabled() { |
|
9 | 5612 |
if ( ! is_multisite() ) { |
0 | 5613 |
return false; |
9 | 5614 |
} |
0 | 5615 |
|
5616 |
static $global_terms = null; |
|
5617 |
if ( is_null( $global_terms ) ) { |
|
5 | 5618 |
|
5619 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5620 |
* Filters whether global terms are enabled. |
5 | 5621 |
* |
16 | 5622 |
* Returning a non-null value from the filter will effectively short-circuit the function |
5623 |
* and return the value of the 'global_terms_enabled' site option instead. |
|
5 | 5624 |
* |
5625 |
* @since 3.0.0 |
|
5626 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5627 |
* @param null $enabled Whether global terms are enabled. |
5 | 5628 |
*/ |
0 | 5629 |
$filter = apply_filters( 'global_terms_enabled', null ); |
9 | 5630 |
if ( ! is_null( $filter ) ) { |
0 | 5631 |
$global_terms = (bool) $filter; |
9 | 5632 |
} else { |
0 | 5633 |
$global_terms = (bool) get_site_option( 'global_terms_enabled', false ); |
9 | 5634 |
} |
0 | 5635 |
} |
5636 |
return $global_terms; |
|
5637 |
} |
|
5638 |
||
5639 |
/** |
|
9 | 5640 |
* Determines whether site meta is enabled. |
5641 |
* |
|
5642 |
* This function checks whether the 'blogmeta' database table exists. The result is saved as |
|
5643 |
* a setting for the main network, making it essentially a global setting. Subsequent requests |
|
5644 |
* will refer to this setting instead of running the query. |
|
5645 |
* |
|
5646 |
* @since 5.1.0 |
|
5647 |
* |
|
5648 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5649 |
* |
|
5650 |
* @return bool True if site meta is supported, false otherwise. |
|
5651 |
*/ |
|
5652 |
function is_site_meta_supported() { |
|
5653 |
global $wpdb; |
|
5654 |
||
5655 |
if ( ! is_multisite() ) { |
|
5656 |
return false; |
|
5657 |
} |
|
5658 |
||
5659 |
$network_id = get_main_network_id(); |
|
5660 |
||
5661 |
$supported = get_network_option( $network_id, 'site_meta_supported', false ); |
|
5662 |
if ( false === $supported ) { |
|
5663 |
$supported = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->blogmeta}'" ) ? 1 : 0; |
|
5664 |
||
5665 |
update_network_option( $network_id, 'site_meta_supported', $supported ); |
|
5666 |
} |
|
5667 |
||
5668 |
return (bool) $supported; |
|
5669 |
} |
|
5670 |
||
5671 |
/** |
|
0 | 5672 |
* gmt_offset modification for smart timezone handling. |
5673 |
* |
|
5674 |
* Overrides the gmt_offset option if we have a timezone_string available. |
|
5675 |
* |
|
5676 |
* @since 2.8.0 |
|
5677 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5678 |
* @return float|false Timezone GMT offset, false otherwise. |
0 | 5679 |
*/ |
5680 |
function wp_timezone_override_offset() { |
|
16 | 5681 |
$timezone_string = get_option( 'timezone_string' ); |
5682 |
if ( ! $timezone_string ) { |
|
0 | 5683 |
return false; |
5684 |
} |
|
5685 |
||
5686 |
$timezone_object = timezone_open( $timezone_string ); |
|
5687 |
$datetime_object = date_create(); |
|
5688 |
if ( false === $timezone_object || false === $datetime_object ) { |
|
5689 |
return false; |
|
5690 |
} |
|
5691 |
return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 ); |
|
5692 |
} |
|
5693 |
||
5694 |
/** |
|
5695 |
* Sort-helper for timezones. |
|
5696 |
* |
|
5697 |
* @since 2.9.0 |
|
5 | 5698 |
* @access private |
0 | 5699 |
* |
5700 |
* @param array $a |
|
5701 |
* @param array $b |
|
5702 |
* @return int |
|
5703 |
*/ |
|
5704 |
function _wp_timezone_choice_usort_callback( $a, $b ) { |
|
16 | 5705 |
// Don't use translated versions of Etc. |
0 | 5706 |
if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) { |
16 | 5707 |
// Make the order of these more like the old dropdown. |
0 | 5708 |
if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) { |
5709 |
return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) ); |
|
5710 |
} |
|
5711 |
if ( 'UTC' === $a['city'] ) { |
|
5712 |
if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) { |
|
5713 |
return 1; |
|
5714 |
} |
|
5715 |
return -1; |
|
5716 |
} |
|
5717 |
if ( 'UTC' === $b['city'] ) { |
|
5718 |
if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) { |
|
5719 |
return -1; |
|
5720 |
} |
|
5721 |
return 1; |
|
5722 |
} |
|
5723 |
return strnatcasecmp( $a['city'], $b['city'] ); |
|
5724 |
} |
|
5725 |
if ( $a['t_continent'] == $b['t_continent'] ) { |
|
5726 |
if ( $a['t_city'] == $b['t_city'] ) { |
|
5727 |
return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] ); |
|
5728 |
} |
|
5729 |
return strnatcasecmp( $a['t_city'], $b['t_city'] ); |
|
5730 |
} else { |
|
16 | 5731 |
// Force Etc to the bottom of the list. |
0 | 5732 |
if ( 'Etc' === $a['continent'] ) { |
5733 |
return 1; |
|
5734 |
} |
|
5735 |
if ( 'Etc' === $b['continent'] ) { |
|
5736 |
return -1; |
|
5737 |
} |
|
5738 |
return strnatcasecmp( $a['t_continent'], $b['t_continent'] ); |
|
5739 |
} |
|
5740 |
} |
|
5741 |
||
5742 |
/** |
|
5 | 5743 |
* Gives a nicely-formatted list of timezone strings. |
0 | 5744 |
* |
5745 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5746 |
* @since 4.7.0 Added the `$locale` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5747 |
* |
5 | 5748 |
* @param string $selected_zone Selected timezone. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5749 |
* @param string $locale Optional. Locale to load the timezones in. Default current site locale. |
0 | 5750 |
* @return string |
5751 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5752 |
function wp_timezone_choice( $selected_zone, $locale = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5753 |
static $mo_loaded = false, $locale_loaded = null; |
0 | 5754 |
|
9 | 5755 |
$continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' ); |
0 | 5756 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5757 |
// Load translations for continents and cities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5758 |
if ( ! $mo_loaded || $locale !== $locale_loaded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5759 |
$locale_loaded = $locale ? $locale : get_locale(); |
9 | 5760 |
$mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5761 |
unload_textdomain( 'continents-cities' ); |
0 | 5762 |
load_textdomain( 'continents-cities', $mofile ); |
5763 |
$mo_loaded = true; |
|
5764 |
} |
|
5765 |
||
5766 |
$zonen = array(); |
|
5767 |
foreach ( timezone_identifiers_list() as $zone ) { |
|
5768 |
$zone = explode( '/', $zone ); |
|
16 | 5769 |
if ( ! in_array( $zone[0], $continents, true ) ) { |
0 | 5770 |
continue; |
5771 |
} |
|
5772 |
||
16 | 5773 |
// This determines what gets set and translated - we don't translate Etc/* strings here, they are done later. |
9 | 5774 |
$exists = array( |
0 | 5775 |
0 => ( isset( $zone[0] ) && $zone[0] ), |
5776 |
1 => ( isset( $zone[1] ) && $zone[1] ), |
|
5777 |
2 => ( isset( $zone[2] ) && $zone[2] ), |
|
5778 |
); |
|
5779 |
$exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ); |
|
5780 |
$exists[4] = ( $exists[1] && $exists[3] ); |
|
5781 |
$exists[5] = ( $exists[2] && $exists[3] ); |
|
5782 |
||
9 | 5783 |
// phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
0 | 5784 |
$zonen[] = array( |
5785 |
'continent' => ( $exists[0] ? $zone[0] : '' ), |
|
5786 |
'city' => ( $exists[1] ? $zone[1] : '' ), |
|
5787 |
'subcity' => ( $exists[2] ? $zone[2] : '' ), |
|
5788 |
't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ), |
|
5789 |
't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ), |
|
9 | 5790 |
't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ), |
0 | 5791 |
); |
9 | 5792 |
// phpcs:enable |
0 | 5793 |
} |
5794 |
usort( $zonen, '_wp_timezone_choice_usort_callback' ); |
|
5795 |
||
5796 |
$structure = array(); |
|
5797 |
||
5798 |
if ( empty( $selected_zone ) ) { |
|
5799 |
$structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>'; |
|
5800 |
} |
|
5801 |
||
5802 |
foreach ( $zonen as $key => $zone ) { |
|
16 | 5803 |
// Build value in an array to join later. |
0 | 5804 |
$value = array( $zone['continent'] ); |
5805 |
||
5806 |
if ( empty( $zone['city'] ) ) { |
|
16 | 5807 |
// It's at the continent level (generally won't happen). |
0 | 5808 |
$display = $zone['t_continent']; |
5809 |
} else { |
|
16 | 5810 |
// It's inside a continent group. |
5811 |
||
5812 |
// Continent optgroup. |
|
9 | 5813 |
if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) { |
5814 |
$label = $zone['t_continent']; |
|
5815 |
$structure[] = '<optgroup label="' . esc_attr( $label ) . '">'; |
|
0 | 5816 |
} |
5817 |
||
16 | 5818 |
// Add the city to the value. |
0 | 5819 |
$value[] = $zone['city']; |
5820 |
||
5821 |
$display = $zone['t_city']; |
|
9 | 5822 |
if ( ! empty( $zone['subcity'] ) ) { |
16 | 5823 |
// Add the subcity to the value. |
9 | 5824 |
$value[] = $zone['subcity']; |
0 | 5825 |
$display .= ' - ' . $zone['t_subcity']; |
5826 |
} |
|
5827 |
} |
|
5828 |
||
16 | 5829 |
// Build the value. |
9 | 5830 |
$value = join( '/', $value ); |
0 | 5831 |
$selected = ''; |
5832 |
if ( $value === $selected_zone ) { |
|
5833 |
$selected = 'selected="selected" '; |
|
5834 |
} |
|
9 | 5835 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . '</option>'; |
0 | 5836 |
|
16 | 5837 |
// Close continent optgroup. |
9 | 5838 |
if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) { |
0 | 5839 |
$structure[] = '</optgroup>'; |
5840 |
} |
|
5841 |
} |
|
5842 |
||
16 | 5843 |
// Do UTC. |
9 | 5844 |
$structure[] = '<optgroup label="' . esc_attr__( 'UTC' ) . '">'; |
5845 |
$selected = ''; |
|
5846 |
if ( 'UTC' === $selected_zone ) { |
|
0 | 5847 |
$selected = 'selected="selected" '; |
9 | 5848 |
} |
5849 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC' ) . '</option>'; |
|
0 | 5850 |
$structure[] = '</optgroup>'; |
5851 |
||
16 | 5852 |
// Do manual UTC offsets. |
9 | 5853 |
$structure[] = '<optgroup label="' . esc_attr__( 'Manual Offsets' ) . '">'; |
5854 |
$offset_range = array( |
|
5855 |
-12, |
|
5856 |
-11.5, |
|
5857 |
-11, |
|
5858 |
-10.5, |
|
5859 |
-10, |
|
5860 |
-9.5, |
|
5861 |
-9, |
|
5862 |
-8.5, |
|
5863 |
-8, |
|
5864 |
-7.5, |
|
5865 |
-7, |
|
5866 |
-6.5, |
|
5867 |
-6, |
|
5868 |
-5.5, |
|
5869 |
-5, |
|
5870 |
-4.5, |
|
5871 |
-4, |
|
5872 |
-3.5, |
|
5873 |
-3, |
|
5874 |
-2.5, |
|
5875 |
-2, |
|
5876 |
-1.5, |
|
5877 |
-1, |
|
5878 |
-0.5, |
|
5879 |
0, |
|
5880 |
0.5, |
|
5881 |
1, |
|
5882 |
1.5, |
|
5883 |
2, |
|
5884 |
2.5, |
|
5885 |
3, |
|
5886 |
3.5, |
|
5887 |
4, |
|
5888 |
4.5, |
|
5889 |
5, |
|
5890 |
5.5, |
|
5891 |
5.75, |
|
5892 |
6, |
|
5893 |
6.5, |
|
5894 |
7, |
|
5895 |
7.5, |
|
5896 |
8, |
|
5897 |
8.5, |
|
5898 |
8.75, |
|
5899 |
9, |
|
5900 |
9.5, |
|
5901 |
10, |
|
5902 |
10.5, |
|
5903 |
11, |
|
5904 |
11.5, |
|
5905 |
12, |
|
5906 |
12.75, |
|
5907 |
13, |
|
5908 |
13.75, |
|
5909 |
14, |
|
5910 |
); |
|
0 | 5911 |
foreach ( $offset_range as $offset ) { |
9 | 5912 |
if ( 0 <= $offset ) { |
0 | 5913 |
$offset_name = '+' . $offset; |
9 | 5914 |
} else { |
0 | 5915 |
$offset_name = (string) $offset; |
9 | 5916 |
} |
0 | 5917 |
|
5918 |
$offset_value = $offset_name; |
|
9 | 5919 |
$offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_name ); |
5920 |
$offset_name = 'UTC' . $offset_name; |
|
0 | 5921 |
$offset_value = 'UTC' . $offset_value; |
9 | 5922 |
$selected = ''; |
5923 |
if ( $offset_value === $selected_zone ) { |
|
0 | 5924 |
$selected = 'selected="selected" '; |
9 | 5925 |
} |
5926 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . '</option>'; |
|
0 | 5927 |
|
5928 |
} |
|
5929 |
$structure[] = '</optgroup>'; |
|
5930 |
||
5931 |
return join( "\n", $structure ); |
|
5932 |
} |
|
5933 |
||
5934 |
/** |
|
5935 |
* Strip close comment and close php tags from file headers used by WP. |
|
5936 |
* |
|
5937 |
* @since 2.8.0 |
|
5 | 5938 |
* @access private |
5939 |
* |
|
5940 |
* @see https://core.trac.wordpress.org/ticket/8497 |
|
5941 |
* |
|
5942 |
* @param string $str Header comment to clean up. |
|
0 | 5943 |
* @return string |
5944 |
*/ |
|
5 | 5945 |
function _cleanup_header_comment( $str ) { |
9 | 5946 |
return trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $str ) ); |
0 | 5947 |
} |
5948 |
||
5949 |
/** |
|
5 | 5950 |
* Permanently delete comments or posts of any type that have held a status |
5951 |
* of 'trash' for the number of days defined in EMPTY_TRASH_DAYS. |
|
5952 |
* |
|
5953 |
* The default value of `EMPTY_TRASH_DAYS` is 30 (days). |
|
0 | 5954 |
* |
5955 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5956 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5957 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 5958 |
*/ |
5959 |
function wp_scheduled_delete() { |
|
5960 |
global $wpdb; |
|
5961 |
||
5962 |
$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
|
5963 |
||
9 | 5964 |
$posts_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A ); |
0 | 5965 |
|
5966 |
foreach ( (array) $posts_to_delete as $post ) { |
|
5967 |
$post_id = (int) $post['post_id']; |
|
9 | 5968 |
if ( ! $post_id ) { |
0 | 5969 |
continue; |
9 | 5970 |
} |
5971 |
||
5972 |
$del_post = get_post( $post_id ); |
|
5973 |
||
16 | 5974 |
if ( ! $del_post || 'trash' !== $del_post->post_status ) { |
9 | 5975 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
5976 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|
0 | 5977 |
} else { |
9 | 5978 |
wp_delete_post( $post_id ); |
0 | 5979 |
} |
5980 |
} |
|
5981 |
||
9 | 5982 |
$comments_to_delete = $wpdb->get_results( $wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = '_wp_trash_meta_time' AND meta_value < %d", $delete_timestamp ), ARRAY_A ); |
0 | 5983 |
|
5984 |
foreach ( (array) $comments_to_delete as $comment ) { |
|
5985 |
$comment_id = (int) $comment['comment_id']; |
|
9 | 5986 |
if ( ! $comment_id ) { |
0 | 5987 |
continue; |
9 | 5988 |
} |
5989 |
||
5990 |
$del_comment = get_comment( $comment_id ); |
|
5991 |
||
16 | 5992 |
if ( ! $del_comment || 'trash' !== $del_comment->comment_approved ) { |
9 | 5993 |
delete_comment_meta( $comment_id, '_wp_trash_meta_time' ); |
5994 |
delete_comment_meta( $comment_id, '_wp_trash_meta_status' ); |
|
0 | 5995 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5996 |
wp_delete_comment( $del_comment ); |
0 | 5997 |
} |
5998 |
} |
|
5999 |
} |
|
6000 |
||
6001 |
/** |
|
6002 |
* Retrieve metadata from a file. |
|
6003 |
* |
|
16 | 6004 |
* Searches for metadata in the first 8 KB of a file, such as a plugin or theme. |
0 | 6005 |
* Each piece of metadata must be on its own line. Fields can not span multiple |
6006 |
* lines, the value will get cut at the end of the first line. |
|
6007 |
* |
|
16 | 6008 |
* If the file data is not within that first 8 KB, then the author should correct |
0 | 6009 |
* their plugin file and move the data headers to the top. |
6010 |
* |
|
5 | 6011 |
* @link https://codex.wordpress.org/File_Header |
0 | 6012 |
* |
6013 |
* @since 2.9.0 |
|
5 | 6014 |
* |
9 | 6015 |
* @param string $file Absolute path to the file. |
16 | 6016 |
* @param array $default_headers List of headers, in the format `array( 'HeaderKey' => 'Header Name' )`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6017 |
* @param string $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}. |
5 | 6018 |
* Default empty. |
16 | 6019 |
* @return string[] Array of file header values keyed by header name. |
0 | 6020 |
*/ |
6021 |
function get_file_data( $file, $default_headers, $context = '' ) { |
|
6022 |
// We don't need to write to the file, so just open for reading. |
|
6023 |
$fp = fopen( $file, 'r' ); |
|
6024 |
||
16 | 6025 |
// Pull only the first 8 KB of the file in. |
6026 |
$file_data = fread( $fp, 8 * KB_IN_BYTES ); |
|
0 | 6027 |
|
6028 |
// PHP will close file handle, but we are good citizens. |
|
6029 |
fclose( $fp ); |
|
6030 |
||
6031 |
// Make sure we catch CR-only line endings. |
|
6032 |
$file_data = str_replace( "\r", "\n", $file_data ); |
|
6033 |
||
5 | 6034 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6035 |
* Filters extra file headers by context. |
5 | 6036 |
* |
6037 |
* The dynamic portion of the hook name, `$context`, refers to |
|
6038 |
* the context where extra headers might be loaded. |
|
6039 |
* |
|
6040 |
* @since 2.9.0 |
|
6041 |
* |
|
6042 |
* @param array $extra_context_headers Empty array by default. |
|
6043 |
*/ |
|
16 | 6044 |
$extra_headers = $context ? apply_filters( "extra_{$context}_headers", array() ) : array(); |
6045 |
if ( $extra_headers ) { |
|
6046 |
$extra_headers = array_combine( $extra_headers, $extra_headers ); // Keys equal values. |
|
9 | 6047 |
$all_headers = array_merge( $extra_headers, (array) $default_headers ); |
0 | 6048 |
} else { |
6049 |
$all_headers = $default_headers; |
|
6050 |
} |
|
6051 |
||
6052 |
foreach ( $all_headers as $field => $regex ) { |
|
9 | 6053 |
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { |
0 | 6054 |
$all_headers[ $field ] = _cleanup_header_comment( $match[1] ); |
9 | 6055 |
} else { |
0 | 6056 |
$all_headers[ $field ] = ''; |
9 | 6057 |
} |
0 | 6058 |
} |
6059 |
||
6060 |
return $all_headers; |
|
6061 |
} |
|
6062 |
||
6063 |
/** |
|
6064 |
* Returns true. |
|
6065 |
* |
|
6066 |
* Useful for returning true to filters easily. |
|
6067 |
* |
|
6068 |
* @since 3.0.0 |
|
5 | 6069 |
* |
0 | 6070 |
* @see __return_false() |
5 | 6071 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6072 |
* @return true True. |
0 | 6073 |
*/ |
16 | 6074 |
function __return_true() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6075 |
return true; |
6076 |
} |
|
6077 |
||
6078 |
/** |
|
6079 |
* Returns false. |
|
6080 |
* |
|
6081 |
* Useful for returning false to filters easily. |
|
6082 |
* |
|
6083 |
* @since 3.0.0 |
|
5 | 6084 |
* |
0 | 6085 |
* @see __return_true() |
5 | 6086 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6087 |
* @return false False. |
0 | 6088 |
*/ |
16 | 6089 |
function __return_false() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6090 |
return false; |
6091 |
} |
|
6092 |
||
6093 |
/** |
|
6094 |
* Returns 0. |
|
6095 |
* |
|
6096 |
* Useful for returning 0 to filters easily. |
|
6097 |
* |
|
6098 |
* @since 3.0.0 |
|
5 | 6099 |
* |
6100 |
* @return int 0. |
|
0 | 6101 |
*/ |
16 | 6102 |
function __return_zero() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6103 |
return 0; |
6104 |
} |
|
6105 |
||
6106 |
/** |
|
6107 |
* Returns an empty array. |
|
6108 |
* |
|
6109 |
* Useful for returning an empty array to filters easily. |
|
6110 |
* |
|
6111 |
* @since 3.0.0 |
|
5 | 6112 |
* |
6113 |
* @return array Empty array. |
|
0 | 6114 |
*/ |
16 | 6115 |
function __return_empty_array() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6116 |
return array(); |
6117 |
} |
|
6118 |
||
6119 |
/** |
|
6120 |
* Returns null. |
|
6121 |
* |
|
6122 |
* Useful for returning null to filters easily. |
|
6123 |
* |
|
6124 |
* @since 3.4.0 |
|
5 | 6125 |
* |
6126 |
* @return null Null value. |
|
0 | 6127 |
*/ |
16 | 6128 |
function __return_null() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6129 |
return null; |
6130 |
} |
|
6131 |
||
6132 |
/** |
|
6133 |
* Returns an empty string. |
|
6134 |
* |
|
6135 |
* Useful for returning an empty string to filters easily. |
|
6136 |
* |
|
6137 |
* @since 3.7.0 |
|
5 | 6138 |
* |
0 | 6139 |
* @see __return_null() |
5 | 6140 |
* |
6141 |
* @return string Empty string. |
|
0 | 6142 |
*/ |
16 | 6143 |
function __return_empty_string() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
0 | 6144 |
return ''; |
6145 |
} |
|
6146 |
||
6147 |
/** |
|
6148 |
* Send a HTTP header to disable content type sniffing in browsers which support it. |
|
6149 |
* |
|
6150 |
* @since 3.0.0 |
|
5 | 6151 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6152 |
* @see https://blogs.msdn.com/ie/archive/2008/07/02/ie8-security-part-v-comprehensive-protection.aspx |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6153 |
* @see https://src.chromium.org/viewvc/chrome?view=rev&revision=6985 |
0 | 6154 |
*/ |
6155 |
function send_nosniff_header() { |
|
16 | 6156 |
header( 'X-Content-Type-Options: nosniff' ); |
0 | 6157 |
} |
6158 |
||
6159 |
/** |
|
5 | 6160 |
* Return a MySQL expression for selecting the week number based on the start_of_week option. |
6161 |
* |
|
6162 |
* @ignore |
|
0 | 6163 |
* @since 3.0.0 |
5 | 6164 |
* |
6165 |
* @param string $column Database column. |
|
6166 |
* @return string SQL clause. |
|
0 | 6167 |
*/ |
6168 |
function _wp_mysql_week( $column ) { |
|
16 | 6169 |
$start_of_week = (int) get_option( 'start_of_week' ); |
6170 |
switch ( $start_of_week ) { |
|
9 | 6171 |
case 1: |
6172 |
return "WEEK( $column, 1 )"; |
|
6173 |
case 2: |
|
6174 |
case 3: |
|
6175 |
case 4: |
|
6176 |
case 5: |
|
6177 |
case 6: |
|
6178 |
return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )"; |
|
6179 |
case 0: |
|
6180 |
default: |
|
6181 |
return "WEEK( $column, 0 )"; |
|
0 | 6182 |
} |
6183 |
} |
|
6184 |
||
6185 |
/** |
|
5 | 6186 |
* Find hierarchy loops using a callback function that maps object IDs to parent IDs. |
0 | 6187 |
* |
6188 |
* @since 3.1.0 |
|
6189 |
* @access private |
|
6190 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6191 |
* @param callable $callback Function that accepts ( ID, $callback_args ) and outputs parent_ID. |
5 | 6192 |
* @param int $start The ID to start the loop check at. |
6193 |
* @param int $start_parent The parent_ID of $start to use instead of calling $callback( $start ). |
|
6194 |
* Use null to always use $callback |
|
6195 |
* @param array $callback_args Optional. Additional arguments to send to $callback. |
|
6196 |
* @return array IDs of all members of loop. |
|
0 | 6197 |
*/ |
6198 |
function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) { |
|
6199 |
$override = is_null( $start_parent ) ? array() : array( $start => $start_parent ); |
|
6200 |
||
16 | 6201 |
$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ); |
6202 |
if ( ! $arbitrary_loop_member ) { |
|
0 | 6203 |
return array(); |
9 | 6204 |
} |
0 | 6205 |
|
6206 |
return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true ); |
|
6207 |
} |
|
6208 |
||
6209 |
/** |
|
5 | 6210 |
* Use the "The Tortoise and the Hare" algorithm to detect loops. |
0 | 6211 |
* |
6212 |
* For every step of the algorithm, the hare takes two steps and the tortoise one. |
|
6213 |
* If the hare ever laps the tortoise, there must be a loop. |
|
6214 |
* |
|
6215 |
* @since 3.1.0 |
|
6216 |
* @access private |
|
6217 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6218 |
* @param callable $callback Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID. |
5 | 6219 |
* @param int $start The ID to start the loop check at. |
6220 |
* @param array $override Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback. |
|
6221 |
* Default empty array. |
|
6222 |
* @param array $callback_args Optional. Additional arguments to send to $callback. Default empty array. |
|
6223 |
* @param bool $_return_loop Optional. Return loop members or just detect presence of loop? Only set |
|
6224 |
* to true if you already know the given $start is part of a loop (otherwise |
|
6225 |
* the returned array might include branches). Default false. |
|
6226 |
* @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if |
|
6227 |
* $_return_loop |
|
0 | 6228 |
*/ |
6229 |
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) { |
|
16 | 6230 |
$tortoise = $start; |
6231 |
$hare = $start; |
|
6232 |
$evanescent_hare = $start; |
|
6233 |
$return = array(); |
|
6234 |
||
6235 |
// Set evanescent_hare to one past hare. |
|
6236 |
// Increment hare two steps. |
|
0 | 6237 |
while ( |
6238 |
$tortoise |
|
6239 |
&& |
|
9 | 6240 |
( $evanescent_hare = isset( $override[ $hare ] ) ? $override[ $hare ] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) ) |
0 | 6241 |
&& |
9 | 6242 |
( $hare = isset( $override[ $evanescent_hare ] ) ? $override[ $evanescent_hare ] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) ) |
0 | 6243 |
) { |
9 | 6244 |
if ( $_return_loop ) { |
16 | 6245 |
$return[ $tortoise ] = true; |
6246 |
$return[ $evanescent_hare ] = true; |
|
6247 |
$return[ $hare ] = true; |
|
9 | 6248 |
} |
0 | 6249 |
|
16 | 6250 |
// Tortoise got lapped - must be a loop. |
9 | 6251 |
if ( $tortoise == $evanescent_hare || $tortoise == $hare ) { |
0 | 6252 |
return $_return_loop ? $return : $tortoise; |
9 | 6253 |
} |
0 | 6254 |
|
16 | 6255 |
// Increment tortoise by one step. |
9 | 6256 |
$tortoise = isset( $override[ $tortoise ] ) ? $override[ $tortoise ] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) ); |
0 | 6257 |
} |
6258 |
||
6259 |
return false; |
|
6260 |
} |
|
6261 |
||
6262 |
/** |
|
6263 |
* Send a HTTP header to limit rendering of pages to same origin iframes. |
|
6264 |
* |
|
6265 |
* @since 3.1.3 |
|
5 | 6266 |
* |
6267 |
* @see https://developer.mozilla.org/en/the_x-frame-options_response_header |
|
0 | 6268 |
*/ |
6269 |
function send_frame_options_header() { |
|
16 | 6270 |
header( 'X-Frame-Options: SAMEORIGIN' ); |
0 | 6271 |
} |
6272 |
||
6273 |
/** |
|
6274 |
* Retrieve a list of protocols to allow in HTML attributes. |
|
6275 |
* |
|
6276 |
* @since 3.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6277 |
* @since 4.3.0 Added 'webcal' to the protocols array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6278 |
* @since 4.7.0 Added 'urn' to the protocols array. |
16 | 6279 |
* @since 5.3.0 Added 'sms' to the protocols array. |
5 | 6280 |
* |
0 | 6281 |
* @see wp_kses() |
6282 |
* @see esc_url() |
|
6283 |
* |
|
9 | 6284 |
* @return string[] Array of allowed protocols. Defaults to an array containing 'http', 'https', |
6285 |
* 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', |
|
16 | 6286 |
* 'mms', 'rtsp', 'sms', 'svn', 'tel', 'fax', 'xmpp', 'webcal', and 'urn'. |
6287 |
* This covers all common link protocols, except for 'javascript' which should not |
|
6288 |
* be allowed for untrusted users. |
|
0 | 6289 |
*/ |
6290 |
function wp_allowed_protocols() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6291 |
static $protocols = array(); |
0 | 6292 |
|
6293 |
if ( empty( $protocols ) ) { |
|
16 | 6294 |
$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'sms', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6295 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6296 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6297 |
if ( ! did_action( 'wp_loaded' ) ) { |
5 | 6298 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6299 |
* Filters the list of protocols allowed in HTML attributes. |
5 | 6300 |
* |
6301 |
* @since 3.0.0 |
|
6302 |
* |
|
16 | 6303 |
* @param string[] $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more. |
5 | 6304 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6305 |
$protocols = array_unique( (array) apply_filters( 'kses_allowed_protocols', $protocols ) ); |
0 | 6306 |
} |
6307 |
||
6308 |
return $protocols; |
|
6309 |
} |
|
6310 |
||
6311 |
/** |
|
5 | 6312 |
* Return a comma-separated string of functions that have been called to get |
6313 |
* to the current point in code. |
|
6314 |
* |
|
6315 |
* @since 3.4.0 |
|
6316 |
* |
|
6317 |
* @see https://core.trac.wordpress.org/ticket/19589 |
|
6318 |
* |
|
6319 |
* @param string $ignore_class Optional. A class to ignore all function calls within - useful |
|
6320 |
* when you want to just give info about the callee. Default null. |
|
6321 |
* @param int $skip_frames Optional. A number of stack frames to skip - useful for unwinding |
|
6322 |
* back to the source of the issue. Default 0. |
|
6323 |
* @param bool $pretty Optional. Whether or not you want a comma separated string or raw |
|
6324 |
* array returned. Default true. |
|
6325 |
* @return string|array Either a string containing a reversed comma separated trace or an array |
|
6326 |
* of individual calls. |
|
0 | 6327 |
*/ |
6328 |
function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) { |
|
9 | 6329 |
static $truncate_paths; |
6330 |
||
16 | 6331 |
$trace = debug_backtrace( false ); |
9 | 6332 |
$caller = array(); |
0 | 6333 |
$check_class = ! is_null( $ignore_class ); |
16 | 6334 |
$skip_frames++; // Skip this function. |
0 | 6335 |
|
9 | 6336 |
if ( ! isset( $truncate_paths ) ) { |
6337 |
$truncate_paths = array( |
|
6338 |
wp_normalize_path( WP_CONTENT_DIR ), |
|
6339 |
wp_normalize_path( ABSPATH ), |
|
6340 |
); |
|
6341 |
} |
|
6342 |
||
0 | 6343 |
foreach ( $trace as $call ) { |
6344 |
if ( $skip_frames > 0 ) { |
|
6345 |
$skip_frames--; |
|
6346 |
} elseif ( isset( $call['class'] ) ) { |
|
9 | 6347 |
if ( $check_class && $ignore_class == $call['class'] ) { |
16 | 6348 |
continue; // Filter out calls. |
9 | 6349 |
} |
0 | 6350 |
|
6351 |
$caller[] = "{$call['class']}{$call['type']}{$call['function']}"; |
|
6352 |
} else { |
|
16 | 6353 |
if ( in_array( $call['function'], array( 'do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array' ), true ) ) { |
0 | 6354 |
$caller[] = "{$call['function']}('{$call['args'][0]}')"; |
16 | 6355 |
} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ), true ) ) { |
9 | 6356 |
$filename = isset( $call['args'][0] ) ? $call['args'][0] : ''; |
6357 |
$caller[] = $call['function'] . "('" . str_replace( $truncate_paths, '', wp_normalize_path( $filename ) ) . "')"; |
|
0 | 6358 |
} else { |
6359 |
$caller[] = $call['function']; |
|
6360 |
} |
|
6361 |
} |
|
6362 |
} |
|
9 | 6363 |
if ( $pretty ) { |
0 | 6364 |
return join( ', ', array_reverse( $caller ) ); |
9 | 6365 |
} else { |
0 | 6366 |
return $caller; |
9 | 6367 |
} |
6368 |
} |
|
6369 |
||
6370 |
/** |
|
6371 |
* Retrieve IDs that are not already present in the cache. |
|
0 | 6372 |
* |
6373 |
* @since 3.4.0 |
|
5 | 6374 |
* @access private |
6375 |
* |
|
9 | 6376 |
* @param int[] $object_ids Array of IDs. |
5 | 6377 |
* @param string $cache_key The cache bucket to check against. |
9 | 6378 |
* @return int[] Array of IDs not present in the cache. |
0 | 6379 |
*/ |
6380 |
function _get_non_cached_ids( $object_ids, $cache_key ) { |
|
16 | 6381 |
$non_cached_ids = array(); |
6382 |
$cache_values = wp_cache_get_multiple( $object_ids, $cache_key ); |
|
6383 |
||
6384 |
foreach ( $cache_values as $id => $value ) { |
|
6385 |
if ( ! $value ) { |
|
6386 |
$non_cached_ids[] = (int) $id; |
|
0 | 6387 |
} |
6388 |
} |
|
6389 |
||
16 | 6390 |
return $non_cached_ids; |
0 | 6391 |
} |
6392 |
||
6393 |
/** |
|
6394 |
* Test if the current device has the capability to upload files. |
|
6395 |
* |
|
6396 |
* @since 3.4.0 |
|
6397 |
* @access private |
|
6398 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6399 |
* @return bool Whether the device is able to upload files. |
0 | 6400 |
*/ |
6401 |
function _device_can_upload() { |
|
9 | 6402 |
if ( ! wp_is_mobile() ) { |
0 | 6403 |
return true; |
9 | 6404 |
} |
0 | 6405 |
|
6406 |
$ua = $_SERVER['HTTP_USER_AGENT']; |
|
6407 |
||
9 | 6408 |
if ( strpos( $ua, 'iPhone' ) !== false |
6409 |
|| strpos( $ua, 'iPad' ) !== false |
|
6410 |
|| strpos( $ua, 'iPod' ) !== false ) { |
|
0 | 6411 |
return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' ); |
6412 |
} |
|
6413 |
||
6414 |
return true; |
|
6415 |
} |
|
6416 |
||
6417 |
/** |
|
6418 |
* Test if a given path is a stream URL |
|
6419 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6420 |
* @since 3.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6421 |
* |
5 | 6422 |
* @param string $path The resource path or URL. |
6423 |
* @return bool True if the path is a stream URL. |
|
0 | 6424 |
*/ |
6425 |
function wp_is_stream( $path ) { |
|
9 | 6426 |
$scheme_separator = strpos( $path, '://' ); |
6427 |
||
6428 |
if ( false === $scheme_separator ) { |
|
16 | 6429 |
// $path isn't a stream. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6430 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6431 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6432 |
|
9 | 6433 |
$stream = substr( $path, 0, $scheme_separator ); |
6434 |
||
6435 |
return in_array( $stream, stream_get_wrappers(), true ); |
|
0 | 6436 |
} |
6437 |
||
6438 |
/** |
|
5 | 6439 |
* Test if the supplied date is valid for the Gregorian calendar. |
0 | 6440 |
* |
6441 |
* @since 3.5.0 |
|
6442 |
* |
|
16 | 6443 |
* @link https://www.php.net/manual/en/function.checkdate.php |
6444 |
* |
|
6445 |
* @param int $month Month number. |
|
6446 |
* @param int $day Day number. |
|
6447 |
* @param int $year Year number. |
|
6448 |
* @param string $source_date The date to filter. |
|
5 | 6449 |
* @return bool True if valid date, false if not valid date. |
0 | 6450 |
*/ |
6451 |
function wp_checkdate( $month, $day, $year, $source_date ) { |
|
5 | 6452 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6453 |
* Filters whether the given date is valid for the Gregorian calendar. |
5 | 6454 |
* |
6455 |
* @since 3.5.0 |
|
6456 |
* |
|
6457 |
* @param bool $checkdate Whether the given date is valid. |
|
6458 |
* @param string $source_date Date to check. |
|
6459 |
*/ |
|
0 | 6460 |
return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); |
6461 |
} |
|
6462 |
||
6463 |
/** |
|
6464 |
* Load the auth check for monitoring whether the user is still logged in. |
|
6465 |
* |
|
6466 |
* Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' ); |
|
6467 |
* |
|
6468 |
* This is disabled for certain screens where a login screen could cause an |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6469 |
* inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used |
0 | 6470 |
* for fine-grained control. |
6471 |
* |
|
6472 |
* @since 3.6.0 |
|
6473 |
*/ |
|
6474 |
function wp_auth_check_load() { |
|
9 | 6475 |
if ( ! is_admin() && ! is_user_logged_in() ) { |
0 | 6476 |
return; |
9 | 6477 |
} |
6478 |
||
6479 |
if ( defined( 'IFRAME_REQUEST' ) ) { |
|
0 | 6480 |
return; |
9 | 6481 |
} |
0 | 6482 |
|
6483 |
$screen = get_current_screen(); |
|
6484 |
$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' ); |
|
16 | 6485 |
$show = ! in_array( $screen->id, $hidden, true ); |
0 | 6486 |
|
5 | 6487 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6488 |
* Filters whether to load the authentication check. |
5 | 6489 |
* |
16 | 6490 |
* Returning a falsey value from the filter will effectively short-circuit |
5 | 6491 |
* loading the authentication check. |
6492 |
* |
|
6493 |
* @since 3.6.0 |
|
6494 |
* |
|
6495 |
* @param bool $show Whether to load the authentication check. |
|
6496 |
* @param WP_Screen $screen The current screen object. |
|
6497 |
*/ |
|
0 | 6498 |
if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) { |
6499 |
wp_enqueue_style( 'wp-auth-check' ); |
|
6500 |
wp_enqueue_script( 'wp-auth-check' ); |
|
6501 |
||
6502 |
add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 ); |
|
6503 |
add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 ); |
|
6504 |
} |
|
6505 |
} |
|
6506 |
||
6507 |
/** |
|
6508 |
* Output the HTML that shows the wp-login dialog when the user is no longer logged in. |
|
6509 |
* |
|
6510 |
* @since 3.6.0 |
|
6511 |
*/ |
|
6512 |
function wp_auth_check_html() { |
|
9 | 6513 |
$login_url = wp_login_url(); |
0 | 6514 |
$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST']; |
9 | 6515 |
$same_domain = ( strpos( $login_url, $current_domain ) === 0 ); |
0 | 6516 |
|
5 | 6517 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6518 |
* Filters whether the authentication check originated at the same domain. |
5 | 6519 |
* |
6520 |
* @since 3.6.0 |
|
6521 |
* |
|
6522 |
* @param bool $same_domain Whether the authentication check originated at the same domain. |
|
6523 |
*/ |
|
0 | 6524 |
$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain ); |
9 | 6525 |
$wrap_class = $same_domain ? 'hidden' : 'hidden fallback'; |
0 | 6526 |
|
6527 |
?> |
|
6528 |
<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>"> |
|
6529 |
<div id="wp-auth-check-bg"></div> |
|
6530 |
<div id="wp-auth-check"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6531 |
<button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button> |
0 | 6532 |
<?php |
6533 |
||
6534 |
if ( $same_domain ) { |
|
9 | 6535 |
$login_src = add_query_arg( |
6536 |
array( |
|
6537 |
'interim-login' => '1', |
|
6538 |
'wp_lang' => get_user_locale(), |
|
6539 |
), |
|
6540 |
$login_url |
|
6541 |
); |
|
0 | 6542 |
?> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6543 |
<div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( $login_src ); ?>"></div> |
0 | 6544 |
<?php |
6545 |
} |
|
6546 |
||
6547 |
?> |
|
6548 |
<div class="wp-auth-fallback"> |
|
9 | 6549 |
<p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e( 'Session expired' ); ?></b></p> |
6550 |
<p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e( 'Please log in again.' ); ?></a> |
|
6551 |
<?php _e( 'The login page will open in a new tab. After logging in you can close it and return to this page.' ); ?></p> |
|
0 | 6552 |
</div> |
6553 |
</div> |
|
6554 |
</div> |
|
6555 |
<?php |
|
6556 |
} |
|
6557 |
||
6558 |
/** |
|
6559 |
* Check whether a user is still logged in, for the heartbeat. |
|
6560 |
* |
|
6561 |
* Send a result that shows a log-in box if the user is no longer logged in, |
|
6562 |
* or if their cookie is within the grace period. |
|
6563 |
* |
|
6564 |
* @since 3.6.0 |
|
5 | 6565 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6566 |
* @global int $login_grace_period |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6567 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6568 |
* @param array $response The Heartbeat response. |
16 | 6569 |
* @return array The Heartbeat response with 'wp-auth-check' value set. |
0 | 6570 |
*/ |
5 | 6571 |
function wp_auth_check( $response ) { |
0 | 6572 |
$response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] ); |
6573 |
return $response; |
|
6574 |
} |
|
6575 |
||
6576 |
/** |
|
5 | 6577 |
* Return RegEx body to liberally match an opening HTML tag. |
6578 |
* |
|
6579 |
* Matches an opening HTML tag that: |
|
0 | 6580 |
* 1. Is self-closing or |
6581 |
* 2. Has no body but has a closing tag of the same name or |
|
6582 |
* 3. Contains a body and a closing tag of the same name |
|
6583 |
* |
|
5 | 6584 |
* Note: this RegEx does not balance inner tags and does not attempt |
6585 |
* to produce valid HTML |
|
0 | 6586 |
* |
6587 |
* @since 3.6.0 |
|
6588 |
* |
|
5 | 6589 |
* @param string $tag An HTML tag name. Example: 'video'. |
6590 |
* @return string Tag RegEx. |
|
0 | 6591 |
*/ |
6592 |
function get_tag_regex( $tag ) { |
|
9 | 6593 |
if ( empty( $tag ) ) { |
16 | 6594 |
return ''; |
9 | 6595 |
} |
0 | 6596 |
return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) ); |
6597 |
} |
|
6598 |
||
6599 |
/** |
|
5 | 6600 |
* Retrieve a canonical form of the provided charset appropriate for passing to PHP |
16 | 6601 |
* functions such as htmlspecialchars() and charset HTML attributes. |
0 | 6602 |
* |
6603 |
* @since 3.6.0 |
|
5 | 6604 |
* @access private |
6605 |
* |
|
6606 |
* @see https://core.trac.wordpress.org/ticket/23688 |
|
6607 |
* |
|
6608 |
* @param string $charset A charset name. |
|
6609 |
* @return string The canonical form of the charset. |
|
0 | 6610 |
*/ |
6611 |
function _canonical_charset( $charset ) { |
|
9 | 6612 |
if ( 'utf-8' === strtolower( $charset ) || 'utf8' === strtolower( $charset ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6613 |
|
0 | 6614 |
return 'UTF-8'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6615 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6616 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6617 |
if ( 'iso-8859-1' === strtolower( $charset ) || 'iso8859-1' === strtolower( $charset ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6618 |
|
0 | 6619 |
return 'ISO-8859-1'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6620 |
} |
0 | 6621 |
|
6622 |
return $charset; |
|
6623 |
} |
|
6624 |
||
6625 |
/** |
|
5 | 6626 |
* Set the mbstring internal encoding to a binary safe encoding when func_overload |
6627 |
* is enabled. |
|
6628 |
* |
|
6629 |
* When mbstring.func_overload is in use for multi-byte encodings, the results from |
|
6630 |
* strlen() and similar functions respect the utf8 characters, causing binary data |
|
6631 |
* to return incorrect lengths. |
|
6632 |
* |
|
6633 |
* This function overrides the mbstring encoding to a binary-safe encoding, and |
|
6634 |
* resets it to the users expected encoding afterwards through the |
|
6635 |
* `reset_mbstring_encoding` function. |
|
6636 |
* |
|
6637 |
* It is safe to recursively call this function, however each |
|
6638 |
* `mbstring_binary_safe_encoding()` call must be followed up with an equal number |
|
6639 |
* of `reset_mbstring_encoding()` calls. |
|
6640 |
* |
|
6641 |
* @since 3.7.0 |
|
0 | 6642 |
* |
6643 |
* @see reset_mbstring_encoding() |
|
6644 |
* |
|
5 | 6645 |
* @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding. |
6646 |
* Default false. |
|
0 | 6647 |
*/ |
6648 |
function mbstring_binary_safe_encoding( $reset = false ) { |
|
9 | 6649 |
static $encodings = array(); |
0 | 6650 |
static $overloaded = null; |
6651 |
||
9 | 6652 |
if ( is_null( $overloaded ) ) { |
16 | 6653 |
$overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 ); // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated |
9 | 6654 |
} |
6655 |
||
6656 |
if ( false === $overloaded ) { |
|
0 | 6657 |
return; |
9 | 6658 |
} |
0 | 6659 |
|
6660 |
if ( ! $reset ) { |
|
6661 |
$encoding = mb_internal_encoding(); |
|
6662 |
array_push( $encodings, $encoding ); |
|
6663 |
mb_internal_encoding( 'ISO-8859-1' ); |
|
6664 |
} |
|
6665 |
||
6666 |
if ( $reset && $encodings ) { |
|
6667 |
$encoding = array_pop( $encodings ); |
|
6668 |
mb_internal_encoding( $encoding ); |
|
6669 |
} |
|
6670 |
} |
|
6671 |
||
6672 |
/** |
|
5 | 6673 |
* Reset the mbstring internal encoding to a users previously set encoding. |
0 | 6674 |
* |
6675 |
* @see mbstring_binary_safe_encoding() |
|
6676 |
* |
|
6677 |
* @since 3.7.0 |
|
6678 |
*/ |
|
6679 |
function reset_mbstring_encoding() { |
|
6680 |
mbstring_binary_safe_encoding( true ); |
|
6681 |
} |
|
5 | 6682 |
|
6683 |
/** |
|
6684 |
* Filter/validate a variable as a boolean. |
|
6685 |
* |
|
6686 |
* Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`. |
|
6687 |
* |
|
6688 |
* @since 4.0.0 |
|
6689 |
* |
|
6690 |
* @param mixed $var Boolean value to validate. |
|
6691 |
* @return bool Whether the value is validated. |
|
6692 |
*/ |
|
6693 |
function wp_validate_boolean( $var ) { |
|
6694 |
if ( is_bool( $var ) ) { |
|
6695 |
return $var; |
|
6696 |
} |
|
6697 |
||
6698 |
if ( is_string( $var ) && 'false' === strtolower( $var ) ) { |
|
6699 |
return false; |
|
6700 |
} |
|
6701 |
||
6702 |
return (bool) $var; |
|
6703 |
} |
|
6704 |
||
6705 |
/** |
|
6706 |
* Delete a file |
|
6707 |
* |
|
6708 |
* @since 4.2.0 |
|
6709 |
* |
|
6710 |
* @param string $file The path to the file to delete. |
|
6711 |
*/ |
|
6712 |
function wp_delete_file( $file ) { |
|
6713 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6714 |
* Filters the path of the file to delete. |
5 | 6715 |
* |
6716 |
* @since 2.1.0 |
|
6717 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6718 |
* @param string $file Path to the file to delete. |
5 | 6719 |
*/ |
6720 |
$delete = apply_filters( 'wp_delete_file', $file ); |
|
6721 |
if ( ! empty( $delete ) ) { |
|
6722 |
@unlink( $delete ); |
|
6723 |
} |
|
6724 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6725 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6726 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6727 |
* Deletes a file if its path is within the given directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6728 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6729 |
* @since 4.9.7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6730 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6731 |
* @param string $file Absolute path to the file to delete. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6732 |
* @param string $directory Absolute path to a directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6733 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6734 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6735 |
function wp_delete_file_from_directory( $file, $directory ) { |
9 | 6736 |
if ( wp_is_stream( $file ) ) { |
6737 |
$real_file = $file; |
|
6738 |
$real_directory = $directory; |
|
6739 |
} else { |
|
6740 |
$real_file = realpath( wp_normalize_path( $file ) ); |
|
6741 |
$real_directory = realpath( wp_normalize_path( $directory ) ); |
|
6742 |
} |
|
6743 |
||
6744 |
if ( false !== $real_file ) { |
|
6745 |
$real_file = wp_normalize_path( $real_file ); |
|
6746 |
} |
|
6747 |
||
6748 |
if ( false !== $real_directory ) { |
|
6749 |
$real_directory = wp_normalize_path( $real_directory ); |
|
6750 |
} |
|
6751 |
||
6752 |
if ( false === $real_file || false === $real_directory || strpos( $real_file, trailingslashit( $real_directory ) ) !== 0 ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6753 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6754 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6755 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6756 |
wp_delete_file( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6757 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6758 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6759 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6760 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6761 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6762 |
* Outputs a small JS snippet on preview tabs/windows to remove `window.name` on unload. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6763 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6764 |
* This prevents reusing the same tab for a preview when the user has navigated away. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6765 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6766 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6767 |
* |
16 | 6768 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6769 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6770 |
function wp_post_preview_js() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6771 |
global $post; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6772 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6773 |
if ( ! is_preview() || empty( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6774 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6775 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6776 |
|
16 | 6777 |
// Has to match the window name used in post_submit_meta_box(). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6778 |
$name = 'wp-preview-' . (int) $post->ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6779 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6780 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6781 |
<script> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6782 |
( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6783 |
var query = document.location.search; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6784 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6785 |
if ( query && query.indexOf( 'preview=true' ) !== -1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6786 |
window.name = '<?php echo $name; ?>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6787 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6788 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6789 |
if ( window.addEventListener ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6790 |
window.addEventListener( 'unload', function() { window.name = ''; }, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6791 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6792 |
}()); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6793 |
</script> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6794 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6795 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6796 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6797 |
/** |
9 | 6798 |
* Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601 (Y-m-d\TH:i:s). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6799 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6800 |
* Explicitly strips timezones, as datetimes are not saved with any timezone |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6801 |
* information. Including any information on the offset could be misleading. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6802 |
* |
9 | 6803 |
* Despite historical function name, the output does not conform to RFC3339 format, |
6804 |
* which must contain timezone. |
|
6805 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6806 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6807 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6808 |
* @param string $date_string Date string to parse and format. |
9 | 6809 |
* @return string Date formatted for ISO8601 without time zone. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6810 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6811 |
function mysql_to_rfc3339( $date_string ) { |
9 | 6812 |
return mysql2date( 'Y-m-d\TH:i:s', $date_string, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6813 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6814 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6815 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6816 |
* Attempts to raise the PHP memory limit for memory intensive processes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6817 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6818 |
* Only allows raising the existing limit and prevents lowering it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6819 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6820 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6821 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6822 |
* @param string $context Optional. Context in which the function is called. Accepts either 'admin', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6823 |
* 'image', or an arbitrary other context. If an arbitrary context is passed, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6824 |
* the similarly arbitrary {@see '{$context}_memory_limit'} filter will be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6825 |
* invoked. Default 'admin'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6826 |
* @return bool|int|string The limit that was set or false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6827 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6828 |
function wp_raise_memory_limit( $context = 'admin' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6829 |
// Exit early if the limit cannot be changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6830 |
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6831 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6832 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6833 |
|
16 | 6834 |
$current_limit = ini_get( 'memory_limit' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6835 |
$current_limit_int = wp_convert_hr_to_bytes( $current_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6836 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6837 |
if ( -1 === $current_limit_int ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6838 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6839 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6840 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6841 |
$wp_max_limit = WP_MAX_MEMORY_LIMIT; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6842 |
$wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6843 |
$filtered_limit = $wp_max_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6844 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6845 |
switch ( $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6846 |
case 'admin': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6847 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6848 |
* Filters the maximum memory limit available for administration screens. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6849 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6850 |
* This only applies to administrators, who may require more memory for tasks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6851 |
* like updates. Memory limits when processing images (uploaded or edited by |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6852 |
* users of any role) are handled separately. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6853 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6854 |
* The `WP_MAX_MEMORY_LIMIT` constant specifically defines the maximum memory |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6855 |
* limit available when in the administration back end. The default is 256M |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6856 |
* (256 megabytes of memory) or the original `memory_limit` php.ini value if |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6857 |
* this is higher. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6858 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6859 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6860 |
* @since 4.6.0 The default now takes the original `memory_limit` into account. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6861 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6862 |
* @param int|string $filtered_limit The maximum WordPress memory limit. Accepts an integer |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6863 |
* (bytes), or a shorthand string notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6864 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6865 |
$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6866 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6867 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6868 |
case 'image': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6869 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6870 |
* Filters the memory limit allocated for image manipulation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6871 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6872 |
* @since 3.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6873 |
* @since 4.6.0 The default now takes the original `memory_limit` into account. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6874 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6875 |
* @param int|string $filtered_limit Maximum memory limit to allocate for images. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6876 |
* Default `WP_MAX_MEMORY_LIMIT` or the original |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6877 |
* php.ini `memory_limit`, whichever is higher. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6878 |
* Accepts an integer (bytes), or a shorthand string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6879 |
* notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6880 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6881 |
$filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6882 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6883 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6884 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6885 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6886 |
* Filters the memory limit allocated for arbitrary contexts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6887 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6888 |
* The dynamic portion of the hook name, `$context`, refers to an arbitrary |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6889 |
* context passed on calling the function. This allows for plugins to define |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6890 |
* their own contexts for raising the memory limit. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6891 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6892 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6893 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6894 |
* @param int|string $filtered_limit Maximum memory limit to allocate for images. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6895 |
* Default '256M' or the original php.ini `memory_limit`, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6896 |
* whichever is higher. Accepts an integer (bytes), or a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6897 |
* shorthand string notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6898 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6899 |
$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6900 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6901 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6902 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6903 |
$filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6904 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6905 |
if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
16 | 6906 |
if ( false !== ini_set( 'memory_limit', $filtered_limit ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6907 |
return $filtered_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6908 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6909 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6910 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6911 |
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
16 | 6912 |
if ( false !== ini_set( 'memory_limit', $wp_max_limit ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6913 |
return $wp_max_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6914 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6915 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6916 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6917 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6918 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6919 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6920 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6921 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6922 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6923 |
* Generate a random UUID (version 4). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6924 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6925 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6926 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6927 |
* @return string UUID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6928 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6929 |
function wp_generate_uuid4() { |
9 | 6930 |
return sprintf( |
6931 |
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
6932 |
mt_rand( 0, 0xffff ), |
|
6933 |
mt_rand( 0, 0xffff ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6934 |
mt_rand( 0, 0xffff ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6935 |
mt_rand( 0, 0x0fff ) | 0x4000, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6936 |
mt_rand( 0, 0x3fff ) | 0x8000, |
9 | 6937 |
mt_rand( 0, 0xffff ), |
6938 |
mt_rand( 0, 0xffff ), |
|
6939 |
mt_rand( 0, 0xffff ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6940 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6941 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6942 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6943 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6944 |
* Validates that a UUID is valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6945 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6946 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6947 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6948 |
* @param mixed $uuid UUID to check. |
9 | 6949 |
* @param int $version Specify which version of UUID to check against. Default is none, |
6950 |
* to accept any UUID version. Otherwise, only version allowed is `4`. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6951 |
* @return bool The string is a valid UUID or false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6952 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6953 |
function wp_is_uuid( $uuid, $version = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6954 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6955 |
if ( ! is_string( $uuid ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6956 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6957 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6958 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6959 |
if ( is_numeric( $version ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6960 |
if ( 4 !== (int) $version ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6961 |
_doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ), '4.9.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6962 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6963 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6964 |
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6965 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6966 |
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6967 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6968 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6969 |
return (bool) preg_match( $regex, $uuid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6970 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6971 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6972 |
/** |
16 | 6973 |
* Gets unique ID. |
9 | 6974 |
* |
6975 |
* This is a PHP implementation of Underscore's uniqueId method. A static variable |
|
6976 |
* contains an integer that is incremented with each call. This number is returned |
|
6977 |
* with the optional prefix. As such the returned value is not universally unique, |
|
6978 |
* but it is unique across the life of the PHP process. |
|
6979 |
* |
|
6980 |
* @since 5.0.3 |
|
6981 |
* |
|
6982 |
* @param string $prefix Prefix for the returned ID. |
|
6983 |
* @return string Unique ID. |
|
6984 |
*/ |
|
6985 |
function wp_unique_id( $prefix = '' ) { |
|
6986 |
static $id_counter = 0; |
|
6987 |
return $prefix . (string) ++$id_counter; |
|
6988 |
} |
|
6989 |
||
6990 |
/** |
|
16 | 6991 |
* Gets last changed date for the specified cache group. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6992 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6993 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6994 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6995 |
* @param string $group Where the cache contents are grouped. |
16 | 6996 |
* @return string UNIX timestamp with microseconds representing when the group was last changed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6997 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6998 |
function wp_cache_get_last_changed( $group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6999 |
$last_changed = wp_cache_get( 'last_changed', $group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7000 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7001 |
if ( ! $last_changed ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7002 |
$last_changed = microtime(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7003 |
wp_cache_set( 'last_changed', $last_changed, $group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7004 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7005 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7006 |
return $last_changed; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7007 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7008 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7009 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7010 |
* Send an email to the old site admin email address when the site admin email address changes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7011 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7012 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7013 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7014 |
* @param string $old_email The old site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7015 |
* @param string $new_email The new site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7016 |
* @param string $option_name The relevant database option name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7017 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7018 |
function wp_site_admin_email_change_notification( $old_email, $new_email, $option_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7019 |
$send = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7020 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7021 |
// Don't send the notification to the default 'admin_email' value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7022 |
if ( 'you@example.com' === $old_email ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7023 |
$send = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7024 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7025 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7026 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7027 |
* Filters whether to send the site admin email change notification email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7028 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7029 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7030 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7031 |
* @param bool $send Whether to send the email notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7032 |
* @param string $old_email The old site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7033 |
* @param string $new_email The new site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7034 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7035 |
$send = apply_filters( 'send_site_admin_email_change_email', $send, $old_email, $new_email ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7036 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7037 |
if ( ! $send ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7038 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7039 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7040 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7041 |
/* translators: Do not translate OLD_EMAIL, NEW_EMAIL, SITENAME, SITEURL: those are placeholders. */ |
9 | 7042 |
$email_change_text = __( |
7043 |
'Hi, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7044 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7045 |
This notice confirms that the admin email address was changed on ###SITENAME###. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7046 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7047 |
The new admin email address is ###NEW_EMAIL###. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7048 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7049 |
This email has been sent to ###OLD_EMAIL### |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7050 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7051 |
Regards, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7052 |
All at ###SITENAME### |
9 | 7053 |
###SITEURL###' |
7054 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7055 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7056 |
$email_change_email = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7057 |
'to' => $old_email, |
16 | 7058 |
/* translators: Site admin email change notification email subject. %s: Site title. */ |
9 | 7059 |
'subject' => __( '[%s] Admin Email Changed' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7060 |
'message' => $email_change_text, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7061 |
'headers' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7062 |
); |
16 | 7063 |
|
7064 |
// Get site name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7065 |
$site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7066 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7067 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7068 |
* Filters the contents of the email notification sent when the site admin email address is changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7069 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7070 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7071 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7072 |
* @param array $email_change_email { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7073 |
* Used to build wp_mail(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7074 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7075 |
* @type string $to The intended recipient. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7076 |
* @type string $subject The subject of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7077 |
* @type string $message The content of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7078 |
* The following strings have a special meaning and will get replaced dynamically: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7079 |
* - ###OLD_EMAIL### The old site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7080 |
* - ###NEW_EMAIL### The new site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7081 |
* - ###SITENAME### The name of the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7082 |
* - ###SITEURL### The URL to the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7083 |
* @type string $headers Headers. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7084 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7085 |
* @param string $old_email The old site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7086 |
* @param string $new_email The new site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7087 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7088 |
$email_change_email = apply_filters( 'site_admin_email_change_email', $email_change_email, $old_email, $new_email ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7089 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7090 |
$email_change_email['message'] = str_replace( '###OLD_EMAIL###', $old_email, $email_change_email['message'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7091 |
$email_change_email['message'] = str_replace( '###NEW_EMAIL###', $new_email, $email_change_email['message'] ); |
9 | 7092 |
$email_change_email['message'] = str_replace( '###SITENAME###', $site_name, $email_change_email['message'] ); |
7093 |
$email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] ); |
|
7094 |
||
7095 |
wp_mail( |
|
7096 |
$email_change_email['to'], |
|
7097 |
sprintf( |
|
7098 |
$email_change_email['subject'], |
|
7099 |
$site_name |
|
7100 |
), |
|
7101 |
$email_change_email['message'], |
|
7102 |
$email_change_email['headers'] |
|
7103 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7104 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7105 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7106 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7107 |
* Return an anonymized IPv4 or IPv6 address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7108 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7109 |
* @since 4.9.6 Abstracted from `WP_Community_Events::get_unsafe_client_ip()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7110 |
* |
16 | 7111 |
* @param string $ip_addr The IPv4 or IPv6 address to be anonymized. |
7112 |
* @param bool $ipv6_fallback Optional. Whether to return the original IPv6 address if the needed functions |
|
7113 |
* to anonymize it are not present. Default false, return `::` (unspecified address). |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7114 |
* @return string The anonymized IP address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7115 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7116 |
function wp_privacy_anonymize_ip( $ip_addr, $ipv6_fallback = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7117 |
// Detect what kind of IP address this is. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7118 |
$ip_prefix = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7119 |
$is_ipv6 = substr_count( $ip_addr, ':' ) > 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7120 |
$is_ipv4 = ( 3 === substr_count( $ip_addr, '.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7121 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7122 |
if ( $is_ipv6 && $is_ipv4 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7123 |
// IPv6 compatibility mode, temporarily strip the IPv6 part, and treat it like IPv4. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7124 |
$ip_prefix = '::ffff:'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7125 |
$ip_addr = preg_replace( '/^\[?[0-9a-f:]*:/i', '', $ip_addr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7126 |
$ip_addr = str_replace( ']', '', $ip_addr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7127 |
$is_ipv6 = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7128 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7130 |
if ( $is_ipv6 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7131 |
// IPv6 addresses will always be enclosed in [] if there's a port. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7132 |
$left_bracket = strpos( $ip_addr, '[' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7133 |
$right_bracket = strpos( $ip_addr, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7134 |
$percent = strpos( $ip_addr, '%' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7135 |
$netmask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7136 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7137 |
// Strip the port (and [] from IPv6 addresses), if they exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7138 |
if ( false !== $left_bracket && false !== $right_bracket ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7139 |
$ip_addr = substr( $ip_addr, $left_bracket + 1, $right_bracket - $left_bracket - 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7140 |
} elseif ( false !== $left_bracket || false !== $right_bracket ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7141 |
// The IP has one bracket, but not both, so it's malformed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7142 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7143 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7144 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7145 |
// Strip the reachability scope. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7146 |
if ( false !== $percent ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7147 |
$ip_addr = substr( $ip_addr, 0, $percent ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7148 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7149 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7150 |
// No invalid characters should be left. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7151 |
if ( preg_match( '/[^0-9a-f:]/i', $ip_addr ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7152 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7153 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7154 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7155 |
// Partially anonymize the IP by reducing it to the corresponding network ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7156 |
if ( function_exists( 'inet_pton' ) && function_exists( 'inet_ntop' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7157 |
$ip_addr = inet_ntop( inet_pton( $ip_addr ) & inet_pton( $netmask ) ); |
9 | 7158 |
if ( false === $ip_addr ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7159 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7160 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7161 |
} elseif ( ! $ipv6_fallback ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7162 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7163 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7164 |
} elseif ( $is_ipv4 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7165 |
// Strip any port and partially anonymize the IP. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7166 |
$last_octet_position = strrpos( $ip_addr, '.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7167 |
$ip_addr = substr( $ip_addr, 0, $last_octet_position ) . '.0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7168 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7169 |
return '0.0.0.0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7170 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7171 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7172 |
// Restore the IPv6 prefix to compatibility mode addresses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7173 |
return $ip_prefix . $ip_addr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7174 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7175 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7176 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7177 |
* Return uniform "anonymous" data by type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7178 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7179 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7180 |
* |
16 | 7181 |
* @param string $type The type of data to be anonymized. |
7182 |
* @param string $data Optional The data to be anonymized. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7183 |
* @return string The anonymous data for the requested type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7184 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7185 |
function wp_privacy_anonymize_data( $type, $data = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7186 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7187 |
switch ( $type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7188 |
case 'email': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7189 |
$anonymous = 'deleted@site.invalid'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7190 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7191 |
case 'url': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7192 |
$anonymous = 'https://site.invalid'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7193 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7194 |
case 'ip': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7195 |
$anonymous = wp_privacy_anonymize_ip( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7196 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7197 |
case 'date': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7198 |
$anonymous = '0000-00-00 00:00:00'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7199 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7200 |
case 'text': |
16 | 7201 |
/* translators: Deleted text. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7202 |
$anonymous = __( '[deleted]' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7203 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7204 |
case 'longtext': |
16 | 7205 |
/* translators: Deleted long text. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7206 |
$anonymous = __( 'This content was deleted by the author.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7207 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7208 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7209 |
$anonymous = ''; |
16 | 7210 |
break; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7211 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7212 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7213 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7214 |
* Filters the anonymous data for each type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7215 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7216 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7217 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7218 |
* @param string $anonymous Anonymized data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7219 |
* @param string $type Type of the data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7220 |
* @param string $data Original data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7221 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7222 |
return apply_filters( 'wp_privacy_anonymize_data', $anonymous, $type, $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7223 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7224 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7225 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7226 |
* Returns the directory used to store personal data export files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7227 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7228 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7230 |
* @see wp_privacy_exports_url |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7231 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7232 |
* @return string Exports directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7233 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7234 |
function wp_privacy_exports_dir() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7235 |
$upload_dir = wp_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7236 |
$exports_dir = trailingslashit( $upload_dir['basedir'] ) . 'wp-personal-data-exports/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7237 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7238 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7239 |
* Filters the directory used to store personal data export files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7240 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7241 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7242 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7243 |
* @param string $exports_dir Exports directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7244 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7245 |
return apply_filters( 'wp_privacy_exports_dir', $exports_dir ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7246 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7247 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7248 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7249 |
* Returns the URL of the directory used to store personal data export files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7250 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7251 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7252 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7253 |
* @see wp_privacy_exports_dir |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7254 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7255 |
* @return string Exports directory URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7256 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7257 |
function wp_privacy_exports_url() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7258 |
$upload_dir = wp_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7259 |
$exports_url = trailingslashit( $upload_dir['baseurl'] ) . 'wp-personal-data-exports/'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7260 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7261 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7262 |
* Filters the URL of the directory used to store personal data export files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7263 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7264 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7265 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7266 |
* @param string $exports_url Exports directory URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7267 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7268 |
return apply_filters( 'wp_privacy_exports_url', $exports_url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7269 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7270 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7271 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7272 |
* Schedule a `WP_Cron` job to delete expired export files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7273 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7274 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7275 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7276 |
function wp_schedule_delete_old_privacy_export_files() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7277 |
if ( wp_installing() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7278 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7279 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7280 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7281 |
if ( ! wp_next_scheduled( 'wp_privacy_delete_old_export_files' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7282 |
wp_schedule_event( time(), 'hourly', 'wp_privacy_delete_old_export_files' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7283 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7284 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7285 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7286 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7287 |
* Cleans up export files older than three days old. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7288 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7289 |
* The export files are stored in `wp-content/uploads`, and are therefore publicly |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7290 |
* accessible. A CSPRN is appended to the filename to mitigate the risk of an |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7291 |
* unauthorized person downloading the file, but it is still possible. Deleting |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7292 |
* the file after the data subject has had a chance to delete it adds an additional |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7293 |
* layer of protection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7294 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7295 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7296 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7297 |
function wp_privacy_delete_old_export_files() { |
9 | 7298 |
$exports_dir = wp_privacy_exports_dir(); |
7299 |
if ( ! is_dir( $exports_dir ) ) { |
|
7300 |
return; |
|
7301 |
} |
|
7302 |
||
16 | 7303 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7304 |
$export_files = list_files( $exports_dir, 100, array( 'index.html' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7305 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7306 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7307 |
* Filters the lifetime, in seconds, of a personal data export file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7308 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7309 |
* By default, the lifetime is 3 days. Once the file reaches that age, it will automatically |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7310 |
* be deleted by a cron job. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7311 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7312 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7313 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7314 |
* @param int $expiration The expiration age of the export, in seconds. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7315 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7316 |
$expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7317 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7318 |
foreach ( (array) $export_files as $export_file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7319 |
$file_age_in_seconds = time() - filemtime( $export_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7321 |
if ( $expiration < $file_age_in_seconds ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7322 |
unlink( $export_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7323 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7324 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7325 |
} |
9 | 7326 |
|
7327 |
/** |
|
7328 |
* Gets the URL to learn more about updating the PHP version the site is running on. |
|
7329 |
* |
|
7330 |
* This URL can be overridden by specifying an environment variable `WP_UPDATE_PHP_URL` or by using the |
|
7331 |
* {@see 'wp_update_php_url'} filter. Providing an empty string is not allowed and will result in the |
|
7332 |
* default URL being used. Furthermore the page the URL links to should preferably be localized in the |
|
7333 |
* site language. |
|
7334 |
* |
|
7335 |
* @since 5.1.0 |
|
7336 |
* |
|
7337 |
* @return string URL to learn more about updating PHP. |
|
7338 |
*/ |
|
7339 |
function wp_get_update_php_url() { |
|
7340 |
$default_url = wp_get_default_update_php_url(); |
|
7341 |
||
7342 |
$update_url = $default_url; |
|
7343 |
if ( false !== getenv( 'WP_UPDATE_PHP_URL' ) ) { |
|
7344 |
$update_url = getenv( 'WP_UPDATE_PHP_URL' ); |
|
7345 |
} |
|
7346 |
||
7347 |
/** |
|
7348 |
* Filters the URL to learn more about updating the PHP version the site is running on. |
|
7349 |
* |
|
7350 |
* Providing an empty string is not allowed and will result in the default URL being used. Furthermore |
|
7351 |
* the page the URL links to should preferably be localized in the site language. |
|
7352 |
* |
|
7353 |
* @since 5.1.0 |
|
7354 |
* |
|
7355 |
* @param string $update_url URL to learn more about updating PHP. |
|
7356 |
*/ |
|
7357 |
$update_url = apply_filters( 'wp_update_php_url', $update_url ); |
|
7358 |
||
7359 |
if ( empty( $update_url ) ) { |
|
7360 |
$update_url = $default_url; |
|
7361 |
} |
|
7362 |
||
7363 |
return $update_url; |
|
7364 |
} |
|
7365 |
||
7366 |
/** |
|
7367 |
* Gets the default URL to learn more about updating the PHP version the site is running on. |
|
7368 |
* |
|
7369 |
* Do not use this function to retrieve this URL. Instead, use {@see wp_get_update_php_url()} when relying on the URL. |
|
7370 |
* This function does not allow modifying the returned URL, and is only used to compare the actually used URL with the |
|
7371 |
* default one. |
|
7372 |
* |
|
7373 |
* @since 5.1.0 |
|
7374 |
* @access private |
|
7375 |
* |
|
7376 |
* @return string Default URL to learn more about updating PHP. |
|
7377 |
*/ |
|
7378 |
function wp_get_default_update_php_url() { |
|
7379 |
return _x( 'https://wordpress.org/support/update-php/', 'localized PHP upgrade information page' ); |
|
7380 |
} |
|
7381 |
||
7382 |
/** |
|
7383 |
* Prints the default annotation for the web host altering the "Update PHP" page URL. |
|
7384 |
* |
|
7385 |
* This function is to be used after {@see wp_get_update_php_url()} to display a consistent |
|
7386 |
* annotation if the web host has altered the default "Update PHP" page URL. |
|
7387 |
* |
|
7388 |
* @since 5.1.0 |
|
7389 |
* @since 5.2.0 Added the `$before` and `$after` parameters. |
|
7390 |
* |
|
7391 |
* @param string $before Markup to output before the annotation. Default `<p class="description">`. |
|
7392 |
* @param string $after Markup to output after the annotation. Default `</p>`. |
|
7393 |
*/ |
|
7394 |
function wp_update_php_annotation( $before = '<p class="description">', $after = '</p>' ) { |
|
7395 |
$annotation = wp_get_update_php_annotation(); |
|
7396 |
||
7397 |
if ( $annotation ) { |
|
7398 |
echo $before . $annotation . $after; |
|
7399 |
} |
|
7400 |
} |
|
7401 |
||
7402 |
/** |
|
7403 |
* Returns the default annotation for the web hosting altering the "Update PHP" page URL. |
|
7404 |
* |
|
7405 |
* This function is to be used after {@see wp_get_update_php_url()} to return a consistent |
|
7406 |
* annotation if the web host has altered the default "Update PHP" page URL. |
|
7407 |
* |
|
7408 |
* @since 5.2.0 |
|
7409 |
* |
|
16 | 7410 |
* @return string Update PHP page annotation. An empty string if no custom URLs are provided. |
9 | 7411 |
*/ |
7412 |
function wp_get_update_php_annotation() { |
|
7413 |
$update_url = wp_get_update_php_url(); |
|
7414 |
$default_url = wp_get_default_update_php_url(); |
|
7415 |
||
7416 |
if ( $update_url === $default_url ) { |
|
7417 |
return ''; |
|
7418 |
} |
|
7419 |
||
7420 |
$annotation = sprintf( |
|
16 | 7421 |
/* translators: %s: Default Update PHP page URL. */ |
9 | 7422 |
__( 'This resource is provided by your web host, and is specific to your site. For more information, <a href="%s" target="_blank">see the official WordPress documentation</a>.' ), |
7423 |
esc_url( $default_url ) |
|
7424 |
); |
|
7425 |
||
7426 |
return $annotation; |
|
7427 |
} |
|
7428 |
||
7429 |
/** |
|
7430 |
* Gets the URL for directly updating the PHP version the site is running on. |
|
7431 |
* |
|
7432 |
* A URL will only be returned if the `WP_DIRECT_UPDATE_PHP_URL` environment variable is specified or |
|
7433 |
* by using the {@see 'wp_direct_php_update_url'} filter. This allows hosts to send users directly to |
|
7434 |
* the page where they can update PHP to a newer version. |
|
7435 |
* |
|
7436 |
* @since 5.1.1 |
|
7437 |
* |
|
7438 |
* @return string URL for directly updating PHP or empty string. |
|
7439 |
*/ |
|
7440 |
function wp_get_direct_php_update_url() { |
|
7441 |
$direct_update_url = ''; |
|
7442 |
||
7443 |
if ( false !== getenv( 'WP_DIRECT_UPDATE_PHP_URL' ) ) { |
|
7444 |
$direct_update_url = getenv( 'WP_DIRECT_UPDATE_PHP_URL' ); |
|
7445 |
} |
|
7446 |
||
7447 |
/** |
|
7448 |
* Filters the URL for directly updating the PHP version the site is running on from the host. |
|
7449 |
* |
|
7450 |
* @since 5.1.1 |
|
7451 |
* |
|
7452 |
* @param string $direct_update_url URL for directly updating PHP. |
|
7453 |
*/ |
|
7454 |
$direct_update_url = apply_filters( 'wp_direct_php_update_url', $direct_update_url ); |
|
7455 |
||
7456 |
return $direct_update_url; |
|
7457 |
} |
|
7458 |
||
7459 |
/** |
|
7460 |
* Display a button directly linking to a PHP update process. |
|
7461 |
* |
|
7462 |
* This provides hosts with a way for users to be sent directly to their PHP update process. |
|
7463 |
* |
|
7464 |
* The button is only displayed if a URL is returned by `wp_get_direct_php_update_url()`. |
|
7465 |
* |
|
7466 |
* @since 5.1.1 |
|
7467 |
*/ |
|
7468 |
function wp_direct_php_update_button() { |
|
7469 |
$direct_update_url = wp_get_direct_php_update_url(); |
|
7470 |
||
7471 |
if ( empty( $direct_update_url ) ) { |
|
7472 |
return; |
|
7473 |
} |
|
7474 |
||
7475 |
echo '<p class="button-container">'; |
|
7476 |
printf( |
|
7477 |
'<a class="button button-primary" href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
|
7478 |
esc_url( $direct_update_url ), |
|
7479 |
__( 'Update PHP' ), |
|
16 | 7480 |
/* translators: Accessibility text. */ |
9 | 7481 |
__( '(opens in a new tab)' ) |
7482 |
); |
|
7483 |
echo '</p>'; |
|
7484 |
} |
|
7485 |
||
7486 |
/** |
|
7487 |
* Get the size of a directory. |
|
7488 |
* |
|
7489 |
* A helper function that is used primarily to check whether |
|
7490 |
* a blog has exceeded its allowed upload space. |
|
7491 |
* |
|
7492 |
* @since MU (3.0.0) |
|
7493 |
* @since 5.2.0 $max_execution_time parameter added. |
|
7494 |
* |
|
7495 |
* @param string $directory Full path of a directory. |
|
7496 |
* @param int $max_execution_time Maximum time to run before giving up. In seconds. |
|
7497 |
* The timeout is global and is measured from the moment WordPress started to load. |
|
7498 |
* @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. |
|
7499 |
*/ |
|
7500 |
function get_dirsize( $directory, $max_execution_time = null ) { |
|
7501 |
$dirsize = get_transient( 'dirsize_cache' ); |
|
7502 |
||
7503 |
if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) { |
|
7504 |
return $dirsize[ $directory ]['size']; |
|
7505 |
} |
|
7506 |
||
7507 |
if ( ! is_array( $dirsize ) ) { |
|
7508 |
$dirsize = array(); |
|
7509 |
} |
|
7510 |
||
16 | 7511 |
// Exclude individual site directories from the total when checking the main site of a network, |
9 | 7512 |
// as they are subdirectories and should not be counted. |
7513 |
if ( is_multisite() && is_main_site() ) { |
|
7514 |
$dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time ); |
|
7515 |
} else { |
|
7516 |
$dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time ); |
|
7517 |
} |
|
7518 |
||
7519 |
set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS ); |
|
7520 |
return $dirsize[ $directory ]['size']; |
|
7521 |
} |
|
7522 |
||
7523 |
/** |
|
7524 |
* Get the size of a directory recursively. |
|
7525 |
* |
|
7526 |
* Used by get_dirsize() to get a directory's size when it contains |
|
7527 |
* other directories. |
|
7528 |
* |
|
7529 |
* @since MU (3.0.0) |
|
7530 |
* @since 4.3.0 $exclude parameter added. |
|
7531 |
* @since 5.2.0 $max_execution_time parameter added. |
|
7532 |
* |
|
16 | 7533 |
* @param string $directory Full path of a directory. |
7534 |
* @param string|array $exclude Optional. Full path of a subdirectory to exclude from the total, |
|
7535 |
* or array of paths. Expected without trailing slash(es). |
|
7536 |
* @param int $max_execution_time Maximum time to run before giving up. In seconds. The timeout is global |
|
7537 |
* and is measured from the moment WordPress started to load. |
|
9 | 7538 |
* @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. |
7539 |
*/ |
|
7540 |
function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) { |
|
7541 |
$size = 0; |
|
7542 |
||
7543 |
$directory = untrailingslashit( $directory ); |
|
7544 |
||
7545 |
if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) { |
|
7546 |
return false; |
|
7547 |
} |
|
7548 |
||
7549 |
if ( |
|
7550 |
( is_string( $exclude ) && $directory === $exclude ) || |
|
7551 |
( is_array( $exclude ) && in_array( $directory, $exclude, true ) ) |
|
7552 |
) { |
|
7553 |
return false; |
|
7554 |
} |
|
7555 |
||
16 | 7556 |
if ( null === $max_execution_time ) { |
9 | 7557 |
// Keep the previous behavior but attempt to prevent fatal errors from timeout if possible. |
7558 |
if ( function_exists( 'ini_get' ) ) { |
|
7559 |
$max_execution_time = ini_get( 'max_execution_time' ); |
|
7560 |
} else { |
|
7561 |
// Disable... |
|
7562 |
$max_execution_time = 0; |
|
7563 |
} |
|
7564 |
||
7565 |
// Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value. |
|
7566 |
if ( $max_execution_time > 10 ) { |
|
7567 |
$max_execution_time -= 1; |
|
7568 |
} |
|
7569 |
} |
|
7570 |
||
16 | 7571 |
$handle = opendir( $directory ); |
7572 |
if ( $handle ) { |
|
9 | 7573 |
while ( ( $file = readdir( $handle ) ) !== false ) { |
7574 |
$path = $directory . '/' . $file; |
|
16 | 7575 |
if ( '.' !== $file && '..' !== $file ) { |
9 | 7576 |
if ( is_file( $path ) ) { |
7577 |
$size += filesize( $path ); |
|
7578 |
} elseif ( is_dir( $path ) ) { |
|
7579 |
$handlesize = recurse_dirsize( $path, $exclude, $max_execution_time ); |
|
7580 |
if ( $handlesize > 0 ) { |
|
7581 |
$size += $handlesize; |
|
7582 |
} |
|
7583 |
} |
|
7584 |
||
7585 |
if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) { |
|
7586 |
// Time exceeded. Give up instead of risking a fatal timeout. |
|
7587 |
$size = null; |
|
7588 |
break; |
|
7589 |
} |
|
7590 |
} |
|
7591 |
} |
|
7592 |
closedir( $handle ); |
|
7593 |
} |
|
7594 |
return $size; |
|
7595 |
} |
|
7596 |
||
7597 |
/** |
|
16 | 7598 |
* Checks compatibility with the current WordPress version. |
7599 |
* |
|
7600 |
* @since 5.2.0 |
|
7601 |
* |
|
7602 |
* @param string $required Minimum required WordPress version. |
|
7603 |
* @return bool True if required version is compatible or empty, false if not. |
|
7604 |
*/ |
|
9 | 7605 |
function is_wp_version_compatible( $required ) { |
7606 |
return empty( $required ) || version_compare( get_bloginfo( 'version' ), $required, '>=' ); |
|
7607 |
} |
|
7608 |
||
7609 |
/** |
|
7610 |
* Checks compatibility with the current PHP version. |
|
7611 |
* |
|
7612 |
* @since 5.2.0 |
|
7613 |
* |
|
7614 |
* @param string $required Minimum required PHP version. |
|
7615 |
* @return bool True if required version is compatible or empty, false if not. |
|
7616 |
*/ |
|
7617 |
function is_php_version_compatible( $required ) { |
|
7618 |
return empty( $required ) || version_compare( phpversion(), $required, '>=' ); |
|
7619 |
} |
|
16 | 7620 |
|
7621 |
/** |
|
7622 |
* Check if two numbers are nearly the same. |
|
7623 |
* |
|
7624 |
* This is similar to using `round()` but the precision is more fine-grained. |
|
7625 |
* |
|
7626 |
* @since 5.3.0 |
|
7627 |
* |
|
7628 |
* @param int|float $expected The expected value. |
|
7629 |
* @param int|float $actual The actual number. |
|
7630 |
* @param int|float $precision The allowed variation. |
|
7631 |
* @return bool Whether the numbers match whithin the specified precision. |
|
7632 |
*/ |
|
7633 |
function wp_fuzzy_number_match( $expected, $actual, $precision = 1 ) { |
|
7634 |
return abs( (float) $expected - (float) $actual ) <= $precision; |
|
7635 |
} |