author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 13 | d255fe9cd479 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Main WordPress API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
||
8 |
require( ABSPATH . WPINC . '/option.php' ); |
|
9 |
||
10 |
/** |
|
5 | 11 |
* Convert given date string into a different format. |
0 | 12 |
* |
13 |
* $format should be either a PHP date format string, e.g. 'U' for a Unix |
|
14 |
* timestamp, or 'G' for a Unix timestamp assuming that $date is GMT. |
|
15 |
* |
|
16 |
* If $translate is true then the given date and format string will |
|
17 |
* be passed to date_i18n() for translation. |
|
18 |
* |
|
19 |
* @since 0.71 |
|
20 |
* |
|
5 | 21 |
* @param string $format Format of the date to return. |
22 |
* @param string $date Date string to convert. |
|
23 |
* @param bool $translate Whether the return date should be translated. Default true. |
|
24 |
* @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty. |
|
0 | 25 |
*/ |
26 |
function mysql2date( $format, $date, $translate = true ) { |
|
9 | 27 |
if ( empty( $date ) ) { |
0 | 28 |
return false; |
9 | 29 |
} |
30 |
||
31 |
if ( 'G' == $format ) { |
|
0 | 32 |
return strtotime( $date . ' +0000' ); |
9 | 33 |
} |
0 | 34 |
|
35 |
$i = strtotime( $date ); |
|
36 |
||
9 | 37 |
if ( 'U' == $format ) { |
0 | 38 |
return $i; |
9 | 39 |
} |
40 |
||
41 |
if ( $translate ) { |
|
0 | 42 |
return date_i18n( $format, $i ); |
9 | 43 |
} else { |
0 | 44 |
return date( $format, $i ); |
9 | 45 |
} |
0 | 46 |
} |
47 |
||
48 |
/** |
|
49 |
* Retrieve the current time based on specified type. |
|
50 |
* |
|
51 |
* The 'mysql' type will return the time in the format for MySQL DATETIME field. |
|
52 |
* The 'timestamp' type will return the current timestamp. |
|
5 | 53 |
* Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). |
0 | 54 |
* |
55 |
* If $gmt is set to either '1' or 'true', then both types will use GMT time. |
|
56 |
* if $gmt is false, the output is adjusted with the GMT offset in the WordPress option. |
|
57 |
* |
|
58 |
* @since 1.0.0 |
|
59 |
* |
|
5 | 60 |
* @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date |
61 |
* format string (e.g. 'Y-m-d'). |
|
62 |
* @param int|bool $gmt Optional. Whether to use GMT timezone. Default false. |
|
63 |
* @return int|string Integer if $type is 'timestamp', string otherwise. |
|
0 | 64 |
*/ |
65 |
function current_time( $type, $gmt = 0 ) { |
|
66 |
switch ( $type ) { |
|
67 |
case 'mysql': |
|
68 |
return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) ); |
|
69 |
case 'timestamp': |
|
70 |
return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); |
|
5 | 71 |
default: |
9 | 72 |
return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); |
73 |
} |
|
74 |
} |
|
75 |
||
76 |
/** |
|
77 |
* Retrieve the date in localized format, based on a sum of Unix timestamp and |
|
78 |
* timezone offset in seconds. |
|
0 | 79 |
* |
80 |
* If the locale specifies the locale month and weekday, then the locale will |
|
81 |
* take over the format for the date. If it isn't, then the date format string |
|
82 |
* will be used instead. |
|
83 |
* |
|
84 |
* @since 0.71 |
|
85 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
* @global WP_Locale $wp_locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* |
9 | 88 |
* @param string $dateformatstring Format to display the date. |
89 |
* @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds. |
|
90 |
* Default false. |
|
91 |
* @param bool $gmt Optional. Whether to use GMT timezone. Only applies if timestamp is |
|
92 |
* not provided. Default false. |
|
5 | 93 |
* |
0 | 94 |
* @return string The date, translated if locale specifies it. |
95 |
*/ |
|
9 | 96 |
function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = false ) { |
0 | 97 |
global $wp_locale; |
9 | 98 |
$i = $timestamp_with_offset; |
0 | 99 |
|
100 |
if ( false === $i ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
$i = current_time( 'timestamp', $gmt ); |
0 | 102 |
} |
103 |
||
5 | 104 |
/* |
105 |
* Store original value for language with untypical grammars. |
|
106 |
* See https://core.trac.wordpress.org/ticket/9396 |
|
107 |
*/ |
|
0 | 108 |
$req_format = $dateformatstring; |
109 |
||
9 | 110 |
$dateformatstring = preg_replace( '/(?<!\\\\)c/', DATE_W3C, $dateformatstring ); |
111 |
$dateformatstring = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $dateformatstring ); |
|
112 |
||
113 |
if ( ( ! empty( $wp_locale->month ) ) && ( ! empty( $wp_locale->weekday ) ) ) { |
|
114 |
$datemonth = $wp_locale->get_month( date( 'm', $i ) ); |
|
115 |
$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth ); |
|
116 |
$dateweekday = $wp_locale->get_weekday( date( 'w', $i ) ); |
|
117 |
$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday ); |
|
118 |
$datemeridiem = $wp_locale->get_meridiem( date( 'a', $i ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
$datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) ); |
9 | 120 |
$dateformatstring = ' ' . $dateformatstring; |
121 |
$dateformatstring = preg_replace( '/([^\\\])D/', "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring ); |
|
122 |
$dateformatstring = preg_replace( '/([^\\\])F/', "\\1" . backslashit( $datemonth ), $dateformatstring ); |
|
123 |
$dateformatstring = preg_replace( '/([^\\\])l/', "\\1" . backslashit( $dateweekday ), $dateformatstring ); |
|
124 |
$dateformatstring = preg_replace( '/([^\\\])M/', "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring ); |
|
125 |
$dateformatstring = preg_replace( '/([^\\\])a/', "\\1" . backslashit( $datemeridiem ), $dateformatstring ); |
|
126 |
$dateformatstring = preg_replace( '/([^\\\])A/', "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring ); |
|
127 |
||
128 |
$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 ); |
|
129 |
} |
|
130 |
$timezone_formats = array( 'P', 'I', 'O', 'T', 'Z', 'e' ); |
|
0 | 131 |
$timezone_formats_re = implode( '|', $timezone_formats ); |
132 |
if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) { |
|
133 |
$timezone_string = get_option( 'timezone_string' ); |
|
9 | 134 |
if ( false === $timestamp_with_offset && $gmt ) { |
135 |
$timezone_string = 'UTC'; |
|
136 |
} |
|
0 | 137 |
if ( $timezone_string ) { |
138 |
$timezone_object = timezone_open( $timezone_string ); |
|
9 | 139 |
$date_object = date_create( null, $timezone_object ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
foreach ( $timezone_formats as $timezone_format ) { |
0 | 141 |
if ( false !== strpos( $dateformatstring, $timezone_format ) ) { |
9 | 142 |
$formatted = date_format( $date_object, $timezone_format ); |
143 |
$dateformatstring = ' ' . $dateformatstring; |
|
0 | 144 |
$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring ); |
9 | 145 |
$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 ); |
146 |
} |
|
147 |
} |
|
148 |
} else { |
|
149 |
$offset = get_option( 'gmt_offset' ); |
|
150 |
foreach ( $timezone_formats as $timezone_format ) { |
|
151 |
if ( 'I' === $timezone_format ) { |
|
152 |
continue; |
|
153 |
} |
|
154 |
||
155 |
if ( false !== strpos( $dateformatstring, $timezone_format ) ) { |
|
156 |
if ( 'Z' === $timezone_format ) { |
|
157 |
$formatted = (string) ( $offset * HOUR_IN_SECONDS ); |
|
158 |
} else { |
|
159 |
$prefix = ''; |
|
160 |
$hours = (int) $offset; |
|
161 |
$separator = ''; |
|
162 |
$minutes = abs( ( $offset - $hours ) * 60 ); |
|
163 |
||
164 |
if ( 'T' === $timezone_format ) { |
|
165 |
$prefix = 'GMT'; |
|
166 |
} elseif ( 'e' === $timezone_format || 'P' === $timezone_format ) { |
|
167 |
$separator = ':'; |
|
168 |
} |
|
169 |
||
170 |
$formatted = sprintf( '%s%+03d%s%02d', $prefix, $hours, $separator, $minutes ); |
|
171 |
} |
|
172 |
||
173 |
$dateformatstring = ' ' . $dateformatstring; |
|
174 |
$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring ); |
|
175 |
$dateformatstring = substr( $dateformatstring, 1 ); |
|
0 | 176 |
} |
177 |
} |
|
178 |
} |
|
179 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
$j = @date( $dateformatstring, $i ); |
5 | 181 |
|
182 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
* Filters the date formatted based on the locale. |
5 | 184 |
* |
185 |
* @since 2.8.0 |
|
186 |
* |
|
187 |
* @param string $j Formatted date string. |
|
188 |
* @param string $req_format Format to display the date. |
|
9 | 189 |
* @param int $i A sum of Unix timestamp and timezone offset in seconds. |
190 |
* @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was |
|
191 |
* not provided. Default false. |
|
5 | 192 |
*/ |
193 |
$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt ); |
|
0 | 194 |
return $j; |
195 |
} |
|
196 |
||
197 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
* Determines if the date should be declined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* 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
|
201 |
* 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
|
202 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
* @global WP_Locale $wp_locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
* @param string $date Formatted date string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* @return string The date, declined if locale specifies it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
function wp_maybe_decline_date( $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
global $wp_locale; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
// i18n functions are not available in SHORTINIT mode |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
if ( ! function_exists( '_x' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
/* translators: If months in your language require a genitive case, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
* 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
|
220 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
if ( 'on' === _x( 'off', 'decline months names: on or off' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
// Match a format like 'j F Y' or 'j. F' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
if ( @preg_match( '#^\d{1,2}\.? [^\d ]+#u', $date ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
$months = $wp_locale->month; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
$months_genitive = $wp_locale->month_genitive; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
foreach ( $months as $key => $month ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
$months[ $key ] = '# ' . $month . '( |$)#u'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
foreach ( $months_genitive as $key => $month ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
$months_genitive[ $key ] = ' ' . $month . '$1'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
$date = preg_replace( $months, $months_genitive, $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
// Used for locale-specific rules |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
$locale = get_locale(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
if ( 'ca' === $locale ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
// " 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
|
244 |
$date = preg_replace( '# de ([ao])#i', " d'\\1", $date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* Convert float number to format based on the locale. |
0 | 252 |
* |
253 |
* @since 2.3.0 |
|
254 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @global WP_Locale $wp_locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
* @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
|
258 |
* @param int $decimals Optional. Precision of the number of decimal places. Default 0. |
0 | 259 |
* @return string Converted number in string format. |
260 |
*/ |
|
261 |
function number_format_i18n( $number, $decimals = 0 ) { |
|
262 |
global $wp_locale; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
if ( isset( $wp_locale ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
$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
|
266 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
$formatted = number_format( $number, absint( $decimals ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
} |
5 | 269 |
|
270 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
* Filters the number formatted based on the locale. |
5 | 272 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
* @since 2.8.0 |
9 | 274 |
* @since 4.9.0 The `$number` and `$decimals` parameters were added. |
5 | 275 |
* |
276 |
* @param string $formatted Converted number in string format. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* @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
|
278 |
* @param int $decimals Precision of the number of decimal places. |
5 | 279 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
return apply_filters( 'number_format_i18n', $formatted, $number, $decimals ); |
0 | 281 |
} |
282 |
||
283 |
/** |
|
284 |
* Convert number of bytes largest unit bytes will fit into. |
|
285 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
* It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts |
0 | 287 |
* number of bytes to human readable number by taking the number of that unit |
288 |
* that the bytes will go into it. Supports TB value. |
|
289 |
* |
|
290 |
* Please note that integers in PHP are limited to 32 bits, unless they are on |
|
291 |
* 64 bit architecture, then they have 64 bit size. If you need to place the |
|
292 |
* larger size then what PHP integer type will hold, then use a string. It will |
|
293 |
* be converted to a double, which should always have 64 bit length. |
|
294 |
* |
|
295 |
* Technically the correct unit names for powers of 1024 are KiB, MiB etc. |
|
296 |
* |
|
297 |
* @since 2.3.0 |
|
298 |
* |
|
5 | 299 |
* @param int|string $bytes Number of bytes. Note max integer size for integers. |
300 |
* @param int $decimals Optional. Precision of number of decimal places. Default 0. |
|
301 |
* @return string|false False on failure. Number string on success. |
|
0 | 302 |
*/ |
303 |
function size_format( $bytes, $decimals = 0 ) { |
|
304 |
$quant = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
'TB' => TB_IN_BYTES, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
'GB' => GB_IN_BYTES, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
'MB' => MB_IN_BYTES, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
'KB' => KB_IN_BYTES, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
'B' => 1, |
0 | 310 |
); |
5 | 311 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
312 |
if ( 0 === $bytes ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
return number_format_i18n( 0, $decimals ) . ' B'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
|
5 | 316 |
foreach ( $quant as $unit => $mag ) { |
317 |
if ( doubleval( $bytes ) >= $mag ) { |
|
0 | 318 |
return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit; |
5 | 319 |
} |
320 |
} |
|
0 | 321 |
|
322 |
return false; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
9 | 326 |
* Convert a duration to human readable format. |
327 |
* |
|
328 |
* @since 5.1.0 |
|
329 |
* |
|
330 |
* @param string $duration Duration will be in string format (HH:ii:ss) OR (ii:ss), |
|
331 |
* with a possible prepended negative sign (-). |
|
332 |
* @return string|false A human readable duration string, false on failure. |
|
333 |
*/ |
|
334 |
function human_readable_duration( $duration = '' ) { |
|
335 |
if ( ( empty( $duration ) || ! is_string( $duration ) ) ) { |
|
336 |
return false; |
|
337 |
} |
|
338 |
||
339 |
$duration = trim( $duration ); |
|
340 |
||
341 |
// Remove prepended negative sign. |
|
342 |
if ( '-' === substr( $duration, 0, 1 ) ) { |
|
343 |
$duration = substr( $duration, 1 ); |
|
344 |
} |
|
345 |
||
346 |
// Extract duration parts. |
|
347 |
$duration_parts = array_reverse( explode( ':', $duration ) ); |
|
348 |
$duration_count = count( $duration_parts ); |
|
349 |
||
350 |
$hour = null; |
|
351 |
$minute = null; |
|
352 |
$second = null; |
|
353 |
||
354 |
if ( 3 === $duration_count ) { |
|
355 |
// Validate HH:ii:ss duration format. |
|
356 |
if ( ! ( (bool) preg_match( '/^([0-9]+):([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) { |
|
357 |
return false; |
|
358 |
} |
|
359 |
// Three parts: hours, minutes & seconds. |
|
360 |
list( $second, $minute, $hour ) = $duration_parts; |
|
361 |
} elseif ( 2 === $duration_count ) { |
|
362 |
// Validate ii:ss duration format. |
|
363 |
if ( ! ( (bool) preg_match( '/^([0-5]?[0-9]):([0-5]?[0-9])$/', $duration ) ) ) { |
|
364 |
return false; |
|
365 |
} |
|
366 |
// Two parts: minutes & seconds. |
|
367 |
list( $second, $minute ) = $duration_parts; |
|
368 |
} else { |
|
369 |
return false; |
|
370 |
} |
|
371 |
||
372 |
$human_readable_duration = array(); |
|
373 |
||
374 |
// Add the hour part to the string. |
|
375 |
if ( is_numeric( $hour ) ) { |
|
376 |
/* translators: Time duration in hour or hours. */ |
|
377 |
$human_readable_duration[] = sprintf( _n( '%s hour', '%s hours', $hour ), (int) $hour ); |
|
378 |
} |
|
379 |
||
380 |
// Add the minute part to the string. |
|
381 |
if ( is_numeric( $minute ) ) { |
|
382 |
/* translators: Time duration in minute or minutes. */ |
|
383 |
$human_readable_duration[] = sprintf( _n( '%s minute', '%s minutes', $minute ), (int) $minute ); |
|
384 |
} |
|
385 |
||
386 |
// Add the second part to the string. |
|
387 |
if ( is_numeric( $second ) ) { |
|
388 |
/* translators: Time duration in second or seconds. */ |
|
389 |
$human_readable_duration[] = sprintf( _n( '%s second', '%s seconds', $second ), (int) $second ); |
|
390 |
} |
|
391 |
||
392 |
return implode( ', ', $human_readable_duration ); |
|
393 |
} |
|
394 |
||
395 |
/** |
|
5 | 396 |
* Get the week start and end from the datetime or date string from MySQL. |
0 | 397 |
* |
398 |
* @since 0.71 |
|
399 |
* |
|
5 | 400 |
* @param string $mysqlstring Date or datetime field type from MySQL. |
401 |
* @param int|string $start_of_week Optional. Start of the week as an integer. Default empty string. |
|
0 | 402 |
* @return array Keys are 'start' and 'end'. |
403 |
*/ |
|
404 |
function get_weekstartend( $mysqlstring, $start_of_week = '' ) { |
|
5 | 405 |
// MySQL string year. |
406 |
$my = substr( $mysqlstring, 0, 4 ); |
|
407 |
||
408 |
// MySQL string month. |
|
409 |
$mm = substr( $mysqlstring, 8, 2 ); |
|
410 |
||
411 |
// MySQL string day. |
|
412 |
$md = substr( $mysqlstring, 5, 2 ); |
|
413 |
||
414 |
// The timestamp for MySQL string day. |
|
415 |
$day = mktime( 0, 0, 0, $md, $mm, $my ); |
|
416 |
||
417 |
// The day of the week from the timestamp. |
|
418 |
$weekday = date( 'w', $day ); |
|
419 |
||
9 | 420 |
if ( ! is_numeric( $start_of_week ) ) { |
0 | 421 |
$start_of_week = get_option( 'start_of_week' ); |
9 | 422 |
} |
423 |
||
424 |
if ( $weekday < $start_of_week ) { |
|
0 | 425 |
$weekday += 7; |
9 | 426 |
} |
0 | 427 |
|
5 | 428 |
// The most recent week start day on or before $day. |
429 |
$start = $day - DAY_IN_SECONDS * ( $weekday - $start_of_week ); |
|
430 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
// $start + 1 week - 1 second. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
$end = $start + WEEK_IN_SECONDS - 1; |
0 | 433 |
return compact( 'start', 'end' ); |
434 |
} |
|
435 |
||
436 |
/** |
|
437 |
* Unserialize value only if it was serialized. |
|
438 |
* |
|
439 |
* @since 2.0.0 |
|
440 |
* |
|
441 |
* @param string $original Maybe unserialized original, if is needed. |
|
442 |
* @return mixed Unserialized data can be any type. |
|
443 |
*/ |
|
444 |
function maybe_unserialize( $original ) { |
|
9 | 445 |
if ( is_serialized( $original ) ) { // don't attempt to unserialize data that wasn't serialized going in |
0 | 446 |
return @unserialize( $original ); |
9 | 447 |
} |
0 | 448 |
return $original; |
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* Check value to find if it was serialized. |
|
453 |
* |
|
454 |
* If $data is not an string, then returned value will always be false. |
|
455 |
* Serialized data is always a string. |
|
456 |
* |
|
457 |
* @since 2.0.5 |
|
458 |
* |
|
5 | 459 |
* @param string $data Value to check to see if was serialized. |
460 |
* @param bool $strict Optional. Whether to be strict about the end of the string. Default true. |
|
0 | 461 |
* @return bool False if not serialized and true if it was. |
462 |
*/ |
|
463 |
function is_serialized( $data, $strict = true ) { |
|
5 | 464 |
// if it isn't a string, it isn't serialized. |
465 |
if ( ! is_string( $data ) ) { |
|
0 | 466 |
return false; |
5 | 467 |
} |
0 | 468 |
$data = trim( $data ); |
9 | 469 |
if ( 'N;' == $data ) { |
0 | 470 |
return true; |
5 | 471 |
} |
472 |
if ( strlen( $data ) < 4 ) { |
|
0 | 473 |
return false; |
5 | 474 |
} |
475 |
if ( ':' !== $data[1] ) { |
|
0 | 476 |
return false; |
5 | 477 |
} |
0 | 478 |
if ( $strict ) { |
5 | 479 |
$lastc = substr( $data, -1 ); |
480 |
if ( ';' !== $lastc && '}' !== $lastc ) { |
|
0 | 481 |
return false; |
5 | 482 |
} |
0 | 483 |
} else { |
484 |
$semicolon = strpos( $data, ';' ); |
|
485 |
$brace = strpos( $data, '}' ); |
|
486 |
// Either ; or } must exist. |
|
9 | 487 |
if ( false === $semicolon && false === $brace ) { |
0 | 488 |
return false; |
9 | 489 |
} |
0 | 490 |
// But neither must be in the first X characters. |
9 | 491 |
if ( false !== $semicolon && $semicolon < 3 ) { |
0 | 492 |
return false; |
9 | 493 |
} |
494 |
if ( false !== $brace && $brace < 4 ) { |
|
0 | 495 |
return false; |
9 | 496 |
} |
0 | 497 |
} |
498 |
$token = $data[0]; |
|
499 |
switch ( $token ) { |
|
9 | 500 |
case 's': |
0 | 501 |
if ( $strict ) { |
5 | 502 |
if ( '"' !== substr( $data, -2, 1 ) ) { |
0 | 503 |
return false; |
5 | 504 |
} |
0 | 505 |
} elseif ( false === strpos( $data, '"' ) ) { |
506 |
return false; |
|
507 |
} |
|
508 |
// or else fall through |
|
9 | 509 |
case 'a': |
510 |
case 'O': |
|
0 | 511 |
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); |
9 | 512 |
case 'b': |
513 |
case 'i': |
|
514 |
case 'd': |
|
0 | 515 |
$end = $strict ? '$' : ''; |
516 |
return (bool) preg_match( "/^{$token}:[0-9.E-]+;$end/", $data ); |
|
517 |
} |
|
518 |
return false; |
|
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* Check whether serialized data is of string type. |
|
523 |
* |
|
524 |
* @since 2.0.5 |
|
525 |
* |
|
5 | 526 |
* @param string $data Serialized data. |
0 | 527 |
* @return bool False if not a serialized string, true if it is. |
528 |
*/ |
|
529 |
function is_serialized_string( $data ) { |
|
5 | 530 |
// if it isn't a string, it isn't a serialized string. |
531 |
if ( ! is_string( $data ) ) { |
|
0 | 532 |
return false; |
5 | 533 |
} |
0 | 534 |
$data = trim( $data ); |
5 | 535 |
if ( strlen( $data ) < 4 ) { |
0 | 536 |
return false; |
5 | 537 |
} elseif ( ':' !== $data[1] ) { |
0 | 538 |
return false; |
5 | 539 |
} elseif ( ';' !== substr( $data, -1 ) ) { |
0 | 540 |
return false; |
5 | 541 |
} elseif ( $data[0] !== 's' ) { |
0 | 542 |
return false; |
5 | 543 |
} elseif ( '"' !== substr( $data, -2, 1 ) ) { |
0 | 544 |
return false; |
5 | 545 |
} else { |
0 | 546 |
return true; |
5 | 547 |
} |
0 | 548 |
} |
549 |
||
550 |
/** |
|
551 |
* Serialize data, if needed. |
|
552 |
* |
|
553 |
* @since 2.0.5 |
|
554 |
* |
|
5 | 555 |
* @param string|array|object $data Data that might be serialized. |
0 | 556 |
* @return mixed A scalar data |
557 |
*/ |
|
558 |
function maybe_serialize( $data ) { |
|
9 | 559 |
if ( is_array( $data ) || is_object( $data ) ) { |
0 | 560 |
return serialize( $data ); |
9 | 561 |
} |
0 | 562 |
|
563 |
// Double serialization is required for backward compatibility. |
|
5 | 564 |
// See https://core.trac.wordpress.org/ticket/12930 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
// Also the world will end. See WP 3.6.1. |
9 | 566 |
if ( is_serialized( $data, false ) ) { |
0 | 567 |
return serialize( $data ); |
9 | 568 |
} |
0 | 569 |
|
570 |
return $data; |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Retrieve post title from XMLRPC XML. |
|
575 |
* |
|
576 |
* If the title element is not part of the XML, then the default post title from |
|
577 |
* the $post_default_title will be used instead. |
|
578 |
* |
|
579 |
* @since 0.71 |
|
580 |
* |
|
5 | 581 |
* @global string $post_default_title Default XML-RPC post title. |
0 | 582 |
* |
583 |
* @param string $content XMLRPC XML Request content |
|
584 |
* @return string Post title |
|
585 |
*/ |
|
586 |
function xmlrpc_getposttitle( $content ) { |
|
587 |
global $post_default_title; |
|
588 |
if ( preg_match( '/<title>(.+?)<\/title>/is', $content, $matchtitle ) ) { |
|
589 |
$post_title = $matchtitle[1]; |
|
590 |
} else { |
|
591 |
$post_title = $post_default_title; |
|
592 |
} |
|
593 |
return $post_title; |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Retrieve the post category or categories from XMLRPC XML. |
|
598 |
* |
|
599 |
* If the category element is not found, then the default post category will be |
|
600 |
* used. The return type then would be what $post_default_category. If the |
|
601 |
* category is found, then it will always be an array. |
|
602 |
* |
|
603 |
* @since 0.71 |
|
604 |
* |
|
5 | 605 |
* @global string $post_default_category Default XML-RPC post category. |
0 | 606 |
* |
607 |
* @param string $content XMLRPC XML Request content |
|
608 |
* @return string|array List of categories or category name. |
|
609 |
*/ |
|
610 |
function xmlrpc_getpostcategory( $content ) { |
|
611 |
global $post_default_category; |
|
612 |
if ( preg_match( '/<category>(.+?)<\/category>/is', $content, $matchcat ) ) { |
|
613 |
$post_category = trim( $matchcat[1], ',' ); |
|
614 |
$post_category = explode( ',', $post_category ); |
|
615 |
} else { |
|
616 |
$post_category = $post_default_category; |
|
617 |
} |
|
618 |
return $post_category; |
|
619 |
} |
|
620 |
||
621 |
/** |
|
622 |
* XMLRPC XML content without title and category elements. |
|
623 |
* |
|
624 |
* @since 0.71 |
|
625 |
* |
|
5 | 626 |
* @param string $content XML-RPC XML Request content. |
0 | 627 |
* @return string XMLRPC XML Request content without title and category elements. |
628 |
*/ |
|
629 |
function xmlrpc_removepostdata( $content ) { |
|
630 |
$content = preg_replace( '/<title>(.+?)<\/title>/si', '', $content ); |
|
631 |
$content = preg_replace( '/<category>(.+?)<\/category>/si', '', $content ); |
|
632 |
$content = trim( $content ); |
|
633 |
return $content; |
|
634 |
} |
|
635 |
||
636 |
/** |
|
5 | 637 |
* Use RegEx to extract URLs from arbitrary content. |
0 | 638 |
* |
639 |
* @since 3.7.0 |
|
640 |
* |
|
5 | 641 |
* @param string $content Content to extract URLs from. |
642 |
* @return array URLs found in passed string. |
|
0 | 643 |
*/ |
644 |
function wp_extract_urls( $content ) { |
|
645 |
preg_match_all( |
|
5 | 646 |
"#([\"']?)(" |
9 | 647 |
. '(?:([\w-]+:)?//?)' |
648 |
. '[^\s()<>]+' |
|
649 |
. '[.]' |
|
650 |
. '(?:' |
|
651 |
. '\([\w\d]+\)|' |
|
652 |
. '(?:' |
|
5 | 653 |
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|" |
9 | 654 |
. '(?:[:]\d+)?/?' |
655 |
. ')+' |
|
656 |
. ')' |
|
5 | 657 |
. ")\\1#", |
0 | 658 |
$content, |
659 |
$post_links |
|
660 |
); |
|
661 |
||
5 | 662 |
$post_links = array_unique( array_map( 'html_entity_decode', $post_links[2] ) ); |
0 | 663 |
|
664 |
return array_values( $post_links ); |
|
665 |
} |
|
666 |
||
667 |
/** |
|
668 |
* Check content for video and audio links to add as enclosures. |
|
669 |
* |
|
670 |
* Will not add enclosures that have already been added and will |
|
671 |
* remove enclosures that are no longer in the post. This is called as |
|
672 |
* pingbacks and trackbacks. |
|
673 |
* |
|
674 |
* @since 1.5.0 |
|
675 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
* @global wpdb $wpdb WordPress database abstraction object. |
5 | 677 |
* |
678 |
* @param string $content Post Content. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
* @param int $post_ID Post ID. |
0 | 680 |
*/ |
681 |
function do_enclose( $content, $post_ID ) { |
|
682 |
global $wpdb; |
|
683 |
||
684 |
//TODO: Tidy this ghetto code up and make the debug code optional |
|
685 |
include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
|
686 |
||
687 |
$post_links = array(); |
|
688 |
||
689 |
$pung = get_enclosed( $post_ID ); |
|
690 |
||
691 |
$post_links_temp = wp_extract_urls( $content ); |
|
692 |
||
693 |
foreach ( $pung as $link_test ) { |
|
694 |
if ( ! in_array( $link_test, $post_links_temp ) ) { // link no longer in post |
|
9 | 695 |
$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 ) . '%' ) ); |
696 |
foreach ( $mids as $mid ) { |
|
0 | 697 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 698 |
} |
0 | 699 |
} |
700 |
} |
|
701 |
||
702 |
foreach ( (array) $post_links_temp as $link_test ) { |
|
9 | 703 |
if ( ! in_array( $link_test, $pung ) ) { // If we haven't pung it already |
0 | 704 |
$test = @parse_url( $link_test ); |
9 | 705 |
if ( false === $test ) { |
0 | 706 |
continue; |
9 | 707 |
} |
708 |
if ( isset( $test['query'] ) ) { |
|
0 | 709 |
$post_links[] = $link_test; |
9 | 710 |
} elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) { |
0 | 711 |
$post_links[] = $link_test; |
9 | 712 |
} |
0 | 713 |
} |
714 |
} |
|
715 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
716 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
717 |
* 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
|
718 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
* 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
|
720 |
* to postmeta before checking the database for existing enclosures. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
722 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
723 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
724 |
* @param array $post_links An array of enclosure links. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
* @param int $post_ID Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
726 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
727 |
$post_links = apply_filters( 'enclosure_links', $post_links, $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
728 |
|
0 | 729 |
foreach ( (array) $post_links as $url ) { |
9 | 730 |
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 ) . '%' ) ) ) { |
731 |
||
732 |
if ( $headers = wp_get_http_headers( $url ) ) { |
|
733 |
$len = isset( $headers['content-length'] ) ? (int) $headers['content-length'] : 0; |
|
734 |
$type = isset( $headers['content-type'] ) ? $headers['content-type'] : ''; |
|
0 | 735 |
$allowed_types = array( 'video', 'audio' ); |
736 |
||
737 |
// Check to see if we can figure out the mime type from |
|
738 |
// the extension |
|
739 |
$url_parts = @parse_url( $url ); |
|
740 |
if ( false !== $url_parts ) { |
|
741 |
$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION ); |
|
9 | 742 |
if ( ! empty( $extension ) ) { |
0 | 743 |
foreach ( wp_get_mime_types() as $exts => $mime ) { |
744 |
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { |
|
745 |
$type = $mime; |
|
746 |
break; |
|
747 |
} |
|
748 |
} |
|
749 |
} |
|
750 |
} |
|
751 |
||
9 | 752 |
if ( in_array( substr( $type, 0, strpos( $type, '/' ) ), $allowed_types ) ) { |
0 | 753 |
add_post_meta( $post_ID, 'enclosure', "$url\n$len\n$mime\n" ); |
754 |
} |
|
755 |
} |
|
756 |
} |
|
757 |
} |
|
758 |
} |
|
759 |
||
760 |
/** |
|
761 |
* Retrieve HTTP Headers from URL. |
|
762 |
* |
|
763 |
* @since 1.5.1 |
|
764 |
* |
|
5 | 765 |
* @param string $url URL to retrieve HTTP headers from. |
766 |
* @param bool $deprecated Not Used. |
|
0 | 767 |
* @return bool|string False on failure, headers on success. |
768 |
*/ |
|
769 |
function wp_get_http_headers( $url, $deprecated = false ) { |
|
9 | 770 |
if ( ! empty( $deprecated ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
771 |
_deprecated_argument( __FUNCTION__, '2.7.0' ); |
9 | 772 |
} |
0 | 773 |
|
774 |
$response = wp_safe_remote_head( $url ); |
|
775 |
||
9 | 776 |
if ( is_wp_error( $response ) ) { |
0 | 777 |
return false; |
9 | 778 |
} |
0 | 779 |
|
780 |
return wp_remote_retrieve_headers( $response ); |
|
781 |
} |
|
782 |
||
783 |
/** |
|
9 | 784 |
* Determines whether the publish date of the current post in the loop is different |
785 |
* from the publish date of the previous post in the loop. |
|
786 |
* |
|
787 |
* For more information on this and similar theme functions, check out |
|
788 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
789 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 790 |
* |
791 |
* @since 0.71 |
|
5 | 792 |
* |
793 |
* @global string $currentday The day of the current post in the loop. |
|
794 |
* @global string $previousday The day of the previous post in the loop. |
|
0 | 795 |
* |
796 |
* @return int 1 when new day, 0 if not a new day. |
|
797 |
*/ |
|
798 |
function is_new_day() { |
|
799 |
global $currentday, $previousday; |
|
9 | 800 |
if ( $currentday != $previousday ) { |
0 | 801 |
return 1; |
9 | 802 |
} else { |
0 | 803 |
return 0; |
9 | 804 |
} |
0 | 805 |
} |
806 |
||
807 |
/** |
|
808 |
* Build URL query based on an associative and, or indexed array. |
|
809 |
* |
|
810 |
* This is a convenient function for easily building url queries. It sets the |
|
811 |
* separator to '&' and uses _http_build_query() function. |
|
812 |
* |
|
5 | 813 |
* @since 2.3.0 |
814 |
* |
|
0 | 815 |
* @see _http_build_query() Used to build the query |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
816 |
* @link https://secure.php.net/manual/en/function.http-build-query.php for more on what |
9 | 817 |
* http_build_query() does. |
0 | 818 |
* |
819 |
* @param array $data URL-encode key/value pairs. |
|
5 | 820 |
* @return string URL-encoded string. |
0 | 821 |
*/ |
822 |
function build_query( $data ) { |
|
823 |
return _http_build_query( $data, null, '&', '', false ); |
|
824 |
} |
|
825 |
||
5 | 826 |
/** |
827 |
* From php.net (modified by Mark Jaquith to behave like the native PHP5 function). |
|
828 |
* |
|
829 |
* @since 3.2.0 |
|
830 |
* @access private |
|
831 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
* @see https://secure.php.net/manual/en/function.http-build-query.php |
5 | 833 |
* |
834 |
* @param array|object $data An array or object of data. Converted to array. |
|
835 |
* @param string $prefix Optional. Numeric index. If set, start parameter numbering with it. |
|
836 |
* Default null. |
|
837 |
* @param string $sep Optional. Argument separator; defaults to 'arg_separator.output'. |
|
838 |
* Default null. |
|
839 |
* @param string $key Optional. Used to prefix key name. Default empty. |
|
840 |
* @param bool $urlencode Optional. Whether to use urlencode() in the result. Default true. |
|
841 |
* |
|
842 |
* @return string The query string. |
|
843 |
*/ |
|
844 |
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) { |
|
0 | 845 |
$ret = array(); |
846 |
||
847 |
foreach ( (array) $data as $k => $v ) { |
|
9 | 848 |
if ( $urlencode ) { |
849 |
$k = urlencode( $k ); |
|
850 |
} |
|
851 |
if ( is_int( $k ) && $prefix != null ) { |
|
852 |
$k = $prefix . $k; |
|
853 |
} |
|
854 |
if ( ! empty( $key ) ) { |
|
0 | 855 |
$k = $key . '%5B' . $k . '%5D'; |
9 | 856 |
} |
857 |
if ( $v === null ) { |
|
0 | 858 |
continue; |
9 | 859 |
} elseif ( $v === false ) { |
0 | 860 |
$v = '0'; |
9 | 861 |
} |
862 |
||
863 |
if ( is_array( $v ) || is_object( $v ) ) { |
|
864 |
array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) ); |
|
865 |
} elseif ( $urlencode ) { |
|
866 |
array_push( $ret, $k . '=' . urlencode( $v ) ); |
|
867 |
} else { |
|
868 |
array_push( $ret, $k . '=' . $v ); |
|
869 |
} |
|
870 |
} |
|
871 |
||
872 |
if ( null === $sep ) { |
|
873 |
$sep = ini_get( 'arg_separator.output' ); |
|
874 |
} |
|
875 |
||
876 |
return implode( $sep, $ret ); |
|
0 | 877 |
} |
878 |
||
879 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
880 |
* Retrieves a modified URL query string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
881 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
882 |
* 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
|
883 |
* 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
|
884 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
885 |
* Using a single key and value: |
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 |
* add_query_arg( 'key', 'value', 'http://example.com' ); |
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 |
* Using an associative array: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
* add_query_arg( array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
892 |
* 'key1' => 'value1', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
893 |
* 'key2' => 'value2', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
894 |
* ), 'http://example.com' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
895 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
896 |
* 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
|
897 |
* (the value of `$_SERVER['REQUEST_URI']`). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
899 |
* 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
|
900 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
* 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
|
902 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
* 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
|
904 |
* 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
|
905 |
* (XSS) attacks. |
0 | 906 |
* |
907 |
* @since 1.5.0 |
|
908 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
* @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
|
910 |
* @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
|
911 |
* @param string $url Optional. A URL to act upon. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
* @return string New URL query string (unescaped). |
0 | 913 |
*/ |
914 |
function add_query_arg() { |
|
915 |
$args = func_get_args(); |
|
916 |
if ( is_array( $args[0] ) ) { |
|
9 | 917 |
if ( count( $args ) < 2 || false === $args[1] ) { |
0 | 918 |
$uri = $_SERVER['REQUEST_URI']; |
9 | 919 |
} else { |
0 | 920 |
$uri = $args[1]; |
9 | 921 |
} |
0 | 922 |
} else { |
9 | 923 |
if ( count( $args ) < 3 || false === $args[2] ) { |
0 | 924 |
$uri = $_SERVER['REQUEST_URI']; |
9 | 925 |
} else { |
0 | 926 |
$uri = $args[2]; |
9 | 927 |
} |
928 |
} |
|
929 |
||
930 |
if ( $frag = strstr( $uri, '#' ) ) { |
|
0 | 931 |
$uri = substr( $uri, 0, -strlen( $frag ) ); |
9 | 932 |
} else { |
0 | 933 |
$frag = ''; |
9 | 934 |
} |
0 | 935 |
|
936 |
if ( 0 === stripos( $uri, 'http://' ) ) { |
|
937 |
$protocol = 'http://'; |
|
9 | 938 |
$uri = substr( $uri, 7 ); |
0 | 939 |
} elseif ( 0 === stripos( $uri, 'https://' ) ) { |
940 |
$protocol = 'https://'; |
|
9 | 941 |
$uri = substr( $uri, 8 ); |
0 | 942 |
} else { |
943 |
$protocol = ''; |
|
944 |
} |
|
945 |
||
946 |
if ( strpos( $uri, '?' ) !== false ) { |
|
947 |
list( $base, $query ) = explode( '?', $uri, 2 ); |
|
9 | 948 |
$base .= '?'; |
0 | 949 |
} elseif ( $protocol || strpos( $uri, '=' ) === false ) { |
9 | 950 |
$base = $uri . '?'; |
0 | 951 |
$query = ''; |
952 |
} else { |
|
9 | 953 |
$base = ''; |
0 | 954 |
$query = $uri; |
955 |
} |
|
956 |
||
957 |
wp_parse_str( $query, $qs ); |
|
958 |
$qs = urlencode_deep( $qs ); // this re-URL-encodes things that were already in the query string |
|
959 |
if ( is_array( $args[0] ) ) { |
|
5 | 960 |
foreach ( $args[0] as $k => $v ) { |
961 |
$qs[ $k ] = $v; |
|
962 |
} |
|
0 | 963 |
} else { |
964 |
$qs[ $args[0] ] = $args[1]; |
|
965 |
} |
|
966 |
||
967 |
foreach ( $qs as $k => $v ) { |
|
9 | 968 |
if ( $v === false ) { |
969 |
unset( $qs[ $k ] ); |
|
970 |
} |
|
0 | 971 |
} |
972 |
||
973 |
$ret = build_query( $qs ); |
|
974 |
$ret = trim( $ret, '?' ); |
|
975 |
$ret = preg_replace( '#=(&|$)#', '$1', $ret ); |
|
976 |
$ret = $protocol . $base . $ret . $frag; |
|
977 |
$ret = rtrim( $ret, '?' ); |
|
978 |
return $ret; |
|
979 |
} |
|
980 |
||
981 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
* Removes an item or items from a query string. |
0 | 983 |
* |
984 |
* @since 1.5.0 |
|
985 |
* |
|
5 | 986 |
* @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
|
987 |
* @param bool|string $query Optional. When false uses the current URL. Default false. |
0 | 988 |
* @return string New URL query string. |
989 |
*/ |
|
5 | 990 |
function remove_query_arg( $key, $query = false ) { |
0 | 991 |
if ( is_array( $key ) ) { // removing multiple keys |
9 | 992 |
foreach ( $key as $k ) { |
0 | 993 |
$query = add_query_arg( $k, false, $query ); |
9 | 994 |
} |
0 | 995 |
return $query; |
996 |
} |
|
997 |
return add_query_arg( $key, false, $query ); |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1001 |
* 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
|
1002 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
* @return array An array of parameters to remove from the URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1006 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1007 |
function wp_removable_query_args() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1008 |
$removable_query_args = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1009 |
'activate', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
'activated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1011 |
'approved', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1012 |
'deactivate', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1013 |
'deleted', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1014 |
'disabled', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
'enabled', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
'error', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1017 |
'hotkeys_highlight_first', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1018 |
'hotkeys_highlight_last', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
'locked', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1020 |
'message', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1021 |
'same', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1022 |
'saved', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1023 |
'settings-updated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1024 |
'skipped', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1025 |
'spammed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1026 |
'trashed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1027 |
'unspammed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1028 |
'untrashed', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1029 |
'update', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1030 |
'updated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
'wp-post-new-reload', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1032 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1033 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1034 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1035 |
* Filters the list of query variables to remove. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1036 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1037 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1038 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1039 |
* @param array $removable_query_args An array of query variables to remove from a URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1040 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1041 |
return apply_filters( 'removable_query_args', $removable_query_args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1042 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1043 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1044 |
/** |
0 | 1045 |
* Walks the array while sanitizing the contents. |
1046 |
* |
|
1047 |
* @since 0.71 |
|
1048 |
* |
|
1049 |
* @param array $array Array to walk while sanitizing contents. |
|
1050 |
* @return array Sanitized $array. |
|
1051 |
*/ |
|
1052 |
function add_magic_quotes( $array ) { |
|
1053 |
foreach ( (array) $array as $k => $v ) { |
|
1054 |
if ( is_array( $v ) ) { |
|
9 | 1055 |
$array[ $k ] = add_magic_quotes( $v ); |
0 | 1056 |
} else { |
9 | 1057 |
$array[ $k ] = addslashes( $v ); |
0 | 1058 |
} |
1059 |
} |
|
1060 |
return $array; |
|
1061 |
} |
|
1062 |
||
1063 |
/** |
|
1064 |
* HTTP request for URI to retrieve content. |
|
1065 |
* |
|
1066 |
* @since 1.5.1 |
|
5 | 1067 |
* |
1068 |
* @see wp_safe_remote_get() |
|
0 | 1069 |
* |
1070 |
* @param string $uri URI/URL of web page to retrieve. |
|
5 | 1071 |
* @return false|string HTTP content. False on failure. |
0 | 1072 |
*/ |
1073 |
function wp_remote_fopen( $uri ) { |
|
1074 |
$parsed_url = @parse_url( $uri ); |
|
1075 |
||
9 | 1076 |
if ( ! $parsed_url || ! is_array( $parsed_url ) ) { |
0 | 1077 |
return false; |
9 | 1078 |
} |
1079 |
||
1080 |
$options = array(); |
|
0 | 1081 |
$options['timeout'] = 10; |
1082 |
||
1083 |
$response = wp_safe_remote_get( $uri, $options ); |
|
1084 |
||
9 | 1085 |
if ( is_wp_error( $response ) ) { |
0 | 1086 |
return false; |
9 | 1087 |
} |
0 | 1088 |
|
1089 |
return wp_remote_retrieve_body( $response ); |
|
1090 |
} |
|
1091 |
||
1092 |
/** |
|
1093 |
* Set up the WordPress query. |
|
1094 |
* |
|
1095 |
* @since 2.0.0 |
|
1096 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
* @global WP $wp_locale |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
* @global WP_Query $wp_query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
* @global WP_Query $wp_the_query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
* |
5 | 1101 |
* @param string|array $query_vars Default WP_Query arguments. |
0 | 1102 |
*/ |
1103 |
function wp( $query_vars = '' ) { |
|
1104 |
global $wp, $wp_query, $wp_the_query; |
|
1105 |
$wp->main( $query_vars ); |
|
1106 |
||
9 | 1107 |
if ( ! isset( $wp_the_query ) ) { |
0 | 1108 |
$wp_the_query = $wp_query; |
9 | 1109 |
} |
0 | 1110 |
} |
1111 |
||
1112 |
/** |
|
1113 |
* Retrieve the description for the HTTP status. |
|
1114 |
* |
|
1115 |
* @since 2.3.0 |
|
9 | 1116 |
* @since 3.9.0 Added status codes 418, 428, 429, 431, and 511. |
1117 |
* @since 4.5.0 Added status codes 308, 421, and 451. |
|
1118 |
* @since 5.1.0 Added status code 103. |
|
0 | 1119 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
* @global array $wp_header_to_desc |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
* |
0 | 1122 |
* @param int $code HTTP status code. |
1123 |
* @return string Empty string if not found, or description if found. |
|
1124 |
*/ |
|
1125 |
function get_status_header_desc( $code ) { |
|
1126 |
global $wp_header_to_desc; |
|
1127 |
||
1128 |
$code = absint( $code ); |
|
1129 |
||
9 | 1130 |
if ( ! isset( $wp_header_to_desc ) ) { |
0 | 1131 |
$wp_header_to_desc = array( |
1132 |
100 => 'Continue', |
|
1133 |
101 => 'Switching Protocols', |
|
1134 |
102 => 'Processing', |
|
9 | 1135 |
103 => 'Early Hints', |
0 | 1136 |
|
1137 |
200 => 'OK', |
|
1138 |
201 => 'Created', |
|
1139 |
202 => 'Accepted', |
|
1140 |
203 => 'Non-Authoritative Information', |
|
1141 |
204 => 'No Content', |
|
1142 |
205 => 'Reset Content', |
|
1143 |
206 => 'Partial Content', |
|
1144 |
207 => 'Multi-Status', |
|
1145 |
226 => 'IM Used', |
|
1146 |
||
1147 |
300 => 'Multiple Choices', |
|
1148 |
301 => 'Moved Permanently', |
|
1149 |
302 => 'Found', |
|
1150 |
303 => 'See Other', |
|
1151 |
304 => 'Not Modified', |
|
1152 |
305 => 'Use Proxy', |
|
1153 |
306 => 'Reserved', |
|
1154 |
307 => 'Temporary Redirect', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
308 => 'Permanent Redirect', |
0 | 1156 |
|
1157 |
400 => 'Bad Request', |
|
1158 |
401 => 'Unauthorized', |
|
1159 |
402 => 'Payment Required', |
|
1160 |
403 => 'Forbidden', |
|
1161 |
404 => 'Not Found', |
|
1162 |
405 => 'Method Not Allowed', |
|
1163 |
406 => 'Not Acceptable', |
|
1164 |
407 => 'Proxy Authentication Required', |
|
1165 |
408 => 'Request Timeout', |
|
1166 |
409 => 'Conflict', |
|
1167 |
410 => 'Gone', |
|
1168 |
411 => 'Length Required', |
|
1169 |
412 => 'Precondition Failed', |
|
1170 |
413 => 'Request Entity Too Large', |
|
1171 |
414 => 'Request-URI Too Long', |
|
1172 |
415 => 'Unsupported Media Type', |
|
1173 |
416 => 'Requested Range Not Satisfiable', |
|
1174 |
417 => 'Expectation Failed', |
|
5 | 1175 |
418 => 'I\'m a teapot', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
421 => 'Misdirected Request', |
0 | 1177 |
422 => 'Unprocessable Entity', |
1178 |
423 => 'Locked', |
|
1179 |
424 => 'Failed Dependency', |
|
1180 |
426 => 'Upgrade Required', |
|
5 | 1181 |
428 => 'Precondition Required', |
1182 |
429 => 'Too Many Requests', |
|
1183 |
431 => 'Request Header Fields Too Large', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
451 => 'Unavailable For Legal Reasons', |
0 | 1185 |
|
1186 |
500 => 'Internal Server Error', |
|
1187 |
501 => 'Not Implemented', |
|
1188 |
502 => 'Bad Gateway', |
|
1189 |
503 => 'Service Unavailable', |
|
1190 |
504 => 'Gateway Timeout', |
|
1191 |
505 => 'HTTP Version Not Supported', |
|
1192 |
506 => 'Variant Also Negotiates', |
|
1193 |
507 => 'Insufficient Storage', |
|
5 | 1194 |
510 => 'Not Extended', |
1195 |
511 => 'Network Authentication Required', |
|
0 | 1196 |
); |
1197 |
} |
|
1198 |
||
9 | 1199 |
if ( isset( $wp_header_to_desc[ $code ] ) ) { |
1200 |
return $wp_header_to_desc[ $code ]; |
|
1201 |
} else { |
|
0 | 1202 |
return ''; |
9 | 1203 |
} |
0 | 1204 |
} |
1205 |
||
1206 |
/** |
|
1207 |
* Set HTTP status header. |
|
1208 |
* |
|
1209 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
* @since 4.4.0 Added the `$description` parameter. |
5 | 1211 |
* |
1212 |
* @see get_status_header_desc() |
|
1213 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
* @param int $code HTTP status code. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
* @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
|
1216 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
function status_header( $code, $description = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
if ( ! $description ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
$description = get_status_header_desc( $code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
if ( empty( $description ) ) { |
5 | 1223 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
|
9 | 1226 |
$protocol = wp_get_server_protocol(); |
5 | 1227 |
$status_header = "$protocol $code $description"; |
9 | 1228 |
if ( function_exists( 'apply_filters' ) ) { |
5 | 1229 |
|
1230 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* Filters an HTTP status header. |
5 | 1232 |
* |
1233 |
* @since 2.2.0 |
|
1234 |
* |
|
1235 |
* @param string $status_header HTTP status header. |
|
1236 |
* @param int $code HTTP status code. |
|
1237 |
* @param string $description Description for the status code. |
|
1238 |
* @param string $protocol Server protocol. |
|
1239 |
*/ |
|
1240 |
$status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol ); |
|
9 | 1241 |
} |
5 | 1242 |
|
1243 |
@header( $status_header, true, $code ); |
|
0 | 1244 |
} |
1245 |
||
1246 |
/** |
|
5 | 1247 |
* Get the header information to prevent caching. |
1248 |
* |
|
1249 |
* The several different headers cover the different ways cache prevention |
|
1250 |
* is handled by different browsers |
|
0 | 1251 |
* |
1252 |
* @since 2.8.0 |
|
1253 |
* |
|
1254 |
* @return array The associative array of header names and field values. |
|
1255 |
*/ |
|
1256 |
function wp_get_nocache_headers() { |
|
1257 |
$headers = array( |
|
9 | 1258 |
'Expires' => 'Wed, 11 Jan 1984 05:00:00 GMT', |
0 | 1259 |
'Cache-Control' => 'no-cache, must-revalidate, max-age=0', |
1260 |
); |
|
1261 |
||
9 | 1262 |
if ( function_exists( 'apply_filters' ) ) { |
5 | 1263 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
* Filters the cache-controlling headers. |
5 | 1265 |
* |
1266 |
* @since 2.8.0 |
|
1267 |
* |
|
1268 |
* @see wp_get_nocache_headers() |
|
1269 |
* |
|
1270 |
* @param array $headers { |
|
1271 |
* Header names and field values. |
|
1272 |
* |
|
1273 |
* @type string $Expires Expires header. |
|
1274 |
* @type string $Cache-Control Cache-Control header. |
|
1275 |
* } |
|
1276 |
*/ |
|
1277 |
$headers = (array) apply_filters( 'nocache_headers', $headers ); |
|
0 | 1278 |
} |
1279 |
$headers['Last-Modified'] = false; |
|
1280 |
return $headers; |
|
1281 |
} |
|
1282 |
||
1283 |
/** |
|
5 | 1284 |
* Set the headers to prevent caching for the different browsers. |
1285 |
* |
|
1286 |
* Different browsers support different nocache headers, so several |
|
1287 |
* headers must be sent so that all of them get the point that no |
|
1288 |
* caching should occur. |
|
0 | 1289 |
* |
1290 |
* @since 2.0.0 |
|
5 | 1291 |
* |
1292 |
* @see wp_get_nocache_headers() |
|
0 | 1293 |
*/ |
1294 |
function nocache_headers() { |
|
1295 |
$headers = wp_get_nocache_headers(); |
|
1296 |
||
1297 |
unset( $headers['Last-Modified'] ); |
|
1298 |
||
1299 |
// In PHP 5.3+, make sure we are not sending a Last-Modified header. |
|
1300 |
if ( function_exists( 'header_remove' ) ) { |
|
1301 |
@header_remove( 'Last-Modified' ); |
|
1302 |
} else { |
|
1303 |
// In PHP 5.2, send an empty Last-Modified header, but only as a |
|
1304 |
// last resort to override a header already sent. #WP23021 |
|
1305 |
foreach ( headers_list() as $header ) { |
|
1306 |
if ( 0 === stripos( $header, 'Last-Modified' ) ) { |
|
1307 |
$headers['Last-Modified'] = ''; |
|
1308 |
break; |
|
1309 |
} |
|
1310 |
} |
|
1311 |
} |
|
1312 |
||
9 | 1313 |
foreach ( $headers as $name => $field_value ) { |
1314 |
@header( "{$name}: {$field_value}" ); |
|
1315 |
} |
|
0 | 1316 |
} |
1317 |
||
1318 |
/** |
|
1319 |
* Set the headers for caching for 10 days with JavaScript content type. |
|
1320 |
* |
|
1321 |
* @since 2.1.0 |
|
1322 |
*/ |
|
1323 |
function cache_javascript_headers() { |
|
1324 |
$expiresOffset = 10 * DAY_IN_SECONDS; |
|
5 | 1325 |
|
9 | 1326 |
header( 'Content-Type: text/javascript; charset=' . get_bloginfo( 'charset' ) ); |
1327 |
header( 'Vary: Accept-Encoding' ); // Handle proxies |
|
1328 |
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + $expiresOffset ) . ' GMT' ); |
|
0 | 1329 |
} |
1330 |
||
1331 |
/** |
|
1332 |
* Retrieve the number of database queries during the WordPress execution. |
|
1333 |
* |
|
1334 |
* @since 2.0.0 |
|
1335 |
* |
|
5 | 1336 |
* @global wpdb $wpdb WordPress database abstraction object. |
1337 |
* |
|
1338 |
* @return int Number of database queries. |
|
0 | 1339 |
*/ |
1340 |
function get_num_queries() { |
|
1341 |
global $wpdb; |
|
1342 |
return $wpdb->num_queries; |
|
1343 |
} |
|
1344 |
||
1345 |
/** |
|
5 | 1346 |
* Whether input is yes or no. |
1347 |
* |
|
1348 |
* Must be 'y' to be true. |
|
0 | 1349 |
* |
1350 |
* @since 1.0.0 |
|
1351 |
* |
|
5 | 1352 |
* @param string $yn Character string containing either 'y' (yes) or 'n' (no). |
1353 |
* @return bool True if yes, false on anything else. |
|
0 | 1354 |
*/ |
1355 |
function bool_from_yn( $yn ) { |
|
1356 |
return ( strtolower( $yn ) == 'y' ); |
|
1357 |
} |
|
1358 |
||
1359 |
/** |
|
5 | 1360 |
* Load the feed template from the use of an action hook. |
0 | 1361 |
* |
1362 |
* If the feed action does not have a hook, then the function will die with a |
|
1363 |
* message telling the visitor that the feed is not valid. |
|
1364 |
* |
|
1365 |
* It is better to only have one hook for each feed. |
|
1366 |
* |
|
1367 |
* @since 2.1.0 |
|
5 | 1368 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* @global WP_Query $wp_query Used to tell if the use a comment feed. |
0 | 1370 |
*/ |
1371 |
function do_feed() { |
|
1372 |
global $wp_query; |
|
1373 |
||
1374 |
$feed = get_query_var( 'feed' ); |
|
1375 |
||
1376 |
// Remove the pad, if present. |
|
1377 |
$feed = preg_replace( '/^_+/', '', $feed ); |
|
1378 |
||
9 | 1379 |
if ( $feed == '' || $feed == 'feed' ) { |
0 | 1380 |
$feed = get_default_feed(); |
9 | 1381 |
} |
0 | 1382 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
if ( ! has_action( "do_feed_{$feed}" ) ) { |
0 | 1384 |
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
|
1385 |
} |
0 | 1386 |
|
5 | 1387 |
/** |
1388 |
* Fires once the given feed is loaded. |
|
1389 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
* 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
|
1391 |
* Possible values include: 'rdf', 'rss', 'rss2', and 'atom'. |
5 | 1392 |
* |
1393 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
* @since 4.4.0 The `$feed` parameter was added. |
5 | 1395 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
* @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
|
1397 |
* @param string $feed The feed name. |
5 | 1398 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
do_action( "do_feed_{$feed}", $wp_query->is_comment_feed, $feed ); |
0 | 1400 |
} |
1401 |
||
1402 |
/** |
|
1403 |
* Load the RDF RSS 0.91 Feed template. |
|
1404 |
* |
|
1405 |
* @since 2.1.0 |
|
5 | 1406 |
* |
1407 |
* @see load_template() |
|
0 | 1408 |
*/ |
1409 |
function do_feed_rdf() { |
|
1410 |
load_template( ABSPATH . WPINC . '/feed-rdf.php' ); |
|
1411 |
} |
|
1412 |
||
1413 |
/** |
|
1414 |
* Load the RSS 1.0 Feed Template. |
|
1415 |
* |
|
1416 |
* @since 2.1.0 |
|
5 | 1417 |
* |
1418 |
* @see load_template() |
|
0 | 1419 |
*/ |
1420 |
function do_feed_rss() { |
|
1421 |
load_template( ABSPATH . WPINC . '/feed-rss.php' ); |
|
1422 |
} |
|
1423 |
||
1424 |
/** |
|
1425 |
* Load either the RSS2 comment feed or the RSS2 posts feed. |
|
1426 |
* |
|
1427 |
* @since 2.1.0 |
|
1428 |
* |
|
5 | 1429 |
* @see load_template() |
1430 |
* |
|
0 | 1431 |
* @param bool $for_comments True for the comment feed, false for normal feed. |
1432 |
*/ |
|
1433 |
function do_feed_rss2( $for_comments ) { |
|
9 | 1434 |
if ( $for_comments ) { |
0 | 1435 |
load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' ); |
9 | 1436 |
} else { |
0 | 1437 |
load_template( ABSPATH . WPINC . '/feed-rss2.php' ); |
9 | 1438 |
} |
0 | 1439 |
} |
1440 |
||
1441 |
/** |
|
1442 |
* Load either Atom comment feed or Atom posts feed. |
|
1443 |
* |
|
1444 |
* @since 2.1.0 |
|
1445 |
* |
|
5 | 1446 |
* @see load_template() |
1447 |
* |
|
0 | 1448 |
* @param bool $for_comments True for the comment feed, false for normal feed. |
1449 |
*/ |
|
1450 |
function do_feed_atom( $for_comments ) { |
|
9 | 1451 |
if ( $for_comments ) { |
1452 |
load_template( ABSPATH . WPINC . '/feed-atom-comments.php' ); |
|
1453 |
} else { |
|
0 | 1454 |
load_template( ABSPATH . WPINC . '/feed-atom.php' ); |
9 | 1455 |
} |
0 | 1456 |
} |
1457 |
||
1458 |
/** |
|
1459 |
* Display the robots.txt file content. |
|
1460 |
* |
|
1461 |
* The echo content should be with usage of the permalinks or for creating the |
|
1462 |
* robots.txt file. |
|
1463 |
* |
|
1464 |
* @since 2.1.0 |
|
1465 |
*/ |
|
1466 |
function do_robots() { |
|
1467 |
header( 'Content-Type: text/plain; charset=utf-8' ); |
|
1468 |
||
5 | 1469 |
/** |
1470 |
* Fires when displaying the robots.txt file. |
|
1471 |
* |
|
1472 |
* @since 2.1.0 |
|
1473 |
*/ |
|
0 | 1474 |
do_action( 'do_robotstxt' ); |
1475 |
||
1476 |
$output = "User-agent: *\n"; |
|
1477 |
$public = get_option( 'blog_public' ); |
|
1478 |
if ( '0' == $public ) { |
|
1479 |
$output .= "Disallow: /\n"; |
|
1480 |
} else { |
|
1481 |
$site_url = parse_url( site_url() ); |
|
9 | 1482 |
$path = ( ! empty( $site_url['path'] ) ) ? $site_url['path'] : ''; |
1483 |
$output .= "Disallow: $path/wp-admin/\n"; |
|
1484 |
$output .= "Allow: $path/wp-admin/admin-ajax.php\n"; |
|
0 | 1485 |
} |
1486 |
||
5 | 1487 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
* Filters the robots.txt output. |
5 | 1489 |
* |
1490 |
* @since 3.0.0 |
|
1491 |
* |
|
1492 |
* @param string $output Robots.txt output. |
|
1493 |
* @param bool $public Whether the site is considered "public". |
|
1494 |
*/ |
|
1495 |
echo apply_filters( 'robots_txt', $output, $public ); |
|
0 | 1496 |
} |
1497 |
||
1498 |
/** |
|
9 | 1499 |
* Determines whether WordPress is already installed. |
0 | 1500 |
* |
5 | 1501 |
* The cache will be checked first. If you have a cache plugin, which saves |
1502 |
* the cache values, then this will work. If you use the default WordPress |
|
1503 |
* cache, and the database goes away, then you might have problems. |
|
1504 |
* |
|
1505 |
* Checks for the 'siteurl' option for whether WordPress is installed. |
|
0 | 1506 |
* |
9 | 1507 |
* For more information on this and similar theme functions, check out |
1508 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1509 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
1510 |
* |
|
0 | 1511 |
* @since 2.1.0 |
5 | 1512 |
* |
1513 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
1514 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
* @return bool Whether the site is already installed. |
0 | 1516 |
*/ |
1517 |
function is_blog_installed() { |
|
1518 |
global $wpdb; |
|
1519 |
||
5 | 1520 |
/* |
1521 |
* Check cache first. If options table goes away and we have true |
|
1522 |
* cached, oh well. |
|
1523 |
*/ |
|
9 | 1524 |
if ( wp_cache_get( 'is_blog_installed' ) ) { |
0 | 1525 |
return true; |
9 | 1526 |
} |
0 | 1527 |
|
1528 |
$suppress = $wpdb->suppress_errors(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
if ( ! wp_installing() ) { |
0 | 1530 |
$alloptions = wp_load_alloptions(); |
1531 |
} |
|
1532 |
// If siteurl is not set to autoload, check it specifically |
|
9 | 1533 |
if ( ! isset( $alloptions['siteurl'] ) ) { |
0 | 1534 |
$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" ); |
9 | 1535 |
} else { |
0 | 1536 |
$installed = $alloptions['siteurl']; |
9 | 1537 |
} |
0 | 1538 |
$wpdb->suppress_errors( $suppress ); |
1539 |
||
9 | 1540 |
$installed = ! empty( $installed ); |
0 | 1541 |
wp_cache_set( 'is_blog_installed', $installed ); |
1542 |
||
9 | 1543 |
if ( $installed ) { |
0 | 1544 |
return true; |
9 | 1545 |
} |
0 | 1546 |
|
1547 |
// If visiting repair.php, return true and let it take over. |
|
9 | 1548 |
if ( defined( 'WP_REPAIRING' ) ) { |
0 | 1549 |
return true; |
9 | 1550 |
} |
0 | 1551 |
|
1552 |
$suppress = $wpdb->suppress_errors(); |
|
1553 |
||
5 | 1554 |
/* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1555 |
* Loop over the WP tables. If none exist, then scratch installation is allowed. |
5 | 1556 |
* If one or more exist, suggest table repair since we got here because the |
1557 |
* options table could not be accessed. |
|
1558 |
*/ |
|
0 | 1559 |
$wp_tables = $wpdb->tables(); |
1560 |
foreach ( $wp_tables as $table ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
// The existence of custom user tables shouldn't suggest an insane state or prevent a clean installation. |
9 | 1562 |
if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE == $table ) { |
0 | 1563 |
continue; |
9 | 1564 |
} |
1565 |
if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE == $table ) { |
|
0 | 1566 |
continue; |
9 | 1567 |
} |
1568 |
||
1569 |
if ( ! $wpdb->get_results( "DESCRIBE $table;" ) ) { |
|
0 | 1570 |
continue; |
9 | 1571 |
} |
0 | 1572 |
|
1573 |
// One or more tables exist. We are insane. |
|
1574 |
||
1575 |
wp_load_translations_early(); |
|
1576 |
||
1577 |
// Die with a DB error. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1578 |
$wpdb->error = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
/* translators: %s: database repair URL */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
__( '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
|
1581 |
'maint/repair.php?referrer=is_blog_installed' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
|
0 | 1584 |
dead_db(); |
1585 |
} |
|
1586 |
||
1587 |
$wpdb->suppress_errors( $suppress ); |
|
1588 |
||
1589 |
wp_cache_set( 'is_blog_installed', false ); |
|
1590 |
||
1591 |
return false; |
|
1592 |
} |
|
1593 |
||
1594 |
/** |
|
1595 |
* Retrieve URL with nonce added to URL query. |
|
1596 |
* |
|
1597 |
* @since 2.0.4 |
|
1598 |
* |
|
5 | 1599 |
* @param string $actionurl URL to add nonce action. |
1600 |
* @param int|string $action Optional. Nonce action name. Default -1. |
|
1601 |
* @param string $name Optional. Nonce name. Default '_wpnonce'. |
|
1602 |
* @return string Escaped URL with nonce action added. |
|
0 | 1603 |
*/ |
1604 |
function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) { |
|
1605 |
$actionurl = str_replace( '&', '&', $actionurl ); |
|
1606 |
return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) ); |
|
1607 |
} |
|
1608 |
||
1609 |
/** |
|
1610 |
* Retrieve or display nonce hidden field for forms. |
|
1611 |
* |
|
1612 |
* The nonce field is used to validate that the contents of the form came from |
|
1613 |
* the location on the current site and not somewhere else. The nonce does not |
|
1614 |
* offer absolute protection, but should protect against most cases. It is very |
|
1615 |
* important to use nonce field in forms. |
|
1616 |
* |
|
1617 |
* The $action and $name are optional, but if you want to have better security, |
|
1618 |
* it is strongly suggested to set those two parameters. It is easier to just |
|
1619 |
* call the function without any parameters, because validation of the nonce |
|
1620 |
* doesn't require any parameters, but since crackers know what the default is |
|
1621 |
* it won't be difficult for them to find a way around your nonce and cause |
|
1622 |
* damage. |
|
1623 |
* |
|
1624 |
* The input name will be whatever $name value you gave. The input value will be |
|
1625 |
* the nonce creation value. |
|
1626 |
* |
|
1627 |
* @since 2.0.4 |
|
1628 |
* |
|
5 | 1629 |
* @param int|string $action Optional. Action name. Default -1. |
1630 |
* @param string $name Optional. Nonce name. Default '_wpnonce'. |
|
1631 |
* @param bool $referer Optional. Whether to set the referer field for validation. Default true. |
|
1632 |
* @param bool $echo Optional. Whether to display or return hidden form field. Default true. |
|
1633 |
* @return string Nonce field HTML markup. |
|
0 | 1634 |
*/ |
9 | 1635 |
function wp_nonce_field( $action = -1, $name = '_wpnonce', $referer = true, $echo = true ) { |
1636 |
$name = esc_attr( $name ); |
|
0 | 1637 |
$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />'; |
1638 |
||
9 | 1639 |
if ( $referer ) { |
0 | 1640 |
$nonce_field .= wp_referer_field( false ); |
9 | 1641 |
} |
1642 |
||
1643 |
if ( $echo ) { |
|
0 | 1644 |
echo $nonce_field; |
9 | 1645 |
} |
0 | 1646 |
|
1647 |
return $nonce_field; |
|
1648 |
} |
|
1649 |
||
1650 |
/** |
|
1651 |
* Retrieve or display referer hidden field for forms. |
|
1652 |
* |
|
1653 |
* The referer link is the current Request URI from the server super global. The |
|
1654 |
* input name is '_wp_http_referer', in case you wanted to check manually. |
|
1655 |
* |
|
1656 |
* @since 2.0.4 |
|
1657 |
* |
|
5 | 1658 |
* @param bool $echo Optional. Whether to echo or return the referer field. Default true. |
1659 |
* @return string Referer field HTML markup. |
|
0 | 1660 |
*/ |
1661 |
function wp_referer_field( $echo = true ) { |
|
9 | 1662 |
$referer_field = '<input type="hidden" name="_wp_http_referer" value="' . esc_attr( wp_unslash( $_SERVER['REQUEST_URI'] ) ) . '" />'; |
1663 |
||
1664 |
if ( $echo ) { |
|
0 | 1665 |
echo $referer_field; |
9 | 1666 |
} |
0 | 1667 |
return $referer_field; |
1668 |
} |
|
1669 |
||
1670 |
/** |
|
1671 |
* Retrieve or display original referer hidden field for forms. |
|
1672 |
* |
|
1673 |
* The input name is '_wp_original_http_referer' and will be either the same |
|
5 | 1674 |
* value of wp_referer_field(), if that was posted already or it will be the |
1675 |
* current page, if it doesn't exist. |
|
1676 |
* |
|
0 | 1677 |
* @since 2.0.4 |
1678 |
* |
|
5 | 1679 |
* @param bool $echo Optional. Whether to echo the original http referer. Default true. |
1680 |
* @param string $jump_back_to Optional. Can be 'previous' or page you want to jump back to. |
|
1681 |
* Default 'current'. |
|
0 | 1682 |
* @return string Original referer field. |
1683 |
*/ |
|
1684 |
function wp_original_referer_field( $echo = true, $jump_back_to = 'current' ) { |
|
1685 |
if ( ! $ref = wp_get_original_referer() ) { |
|
1686 |
$ref = 'previous' == $jump_back_to ? wp_get_referer() : wp_unslash( $_SERVER['REQUEST_URI'] ); |
|
1687 |
} |
|
1688 |
$orig_referer_field = '<input type="hidden" name="_wp_original_http_referer" value="' . esc_attr( $ref ) . '" />'; |
|
9 | 1689 |
if ( $echo ) { |
0 | 1690 |
echo $orig_referer_field; |
9 | 1691 |
} |
0 | 1692 |
return $orig_referer_field; |
1693 |
} |
|
1694 |
||
1695 |
/** |
|
5 | 1696 |
* Retrieve referer from '_wp_http_referer' or HTTP referer. |
1697 |
* |
|
1698 |
* If it's the same as the current request URL, will return false. |
|
1699 |
* |
|
0 | 1700 |
* @since 2.0.4 |
1701 |
* |
|
5 | 1702 |
* @return false|string False on failure. Referer URL on success. |
0 | 1703 |
*/ |
1704 |
function wp_get_referer() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1705 |
if ( ! function_exists( 'wp_validate_redirect' ) ) { |
0 | 1706 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1707 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
$ref = wp_get_raw_referer(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
if ( $ref && $ref !== wp_unslash( $_SERVER['REQUEST_URI'] ) && $ref !== home_url() . wp_unslash( $_SERVER['REQUEST_URI'] ) ) { |
0 | 1712 |
return wp_validate_redirect( $ref, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1713 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1714 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1715 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1717 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1718 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1719 |
* 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
|
1720 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1721 |
* 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
|
1722 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1724 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1725 |
* @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
|
1726 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1727 |
function wp_get_raw_referer() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1728 |
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1729 |
return wp_unslash( $_REQUEST['_wp_http_referer'] ); |
9 | 1730 |
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1731 |
return wp_unslash( $_SERVER['HTTP_REFERER'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1732 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
|
0 | 1734 |
return false; |
1735 |
} |
|
1736 |
||
1737 |
/** |
|
1738 |
* Retrieve original referer that was posted, if it exists. |
|
1739 |
* |
|
1740 |
* @since 2.0.4 |
|
1741 |
* |
|
5 | 1742 |
* @return string|false False if no original referer or original referer if set. |
0 | 1743 |
*/ |
1744 |
function wp_get_original_referer() { |
|
9 | 1745 |
if ( ! empty( $_REQUEST['_wp_original_http_referer'] ) && function_exists( 'wp_validate_redirect' ) ) { |
0 | 1746 |
return wp_validate_redirect( wp_unslash( $_REQUEST['_wp_original_http_referer'] ), false ); |
9 | 1747 |
} |
0 | 1748 |
return false; |
1749 |
} |
|
1750 |
||
1751 |
/** |
|
1752 |
* Recursive directory creation based on full path. |
|
1753 |
* |
|
1754 |
* Will attempt to set permissions on folders. |
|
1755 |
* |
|
1756 |
* @since 2.0.1 |
|
1757 |
* |
|
1758 |
* @param string $target Full path to attempt to create. |
|
1759 |
* @return bool Whether the path was created. True if path already exists. |
|
1760 |
*/ |
|
1761 |
function wp_mkdir_p( $target ) { |
|
1762 |
$wrapper = null; |
|
1763 |
||
5 | 1764 |
// Strip the protocol. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
if ( wp_is_stream( $target ) ) { |
0 | 1766 |
list( $wrapper, $target ) = explode( '://', $target, 2 ); |
1767 |
} |
|
1768 |
||
5 | 1769 |
// From php.net/mkdir user contributed notes. |
0 | 1770 |
$target = str_replace( '//', '/', $target ); |
1771 |
||
5 | 1772 |
// Put the wrapper back on the target. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
if ( $wrapper !== null ) { |
0 | 1774 |
$target = $wrapper . '://' . $target; |
1775 |
} |
|
1776 |
||
5 | 1777 |
/* |
1778 |
* Safe mode fails with a trailing slash under certain PHP versions. |
|
1779 |
* Use rtrim() instead of untrailingslashit to avoid formatting.php dependency. |
|
1780 |
*/ |
|
9 | 1781 |
$target = rtrim( $target, '/' ); |
1782 |
if ( empty( $target ) ) { |
|
0 | 1783 |
$target = '/'; |
9 | 1784 |
} |
1785 |
||
1786 |
if ( file_exists( $target ) ) { |
|
0 | 1787 |
return @is_dir( $target ); |
9 | 1788 |
} |
0 | 1789 |
|
1790 |
// We need to find the permissions of the parent folder that exists and inherit that. |
|
1791 |
$target_parent = dirname( $target ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1792 |
while ( '.' != $target_parent && ! is_dir( $target_parent ) && dirname( $target_parent ) !== $target_parent ) { |
0 | 1793 |
$target_parent = dirname( $target_parent ); |
1794 |
} |
|
1795 |
||
1796 |
// Get the permission bits. |
|
5 | 1797 |
if ( $stat = @stat( $target_parent ) ) { |
0 | 1798 |
$dir_perms = $stat['mode'] & 0007777; |
1799 |
} else { |
|
1800 |
$dir_perms = 0777; |
|
1801 |
} |
|
1802 |
||
1803 |
if ( @mkdir( $target, $dir_perms, true ) ) { |
|
5 | 1804 |
|
1805 |
/* |
|
1806 |
* If a umask is set that modifies $dir_perms, we'll have to re-set |
|
1807 |
* the $dir_perms correctly with chmod() |
|
1808 |
*/ |
|
1809 |
if ( $dir_perms != ( $dir_perms & ~umask() ) ) { |
|
1810 |
$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) ); |
|
1811 |
for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) { |
|
1812 |
@chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms ); |
|
1813 |
} |
|
1814 |
} |
|
1815 |
||
0 | 1816 |
return true; |
1817 |
} |
|
1818 |
||
1819 |
return false; |
|
1820 |
} |
|
1821 |
||
1822 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1823 |
* Test if a given filesystem path is absolute. |
5 | 1824 |
* |
1825 |
* For example, '/foo/bar', or 'c:\windows'. |
|
0 | 1826 |
* |
1827 |
* @since 2.5.0 |
|
1828 |
* |
|
5 | 1829 |
* @param string $path File path. |
0 | 1830 |
* @return bool True if path is absolute, false is not absolute. |
1831 |
*/ |
|
1832 |
function path_is_absolute( $path ) { |
|
5 | 1833 |
/* |
9 | 1834 |
* Check to see if the path is a stream and check to see if its an actual |
1835 |
* path or file as realpath() does not support stream wrappers. |
|
1836 |
*/ |
|
1837 |
if ( wp_is_stream( $path ) && ( is_dir( $path ) || is_file( $path ) ) ) { |
|
1838 |
return true; |
|
1839 |
} |
|
1840 |
||
1841 |
/* |
|
5 | 1842 |
* This is definitive if true but fails if $path does not exist or contains |
1843 |
* a symbolic link. |
|
1844 |
*/ |
|
9 | 1845 |
if ( realpath( $path ) == $path ) { |
0 | 1846 |
return true; |
9 | 1847 |
} |
1848 |
||
1849 |
if ( strlen( $path ) == 0 || $path[0] == '.' ) { |
|
0 | 1850 |
return false; |
9 | 1851 |
} |
0 | 1852 |
|
5 | 1853 |
// Windows allows absolute paths like this. |
9 | 1854 |
if ( preg_match( '#^[a-zA-Z]:\\\\#', $path ) ) { |
0 | 1855 |
return true; |
9 | 1856 |
} |
0 | 1857 |
|
5 | 1858 |
// A path starting with / or \ is absolute; anything else is relative. |
0 | 1859 |
return ( $path[0] == '/' || $path[0] == '\\' ); |
1860 |
} |
|
1861 |
||
1862 |
/** |
|
5 | 1863 |
* Join two filesystem paths together. |
1864 |
* |
|
1865 |
* For example, 'give me $path relative to $base'. If the $path is absolute, |
|
1866 |
* then it the full path is returned. |
|
0 | 1867 |
* |
1868 |
* @since 2.5.0 |
|
1869 |
* |
|
5 | 1870 |
* @param string $base Base path. |
1871 |
* @param string $path Path relative to $base. |
|
0 | 1872 |
* @return string The path with the base or absolute path. |
1873 |
*/ |
|
1874 |
function path_join( $base, $path ) { |
|
9 | 1875 |
if ( path_is_absolute( $path ) ) { |
0 | 1876 |
return $path; |
9 | 1877 |
} |
1878 |
||
1879 |
return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' ); |
|
0 | 1880 |
} |
1881 |
||
1882 |
/** |
|
5 | 1883 |
* Normalize a filesystem path. |
1884 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1885 |
* On windows systems, replaces backslashes with forward slashes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1886 |
* and forces upper-case drive letters. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1887 |
* 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
|
1888 |
* ensures that all other duplicate slashes are reduced to a single. |
5 | 1889 |
* |
1890 |
* @since 3.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1891 |
* @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
|
1892 |
* @since 4.5.0 Allows for Windows network shares. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1893 |
* @since 4.9.7 Allows for PHP file wrappers. |
5 | 1894 |
* |
1895 |
* @param string $path Path to normalize. |
|
1896 |
* @return string Normalized path. |
|
1897 |
*/ |
|
1898 |
function wp_normalize_path( $path ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1899 |
$wrapper = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1900 |
if ( wp_is_stream( $path ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1901 |
list( $wrapper, $path ) = explode( '://', $path, 2 ); |
9 | 1902 |
$wrapper .= '://'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1903 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1904 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1905 |
// Standardise all paths to use / |
5 | 1906 |
$path = str_replace( '\\', '/', $path ); |
7
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 |
// 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
|
1909 |
$path = preg_replace( '|(?<=.)/+|', '/', $path ); |
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 |
// Windows paths should uppercase the drive letter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1912 |
if ( ':' === substr( $path, 1, 1 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1913 |
$path = ucfirst( $path ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1914 |
} |
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 |
return $wrapper . $path; |
5 | 1917 |
} |
1918 |
||
1919 |
/** |
|
1920 |
* Determine a writable directory for temporary files. |
|
1921 |
* |
|
1922 |
* Function's preference is the return value of sys_get_temp_dir(), |
|
0 | 1923 |
* followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR, |
1924 |
* before finally defaulting to /tmp/ |
|
1925 |
* |
|
1926 |
* In the event that this function does not find a writable location, |
|
5 | 1927 |
* It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file. |
0 | 1928 |
* |
1929 |
* @since 2.5.0 |
|
1930 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1931 |
* @staticvar string $temp |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
* |
5 | 1933 |
* @return string Writable temporary directory. |
0 | 1934 |
*/ |
1935 |
function get_temp_dir() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
static $temp = ''; |
9 | 1937 |
if ( defined( 'WP_TEMP_DIR' ) ) { |
1938 |
return trailingslashit( WP_TEMP_DIR ); |
|
1939 |
} |
|
1940 |
||
1941 |
if ( $temp ) { |
|
5 | 1942 |
return trailingslashit( $temp ); |
9 | 1943 |
} |
1944 |
||
1945 |
if ( function_exists( 'sys_get_temp_dir' ) ) { |
|
0 | 1946 |
$temp = sys_get_temp_dir(); |
9 | 1947 |
if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { |
5 | 1948 |
return trailingslashit( $temp ); |
9 | 1949 |
} |
1950 |
} |
|
1951 |
||
1952 |
$temp = ini_get( 'upload_tmp_dir' ); |
|
1953 |
if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) { |
|
5 | 1954 |
return trailingslashit( $temp ); |
9 | 1955 |
} |
0 | 1956 |
|
1957 |
$temp = WP_CONTENT_DIR . '/'; |
|
9 | 1958 |
if ( is_dir( $temp ) && wp_is_writable( $temp ) ) { |
0 | 1959 |
return $temp; |
9 | 1960 |
} |
0 | 1961 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
return '/tmp/'; |
0 | 1963 |
} |
1964 |
||
1965 |
/** |
|
1966 |
* Determine if a directory is writable. |
|
1967 |
* |
|
5 | 1968 |
* This function is used to work around certain ACL issues in PHP primarily |
1969 |
* affecting Windows Servers. |
|
1970 |
* |
|
1971 |
* @since 3.6.0 |
|
0 | 1972 |
* |
1973 |
* @see win_is_writable() |
|
1974 |
* |
|
5 | 1975 |
* @param string $path Path to check for write-ability. |
1976 |
* @return bool Whether the path is writable. |
|
0 | 1977 |
*/ |
1978 |
function wp_is_writable( $path ) { |
|
9 | 1979 |
if ( 'WIN' === strtoupper( substr( PHP_OS, 0, 3 ) ) ) { |
0 | 1980 |
return win_is_writable( $path ); |
9 | 1981 |
} else { |
0 | 1982 |
return @is_writable( $path ); |
9 | 1983 |
} |
0 | 1984 |
} |
1985 |
||
1986 |
/** |
|
1987 |
* Workaround for Windows bug in is_writable() function |
|
1988 |
* |
|
1989 |
* PHP has issues with Windows ACL's for determine if a |
|
1990 |
* directory is writable or not, this works around them by |
|
1991 |
* checking the ability to open files rather than relying |
|
1992 |
* upon PHP to interprate the OS ACL. |
|
1993 |
* |
|
1994 |
* @since 2.8.0 |
|
1995 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1996 |
* @see https://bugs.php.net/bug.php?id=27609 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1997 |
* @see https://bugs.php.net/bug.php?id=30931 |
5 | 1998 |
* |
1999 |
* @param string $path Windows path to check for write-ability. |
|
2000 |
* @return bool Whether the path is writable. |
|
0 | 2001 |
*/ |
2002 |
function win_is_writable( $path ) { |
|
2003 |
||
9 | 2004 |
if ( $path[ strlen( $path ) - 1 ] == '/' ) { // if it looks like a directory, check a random file within the directory |
2005 |
return win_is_writable( $path . uniqid( mt_rand() ) . '.tmp' ); |
|
5 | 2006 |
} elseif ( is_dir( $path ) ) { // If it's a directory (and not a file) check a random file within the directory |
0 | 2007 |
return win_is_writable( $path . '/' . uniqid( mt_rand() ) . '.tmp' ); |
5 | 2008 |
} |
0 | 2009 |
// check tmp file for read/write capabilities |
9 | 2010 |
$should_delete_tmp_file = ! file_exists( $path ); |
2011 |
$f = @fopen( $path, 'a' ); |
|
2012 |
if ( $f === false ) { |
|
0 | 2013 |
return false; |
9 | 2014 |
} |
0 | 2015 |
fclose( $f ); |
9 | 2016 |
if ( $should_delete_tmp_file ) { |
0 | 2017 |
unlink( $path ); |
9 | 2018 |
} |
0 | 2019 |
return true; |
2020 |
} |
|
2021 |
||
2022 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2023 |
* Retrieves uploads directory information. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2024 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2025 |
* 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
|
2026 |
* 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
|
2027 |
* when not uploading files. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2028 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2029 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2030 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2031 |
* @see wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2032 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2033 |
* @return array See wp_upload_dir() for description. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2034 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2035 |
function wp_get_upload_dir() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2036 |
return wp_upload_dir( null, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2037 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2038 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2039 |
/** |
0 | 2040 |
* Get an array containing the current upload directory's path and url. |
2041 |
* |
|
2042 |
* Checks the 'upload_path' option, which should be from the web root folder, |
|
2043 |
* and if it isn't empty it will be used. If it is empty, then the path will be |
|
2044 |
* 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will |
|
2045 |
* override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path. |
|
2046 |
* |
|
2047 |
* The upload URL path is set either by the 'upload_url_path' option or by using |
|
2048 |
* the 'WP_CONTENT_URL' constant and appending '/uploads' to the path. |
|
2049 |
* |
|
2050 |
* If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in |
|
2051 |
* the administration settings panel), then the time will be used. The format |
|
2052 |
* will be year first and then month. |
|
2053 |
* |
|
2054 |
* If the path couldn't be created, then an error will be returned with the key |
|
2055 |
* 'error' containing the error message. The error suggests that the parent |
|
2056 |
* directory is not writable by the server. |
|
2057 |
* |
|
2058 |
* On success, the returned array will have many indices: |
|
2059 |
* 'path' - base directory and sub directory or full path to upload directory. |
|
2060 |
* 'url' - base url and sub directory or absolute URL to upload directory. |
|
2061 |
* 'subdir' - sub directory if uploads use year/month folders option is on. |
|
2062 |
* 'basedir' - path without subdir. |
|
2063 |
* 'baseurl' - URL path without subdir. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2064 |
* 'error' - false or error message. |
0 | 2065 |
* |
2066 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2067 |
* @uses _wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2068 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2069 |
* @staticvar array $cache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2070 |
* @staticvar array $tested_paths |
5 | 2071 |
* |
2072 |
* @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
|
2073 |
* @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
|
2074 |
* Default true for backward compatibility. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2075 |
* @param bool $refresh_cache Optional. Whether to refresh the cache. Default false. |
0 | 2076 |
* @return array See above for description. |
2077 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2078 |
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
|
2079 |
static $cache = array(), $tested_paths = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2080 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2081 |
$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
|
2082 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2083 |
if ( $refresh_cache || empty( $cache[ $key ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2084 |
$cache[ $key ] = _wp_upload_dir( $time ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2085 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2086 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2087 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2088 |
* Filters the uploads directory data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2089 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2090 |
* @since 2.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2091 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2092 |
* @param array $uploads Array of upload directory data with keys of 'path', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2093 |
* 'url', 'subdir, 'basedir', and 'error'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2094 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2095 |
$uploads = apply_filters( 'upload_dir', $cache[ $key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2096 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2097 |
if ( $create_dir ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2098 |
$path = $uploads['path']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2099 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2100 |
if ( array_key_exists( $path, $tested_paths ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2101 |
$uploads['error'] = $tested_paths[ $path ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2102 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2103 |
if ( ! wp_mkdir_p( $path ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2104 |
if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2105 |
$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2106 |
} else { |
9 | 2107 |
$error_path = wp_basename( $uploads['basedir'] ) . $uploads['subdir']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2108 |
} |
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 |
$uploads['error'] = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2111 |
/* translators: %s: directory path */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2112 |
__( '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
|
2113 |
esc_html( $error_path ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2114 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2115 |
} |
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 |
$tested_paths[ $path ] = $uploads['error']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2118 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2119 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2120 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2121 |
return $uploads; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2122 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2123 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2124 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2125 |
* 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
|
2126 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2127 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2128 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2129 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2130 |
* @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
|
2131 |
* @return array See wp_upload_dir() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2132 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2133 |
function _wp_upload_dir( $time = null ) { |
9 | 2134 |
$siteurl = get_option( 'siteurl' ); |
0 | 2135 |
$upload_path = trim( get_option( 'upload_path' ) ); |
2136 |
||
2137 |
if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) { |
|
2138 |
$dir = WP_CONTENT_DIR . '/uploads'; |
|
2139 |
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) { |
|
2140 |
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH |
|
2141 |
$dir = path_join( ABSPATH, $upload_path ); |
|
2142 |
} else { |
|
2143 |
$dir = $upload_path; |
|
2144 |
} |
|
2145 |
||
9 | 2146 |
if ( ! $url = get_option( 'upload_url_path' ) ) { |
2147 |
if ( empty( $upload_path ) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) ) { |
|
0 | 2148 |
$url = WP_CONTENT_URL . '/uploads'; |
9 | 2149 |
} else { |
0 | 2150 |
$url = trailingslashit( $siteurl ) . $upload_path; |
9 | 2151 |
} |
0 | 2152 |
} |
2153 |
||
5 | 2154 |
/* |
2155 |
* Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled. |
|
2156 |
* We also sometimes obey UPLOADS when rewriting is enabled -- see the next block. |
|
2157 |
*/ |
|
0 | 2158 |
if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) { |
2159 |
$dir = ABSPATH . UPLOADS; |
|
2160 |
$url = trailingslashit( $siteurl ) . UPLOADS; |
|
2161 |
} |
|
2162 |
||
2163 |
// If multisite (and if not the main site in a post-MU network) |
|
2164 |
if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) { |
|
2165 |
||
2166 |
if ( ! get_site_option( 'ms_files_rewriting' ) ) { |
|
5 | 2167 |
/* |
2168 |
* If ms-files rewriting is disabled (networks created post-3.5), it is fairly |
|
2169 |
* straightforward: Append sites/%d if we're not on the main site (for post-MU |
|
2170 |
* networks). (The extra directory prevents a four-digit ID from conflicting with |
|
2171 |
* a year-based directory for the main site. But if a MU-era network has disabled |
|
2172 |
* ms-files rewriting manually, they don't need the extra directory, as they never |
|
2173 |
* had wp-content/uploads for the main site.) |
|
2174 |
*/ |
|
0 | 2175 |
|
9 | 2176 |
if ( defined( 'MULTISITE' ) ) { |
0 | 2177 |
$ms_dir = '/sites/' . get_current_blog_id(); |
9 | 2178 |
} else { |
0 | 2179 |
$ms_dir = '/' . get_current_blog_id(); |
9 | 2180 |
} |
0 | 2181 |
|
2182 |
$dir .= $ms_dir; |
|
2183 |
$url .= $ms_dir; |
|
2184 |
||
2185 |
} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) { |
|
5 | 2186 |
/* |
2187 |
* Handle the old-form ms-files.php rewriting if the network still has that enabled. |
|
2188 |
* When ms-files rewriting is enabled, then we only listen to UPLOADS when: |
|
2189 |
* 1) We are not on the main site in a post-MU network, as wp-content/uploads is used |
|
2190 |
* there, and |
|
2191 |
* 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect |
|
2192 |
* the original blog ID. |
|
2193 |
* |
|
2194 |
* Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute. |
|
2195 |
* (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as |
|
2196 |
* as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files |
|
2197 |
* rewriting in multisite, the resulting URL is /files. (#WP22702 for background.) |
|
2198 |
*/ |
|
0 | 2199 |
|
9 | 2200 |
if ( defined( 'BLOGUPLOADDIR' ) ) { |
0 | 2201 |
$dir = untrailingslashit( BLOGUPLOADDIR ); |
9 | 2202 |
} else { |
0 | 2203 |
$dir = ABSPATH . UPLOADS; |
9 | 2204 |
} |
0 | 2205 |
$url = trailingslashit( $siteurl ) . 'files'; |
2206 |
} |
|
2207 |
} |
|
2208 |
||
2209 |
$basedir = $dir; |
|
2210 |
$baseurl = $url; |
|
2211 |
||
2212 |
$subdir = ''; |
|
2213 |
if ( get_option( 'uploads_use_yearmonth_folders' ) ) { |
|
2214 |
// Generate the yearly and monthly dirs |
|
9 | 2215 |
if ( ! $time ) { |
0 | 2216 |
$time = current_time( 'mysql' ); |
9 | 2217 |
} |
2218 |
$y = substr( $time, 0, 4 ); |
|
2219 |
$m = substr( $time, 5, 2 ); |
|
0 | 2220 |
$subdir = "/$y/$m"; |
2221 |
} |
|
2222 |
||
2223 |
$dir .= $subdir; |
|
2224 |
$url .= $subdir; |
|
2225 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2226 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2227 |
'path' => $dir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2228 |
'url' => $url, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2229 |
'subdir' => $subdir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2230 |
'basedir' => $basedir, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2231 |
'baseurl' => $baseurl, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2232 |
'error' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2233 |
); |
0 | 2234 |
} |
2235 |
||
2236 |
/** |
|
2237 |
* Get a filename that is sanitized and unique for the given directory. |
|
2238 |
* |
|
2239 |
* If the filename is not unique, then a number will be added to the filename |
|
2240 |
* before the extension, and will continue adding numbers until the filename is |
|
2241 |
* unique. |
|
2242 |
* |
|
2243 |
* The callback is passed three parameters, the first one is the directory, the |
|
2244 |
* second is the filename, and the third is the extension. |
|
2245 |
* |
|
2246 |
* @since 2.5.0 |
|
2247 |
* |
|
5 | 2248 |
* @param string $dir Directory. |
2249 |
* @param string $filename File name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2250 |
* @param callable $unique_filename_callback Callback. Default null. |
0 | 2251 |
* @return string New filename, if given wasn't unique. |
2252 |
*/ |
|
2253 |
function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) { |
|
5 | 2254 |
// Sanitize the file name before we begin processing. |
9 | 2255 |
$filename = sanitize_file_name( $filename ); |
0 | 2256 |
|
5 | 2257 |
// Separate the filename into a name and extension. |
9 | 2258 |
$ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2259 |
$name = pathinfo( $filename, PATHINFO_BASENAME ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2260 |
if ( $ext ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2261 |
$ext = '.' . $ext; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2262 |
} |
0 | 2263 |
|
5 | 2264 |
// 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
|
2265 |
if ( $name === $ext ) { |
0 | 2266 |
$name = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2267 |
} |
0 | 2268 |
|
5 | 2269 |
/* |
2270 |
* Increment the file number until we have a unique file to save in $dir. |
|
2271 |
* Use callback if supplied. |
|
2272 |
*/ |
|
0 | 2273 |
if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) { |
2274 |
$filename = call_user_func( $unique_filename_callback, $dir, $name, $ext ); |
|
2275 |
} else { |
|
2276 |
$number = ''; |
|
2277 |
||
5 | 2278 |
// Change '.ext' to lower case. |
9 | 2279 |
if ( $ext && strtolower( $ext ) != $ext ) { |
2280 |
$ext2 = strtolower( $ext ); |
|
2281 |
$filename2 = preg_replace( '|' . preg_quote( $ext ) . '$|', $ext2, $filename ); |
|
0 | 2282 |
|
5 | 2283 |
// Check for both lower and upper case extension or image sub-sizes may be overwritten. |
9 | 2284 |
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
|
2285 |
$new_number = (int) $number + 1; |
9 | 2286 |
$filename = str_replace( array( "-$number$ext", "$number$ext" ), "-$new_number$ext", $filename ); |
2287 |
$filename2 = str_replace( array( "-$number$ext2", "$number$ext2" ), "-$new_number$ext2", $filename2 ); |
|
2288 |
$number = $new_number; |
|
0 | 2289 |
} |
7
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2292 |
* Filters the result when generating a unique file name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2293 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2294 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2295 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2296 |
* @param string $filename Unique file name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2297 |
* @param string $ext File extension, eg. ".png". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2298 |
* @param string $dir Directory path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2299 |
* @param callable|null $unique_filename_callback Callback function that generates the unique file name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2300 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2301 |
return apply_filters( 'wp_unique_filename', $filename2, $ext, $dir, $unique_filename_callback ); |
0 | 2302 |
} |
2303 |
||
2304 |
while ( file_exists( $dir . "/$filename" ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2305 |
$new_number = (int) $number + 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2306 |
if ( '' == "$number$ext" ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2307 |
$filename = "$filename-" . $new_number; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2308 |
} else { |
9 | 2309 |
$filename = str_replace( array( "-$number$ext", "$number$ext" ), '-' . $new_number . $ext, $filename ); |
7
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 |
$number = $new_number; |
0 | 2312 |
} |
2313 |
} |
|
2314 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2315 |
/** This filter is documented in wp-includes/functions.php */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2316 |
return apply_filters( 'wp_unique_filename', $filename, $ext, $dir, $unique_filename_callback ); |
0 | 2317 |
} |
2318 |
||
2319 |
/** |
|
2320 |
* Create a file in the upload folder with given content. |
|
2321 |
* |
|
2322 |
* If there is an error, then the key 'error' will exist with the error message. |
|
2323 |
* If success, then the key 'file' will have the unique file path, the 'url' key |
|
2324 |
* will have the link to the new file. and the 'error' key will be set to false. |
|
2325 |
* |
|
2326 |
* This function will not move an uploaded file to the upload folder. It will |
|
2327 |
* create a new file with the content in $bits parameter. If you move the upload |
|
2328 |
* file, read the content of the uploaded file, and then you can give the |
|
2329 |
* filename and content to this function, which will add it to the upload |
|
2330 |
* folder. |
|
2331 |
* |
|
2332 |
* The permissions will be set on the new file automatically by this function. |
|
2333 |
* |
|
2334 |
* @since 2.0.0 |
|
2335 |
* |
|
5 | 2336 |
* @param string $name Filename. |
2337 |
* @param null|string $deprecated Never used. Set to null. |
|
2338 |
* @param mixed $bits File content |
|
2339 |
* @param string $time Optional. Time formatted in 'yyyy/mm'. Default null. |
|
0 | 2340 |
* @return array |
2341 |
*/ |
|
2342 |
function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { |
|
9 | 2343 |
if ( ! empty( $deprecated ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2344 |
_deprecated_argument( __FUNCTION__, '2.0.0' ); |
9 | 2345 |
} |
2346 |
||
2347 |
if ( empty( $name ) ) { |
|
0 | 2348 |
return array( 'error' => __( 'Empty filename' ) ); |
9 | 2349 |
} |
0 | 2350 |
|
2351 |
$wp_filetype = wp_check_filetype( $name ); |
|
9 | 2352 |
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
|
2353 |
return array( 'error' => __( 'Sorry, this file type is not permitted for security reasons.' ) ); |
9 | 2354 |
} |
0 | 2355 |
|
2356 |
$upload = wp_upload_dir( $time ); |
|
2357 |
||
9 | 2358 |
if ( $upload['error'] !== false ) { |
0 | 2359 |
return $upload; |
9 | 2360 |
} |
0 | 2361 |
|
5 | 2362 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2363 |
* Filters whether to treat the upload bits as an error. |
5 | 2364 |
* |
2365 |
* Passing a non-array to the filter will effectively short-circuit preparing |
|
2366 |
* the upload bits, returning that value instead. |
|
2367 |
* |
|
2368 |
* @since 3.0.0 |
|
2369 |
* |
|
2370 |
* @param mixed $upload_bits_error An array of upload bits data, or a non-array error to return. |
|
2371 |
*/ |
|
9 | 2372 |
$upload_bits_error = apply_filters( |
2373 |
'wp_upload_bits', |
|
2374 |
array( |
|
2375 |
'name' => $name, |
|
2376 |
'bits' => $bits, |
|
2377 |
'time' => $time, |
|
2378 |
) |
|
2379 |
); |
|
2380 |
if ( ! is_array( $upload_bits_error ) ) { |
|
2381 |
$upload['error'] = $upload_bits_error; |
|
0 | 2382 |
return $upload; |
2383 |
} |
|
2384 |
||
2385 |
$filename = wp_unique_filename( $upload['path'], $name ); |
|
2386 |
||
2387 |
$new_file = $upload['path'] . "/$filename"; |
|
2388 |
if ( ! wp_mkdir_p( dirname( $new_file ) ) ) { |
|
9 | 2389 |
if ( 0 === strpos( $upload['basedir'], ABSPATH ) ) { |
0 | 2390 |
$error_path = str_replace( ABSPATH, '', $upload['basedir'] ) . $upload['subdir']; |
9 | 2391 |
} else { |
2392 |
$error_path = wp_basename( $upload['basedir'] ) . $upload['subdir']; |
|
2393 |
} |
|
0 | 2394 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2395 |
$message = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2396 |
/* translators: %s: directory path */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2397 |
__( '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
|
2398 |
$error_path |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2399 |
); |
0 | 2400 |
return array( 'error' => $message ); |
2401 |
} |
|
2402 |
||
2403 |
$ifp = @ fopen( $new_file, 'wb' ); |
|
9 | 2404 |
if ( ! $ifp ) { |
0 | 2405 |
return array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) ); |
9 | 2406 |
} |
0 | 2407 |
|
2408 |
@fwrite( $ifp, $bits ); |
|
2409 |
fclose( $ifp ); |
|
2410 |
clearstatcache(); |
|
2411 |
||
2412 |
// Set correct file permissions |
|
9 | 2413 |
$stat = @ stat( dirname( $new_file ) ); |
0 | 2414 |
$perms = $stat['mode'] & 0007777; |
2415 |
$perms = $perms & 0000666; |
|
2416 |
@ chmod( $new_file, $perms ); |
|
2417 |
clearstatcache(); |
|
2418 |
||
2419 |
// Compute the URL |
|
2420 |
$url = $upload['url'] . "/$filename"; |
|
2421 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2422 |
/** This filter is documented in wp-admin/includes/file.php */ |
9 | 2423 |
return apply_filters( |
2424 |
'wp_handle_upload', |
|
2425 |
array( |
|
2426 |
'file' => $new_file, |
|
2427 |
'url' => $url, |
|
2428 |
'type' => $wp_filetype['type'], |
|
2429 |
'error' => false, |
|
2430 |
), |
|
2431 |
'sideload' |
|
2432 |
); |
|
0 | 2433 |
} |
2434 |
||
2435 |
/** |
|
2436 |
* Retrieve the file type based on the extension name. |
|
2437 |
* |
|
2438 |
* @since 2.5.0 |
|
2439 |
* |
|
2440 |
* @param string $ext The extension to search. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2441 |
* @return string|void The file type, example: audio, video, document, spreadsheet, etc. |
0 | 2442 |
*/ |
2443 |
function wp_ext2type( $ext ) { |
|
2444 |
$ext = strtolower( $ext ); |
|
5 | 2445 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2446 |
$ext2type = wp_get_ext_types(); |
9 | 2447 |
foreach ( $ext2type as $type => $exts ) { |
2448 |
if ( in_array( $ext, $exts ) ) { |
|
0 | 2449 |
return $type; |
9 | 2450 |
} |
2451 |
} |
|
0 | 2452 |
} |
2453 |
||
2454 |
/** |
|
2455 |
* Retrieve the file type from the file name. |
|
2456 |
* |
|
2457 |
* You can optionally define the mime array, if needed. |
|
2458 |
* |
|
2459 |
* @since 2.0.4 |
|
2460 |
* |
|
2461 |
* @param string $filename File name or path. |
|
5 | 2462 |
* @param array $mimes Optional. Key is the file extension with value as the mime type. |
0 | 2463 |
* @return array Values with extension first and mime type. |
2464 |
*/ |
|
2465 |
function wp_check_filetype( $filename, $mimes = null ) { |
|
9 | 2466 |
if ( empty( $mimes ) ) { |
0 | 2467 |
$mimes = get_allowed_mime_types(); |
9 | 2468 |
} |
0 | 2469 |
$type = false; |
9 | 2470 |
$ext = false; |
0 | 2471 |
|
2472 |
foreach ( $mimes as $ext_preg => $mime_match ) { |
|
2473 |
$ext_preg = '!\.(' . $ext_preg . ')$!i'; |
|
2474 |
if ( preg_match( $ext_preg, $filename, $ext_matches ) ) { |
|
2475 |
$type = $mime_match; |
|
9 | 2476 |
$ext = $ext_matches[1]; |
0 | 2477 |
break; |
2478 |
} |
|
2479 |
} |
|
2480 |
||
2481 |
return compact( 'ext', 'type' ); |
|
2482 |
} |
|
2483 |
||
2484 |
/** |
|
2485 |
* Attempt to determine the real file type of a file. |
|
5 | 2486 |
* |
0 | 2487 |
* If unable to, the file name extension will be used to determine type. |
2488 |
* |
|
2489 |
* If it's determined that the extension does not match the file's real type, |
|
2490 |
* then the "proper_filename" value will be set with a proper filename and extension. |
|
2491 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2492 |
* Currently this function only supports renaming images validated via wp_get_image_mime(). |
0 | 2493 |
* |
2494 |
* @since 3.0.0 |
|
2495 |
* |
|
5 | 2496 |
* @param string $file Full path to the file. |
2497 |
* @param string $filename The name of the file (may differ from $file due to $file being |
|
2498 |
* in a tmp directory). |
|
2499 |
* @param array $mimes Optional. Key is the file extension with value as the mime type. |
|
2500 |
* @return array Values for the extension, MIME, and either a corrected filename or false |
|
2501 |
* if original $filename is valid. |
|
0 | 2502 |
*/ |
2503 |
function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { |
|
2504 |
$proper_filename = false; |
|
2505 |
||
2506 |
// Do basic extension validation and MIME mapping |
|
2507 |
$wp_filetype = wp_check_filetype( $filename, $mimes ); |
|
9 | 2508 |
$ext = $wp_filetype['ext']; |
2509 |
$type = $wp_filetype['type']; |
|
0 | 2510 |
|
2511 |
// We can't do any further validation without a file to work with |
|
5 | 2512 |
if ( ! file_exists( $file ) ) { |
0 | 2513 |
return compact( 'ext', 'type', 'proper_filename' ); |
5 | 2514 |
} |
0 | 2515 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2516 |
$real_mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2517 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2518 |
// Validate image types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2519 |
if ( $type && 0 === strpos( $type, 'image/' ) ) { |
0 | 2520 |
|
2521 |
// 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
|
2522 |
$real_mime = wp_get_image_mime( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2523 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2524 |
if ( $real_mime && $real_mime != $type ) { |
5 | 2525 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2526 |
* Filters the list mapping image mime types to their respective extensions. |
5 | 2527 |
* |
2528 |
* @since 3.0.0 |
|
2529 |
* |
|
2530 |
* @param array $mime_to_ext Array of image mime types and their matching extensions. |
|
2531 |
*/ |
|
9 | 2532 |
$mime_to_ext = apply_filters( |
2533 |
'getimagesize_mimes_to_exts', |
|
2534 |
array( |
|
2535 |
'image/jpeg' => 'jpg', |
|
2536 |
'image/png' => 'png', |
|
2537 |
'image/gif' => 'gif', |
|
2538 |
'image/bmp' => 'bmp', |
|
2539 |
'image/tiff' => 'tif', |
|
2540 |
) |
|
2541 |
); |
|
0 | 2542 |
|
2543 |
// 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
|
2544 |
if ( ! empty( $mime_to_ext[ $real_mime ] ) ) { |
0 | 2545 |
$filename_parts = explode( '.', $filename ); |
2546 |
array_pop( $filename_parts ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2547 |
$filename_parts[] = $mime_to_ext[ $real_mime ]; |
9 | 2548 |
$new_filename = implode( '.', $filename_parts ); |
0 | 2549 |
|
5 | 2550 |
if ( $new_filename != $filename ) { |
0 | 2551 |
$proper_filename = $new_filename; // Mark that it changed |
5 | 2552 |
} |
0 | 2553 |
// Redefine the extension / MIME |
2554 |
$wp_filetype = wp_check_filetype( $new_filename, $mimes ); |
|
9 | 2555 |
$ext = $wp_filetype['ext']; |
2556 |
$type = $wp_filetype['type']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2557 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2558 |
// Reset $real_mime and try validating again. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2559 |
$real_mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2560 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2561 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2562 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2563 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2564 |
// 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
|
2565 |
if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) { |
9 | 2566 |
$finfo = finfo_open( FILEINFO_MIME_TYPE ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2567 |
$real_mime = finfo_file( $finfo, $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2568 |
finfo_close( $finfo ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2569 |
|
9 | 2570 |
// fileinfo often misidentifies obscure files as one of these types |
2571 |
$nonspecific_types = array( |
|
2572 |
'application/octet-stream', |
|
2573 |
'application/encrypted', |
|
2574 |
'application/CDFV2-encrypted', |
|
2575 |
'application/zip', |
|
2576 |
); |
|
2577 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2578 |
/* |
9 | 2579 |
* If $real_mime doesn't match the content type we're expecting from the file's extension, |
2580 |
* we need to do some additional vetting. Media types and those listed in $nonspecific_types are |
|
2581 |
* 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
|
2582 |
*/ |
9 | 2583 |
if ( in_array( $real_mime, $nonspecific_types, true ) ) { |
2584 |
// File is a non-specific binary type. That's ok if it's a type that generally tends to be binary. |
|
2585 |
if ( ! in_array( substr( $type, 0, strcspn( $type, '/' ) ), array( 'application', 'video', 'audio' ) ) ) { |
|
2586 |
$type = $ext = false; |
|
2587 |
} |
|
2588 |
} elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) { |
|
2589 |
/* |
|
2590 |
* For these types, only the major type must match the real value. |
|
2591 |
* This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip, |
|
2592 |
* and some media files are commonly named with the wrong extension (.mov instead of .mp4) |
|
2593 |
*/ |
|
2594 |
if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2595 |
$type = $ext = false; |
0 | 2596 |
} |
9 | 2597 |
} elseif ( 'text/plain' === $real_mime ) { |
2598 |
// A few common file types are occasionally detected as text/plain; allow those. |
|
2599 |
if ( ! in_array( |
|
2600 |
$type, |
|
2601 |
array( |
|
2602 |
'text/plain', |
|
2603 |
'text/csv', |
|
2604 |
'text/richtext', |
|
2605 |
'text/tsv', |
|
2606 |
'text/vtt', |
|
2607 |
) |
|
2608 |
) |
|
2609 |
) { |
|
2610 |
$type = $ext = false; |
|
2611 |
} |
|
2612 |
} elseif ( 'text/rtf' === $real_mime ) { |
|
2613 |
// Special casing for RTF files. |
|
2614 |
if ( ! in_array( |
|
2615 |
$type, |
|
2616 |
array( |
|
2617 |
'text/rtf', |
|
2618 |
'text/plain', |
|
2619 |
'application/rtf', |
|
2620 |
) |
|
2621 |
) |
|
2622 |
) { |
|
2623 |
$type = $ext = false; |
|
2624 |
} |
|
2625 |
} else { |
|
2626 |
if ( $type !== $real_mime ) { |
|
2627 |
/* |
|
2628 |
* Everything else including image/* and application/*: |
|
2629 |
* If the real content type doesn't match the file extension, assume it's dangerous. |
|
2630 |
*/ |
|
2631 |
$type = $ext = false; |
|
2632 |
} |
|
2633 |
} |
|
2634 |
} |
|
2635 |
||
2636 |
// The mime type must be allowed |
|
2637 |
if ( $type ) { |
|
2638 |
$allowed = get_allowed_mime_types(); |
|
2639 |
||
2640 |
if ( ! in_array( $type, $allowed ) ) { |
|
2641 |
$type = $ext = false; |
|
0 | 2642 |
} |
2643 |
} |
|
2644 |
||
5 | 2645 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2646 |
* Filters the "real" file type of the given file. |
5 | 2647 |
* |
2648 |
* @since 3.0.0 |
|
9 | 2649 |
* @since 5.1.0 The $real_mime parameter was added. |
5 | 2650 |
* |
9 | 2651 |
* @param array $wp_check_filetype_and_ext File data array containing 'ext', 'type', and |
2652 |
* 'proper_filename' keys. |
|
2653 |
* @param string $file Full path to the file. |
|
2654 |
* @param string $filename The name of the file (may differ from $file due to |
|
2655 |
* $file being in a tmp directory). |
|
2656 |
* @param array $mimes Key is the file extension with value as the mime type. |
|
2657 |
* @param string|bool $real_mime The actual mime type or false if the type cannot be determined. |
|
5 | 2658 |
*/ |
9 | 2659 |
return apply_filters( 'wp_check_filetype_and_ext', compact( 'ext', 'type', 'proper_filename' ), $file, $filename, $mimes, $real_mime ); |
0 | 2660 |
} |
2661 |
||
2662 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2663 |
* Returns the real mime type of an image file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2664 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2665 |
* 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
|
2666 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2667 |
* @since 4.7.1 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2668 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2669 |
* @param string $file Full path to the file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2670 |
* @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
|
2671 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2672 |
function wp_get_image_mime( $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2673 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2674 |
* 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
|
2675 |
* 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
|
2676 |
* we assume the file could not be validated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2677 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2678 |
try { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2679 |
if ( is_callable( 'exif_imagetype' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2680 |
$imagetype = exif_imagetype( $file ); |
9 | 2681 |
$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
|
2682 |
} elseif ( function_exists( 'getimagesize' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2683 |
$imagesize = getimagesize( $file ); |
9 | 2684 |
$mime = ( isset( $imagesize['mime'] ) ) ? $imagesize['mime'] : false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2685 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2686 |
$mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2687 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2688 |
} catch ( Exception $e ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2689 |
$mime = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2690 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2691 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2692 |
return $mime; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2693 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2694 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2695 |
/** |
0 | 2696 |
* Retrieve list of mime types and file extensions. |
2697 |
* |
|
2698 |
* @since 3.5.0 |
|
5 | 2699 |
* @since 4.2.0 Support was added for GIMP (xcf) files. |
0 | 2700 |
* |
2701 |
* @return array Array of mime types keyed by the file extension regex corresponding to those types. |
|
2702 |
*/ |
|
2703 |
function wp_get_mime_types() { |
|
5 | 2704 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2705 |
* Filters the list of mime types and file extensions. |
5 | 2706 |
* |
2707 |
* 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
|
2708 |
* mime types, use the {@see 'upload_mimes'} filter. |
5 | 2709 |
* |
2710 |
* @since 3.5.0 |
|
2711 |
* |
|
2712 |
* @param array $wp_get_mime_types Mime types keyed by the file extension regex |
|
2713 |
* corresponding to those types. |
|
2714 |
*/ |
|
9 | 2715 |
return apply_filters( |
2716 |
'mime_types', |
|
2717 |
array( |
|
2718 |
// Image formats. |
|
2719 |
'jpg|jpeg|jpe' => 'image/jpeg', |
|
2720 |
'gif' => 'image/gif', |
|
2721 |
'png' => 'image/png', |
|
2722 |
'bmp' => 'image/bmp', |
|
2723 |
'tiff|tif' => 'image/tiff', |
|
2724 |
'ico' => 'image/x-icon', |
|
2725 |
// Video formats. |
|
2726 |
'asf|asx' => 'video/x-ms-asf', |
|
2727 |
'wmv' => 'video/x-ms-wmv', |
|
2728 |
'wmx' => 'video/x-ms-wmx', |
|
2729 |
'wm' => 'video/x-ms-wm', |
|
2730 |
'avi' => 'video/avi', |
|
2731 |
'divx' => 'video/divx', |
|
2732 |
'flv' => 'video/x-flv', |
|
2733 |
'mov|qt' => 'video/quicktime', |
|
2734 |
'mpeg|mpg|mpe' => 'video/mpeg', |
|
2735 |
'mp4|m4v' => 'video/mp4', |
|
2736 |
'ogv' => 'video/ogg', |
|
2737 |
'webm' => 'video/webm', |
|
2738 |
'mkv' => 'video/x-matroska', |
|
2739 |
'3gp|3gpp' => 'video/3gpp', // Can also be audio |
|
2740 |
'3g2|3gp2' => 'video/3gpp2', // Can also be audio |
|
2741 |
// Text formats. |
|
2742 |
'txt|asc|c|cc|h|srt' => 'text/plain', |
|
2743 |
'csv' => 'text/csv', |
|
2744 |
'tsv' => 'text/tab-separated-values', |
|
2745 |
'ics' => 'text/calendar', |
|
2746 |
'rtx' => 'text/richtext', |
|
2747 |
'css' => 'text/css', |
|
2748 |
'htm|html' => 'text/html', |
|
2749 |
'vtt' => 'text/vtt', |
|
2750 |
'dfxp' => 'application/ttaf+xml', |
|
2751 |
// Audio formats. |
|
2752 |
'mp3|m4a|m4b' => 'audio/mpeg', |
|
2753 |
'aac' => 'audio/aac', |
|
2754 |
'ra|ram' => 'audio/x-realaudio', |
|
2755 |
'wav' => 'audio/wav', |
|
2756 |
'ogg|oga' => 'audio/ogg', |
|
2757 |
'flac' => 'audio/flac', |
|
2758 |
'mid|midi' => 'audio/midi', |
|
2759 |
'wma' => 'audio/x-ms-wma', |
|
2760 |
'wax' => 'audio/x-ms-wax', |
|
2761 |
'mka' => 'audio/x-matroska', |
|
2762 |
// Misc application formats. |
|
2763 |
'rtf' => 'application/rtf', |
|
2764 |
'js' => 'application/javascript', |
|
2765 |
'pdf' => 'application/pdf', |
|
2766 |
'swf' => 'application/x-shockwave-flash', |
|
2767 |
'class' => 'application/java', |
|
2768 |
'tar' => 'application/x-tar', |
|
2769 |
'zip' => 'application/zip', |
|
2770 |
'gz|gzip' => 'application/x-gzip', |
|
2771 |
'rar' => 'application/rar', |
|
2772 |
'7z' => 'application/x-7z-compressed', |
|
2773 |
'exe' => 'application/x-msdownload', |
|
2774 |
'psd' => 'application/octet-stream', |
|
2775 |
'xcf' => 'application/octet-stream', |
|
2776 |
// MS Office formats. |
|
2777 |
'doc' => 'application/msword', |
|
2778 |
'pot|pps|ppt' => 'application/vnd.ms-powerpoint', |
|
2779 |
'wri' => 'application/vnd.ms-write', |
|
2780 |
'xla|xls|xlt|xlw' => 'application/vnd.ms-excel', |
|
2781 |
'mdb' => 'application/vnd.ms-access', |
|
2782 |
'mpp' => 'application/vnd.ms-project', |
|
2783 |
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
2784 |
'docm' => 'application/vnd.ms-word.document.macroEnabled.12', |
|
2785 |
'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
|
2786 |
'dotm' => 'application/vnd.ms-word.template.macroEnabled.12', |
|
2787 |
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
2788 |
'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12', |
|
2789 |
'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
|
2790 |
'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
|
2791 |
'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12', |
|
2792 |
'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
|
2793 |
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
2794 |
'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', |
|
2795 |
'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
2796 |
'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', |
|
2797 |
'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
|
2798 |
'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', |
|
2799 |
'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', |
|
2800 |
'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
|
2801 |
'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12', |
|
2802 |
'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote', |
|
2803 |
'oxps' => 'application/oxps', |
|
2804 |
'xps' => 'application/vnd.ms-xpsdocument', |
|
2805 |
// OpenOffice formats. |
|
2806 |
'odt' => 'application/vnd.oasis.opendocument.text', |
|
2807 |
'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
2808 |
'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
2809 |
'odg' => 'application/vnd.oasis.opendocument.graphics', |
|
2810 |
'odc' => 'application/vnd.oasis.opendocument.chart', |
|
2811 |
'odb' => 'application/vnd.oasis.opendocument.database', |
|
2812 |
'odf' => 'application/vnd.oasis.opendocument.formula', |
|
2813 |
// WordPerfect formats. |
|
2814 |
'wp|wpd' => 'application/wordperfect', |
|
2815 |
// iWork formats. |
|
2816 |
'key' => 'application/vnd.apple.keynote', |
|
2817 |
'numbers' => 'application/vnd.apple.numbers', |
|
2818 |
'pages' => 'application/vnd.apple.pages', |
|
2819 |
) |
|
2820 |
); |
|
0 | 2821 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2822 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2823 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2824 |
* 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
|
2825 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2826 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2827 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2828 |
* @return array Array of file extensions types keyed by the type of file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2829 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2830 |
function wp_get_ext_types() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2831 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2832 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2833 |
* Filters file type based on the extension name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2834 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2835 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2836 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2837 |
* @see wp_ext2type() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2838 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2839 |
* @param array $ext2type Multi-dimensional array with extensions for a default set |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2840 |
* of file types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2841 |
*/ |
9 | 2842 |
return apply_filters( |
2843 |
'ext2type', |
|
2844 |
array( |
|
2845 |
'image' => array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico' ), |
|
2846 |
'audio' => array( 'aac', 'ac3', 'aif', 'aiff', 'flac', 'm3a', 'm4a', 'm4b', 'mka', 'mp1', 'mp2', 'mp3', 'ogg', 'oga', 'ram', 'wav', 'wma' ), |
|
2847 |
'video' => array( '3g2', '3gp', '3gpp', 'asf', 'avi', 'divx', 'dv', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mpv', 'ogm', 'ogv', 'qt', 'rm', 'vob', 'wmv' ), |
|
2848 |
'document' => array( 'doc', 'docx', 'docm', 'dotm', 'odt', 'pages', 'pdf', 'xps', 'oxps', 'rtf', 'wp', 'wpd', 'psd', 'xcf' ), |
|
2849 |
'spreadsheet' => array( 'numbers', 'ods', 'xls', 'xlsx', 'xlsm', 'xlsb' ), |
|
2850 |
'interactive' => array( 'swf', 'key', 'ppt', 'pptx', 'pptm', 'pps', 'ppsx', 'ppsm', 'sldx', 'sldm', 'odp' ), |
|
2851 |
'text' => array( 'asc', 'csv', 'tsv', 'txt' ), |
|
2852 |
'archive' => array( 'bz2', 'cab', 'dmg', 'gz', 'rar', 'sea', 'sit', 'sqx', 'tar', 'tgz', 'zip', '7z' ), |
|
2853 |
'code' => array( 'css', 'htm', 'html', 'php', 'js' ), |
|
2854 |
) |
|
2855 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2856 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2857 |
|
0 | 2858 |
/** |
2859 |
* Retrieve list of allowed mime types and file extensions. |
|
2860 |
* |
|
2861 |
* @since 2.8.6 |
|
2862 |
* |
|
2863 |
* @param int|WP_User $user Optional. User to check. Defaults to current user. |
|
5 | 2864 |
* @return array Array of mime types keyed by the file extension regex corresponding |
2865 |
* to those types. |
|
0 | 2866 |
*/ |
2867 |
function get_allowed_mime_types( $user = null ) { |
|
2868 |
$t = wp_get_mime_types(); |
|
2869 |
||
2870 |
unset( $t['swf'], $t['exe'] ); |
|
9 | 2871 |
if ( function_exists( 'current_user_can' ) ) { |
0 | 2872 |
$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' ); |
9 | 2873 |
} |
0 | 2874 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2875 |
if ( empty( $unfiltered ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2876 |
unset( $t['htm|html'], $t['js'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2877 |
} |
0 | 2878 |
|
5 | 2879 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2880 |
* Filters list of allowed mime types and file extensions. |
5 | 2881 |
* |
2882 |
* @since 2.0.0 |
|
2883 |
* |
|
2884 |
* @param array $t Mime types keyed by the file extension regex corresponding to |
|
2885 |
* those types. 'swf' and 'exe' removed from full list. 'htm|html' also |
|
2886 |
* removed depending on '$user' capabilities. |
|
2887 |
* @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user). |
|
2888 |
*/ |
|
0 | 2889 |
return apply_filters( 'upload_mimes', $t, $user ); |
2890 |
} |
|
2891 |
||
2892 |
/** |
|
2893 |
* Display "Are You Sure" message to confirm the action being taken. |
|
2894 |
* |
|
5 | 2895 |
* If the action has the nonce explain message, then it will be displayed |
2896 |
* along with the "Are you sure?" message. |
|
2897 |
* |
|
0 | 2898 |
* @since 2.0.4 |
2899 |
* |
|
2900 |
* @param string $action The nonce action. |
|
2901 |
*/ |
|
2902 |
function wp_nonce_ays( $action ) { |
|
2903 |
if ( 'log-out' == $action ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2904 |
$html = sprintf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2905 |
/* translators: %s: site name */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2906 |
__( 'You are attempting to log out of %s' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2907 |
get_bloginfo( 'name' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2908 |
); |
9 | 2909 |
$html .= '</p><p>'; |
5 | 2910 |
$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; |
9 | 2911 |
$html .= sprintf( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2912 |
/* translators: %s: logout URL */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2913 |
__( '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
|
2914 |
wp_logout_url( $redirect_to ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2915 |
); |
0 | 2916 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2917 |
$html = __( 'The link you followed has expired.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2918 |
if ( wp_get_referer() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2919 |
$html .= '</p><p>'; |
9 | 2920 |
$html .= sprintf( |
2921 |
'<a href="%s">%s</a>', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2922 |
esc_url( remove_query_arg( 'updated', wp_get_referer() ) ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2923 |
__( 'Please try again.' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2924 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2925 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2926 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2927 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2928 |
wp_die( $html, __( 'Something went wrong.' ), 403 ); |
0 | 2929 |
} |
2930 |
||
2931 |
/** |
|
9 | 2932 |
* Kills WordPress execution and displays HTML page with an error message. |
0 | 2933 |
* |
5 | 2934 |
* This function complements the `die()` PHP function. The difference is that |
0 | 2935 |
* HTML will be displayed to the user. It is recommended to use this function |
5 | 2936 |
* only when the execution should not continue any further. It is not recommended |
2937 |
* to call this function very often, and try to handle as many errors as possible |
|
2938 |
* silently or more gracefully. |
|
2939 |
* |
|
2940 |
* As a shorthand, the desired HTTP response code may be passed as an integer to |
|
2941 |
* the `$title` parameter (the default title would apply) or the `$args` parameter. |
|
0 | 2942 |
* |
2943 |
* @since 2.0.4 |
|
5 | 2944 |
* @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept |
2945 |
* an integer to be used as the response code. |
|
9 | 2946 |
* @since 5.1.0 The `$link_url`, `$link_text`, and `$exit` arguments were added. |
2947 |
* |
|
2948 |
* @global WP_Query $wp_query Global WP_Query instance. |
|
5 | 2949 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2950 |
* @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
|
2951 |
* 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
|
2952 |
* Default empty. |
5 | 2953 |
* @param string|int $title Optional. Error title. If `$message` is a `WP_Error` object, |
2954 |
* error data with the key 'title' may be used to specify the title. |
|
2955 |
* If `$title` is an integer, then it is treated as the response |
|
2956 |
* code. Default empty. |
|
2957 |
* @param string|array|int $args { |
|
2958 |
* Optional. Arguments to control behavior. If `$args` is an integer, then it is treated |
|
2959 |
* as the response code. Default empty array. |
|
2960 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2961 |
* @type int $response The HTTP response code. Default 200 for Ajax requests, 500 otherwise. |
9 | 2962 |
* @type string $link_url A URL to include a link to. Only works in combination with $link_text. |
2963 |
* Default empty string. |
|
2964 |
* @type string $link_text A label for the link to include. Only works in combination with $link_url. |
|
2965 |
* Default empty string. |
|
5 | 2966 |
* @type bool $back_link Whether to include a link to go back. Default false. |
2967 |
* @type string $text_direction The text direction. This is only useful internally, when WordPress |
|
2968 |
* is still loading and the site's locale is not set up yet. Accepts 'rtl'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2969 |
* Default is the value of is_rtl(). |
9 | 2970 |
* @type string $code Error code to use. Default is 'wp_die', or the main error code if $message |
2971 |
* is a WP_Error. |
|
2972 |
* @type bool $exit Whether to exit the process after completion. Default true. |
|
5 | 2973 |
* } |
0 | 2974 |
*/ |
2975 |
function wp_die( $message = '', $title = '', $args = array() ) { |
|
9 | 2976 |
global $wp_query; |
5 | 2977 |
|
2978 |
if ( is_int( $args ) ) { |
|
2979 |
$args = array( 'response' => $args ); |
|
2980 |
} elseif ( is_int( $title ) ) { |
|
2981 |
$args = array( 'response' => $title ); |
|
2982 |
$title = ''; |
|
2983 |
} |
|
2984 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2985 |
if ( wp_doing_ajax() ) { |
5 | 2986 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2987 |
* Filters the callback for killing WordPress execution for Ajax requests. |
5 | 2988 |
* |
2989 |
* @since 3.4.0 |
|
2990 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2991 |
* @param callable $function Callback function name. |
5 | 2992 |
*/ |
0 | 2993 |
$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' ); |
9 | 2994 |
} elseif ( wp_is_json_request() ) { |
2995 |
/** |
|
2996 |
* Filters the callback for killing WordPress execution for JSON requests. |
|
2997 |
* |
|
2998 |
* @since 5.1.0 |
|
2999 |
* |
|
3000 |
* @param callable $function Callback function name. |
|
3001 |
*/ |
|
3002 |
$function = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' ); |
|
3003 |
} elseif ( wp_is_jsonp_request() ) { |
|
3004 |
/** |
|
3005 |
* Filters the callback for killing WordPress execution for JSONP requests. |
|
3006 |
* |
|
3007 |
* @since 5.2.0 |
|
3008 |
* |
|
3009 |
* @param callable $function Callback function name. |
|
3010 |
*/ |
|
3011 |
$function = apply_filters( 'wp_die_jsonp_handler', '_jsonp_wp_die_handler' ); |
|
5 | 3012 |
} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { |
3013 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3014 |
* Filters the callback for killing WordPress execution for XML-RPC requests. |
5 | 3015 |
* |
3016 |
* @since 3.4.0 |
|
3017 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3018 |
* @param callable $function Callback function name. |
5 | 3019 |
*/ |
0 | 3020 |
$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' ); |
9 | 3021 |
} elseif ( wp_is_xml_request() |
3022 |
|| isset( $wp_query ) && |
|
3023 |
( function_exists( 'is_feed' ) && is_feed() |
|
3024 |
|| function_exists( 'is_comment_feed' ) && is_comment_feed() |
|
3025 |
|| function_exists( 'is_trackback' ) && is_trackback() ) ) { |
|
3026 |
/** |
|
3027 |
* Filters the callback for killing WordPress execution for XML requests. |
|
3028 |
* |
|
3029 |
* @since 5.2.0 |
|
3030 |
* |
|
3031 |
* @param callable $function Callback function name. |
|
3032 |
*/ |
|
3033 |
$function = apply_filters( 'wp_die_xml_handler', '_xml_wp_die_handler' ); |
|
5 | 3034 |
} else { |
3035 |
/** |
|
9 | 3036 |
* Filters the callback for killing WordPress execution for all non-Ajax, non-JSON, non-XML requests. |
5 | 3037 |
* |
3038 |
* @since 3.0.0 |
|
3039 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3040 |
* @param callable $function Callback function name. |
5 | 3041 |
*/ |
0 | 3042 |
$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' ); |
5 | 3043 |
} |
0 | 3044 |
|
3045 |
call_user_func( $function, $message, $title, $args ); |
|
3046 |
} |
|
3047 |
||
3048 |
/** |
|
9 | 3049 |
* Kills WordPress execution and displays HTML page with an error message. |
3050 |
* |
|
3051 |
* This is the default handler for wp_die(). If you want a custom one, |
|
3052 |
* you can override this using the {@see 'wp_die_handler'} filter in wp_die(). |
|
0 | 3053 |
* |
3054 |
* @since 3.0.0 |
|
3055 |
* @access private |
|
3056 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3057 |
* @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
|
3058 |
* @param string $title Optional. Error title. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3059 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
0 | 3060 |
*/ |
3061 |
function _default_wp_die_handler( $message, $title = '', $args = array() ) { |
|
9 | 3062 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
3063 |
||
3064 |
if ( is_string( $message ) ) { |
|
3065 |
if ( ! empty( $r['additional_errors'] ) ) { |
|
3066 |
$message = array_merge( |
|
3067 |
array( $message ), |
|
3068 |
wp_list_pluck( $r['additional_errors'], 'message' ) |
|
3069 |
); |
|
3070 |
$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $message ) . "</li>\n\t</ul>"; |
|
3071 |
} else { |
|
3072 |
$message = "<p>$message</p>"; |
|
0 | 3073 |
} |
9 | 3074 |
} |
3075 |
||
3076 |
$have_gettext = function_exists( '__' ); |
|
3077 |
||
3078 |
if ( ! empty( $r['link_url'] ) && ! empty( $r['link_text'] ) ) { |
|
3079 |
$link_url = $r['link_url']; |
|
3080 |
if ( function_exists( 'esc_url' ) ) { |
|
3081 |
$link_url = esc_url( $link_url ); |
|
5 | 3082 |
} |
9 | 3083 |
$link_text = $r['link_text']; |
3084 |
$message .= "\n<p><a href='{$link_url}'>{$link_text}</a></p>"; |
|
0 | 3085 |
} |
3086 |
||
3087 |
if ( isset( $r['back_link'] ) && $r['back_link'] ) { |
|
9 | 3088 |
$back_text = $have_gettext ? __( '« Back' ) : '« Back'; |
3089 |
$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>"; |
|
0 | 3090 |
} |
3091 |
||
3092 |
if ( ! did_action( 'admin_head' ) ) : |
|
9 | 3093 |
if ( ! headers_sent() ) { |
3094 |
header( 'Content-Type: text/html; charset=utf-8' ); |
|
0 | 3095 |
status_header( $r['response'] ); |
3096 |
nocache_headers(); |
|
3097 |
} |
|
3098 |
||
9 | 3099 |
$text_direction = $r['text_direction']; |
3100 |
if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) { |
|
3101 |
$dir_attr = get_language_attributes(); |
|
3102 |
} else { |
|
3103 |
$dir_attr = "dir='$text_direction'"; |
|
3104 |
} |
|
3105 |
?> |
|
0 | 3106 |
<!DOCTYPE html> |
9 | 3107 |
<html xmlns="http://www.w3.org/1999/xhtml" <?php echo $dir_attr; ?>> |
0 | 3108 |
<head> |
3109 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3110 |
<meta name="viewport" content="width=device-width"> |
9 | 3111 |
<?php |
3112 |
if ( function_exists( 'wp_no_robots' ) ) { |
|
3113 |
wp_no_robots(); |
|
3114 |
} |
|
3115 |
?> |
|
3116 |
<title><?php echo $title; ?></title> |
|
0 | 3117 |
<style type="text/css"> |
3118 |
html { |
|
5 | 3119 |
background: #f1f1f1; |
0 | 3120 |
} |
3121 |
body { |
|
3122 |
background: #fff; |
|
5 | 3123 |
color: #444; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3124 |
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
0 | 3125 |
margin: 2em auto; |
3126 |
padding: 1em 2em; |
|
3127 |
max-width: 700px; |
|
9 | 3128 |
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.13); |
3129 |
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.13); |
|
0 | 3130 |
} |
3131 |
h1 { |
|
3132 |
border-bottom: 1px solid #dadada; |
|
3133 |
clear: both; |
|
3134 |
color: #666; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3135 |
font-size: 24px; |
0 | 3136 |
margin: 30px 0 0 0; |
3137 |
padding: 0; |
|
3138 |
padding-bottom: 7px; |
|
3139 |
} |
|
3140 |
#error-page { |
|
3141 |
margin-top: 50px; |
|
3142 |
} |
|
3143 |
#error-page p { |
|
3144 |
font-size: 14px; |
|
3145 |
line-height: 1.5; |
|
3146 |
margin: 25px 0 20px; |
|
3147 |
} |
|
3148 |
#error-page code { |
|
3149 |
font-family: Consolas, Monaco, monospace; |
|
3150 |
} |
|
3151 |
ul li { |
|
3152 |
margin-bottom: 10px; |
|
3153 |
font-size: 14px ; |
|
3154 |
} |
|
3155 |
a { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3156 |
color: #0073aa; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3157 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3158 |
a:hover, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3159 |
a:active { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3160 |
color: #00a0d2; |
0 | 3161 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3162 |
a:focus { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3163 |
color: #124964; |
9 | 3164 |
-webkit-box-shadow: |
3165 |
0 0 0 1px #5b9dd9, |
|
3166 |
0 0 2px 1px rgba(30, 140, 190, 0.8); |
|
3167 |
box-shadow: |
|
3168 |
0 0 0 1px #5b9dd9, |
|
3169 |
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
|
3170 |
outline: none; |
0 | 3171 |
} |
3172 |
.button { |
|
5 | 3173 |
background: #f7f7f7; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3174 |
border: 1px solid #ccc; |
5 | 3175 |
color: #555; |
0 | 3176 |
display: inline-block; |
3177 |
text-decoration: none; |
|
5 | 3178 |
font-size: 13px; |
3179 |
line-height: 26px; |
|
3180 |
height: 28px; |
|
0 | 3181 |
margin: 0; |
3182 |
padding: 0 10px 1px; |
|
3183 |
cursor: pointer; |
|
3184 |
-webkit-border-radius: 3px; |
|
5 | 3185 |
-webkit-appearance: none; |
0 | 3186 |
border-radius: 3px; |
3187 |
white-space: nowrap; |
|
3188 |
-webkit-box-sizing: border-box; |
|
3189 |
-moz-box-sizing: border-box; |
|
3190 |
box-sizing: border-box; |
|
5 | 3191 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3192 |
-webkit-box-shadow: 0 1px 0 #ccc; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3193 |
box-shadow: 0 1px 0 #ccc; |
9 | 3194 |
vertical-align: top; |
0 | 3195 |
} |
3196 |
||
3197 |
.button.button-large { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3198 |
height: 30px; |
0 | 3199 |
line-height: 28px; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3200 |
padding: 0 12px 2px; |
0 | 3201 |
} |
3202 |
||
3203 |
.button:hover, |
|
3204 |
.button:focus { |
|
5 | 3205 |
background: #fafafa; |
0 | 3206 |
border-color: #999; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3207 |
color: #23282d; |
0 | 3208 |
} |
3209 |
||
9 | 3210 |
.button:focus { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3211 |
border-color: #5b9dd9; |
9 | 3212 |
-webkit-box-shadow: 0 0 3px rgba(0, 115, 170, 0.8); |
3213 |
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
|
3214 |
outline: none; |
0 | 3215 |
} |
3216 |
||
3217 |
.button:active { |
|
3218 |
background: #eee; |
|
3219 |
border-color: #999; |
|
9 | 3220 |
-webkit-box-shadow: inset 0 2px 5px -3px rgba(0, 0, 0, 0.5); |
3221 |
box-shadow: inset 0 2px 5px -3px rgba(0, 0, 0, 0.5); |
|
3222 |
-webkit-transform: translateY(1px); |
|
3223 |
-ms-transform: translateY(1px); |
|
3224 |
transform: translateY(1px); |
|
0 | 3225 |
} |
3226 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3227 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3228 |
if ( 'rtl' == $text_direction ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3229 |
echo 'body { font-family: Tahoma, Arial; }'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3230 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3231 |
?> |
0 | 3232 |
</style> |
3233 |
</head> |
|
3234 |
<body id="error-page"> |
|
3235 |
<?php endif; // ! did_action( 'admin_head' ) ?> |
|
3236 |
<?php echo $message; ?> |
|
3237 |
</body> |
|
3238 |
</html> |
|
9 | 3239 |
<?php |
3240 |
if ( $r['exit'] ) { |
|
3241 |
die(); |
|
3242 |
} |
|
3243 |
} |
|
3244 |
||
3245 |
/** |
|
3246 |
* Kills WordPress execution and displays Ajax response with an error message. |
|
3247 |
* |
|
3248 |
* This is the handler for wp_die() when processing Ajax requests. |
|
3249 |
* |
|
3250 |
* @since 3.4.0 |
|
3251 |
* @access private |
|
3252 |
* |
|
3253 |
* @param string $message Error message. |
|
3254 |
* @param string $title Optional. Error title (unused). Default empty. |
|
3255 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3256 |
*/ |
|
3257 |
function _ajax_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3258 |
// Set default 'response' to 200 for AJAX requests. |
|
3259 |
$args = wp_parse_args( |
|
3260 |
$args, |
|
3261 |
array( 'response' => 200 ) |
|
3262 |
); |
|
3263 |
||
3264 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3265 |
||
3266 |
if ( ! headers_sent() ) { |
|
3267 |
// This is intentional. For backward-compatibility, support passing null here. |
|
3268 |
if ( null !== $args['response'] ) { |
|
3269 |
status_header( $r['response'] ); |
|
3270 |
} |
|
3271 |
nocache_headers(); |
|
3272 |
} |
|
3273 |
||
3274 |
if ( is_scalar( $message ) ) { |
|
3275 |
$message = (string) $message; |
|
3276 |
} else { |
|
3277 |
$message = '0'; |
|
3278 |
} |
|
3279 |
||
3280 |
if ( $r['exit'] ) { |
|
3281 |
die( $message ); |
|
3282 |
} |
|
3283 |
||
3284 |
echo $message; |
|
3285 |
} |
|
3286 |
||
3287 |
/** |
|
3288 |
* Kills WordPress execution and displays JSON response with an error message. |
|
3289 |
* |
|
3290 |
* This is the handler for wp_die() when processing JSON requests. |
|
3291 |
* |
|
3292 |
* @since 5.1.0 |
|
3293 |
* @access private |
|
3294 |
* |
|
3295 |
* @param string $message Error message. |
|
3296 |
* @param string $title Optional. Error title. Default empty. |
|
3297 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3298 |
*/ |
|
3299 |
function _json_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3300 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3301 |
||
3302 |
$data = array( |
|
3303 |
'code' => $r['code'], |
|
3304 |
'message' => $message, |
|
3305 |
'data' => array( |
|
3306 |
'status' => $r['response'], |
|
3307 |
), |
|
3308 |
'additional_errors' => $r['additional_errors'], |
|
3309 |
); |
|
3310 |
||
3311 |
if ( ! headers_sent() ) { |
|
3312 |
header( 'Content-Type: application/json; charset=utf-8' ); |
|
3313 |
if ( null !== $r['response'] ) { |
|
3314 |
status_header( $r['response'] ); |
|
3315 |
} |
|
3316 |
nocache_headers(); |
|
3317 |
} |
|
3318 |
||
3319 |
echo wp_json_encode( $data ); |
|
3320 |
if ( $r['exit'] ) { |
|
3321 |
die(); |
|
3322 |
} |
|
3323 |
} |
|
3324 |
||
3325 |
/** |
|
3326 |
* Kills WordPress execution and displays JSONP response with an error message. |
|
3327 |
* |
|
3328 |
* This is the handler for wp_die() when processing JSONP requests. |
|
3329 |
* |
|
3330 |
* @since 5.2.0 |
|
3331 |
* @access private |
|
3332 |
* |
|
3333 |
* @param string $message Error message. |
|
3334 |
* @param string $title Optional. Error title. Default empty. |
|
3335 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3336 |
*/ |
|
3337 |
function _jsonp_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3338 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3339 |
||
3340 |
$data = array( |
|
3341 |
'code' => $r['code'], |
|
3342 |
'message' => $message, |
|
3343 |
'data' => array( |
|
3344 |
'status' => $r['response'], |
|
3345 |
), |
|
3346 |
'additional_errors' => $r['additional_errors'], |
|
3347 |
); |
|
3348 |
||
3349 |
if ( ! headers_sent() ) { |
|
3350 |
header( 'Content-Type: application/javascript; charset=utf-8' ); |
|
3351 |
header( 'X-Content-Type-Options: nosniff' ); |
|
3352 |
header( 'X-Robots-Tag: noindex' ); |
|
3353 |
if ( null !== $r['response'] ) { |
|
3354 |
status_header( $r['response'] ); |
|
3355 |
} |
|
3356 |
nocache_headers(); |
|
3357 |
} |
|
3358 |
||
3359 |
$result = wp_json_encode( $data ); |
|
3360 |
$jsonp_callback = $_GET['_jsonp']; |
|
3361 |
echo '/**/' . $jsonp_callback . '(' . $result . ')'; |
|
3362 |
if ( $r['exit'] ) { |
|
3363 |
die(); |
|
3364 |
} |
|
3365 |
} |
|
3366 |
||
3367 |
/** |
|
3368 |
* Kills WordPress execution and displays XML response with an error message. |
|
3369 |
* |
|
3370 |
* This is the handler for wp_die() when processing XMLRPC requests. |
|
0 | 3371 |
* |
3372 |
* @since 3.2.0 |
|
3373 |
* @access private |
|
3374 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3375 |
* @global wp_xmlrpc_server $wp_xmlrpc_server |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3376 |
* |
5 | 3377 |
* @param string $message Error message. |
3378 |
* @param string $title Optional. Error title. Default empty. |
|
3379 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
0 | 3380 |
*/ |
3381 |
function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3382 |
global $wp_xmlrpc_server; |
|
9 | 3383 |
|
3384 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3385 |
||
3386 |
if ( ! headers_sent() ) { |
|
3387 |
nocache_headers(); |
|
3388 |
} |
|
0 | 3389 |
|
3390 |
if ( $wp_xmlrpc_server ) { |
|
9 | 3391 |
$error = new IXR_Error( $r['response'], $message ); |
0 | 3392 |
$wp_xmlrpc_server->output( $error->getXml() ); |
3393 |
} |
|
9 | 3394 |
if ( $r['exit'] ) { |
3395 |
die(); |
|
3396 |
} |
|
3397 |
} |
|
3398 |
||
3399 |
/** |
|
3400 |
* Kills WordPress execution and displays XML response with an error message. |
|
3401 |
* |
|
3402 |
* This is the handler for wp_die() when processing XML requests. |
|
3403 |
* |
|
3404 |
* @since 5.2.0 |
|
0 | 3405 |
* @access private |
3406 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3407 |
* @param string $message Error message. |
9 | 3408 |
* @param string $title Optional. Error title. Default empty. |
3409 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3410 |
*/ |
|
3411 |
function _xml_wp_die_handler( $message, $title = '', $args = array() ) { |
|
3412 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3413 |
||
3414 |
$message = htmlspecialchars( $message ); |
|
3415 |
$title = htmlspecialchars( $title ); |
|
3416 |
||
3417 |
$xml = <<<EOD |
|
3418 |
<error> |
|
3419 |
<code>{$r['code']}</code> |
|
3420 |
<title><![CDATA[{$title}]]></title> |
|
3421 |
<message><![CDATA[{$message}]]></message> |
|
3422 |
<data> |
|
3423 |
<status>{$r['response']}</status> |
|
3424 |
</data> |
|
3425 |
</error> |
|
3426 |
||
3427 |
EOD; |
|
3428 |
||
3429 |
if ( ! headers_sent() ) { |
|
3430 |
header( 'Content-Type: text/xml; charset=utf-8' ); |
|
3431 |
if ( null !== $r['response'] ) { |
|
3432 |
status_header( $r['response'] ); |
|
3433 |
} |
|
3434 |
nocache_headers(); |
|
3435 |
} |
|
3436 |
||
3437 |
echo $xml; |
|
3438 |
if ( $r['exit'] ) { |
|
3439 |
die(); |
|
3440 |
} |
|
3441 |
} |
|
3442 |
||
3443 |
/** |
|
3444 |
* Kills WordPress execution and displays an error message. |
|
3445 |
* |
|
3446 |
* This is the handler for wp_die() when processing APP requests. |
|
3447 |
* |
|
3448 |
* @since 3.4.0 |
|
3449 |
* @since 5.1.0 Added the $title and $args parameters. |
|
3450 |
* @access private |
|
3451 |
* |
|
3452 |
* @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
|
3453 |
* @param string $title Optional. Error title (unused). Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3454 |
* @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
|
3455 |
*/ |
9 | 3456 |
function _scalar_wp_die_handler( $message = '', $title = '', $args = array() ) { |
3457 |
list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args ); |
|
3458 |
||
3459 |
if ( $r['exit'] ) { |
|
3460 |
if ( is_scalar( $message ) ) { |
|
3461 |
die( (string) $message ); |
|
3462 |
} |
|
3463 |
die(); |
|
3464 |
} |
|
3465 |
||
3466 |
if ( is_scalar( $message ) ) { |
|
3467 |
echo (string) $message; |
|
3468 |
} |
|
3469 |
} |
|
3470 |
||
3471 |
/** |
|
3472 |
* Processes arguments passed to wp_die() consistently for its handlers. |
|
3473 |
* |
|
3474 |
* @since 5.1.0 |
|
3475 |
* @access private |
|
3476 |
* |
|
3477 |
* @param string $message Error message. |
|
3478 |
* @param string $title Optional. Error title. Default empty. |
|
3479 |
* @param string|array $args Optional. Arguments to control behavior. Default empty array. |
|
3480 |
* @return array List of processed $message string, $title string, and $args array. |
|
3481 |
*/ |
|
3482 |
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
|
3483 |
$defaults = array( |
9 | 3484 |
'response' => 0, |
3485 |
'code' => '', |
|
3486 |
'exit' => true, |
|
3487 |
'back_link' => false, |
|
3488 |
'link_url' => '', |
|
3489 |
'link_text' => '', |
|
3490 |
'text_direction' => '', |
|
3491 |
'additional_errors' => array(), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3492 |
); |
9 | 3493 |
|
3494 |
$args = wp_parse_args( $args, $defaults ); |
|
3495 |
||
3496 |
if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) { |
|
3497 |
if ( ! empty( $message->errors ) ) { |
|
3498 |
$errors = array(); |
|
3499 |
foreach ( (array) $message->errors as $error_code => $error_messages ) { |
|
3500 |
foreach ( (array) $error_messages as $error_message ) { |
|
3501 |
$errors[] = array( |
|
3502 |
'code' => $error_code, |
|
3503 |
'message' => $error_message, |
|
3504 |
'data' => $message->get_error_data( $error_code ), |
|
3505 |
); |
|
3506 |
} |
|
3507 |
} |
|
3508 |
||
3509 |
$message = $errors[0]['message']; |
|
3510 |
if ( empty( $args['code'] ) ) { |
|
3511 |
$args['code'] = $errors[0]['code']; |
|
3512 |
} |
|
3513 |
if ( empty( $args['response'] ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['status'] ) ) { |
|
3514 |
$args['response'] = $errors[0]['data']['status']; |
|
3515 |
} |
|
3516 |
if ( empty( $title ) && is_array( $errors[0]['data'] ) && ! empty( $errors[0]['data']['title'] ) ) { |
|
3517 |
$title = $errors[0]['data']['title']; |
|
3518 |
} |
|
3519 |
||
3520 |
unset( $errors[0] ); |
|
3521 |
$args['additional_errors'] = array_values( $errors ); |
|
3522 |
} else { |
|
3523 |
$message = ''; |
|
3524 |
} |
|
3525 |
} |
|
3526 |
||
3527 |
$have_gettext = function_exists( '__' ); |
|
3528 |
||
3529 |
// The $title and these specific $args must always have a non-empty value. |
|
3530 |
if ( empty( $args['code'] ) ) { |
|
3531 |
$args['code'] = 'wp_die'; |
|
3532 |
} |
|
3533 |
if ( empty( $args['response'] ) ) { |
|
3534 |
$args['response'] = 500; |
|
3535 |
} |
|
3536 |
if ( empty( $title ) ) { |
|
3537 |
$title = $have_gettext ? __( 'WordPress › Error' ) : 'WordPress › Error'; |
|
3538 |
} |
|
3539 |
if ( empty( $args['text_direction'] ) || ! in_array( $args['text_direction'], array( 'ltr', 'rtl' ), true ) ) { |
|
3540 |
$args['text_direction'] = 'ltr'; |
|
3541 |
if ( function_exists( 'is_rtl' ) && is_rtl() ) { |
|
3542 |
$args['text_direction'] = 'rtl'; |
|
3543 |
} |
|
3544 |
} |
|
3545 |
||
3546 |
return array( $message, $title, $args ); |
|
0 | 3547 |
} |
3548 |
||
3549 |
/** |
|
5 | 3550 |
* Encode a variable into JSON, with some sanity checks. |
3551 |
* |
|
3552 |
* @since 4.1.0 |
|
3553 |
* |
|
3554 |
* @param mixed $data Variable (usually an array or object) to encode as JSON. |
|
3555 |
* @param int $options Optional. Options to be passed to json_encode(). Default 0. |
|
3556 |
* @param int $depth Optional. Maximum depth to walk through $data. Must be |
|
3557 |
* greater than 0. Default 512. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3558 |
* @return string|false The JSON encoded string, or false if it cannot be encoded. |
5 | 3559 |
*/ |
3560 |
function wp_json_encode( $data, $options = 0, $depth = 512 ) { |
|
3561 |
/* |
|
3562 |
* json_encode() has had extra params added over the years. |
|
3563 |
* $options was added in 5.3, and $depth in 5.5. |
|
3564 |
* We need to make sure we call it with the correct arguments. |
|
3565 |
*/ |
|
3566 |
if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { |
|
3567 |
$args = array( $data, $options, $depth ); |
|
3568 |
} elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) { |
|
3569 |
$args = array( $data, $options ); |
|
3570 |
} else { |
|
3571 |
$args = array( $data ); |
|
3572 |
} |
|
3573 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3574 |
// Prepare the data for JSON serialization. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3575 |
$args[0] = _wp_json_prepare_data( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3576 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3577 |
$json = @call_user_func_array( 'json_encode', $args ); |
5 | 3578 |
|
3579 |
// If json_encode() was successful, no need to do more sanity checking. |
|
3580 |
// ... unless we're in an old version of PHP, and json_encode() returned |
|
3581 |
// a string containing 'null'. Then we need to do more sanity checking. |
|
9 | 3582 |
if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) { |
5 | 3583 |
return $json; |
3584 |
} |
|
3585 |
||
3586 |
try { |
|
3587 |
$args[0] = _wp_json_sanity_check( $data, $depth ); |
|
3588 |
} catch ( Exception $e ) { |
|
3589 |
return false; |
|
3590 |
} |
|
3591 |
||
3592 |
return call_user_func_array( 'json_encode', $args ); |
|
3593 |
} |
|
3594 |
||
3595 |
/** |
|
3596 |
* Perform sanity checks on data that shall be encoded to JSON. |
|
3597 |
* |
|
3598 |
* @ignore |
|
3599 |
* @since 4.1.0 |
|
3600 |
* @access private |
|
3601 |
* |
|
3602 |
* @see wp_json_encode() |
|
3603 |
* |
|
3604 |
* @param mixed $data Variable (usually an array or object) to encode as JSON. |
|
3605 |
* @param int $depth Maximum depth to walk through $data. Must be greater than 0. |
|
3606 |
* @return mixed The sanitized data that shall be encoded to JSON. |
|
3607 |
*/ |
|
3608 |
function _wp_json_sanity_check( $data, $depth ) { |
|
3609 |
if ( $depth < 0 ) { |
|
3610 |
throw new Exception( 'Reached depth limit' ); |
|
3611 |
} |
|
3612 |
||
3613 |
if ( is_array( $data ) ) { |
|
3614 |
$output = array(); |
|
3615 |
foreach ( $data as $id => $el ) { |
|
3616 |
// Don't forget to sanitize the ID! |
|
3617 |
if ( is_string( $id ) ) { |
|
3618 |
$clean_id = _wp_json_convert_string( $id ); |
|
3619 |
} else { |
|
3620 |
$clean_id = $id; |
|
3621 |
} |
|
3622 |
||
3623 |
// Check the element type, so that we're only recursing if we really have to. |
|
3624 |
if ( is_array( $el ) || is_object( $el ) ) { |
|
3625 |
$output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 ); |
|
3626 |
} elseif ( is_string( $el ) ) { |
|
3627 |
$output[ $clean_id ] = _wp_json_convert_string( $el ); |
|
3628 |
} else { |
|
3629 |
$output[ $clean_id ] = $el; |
|
3630 |
} |
|
3631 |
} |
|
3632 |
} elseif ( is_object( $data ) ) { |
|
3633 |
$output = new stdClass; |
|
3634 |
foreach ( $data as $id => $el ) { |
|
3635 |
if ( is_string( $id ) ) { |
|
3636 |
$clean_id = _wp_json_convert_string( $id ); |
|
3637 |
} else { |
|
3638 |
$clean_id = $id; |
|
3639 |
} |
|
3640 |
||
3641 |
if ( is_array( $el ) || is_object( $el ) ) { |
|
3642 |
$output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 ); |
|
3643 |
} elseif ( is_string( $el ) ) { |
|
3644 |
$output->$clean_id = _wp_json_convert_string( $el ); |
|
3645 |
} else { |
|
3646 |
$output->$clean_id = $el; |
|
3647 |
} |
|
3648 |
} |
|
3649 |
} elseif ( is_string( $data ) ) { |
|
3650 |
return _wp_json_convert_string( $data ); |
|
3651 |
} else { |
|
3652 |
return $data; |
|
3653 |
} |
|
3654 |
||
3655 |
return $output; |
|
3656 |
} |
|
3657 |
||
3658 |
/** |
|
3659 |
* Convert a string to UTF-8, so that it can be safely encoded to JSON. |
|
3660 |
* |
|
3661 |
* @ignore |
|
3662 |
* @since 4.1.0 |
|
3663 |
* @access private |
|
3664 |
* |
|
3665 |
* @see _wp_json_sanity_check() |
|
3666 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3667 |
* @staticvar bool $use_mb |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3668 |
* |
5 | 3669 |
* @param string $string The string which is to be converted. |
3670 |
* @return string The checked string. |
|
3671 |
*/ |
|
3672 |
function _wp_json_convert_string( $string ) { |
|
3673 |
static $use_mb = null; |
|
3674 |
if ( is_null( $use_mb ) ) { |
|
3675 |
$use_mb = function_exists( 'mb_convert_encoding' ); |
|
3676 |
} |
|
3677 |
||
3678 |
if ( $use_mb ) { |
|
3679 |
$encoding = mb_detect_encoding( $string, mb_detect_order(), true ); |
|
3680 |
if ( $encoding ) { |
|
3681 |
return mb_convert_encoding( $string, 'UTF-8', $encoding ); |
|
3682 |
} else { |
|
3683 |
return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' ); |
|
3684 |
} |
|
3685 |
} else { |
|
3686 |
return wp_check_invalid_utf8( $string, true ); |
|
3687 |
} |
|
3688 |
} |
|
3689 |
||
3690 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3691 |
* Prepares response data to be serialized to JSON. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3692 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3693 |
* 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
|
3694 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3695 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3696 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3697 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3698 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3699 |
* @param mixed $data Native representation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3700 |
* @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
|
3701 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3702 |
function _wp_json_prepare_data( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3703 |
if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3704 |
return $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3705 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3706 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3707 |
switch ( gettype( $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3708 |
case 'boolean': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3709 |
case 'integer': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3710 |
case 'double': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3711 |
case 'string': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3712 |
case 'NULL': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3713 |
// These values can be passed through. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3714 |
return $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3715 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3716 |
case 'array': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3717 |
// Arrays must be mapped in case they also return objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3718 |
return array_map( '_wp_json_prepare_data', $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3719 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3720 |
case 'object': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3721 |
// If this is an incomplete object (__PHP_Incomplete_Class), bail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3722 |
if ( ! is_object( $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3723 |
return null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3724 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3725 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3726 |
if ( $data instanceof JsonSerializable ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3727 |
$data = $data->jsonSerialize(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3728 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3729 |
$data = get_object_vars( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3730 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3731 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3732 |
// Now, pass the array (or whatever was returned from jsonSerialize through). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3733 |
return _wp_json_prepare_data( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3734 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3735 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3736 |
return null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3737 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3738 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3739 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3740 |
/** |
0 | 3741 |
* Send a JSON response back to an Ajax request. |
3742 |
* |
|
3743 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3744 |
* @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
|
3745 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3746 |
* @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
|
3747 |
* then print and die. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3748 |
* @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
|
3749 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3750 |
function wp_send_json( $response, $status_code = null ) { |
0 | 3751 |
@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3752 |
if ( null !== $status_code ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3753 |
status_header( $status_code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3754 |
} |
5 | 3755 |
echo wp_json_encode( $response ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3756 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3757 |
if ( wp_doing_ajax() ) { |
9 | 3758 |
wp_die( |
3759 |
'', |
|
3760 |
'', |
|
3761 |
array( |
|
3762 |
'response' => null, |
|
3763 |
) |
|
3764 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3765 |
} else { |
0 | 3766 |
die; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3767 |
} |
0 | 3768 |
} |
3769 |
||
3770 |
/** |
|
3771 |
* Send a JSON response back to an Ajax request, indicating success. |
|
3772 |
* |
|
3773 |
* @since 3.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3774 |
* @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
|
3775 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3776 |
* @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
|
3777 |
* @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
|
3778 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3779 |
function wp_send_json_success( $data = null, $status_code = null ) { |
0 | 3780 |
$response = array( 'success' => true ); |
3781 |
||
9 | 3782 |
if ( isset( $data ) ) { |
0 | 3783 |
$response['data'] = $data; |
9 | 3784 |
} |
0 | 3785 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3786 |
wp_send_json( $response, $status_code ); |
0 | 3787 |
} |
3788 |
||
3789 |
/** |
|
3790 |
* Send a JSON response back to an Ajax request, indicating failure. |
|
3791 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3792 |
* If the `$data` parameter is a WP_Error object, the errors |
5 | 3793 |
* within the object are processed and output as an array of error |
3794 |
* codes and corresponding messages. All other types are output |
|
3795 |
* without further processing. |
|
3796 |
* |
|
0 | 3797 |
* @since 3.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3798 |
* @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
|
3799 |
* @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
|
3800 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3801 |
* @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
|
3802 |
* @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
|
3803 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3804 |
function wp_send_json_error( $data = null, $status_code = null ) { |
0 | 3805 |
$response = array( 'success' => false ); |
3806 |
||
5 | 3807 |
if ( isset( $data ) ) { |
3808 |
if ( is_wp_error( $data ) ) { |
|
3809 |
$result = array(); |
|
3810 |
foreach ( $data->errors as $code => $messages ) { |
|
3811 |
foreach ( $messages as $message ) { |
|
9 | 3812 |
$result[] = array( |
3813 |
'code' => $code, |
|
3814 |
'message' => $message, |
|
3815 |
); |
|
5 | 3816 |
} |
3817 |
} |
|
3818 |
||
3819 |
$response['data'] = $result; |
|
3820 |
} else { |
|
3821 |
$response['data'] = $data; |
|
3822 |
} |
|
3823 |
} |
|
0 | 3824 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3825 |
wp_send_json( $response, $status_code ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3826 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3827 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3828 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3829 |
* Checks that a JSONP callback is a valid JavaScript callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3830 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3831 |
* 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
|
3832 |
* 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
|
3833 |
* outputting user input. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3834 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3835 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3836 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3837 |
* @param string $callback Supplied JSONP callback function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3838 |
* @return bool True if valid callback, otherwise false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3839 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3840 |
function wp_check_jsonp_callback( $callback ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3841 |
if ( ! is_string( $callback ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3842 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3843 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3844 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3845 |
preg_replace( '/[^\w\.]/', '', $callback, -1, $illegal_char_count ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3846 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3847 |
return 0 === $illegal_char_count; |
0 | 3848 |
} |
3849 |
||
3850 |
/** |
|
3851 |
* Retrieve the WordPress home page URL. |
|
3852 |
* |
|
5 | 3853 |
* If the constant named 'WP_HOME' exists, then it will be used and returned |
3854 |
* by the function. This can be used to counter the redirection on your local |
|
0 | 3855 |
* development environment. |
3856 |
* |
|
5 | 3857 |
* @since 2.2.0 |
0 | 3858 |
* @access private |
5 | 3859 |
* |
3860 |
* @see WP_HOME |
|
3861 |
* |
|
3862 |
* @param string $url URL for the home location. |
|
0 | 3863 |
* @return string Homepage location. |
3864 |
*/ |
|
3865 |
function _config_wp_home( $url = '' ) { |
|
9 | 3866 |
if ( defined( 'WP_HOME' ) ) { |
0 | 3867 |
return untrailingslashit( WP_HOME ); |
9 | 3868 |
} |
0 | 3869 |
return $url; |
3870 |
} |
|
3871 |
||
3872 |
/** |
|
3873 |
* Retrieve the WordPress site URL. |
|
3874 |
* |
|
3875 |
* If the constant named 'WP_SITEURL' is defined, then the value in that |
|
5 | 3876 |
* constant will always be returned. This can be used for debugging a site |
3877 |
* on your localhost while not having to change the database to your URL. |
|
3878 |
* |
|
3879 |
* @since 2.2.0 |
|
0 | 3880 |
* @access private |
5 | 3881 |
* |
3882 |
* @see WP_SITEURL |
|
0 | 3883 |
* |
3884 |
* @param string $url URL to set the WordPress site location. |
|
5 | 3885 |
* @return string The WordPress Site URL. |
0 | 3886 |
*/ |
3887 |
function _config_wp_siteurl( $url = '' ) { |
|
9 | 3888 |
if ( defined( 'WP_SITEURL' ) ) { |
0 | 3889 |
return untrailingslashit( WP_SITEURL ); |
9 | 3890 |
} |
0 | 3891 |
return $url; |
3892 |
} |
|
3893 |
||
3894 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3895 |
* Delete the fresh site option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3896 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3897 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3898 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3899 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3900 |
function _delete_option_fresh_site() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3901 |
update_option( 'fresh_site', '0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3902 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3903 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3904 |
/** |
0 | 3905 |
* Set the localized direction for MCE plugin. |
3906 |
* |
|
5 | 3907 |
* Will only set the direction to 'rtl', if the WordPress locale has |
3908 |
* the text direction set to 'rtl'. |
|
3909 |
* |
|
3910 |
* Fills in the 'directionality' setting, enables the 'directionality' |
|
3911 |
* plugin, and adds the 'ltr' button to 'toolbar1', formerly |
|
3912 |
* '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
|
3913 |
* in the $mce_init (TinyMCE settings) array. |
5 | 3914 |
* |
3915 |
* @since 2.1.0 |
|
0 | 3916 |
* @access private |
5 | 3917 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3918 |
* @param array $mce_init MCE settings array. |
0 | 3919 |
* @return array Direction set for 'rtl', if needed by locale. |
3920 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3921 |
function _mce_set_direction( $mce_init ) { |
0 | 3922 |
if ( is_rtl() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3923 |
$mce_init['directionality'] = 'rtl'; |
9 | 3924 |
$mce_init['rtl_ui'] = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3925 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3926 |
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
|
3927 |
$mce_init['plugins'] .= ',directionality'; |
5 | 3928 |
} |
3929 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3930 |
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
|
3931 |
$mce_init['toolbar1'] .= ',ltr'; |
5 | 3932 |
} |
0 | 3933 |
} |
3934 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3935 |
return $mce_init; |
0 | 3936 |
} |
3937 |
||
5 | 3938 |
|
0 | 3939 |
/** |
3940 |
* Convert smiley code to the icon graphic file equivalent. |
|
3941 |
* |
|
3942 |
* You can turn off smilies, by going to the write setting screen and unchecking |
|
3943 |
* the box, or by setting 'use_smilies' option to false or removing the option. |
|
3944 |
* |
|
3945 |
* Plugins may override the default smiley list by setting the $wpsmiliestrans |
|
3946 |
* to an array, with the key the code the blogger types in and the value the |
|
3947 |
* image file. |
|
3948 |
* |
|
3949 |
* The $wp_smiliessearch global is for the regular expression and is set each |
|
3950 |
* time the function is called. |
|
3951 |
* |
|
3952 |
* The full list of smilies can be found in the function and won't be listed in |
|
3953 |
* the description. Probably should create a Codex page for it, so that it is |
|
3954 |
* available. |
|
3955 |
* |
|
3956 |
* @global array $wpsmiliestrans |
|
3957 |
* @global array $wp_smiliessearch |
|
5 | 3958 |
* |
0 | 3959 |
* @since 2.2.0 |
3960 |
*/ |
|
3961 |
function smilies_init() { |
|
3962 |
global $wpsmiliestrans, $wp_smiliessearch; |
|
3963 |
||
3964 |
// don't bother setting up smilies if they are disabled |
|
9 | 3965 |
if ( ! get_option( 'use_smilies' ) ) { |
0 | 3966 |
return; |
9 | 3967 |
} |
3968 |
||
3969 |
if ( ! isset( $wpsmiliestrans ) ) { |
|
0 | 3970 |
$wpsmiliestrans = array( |
9 | 3971 |
':mrgreen:' => 'mrgreen.png', |
3972 |
':neutral:' => "\xf0\x9f\x98\x90", |
|
3973 |
':twisted:' => "\xf0\x9f\x98\x88", |
|
3974 |
':arrow:' => "\xe2\x9e\xa1", |
|
3975 |
':shock:' => "\xf0\x9f\x98\xaf", |
|
3976 |
':smile:' => "\xf0\x9f\x99\x82", |
|
3977 |
':???:' => "\xf0\x9f\x98\x95", |
|
3978 |
':cool:' => "\xf0\x9f\x98\x8e", |
|
3979 |
':evil:' => "\xf0\x9f\x91\xbf", |
|
3980 |
':grin:' => "\xf0\x9f\x98\x80", |
|
3981 |
':idea:' => "\xf0\x9f\x92\xa1", |
|
3982 |
':oops:' => "\xf0\x9f\x98\xb3", |
|
3983 |
':razz:' => "\xf0\x9f\x98\x9b", |
|
3984 |
':roll:' => "\xf0\x9f\x99\x84", |
|
3985 |
':wink:' => "\xf0\x9f\x98\x89", |
|
3986 |
':cry:' => "\xf0\x9f\x98\xa5", |
|
3987 |
':eek:' => "\xf0\x9f\x98\xae", |
|
3988 |
':lol:' => "\xf0\x9f\x98\x86", |
|
3989 |
':mad:' => "\xf0\x9f\x98\xa1", |
|
3990 |
':sad:' => "\xf0\x9f\x99\x81", |
|
3991 |
'8-)' => "\xf0\x9f\x98\x8e", |
|
3992 |
'8-O' => "\xf0\x9f\x98\xaf", |
|
3993 |
':-(' => "\xf0\x9f\x99\x81", |
|
3994 |
':-)' => "\xf0\x9f\x99\x82", |
|
3995 |
':-?' => "\xf0\x9f\x98\x95", |
|
3996 |
':-D' => "\xf0\x9f\x98\x80", |
|
3997 |
':-P' => "\xf0\x9f\x98\x9b", |
|
3998 |
':-o' => "\xf0\x9f\x98\xae", |
|
3999 |
':-x' => "\xf0\x9f\x98\xa1", |
|
4000 |
':-|' => "\xf0\x9f\x98\x90", |
|
4001 |
';-)' => "\xf0\x9f\x98\x89", |
|
4002 |
// This one transformation breaks regular text with frequency. |
|
4003 |
// '8)' => "\xf0\x9f\x98\x8e", |
|
4004 |
'8O' => "\xf0\x9f\x98\xaf", |
|
4005 |
':(' => "\xf0\x9f\x99\x81", |
|
4006 |
':)' => "\xf0\x9f\x99\x82", |
|
4007 |
':?' => "\xf0\x9f\x98\x95", |
|
4008 |
':D' => "\xf0\x9f\x98\x80", |
|
4009 |
':P' => "\xf0\x9f\x98\x9b", |
|
4010 |
':o' => "\xf0\x9f\x98\xae", |
|
4011 |
':x' => "\xf0\x9f\x98\xa1", |
|
4012 |
':|' => "\xf0\x9f\x98\x90", |
|
4013 |
';)' => "\xf0\x9f\x98\x89", |
|
4014 |
':!:' => "\xe2\x9d\x97", |
|
4015 |
':?:' => "\xe2\x9d\x93", |
|
0 | 4016 |
); |
4017 |
} |
|
4018 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4019 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4020 |
* Filters all the smilies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4021 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4022 |
* 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
|
4023 |
* 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
|
4024 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4025 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4026 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4027 |
* @param array $wpsmiliestrans List of the smilies. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4028 |
*/ |
9 | 4029 |
$wpsmiliestrans = apply_filters( 'smilies', $wpsmiliestrans ); |
4030 |
||
4031 |
if ( count( $wpsmiliestrans ) == 0 ) { |
|
0 | 4032 |
return; |
4033 |
} |
|
4034 |
||
4035 |
/* |
|
4036 |
* NOTE: we sort the smilies in reverse key order. This is to make sure |
|
4037 |
* we match the longest possible smilie (:???: vs :?) as the regular |
|
4038 |
* expression used below is first-match |
|
4039 |
*/ |
|
9 | 4040 |
krsort( $wpsmiliestrans ); |
0 | 4041 |
|
5 | 4042 |
$spaces = wp_spaces_regexp(); |
4043 |
||
4044 |
// Begin first "subpattern" |
|
4045 |
$wp_smiliessearch = '/(?<=' . $spaces . '|^)'; |
|
0 | 4046 |
|
4047 |
$subchar = ''; |
|
4048 |
foreach ( (array) $wpsmiliestrans as $smiley => $img ) { |
|
9 | 4049 |
$firstchar = substr( $smiley, 0, 1 ); |
4050 |
$rest = substr( $smiley, 1 ); |
|
0 | 4051 |
|
4052 |
// new subpattern? |
|
9 | 4053 |
if ( $firstchar != $subchar ) { |
4054 |
if ( $subchar != '' ) { |
|
5 | 4055 |
$wp_smiliessearch .= ')(?=' . $spaces . '|$)'; // End previous "subpattern" |
4056 |
$wp_smiliessearch .= '|(?<=' . $spaces . '|^)'; // Begin another "subpattern" |
|
0 | 4057 |
} |
9 | 4058 |
$subchar = $firstchar; |
4059 |
$wp_smiliessearch .= preg_quote( $firstchar, '/' ) . '(?:'; |
|
0 | 4060 |
} else { |
4061 |
$wp_smiliessearch .= '|'; |
|
4062 |
} |
|
9 | 4063 |
$wp_smiliessearch .= preg_quote( $rest, '/' ); |
0 | 4064 |
} |
4065 |
||
5 | 4066 |
$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m'; |
4067 |
||
0 | 4068 |
} |
4069 |
||
4070 |
/** |
|
4071 |
* Merge user defined arguments into defaults array. |
|
4072 |
* |
|
4073 |
* This function is used throughout WordPress to allow for both string or array |
|
4074 |
* to be merged into another array. |
|
4075 |
* |
|
4076 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4077 |
* @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
|
4078 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4079 |
* @param string|array|object $args Value to merge with $defaults. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4080 |
* @param array $defaults Optional. Array that serves as the defaults. Default empty. |
0 | 4081 |
* @return array Merged user defined values with defaults. |
4082 |
*/ |
|
4083 |
function wp_parse_args( $args, $defaults = '' ) { |
|
9 | 4084 |
if ( is_object( $args ) ) { |
0 | 4085 |
$r = get_object_vars( $args ); |
9 | 4086 |
} elseif ( is_array( $args ) ) { |
0 | 4087 |
$r =& $args; |
9 | 4088 |
} else { |
0 | 4089 |
wp_parse_str( $args, $r ); |
9 | 4090 |
} |
4091 |
||
4092 |
if ( is_array( $defaults ) ) { |
|
0 | 4093 |
return array_merge( $defaults, $r ); |
9 | 4094 |
} |
0 | 4095 |
return $r; |
4096 |
} |
|
4097 |
||
4098 |
/** |
|
9 | 4099 |
* Cleans up an array, comma- or space-separated list of scalar values. |
4100 |
* |
|
4101 |
* @since 5.1.0 |
|
4102 |
* |
|
4103 |
* @param array|string $list List of values. |
|
4104 |
* @return array Sanitized array of values. |
|
4105 |
*/ |
|
4106 |
function wp_parse_list( $list ) { |
|
4107 |
if ( ! is_array( $list ) ) { |
|
4108 |
return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
4109 |
} |
|
4110 |
||
4111 |
return $list; |
|
4112 |
} |
|
4113 |
||
4114 |
/** |
|
0 | 4115 |
* Clean up an array, comma- or space-separated list of IDs. |
4116 |
* |
|
4117 |
* @since 3.0.0 |
|
4118 |
* |
|
5 | 4119 |
* @param array|string $list List of ids. |
4120 |
* @return array Sanitized array of IDs. |
|
0 | 4121 |
*/ |
4122 |
function wp_parse_id_list( $list ) { |
|
9 | 4123 |
$list = wp_parse_list( $list ); |
4124 |
||
4125 |
return array_unique( array_map( 'absint', $list ) ); |
|
0 | 4126 |
} |
4127 |
||
4128 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4129 |
* 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
|
4130 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4131 |
* @since 4.7.0 |
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 |
* @param array|string $list List of slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4134 |
* @return array Sanitized array of slugs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4135 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4136 |
function wp_parse_slug_list( $list ) { |
9 | 4137 |
$list = wp_parse_list( $list ); |
4138 |
||
4139 |
return array_unique( array_map( 'sanitize_title', $list ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4140 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4141 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4142 |
/** |
0 | 4143 |
* Extract a slice of an array, given a list of keys. |
4144 |
* |
|
4145 |
* @since 3.1.0 |
|
4146 |
* |
|
5 | 4147 |
* @param array $array The original array. |
4148 |
* @param array $keys The list of keys. |
|
4149 |
* @return array The array slice. |
|
0 | 4150 |
*/ |
4151 |
function wp_array_slice_assoc( $array, $keys ) { |
|
4152 |
$slice = array(); |
|
9 | 4153 |
foreach ( $keys as $key ) { |
4154 |
if ( isset( $array[ $key ] ) ) { |
|
0 | 4155 |
$slice[ $key ] = $array[ $key ]; |
9 | 4156 |
} |
4157 |
} |
|
0 | 4158 |
|
4159 |
return $slice; |
|
4160 |
} |
|
4161 |
||
4162 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4163 |
* Determines if the variable is a numeric-indexed array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4164 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4165 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4166 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4167 |
* @param mixed $data Variable to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4168 |
* @return bool Whether the variable is a list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4169 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4170 |
function wp_is_numeric_array( $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4171 |
if ( ! is_array( $data ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4172 |
return false; |
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 |
|
9 | 4175 |
$keys = array_keys( $data ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4176 |
$string_keys = array_filter( $keys, 'is_string' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4177 |
return count( $string_keys ) === 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4178 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4179 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4180 |
/** |
0 | 4181 |
* Filters a list of objects, based on a set of key => value arguments. |
4182 |
* |
|
4183 |
* @since 3.0.0 |
|
9 | 4184 |
* @since 4.7.0 Uses `WP_List_Util` class. |
0 | 4185 |
* |
5 | 4186 |
* @param array $list An array of objects to filter |
4187 |
* @param array $args Optional. An array of key => value arguments to match |
|
4188 |
* against each object. Default empty array. |
|
4189 |
* @param string $operator Optional. The logical operation to perform. 'or' means |
|
4190 |
* 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
|
4191 |
* 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
|
4192 |
* match. Default 'and'. |
5 | 4193 |
* @param bool|string $field A field from the object to place instead of the entire object. |
4194 |
* Default false. |
|
4195 |
* @return array A list of objects or object fields. |
|
0 | 4196 |
*/ |
4197 |
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
|
4198 |
if ( ! is_array( $list ) ) { |
0 | 4199 |
return array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4202 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4203 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4204 |
$util->filter( $args, $operator ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4205 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4206 |
if ( $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4207 |
$util->pluck( $field ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4208 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4209 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4210 |
return $util->get_output(); |
0 | 4211 |
} |
4212 |
||
4213 |
/** |
|
4214 |
* Filters a list of objects, based on a set of key => value arguments. |
|
4215 |
* |
|
4216 |
* @since 3.1.0 |
|
9 | 4217 |
* @since 4.7.0 Uses `WP_List_Util` class. |
0 | 4218 |
* |
5 | 4219 |
* @param array $list An array of objects to filter. |
4220 |
* @param array $args Optional. An array of key => value arguments to match |
|
4221 |
* against each object. Default empty array. |
|
4222 |
* @param string $operator Optional. The logical operation to perform. 'AND' means |
|
4223 |
* all elements from the array must match. 'OR' means only |
|
4224 |
* one element needs to match. 'NOT' means no elements may |
|
4225 |
* match. Default 'AND'. |
|
4226 |
* @return array Array of found values. |
|
0 | 4227 |
*/ |
4228 |
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
|
4229 |
if ( ! is_array( $list ) ) { |
0 | 4230 |
return array(); |
7
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 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4234 |
return $util->filter( $args, $operator ); |
0 | 4235 |
} |
4236 |
||
4237 |
/** |
|
4238 |
* Pluck a certain field out of each object in a list. |
|
4239 |
* |
|
5 | 4240 |
* This has the same functionality and prototype of |
4241 |
* array_column() (PHP 5.5) but also supports objects. |
|
4242 |
* |
|
0 | 4243 |
* @since 3.1.0 |
5 | 4244 |
* @since 4.0.0 $index_key parameter added. |
9 | 4245 |
* @since 4.7.0 Uses `WP_List_Util` class. |
5 | 4246 |
* |
4247 |
* @param array $list List of objects or arrays |
|
4248 |
* @param int|string $field Field from the object to place instead of the entire object |
|
4249 |
* @param int|string $index_key Optional. Field from the object to use as keys for the new array. |
|
4250 |
* Default null. |
|
4251 |
* @return array Array of found values. If `$index_key` is set, an array of found values with keys |
|
4252 |
* corresponding to `$index_key`. If `$index_key` is null, array keys from the original |
|
4253 |
* `$list` will be preserved in the results. |
|
0 | 4254 |
*/ |
5 | 4255 |
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
|
4256 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4257 |
return $util->pluck( $field, $index_key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4258 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4259 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4260 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4261 |
* 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
|
4262 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4263 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4264 |
* |
9 | 4265 |
* @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
|
4266 |
* @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
|
4267 |
* of multiple orderby fields as $orderby => $order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4268 |
* @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
|
4269 |
* is a string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4270 |
* @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
|
4271 |
* @return array The sorted array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4272 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4273 |
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
|
4274 |
if ( ! is_array( $list ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4275 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4276 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4277 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4278 |
$util = new WP_List_Util( $list ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4279 |
return $util->sort( $orderby, $order, $preserve_keys ); |
0 | 4280 |
} |
4281 |
||
4282 |
/** |
|
4283 |
* Determines if Widgets library should be loaded. |
|
4284 |
* |
|
5 | 4285 |
* Checks to make sure that the widgets library hasn't already been loaded. |
4286 |
* If it hasn't, then it will load the widgets library and run an action hook. |
|
0 | 4287 |
* |
4288 |
* @since 2.2.0 |
|
4289 |
*/ |
|
4290 |
function wp_maybe_load_widgets() { |
|
5 | 4291 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4292 |
* Filters whether to load the Widgets library. |
5 | 4293 |
* |
4294 |
* Passing a falsey value to the filter will effectively short-circuit |
|
4295 |
* the Widgets library from loading. |
|
4296 |
* |
|
4297 |
* @since 2.8.0 |
|
4298 |
* |
|
4299 |
* @param bool $wp_maybe_load_widgets Whether to load the Widgets library. |
|
4300 |
* Default true. |
|
4301 |
*/ |
|
4302 |
if ( ! apply_filters( 'load_default_widgets', true ) ) { |
|
0 | 4303 |
return; |
5 | 4304 |
} |
4305 |
||
0 | 4306 |
require_once( ABSPATH . WPINC . '/default-widgets.php' ); |
5 | 4307 |
|
0 | 4308 |
add_action( '_admin_menu', 'wp_widgets_add_menu' ); |
4309 |
} |
|
4310 |
||
4311 |
/** |
|
4312 |
* Append the Widgets menu to the themes main menu. |
|
4313 |
* |
|
4314 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4315 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4316 |
* @global array $submenu |
0 | 4317 |
*/ |
4318 |
function wp_widgets_add_menu() { |
|
4319 |
global $submenu; |
|
4320 |
||
9 | 4321 |
if ( ! current_theme_supports( 'widgets' ) ) { |
0 | 4322 |
return; |
9 | 4323 |
} |
0 | 4324 |
|
4325 |
$submenu['themes.php'][7] = array( __( 'Widgets' ), 'edit_theme_options', 'widgets.php' ); |
|
4326 |
ksort( $submenu['themes.php'], SORT_NUMERIC ); |
|
4327 |
} |
|
4328 |
||
4329 |
/** |
|
4330 |
* Flush all output buffers for PHP 5.2. |
|
4331 |
* |
|
5 | 4332 |
* Make sure all output buffers are flushed before our singletons are destroyed. |
0 | 4333 |
* |
4334 |
* @since 2.2.0 |
|
4335 |
*/ |
|
4336 |
function wp_ob_end_flush_all() { |
|
4337 |
$levels = ob_get_level(); |
|
9 | 4338 |
for ( $i = 0; $i < $levels; $i++ ) { |
0 | 4339 |
ob_end_flush(); |
9 | 4340 |
} |
0 | 4341 |
} |
4342 |
||
4343 |
/** |
|
4344 |
* Load custom DB error or display WordPress DB error. |
|
4345 |
* |
|
4346 |
* If a file exists in the wp-content directory named db-error.php, then it will |
|
4347 |
* be loaded instead of displaying the WordPress DB error. If it is not found, |
|
4348 |
* then the WordPress DB error will be displayed instead. |
|
4349 |
* |
|
4350 |
* The WordPress DB error sets the HTTP status header to 500 to try to prevent |
|
4351 |
* search engines from caching the message. Custom DB messages should do the |
|
4352 |
* same. |
|
4353 |
* |
|
4354 |
* This function was backported to WordPress 2.3.2, but originally was added |
|
4355 |
* in WordPress 2.5.0. |
|
4356 |
* |
|
4357 |
* @since 2.3.2 |
|
5 | 4358 |
* |
4359 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4360 |
*/ |
4361 |
function dead_db() { |
|
4362 |
global $wpdb; |
|
4363 |
||
5 | 4364 |
wp_load_translations_early(); |
4365 |
||
0 | 4366 |
// Load custom DB error template, if present. |
4367 |
if ( file_exists( WP_CONTENT_DIR . '/db-error.php' ) ) { |
|
4368 |
require_once( WP_CONTENT_DIR . '/db-error.php' ); |
|
4369 |
die(); |
|
4370 |
} |
|
4371 |
||
4372 |
// If installing or in the admin, provide the verbose message. |
|
9 | 4373 |
if ( wp_installing() || defined( 'WP_ADMIN' ) ) { |
4374 |
wp_die( $wpdb->error ); |
|
4375 |
} |
|
0 | 4376 |
|
4377 |
// Otherwise, be terse. |
|
9 | 4378 |
wp_die( '<h1>' . __( 'Error establishing a database connection' ) . '</h1>', __( 'Database Error' ) ); |
0 | 4379 |
} |
4380 |
||
4381 |
/** |
|
5 | 4382 |
* Convert a value to non-negative integer. |
0 | 4383 |
* |
4384 |
* @since 2.5.0 |
|
4385 |
* |
|
5 | 4386 |
* @param mixed $maybeint Data you wish to have converted to a non-negative integer. |
4387 |
* @return int A non-negative integer. |
|
0 | 4388 |
*/ |
4389 |
function absint( $maybeint ) { |
|
4390 |
return abs( intval( $maybeint ) ); |
|
4391 |
} |
|
4392 |
||
4393 |
/** |
|
5 | 4394 |
* Mark a function as deprecated and inform when it has been used. |
0 | 4395 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4396 |
* There is a {@see 'hook deprecated_function_run'} that will be called that can be used |
0 | 4397 |
* to get the backtrace up to what file and function called the deprecated |
4398 |
* function. |
|
4399 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4400 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 4401 |
* |
4402 |
* This function is to be used in every function that is deprecated. |
|
4403 |
* |
|
4404 |
* @since 2.5.0 |
|
4405 |
* @access private |
|
4406 |
* |
|
5 | 4407 |
* @param string $function The function that was called. |
4408 |
* @param string $version The version of WordPress that deprecated the function. |
|
4409 |
* @param string $replacement Optional. The function that should have been called. Default null. |
|
0 | 4410 |
*/ |
4411 |
function _deprecated_function( $function, $version, $replacement = null ) { |
|
4412 |
||
5 | 4413 |
/** |
4414 |
* Fires when a deprecated function is called. |
|
4415 |
* |
|
4416 |
* @since 2.5.0 |
|
4417 |
* |
|
4418 |
* @param string $function The function that was called. |
|
4419 |
* @param string $replacement The function that should have been called. |
|
4420 |
* @param string $version The version of WordPress that deprecated the function. |
|
4421 |
*/ |
|
0 | 4422 |
do_action( 'deprecated_function_run', $function, $replacement, $version ); |
4423 |
||
5 | 4424 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4425 |
* Filters whether to trigger an error for deprecated functions. |
5 | 4426 |
* |
4427 |
* @since 2.5.0 |
|
4428 |
* |
|
4429 |
* @param bool $trigger Whether to trigger the error for deprecated functions. Default true. |
|
4430 |
*/ |
|
0 | 4431 |
if ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) { |
4432 |
if ( function_exists( '__' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4433 |
if ( ! is_null( $replacement ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4434 |
/* translators: 1: PHP function name, 2: version number, 3: alternative function name */ |
9 | 4435 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $function, $version, $replacement ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4436 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4437 |
/* translators: 1: PHP function name, 2: version number */ |
9 | 4438 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $function, $version ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4439 |
} |
0 | 4440 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4441 |
if ( ! is_null( $replacement ) ) { |
0 | 4442 |
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $function, $version, $replacement ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4443 |
} else { |
0 | 4444 |
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4445 |
} |
0 | 4446 |
} |
4447 |
} |
|
4448 |
} |
|
4449 |
||
4450 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4451 |
* 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
|
4452 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4453 |
* 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
|
4454 |
* remove PHP4 style constructors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4455 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4456 |
* 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
|
4457 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4458 |
* 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
|
4459 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4460 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4461 |
* @since 4.5.0 Added the `$parent_class` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4462 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4463 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4464 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4465 |
* @param string $class The class containing the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4466 |
* @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
|
4467 |
* @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
|
4468 |
* Default empty string. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4469 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4470 |
function _deprecated_constructor( $class, $version, $parent_class = '' ) { |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4473 |
* Fires when a deprecated constructor is called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4474 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4475 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4476 |
* @since 4.5.0 Added the `$parent_class` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4477 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4478 |
* @param string $class The class containing the deprecated constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4479 |
* @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
|
4480 |
* @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
|
4481 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4482 |
do_action( 'deprecated_constructor_run', $class, $version, $parent_class ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4483 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4484 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4485 |
* Filters whether to trigger an error for deprecated functions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4486 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4487 |
* `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
|
4488 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4489 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4490 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4491 |
* @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
|
4492 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4493 |
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
|
4494 |
if ( function_exists( '__' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4495 |
if ( ! empty( $parent_class ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4496 |
/* translators: 1: PHP class name, 2: PHP parent class name, 3: version number, 4: __construct() method */ |
9 | 4497 |
trigger_error( |
4498 |
sprintf( |
|
4499 |
__( 'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.' ), |
|
4500 |
$class, |
|
4501 |
$parent_class, |
|
4502 |
$version, |
|
4503 |
'<pre>__construct()</pre>' |
|
4504 |
) |
|
4505 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4506 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4507 |
/* translators: 1: PHP class name, 2: version number, 3: __construct() method */ |
9 | 4508 |
trigger_error( |
4509 |
sprintf( |
|
4510 |
__( 'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), |
|
4511 |
$class, |
|
4512 |
$version, |
|
4513 |
'<pre>__construct()</pre>' |
|
4514 |
) |
|
4515 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4516 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4517 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4518 |
if ( ! empty( $parent_class ) ) { |
9 | 4519 |
trigger_error( |
4520 |
sprintf( |
|
4521 |
'The called constructor method for %1$s in %2$s is <strong>deprecated</strong> since version %3$s! Use %4$s instead.', |
|
4522 |
$class, |
|
4523 |
$parent_class, |
|
4524 |
$version, |
|
4525 |
'<pre>__construct()</pre>' |
|
4526 |
) |
|
4527 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4528 |
} else { |
9 | 4529 |
trigger_error( |
4530 |
sprintf( |
|
4531 |
'The called constructor method for %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', |
|
4532 |
$class, |
|
4533 |
$version, |
|
4534 |
'<pre>__construct()</pre>' |
|
4535 |
) |
|
4536 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4537 |
} |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4541 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4542 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4543 |
/** |
5 | 4544 |
* Mark a file as deprecated and inform when it has been used. |
0 | 4545 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4546 |
* There is a hook {@see 'deprecated_file_included'} that will be called that can be used |
0 | 4547 |
* to get the backtrace up to what file and function included the deprecated |
4548 |
* file. |
|
4549 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4550 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 4551 |
* |
4552 |
* This function is to be used in every file that is deprecated. |
|
4553 |
* |
|
4554 |
* @since 2.5.0 |
|
4555 |
* @access private |
|
4556 |
* |
|
5 | 4557 |
* @param string $file The file that was included. |
4558 |
* @param string $version The version of WordPress that deprecated the file. |
|
4559 |
* @param string $replacement Optional. The file that should have been included based on ABSPATH. |
|
4560 |
* Default null. |
|
4561 |
* @param string $message Optional. A message regarding the change. Default empty. |
|
0 | 4562 |
*/ |
4563 |
function _deprecated_file( $file, $version, $replacement = null, $message = '' ) { |
|
4564 |
||
5 | 4565 |
/** |
4566 |
* Fires when a deprecated file is called. |
|
4567 |
* |
|
4568 |
* @since 2.5.0 |
|
4569 |
* |
|
4570 |
* @param string $file The file that was called. |
|
4571 |
* @param string $replacement The file that should have been included based on ABSPATH. |
|
4572 |
* @param string $version The version of WordPress that deprecated the file. |
|
4573 |
* @param string $message A message regarding the change. |
|
4574 |
*/ |
|
0 | 4575 |
do_action( 'deprecated_file_included', $file, $replacement, $version, $message ); |
4576 |
||
5 | 4577 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4578 |
* Filters whether to trigger an error for deprecated files. |
5 | 4579 |
* |
4580 |
* @since 2.5.0 |
|
4581 |
* |
|
4582 |
* @param bool $trigger Whether to trigger the error for deprecated files. Default true. |
|
4583 |
*/ |
|
0 | 4584 |
if ( WP_DEBUG && apply_filters( 'deprecated_file_trigger_error', true ) ) { |
4585 |
$message = empty( $message ) ? '' : ' ' . $message; |
|
4586 |
if ( function_exists( '__' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4587 |
if ( ! is_null( $replacement ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4588 |
/* translators: 1: PHP file name, 2: version number, 3: alternative file name */ |
9 | 4589 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $file, $version, $replacement ) . $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4590 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4591 |
/* translators: 1: PHP file name, 2: version number */ |
9 | 4592 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $file, $version ) . $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4593 |
} |
0 | 4594 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4595 |
if ( ! is_null( $replacement ) ) { |
0 | 4596 |
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $file, $version, $replacement ) . $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4597 |
} else { |
0 | 4598 |
trigger_error( sprintf( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $file, $version ) . $message ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4599 |
} |
0 | 4600 |
} |
4601 |
} |
|
4602 |
} |
|
4603 |
/** |
|
5 | 4604 |
* Mark a function argument as deprecated and inform when it has been used. |
0 | 4605 |
* |
4606 |
* This function is to be used whenever a deprecated function argument is used. |
|
4607 |
* Before this function is called, the argument must be checked for whether it was |
|
4608 |
* used by comparing it to its default value or evaluating whether it is empty. |
|
4609 |
* For example: |
|
5 | 4610 |
* |
4611 |
* if ( ! empty( $deprecated ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4612 |
* _deprecated_argument( __FUNCTION__, '3.0.0' ); |
5 | 4613 |
* } |
4614 |
* |
|
0 | 4615 |
* There is a hook deprecated_argument_run that will be called that can be used |
4616 |
* to get the backtrace up to what file and function used the deprecated |
|
4617 |
* argument. |
|
4618 |
* |
|
4619 |
* The current behavior is to trigger a user error if WP_DEBUG is true. |
|
4620 |
* |
|
4621 |
* @since 3.0.0 |
|
4622 |
* @access private |
|
4623 |
* |
|
5 | 4624 |
* @param string $function The function that was called. |
4625 |
* @param string $version The version of WordPress that deprecated the argument used. |
|
4626 |
* @param string $message Optional. A message regarding the change. Default null. |
|
0 | 4627 |
*/ |
4628 |
function _deprecated_argument( $function, $version, $message = null ) { |
|
4629 |
||
5 | 4630 |
/** |
4631 |
* Fires when a deprecated argument is called. |
|
4632 |
* |
|
4633 |
* @since 3.0.0 |
|
4634 |
* |
|
4635 |
* @param string $function The function that was called. |
|
4636 |
* @param string $message A message regarding the change. |
|
4637 |
* @param string $version The version of WordPress that deprecated the argument used. |
|
4638 |
*/ |
|
0 | 4639 |
do_action( 'deprecated_argument_run', $function, $message, $version ); |
4640 |
||
5 | 4641 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4642 |
* Filters whether to trigger an error for deprecated arguments. |
5 | 4643 |
* |
4644 |
* @since 3.0.0 |
|
4645 |
* |
|
4646 |
* @param bool $trigger Whether to trigger the error for deprecated arguments. Default true. |
|
4647 |
*/ |
|
0 | 4648 |
if ( WP_DEBUG && apply_filters( 'deprecated_argument_trigger_error', true ) ) { |
4649 |
if ( function_exists( '__' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4650 |
if ( ! is_null( $message ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4651 |
/* translators: 1: PHP function name, 2: version number, 3: optional message regarding the change */ |
9 | 4652 |
trigger_error( sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s' ), $function, $version, $message ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4653 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4654 |
/* translators: 1: PHP function name, 2: version number */ |
9 | 4655 |
trigger_error( sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $function, $version ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4656 |
} |
0 | 4657 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4658 |
if ( ! is_null( $message ) ) { |
0 | 4659 |
trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s! %3$s', $function, $version, $message ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4660 |
} else { |
0 | 4661 |
trigger_error( sprintf( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s with no alternative available.', $function, $version ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4662 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4663 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4664 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4665 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4666 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4667 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4668 |
* 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
|
4669 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4670 |
* 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
|
4671 |
* the deprecated hook was called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4672 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4673 |
* 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
|
4674 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4675 |
* 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
|
4676 |
* 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
|
4677 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4678 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4679 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4680 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4681 |
* @param string $hook The hook that was used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4682 |
* @param string $version The version of WordPress that deprecated the hook. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4683 |
* @param string $replacement Optional. The hook that should have been used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4684 |
* @param string $message Optional. A message regarding the change. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4685 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4686 |
function _deprecated_hook( $hook, $version, $replacement = null, $message = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4687 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4688 |
* Fires when a deprecated hook is called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4689 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4690 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4691 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4692 |
* @param string $hook The hook that was called. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4693 |
* @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
|
4694 |
* @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
|
4695 |
* @param string $message A message regarding the change. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4696 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4697 |
do_action( 'deprecated_hook_run', $hook, $replacement, $version, $message ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4698 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4699 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4700 |
* Filters whether to trigger deprecated hook errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4701 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4702 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4703 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4704 |
* @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
|
4705 |
* `WP_DEBUG` to be defined true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4706 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4707 |
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
|
4708 |
$message = empty( $message ) ? '' : ' ' . $message; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4709 |
if ( ! is_null( $replacement ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4710 |
/* translators: 1: WordPress hook name, 2: version number, 3: alternative hook name */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4711 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.' ), $hook, $version, $replacement ) . $message ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4712 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4713 |
/* translators: 1: WordPress hook name, 2: version number */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4714 |
trigger_error( sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.' ), $hook, $version ) . $message ); |
0 | 4715 |
} |
4716 |
} |
|
4717 |
} |
|
4718 |
||
4719 |
/** |
|
5 | 4720 |
* Mark something as being incorrectly called. |
0 | 4721 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4722 |
* There is a hook {@see 'doing_it_wrong_run'} that will be called that can be used |
0 | 4723 |
* to get the backtrace up to what file and function called the deprecated |
4724 |
* function. |
|
4725 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4726 |
* The current behavior is to trigger a user error if `WP_DEBUG` is true. |
0 | 4727 |
* |
4728 |
* @since 3.1.0 |
|
4729 |
* @access private |
|
4730 |
* |
|
4731 |
* @param string $function The function that was called. |
|
5 | 4732 |
* @param string $message A message explaining what has been done incorrectly. |
4733 |
* @param string $version The version of WordPress where the message was added. |
|
0 | 4734 |
*/ |
4735 |
function _doing_it_wrong( $function, $message, $version ) { |
|
4736 |
||
5 | 4737 |
/** |
4738 |
* Fires when the given function is being used incorrectly. |
|
4739 |
* |
|
4740 |
* @since 3.1.0 |
|
4741 |
* |
|
4742 |
* @param string $function The function that was called. |
|
4743 |
* @param string $message A message explaining what has been done incorrectly. |
|
4744 |
* @param string $version The version of WordPress where the message was added. |
|
4745 |
*/ |
|
0 | 4746 |
do_action( 'doing_it_wrong_run', $function, $message, $version ); |
4747 |
||
5 | 4748 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4749 |
* Filters whether to trigger an error for _doing_it_wrong() calls. |
5 | 4750 |
* |
4751 |
* @since 3.1.0 |
|
9 | 4752 |
* @since 5.1.0 Added the $function, $message and $version parameters. |
5 | 4753 |
* |
9 | 4754 |
* @param bool $trigger Whether to trigger the error for _doing_it_wrong() calls. Default true. |
4755 |
* @param string $function The function that was called. |
|
4756 |
* @param string $message A message explaining what has been done incorrectly. |
|
4757 |
* @param string $version The version of WordPress where the message was added. |
|
5 | 4758 |
*/ |
9 | 4759 |
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function, $message, $version ) ) { |
0 | 4760 |
if ( function_exists( '__' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4761 |
if ( is_null( $version ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4762 |
$version = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4763 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4764 |
/* translators: %s: version number */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4765 |
$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
|
4766 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4767 |
/* translators: %s: Codex URL */ |
9 | 4768 |
$message .= ' ' . sprintf( |
4769 |
__( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4770 |
__( 'https://codex.wordpress.org/Debugging_in_WordPress' ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4771 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4772 |
/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: Version information message */ |
0 | 4773 |
trigger_error( sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ), $function, $message, $version ) ); |
4774 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4775 |
if ( is_null( $version ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4776 |
$version = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4777 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4778 |
$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
|
4779 |
} |
9 | 4780 |
$message .= sprintf( |
4781 |
' Please see <a href="%s">Debugging in WordPress</a> for more information.', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4782 |
'https://codex.wordpress.org/Debugging_in_WordPress' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4783 |
); |
0 | 4784 |
trigger_error( sprintf( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', $function, $message, $version ) ); |
4785 |
} |
|
4786 |
} |
|
4787 |
} |
|
4788 |
||
4789 |
/** |
|
4790 |
* Is the server running earlier than 1.5.0 version of lighttpd? |
|
4791 |
* |
|
4792 |
* @since 2.5.0 |
|
4793 |
* |
|
5 | 4794 |
* @return bool Whether the server is running lighttpd < 1.5.0. |
0 | 4795 |
*/ |
4796 |
function is_lighttpd_before_150() { |
|
9 | 4797 |
$server_parts = explode( '/', isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '' ); |
4798 |
$server_parts[1] = isset( $server_parts[1] ) ? $server_parts[1] : ''; |
|
0 | 4799 |
return 'lighttpd' == $server_parts[0] && -1 == version_compare( $server_parts[1], '1.5.0' ); |
4800 |
} |
|
4801 |
||
4802 |
/** |
|
4803 |
* Does the specified module exist in the Apache config? |
|
4804 |
* |
|
4805 |
* @since 2.5.0 |
|
4806 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4807 |
* @global bool $is_apache |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4808 |
* |
5 | 4809 |
* @param string $mod The module, e.g. mod_rewrite. |
4810 |
* @param bool $default Optional. The default return value if the module is not found. Default false. |
|
4811 |
* @return bool Whether the specified module is loaded. |
|
0 | 4812 |
*/ |
9 | 4813 |
function apache_mod_loaded( $mod, $default = false ) { |
0 | 4814 |
global $is_apache; |
4815 |
||
9 | 4816 |
if ( ! $is_apache ) { |
0 | 4817 |
return false; |
9 | 4818 |
} |
0 | 4819 |
|
5 | 4820 |
if ( function_exists( 'apache_get_modules' ) ) { |
0 | 4821 |
$mods = apache_get_modules(); |
9 | 4822 |
if ( in_array( $mod, $mods ) ) { |
0 | 4823 |
return true; |
9 | 4824 |
} |
5 | 4825 |
} elseif ( function_exists( 'phpinfo' ) && false === strpos( ini_get( 'disable_functions' ), 'phpinfo' ) ) { |
0 | 4826 |
ob_start(); |
9 | 4827 |
phpinfo( 8 ); |
0 | 4828 |
$phpinfo = ob_get_clean(); |
9 | 4829 |
if ( false !== strpos( $phpinfo, $mod ) ) { |
4830 |
return true; |
|
4831 |
} |
|
0 | 4832 |
} |
4833 |
return $default; |
|
4834 |
} |
|
4835 |
||
4836 |
/** |
|
4837 |
* Check if IIS 7+ supports pretty permalinks. |
|
4838 |
* |
|
4839 |
* @since 2.8.0 |
|
4840 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4841 |
* @global bool $is_iis7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4842 |
* |
5 | 4843 |
* @return bool Whether IIS7 supports permalinks. |
0 | 4844 |
*/ |
4845 |
function iis7_supports_permalinks() { |
|
4846 |
global $is_iis7; |
|
4847 |
||
4848 |
$supports_permalinks = false; |
|
4849 |
if ( $is_iis7 ) { |
|
4850 |
/* First we check if the DOMDocument class exists. If it does not exist, then we cannot |
|
4851 |
* easily update the xml configuration file, hence we just bail out and tell user that |
|
4852 |
* pretty permalinks cannot be used. |
|
4853 |
* |
|
4854 |
* Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When |
|
4855 |
* URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'. |
|
4856 |
* Lastly we make sure that PHP is running via FastCGI. This is important because if it runs |
|
4857 |
* via ISAPI then pretty permalinks will not work. |
|
4858 |
*/ |
|
9 | 4859 |
$supports_permalinks = class_exists( 'DOMDocument', false ) && isset( $_SERVER['IIS_UrlRewriteModule'] ) && ( PHP_SAPI == 'cgi-fcgi' ); |
0 | 4860 |
} |
4861 |
||
5 | 4862 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4863 |
* Filters whether IIS 7+ supports pretty permalinks. |
5 | 4864 |
* |
4865 |
* @since 2.8.0 |
|
4866 |
* |
|
4867 |
* @param bool $supports_permalinks Whether IIS7 supports permalinks. Default false. |
|
4868 |
*/ |
|
4869 |
return apply_filters( 'iis7_supports_permalinks', $supports_permalinks ); |
|
0 | 4870 |
} |
4871 |
||
4872 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4873 |
* 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
|
4874 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4875 |
* 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
|
4876 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4877 |
* 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
|
4878 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4879 |
* A return value of `3` means the file is not in the allowed files list. |
0 | 4880 |
* |
4881 |
* @since 1.2.0 |
|
4882 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4883 |
* @param string $file File path. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4884 |
* @param array $allowed_files Optional. List of allowed files. |
0 | 4885 |
* @return int 0 means nothing is wrong, greater than 0 means something was wrong. |
4886 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4887 |
function validate_file( $file, $allowed_files = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4888 |
// `../` on its own is not allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4889 |
if ( '../' === $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4890 |
return 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4891 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4892 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4893 |
// More than one occurence of `../` is not allowed: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4894 |
if ( preg_match_all( '#\.\./#', $file, $matches, PREG_SET_ORDER ) && ( count( $matches ) > 1 ) ) { |
0 | 4895 |
return 1; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4896 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4897 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4898 |
// `../` 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
|
4899 |
if ( false !== strpos( $file, '../' ) && '../' !== mb_substr( $file, -3, 3 ) ) { |
0 | 4900 |
return 1; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4901 |
} |
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 |
// Files not in the allowed file list are not allowed: |
9 | 4904 |
if ( ! empty( $allowed_files ) && ! in_array( $file, $allowed_files ) ) { |
0 | 4905 |
return 3; |
9 | 4906 |
} |
0 | 4907 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4908 |
// Absolute Windows drive paths are not allowed: |
9 | 4909 |
if ( ':' == substr( $file, 1, 1 ) ) { |
0 | 4910 |
return 2; |
9 | 4911 |
} |
0 | 4912 |
|
4913 |
return 0; |
|
4914 |
} |
|
4915 |
||
4916 |
/** |
|
4917 |
* Whether to force SSL used for the Administration Screens. |
|
4918 |
* |
|
4919 |
* @since 2.6.0 |
|
4920 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4921 |
* @staticvar bool $forced |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4922 |
* |
5 | 4923 |
* @param string|bool $force Optional. Whether to force SSL in admin screens. Default null. |
0 | 4924 |
* @return bool True if forced, false if not forced. |
4925 |
*/ |
|
4926 |
function force_ssl_admin( $force = null ) { |
|
4927 |
static $forced = false; |
|
4928 |
||
9 | 4929 |
if ( ! is_null( $force ) ) { |
0 | 4930 |
$old_forced = $forced; |
9 | 4931 |
$forced = $force; |
0 | 4932 |
return $old_forced; |
4933 |
} |
|
4934 |
||
4935 |
return $forced; |
|
4936 |
} |
|
4937 |
||
4938 |
/** |
|
4939 |
* Guess the URL for the site. |
|
4940 |
* |
|
4941 |
* Will remove wp-admin links to retrieve only return URLs not in the wp-admin |
|
4942 |
* directory. |
|
4943 |
* |
|
4944 |
* @since 2.6.0 |
|
4945 |
* |
|
5 | 4946 |
* @return string The guessed URL. |
0 | 4947 |
*/ |
4948 |
function wp_guess_url() { |
|
9 | 4949 |
if ( defined( 'WP_SITEURL' ) && '' != WP_SITEURL ) { |
0 | 4950 |
$url = WP_SITEURL; |
4951 |
} else { |
|
9 | 4952 |
$abspath_fix = str_replace( '\\', '/', ABSPATH ); |
0 | 4953 |
$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ); |
4954 |
||
4955 |
// The request is for the admin |
|
4956 |
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'wp-login.php' ) !== false ) { |
|
4957 |
$path = preg_replace( '#/(wp-admin/.*|wp-login.php)#i', '', $_SERVER['REQUEST_URI'] ); |
|
4958 |
||
9 | 4959 |
// The request is for a file in ABSPATH |
0 | 4960 |
} elseif ( $script_filename_dir . '/' == $abspath_fix ) { |
4961 |
// Strip off any file/query params in the path |
|
4962 |
$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] ); |
|
4963 |
||
4964 |
} else { |
|
4965 |
if ( false !== strpos( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) { |
|
4966 |
// Request is hitting a file inside ABSPATH |
|
4967 |
$directory = str_replace( ABSPATH, '', $script_filename_dir ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4968 |
// Strip off the sub directory, and any file/query params |
9 | 4969 |
$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ); |
0 | 4970 |
} elseif ( false !== strpos( $abspath_fix, $script_filename_dir ) ) { |
4971 |
// Request is hitting a file above ABSPATH |
|
5 | 4972 |
$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4973 |
// Strip off any file/query params from the path, appending the sub directory to the installation |
9 | 4974 |
$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory; |
0 | 4975 |
} else { |
4976 |
$path = $_SERVER['REQUEST_URI']; |
|
4977 |
} |
|
4978 |
} |
|
4979 |
||
4980 |
$schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet |
|
9 | 4981 |
$url = $schema . $_SERVER['HTTP_HOST'] . $path; |
4982 |
} |
|
4983 |
||
4984 |
return rtrim( $url, '/' ); |
|
0 | 4985 |
} |
4986 |
||
4987 |
/** |
|
4988 |
* Temporarily suspend cache additions. |
|
4989 |
* |
|
4990 |
* Stops more data being added to the cache, but still allows cache retrieval. |
|
4991 |
* This is useful for actions, such as imports, when a lot of data would otherwise |
|
4992 |
* be almost uselessly added to the cache. |
|
4993 |
* |
|
4994 |
* Suspension lasts for a single page load at most. Remember to call this |
|
4995 |
* function again if you wish to re-enable cache adds earlier. |
|
4996 |
* |
|
4997 |
* @since 3.3.0 |
|
4998 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4999 |
* @staticvar bool $_suspend |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5000 |
* |
0 | 5001 |
* @param bool $suspend Optional. Suspends additions if true, re-enables them if false. |
5002 |
* @return bool The current suspend setting |
|
5003 |
*/ |
|
5004 |
function wp_suspend_cache_addition( $suspend = null ) { |
|
5005 |
static $_suspend = false; |
|
5006 |
||
9 | 5007 |
if ( is_bool( $suspend ) ) { |
0 | 5008 |
$_suspend = $suspend; |
9 | 5009 |
} |
0 | 5010 |
|
5011 |
return $_suspend; |
|
5012 |
} |
|
5013 |
||
5014 |
/** |
|
5015 |
* Suspend cache invalidation. |
|
5016 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5017 |
* Turns cache invalidation on and off. Useful during imports where you don't want to do |
5 | 5018 |
* invalidations every time a post is inserted. Callers must be sure that what they are |
5019 |
* doing won't lead to an inconsistent cache when invalidation is suspended. |
|
0 | 5020 |
* |
5021 |
* @since 2.7.0 |
|
5022 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5023 |
* @global bool $_wp_suspend_cache_invalidation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5024 |
* |
5 | 5025 |
* @param bool $suspend Optional. Whether to suspend or enable cache invalidation. Default true. |
5026 |
* @return bool The current suspend setting. |
|
0 | 5027 |
*/ |
5 | 5028 |
function wp_suspend_cache_invalidation( $suspend = true ) { |
0 | 5029 |
global $_wp_suspend_cache_invalidation; |
5030 |
||
9 | 5031 |
$current_suspend = $_wp_suspend_cache_invalidation; |
0 | 5032 |
$_wp_suspend_cache_invalidation = $suspend; |
5033 |
return $current_suspend; |
|
5034 |
} |
|
5035 |
||
5036 |
/** |
|
5 | 5037 |
* Determine whether a site is the main site of the current network. |
0 | 5038 |
* |
5039 |
* @since 3.0.0 |
|
9 | 5040 |
* @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
|
5041 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5042 |
* @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
|
5043 |
* @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
|
5044 |
* Defaults to current network. |
5 | 5045 |
* @return bool True if $site_id is the main site of the network, or if not |
5046 |
* running Multisite. |
|
0 | 5047 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5048 |
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
|
5049 |
if ( ! is_multisite() ) { |
0 | 5050 |
return true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5051 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5052 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5053 |
if ( ! $site_id ) { |
0 | 5054 |
$site_id = get_current_blog_id(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5055 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5056 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5057 |
$site_id = (int) $site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5058 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5059 |
return $site_id === get_main_site_id( $network_id ); |
0 | 5060 |
} |
5061 |
||
5062 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5063 |
* Gets the main site ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5064 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5065 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5066 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5067 |
* @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
|
5068 |
* Defaults to the current network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5069 |
* @return int The ID of the main site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5070 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5071 |
function get_main_site_id( $network_id = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5072 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5073 |
return get_current_blog_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5074 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5075 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5076 |
$network = get_network( $network_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5077 |
if ( ! $network ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5078 |
return 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5079 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5080 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5081 |
return $network->site_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5082 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5083 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5084 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5085 |
* Determine whether a network is the main network of the Multisite installation. |
0 | 5086 |
* |
5087 |
* @since 3.7.0 |
|
5088 |
* |
|
5089 |
* @param int $network_id Optional. Network ID to test. Defaults to current network. |
|
5 | 5090 |
* @return bool True if $network_id is the main network, or if not running Multisite. |
0 | 5091 |
*/ |
5092 |
function is_main_network( $network_id = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5093 |
if ( ! is_multisite() ) { |
0 | 5094 |
return true; |
7
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5097 |
if ( null === $network_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5098 |
$network_id = get_current_network_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5099 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5100 |
|
0 | 5101 |
$network_id = (int) $network_id; |
5102 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5103 |
return ( $network_id === get_main_network_id() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5104 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5107 |
* Get the main network ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5108 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5109 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5110 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5111 |
* @return int The ID of the main network. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5112 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5113 |
function get_main_network_id() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5114 |
if ( ! is_multisite() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5115 |
return 1; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5118 |
$current_network = get_network(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5119 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5120 |
if ( defined( 'PRIMARY_NETWORK_ID' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5121 |
$main_network_id = PRIMARY_NETWORK_ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5122 |
} 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
|
5123 |
// 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
|
5124 |
$main_network_id = 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5125 |
} else { |
9 | 5126 |
$_networks = get_networks( |
5127 |
array( |
|
5128 |
'fields' => 'ids', |
|
5129 |
'number' => 1, |
|
5130 |
) |
|
5131 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5132 |
$main_network_id = array_shift( $_networks ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5133 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5134 |
|
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 |
* Filters the main network ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5137 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5138 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5139 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5140 |
* @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
|
5141 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5142 |
return (int) apply_filters( 'get_main_network_id', $main_network_id ); |
0 | 5143 |
} |
5144 |
||
5145 |
/** |
|
5 | 5146 |
* Determine whether global terms are enabled. |
0 | 5147 |
* |
5148 |
* @since 3.0.0 |
|
5 | 5149 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5150 |
* @staticvar bool $global_terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5151 |
* |
5 | 5152 |
* @return bool True if multisite and global terms enabled. |
0 | 5153 |
*/ |
5154 |
function global_terms_enabled() { |
|
9 | 5155 |
if ( ! is_multisite() ) { |
0 | 5156 |
return false; |
9 | 5157 |
} |
0 | 5158 |
|
5159 |
static $global_terms = null; |
|
5160 |
if ( is_null( $global_terms ) ) { |
|
5 | 5161 |
|
5162 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5163 |
* Filters whether global terms are enabled. |
5 | 5164 |
* |
5165 |
* Passing a non-null value to the filter will effectively short-circuit the function, |
|
5166 |
* returning the value of the 'global_terms_enabled' site option instead. |
|
5167 |
* |
|
5168 |
* @since 3.0.0 |
|
5169 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5170 |
* @param null $enabled Whether global terms are enabled. |
5 | 5171 |
*/ |
0 | 5172 |
$filter = apply_filters( 'global_terms_enabled', null ); |
9 | 5173 |
if ( ! is_null( $filter ) ) { |
0 | 5174 |
$global_terms = (bool) $filter; |
9 | 5175 |
} else { |
0 | 5176 |
$global_terms = (bool) get_site_option( 'global_terms_enabled', false ); |
9 | 5177 |
} |
0 | 5178 |
} |
5179 |
return $global_terms; |
|
5180 |
} |
|
5181 |
||
5182 |
/** |
|
9 | 5183 |
* Determines whether site meta is enabled. |
5184 |
* |
|
5185 |
* This function checks whether the 'blogmeta' database table exists. The result is saved as |
|
5186 |
* a setting for the main network, making it essentially a global setting. Subsequent requests |
|
5187 |
* will refer to this setting instead of running the query. |
|
5188 |
* |
|
5189 |
* @since 5.1.0 |
|
5190 |
* |
|
5191 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5192 |
* |
|
5193 |
* @return bool True if site meta is supported, false otherwise. |
|
5194 |
*/ |
|
5195 |
function is_site_meta_supported() { |
|
5196 |
global $wpdb; |
|
5197 |
||
5198 |
if ( ! is_multisite() ) { |
|
5199 |
return false; |
|
5200 |
} |
|
5201 |
||
5202 |
$network_id = get_main_network_id(); |
|
5203 |
||
5204 |
$supported = get_network_option( $network_id, 'site_meta_supported', false ); |
|
5205 |
if ( false === $supported ) { |
|
5206 |
$supported = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->blogmeta}'" ) ? 1 : 0; |
|
5207 |
||
5208 |
update_network_option( $network_id, 'site_meta_supported', $supported ); |
|
5209 |
} |
|
5210 |
||
5211 |
return (bool) $supported; |
|
5212 |
} |
|
5213 |
||
5214 |
/** |
|
0 | 5215 |
* gmt_offset modification for smart timezone handling. |
5216 |
* |
|
5217 |
* Overrides the gmt_offset option if we have a timezone_string available. |
|
5218 |
* |
|
5219 |
* @since 2.8.0 |
|
5220 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5221 |
* @return float|false Timezone GMT offset, false otherwise. |
0 | 5222 |
*/ |
5223 |
function wp_timezone_override_offset() { |
|
9 | 5224 |
if ( ! $timezone_string = get_option( 'timezone_string' ) ) { |
0 | 5225 |
return false; |
5226 |
} |
|
5227 |
||
5228 |
$timezone_object = timezone_open( $timezone_string ); |
|
5229 |
$datetime_object = date_create(); |
|
5230 |
if ( false === $timezone_object || false === $datetime_object ) { |
|
5231 |
return false; |
|
5232 |
} |
|
5233 |
return round( timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS, 2 ); |
|
5234 |
} |
|
5235 |
||
5236 |
/** |
|
5237 |
* Sort-helper for timezones. |
|
5238 |
* |
|
5239 |
* @since 2.9.0 |
|
5 | 5240 |
* @access private |
0 | 5241 |
* |
5242 |
* @param array $a |
|
5243 |
* @param array $b |
|
5244 |
* @return int |
|
5245 |
*/ |
|
5246 |
function _wp_timezone_choice_usort_callback( $a, $b ) { |
|
5247 |
// Don't use translated versions of Etc |
|
5248 |
if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) { |
|
5249 |
// Make the order of these more like the old dropdown |
|
5250 |
if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) { |
|
5251 |
return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) ); |
|
5252 |
} |
|
5253 |
if ( 'UTC' === $a['city'] ) { |
|
5254 |
if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) { |
|
5255 |
return 1; |
|
5256 |
} |
|
5257 |
return -1; |
|
5258 |
} |
|
5259 |
if ( 'UTC' === $b['city'] ) { |
|
5260 |
if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) { |
|
5261 |
return -1; |
|
5262 |
} |
|
5263 |
return 1; |
|
5264 |
} |
|
5265 |
return strnatcasecmp( $a['city'], $b['city'] ); |
|
5266 |
} |
|
5267 |
if ( $a['t_continent'] == $b['t_continent'] ) { |
|
5268 |
if ( $a['t_city'] == $b['t_city'] ) { |
|
5269 |
return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] ); |
|
5270 |
} |
|
5271 |
return strnatcasecmp( $a['t_city'], $b['t_city'] ); |
|
5272 |
} else { |
|
5273 |
// Force Etc to the bottom of the list |
|
5274 |
if ( 'Etc' === $a['continent'] ) { |
|
5275 |
return 1; |
|
5276 |
} |
|
5277 |
if ( 'Etc' === $b['continent'] ) { |
|
5278 |
return -1; |
|
5279 |
} |
|
5280 |
return strnatcasecmp( $a['t_continent'], $b['t_continent'] ); |
|
5281 |
} |
|
5282 |
} |
|
5283 |
||
5284 |
/** |
|
5 | 5285 |
* Gives a nicely-formatted list of timezone strings. |
0 | 5286 |
* |
5287 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5288 |
* @since 4.7.0 Added the `$locale` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5289 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5290 |
* @staticvar bool $mo_loaded |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5291 |
* @staticvar string $locale_loaded |
0 | 5292 |
* |
5 | 5293 |
* @param string $selected_zone Selected timezone. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5294 |
* @param string $locale Optional. Locale to load the timezones in. Default current site locale. |
0 | 5295 |
* @return string |
5296 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5297 |
function wp_timezone_choice( $selected_zone, $locale = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5298 |
static $mo_loaded = false, $locale_loaded = null; |
0 | 5299 |
|
9 | 5300 |
$continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' ); |
0 | 5301 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5302 |
// Load translations for continents and cities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5303 |
if ( ! $mo_loaded || $locale !== $locale_loaded ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5304 |
$locale_loaded = $locale ? $locale : get_locale(); |
9 | 5305 |
$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
|
5306 |
unload_textdomain( 'continents-cities' ); |
0 | 5307 |
load_textdomain( 'continents-cities', $mofile ); |
5308 |
$mo_loaded = true; |
|
5309 |
} |
|
5310 |
||
5311 |
$zonen = array(); |
|
5312 |
foreach ( timezone_identifiers_list() as $zone ) { |
|
5313 |
$zone = explode( '/', $zone ); |
|
9 | 5314 |
if ( ! in_array( $zone[0], $continents ) ) { |
0 | 5315 |
continue; |
5316 |
} |
|
5317 |
||
5318 |
// This determines what gets set and translated - we don't translate Etc/* strings here, they are done later |
|
9 | 5319 |
$exists = array( |
0 | 5320 |
0 => ( isset( $zone[0] ) && $zone[0] ), |
5321 |
1 => ( isset( $zone[1] ) && $zone[1] ), |
|
5322 |
2 => ( isset( $zone[2] ) && $zone[2] ), |
|
5323 |
); |
|
5324 |
$exists[3] = ( $exists[0] && 'Etc' !== $zone[0] ); |
|
5325 |
$exists[4] = ( $exists[1] && $exists[3] ); |
|
5326 |
$exists[5] = ( $exists[2] && $exists[3] ); |
|
5327 |
||
9 | 5328 |
// phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText |
0 | 5329 |
$zonen[] = array( |
5330 |
'continent' => ( $exists[0] ? $zone[0] : '' ), |
|
5331 |
'city' => ( $exists[1] ? $zone[1] : '' ), |
|
5332 |
'subcity' => ( $exists[2] ? $zone[2] : '' ), |
|
5333 |
't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ), |
|
5334 |
't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ), |
|
9 | 5335 |
't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ), |
0 | 5336 |
); |
9 | 5337 |
// phpcs:enable |
0 | 5338 |
} |
5339 |
usort( $zonen, '_wp_timezone_choice_usort_callback' ); |
|
5340 |
||
5341 |
$structure = array(); |
|
5342 |
||
5343 |
if ( empty( $selected_zone ) ) { |
|
5344 |
$structure[] = '<option selected="selected" value="">' . __( 'Select a city' ) . '</option>'; |
|
5345 |
} |
|
5346 |
||
5347 |
foreach ( $zonen as $key => $zone ) { |
|
5348 |
// Build value in an array to join later |
|
5349 |
$value = array( $zone['continent'] ); |
|
5350 |
||
5351 |
if ( empty( $zone['city'] ) ) { |
|
5352 |
// It's at the continent level (generally won't happen) |
|
5353 |
$display = $zone['t_continent']; |
|
5354 |
} else { |
|
5355 |
// It's inside a continent group |
|
5356 |
||
5357 |
// Continent optgroup |
|
9 | 5358 |
if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) { |
5359 |
$label = $zone['t_continent']; |
|
5360 |
$structure[] = '<optgroup label="' . esc_attr( $label ) . '">'; |
|
0 | 5361 |
} |
5362 |
||
5363 |
// Add the city to the value |
|
5364 |
$value[] = $zone['city']; |
|
5365 |
||
5366 |
$display = $zone['t_city']; |
|
9 | 5367 |
if ( ! empty( $zone['subcity'] ) ) { |
0 | 5368 |
// Add the subcity to the value |
9 | 5369 |
$value[] = $zone['subcity']; |
0 | 5370 |
$display .= ' - ' . $zone['t_subcity']; |
5371 |
} |
|
5372 |
} |
|
5373 |
||
5374 |
// Build the value |
|
9 | 5375 |
$value = join( '/', $value ); |
0 | 5376 |
$selected = ''; |
5377 |
if ( $value === $selected_zone ) { |
|
5378 |
$selected = 'selected="selected" '; |
|
5379 |
} |
|
9 | 5380 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $display ) . '</option>'; |
0 | 5381 |
|
5382 |
// Close continent optgroup |
|
9 | 5383 |
if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) { |
0 | 5384 |
$structure[] = '</optgroup>'; |
5385 |
} |
|
5386 |
} |
|
5387 |
||
5388 |
// Do UTC |
|
9 | 5389 |
$structure[] = '<optgroup label="' . esc_attr__( 'UTC' ) . '">'; |
5390 |
$selected = ''; |
|
5391 |
if ( 'UTC' === $selected_zone ) { |
|
0 | 5392 |
$selected = 'selected="selected" '; |
9 | 5393 |
} |
5394 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC' ) . '</option>'; |
|
0 | 5395 |
$structure[] = '</optgroup>'; |
5396 |
||
5397 |
// Do manual UTC offsets |
|
9 | 5398 |
$structure[] = '<optgroup label="' . esc_attr__( 'Manual Offsets' ) . '">'; |
5399 |
$offset_range = array( |
|
5400 |
-12, |
|
5401 |
-11.5, |
|
5402 |
-11, |
|
5403 |
-10.5, |
|
5404 |
-10, |
|
5405 |
-9.5, |
|
5406 |
-9, |
|
5407 |
-8.5, |
|
5408 |
-8, |
|
5409 |
-7.5, |
|
5410 |
-7, |
|
5411 |
-6.5, |
|
5412 |
-6, |
|
5413 |
-5.5, |
|
5414 |
-5, |
|
5415 |
-4.5, |
|
5416 |
-4, |
|
5417 |
-3.5, |
|
5418 |
-3, |
|
5419 |
-2.5, |
|
5420 |
-2, |
|
5421 |
-1.5, |
|
5422 |
-1, |
|
5423 |
-0.5, |
|
5424 |
0, |
|
5425 |
0.5, |
|
5426 |
1, |
|
5427 |
1.5, |
|
5428 |
2, |
|
5429 |
2.5, |
|
5430 |
3, |
|
5431 |
3.5, |
|
5432 |
4, |
|
5433 |
4.5, |
|
5434 |
5, |
|
5435 |
5.5, |
|
5436 |
5.75, |
|
5437 |
6, |
|
5438 |
6.5, |
|
5439 |
7, |
|
5440 |
7.5, |
|
5441 |
8, |
|
5442 |
8.5, |
|
5443 |
8.75, |
|
5444 |
9, |
|
5445 |
9.5, |
|
5446 |
10, |
|
5447 |
10.5, |
|
5448 |
11, |
|
5449 |
11.5, |
|
5450 |
12, |
|
5451 |
12.75, |
|
5452 |
13, |
|
5453 |
13.75, |
|
5454 |
14, |
|
5455 |
); |
|
0 | 5456 |
foreach ( $offset_range as $offset ) { |
9 | 5457 |
if ( 0 <= $offset ) { |
0 | 5458 |
$offset_name = '+' . $offset; |
9 | 5459 |
} else { |
0 | 5460 |
$offset_name = (string) $offset; |
9 | 5461 |
} |
0 | 5462 |
|
5463 |
$offset_value = $offset_name; |
|
9 | 5464 |
$offset_name = str_replace( array( '.25', '.5', '.75' ), array( ':15', ':30', ':45' ), $offset_name ); |
5465 |
$offset_name = 'UTC' . $offset_name; |
|
0 | 5466 |
$offset_value = 'UTC' . $offset_value; |
9 | 5467 |
$selected = ''; |
5468 |
if ( $offset_value === $selected_zone ) { |
|
0 | 5469 |
$selected = 'selected="selected" '; |
9 | 5470 |
} |
5471 |
$structure[] = '<option ' . $selected . 'value="' . esc_attr( $offset_value ) . '">' . esc_html( $offset_name ) . '</option>'; |
|
0 | 5472 |
|
5473 |
} |
|
5474 |
$structure[] = '</optgroup>'; |
|
5475 |
||
5476 |
return join( "\n", $structure ); |
|
5477 |
} |
|
5478 |
||
5479 |
/** |
|
5480 |
* Strip close comment and close php tags from file headers used by WP. |
|
5481 |
* |
|
5482 |
* @since 2.8.0 |
|
5 | 5483 |
* @access private |
5484 |
* |
|
5485 |
* @see https://core.trac.wordpress.org/ticket/8497 |
|
5486 |
* |
|
5487 |
* @param string $str Header comment to clean up. |
|
0 | 5488 |
* @return string |
5489 |
*/ |
|
5 | 5490 |
function _cleanup_header_comment( $str ) { |
9 | 5491 |
return trim( preg_replace( '/\s*(?:\*\/|\?>).*/', '', $str ) ); |
0 | 5492 |
} |
5493 |
||
5494 |
/** |
|
5 | 5495 |
* Permanently delete comments or posts of any type that have held a status |
5496 |
* of 'trash' for the number of days defined in EMPTY_TRASH_DAYS. |
|
5497 |
* |
|
5498 |
* The default value of `EMPTY_TRASH_DAYS` is 30 (days). |
|
0 | 5499 |
* |
5500 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5501 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5502 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 5503 |
*/ |
5504 |
function wp_scheduled_delete() { |
|
5505 |
global $wpdb; |
|
5506 |
||
5507 |
$delete_timestamp = time() - ( DAY_IN_SECONDS * EMPTY_TRASH_DAYS ); |
|
5508 |
||
9 | 5509 |
$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 | 5510 |
|
5511 |
foreach ( (array) $posts_to_delete as $post ) { |
|
5512 |
$post_id = (int) $post['post_id']; |
|
9 | 5513 |
if ( ! $post_id ) { |
0 | 5514 |
continue; |
9 | 5515 |
} |
5516 |
||
5517 |
$del_post = get_post( $post_id ); |
|
5518 |
||
5519 |
if ( ! $del_post || 'trash' != $del_post->post_status ) { |
|
5520 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
|
5521 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|
0 | 5522 |
} else { |
9 | 5523 |
wp_delete_post( $post_id ); |
0 | 5524 |
} |
5525 |
} |
|
5526 |
||
9 | 5527 |
$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 | 5528 |
|
5529 |
foreach ( (array) $comments_to_delete as $comment ) { |
|
5530 |
$comment_id = (int) $comment['comment_id']; |
|
9 | 5531 |
if ( ! $comment_id ) { |
0 | 5532 |
continue; |
9 | 5533 |
} |
5534 |
||
5535 |
$del_comment = get_comment( $comment_id ); |
|
5536 |
||
5537 |
if ( ! $del_comment || 'trash' != $del_comment->comment_approved ) { |
|
5538 |
delete_comment_meta( $comment_id, '_wp_trash_meta_time' ); |
|
5539 |
delete_comment_meta( $comment_id, '_wp_trash_meta_status' ); |
|
0 | 5540 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5541 |
wp_delete_comment( $del_comment ); |
0 | 5542 |
} |
5543 |
} |
|
5544 |
} |
|
5545 |
||
5546 |
/** |
|
5547 |
* Retrieve metadata from a file. |
|
5548 |
* |
|
5549 |
* Searches for metadata in the first 8kiB of a file, such as a plugin or theme. |
|
5550 |
* Each piece of metadata must be on its own line. Fields can not span multiple |
|
5551 |
* lines, the value will get cut at the end of the first line. |
|
5552 |
* |
|
5553 |
* If the file data is not within that first 8kiB, then the author should correct |
|
5554 |
* their plugin file and move the data headers to the top. |
|
5555 |
* |
|
5 | 5556 |
* @link https://codex.wordpress.org/File_Header |
0 | 5557 |
* |
5558 |
* @since 2.9.0 |
|
5 | 5559 |
* |
9 | 5560 |
* @param string $file Absolute path to the file. |
5561 |
* @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
|
5562 |
* @param string $context Optional. If specified adds filter hook {@see 'extra_$context_headers'}. |
5 | 5563 |
* Default empty. |
5564 |
* @return array Array of file headers in `HeaderKey => Header Value` format. |
|
0 | 5565 |
*/ |
5566 |
function get_file_data( $file, $default_headers, $context = '' ) { |
|
5567 |
// We don't need to write to the file, so just open for reading. |
|
5568 |
$fp = fopen( $file, 'r' ); |
|
5569 |
||
5570 |
// Pull only the first 8kiB of the file in. |
|
5571 |
$file_data = fread( $fp, 8192 ); |
|
5572 |
||
5573 |
// PHP will close file handle, but we are good citizens. |
|
5574 |
fclose( $fp ); |
|
5575 |
||
5576 |
// Make sure we catch CR-only line endings. |
|
5577 |
$file_data = str_replace( "\r", "\n", $file_data ); |
|
5578 |
||
5 | 5579 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5580 |
* Filters extra file headers by context. |
5 | 5581 |
* |
5582 |
* The dynamic portion of the hook name, `$context`, refers to |
|
5583 |
* the context where extra headers might be loaded. |
|
5584 |
* |
|
5585 |
* @since 2.9.0 |
|
5586 |
* |
|
5587 |
* @param array $extra_context_headers Empty array by default. |
|
5588 |
*/ |
|
0 | 5589 |
if ( $context && $extra_headers = apply_filters( "extra_{$context}_headers", array() ) ) { |
5590 |
$extra_headers = array_combine( $extra_headers, $extra_headers ); // keys equal values |
|
9 | 5591 |
$all_headers = array_merge( $extra_headers, (array) $default_headers ); |
0 | 5592 |
} else { |
5593 |
$all_headers = $default_headers; |
|
5594 |
} |
|
5595 |
||
5596 |
foreach ( $all_headers as $field => $regex ) { |
|
9 | 5597 |
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) { |
0 | 5598 |
$all_headers[ $field ] = _cleanup_header_comment( $match[1] ); |
9 | 5599 |
} else { |
0 | 5600 |
$all_headers[ $field ] = ''; |
9 | 5601 |
} |
0 | 5602 |
} |
5603 |
||
5604 |
return $all_headers; |
|
5605 |
} |
|
5606 |
||
5607 |
/** |
|
5608 |
* Returns true. |
|
5609 |
* |
|
5610 |
* Useful for returning true to filters easily. |
|
5611 |
* |
|
5612 |
* @since 3.0.0 |
|
5 | 5613 |
* |
0 | 5614 |
* @see __return_false() |
5 | 5615 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5616 |
* @return true True. |
0 | 5617 |
*/ |
5618 |
function __return_true() { |
|
5619 |
return true; |
|
5620 |
} |
|
5621 |
||
5622 |
/** |
|
5623 |
* Returns false. |
|
5624 |
* |
|
5625 |
* Useful for returning false to filters easily. |
|
5626 |
* |
|
5627 |
* @since 3.0.0 |
|
5 | 5628 |
* |
0 | 5629 |
* @see __return_true() |
5 | 5630 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5631 |
* @return false False. |
0 | 5632 |
*/ |
5633 |
function __return_false() { |
|
5634 |
return false; |
|
5635 |
} |
|
5636 |
||
5637 |
/** |
|
5638 |
* Returns 0. |
|
5639 |
* |
|
5640 |
* Useful for returning 0 to filters easily. |
|
5641 |
* |
|
5642 |
* @since 3.0.0 |
|
5 | 5643 |
* |
5644 |
* @return int 0. |
|
0 | 5645 |
*/ |
5646 |
function __return_zero() { |
|
5647 |
return 0; |
|
5648 |
} |
|
5649 |
||
5650 |
/** |
|
5651 |
* Returns an empty array. |
|
5652 |
* |
|
5653 |
* Useful for returning an empty array to filters easily. |
|
5654 |
* |
|
5655 |
* @since 3.0.0 |
|
5 | 5656 |
* |
5657 |
* @return array Empty array. |
|
0 | 5658 |
*/ |
5659 |
function __return_empty_array() { |
|
5660 |
return array(); |
|
5661 |
} |
|
5662 |
||
5663 |
/** |
|
5664 |
* Returns null. |
|
5665 |
* |
|
5666 |
* Useful for returning null to filters easily. |
|
5667 |
* |
|
5668 |
* @since 3.4.0 |
|
5 | 5669 |
* |
5670 |
* @return null Null value. |
|
0 | 5671 |
*/ |
5672 |
function __return_null() { |
|
5673 |
return null; |
|
5674 |
} |
|
5675 |
||
5676 |
/** |
|
5677 |
* Returns an empty string. |
|
5678 |
* |
|
5679 |
* Useful for returning an empty string to filters easily. |
|
5680 |
* |
|
5681 |
* @since 3.7.0 |
|
5 | 5682 |
* |
0 | 5683 |
* @see __return_null() |
5 | 5684 |
* |
5685 |
* @return string Empty string. |
|
0 | 5686 |
*/ |
5687 |
function __return_empty_string() { |
|
5688 |
return ''; |
|
5689 |
} |
|
5690 |
||
5691 |
/** |
|
5692 |
* Send a HTTP header to disable content type sniffing in browsers which support it. |
|
5693 |
* |
|
5694 |
* @since 3.0.0 |
|
5 | 5695 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5696 |
* @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
|
5697 |
* @see https://src.chromium.org/viewvc/chrome?view=rev&revision=6985 |
0 | 5698 |
*/ |
5699 |
function send_nosniff_header() { |
|
5700 |
@header( 'X-Content-Type-Options: nosniff' ); |
|
5701 |
} |
|
5702 |
||
5703 |
/** |
|
5 | 5704 |
* Return a MySQL expression for selecting the week number based on the start_of_week option. |
5705 |
* |
|
5706 |
* @ignore |
|
0 | 5707 |
* @since 3.0.0 |
5 | 5708 |
* |
5709 |
* @param string $column Database column. |
|
5710 |
* @return string SQL clause. |
|
0 | 5711 |
*/ |
5712 |
function _wp_mysql_week( $column ) { |
|
5713 |
switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) { |
|
9 | 5714 |
case 1: |
5715 |
return "WEEK( $column, 1 )"; |
|
5716 |
case 2: |
|
5717 |
case 3: |
|
5718 |
case 4: |
|
5719 |
case 5: |
|
5720 |
case 6: |
|
5721 |
return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )"; |
|
5722 |
case 0: |
|
5723 |
default: |
|
5724 |
return "WEEK( $column, 0 )"; |
|
0 | 5725 |
} |
5726 |
} |
|
5727 |
||
5728 |
/** |
|
5 | 5729 |
* Find hierarchy loops using a callback function that maps object IDs to parent IDs. |
0 | 5730 |
* |
5731 |
* @since 3.1.0 |
|
5732 |
* @access private |
|
5733 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5734 |
* @param callable $callback Function that accepts ( ID, $callback_args ) and outputs parent_ID. |
5 | 5735 |
* @param int $start The ID to start the loop check at. |
5736 |
* @param int $start_parent The parent_ID of $start to use instead of calling $callback( $start ). |
|
5737 |
* Use null to always use $callback |
|
5738 |
* @param array $callback_args Optional. Additional arguments to send to $callback. |
|
5739 |
* @return array IDs of all members of loop. |
|
0 | 5740 |
*/ |
5741 |
function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) { |
|
5742 |
$override = is_null( $start_parent ) ? array() : array( $start => $start_parent ); |
|
5743 |
||
9 | 5744 |
if ( ! $arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) ) { |
0 | 5745 |
return array(); |
9 | 5746 |
} |
0 | 5747 |
|
5748 |
return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true ); |
|
5749 |
} |
|
5750 |
||
5751 |
/** |
|
5 | 5752 |
* Use the "The Tortoise and the Hare" algorithm to detect loops. |
0 | 5753 |
* |
5754 |
* For every step of the algorithm, the hare takes two steps and the tortoise one. |
|
5755 |
* If the hare ever laps the tortoise, there must be a loop. |
|
5756 |
* |
|
5757 |
* @since 3.1.0 |
|
5758 |
* @access private |
|
5759 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5760 |
* @param callable $callback Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID. |
5 | 5761 |
* @param int $start The ID to start the loop check at. |
5762 |
* @param array $override Optional. An array of ( ID => parent_ID, ... ) to use instead of $callback. |
|
5763 |
* Default empty array. |
|
5764 |
* @param array $callback_args Optional. Additional arguments to send to $callback. Default empty array. |
|
5765 |
* @param bool $_return_loop Optional. Return loop members or just detect presence of loop? Only set |
|
5766 |
* to true if you already know the given $start is part of a loop (otherwise |
|
5767 |
* the returned array might include branches). Default false. |
|
5768 |
* @return mixed Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if |
|
5769 |
* $_return_loop |
|
0 | 5770 |
*/ |
5771 |
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) { |
|
5772 |
$tortoise = $hare = $evanescent_hare = $start; |
|
9 | 5773 |
$return = array(); |
0 | 5774 |
|
5775 |
// Set evanescent_hare to one past hare |
|
5776 |
// Increment hare two steps |
|
5777 |
while ( |
|
5778 |
$tortoise |
|
5779 |
&& |
|
9 | 5780 |
( $evanescent_hare = isset( $override[ $hare ] ) ? $override[ $hare ] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) ) |
0 | 5781 |
&& |
9 | 5782 |
( $hare = isset( $override[ $evanescent_hare ] ) ? $override[ $evanescent_hare ] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) ) |
0 | 5783 |
) { |
9 | 5784 |
if ( $_return_loop ) { |
5785 |
$return[ $tortoise ] = $return[ $evanescent_hare ] = $return[ $hare ] = true; |
|
5786 |
} |
|
0 | 5787 |
|
5788 |
// tortoise got lapped - must be a loop |
|
9 | 5789 |
if ( $tortoise == $evanescent_hare || $tortoise == $hare ) { |
0 | 5790 |
return $_return_loop ? $return : $tortoise; |
9 | 5791 |
} |
0 | 5792 |
|
5793 |
// Increment tortoise by one step |
|
9 | 5794 |
$tortoise = isset( $override[ $tortoise ] ) ? $override[ $tortoise ] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) ); |
0 | 5795 |
} |
5796 |
||
5797 |
return false; |
|
5798 |
} |
|
5799 |
||
5800 |
/** |
|
5801 |
* Send a HTTP header to limit rendering of pages to same origin iframes. |
|
5802 |
* |
|
5803 |
* @since 3.1.3 |
|
5 | 5804 |
* |
5805 |
* @see https://developer.mozilla.org/en/the_x-frame-options_response_header |
|
0 | 5806 |
*/ |
5807 |
function send_frame_options_header() { |
|
5808 |
@header( 'X-Frame-Options: SAMEORIGIN' ); |
|
5809 |
} |
|
5810 |
||
5811 |
/** |
|
5812 |
* Retrieve a list of protocols to allow in HTML attributes. |
|
5813 |
* |
|
5814 |
* @since 3.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5815 |
* @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
|
5816 |
* @since 4.7.0 Added 'urn' to the protocols array. |
5 | 5817 |
* |
0 | 5818 |
* @see wp_kses() |
5819 |
* @see esc_url() |
|
5820 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5821 |
* @staticvar array $protocols |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5822 |
* |
9 | 5823 |
* @return string[] Array of allowed protocols. Defaults to an array containing 'http', 'https', |
5824 |
* 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', |
|
5825 |
* 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', and 'urn'. This covers |
|
5826 |
* all common link protocols, except for 'javascript' which should not be |
|
5827 |
* allowed for untrusted users. |
|
0 | 5828 |
*/ |
5829 |
function wp_allowed_protocols() { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5830 |
static $protocols = array(); |
0 | 5831 |
|
5832 |
if ( empty( $protocols ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5833 |
$protocols = array( 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'fax', 'xmpp', 'webcal', 'urn' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5834 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5835 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5836 |
if ( ! did_action( 'wp_loaded' ) ) { |
5 | 5837 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5838 |
* Filters the list of protocols allowed in HTML attributes. |
5 | 5839 |
* |
5840 |
* @since 3.0.0 |
|
5841 |
* |
|
5842 |
* @param array $protocols Array of allowed protocols e.g. 'http', 'ftp', 'tel', and more. |
|
5843 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5844 |
$protocols = array_unique( (array) apply_filters( 'kses_allowed_protocols', $protocols ) ); |
0 | 5845 |
} |
5846 |
||
5847 |
return $protocols; |
|
5848 |
} |
|
5849 |
||
5850 |
/** |
|
5 | 5851 |
* Return a comma-separated string of functions that have been called to get |
5852 |
* to the current point in code. |
|
5853 |
* |
|
5854 |
* @since 3.4.0 |
|
5855 |
* |
|
5856 |
* @see https://core.trac.wordpress.org/ticket/19589 |
|
5857 |
* |
|
9 | 5858 |
* @staticvar array $truncate_paths Array of paths to truncate. |
5859 |
* |
|
5 | 5860 |
* @param string $ignore_class Optional. A class to ignore all function calls within - useful |
5861 |
* when you want to just give info about the callee. Default null. |
|
5862 |
* @param int $skip_frames Optional. A number of stack frames to skip - useful for unwinding |
|
5863 |
* back to the source of the issue. Default 0. |
|
5864 |
* @param bool $pretty Optional. Whether or not you want a comma separated string or raw |
|
5865 |
* array returned. Default true. |
|
5866 |
* @return string|array Either a string containing a reversed comma separated trace or an array |
|
5867 |
* of individual calls. |
|
0 | 5868 |
*/ |
5869 |
function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) { |
|
9 | 5870 |
static $truncate_paths; |
5871 |
||
5872 |
if ( version_compare( PHP_VERSION, '5.2.5', '>=' ) ) { |
|
0 | 5873 |
$trace = debug_backtrace( false ); |
9 | 5874 |
} else { |
0 | 5875 |
$trace = debug_backtrace(); |
9 | 5876 |
} |
5877 |
||
5878 |
$caller = array(); |
|
0 | 5879 |
$check_class = ! is_null( $ignore_class ); |
5880 |
$skip_frames++; // skip this function |
|
5881 |
||
9 | 5882 |
if ( ! isset( $truncate_paths ) ) { |
5883 |
$truncate_paths = array( |
|
5884 |
wp_normalize_path( WP_CONTENT_DIR ), |
|
5885 |
wp_normalize_path( ABSPATH ), |
|
5886 |
); |
|
5887 |
} |
|
5888 |
||
0 | 5889 |
foreach ( $trace as $call ) { |
5890 |
if ( $skip_frames > 0 ) { |
|
5891 |
$skip_frames--; |
|
5892 |
} elseif ( isset( $call['class'] ) ) { |
|
9 | 5893 |
if ( $check_class && $ignore_class == $call['class'] ) { |
0 | 5894 |
continue; // Filter out calls |
9 | 5895 |
} |
0 | 5896 |
|
5897 |
$caller[] = "{$call['class']}{$call['type']}{$call['function']}"; |
|
5898 |
} else { |
|
9 | 5899 |
if ( in_array( $call['function'], array( 'do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array' ) ) ) { |
0 | 5900 |
$caller[] = "{$call['function']}('{$call['args'][0]}')"; |
5901 |
} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) { |
|
9 | 5902 |
$filename = isset( $call['args'][0] ) ? $call['args'][0] : ''; |
5903 |
$caller[] = $call['function'] . "('" . str_replace( $truncate_paths, '', wp_normalize_path( $filename ) ) . "')"; |
|
0 | 5904 |
} else { |
5905 |
$caller[] = $call['function']; |
|
5906 |
} |
|
5907 |
} |
|
5908 |
} |
|
9 | 5909 |
if ( $pretty ) { |
0 | 5910 |
return join( ', ', array_reverse( $caller ) ); |
9 | 5911 |
} else { |
0 | 5912 |
return $caller; |
9 | 5913 |
} |
5914 |
} |
|
5915 |
||
5916 |
/** |
|
5917 |
* Retrieve IDs that are not already present in the cache. |
|
0 | 5918 |
* |
5919 |
* @since 3.4.0 |
|
5 | 5920 |
* @access private |
5921 |
* |
|
9 | 5922 |
* @param int[] $object_ids Array of IDs. |
5 | 5923 |
* @param string $cache_key The cache bucket to check against. |
9 | 5924 |
* @return int[] Array of IDs not present in the cache. |
0 | 5925 |
*/ |
5926 |
function _get_non_cached_ids( $object_ids, $cache_key ) { |
|
5927 |
$clean = array(); |
|
5928 |
foreach ( $object_ids as $id ) { |
|
5929 |
$id = (int) $id; |
|
9 | 5930 |
if ( ! wp_cache_get( $id, $cache_key ) ) { |
0 | 5931 |
$clean[] = $id; |
5932 |
} |
|
5933 |
} |
|
5934 |
||
5935 |
return $clean; |
|
5936 |
} |
|
5937 |
||
5938 |
/** |
|
5939 |
* Test if the current device has the capability to upload files. |
|
5940 |
* |
|
5941 |
* @since 3.4.0 |
|
5942 |
* @access private |
|
5943 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5944 |
* @return bool Whether the device is able to upload files. |
0 | 5945 |
*/ |
5946 |
function _device_can_upload() { |
|
9 | 5947 |
if ( ! wp_is_mobile() ) { |
0 | 5948 |
return true; |
9 | 5949 |
} |
0 | 5950 |
|
5951 |
$ua = $_SERVER['HTTP_USER_AGENT']; |
|
5952 |
||
9 | 5953 |
if ( strpos( $ua, 'iPhone' ) !== false |
5954 |
|| strpos( $ua, 'iPad' ) !== false |
|
5955 |
|| strpos( $ua, 'iPod' ) !== false ) { |
|
0 | 5956 |
return preg_match( '#OS ([\d_]+) like Mac OS X#', $ua, $version ) && version_compare( $version[1], '6', '>=' ); |
5957 |
} |
|
5958 |
||
5959 |
return true; |
|
5960 |
} |
|
5961 |
||
5962 |
/** |
|
5963 |
* Test if a given path is a stream URL |
|
5964 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5965 |
* @since 3.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5966 |
* |
5 | 5967 |
* @param string $path The resource path or URL. |
5968 |
* @return bool True if the path is a stream URL. |
|
0 | 5969 |
*/ |
5970 |
function wp_is_stream( $path ) { |
|
9 | 5971 |
$scheme_separator = strpos( $path, '://' ); |
5972 |
||
5973 |
if ( false === $scheme_separator ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5974 |
// $path isn't a stream |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5975 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5976 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5977 |
|
9 | 5978 |
$stream = substr( $path, 0, $scheme_separator ); |
5979 |
||
5980 |
return in_array( $stream, stream_get_wrappers(), true ); |
|
0 | 5981 |
} |
5982 |
||
5983 |
/** |
|
5 | 5984 |
* Test if the supplied date is valid for the Gregorian calendar. |
0 | 5985 |
* |
5986 |
* @since 3.5.0 |
|
5987 |
* |
|
9 | 5988 |
* @link https://secure.php.net/manual/en/function.checkdate.php |
5 | 5989 |
* |
5990 |
* @param int $month Month number. |
|
5991 |
* @param int $day Day number. |
|
5992 |
* @param int $year Year number. |
|
5993 |
* @param string $source_date The date to filter. |
|
5994 |
* @return bool True if valid date, false if not valid date. |
|
0 | 5995 |
*/ |
5996 |
function wp_checkdate( $month, $day, $year, $source_date ) { |
|
5 | 5997 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5998 |
* Filters whether the given date is valid for the Gregorian calendar. |
5 | 5999 |
* |
6000 |
* @since 3.5.0 |
|
6001 |
* |
|
6002 |
* @param bool $checkdate Whether the given date is valid. |
|
6003 |
* @param string $source_date Date to check. |
|
6004 |
*/ |
|
0 | 6005 |
return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); |
6006 |
} |
|
6007 |
||
6008 |
/** |
|
6009 |
* Load the auth check for monitoring whether the user is still logged in. |
|
6010 |
* |
|
6011 |
* Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' ); |
|
6012 |
* |
|
6013 |
* 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
|
6014 |
* inconvenient interruption. A filter called {@see 'wp_auth_check_load'} can be used |
0 | 6015 |
* for fine-grained control. |
6016 |
* |
|
6017 |
* @since 3.6.0 |
|
6018 |
*/ |
|
6019 |
function wp_auth_check_load() { |
|
9 | 6020 |
if ( ! is_admin() && ! is_user_logged_in() ) { |
0 | 6021 |
return; |
9 | 6022 |
} |
6023 |
||
6024 |
if ( defined( 'IFRAME_REQUEST' ) ) { |
|
0 | 6025 |
return; |
9 | 6026 |
} |
0 | 6027 |
|
6028 |
$screen = get_current_screen(); |
|
6029 |
$hidden = array( 'update', 'update-network', 'update-core', 'update-core-network', 'upgrade', 'upgrade-network', 'network' ); |
|
9 | 6030 |
$show = ! in_array( $screen->id, $hidden ); |
0 | 6031 |
|
5 | 6032 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6033 |
* Filters whether to load the authentication check. |
5 | 6034 |
* |
6035 |
* Passing a falsey value to the filter will effectively short-circuit |
|
6036 |
* loading the authentication check. |
|
6037 |
* |
|
6038 |
* @since 3.6.0 |
|
6039 |
* |
|
6040 |
* @param bool $show Whether to load the authentication check. |
|
6041 |
* @param WP_Screen $screen The current screen object. |
|
6042 |
*/ |
|
0 | 6043 |
if ( apply_filters( 'wp_auth_check_load', $show, $screen ) ) { |
6044 |
wp_enqueue_style( 'wp-auth-check' ); |
|
6045 |
wp_enqueue_script( 'wp-auth-check' ); |
|
6046 |
||
6047 |
add_action( 'admin_print_footer_scripts', 'wp_auth_check_html', 5 ); |
|
6048 |
add_action( 'wp_print_footer_scripts', 'wp_auth_check_html', 5 ); |
|
6049 |
} |
|
6050 |
} |
|
6051 |
||
6052 |
/** |
|
6053 |
* Output the HTML that shows the wp-login dialog when the user is no longer logged in. |
|
6054 |
* |
|
6055 |
* @since 3.6.0 |
|
6056 |
*/ |
|
6057 |
function wp_auth_check_html() { |
|
9 | 6058 |
$login_url = wp_login_url(); |
0 | 6059 |
$current_domain = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST']; |
9 | 6060 |
$same_domain = ( strpos( $login_url, $current_domain ) === 0 ); |
0 | 6061 |
|
5 | 6062 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6063 |
* Filters whether the authentication check originated at the same domain. |
5 | 6064 |
* |
6065 |
* @since 3.6.0 |
|
6066 |
* |
|
6067 |
* @param bool $same_domain Whether the authentication check originated at the same domain. |
|
6068 |
*/ |
|
0 | 6069 |
$same_domain = apply_filters( 'wp_auth_check_same_domain', $same_domain ); |
9 | 6070 |
$wrap_class = $same_domain ? 'hidden' : 'hidden fallback'; |
0 | 6071 |
|
6072 |
?> |
|
6073 |
<div id="wp-auth-check-wrap" class="<?php echo $wrap_class; ?>"> |
|
6074 |
<div id="wp-auth-check-bg"></div> |
|
6075 |
<div id="wp-auth-check"> |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6076 |
<button type="button" class="wp-auth-check-close button-link"><span class="screen-reader-text"><?php _e( 'Close dialog' ); ?></span></button> |
0 | 6077 |
<?php |
6078 |
||
6079 |
if ( $same_domain ) { |
|
9 | 6080 |
$login_src = add_query_arg( |
6081 |
array( |
|
6082 |
'interim-login' => '1', |
|
6083 |
'wp_lang' => get_user_locale(), |
|
6084 |
), |
|
6085 |
$login_url |
|
6086 |
); |
|
0 | 6087 |
?> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6088 |
<div id="wp-auth-check-form" class="loading" data-src="<?php echo esc_url( $login_src ); ?>"></div> |
0 | 6089 |
<?php |
6090 |
} |
|
6091 |
||
6092 |
?> |
|
6093 |
<div class="wp-auth-fallback"> |
|
9 | 6094 |
<p><b class="wp-auth-fallback-expired" tabindex="0"><?php _e( 'Session expired' ); ?></b></p> |
6095 |
<p><a href="<?php echo esc_url( $login_url ); ?>" target="_blank"><?php _e( 'Please log in again.' ); ?></a> |
|
6096 |
<?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 | 6097 |
</div> |
6098 |
</div> |
|
6099 |
</div> |
|
6100 |
<?php |
|
6101 |
} |
|
6102 |
||
6103 |
/** |
|
6104 |
* Check whether a user is still logged in, for the heartbeat. |
|
6105 |
* |
|
6106 |
* Send a result that shows a log-in box if the user is no longer logged in, |
|
6107 |
* or if their cookie is within the grace period. |
|
6108 |
* |
|
6109 |
* @since 3.6.0 |
|
5 | 6110 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6111 |
* @global int $login_grace_period |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6112 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6113 |
* @param array $response The Heartbeat response. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6114 |
* @return array $response The Heartbeat response with 'wp-auth-check' value set. |
0 | 6115 |
*/ |
5 | 6116 |
function wp_auth_check( $response ) { |
0 | 6117 |
$response['wp-auth-check'] = is_user_logged_in() && empty( $GLOBALS['login_grace_period'] ); |
6118 |
return $response; |
|
6119 |
} |
|
6120 |
||
6121 |
/** |
|
5 | 6122 |
* Return RegEx body to liberally match an opening HTML tag. |
6123 |
* |
|
6124 |
* Matches an opening HTML tag that: |
|
0 | 6125 |
* 1. Is self-closing or |
6126 |
* 2. Has no body but has a closing tag of the same name or |
|
6127 |
* 3. Contains a body and a closing tag of the same name |
|
6128 |
* |
|
5 | 6129 |
* Note: this RegEx does not balance inner tags and does not attempt |
6130 |
* to produce valid HTML |
|
0 | 6131 |
* |
6132 |
* @since 3.6.0 |
|
6133 |
* |
|
5 | 6134 |
* @param string $tag An HTML tag name. Example: 'video'. |
6135 |
* @return string Tag RegEx. |
|
0 | 6136 |
*/ |
6137 |
function get_tag_regex( $tag ) { |
|
9 | 6138 |
if ( empty( $tag ) ) { |
0 | 6139 |
return; |
9 | 6140 |
} |
0 | 6141 |
return sprintf( '<%1$s[^<]*(?:>[\s\S]*<\/%1$s>|\s*\/>)', tag_escape( $tag ) ); |
6142 |
} |
|
6143 |
||
6144 |
/** |
|
5 | 6145 |
* Retrieve a canonical form of the provided charset appropriate for passing to PHP |
0 | 6146 |
* functions such as htmlspecialchars() and charset html attributes. |
6147 |
* |
|
6148 |
* @since 3.6.0 |
|
5 | 6149 |
* @access private |
6150 |
* |
|
6151 |
* @see https://core.trac.wordpress.org/ticket/23688 |
|
6152 |
* |
|
6153 |
* @param string $charset A charset name. |
|
6154 |
* @return string The canonical form of the charset. |
|
0 | 6155 |
*/ |
6156 |
function _canonical_charset( $charset ) { |
|
9 | 6157 |
if ( 'utf-8' === strtolower( $charset ) || 'utf8' === strtolower( $charset ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6158 |
|
0 | 6159 |
return 'UTF-8'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6160 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6161 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6162 |
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
|
6163 |
|
0 | 6164 |
return 'ISO-8859-1'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6165 |
} |
0 | 6166 |
|
6167 |
return $charset; |
|
6168 |
} |
|
6169 |
||
6170 |
/** |
|
5 | 6171 |
* Set the mbstring internal encoding to a binary safe encoding when func_overload |
6172 |
* is enabled. |
|
6173 |
* |
|
6174 |
* When mbstring.func_overload is in use for multi-byte encodings, the results from |
|
6175 |
* strlen() and similar functions respect the utf8 characters, causing binary data |
|
6176 |
* to return incorrect lengths. |
|
6177 |
* |
|
6178 |
* This function overrides the mbstring encoding to a binary-safe encoding, and |
|
6179 |
* resets it to the users expected encoding afterwards through the |
|
6180 |
* `reset_mbstring_encoding` function. |
|
6181 |
* |
|
6182 |
* It is safe to recursively call this function, however each |
|
6183 |
* `mbstring_binary_safe_encoding()` call must be followed up with an equal number |
|
6184 |
* of `reset_mbstring_encoding()` calls. |
|
6185 |
* |
|
6186 |
* @since 3.7.0 |
|
0 | 6187 |
* |
6188 |
* @see reset_mbstring_encoding() |
|
6189 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6190 |
* @staticvar array $encodings |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6191 |
* @staticvar bool $overloaded |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6192 |
* |
5 | 6193 |
* @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding. |
6194 |
* Default false. |
|
0 | 6195 |
*/ |
6196 |
function mbstring_binary_safe_encoding( $reset = false ) { |
|
9 | 6197 |
static $encodings = array(); |
0 | 6198 |
static $overloaded = null; |
6199 |
||
9 | 6200 |
if ( is_null( $overloaded ) ) { |
0 | 6201 |
$overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 ); |
9 | 6202 |
} |
6203 |
||
6204 |
if ( false === $overloaded ) { |
|
0 | 6205 |
return; |
9 | 6206 |
} |
0 | 6207 |
|
6208 |
if ( ! $reset ) { |
|
6209 |
$encoding = mb_internal_encoding(); |
|
6210 |
array_push( $encodings, $encoding ); |
|
6211 |
mb_internal_encoding( 'ISO-8859-1' ); |
|
6212 |
} |
|
6213 |
||
6214 |
if ( $reset && $encodings ) { |
|
6215 |
$encoding = array_pop( $encodings ); |
|
6216 |
mb_internal_encoding( $encoding ); |
|
6217 |
} |
|
6218 |
} |
|
6219 |
||
6220 |
/** |
|
5 | 6221 |
* Reset the mbstring internal encoding to a users previously set encoding. |
0 | 6222 |
* |
6223 |
* @see mbstring_binary_safe_encoding() |
|
6224 |
* |
|
6225 |
* @since 3.7.0 |
|
6226 |
*/ |
|
6227 |
function reset_mbstring_encoding() { |
|
6228 |
mbstring_binary_safe_encoding( true ); |
|
6229 |
} |
|
5 | 6230 |
|
6231 |
/** |
|
6232 |
* Filter/validate a variable as a boolean. |
|
6233 |
* |
|
6234 |
* Alternative to `filter_var( $var, FILTER_VALIDATE_BOOLEAN )`. |
|
6235 |
* |
|
6236 |
* @since 4.0.0 |
|
6237 |
* |
|
6238 |
* @param mixed $var Boolean value to validate. |
|
6239 |
* @return bool Whether the value is validated. |
|
6240 |
*/ |
|
6241 |
function wp_validate_boolean( $var ) { |
|
6242 |
if ( is_bool( $var ) ) { |
|
6243 |
return $var; |
|
6244 |
} |
|
6245 |
||
6246 |
if ( is_string( $var ) && 'false' === strtolower( $var ) ) { |
|
6247 |
return false; |
|
6248 |
} |
|
6249 |
||
6250 |
return (bool) $var; |
|
6251 |
} |
|
6252 |
||
6253 |
/** |
|
6254 |
* Delete a file |
|
6255 |
* |
|
6256 |
* @since 4.2.0 |
|
6257 |
* |
|
6258 |
* @param string $file The path to the file to delete. |
|
6259 |
*/ |
|
6260 |
function wp_delete_file( $file ) { |
|
6261 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6262 |
* Filters the path of the file to delete. |
5 | 6263 |
* |
6264 |
* @since 2.1.0 |
|
6265 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6266 |
* @param string $file Path to the file to delete. |
5 | 6267 |
*/ |
6268 |
$delete = apply_filters( 'wp_delete_file', $file ); |
|
6269 |
if ( ! empty( $delete ) ) { |
|
6270 |
@unlink( $delete ); |
|
6271 |
} |
|
6272 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6273 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6274 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6275 |
* 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
|
6276 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6277 |
* @since 4.9.7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6278 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6279 |
* @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
|
6280 |
* @param string $directory Absolute path to a directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6281 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6282 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6283 |
function wp_delete_file_from_directory( $file, $directory ) { |
9 | 6284 |
if ( wp_is_stream( $file ) ) { |
6285 |
$real_file = $file; |
|
6286 |
$real_directory = $directory; |
|
6287 |
} else { |
|
6288 |
$real_file = realpath( wp_normalize_path( $file ) ); |
|
6289 |
$real_directory = realpath( wp_normalize_path( $directory ) ); |
|
6290 |
} |
|
6291 |
||
6292 |
if ( false !== $real_file ) { |
|
6293 |
$real_file = wp_normalize_path( $real_file ); |
|
6294 |
} |
|
6295 |
||
6296 |
if ( false !== $real_directory ) { |
|
6297 |
$real_directory = wp_normalize_path( $real_directory ); |
|
6298 |
} |
|
6299 |
||
6300 |
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
|
6301 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6302 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6303 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6304 |
wp_delete_file( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6305 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6306 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6307 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6308 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6309 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6310 |
* 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
|
6311 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6312 |
* 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
|
6313 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6314 |
* @since 4.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6315 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6316 |
* @global WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6317 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6318 |
function wp_post_preview_js() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6319 |
global $post; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6321 |
if ( ! is_preview() || empty( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6322 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6323 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6324 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6325 |
// Has to match the window name used in post_submit_meta_box() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6326 |
$name = 'wp-preview-' . (int) $post->ID; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6327 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6328 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6329 |
<script> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6330 |
( function() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6331 |
var query = document.location.search; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6332 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6333 |
if ( query && query.indexOf( 'preview=true' ) !== -1 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6334 |
window.name = '<?php echo $name; ?>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6335 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6336 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6337 |
if ( window.addEventListener ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6338 |
window.addEventListener( 'unload', function() { window.name = ''; }, false ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6339 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6340 |
}()); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6341 |
</script> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6342 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6343 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6344 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6345 |
/** |
9 | 6346 |
* 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
|
6347 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6348 |
* 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
|
6349 |
* 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
|
6350 |
* |
9 | 6351 |
* Despite historical function name, the output does not conform to RFC3339 format, |
6352 |
* which must contain timezone. |
|
6353 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6354 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6355 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6356 |
* @param string $date_string Date string to parse and format. |
9 | 6357 |
* @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
|
6358 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6359 |
function mysql_to_rfc3339( $date_string ) { |
9 | 6360 |
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
|
6361 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6362 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6363 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6364 |
* 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
|
6365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6366 |
* 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
|
6367 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6368 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6369 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6370 |
* @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
|
6371 |
* '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
|
6372 |
* 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
|
6373 |
* invoked. Default 'admin'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6374 |
* @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
|
6375 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6376 |
function wp_raise_memory_limit( $context = 'admin' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6377 |
// Exit early if the limit cannot be changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6378 |
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6379 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6380 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6381 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6382 |
$current_limit = @ini_get( 'memory_limit' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6383 |
$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
|
6384 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6385 |
if ( -1 === $current_limit_int ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6386 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6387 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6388 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6389 |
$wp_max_limit = WP_MAX_MEMORY_LIMIT; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6390 |
$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
|
6391 |
$filtered_limit = $wp_max_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6392 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6393 |
switch ( $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6394 |
case 'admin': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6395 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6396 |
* Filters the maximum memory limit available for administration screens. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6397 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6398 |
* 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
|
6399 |
* 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
|
6400 |
* users of any role) are handled separately. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6401 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6402 |
* 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
|
6403 |
* 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
|
6404 |
* (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
|
6405 |
* this is higher. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6406 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6407 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6408 |
* @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
|
6409 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6410 |
* @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
|
6411 |
* (bytes), or a shorthand string notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6412 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6413 |
$filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6414 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6416 |
case 'image': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6417 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6418 |
* Filters the memory limit allocated for image manipulation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6419 |
* |
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 |
* @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
|
6422 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6423 |
* @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
|
6424 |
* Default `WP_MAX_MEMORY_LIMIT` or the original |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6425 |
* php.ini `memory_limit`, whichever is higher. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6426 |
* Accepts an integer (bytes), or a shorthand string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6427 |
* notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6428 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6429 |
$filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6430 |
break; |
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 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6433 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6434 |
* Filters the memory limit allocated for arbitrary contexts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6435 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6436 |
* 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
|
6437 |
* 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
|
6438 |
* their own contexts for raising the memory limit. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6439 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6440 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6441 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6442 |
* @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
|
6443 |
* Default '256M' or the original php.ini `memory_limit`, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6444 |
* whichever is higher. Accepts an integer (bytes), or a |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6445 |
* shorthand string notation, such as '256M'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6446 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6447 |
$filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6448 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6449 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6450 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6451 |
$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
|
6452 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6453 |
if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6454 |
if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6455 |
return $filtered_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6456 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6457 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6458 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6459 |
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6460 |
if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6461 |
return $wp_max_limit; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6462 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6463 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6464 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6465 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6466 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6467 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6468 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6469 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6470 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6471 |
* Generate a random UUID (version 4). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6472 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6473 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6474 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6475 |
* @return string UUID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6476 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6477 |
function wp_generate_uuid4() { |
9 | 6478 |
return sprintf( |
6479 |
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
6480 |
mt_rand( 0, 0xffff ), |
|
6481 |
mt_rand( 0, 0xffff ), |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6482 |
mt_rand( 0, 0xffff ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6483 |
mt_rand( 0, 0x0fff ) | 0x4000, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6484 |
mt_rand( 0, 0x3fff ) | 0x8000, |
9 | 6485 |
mt_rand( 0, 0xffff ), |
6486 |
mt_rand( 0, 0xffff ), |
|
6487 |
mt_rand( 0, 0xffff ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6488 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6489 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6490 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6491 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6492 |
* Validates that a UUID is valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6493 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6494 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6495 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6496 |
* @param mixed $uuid UUID to check. |
9 | 6497 |
* @param int $version Specify which version of UUID to check against. Default is none, |
6498 |
* 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
|
6499 |
* @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
|
6500 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6501 |
function wp_is_uuid( $uuid, $version = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6502 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6503 |
if ( ! is_string( $uuid ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6504 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6505 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6506 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6507 |
if ( is_numeric( $version ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6508 |
if ( 4 !== (int) $version ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6509 |
_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
|
6510 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6511 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6512 |
$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
|
6513 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6514 |
$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
|
6515 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6516 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6517 |
return (bool) preg_match( $regex, $uuid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6518 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6519 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6520 |
/** |
9 | 6521 |
* Get unique ID. |
6522 |
* |
|
6523 |
* This is a PHP implementation of Underscore's uniqueId method. A static variable |
|
6524 |
* contains an integer that is incremented with each call. This number is returned |
|
6525 |
* with the optional prefix. As such the returned value is not universally unique, |
|
6526 |
* but it is unique across the life of the PHP process. |
|
6527 |
* |
|
6528 |
* @since 5.0.3 |
|
6529 |
* |
|
6530 |
* @staticvar int $id_counter |
|
6531 |
* |
|
6532 |
* @param string $prefix Prefix for the returned ID. |
|
6533 |
* @return string Unique ID. |
|
6534 |
*/ |
|
6535 |
function wp_unique_id( $prefix = '' ) { |
|
6536 |
static $id_counter = 0; |
|
6537 |
return $prefix . (string) ++$id_counter; |
|
6538 |
} |
|
6539 |
||
6540 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6541 |
* Get last changed date for the specified cache group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6542 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6543 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6544 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6545 |
* @param string $group Where the cache contents are grouped. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6546 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6547 |
* @return string $last_changed UNIX timestamp with microseconds representing when the group was last changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6548 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6549 |
function wp_cache_get_last_changed( $group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6550 |
$last_changed = wp_cache_get( 'last_changed', $group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6551 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6552 |
if ( ! $last_changed ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6553 |
$last_changed = microtime(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6554 |
wp_cache_set( 'last_changed', $last_changed, $group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6555 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6556 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6557 |
return $last_changed; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6558 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6559 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6560 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6561 |
* 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
|
6562 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6563 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6564 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6565 |
* @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
|
6566 |
* @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
|
6567 |
* @param string $option_name The relevant database option name. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6568 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6569 |
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
|
6570 |
$send = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6571 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6572 |
// 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
|
6573 |
if ( 'you@example.com' === $old_email ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6574 |
$send = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6575 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6576 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6577 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6578 |
* 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
|
6579 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6580 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6581 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6582 |
* @param bool $send Whether to send the email notification. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6583 |
* @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
|
6584 |
* @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
|
6585 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6586 |
$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
|
6587 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6588 |
if ( ! $send ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6589 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6590 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6591 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6592 |
/* translators: Do not translate OLD_EMAIL, NEW_EMAIL, SITENAME, SITEURL: those are placeholders. */ |
9 | 6593 |
$email_change_text = __( |
6594 |
'Hi, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6595 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6596 |
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
|
6597 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6598 |
The new admin email address is ###NEW_EMAIL###. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6599 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6600 |
This email has been sent to ###OLD_EMAIL### |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6601 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6602 |
Regards, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6603 |
All at ###SITENAME### |
9 | 6604 |
###SITEURL###' |
6605 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6606 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6607 |
$email_change_email = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6608 |
'to' => $old_email, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6609 |
/* translators: Site admin email change notification email subject. %s: Site title */ |
9 | 6610 |
'subject' => __( '[%s] Admin Email Changed' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6611 |
'message' => $email_change_text, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6612 |
'headers' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6613 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6614 |
// get site name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6615 |
$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
|
6616 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6617 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6618 |
* 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
|
6619 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6620 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6621 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6622 |
* @param array $email_change_email { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6623 |
* Used to build wp_mail(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6624 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6625 |
* @type string $to The intended recipient. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6626 |
* @type string $subject The subject of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6627 |
* @type string $message The content of the email. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6628 |
* 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
|
6629 |
* - ###OLD_EMAIL### The old site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6630 |
* - ###NEW_EMAIL### The new site admin email address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6631 |
* - ###SITENAME### The name of the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6632 |
* - ###SITEURL### The URL to the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6633 |
* @type string $headers Headers. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6634 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6635 |
* @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
|
6636 |
* @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
|
6637 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6638 |
$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
|
6639 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6640 |
$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
|
6641 |
$email_change_email['message'] = str_replace( '###NEW_EMAIL###', $new_email, $email_change_email['message'] ); |
9 | 6642 |
$email_change_email['message'] = str_replace( '###SITENAME###', $site_name, $email_change_email['message'] ); |
6643 |
$email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] ); |
|
6644 |
||
6645 |
wp_mail( |
|
6646 |
$email_change_email['to'], |
|
6647 |
sprintf( |
|
6648 |
$email_change_email['subject'], |
|
6649 |
$site_name |
|
6650 |
), |
|
6651 |
$email_change_email['message'], |
|
6652 |
$email_change_email['headers'] |
|
6653 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6654 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6655 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6656 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6657 |
* Return an anonymized IPv4 or IPv6 address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6658 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6659 |
* @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
|
6660 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6661 |
* @param string $ip_addr The IPv4 or IPv6 address to be anonymized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6662 |
* @param bool $ipv6_fallback Optional. Whether to return the original IPv6 address if the needed functions |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6663 |
* to anonymize it are not present. Default false, return `::` (unspecified address). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6664 |
* @return string The anonymized IP address. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6665 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6666 |
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
|
6667 |
// Detect what kind of IP address this is. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6668 |
$ip_prefix = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6669 |
$is_ipv6 = substr_count( $ip_addr, ':' ) > 1; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6670 |
$is_ipv4 = ( 3 === substr_count( $ip_addr, '.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6671 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6672 |
if ( $is_ipv6 && $is_ipv4 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6673 |
// 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
|
6674 |
$ip_prefix = '::ffff:'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6675 |
$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
|
6676 |
$ip_addr = str_replace( ']', '', $ip_addr ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6677 |
$is_ipv6 = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6678 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6679 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6680 |
if ( $is_ipv6 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6681 |
// 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
|
6682 |
$left_bracket = strpos( $ip_addr, '[' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6683 |
$right_bracket = strpos( $ip_addr, ']' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6684 |
$percent = strpos( $ip_addr, '%' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6685 |
$netmask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6686 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6687 |
// 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
|
6688 |
if ( false !== $left_bracket && false !== $right_bracket ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6689 |
$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
|
6690 |
} elseif ( false !== $left_bracket || false !== $right_bracket ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6691 |
// 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
|
6692 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6693 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6694 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6695 |
// Strip the reachability scope. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6696 |
if ( false !== $percent ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6697 |
$ip_addr = substr( $ip_addr, 0, $percent ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6698 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6699 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6700 |
// No invalid characters should be left. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6701 |
if ( preg_match( '/[^0-9a-f:]/i', $ip_addr ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6702 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6703 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6704 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6705 |
// 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
|
6706 |
if ( function_exists( 'inet_pton' ) && function_exists( 'inet_ntop' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6707 |
$ip_addr = inet_ntop( inet_pton( $ip_addr ) & inet_pton( $netmask ) ); |
9 | 6708 |
if ( false === $ip_addr ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6709 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6710 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6711 |
} elseif ( ! $ipv6_fallback ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6712 |
return '::'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6713 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6714 |
} elseif ( $is_ipv4 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6715 |
// Strip any port and partially anonymize the IP. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6716 |
$last_octet_position = strrpos( $ip_addr, '.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6717 |
$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
|
6718 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6719 |
return '0.0.0.0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6720 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6721 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6722 |
// Restore the IPv6 prefix to compatibility mode addresses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6723 |
return $ip_prefix . $ip_addr; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6724 |
} |
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 |
* Return uniform "anonymous" data by type. |
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.6 |
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 $type The type of data to be anonymized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6732 |
* @param string $data Optional The data to be anonymized. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6733 |
* @return string The anonymous data for the requested type. |
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_privacy_anonymize_data( $type, $data = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6736 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6737 |
switch ( $type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6738 |
case 'email': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6739 |
$anonymous = 'deleted@site.invalid'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6740 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6741 |
case 'url': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6742 |
$anonymous = 'https://site.invalid'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6743 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6744 |
case 'ip': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6745 |
$anonymous = wp_privacy_anonymize_ip( $data ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6746 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6747 |
case 'date': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6748 |
$anonymous = '0000-00-00 00:00:00'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6749 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6750 |
case 'text': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6751 |
/* translators: deleted text */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6752 |
$anonymous = __( '[deleted]' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6753 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6754 |
case 'longtext': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6755 |
/* translators: deleted long text */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6756 |
$anonymous = __( 'This content was deleted by the author.' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6757 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6758 |
default: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6759 |
$anonymous = ''; |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6763 |
* Filters the anonymous data for each type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6764 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6765 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6766 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6767 |
* @param string $anonymous Anonymized data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6768 |
* @param string $type Type of the data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6769 |
* @param string $data Original data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6770 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6771 |
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
|
6772 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6773 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6774 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6775 |
* 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
|
6776 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6777 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6778 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6779 |
* @see wp_privacy_exports_url |
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 |
* @return string Exports directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6782 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6783 |
function wp_privacy_exports_dir() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6784 |
$upload_dir = wp_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6785 |
$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
|
6786 |
|
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 |
* 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
|
6789 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6790 |
* @since 4.9.6 |
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 |
* @param string $exports_dir Exports directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6793 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6794 |
return apply_filters( 'wp_privacy_exports_dir', $exports_dir ); |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6798 |
* 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
|
6799 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6800 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6801 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6802 |
* @see wp_privacy_exports_dir |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6803 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6804 |
* @return string Exports directory URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6805 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6806 |
function wp_privacy_exports_url() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6807 |
$upload_dir = wp_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6808 |
$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
|
6809 |
|
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 |
* 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
|
6812 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6813 |
* @since 4.9.6 |
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 |
* @param string $exports_url Exports directory URL. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6816 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6817 |
return apply_filters( 'wp_privacy_exports_url', $exports_url ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6818 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6821 |
* 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
|
6822 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6823 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6824 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6825 |
function wp_schedule_delete_old_privacy_export_files() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6826 |
if ( wp_installing() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6827 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6828 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6829 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6830 |
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
|
6831 |
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
|
6832 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6833 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6834 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6835 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6836 |
* Cleans up export files older than three days old. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6837 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6838 |
* 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
|
6839 |
* 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
|
6840 |
* 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
|
6841 |
* 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
|
6842 |
* layer of protection. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6843 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6844 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6845 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6846 |
function wp_privacy_delete_old_export_files() { |
9 | 6847 |
$exports_dir = wp_privacy_exports_dir(); |
6848 |
if ( ! is_dir( $exports_dir ) ) { |
|
6849 |
return; |
|
6850 |
} |
|
6851 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6852 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6853 |
$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
|
6854 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6855 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6856 |
* 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
|
6857 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6858 |
* 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
|
6859 |
* be deleted by a cron job. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6860 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6861 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6862 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6863 |
* @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
|
6864 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6865 |
$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
|
6866 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6867 |
foreach ( (array) $export_files as $export_file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6868 |
$file_age_in_seconds = time() - filemtime( $export_file ); |
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 |
if ( $expiration < $file_age_in_seconds ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6871 |
unlink( $export_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6872 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6873 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6874 |
} |
9 | 6875 |
|
6876 |
/** |
|
6877 |
* Gets the URL to learn more about updating the PHP version the site is running on. |
|
6878 |
* |
|
6879 |
* This URL can be overridden by specifying an environment variable `WP_UPDATE_PHP_URL` or by using the |
|
6880 |
* {@see 'wp_update_php_url'} filter. Providing an empty string is not allowed and will result in the |
|
6881 |
* default URL being used. Furthermore the page the URL links to should preferably be localized in the |
|
6882 |
* site language. |
|
6883 |
* |
|
6884 |
* @since 5.1.0 |
|
6885 |
* |
|
6886 |
* @return string URL to learn more about updating PHP. |
|
6887 |
*/ |
|
6888 |
function wp_get_update_php_url() { |
|
6889 |
$default_url = wp_get_default_update_php_url(); |
|
6890 |
||
6891 |
$update_url = $default_url; |
|
6892 |
if ( false !== getenv( 'WP_UPDATE_PHP_URL' ) ) { |
|
6893 |
$update_url = getenv( 'WP_UPDATE_PHP_URL' ); |
|
6894 |
} |
|
6895 |
||
6896 |
/** |
|
6897 |
* Filters the URL to learn more about updating the PHP version the site is running on. |
|
6898 |
* |
|
6899 |
* Providing an empty string is not allowed and will result in the default URL being used. Furthermore |
|
6900 |
* the page the URL links to should preferably be localized in the site language. |
|
6901 |
* |
|
6902 |
* @since 5.1.0 |
|
6903 |
* |
|
6904 |
* @param string $update_url URL to learn more about updating PHP. |
|
6905 |
*/ |
|
6906 |
$update_url = apply_filters( 'wp_update_php_url', $update_url ); |
|
6907 |
||
6908 |
if ( empty( $update_url ) ) { |
|
6909 |
$update_url = $default_url; |
|
6910 |
} |
|
6911 |
||
6912 |
return $update_url; |
|
6913 |
} |
|
6914 |
||
6915 |
/** |
|
6916 |
* Gets the default URL to learn more about updating the PHP version the site is running on. |
|
6917 |
* |
|
6918 |
* Do not use this function to retrieve this URL. Instead, use {@see wp_get_update_php_url()} when relying on the URL. |
|
6919 |
* This function does not allow modifying the returned URL, and is only used to compare the actually used URL with the |
|
6920 |
* default one. |
|
6921 |
* |
|
6922 |
* @since 5.1.0 |
|
6923 |
* @access private |
|
6924 |
* |
|
6925 |
* @return string Default URL to learn more about updating PHP. |
|
6926 |
*/ |
|
6927 |
function wp_get_default_update_php_url() { |
|
6928 |
return _x( 'https://wordpress.org/support/update-php/', 'localized PHP upgrade information page' ); |
|
6929 |
} |
|
6930 |
||
6931 |
/** |
|
6932 |
* Prints the default annotation for the web host altering the "Update PHP" page URL. |
|
6933 |
* |
|
6934 |
* This function is to be used after {@see wp_get_update_php_url()} to display a consistent |
|
6935 |
* annotation if the web host has altered the default "Update PHP" page URL. |
|
6936 |
* |
|
6937 |
* @since 5.1.0 |
|
6938 |
* @since 5.2.0 Added the `$before` and `$after` parameters. |
|
6939 |
* |
|
6940 |
* @param string $before Markup to output before the annotation. Default `<p class="description">`. |
|
6941 |
* @param string $after Markup to output after the annotation. Default `</p>`. |
|
6942 |
*/ |
|
6943 |
function wp_update_php_annotation( $before = '<p class="description">', $after = '</p>' ) { |
|
6944 |
$annotation = wp_get_update_php_annotation(); |
|
6945 |
||
6946 |
if ( $annotation ) { |
|
6947 |
echo $before . $annotation . $after; |
|
6948 |
} |
|
6949 |
} |
|
6950 |
||
6951 |
/** |
|
6952 |
* Returns the default annotation for the web hosting altering the "Update PHP" page URL. |
|
6953 |
* |
|
6954 |
* This function is to be used after {@see wp_get_update_php_url()} to return a consistent |
|
6955 |
* annotation if the web host has altered the default "Update PHP" page URL. |
|
6956 |
* |
|
6957 |
* @since 5.2.0 |
|
6958 |
* |
|
6959 |
* @return string $message Update PHP page annotation. An empty string if no custom URLs are provided. |
|
6960 |
*/ |
|
6961 |
function wp_get_update_php_annotation() { |
|
6962 |
$update_url = wp_get_update_php_url(); |
|
6963 |
$default_url = wp_get_default_update_php_url(); |
|
6964 |
||
6965 |
if ( $update_url === $default_url ) { |
|
6966 |
return ''; |
|
6967 |
} |
|
6968 |
||
6969 |
$annotation = sprintf( |
|
6970 |
/* translators: %s: default Update PHP page URL */ |
|
6971 |
__( '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>.' ), |
|
6972 |
esc_url( $default_url ) |
|
6973 |
); |
|
6974 |
||
6975 |
return $annotation; |
|
6976 |
} |
|
6977 |
||
6978 |
/** |
|
6979 |
* Gets the URL for directly updating the PHP version the site is running on. |
|
6980 |
* |
|
6981 |
* A URL will only be returned if the `WP_DIRECT_UPDATE_PHP_URL` environment variable is specified or |
|
6982 |
* by using the {@see 'wp_direct_php_update_url'} filter. This allows hosts to send users directly to |
|
6983 |
* the page where they can update PHP to a newer version. |
|
6984 |
* |
|
6985 |
* @since 5.1.1 |
|
6986 |
* |
|
6987 |
* @return string URL for directly updating PHP or empty string. |
|
6988 |
*/ |
|
6989 |
function wp_get_direct_php_update_url() { |
|
6990 |
$direct_update_url = ''; |
|
6991 |
||
6992 |
if ( false !== getenv( 'WP_DIRECT_UPDATE_PHP_URL' ) ) { |
|
6993 |
$direct_update_url = getenv( 'WP_DIRECT_UPDATE_PHP_URL' ); |
|
6994 |
} |
|
6995 |
||
6996 |
/** |
|
6997 |
* Filters the URL for directly updating the PHP version the site is running on from the host. |
|
6998 |
* |
|
6999 |
* @since 5.1.1 |
|
7000 |
* |
|
7001 |
* @param string $direct_update_url URL for directly updating PHP. |
|
7002 |
*/ |
|
7003 |
$direct_update_url = apply_filters( 'wp_direct_php_update_url', $direct_update_url ); |
|
7004 |
||
7005 |
return $direct_update_url; |
|
7006 |
} |
|
7007 |
||
7008 |
/** |
|
7009 |
* Display a button directly linking to a PHP update process. |
|
7010 |
* |
|
7011 |
* This provides hosts with a way for users to be sent directly to their PHP update process. |
|
7012 |
* |
|
7013 |
* The button is only displayed if a URL is returned by `wp_get_direct_php_update_url()`. |
|
7014 |
* |
|
7015 |
* @since 5.1.1 |
|
7016 |
*/ |
|
7017 |
function wp_direct_php_update_button() { |
|
7018 |
$direct_update_url = wp_get_direct_php_update_url(); |
|
7019 |
||
7020 |
if ( empty( $direct_update_url ) ) { |
|
7021 |
return; |
|
7022 |
} |
|
7023 |
||
7024 |
echo '<p class="button-container">'; |
|
7025 |
printf( |
|
7026 |
'<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>', |
|
7027 |
esc_url( $direct_update_url ), |
|
7028 |
__( 'Update PHP' ), |
|
7029 |
/* translators: accessibility text */ |
|
7030 |
__( '(opens in a new tab)' ) |
|
7031 |
); |
|
7032 |
echo '</p>'; |
|
7033 |
} |
|
7034 |
||
7035 |
/** |
|
7036 |
* Get the size of a directory. |
|
7037 |
* |
|
7038 |
* A helper function that is used primarily to check whether |
|
7039 |
* a blog has exceeded its allowed upload space. |
|
7040 |
* |
|
7041 |
* @since MU (3.0.0) |
|
7042 |
* @since 5.2.0 $max_execution_time parameter added. |
|
7043 |
* |
|
7044 |
* @param string $directory Full path of a directory. |
|
7045 |
* @param int $max_execution_time Maximum time to run before giving up. In seconds. |
|
7046 |
* The timeout is global and is measured from the moment WordPress started to load. |
|
7047 |
* @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. |
|
7048 |
*/ |
|
7049 |
function get_dirsize( $directory, $max_execution_time = null ) { |
|
7050 |
$dirsize = get_transient( 'dirsize_cache' ); |
|
7051 |
||
7052 |
if ( is_array( $dirsize ) && isset( $dirsize[ $directory ]['size'] ) ) { |
|
7053 |
return $dirsize[ $directory ]['size']; |
|
7054 |
} |
|
7055 |
||
7056 |
if ( ! is_array( $dirsize ) ) { |
|
7057 |
$dirsize = array(); |
|
7058 |
} |
|
7059 |
||
7060 |
// Exclude individual site directories from the total when checking the main site of a network |
|
7061 |
// as they are subdirectories and should not be counted. |
|
7062 |
if ( is_multisite() && is_main_site() ) { |
|
7063 |
$dirsize[ $directory ]['size'] = recurse_dirsize( $directory, $directory . '/sites', $max_execution_time ); |
|
7064 |
} else { |
|
7065 |
$dirsize[ $directory ]['size'] = recurse_dirsize( $directory, null, $max_execution_time ); |
|
7066 |
} |
|
7067 |
||
7068 |
set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS ); |
|
7069 |
return $dirsize[ $directory ]['size']; |
|
7070 |
} |
|
7071 |
||
7072 |
/** |
|
7073 |
* Get the size of a directory recursively. |
|
7074 |
* |
|
7075 |
* Used by get_dirsize() to get a directory's size when it contains |
|
7076 |
* other directories. |
|
7077 |
* |
|
7078 |
* @since MU (3.0.0) |
|
7079 |
* @since 4.3.0 $exclude parameter added. |
|
7080 |
* @since 5.2.0 $max_execution_time parameter added. |
|
7081 |
* |
|
7082 |
* @param string $directory Full path of a directory. |
|
7083 |
* @param string|array $exclude Optional. Full path of a subdirectory to exclude from the total, or array of paths. |
|
7084 |
* Expected without trailing slash(es). |
|
7085 |
* @param int $max_execution_time Maximum time to run before giving up. In seconds. |
|
7086 |
* The timeout is global and is measured from the moment WordPress started to load. |
|
7087 |
* @return int|false|null Size in bytes if a valid directory. False if not. Null if timeout. |
|
7088 |
*/ |
|
7089 |
function recurse_dirsize( $directory, $exclude = null, $max_execution_time = null ) { |
|
7090 |
$size = 0; |
|
7091 |
||
7092 |
$directory = untrailingslashit( $directory ); |
|
7093 |
||
7094 |
if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) ) { |
|
7095 |
return false; |
|
7096 |
} |
|
7097 |
||
7098 |
if ( |
|
7099 |
( is_string( $exclude ) && $directory === $exclude ) || |
|
7100 |
( is_array( $exclude ) && in_array( $directory, $exclude, true ) ) |
|
7101 |
) { |
|
7102 |
return false; |
|
7103 |
} |
|
7104 |
||
7105 |
if ( $max_execution_time === null ) { |
|
7106 |
// Keep the previous behavior but attempt to prevent fatal errors from timeout if possible. |
|
7107 |
if ( function_exists( 'ini_get' ) ) { |
|
7108 |
$max_execution_time = ini_get( 'max_execution_time' ); |
|
7109 |
} else { |
|
7110 |
// Disable... |
|
7111 |
$max_execution_time = 0; |
|
7112 |
} |
|
7113 |
||
7114 |
// Leave 1 second "buffer" for other operations if $max_execution_time has reasonable value. |
|
7115 |
if ( $max_execution_time > 10 ) { |
|
7116 |
$max_execution_time -= 1; |
|
7117 |
} |
|
7118 |
} |
|
7119 |
||
7120 |
if ( $handle = opendir( $directory ) ) { |
|
7121 |
while ( ( $file = readdir( $handle ) ) !== false ) { |
|
7122 |
$path = $directory . '/' . $file; |
|
7123 |
if ( $file != '.' && $file != '..' ) { |
|
7124 |
if ( is_file( $path ) ) { |
|
7125 |
$size += filesize( $path ); |
|
7126 |
} elseif ( is_dir( $path ) ) { |
|
7127 |
$handlesize = recurse_dirsize( $path, $exclude, $max_execution_time ); |
|
7128 |
if ( $handlesize > 0 ) { |
|
7129 |
$size += $handlesize; |
|
7130 |
} |
|
7131 |
} |
|
7132 |
||
7133 |
if ( $max_execution_time > 0 && microtime( true ) - WP_START_TIMESTAMP > $max_execution_time ) { |
|
7134 |
// Time exceeded. Give up instead of risking a fatal timeout. |
|
7135 |
$size = null; |
|
7136 |
break; |
|
7137 |
} |
|
7138 |
} |
|
7139 |
} |
|
7140 |
closedir( $handle ); |
|
7141 |
} |
|
7142 |
return $size; |
|
7143 |
} |
|
7144 |
||
7145 |
/** |
|
7146 |
* Checks compatibility with the current WordPress version. |
|
7147 |
* |
|
7148 |
* @since 5.2.0 |
|
7149 |
* |
|
7150 |
* @param string $required Minimum required WordPress version. |
|
7151 |
* @return bool True if required version is compatible or empty, false if not. |
|
7152 |
*/ |
|
7153 |
function is_wp_version_compatible( $required ) { |
|
7154 |
return empty( $required ) || version_compare( get_bloginfo( 'version' ), $required, '>=' ); |
|
7155 |
} |
|
7156 |
||
7157 |
/** |
|
7158 |
* Checks compatibility with the current PHP version. |
|
7159 |
* |
|
7160 |
* @since 5.2.0 |
|
7161 |
* |
|
7162 |
* @param string $required Minimum required PHP version. |
|
7163 |
* @return bool True if required version is compatible or empty, false if not. |
|
7164 |
*/ |
|
7165 |
function is_php_version_compatible( $required ) { |
|
7166 |
return empty( $required ) || version_compare( phpversion(), $required, '>=' ); |
|
7167 |
} |