Eliminate use of extract() in get_bookmarks().

See #22400.

Built from https://develop.svn.wordpress.org/trunk@28403


git-svn-id: http://core.svn.wordpress.org/trunk@28230 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2014-05-15 01:58:15 +00:00
parent a4a5560b22
commit 12a5b389ab

View File

@ -118,7 +118,7 @@ function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
* @param string|array $args List of arguments to overwrite the defaults * @param string|array $args List of arguments to overwrite the defaults
* @return array List of bookmark row objects * @return array List of bookmark row objects
*/ */
function get_bookmarks($args = '') { function get_bookmarks( $args = '' ) {
global $wpdb; global $wpdb;
$defaults = array( $defaults = array(
@ -130,11 +130,10 @@ function get_bookmarks($args = '') {
); );
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$key = md5( serialize( $r ) ); $key = md5( serialize( $r ) );
if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
if ( is_array($cache) && isset( $cache[ $key ] ) ) { if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
$bookmarks = $cache[ $key ]; $bookmarks = $cache[ $key ];
/** /**
* Filter the returned list of bookmarks. * Filter the returned list of bookmarks.
@ -155,45 +154,50 @@ function get_bookmarks($args = '') {
} }
} }
if ( !is_array($cache) ) if ( ! is_array( $cache ) ) {
$cache = array(); $cache = array();
}
$inclusions = ''; $inclusions = '';
if ( !empty($include) ) { if ( ! empty( $r['include'] ) ) {
$exclude = ''; //ignore exclude, category, and category_name params if using include $r['exclude'] = ''; //ignore exclude, category, and category_name params if using include
$category = ''; $r['category'] = '';
$category_name = ''; $r['category_name'] = '';
$inclinks = preg_split('/[\s,]+/',$include); $inclinks = preg_split( '/[\s,]+/', $r['include'] );
if ( count($inclinks) ) { if ( count( $inclinks ) ) {
foreach ( $inclinks as $inclink ) { foreach ( $inclinks as $inclink ) {
if (empty($inclusions)) if ( empty( $inclusions ) ) {
$inclusions = ' AND ( link_id = ' . intval($inclink) . ' '; $inclusions = ' AND ( link_id = ' . intval( $inclink ) . ' ';
else } else {
$inclusions .= ' OR link_id = ' . intval($inclink) . ' '; $inclusions .= ' OR link_id = ' . intval( $inclink ) . ' ';
}
} }
} }
} }
if (!empty($inclusions)) if (! empty( $inclusions ) ) {
$inclusions .= ')'; $inclusions .= ')';
}
$exclusions = ''; $exclusions = '';
if ( !empty($exclude) ) { if ( ! empty( $r['exclude'] ) ) {
$exlinks = preg_split('/[\s,]+/',$exclude); $exlinks = preg_split( '/[\s,]+/', $r['exclude'] );
if ( count($exlinks) ) { if ( count( $exlinks ) ) {
foreach ( $exlinks as $exlink ) { foreach ( $exlinks as $exlink ) {
if (empty($exclusions)) if ( empty( $exclusions ) ) {
$exclusions = ' AND ( link_id <> ' . intval($exlink) . ' '; $exclusions = ' AND ( link_id <> ' . intval( $exlink ) . ' ';
else } else {
$exclusions .= ' AND link_id <> ' . intval($exlink) . ' '; $exclusions .= ' AND link_id <> ' . intval( $exlink ) . ' ';
}
} }
} }
} }
if (!empty($exclusions)) if ( ! empty( $exclusions ) ) {
$exclusions .= ')'; $exclusions .= ')';
}
if ( !empty($category_name) ) { if ( ! empty( $r['category_name'] ) ) {
if ( $category = get_term_by('name', $category_name, 'link_category') ) { if ( $r['category'] = get_term_by('name', $r['category_name'], 'link_category') ) {
$category = $category->term_id; $r['category'] = $r['category']->term_id;
} else { } else {
$cache[ $key ] = array(); $cache[ $key ] = array();
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
@ -202,38 +206,40 @@ function get_bookmarks($args = '') {
} }
} }
if ( ! empty($search) ) { $search = '';
$search = esc_sql( like_escape( $search ) ); if ( ! empty( $r['search'] ) ) {
$search = esc_sql( like_escape( $r['search'] ) );
$search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) "; $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
} }
$category_query = ''; $category_query = '';
$join = ''; $join = '';
if ( !empty($category) ) { if ( ! empty( $r['category'] ) ) {
$incategories = preg_split('/[\s,]+/',$category); $incategories = preg_split( '/[\s,]+/', $r['category'] );
if ( count($incategories) ) { if ( count($incategories) ) {
foreach ( $incategories as $incat ) { foreach ( $incategories as $incat ) {
if (empty($category_query)) if ( empty( $category_query ) ) {
$category_query = ' AND ( tt.term_id = ' . intval($incat) . ' '; $category_query = ' AND ( tt.term_id = ' . intval( $incat ) . ' ';
else } else {
$category_query .= ' OR tt.term_id = ' . intval($incat) . ' '; $category_query .= ' OR tt.term_id = ' . intval( $incat ) . ' ';
}
} }
} }
} }
if (!empty($category_query)) { if ( ! empty( $category_query ) ) {
$category_query .= ") AND taxonomy = 'link_category'"; $category_query .= ") AND taxonomy = 'link_category'";
$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"; $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";
} }
if ( $show_updated ) { if ( $r['show_updated'] ) {
$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated "; $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ";
} else { } else {
$recently_updated_test = ''; $recently_updated_test = '';
} }
$get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : ''; $get_updated = ( $r['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
$orderby = strtolower($orderby); $orderby = strtolower( $r['orderby'] );
$length = ''; $length = '';
switch ( $orderby ) { switch ( $orderby ) {
case 'length': case 'length':
@ -247,35 +253,41 @@ function get_bookmarks($args = '') {
break; break;
default: default:
$orderparams = array(); $orderparams = array();
foreach ( explode(',', $orderby) as $ordparam ) { $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes' );
$ordparam = trim($ordparam); foreach ( explode( ',', $orderby ) as $ordparam ) {
$keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes' ); $ordparam = trim( $ordparam );
if ( in_array( 'link_' . $ordparam, $keys ) )
if ( in_array( 'link_' . $ordparam, $keys ) ) {
$orderparams[] = 'link_' . $ordparam; $orderparams[] = 'link_' . $ordparam;
elseif ( in_array( $ordparam, $keys ) ) } elseif ( in_array( $ordparam, $keys ) ) {
$orderparams[] = $ordparam; $orderparams[] = $ordparam;
}
} }
$orderby = implode(',', $orderparams); $orderby = implode( ',', $orderparams );
} }
if ( empty( $orderby ) ) if ( empty( $orderby ) ) {
$orderby = 'link_name'; $orderby = 'link_name';
}
$order = strtoupper( $order ); $order = strtoupper( $r['order'] );
if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) ) if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
$order = 'ASC'; $order = 'ASC';
}
$visible = ''; $visible = '';
if ( $hide_invisible ) if ( $r['hide_invisible'] ) {
$visible = "AND link_visible = 'Y'"; $visible = "AND link_visible = 'Y'";
}
$query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query"; $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
$query .= " $exclusions $inclusions $search"; $query .= " $exclusions $inclusions $search";
$query .= " ORDER BY $orderby $order"; $query .= " ORDER BY $orderby $order";
if ($limit != -1) if ( $r['limit'] != -1 ) {
$query .= " LIMIT $limit"; $query .= ' LIMIT ' . $r['limit'];
}
$results = $wpdb->get_results($query); $results = $wpdb->get_results( $query );
$cache[ $key ] = $results; $cache[ $key ] = $results;
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
@ -355,7 +367,7 @@ function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
// We return here so that the categories aren't filtered. // We return here so that the categories aren't filtered.
// The 'link_category' filter is for the name of a link category, not an array of a link's link categories // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
return $value; return $value;
case 'link_visible' : // bool stored as Y|N case 'link_visible' : // bool stored as Y|N
$value = preg_replace('/[^YNyn]/', '', $value); $value = preg_replace('/[^YNyn]/', '', $value);
break; break;