Changeset 42343 for trunk/src/wp-includes/bookmark.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/bookmark.php
r39600 r42343 20 20 * @return array|object|null Type returned depends on $output value. 21 21 */ 22 function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw') {22 function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) { 23 23 global $wpdb; 24 24 25 if ( empty( $bookmark) ) {26 if ( isset( $GLOBALS['link']) )25 if ( empty( $bookmark ) ) { 26 if ( isset( $GLOBALS['link'] ) ) { 27 27 $_bookmark = & $GLOBALS['link']; 28 else28 } else { 29 29 $_bookmark = null; 30 } elseif ( is_object($bookmark) ) { 31 wp_cache_add($bookmark->link_id, $bookmark, 'bookmark'); 30 } 31 } elseif ( is_object( $bookmark ) ) { 32 wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' ); 32 33 $_bookmark = $bookmark; 33 34 } else { 34 if ( isset( $GLOBALS['link']) && ($GLOBALS['link']->link_id == $bookmark) ) {35 if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) { 35 36 $_bookmark = & $GLOBALS['link']; 36 } elseif ( ! $_bookmark = wp_cache_get( $bookmark, 'bookmark') ) {37 $_bookmark = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark));37 } elseif ( ! $_bookmark = wp_cache_get( $bookmark, 'bookmark' ) ) { 38 $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) ); 38 39 if ( $_bookmark ) { 39 40 $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) ); … … 43 44 } 44 45 45 if ( ! $_bookmark ) 46 if ( ! $_bookmark ) { 46 47 return $_bookmark; 47 48 $_bookmark = sanitize_bookmark($_bookmark, $filter); 48 } 49 50 $_bookmark = sanitize_bookmark( $_bookmark, $filter ); 49 51 50 52 if ( $output == OBJECT ) { 51 53 return $_bookmark; 52 54 } elseif ( $output == ARRAY_A ) { 53 return get_object_vars( $_bookmark);55 return get_object_vars( $_bookmark ); 54 56 } elseif ( $output == ARRAY_N ) { 55 return array_values( get_object_vars($_bookmark));57 return array_values( get_object_vars( $_bookmark ) ); 56 58 } else { 57 59 return $_bookmark; … … 73 75 $bookmark = get_bookmark( $bookmark ); 74 76 75 if ( is_wp_error( $bookmark) )77 if ( is_wp_error( $bookmark ) ) { 76 78 return $bookmark; 77 78 if ( !is_object($bookmark) ) 79 } 80 81 if ( ! is_object( $bookmark ) ) { 79 82 return ''; 80 81 if ( !isset($bookmark->$field) ) 83 } 84 85 if ( ! isset( $bookmark->$field ) ) { 82 86 return ''; 83 84 return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context); 87 } 88 89 return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context ); 85 90 } 86 91 … … 120 125 121 126 $defaults = array( 122 'orderby' => 'name', 'order' => 'ASC', 123 'limit' => -1, 'category' => '', 124 'category_name' => '', 'hide_invisible' => 1, 125 'show_updated' => 0, 'include' => '', 126 'exclude' => '', 'search' => '' 127 'orderby' => 'name', 128 'order' => 'ASC', 129 'limit' => -1, 130 'category' => '', 131 'category_name' => '', 132 'hide_invisible' => 1, 133 'show_updated' => 0, 134 'include' => '', 135 'exclude' => '', 136 'search' => '', 127 137 ); 128 138 129 139 $r = wp_parse_args( $args, $defaults ); 130 140 131 $key = md5( serialize( $r ) );141 $key = md5( serialize( $r ) ); 132 142 $cache = false; 133 143 if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { … … 159 169 $inclusions = ''; 160 170 if ( ! empty( $r['include'] ) ) { 161 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include162 $r['category'] = '';171 $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include 172 $r['category'] = ''; 163 173 $r['category_name'] = ''; 164 $inclinks = preg_split( '/[\s,]+/', $r['include'] );174 $inclinks = preg_split( '/[\s,]+/', $r['include'] ); 165 175 if ( count( $inclinks ) ) { 166 176 foreach ( $inclinks as $inclink ) { … … 173 183 } 174 184 } 175 if ( ! empty( $inclusions ) ) {185 if ( ! empty( $inclusions ) ) { 176 186 $inclusions .= ')'; 177 187 } … … 195 205 196 206 if ( ! empty( $r['category_name'] ) ) { 197 if ( $r['category'] = get_term_by( 'name', $r['category_name'], 'link_category') ) {207 if ( $r['category'] = get_term_by( 'name', $r['category_name'], 'link_category' ) ) { 198 208 $r['category'] = $r['category']->term_id; 199 209 } else { … … 207 217 $search = ''; 208 218 if ( ! empty( $r['search'] ) ) { 209 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%';210 $search = $wpdb->prepare( " AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ", $like, $like, $like );219 $like = '%' . $wpdb->esc_like( $r['search'] ) . '%'; 220 $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like ); 211 221 } 212 222 213 223 $category_query = ''; 214 $join = '';224 $join = ''; 215 225 if ( ! empty( $r['category'] ) ) { 216 226 $incategories = preg_split( '/[\s,]+/', $r['category'] ); 217 if ( count( $incategories) ) {227 if ( count( $incategories ) ) { 218 228 foreach ( $incategories as $incat ) { 219 229 if ( empty( $category_query ) ) { … … 227 237 if ( ! empty( $category_query ) ) { 228 238 $category_query .= ") AND taxonomy = 'link_category'"; 229 $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";239 $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"; 230 240 } 231 241 232 242 if ( $r['show_updated'] ) { 233 $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";243 $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated '; 234 244 } else { 235 245 $recently_updated_test = ''; … … 239 249 240 250 $orderby = strtolower( $r['orderby'] ); 241 $length = '';251 $length = ''; 242 252 switch ( $orderby ) { 243 253 case 'length': 244 $length = ", CHAR_LENGTH(link_name) AS length";254 $length = ', CHAR_LENGTH(link_name) AS length'; 245 255 break; 246 256 case 'rand': … … 252 262 default: 253 263 $orderparams = array(); 254 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );264 $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' ); 255 265 foreach ( explode( ',', $orderby ) as $ordparam ) { 256 266 $ordparam = trim( $ordparam ); … … 279 289 } 280 290 281 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";291 $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; 282 292 $query .= " $exclusions $inclusions $search"; 283 293 $query .= " ORDER BY $orderby $order"; … … 304 314 * @param stdClass|array $bookmark Bookmark row 305 315 * @param string $context Optional, default is 'display'. How to filter the 306 * 316 * fields 307 317 * @return stdClass|array Same type as $bookmark but with fields sanitized. 308 318 */ 309 function sanitize_bookmark($bookmark, $context = 'display') { 310 $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category', 311 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated', 312 'link_rel', 'link_notes', 'link_rss', ); 313 314 if ( is_object($bookmark) ) { 319 function sanitize_bookmark( $bookmark, $context = 'display' ) { 320 $fields = array( 321 'link_id', 322 'link_url', 323 'link_name', 324 'link_image', 325 'link_target', 326 'link_category', 327 'link_description', 328 'link_visible', 329 'link_owner', 330 'link_rating', 331 'link_updated', 332 'link_rel', 333 'link_notes', 334 'link_rss', 335 ); 336 337 if ( is_object( $bookmark ) ) { 315 338 $do_object = true; 316 $link_id = $bookmark->link_id;339 $link_id = $bookmark->link_id; 317 340 } else { 318 341 $do_object = false; 319 $link_id = $bookmark['link_id'];342 $link_id = $bookmark['link_id']; 320 343 } 321 344 322 345 foreach ( $fields as $field ) { 323 346 if ( $do_object ) { 324 if ( isset($bookmark->$field) ) 325 $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context); 347 if ( isset( $bookmark->$field ) ) { 348 $bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context ); 349 } 326 350 } else { 327 if ( isset($bookmark[$field]) ) 328 $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context); 351 if ( isset( $bookmark[ $field ] ) ) { 352 $bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context ); 353 } 329 354 } 330 355 } … … 359 384 function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) { 360 385 switch ( $field ) { 361 case 'link_id' : // ints 362 case 'link_rating' : 363 $value = (int) $value; 364 break; 365 case 'link_category' : // array( ints ) 366 $value = array_map('absint', (array) $value); 367 // We return here so that the categories aren't filtered. 368 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories 386 case 'link_id': // ints 387 case 'link_rating': 388 $value = (int) $value; 389 break; 390 case 'link_category': // array( ints ) 391 $value = array_map( 'absint', (array) $value ); 392 // We return here so that the categories aren't filtered. 393 // The 'link_category' filter is for the name of a link category, not an array of a link's link categories 394 return $value; 395 396 case 'link_visible': // bool stored as Y|N 397 $value = preg_replace( '/[^YNyn]/', '', $value ); 398 break; 399 case 'link_target': // "enum" 400 $targets = array( '_top', '_blank' ); 401 if ( ! in_array( $value, $targets ) ) { 402 $value = ''; 403 } 404 break; 405 } 406 407 if ( 'raw' == $context ) { 369 408 return $value; 370 371 case 'link_visible' : // bool stored as Y|N 372 $value = preg_replace('/[^YNyn]/', '', $value); 373 break; 374 case 'link_target' : // "enum" 375 $targets = array('_top', '_blank'); 376 if ( ! in_array($value, $targets) ) 377 $value = ''; 378 break; 379 } 380 381 if ( 'raw' == $context ) 382 return $value; 409 } 383 410 384 411 if ( 'edit' == $context ) { … … 389 416 $value = esc_html( $value ); // textarea_escaped 390 417 } else { 391 $value = esc_attr( $value);418 $value = esc_attr( $value ); 392 419 } 393 420 } elseif ( 'db' == $context ) { … … 418 445 wp_cache_delete( $bookmark_id, 'bookmark' ); 419 446 wp_cache_delete( 'get_bookmarks', 'bookmark' ); 420 clean_object_term_cache( $bookmark_id, 'link' );421 } 447 clean_object_term_cache( $bookmark_id, 'link' ); 448 }
Note: See TracChangeset
for help on using the changeset viewer.