get_links() purtification. Props MaThIbUs. fixes #1244

git-svn-id: http://svn.automattic.com/wordpress/trunk@2795 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-08-20 01:46:18 +00:00
parent 5c6d00bbfc
commit e93dd02321

View File

@ -125,7 +125,7 @@ function wp_get_links($args = '') {
** before (default '') - the html to output before the link ** before (default '') - the html to output before the link
** after (default '<br />') - the html to output after the link ** after (default '<br />') - the html to output after the link
** between (default ' ') - the html to output between the link/image ** between (default ' ') - the html to output between the link/image
** and it's description. Not used if no image or show_images == true ** and its description. Not used if no image or show_images == true
** show_images (default true) - whether to show images (if defined). ** show_images (default true) - whether to show images (if defined).
** orderby (default 'id') - the order to output the links. E.g. 'id', 'name', ** orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
** 'url', 'description', or 'rating'. Or maybe owner. If you start the ** 'url', 'description', or 'rating'. Or maybe owner. If you start the
@ -138,136 +138,133 @@ function wp_get_links($args = '') {
** limit (default -1) - Limit to X entries. If not specified, all entries ** limit (default -1) - Limit to X entries. If not specified, all entries
** are shown. ** are shown.
** show_updated (default 0) - whether to show last updated timestamp ** show_updated (default 0) - whether to show last updated timestamp
** echo (default true) - whether to echo the results, or return them instead
*/ */
function get_links($category = -1, $before = '', $after = '<br />', function get_links($category = -1,
$between = ' ', $show_images = true, $orderby = 'name', $before = '',
$show_description = true, $show_rating = false, $after = '<br />',
$limit = -1, $show_updated = 1, $echo = true) { $between = ' ',
$show_images = true,
$orderby = 'name',
$show_description = true,
$show_rating = false,
$limit = -1,
$show_updated = 1,
$echo = true) {
global $wpdb; global $wpdb;
$direction = ' ASC'; $direction = ' ASC';
$category_query = ""; $category_query = '';
if ($category != -1) { if ($category != -1) {
$category_query = " AND link_category = $category "; $category_query = " AND link_category = $category ";
} }
if (get_settings('links_recently_updated_time')) { if (get_settings('links_recently_updated_time')) {
$recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL ".get_settings('links_recently_updated_time')." MINUTE) >= NOW(), 1,0) as recently_updated "; $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
} else { } else {
$recently_updated_test = ''; $recently_updated_test = '';
} }
if ($show_updated) { if ($show_updated) {
$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
}
$orderby=strtolower($orderby);
if ($orderby == '')
$orderby = 'id';
if (substr($orderby,0,1) == '_') {
$direction = ' DESC';
$orderby = substr($orderby,1);
}
switch($orderby) {
case 'length':
$length = ",CHAR_LENGTH(link_name) AS length";
break;
case 'rand':
$orderby = 'rand()';
break;
default:
$orderby = " link_" . $orderby;
}
if (!isset($length)) {
$length = "";
} }
$sql = "SELECT link_url, link_name, link_image, link_target, $orderby = strtolower($orderby);
link_description, link_rating, link_rel $length $recently_updated_test $get_updated if ($orderby == '')
FROM $wpdb->links $orderby = 'id';
WHERE link_visible = 'Y' " . if (substr($orderby, 0, 1) == '_') {
$category_query; $direction = ' DESC';
$sql .= ' ORDER BY ' . $orderby; $orderby = substr($orderby, 1);
$sql .= $direction; }
/* The next 2 lines implement LIMIT TO processing */
if ($limit != -1)
$sql .= " LIMIT $limit";
//echo $sql;
$results = $wpdb->get_results($sql);
if (!$results) {
return;
}
$output = ""; switch($orderby) {
case 'length':
$length = ", CHAR_LENGTH(link_name) AS length";
break;
case 'rand':
$orderby = 'rand()';
break;
default:
$orderby = " link_" . $orderby;
}
foreach ($results as $row) { if (!isset($length)) {
$length = '';
}
$sql = "SELECT link_url, link_name, link_image, link_target, link_description, link_rating, link_rel $length $recently_updated_test $get_updated FROM $wpdb->links WHERE link_visible = 'Y' " . $category_query;
$sql .= ' ORDER BY ' . $orderby . $direction;
/* The next 2 lines implement LIMIT TO processing */
if ($limit != -1)
$sql .= " LIMIT $limit";
$results = $wpdb->get_results($sql);
if (!$results) {
return;
}
$output = '';
foreach ($results as $row) {
if (!isset($row->recently_updated)) $row->recently_updated = false; if (!isset($row->recently_updated)) $row->recently_updated = false;
$output .= ($before); $output .= $before;
if ($show_updated && $row->recently_updated) {
$output .= get_settings('links_recently_updated_prepend');
}
if ($show_updated && $row->recently_updated) { $the_link = '#';
$output .= get_settings('links_recently_updated_prepend'); if (!empty($row->link_url))
} $the_link = wp_specialchars($row->link_url);
$the_link = '#'; $rel = $row->link_rel;
if ($rel != '') {
$rel = ' rel="' . $rel . '"';
}
if ( !empty($row->link_url) ) $desc = wp_specialchars($row->link_description, ENT_QUOTES);
$the_link = wp_specialchars($row->link_url); $name = wp_specialchars($row->link_name, ENT_QUOTES);
$rel = $row->link_rel; $title = $desc;
if ($rel != '') { if ($show_updated) {
$rel = " rel='$rel'"; if (substr($row->link_updated_f, 0, 2) != '00') {
} $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) . ')';
}
}
$desc = wp_specialchars($row->link_description, ENT_QUOTES); if ('' != $title) {
$name = wp_specialchars($row->link_name, ENT_QUOTES); $title = ' title="' . $title . '"';
}
$title = $desc; $alt = ' alt="' . $name . '"';
if ($show_updated) { $target = $row->link_target;
if (substr($row->link_updated_f,0,2) != '00') { if ('' != $target) {
$title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) .')'; $target = ' target="' . $target . '"';
} }
}
if ('' != $title) { $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
$title = " title='$title'";
}
$alt = " alt='$name'"; if (($row->link_image != null) && $show_images) {
$target = $row->link_target;
if ('' != $target) {
$target = " target='$target'";
}
$output.= "<a href='$the_link'";
$output.= $rel . $title . $target;
$output.= '>';
if (($row->link_image != null) && $show_images) {
if (strstr($row->link_image, 'http')) if (strstr($row->link_image, 'http'))
$output.= "<img src='$row->link_image' $alt $title />"; $output .= "<img src=\"$row->link_image\" $alt $title />";
else // If it's a relative path else // If it's a relative path
$output.= "<img src='" . get_settings('siteurl') . "$row->link_image' $alt $title />"; $output .= "<img src=\"" . get_settings('siteurl') . "$row->link_image\" $alt $title />";
} else { } else {
$output.= $name; $output .= $name;
} }
$output.= '</a>'; $output .= '</a>';
if ($show_updated && $row->recently_updated) { if ($show_updated && $row->recently_updated) {
$output.= get_settings('links_recently_updated_append'); $output .= get_settings('links_recently_updated_append');
} }
if ($show_description && ($desc != '')) { if ($show_description && ($desc != '')) {
$output.= $between.$desc; $output .= $between . $desc;
} }
$output.= "$after\n"; $output .= "$after\n";
} // end while } // end while
if($echo) { if ($echo) {
echo $output; echo $output;
} else { } else {
return $output; return $output;
} }