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