author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Link/Bookmark API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Bookmark |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Retrieve Bookmark data |
|
11 |
* |
|
12 |
* @since 2.1.0 |
|
13 |
* |
|
5 | 14 |
* @global wpdb $wpdb WordPress database abstraction object. |
15 |
* |
|
16 |
* @param int|stdClass $bookmark |
|
16 | 17 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
18 |
* correspond to an stdClass object, an associative array, or a numeric array, |
|
19 |
* respectively. Default OBJECT. |
|
20 |
* @param string $filter Optional. How to sanitize bookmark fields. Default 'raw'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* @return array|object|null Type returned depends on $output value. |
0 | 22 |
*/ |
9 | 23 |
function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) { |
0 | 24 |
global $wpdb; |
25 |
||
9 | 26 |
if ( empty( $bookmark ) ) { |
27 |
if ( isset( $GLOBALS['link'] ) ) { |
|
0 | 28 |
$_bookmark = & $GLOBALS['link']; |
9 | 29 |
} else { |
0 | 30 |
$_bookmark = null; |
9 | 31 |
} |
32 |
} elseif ( is_object( $bookmark ) ) { |
|
33 |
wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' ); |
|
0 | 34 |
$_bookmark = $bookmark; |
35 |
} else { |
|
9 | 36 |
if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) { |
0 | 37 |
$_bookmark = & $GLOBALS['link']; |
16 | 38 |
} else { |
39 |
$_bookmark = wp_cache_get( $bookmark, 'bookmark' ); |
|
40 |
if ( ! $_bookmark ) { |
|
41 |
$_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) ); |
|
42 |
if ( $_bookmark ) { |
|
43 |
$_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); |
|
44 |
wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' ); |
|
45 |
} |
|
0 | 46 |
} |
47 |
} |
|
48 |
} |
|
49 |
||
9 | 50 |
if ( ! $_bookmark ) { |
0 | 51 |
return $_bookmark; |
9 | 52 |
} |
0 | 53 |
|
9 | 54 |
$_bookmark = sanitize_bookmark( $_bookmark, $filter ); |
0 | 55 |
|
18 | 56 |
if ( OBJECT === $output ) { |
0 | 57 |
return $_bookmark; |
18 | 58 |
} elseif ( ARRAY_A === $output ) { |
9 | 59 |
return get_object_vars( $_bookmark ); |
18 | 60 |
} elseif ( ARRAY_N === $output ) { |
9 | 61 |
return array_values( get_object_vars( $_bookmark ) ); |
0 | 62 |
} else { |
63 |
return $_bookmark; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
/** |
|
68 |
* Retrieve single bookmark data item or field. |
|
69 |
* |
|
70 |
* @since 2.3.0 |
|
71 |
* |
|
16 | 72 |
* @param string $field The name of the data field to return. |
73 |
* @param int $bookmark The bookmark ID to get field. |
|
74 |
* @param string $context Optional. The context of how the field will be used. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* @return string|WP_Error |
0 | 76 |
*/ |
77 |
function get_bookmark_field( $field, $bookmark, $context = 'display' ) { |
|
78 |
$bookmark = (int) $bookmark; |
|
79 |
$bookmark = get_bookmark( $bookmark ); |
|
80 |
||
9 | 81 |
if ( is_wp_error( $bookmark ) ) { |
0 | 82 |
return $bookmark; |
9 | 83 |
} |
0 | 84 |
|
9 | 85 |
if ( ! is_object( $bookmark ) ) { |
0 | 86 |
return ''; |
9 | 87 |
} |
0 | 88 |
|
9 | 89 |
if ( ! isset( $bookmark->$field ) ) { |
0 | 90 |
return ''; |
9 | 91 |
} |
0 | 92 |
|
9 | 93 |
return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context ); |
0 | 94 |
} |
95 |
||
96 |
/** |
|
97 |
* Retrieves the list of bookmarks |
|
98 |
* |
|
99 |
* Attempts to retrieve from the cache first based on MD5 hash of arguments. If |
|
100 |
* that fails, then the query will be built from the arguments and executed. The |
|
101 |
* results will be stored to the cache. |
|
102 |
* |
|
5 | 103 |
* @since 2.1.0 |
104 |
* |
|
105 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
106 |
* |
|
107 |
* @param string|array $args { |
|
108 |
* Optional. String or array of arguments to retrieve bookmarks. |
|
0 | 109 |
* |
16 | 110 |
* @type string $orderby How to order the links by. Accepts 'id', 'link_id', 'name', 'link_name', |
111 |
* 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating', |
|
112 |
* 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes', |
|
113 |
* 'description', 'link_description', 'length' and 'rand'. |
|
114 |
* When `$orderby` is 'length', orders by the character length of |
|
115 |
* 'link_name'. Default 'name'. |
|
5 | 116 |
* @type string $order Whether to order bookmarks in ascending or descending order. |
117 |
* Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'. |
|
16 | 118 |
* @type int $limit Amount of bookmarks to display. Accepts any positive number or |
119 |
* -1 for all. Default -1. |
|
120 |
* @type string $category Comma-separated list of category IDs to include links from. |
|
5 | 121 |
* Default empty. |
122 |
* @type string $category_name Category to retrieve links for by name. Default empty. |
|
123 |
* @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts |
|
124 |
* 1|true or 0|false. Default 1|true. |
|
125 |
* @type int|bool $show_updated Whether to display the time the bookmark was last updated. |
|
126 |
* Accepts 1|true or 0|false. Default 0|false. |
|
127 |
* @type string $include Comma-separated list of bookmark IDs to include. Default empty. |
|
128 |
* @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty. |
|
16 | 129 |
* @type string $search Search terms. Will be SQL-formatted with wildcards before and after |
130 |
* and searched in 'link_url', 'link_name' and 'link_description'. |
|
131 |
* Default empty. |
|
5 | 132 |
* } |
16 | 133 |
* @return object[] List of bookmark row objects. |
0 | 134 |
*/ |
5 | 135 |
function get_bookmarks( $args = '' ) { |
0 | 136 |
global $wpdb; |
137 |
||
138 |
$defaults = array( |
|
9 | 139 |
'orderby' => 'name', |
140 |
'order' => 'ASC', |
|
141 |
'limit' => -1, |
|
142 |
'category' => '', |
|
143 |
'category_name' => '', |
|
144 |
'hide_invisible' => 1, |
|
145 |
'show_updated' => 0, |
|
146 |
'include' => '', |
|
147 |
'exclude' => '', |
|
148 |
'search' => '', |
|
0 | 149 |
); |
150 |
||
16 | 151 |
$parsed_args = wp_parse_args( $args, $defaults ); |
0 | 152 |
|
16 | 153 |
$key = md5( serialize( $parsed_args ) ); |
154 |
$cache = wp_cache_get( 'get_bookmarks', 'bookmark' ); |
|
155 |
||
156 |
if ( 'rand' !== $parsed_args['orderby'] && $cache ) { |
|
5 | 157 |
if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { |
158 |
$bookmarks = $cache[ $key ]; |
|
159 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* Filters the returned list of bookmarks. |
5 | 161 |
* |
162 |
* The first time the hook is evaluated in this file, it returns the cached |
|
163 |
* bookmarks list. The second evaluation returns a cached bookmarks list if the |
|
164 |
* link category is passed but does not exist. The third evaluation returns |
|
165 |
* the full cached results. |
|
166 |
* |
|
167 |
* @since 2.1.0 |
|
168 |
* |
|
169 |
* @see get_bookmarks() |
|
170 |
* |
|
16 | 171 |
* @param array $bookmarks List of the cached bookmarks. |
172 |
* @param array $parsed_args An array of bookmark query arguments. |
|
5 | 173 |
*/ |
16 | 174 |
return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args ); |
5 | 175 |
} |
0 | 176 |
} |
177 |
||
5 | 178 |
if ( ! is_array( $cache ) ) { |
0 | 179 |
$cache = array(); |
5 | 180 |
} |
0 | 181 |
|
182 |
$inclusions = ''; |
|
16 | 183 |
if ( ! empty( $parsed_args['include'] ) ) { |
184 |
$parsed_args['exclude'] = ''; // Ignore exclude, category, and category_name params if using include. |
|
185 |
$parsed_args['category'] = ''; |
|
186 |
$parsed_args['category_name'] = ''; |
|
187 |
||
188 |
$inclinks = wp_parse_id_list( $parsed_args['include'] ); |
|
5 | 189 |
if ( count( $inclinks ) ) { |
0 | 190 |
foreach ( $inclinks as $inclink ) { |
5 | 191 |
if ( empty( $inclusions ) ) { |
9 | 192 |
$inclusions = ' AND ( link_id = ' . $inclink . ' '; |
5 | 193 |
} else { |
9 | 194 |
$inclusions .= ' OR link_id = ' . $inclink . ' '; |
5 | 195 |
} |
0 | 196 |
} |
197 |
} |
|
198 |
} |
|
9 | 199 |
if ( ! empty( $inclusions ) ) { |
0 | 200 |
$inclusions .= ')'; |
5 | 201 |
} |
0 | 202 |
|
203 |
$exclusions = ''; |
|
16 | 204 |
if ( ! empty( $parsed_args['exclude'] ) ) { |
205 |
$exlinks = wp_parse_id_list( $parsed_args['exclude'] ); |
|
5 | 206 |
if ( count( $exlinks ) ) { |
0 | 207 |
foreach ( $exlinks as $exlink ) { |
5 | 208 |
if ( empty( $exclusions ) ) { |
9 | 209 |
$exclusions = ' AND ( link_id <> ' . $exlink . ' '; |
5 | 210 |
} else { |
9 | 211 |
$exclusions .= ' AND link_id <> ' . $exlink . ' '; |
5 | 212 |
} |
0 | 213 |
} |
214 |
} |
|
215 |
} |
|
5 | 216 |
if ( ! empty( $exclusions ) ) { |
0 | 217 |
$exclusions .= ')'; |
5 | 218 |
} |
0 | 219 |
|
16 | 220 |
if ( ! empty( $parsed_args['category_name'] ) ) { |
221 |
$parsed_args['category'] = get_term_by( 'name', $parsed_args['category_name'], 'link_category' ); |
|
222 |
if ( $parsed_args['category'] ) { |
|
223 |
$parsed_args['category'] = $parsed_args['category']->term_id; |
|
0 | 224 |
} else { |
225 |
$cache[ $key ] = array(); |
|
226 |
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
|
5 | 227 |
/** This filter is documented in wp-includes/bookmark.php */ |
16 | 228 |
return apply_filters( 'get_bookmarks', array(), $parsed_args ); |
0 | 229 |
} |
230 |
} |
|
231 |
||
5 | 232 |
$search = ''; |
16 | 233 |
if ( ! empty( $parsed_args['search'] ) ) { |
234 |
$like = '%' . $wpdb->esc_like( $parsed_args['search'] ) . '%'; |
|
9 | 235 |
$search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like ); |
0 | 236 |
} |
237 |
||
238 |
$category_query = ''; |
|
9 | 239 |
$join = ''; |
16 | 240 |
if ( ! empty( $parsed_args['category'] ) ) { |
241 |
$incategories = wp_parse_id_list( $parsed_args['category'] ); |
|
9 | 242 |
if ( count( $incategories ) ) { |
0 | 243 |
foreach ( $incategories as $incat ) { |
5 | 244 |
if ( empty( $category_query ) ) { |
9 | 245 |
$category_query = ' AND ( tt.term_id = ' . $incat . ' '; |
5 | 246 |
} else { |
9 | 247 |
$category_query .= ' OR tt.term_id = ' . $incat . ' '; |
5 | 248 |
} |
0 | 249 |
} |
250 |
} |
|
251 |
} |
|
5 | 252 |
if ( ! empty( $category_query ) ) { |
0 | 253 |
$category_query .= ") AND taxonomy = 'link_category'"; |
9 | 254 |
$join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; |
0 | 255 |
} |
256 |
||
16 | 257 |
if ( $parsed_args['show_updated'] ) { |
9 | 258 |
$recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated '; |
0 | 259 |
} else { |
260 |
$recently_updated_test = ''; |
|
261 |
} |
|
262 |
||
16 | 263 |
$get_updated = ( $parsed_args['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; |
0 | 264 |
|
16 | 265 |
$orderby = strtolower( $parsed_args['orderby'] ); |
9 | 266 |
$length = ''; |
0 | 267 |
switch ( $orderby ) { |
268 |
case 'length': |
|
9 | 269 |
$length = ', CHAR_LENGTH(link_name) AS length'; |
0 | 270 |
break; |
271 |
case 'rand': |
|
272 |
$orderby = 'rand()'; |
|
273 |
break; |
|
274 |
case 'link_id': |
|
275 |
$orderby = "$wpdb->links.link_id"; |
|
276 |
break; |
|
277 |
default: |
|
278 |
$orderparams = array(); |
|
9 | 279 |
$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); |
5 | 280 |
foreach ( explode( ',', $orderby ) as $ordparam ) { |
281 |
$ordparam = trim( $ordparam ); |
|
282 |
||
16 | 283 |
if ( in_array( 'link_' . $ordparam, $keys, true ) ) { |
0 | 284 |
$orderparams[] = 'link_' . $ordparam; |
16 | 285 |
} elseif ( in_array( $ordparam, $keys, true ) ) { |
0 | 286 |
$orderparams[] = $ordparam; |
5 | 287 |
} |
0 | 288 |
} |
5 | 289 |
$orderby = implode( ',', $orderparams ); |
0 | 290 |
} |
291 |
||
5 | 292 |
if ( empty( $orderby ) ) { |
0 | 293 |
$orderby = 'link_name'; |
5 | 294 |
} |
0 | 295 |
|
16 | 296 |
$order = strtoupper( $parsed_args['order'] ); |
297 |
if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) { |
|
0 | 298 |
$order = 'ASC'; |
5 | 299 |
} |
0 | 300 |
|
301 |
$visible = ''; |
|
16 | 302 |
if ( $parsed_args['hide_invisible'] ) { |
0 | 303 |
$visible = "AND link_visible = 'Y'"; |
5 | 304 |
} |
0 | 305 |
|
9 | 306 |
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; |
0 | 307 |
$query .= " $exclusions $inclusions $search"; |
308 |
$query .= " ORDER BY $orderby $order"; |
|
16 | 309 |
if ( -1 != $parsed_args['limit'] ) { |
19 | 310 |
$query .= ' LIMIT ' . absint( $parsed_args['limit'] ); |
5 | 311 |
} |
0 | 312 |
|
5 | 313 |
$results = $wpdb->get_results( $query ); |
0 | 314 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
if ( 'rand()' !== $orderby ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
$cache[ $key ] = $results; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
} |
0 | 319 |
|
5 | 320 |
/** This filter is documented in wp-includes/bookmark.php */ |
16 | 321 |
return apply_filters( 'get_bookmarks', $results, $parsed_args ); |
0 | 322 |
} |
323 |
||
324 |
/** |
|
16 | 325 |
* Sanitizes all bookmark fields. |
0 | 326 |
* |
327 |
* @since 2.3.0 |
|
328 |
* |
|
16 | 329 |
* @param stdClass|array $bookmark Bookmark row. |
330 |
* @param string $context Optional. How to filter the fields. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
* @return stdClass|array Same type as $bookmark but with fields sanitized. |
0 | 332 |
*/ |
9 | 333 |
function sanitize_bookmark( $bookmark, $context = 'display' ) { |
334 |
$fields = array( |
|
335 |
'link_id', |
|
336 |
'link_url', |
|
337 |
'link_name', |
|
338 |
'link_image', |
|
339 |
'link_target', |
|
340 |
'link_category', |
|
341 |
'link_description', |
|
342 |
'link_visible', |
|
343 |
'link_owner', |
|
344 |
'link_rating', |
|
345 |
'link_updated', |
|
346 |
'link_rel', |
|
347 |
'link_notes', |
|
348 |
'link_rss', |
|
349 |
); |
|
0 | 350 |
|
9 | 351 |
if ( is_object( $bookmark ) ) { |
0 | 352 |
$do_object = true; |
9 | 353 |
$link_id = $bookmark->link_id; |
0 | 354 |
} else { |
355 |
$do_object = false; |
|
9 | 356 |
$link_id = $bookmark['link_id']; |
0 | 357 |
} |
358 |
||
359 |
foreach ( $fields as $field ) { |
|
360 |
if ( $do_object ) { |
|
9 | 361 |
if ( isset( $bookmark->$field ) ) { |
362 |
$bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context ); |
|
363 |
} |
|
0 | 364 |
} else { |
9 | 365 |
if ( isset( $bookmark[ $field ] ) ) { |
366 |
$bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context ); |
|
367 |
} |
|
0 | 368 |
} |
369 |
} |
|
370 |
||
371 |
return $bookmark; |
|
372 |
} |
|
373 |
||
374 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
* Sanitizes a bookmark field. |
0 | 376 |
* |
377 |
* Sanitizes the bookmark fields based on what the field name is. If the field |
|
378 |
* has a strict value set, then it will be tested for that, else a more generic |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
379 |
* filtering is applied. After the more strict filter is applied, if the `$context` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
380 |
* is 'raw' then the value is immediately return. |
0 | 381 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
382 |
* Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
383 |
* filter will be called and passed the `$value` and `$bookmark_id` respectively. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
385 |
* With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
386 |
* The 'display' context is the final context and has the `$field` has the filter name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* and is passed the `$value`, `$bookmark_id`, and `$context`, respectively. |
0 | 388 |
* |
389 |
* @since 2.3.0 |
|
390 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
* @param string $field The bookmark field. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* @param mixed $value The bookmark field value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
* @param int $bookmark_id Bookmark ID. |
18 | 394 |
* @param string $context How to filter the field value. Accepts 'raw', 'edit', 'db', |
395 |
* 'display', 'attribute', or 'js'. Default 'display'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
* @return mixed The filtered value. |
0 | 397 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) { |
18 | 399 |
$int_fields = array( 'link_id', 'link_rating' ); |
400 |
if ( in_array( $field, $int_fields, true ) ) { |
|
401 |
$value = (int) $value; |
|
402 |
} |
|
403 |
||
0 | 404 |
switch ( $field ) { |
9 | 405 |
case 'link_category': // array( ints ) |
406 |
$value = array_map( 'absint', (array) $value ); |
|
407 |
// We return here so that the categories aren't filtered. |
|
16 | 408 |
// The 'link_category' filter is for the name of a link category, not an array of a link's link categories. |
9 | 409 |
return $value; |
5 | 410 |
|
9 | 411 |
case 'link_visible': // bool stored as Y|N |
412 |
$value = preg_replace( '/[^YNyn]/', '', $value ); |
|
413 |
break; |
|
414 |
case 'link_target': // "enum" |
|
415 |
$targets = array( '_top', '_blank' ); |
|
16 | 416 |
if ( ! in_array( $value, $targets, true ) ) { |
9 | 417 |
$value = ''; |
418 |
} |
|
419 |
break; |
|
0 | 420 |
} |
421 |
||
16 | 422 |
if ( 'raw' === $context ) { |
0 | 423 |
return $value; |
9 | 424 |
} |
0 | 425 |
|
16 | 426 |
if ( 'edit' === $context ) { |
5 | 427 |
/** This filter is documented in wp-includes/post.php */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
$value = apply_filters( "edit_{$field}", $value, $bookmark_id ); |
0 | 429 |
|
16 | 430 |
if ( 'link_notes' === $field ) { |
0 | 431 |
$value = esc_html( $value ); // textarea_escaped |
432 |
} else { |
|
9 | 433 |
$value = esc_attr( $value ); |
0 | 434 |
} |
16 | 435 |
} elseif ( 'db' === $context ) { |
5 | 436 |
/** This filter is documented in wp-includes/post.php */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
$value = apply_filters( "pre_{$field}", $value ); |
0 | 438 |
} else { |
5 | 439 |
/** This filter is documented in wp-includes/post.php */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
$value = apply_filters( "{$field}", $value, $bookmark_id, $context ); |
0 | 441 |
|
16 | 442 |
if ( 'attribute' === $context ) { |
5 | 443 |
$value = esc_attr( $value ); |
16 | 444 |
} elseif ( 'js' === $context ) { |
5 | 445 |
$value = esc_js( $value ); |
446 |
} |
|
0 | 447 |
} |
448 |
||
18 | 449 |
// Restore the type for integer fields after esc_attr(). |
450 |
if ( in_array( $field, $int_fields, true ) ) { |
|
451 |
$value = (int) $value; |
|
452 |
} |
|
453 |
||
0 | 454 |
return $value; |
455 |
} |
|
456 |
||
457 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
* Deletes the bookmark cache. |
0 | 459 |
* |
460 |
* @since 2.7.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* @param int $bookmark_id Bookmark ID. |
0 | 463 |
*/ |
464 |
function clean_bookmark_cache( $bookmark_id ) { |
|
465 |
wp_cache_delete( $bookmark_id, 'bookmark' ); |
|
466 |
wp_cache_delete( 'get_bookmarks', 'bookmark' ); |
|
9 | 467 |
clean_object_term_cache( $bookmark_id, 'link' ); |
0 | 468 |
} |