Diff: STRATO-apps/wordpress_03/app/wp-includes/bookmark.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Link/Bookmark API
4 + *
5 + * @package WordPress
6 + * @subpackage Bookmark
7 + */
8 +
9 + /**
10 + * Retrieves bookmark data.
11 + *
12 + * @since 2.1.0
13 + *
14 + * @global object $link Current link object.
15 + * @global wpdb $wpdb WordPress database abstraction object.
16 + *
17 + * @param int|stdClass $bookmark
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'.
22 + * @return array|object|null Type returned depends on $output value.
23 + */
24 + function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
25 + global $wpdb;
26 +
27 + if ( empty( $bookmark ) ) {
28 + if ( isset( $GLOBALS['link'] ) ) {
29 + $_bookmark = & $GLOBALS['link'];
30 + } else {
31 + $_bookmark = null;
32 + }
33 + } elseif ( is_object( $bookmark ) ) {
34 + wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
35 + $_bookmark = $bookmark;
36 + } else {
37 + if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id === $bookmark ) ) {
38 + $_bookmark = & $GLOBALS['link'];
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 + }
47 + }
48 + }
49 + }
50 +
51 + if ( ! $_bookmark ) {
52 + return $_bookmark;
53 + }
54 +
55 + $_bookmark = sanitize_bookmark( $_bookmark, $filter );
56 +
57 + if ( OBJECT === $output ) {
58 + return $_bookmark;
59 + } elseif ( ARRAY_A === $output ) {
60 + return get_object_vars( $_bookmark );
61 + } elseif ( ARRAY_N === $output ) {
62 + return array_values( get_object_vars( $_bookmark ) );
63 + } else {
64 + return $_bookmark;
65 + }
66 + }
67 +
68 + /**
69 + * Retrieves single bookmark data item or field.
70 + *
71 + * @since 2.3.0
72 + *
73 + * @param string $field The name of the data field to return.
74 + * @param int $bookmark The bookmark ID to get field.
75 + * @param string $context Optional. The context of how the field will be used. Default 'display'.
76 + * @return string|WP_Error
77 + */
78 + function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
79 + $bookmark = (int) $bookmark;
80 + $bookmark = get_bookmark( $bookmark );
81 +
82 + if ( is_wp_error( $bookmark ) ) {
83 + return $bookmark;
84 + }
85 +
86 + if ( ! is_object( $bookmark ) ) {
87 + return '';
88 + }
89 +
90 + if ( ! isset( $bookmark->$field ) ) {
91 + return '';
92 + }
93 +
94 + return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context );
95 + }
96 +
97 + /**
98 + * Retrieves the list of bookmarks.
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 + *
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.
110 + *
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'.
117 + * @type string $order Whether to order bookmarks in ascending or descending order.
118 + * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
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.
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.
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.
133 + * }
134 + * @return object[] List of bookmark row objects.
135 + */
136 + function get_bookmarks( $args = '' ) {
137 + global $wpdb;
138 +
139 + $defaults = array(
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' => '',
150 + );
151 +
152 + $parsed_args = wp_parse_args( $args, $defaults );
153 +
154 + $key = md5( serialize( $parsed_args ) );
155 + $cache = wp_cache_get( 'get_bookmarks', 'bookmark' );
156 +
157 + if ( 'rand' !== $parsed_args['orderby'] && $cache ) {
158 + if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
159 + $bookmarks = $cache[ $key ];
160 + /**
161 + * Filters the returned list of bookmarks.
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 + *
172 + * @param array $bookmarks List of the cached bookmarks.
173 + * @param array $parsed_args An array of bookmark query arguments.
174 + */
175 + return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args );
176 + }
177 + }
178 +
179 + if ( ! is_array( $cache ) ) {
180 + $cache = array();
181 + }
182 +
183 + $inclusions = '';
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'] );
190 + if ( count( $inclinks ) ) {
191 + foreach ( $inclinks as $inclink ) {
192 + if ( empty( $inclusions ) ) {
193 + $inclusions = ' AND ( link_id = ' . $inclink . ' ';
194 + } else {
195 + $inclusions .= ' OR link_id = ' . $inclink . ' ';
196 + }
197 + }
198 + }
199 + }
200 + if ( ! empty( $inclusions ) ) {
201 + $inclusions .= ')';
202 + }
203 +
204 + $exclusions = '';
205 + if ( ! empty( $parsed_args['exclude'] ) ) {
206 + $exlinks = wp_parse_id_list( $parsed_args['exclude'] );
207 + if ( count( $exlinks ) ) {
208 + foreach ( $exlinks as $exlink ) {
209 + if ( empty( $exclusions ) ) {
210 + $exclusions = ' AND ( link_id <> ' . $exlink . ' ';
211 + } else {
212 + $exclusions .= ' AND link_id <> ' . $exlink . ' ';
213 + }
214 + }
215 + }
216 + }
217 + if ( ! empty( $exclusions ) ) {
218 + $exclusions .= ')';
219 + }
220 +
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;
225 + } else {
226 + $cache[ $key ] = array();
227 + wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
228 + /** This filter is documented in wp-includes/bookmark.php */
229 + return apply_filters( 'get_bookmarks', array(), $parsed_args );
230 + }
231 + }
232 +
233 + $search = '';
234 + if ( ! empty( $parsed_args['search'] ) ) {
235 + $like = '%' . $wpdb->esc_like( $parsed_args['search'] ) . '%';
236 + $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like );
237 + }
238 +
239 + $category_query = '';
240 + $join = '';
241 + if ( ! empty( $parsed_args['category'] ) ) {
242 + $incategories = wp_parse_id_list( $parsed_args['category'] );
243 + if ( count( $incategories ) ) {
244 + foreach ( $incategories as $incat ) {
245 + if ( empty( $category_query ) ) {
246 + $category_query = ' AND ( tt.term_id = ' . $incat . ' ';
247 + } else {
248 + $category_query .= ' OR tt.term_id = ' . $incat . ' ';
249 + }
250 + }
251 + }
252 + }
253 + if ( ! empty( $category_query ) ) {
254 + $category_query .= ") AND taxonomy = 'link_category'";
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";
256 + }
257 +
258 + if ( $parsed_args['show_updated'] ) {
259 + $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ';
260 + } else {
261 + $recently_updated_test = '';
262 + }
263 +
264 + $get_updated = ( $parsed_args['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
265 +
266 + $orderby = strtolower( $parsed_args['orderby'] );
267 + $length = '';
268 + switch ( $orderby ) {
269 + case 'length':
270 + $length = ', CHAR_LENGTH(link_name) AS length';
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();
280 + $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
281 + foreach ( explode( ',', $orderby ) as $ordparam ) {
282 + $ordparam = trim( $ordparam );
283 +
284 + if ( in_array( 'link_' . $ordparam, $keys, true ) ) {
285 + $orderparams[] = 'link_' . $ordparam;
286 + } elseif ( in_array( $ordparam, $keys, true ) ) {
287 + $orderparams[] = $ordparam;
288 + }
289 + }
290 + $orderby = implode( ',', $orderparams );
291 + }
292 +
293 + if ( empty( $orderby ) ) {
294 + $orderby = 'link_name';
295 + }
296 +
297 + $order = strtoupper( $parsed_args['order'] );
298 + if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) {
299 + $order = 'ASC';
300 + }
301 +
302 + $visible = '';
303 + if ( $parsed_args['hide_invisible'] ) {
304 + $visible = "AND link_visible = 'Y'";
305 + }
306 +
307 + $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
308 + $query .= " $exclusions $inclusions $search";
309 + $query .= " ORDER BY $orderby $order";
310 + if ( -1 !== $parsed_args['limit'] ) {
311 + $query .= ' LIMIT ' . absint( $parsed_args['limit'] );
312 + }
313 +
314 + $results = $wpdb->get_results( $query );
315 +
316 + if ( 'rand()' !== $orderby ) {
317 + $cache[ $key ] = $results;
318 + wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
319 + }
320 +
321 + /** This filter is documented in wp-includes/bookmark.php */
322 + return apply_filters( 'get_bookmarks', $results, $parsed_args );
323 + }
324 +
325 + /**
326 + * Sanitizes all bookmark fields.
327 + *
328 + * @since 2.3.0
329 + *
330 + * @param stdClass|array $bookmark Bookmark row.
331 + * @param string $context Optional. How to filter the fields. Default 'display'.
332 + * @return stdClass|array Same type as $bookmark but with fields sanitized.
333 + */
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 + );
351 +
352 + if ( is_object( $bookmark ) ) {
353 + $do_object = true;
354 + $link_id = $bookmark->link_id;
355 + } else {
356 + $do_object = false;
357 + $link_id = $bookmark['link_id'];
358 + }
359 +
360 + foreach ( $fields as $field ) {
361 + if ( $do_object ) {
362 + if ( isset( $bookmark->$field ) ) {
363 + $bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context );
364 + }
365 + } else {
366 + if ( isset( $bookmark[ $field ] ) ) {
367 + $bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context );
368 + }
369 + }
370 + }
371 +
372 + return $bookmark;
373 + }
374 +
375 + /**
376 + * Sanitizes a bookmark field.
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
380 + * filtering is applied. After the more strict filter is applied, if the `$context`
381 + * is 'raw' then the value is immediately return.
382 + *
383 + * Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'}
384 + * filter will be called and passed the `$value` and `$bookmark_id` respectively.
385 + *
386 + * With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value.
387 + * The 'display' context is the final context and has the `$field` has the filter name
388 + * and is passed the `$value`, `$bookmark_id`, and `$context`, respectively.
389 + *
390 + * @since 2.3.0
391 + *
392 + * @param string $field The bookmark field.
393 + * @param mixed $value The bookmark field value.
394 + * @param int $bookmark_id Bookmark ID.
395 + * @param string $context How to filter the field value. Accepts 'raw', 'edit', 'db',
396 + * 'display', 'attribute', or 'js'. Default 'display'.
397 + * @return mixed The filtered value.
398 + */
399 + function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
400 + $int_fields = array( 'link_id', 'link_rating' );
401 + if ( in_array( $field, $int_fields, true ) ) {
402 + $value = (int) $value;
403 + }
404 +
405 + switch ( $field ) {
406 + case 'link_category': // array( ints )
407 + $value = array_map( 'absint', (array) $value );
408 + /*
409 + * We return here so that the categories aren't filtered.
410 + * The 'link_category' filter is for the name of a link category, not an array of a link's link categories.
411 + */
412 + return $value;
413 +
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' );
419 + if ( ! in_array( $value, $targets, true ) ) {
420 + $value = '';
421 + }
422 + break;
423 + }
424 +
425 + if ( 'raw' === $context ) {
426 + return $value;
427 + }
428 +
429 + if ( 'edit' === $context ) {
430 + /** This filter is documented in wp-includes/post.php */
431 + $value = apply_filters( "edit_{$field}", $value, $bookmark_id );
432 +
433 + if ( 'link_notes' === $field ) {
434 + $value = esc_html( $value ); // textarea_escaped
435 + } else {
436 + $value = esc_attr( $value );
437 + }
438 + } elseif ( 'db' === $context ) {
439 + /** This filter is documented in wp-includes/post.php */
440 + $value = apply_filters( "pre_{$field}", $value );
441 + } else {
442 + /** This filter is documented in wp-includes/post.php */
443 + $value = apply_filters( "{$field}", $value, $bookmark_id, $context );
444 +
445 + if ( 'attribute' === $context ) {
446 + $value = esc_attr( $value );
447 + } elseif ( 'js' === $context ) {
448 + $value = esc_js( $value );
449 + }
450 + }
451 +
452 + // Restore the type for integer fields after esc_attr().
453 + if ( in_array( $field, $int_fields, true ) ) {
454 + $value = (int) $value;
455 + }
456 +
457 + return $value;
458 + }
459 +
460 + /**
461 + * Deletes the bookmark cache.
462 + *
463 + * @since 2.7.0
464 + *
465 + * @param int $bookmark_id Bookmark ID.
466 + */
467 + function clean_bookmark_cache( $bookmark_id ) {
468 + wp_cache_delete( $bookmark_id, 'bookmark' );
469 + wp_cache_delete( 'get_bookmarks', 'bookmark' );
470 + clean_object_term_cache( $bookmark_id, 'link' );
471 + }
472 +