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
1 changed files with 113 additions and 116 deletions

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,21 +138,29 @@ 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 = '';
} }
@ -160,17 +168,17 @@ function get_links($category = -1, $before = '', $after = '<br />',
$get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f "; $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
} }
$orderby=strtolower($orderby); $orderby = strtolower($orderby);
if ($orderby == '') if ($orderby == '')
$orderby = 'id'; $orderby = 'id';
if (substr($orderby,0,1) == '_') { if (substr($orderby, 0, 1) == '_') {
$direction = ' DESC'; $direction = ' DESC';
$orderby = substr($orderby,1); $orderby = substr($orderby, 1);
} }
switch($orderby) { switch($orderby) {
case 'length': case 'length':
$length = ",CHAR_LENGTH(link_name) AS length"; $length = ", CHAR_LENGTH(link_name) AS length";
break; break;
case 'rand': case 'rand':
$orderby = 'rand()'; $orderby = 'rand()';
@ -180,93 +188,82 @@ function get_links($category = -1, $before = '', $after = '<br />',
} }
if (!isset($length)) { if (!isset($length)) {
$length = ""; $length = '';
} }
$sql = "SELECT link_url, link_name, link_image, link_target, $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;
link_description, link_rating, link_rel $length $recently_updated_test $get_updated $sql .= ' ORDER BY ' . $orderby . $direction;
FROM $wpdb->links
WHERE link_visible = 'Y' " .
$category_query;
$sql .= ' ORDER BY ' . $orderby;
$sql .= $direction;
/* The next 2 lines implement LIMIT TO processing */ /* The next 2 lines implement LIMIT TO processing */
if ($limit != -1) if ($limit != -1)
$sql .= " LIMIT $limit"; $sql .= " LIMIT $limit";
//echo $sql;
$results = $wpdb->get_results($sql); $results = $wpdb->get_results($sql);
if (!$results) { if (!$results) {
return; return;
} }
$output = ""; $output = '';
foreach ($results as $row) { 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) { if ($show_updated && $row->recently_updated) {
$output .= get_settings('links_recently_updated_prepend'); $output .= get_settings('links_recently_updated_prepend');
} }
$the_link = '#'; $the_link = '#';
if (!empty($row->link_url))
if ( !empty($row->link_url) )
$the_link = wp_specialchars($row->link_url); $the_link = wp_specialchars($row->link_url);
$rel = $row->link_rel;
$rel = $row->link_rel;
if ($rel != '') { if ($rel != '') {
$rel = " rel='$rel'"; $rel = ' rel="' . $rel . '"';
} }
$desc = wp_specialchars($row->link_description, ENT_QUOTES); $desc = wp_specialchars($row->link_description, ENT_QUOTES);
$name = wp_specialchars($row->link_name, ENT_QUOTES); $name = wp_specialchars($row->link_name, ENT_QUOTES);
$title = $desc; $title = $desc;
if ($show_updated) { if ($show_updated) {
if (substr($row->link_updated_f,0,2) != '00') { 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)) .')'; $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) . ')';
} }
} }
if ('' != $title) { if ('' != $title) {
$title = " title='$title'"; $title = ' title="' . $title . '"';
} }
$alt = " alt='$name'"; $alt = ' alt="' . $name . '"';
$target = $row->link_target; $target = $row->link_target;
if ('' != $target) { if ('' != $target) {
$target = " target='$target'"; $target = ' target="' . $target . '"';
} }
$output.= "<a href='$the_link'"; $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
$output.= $rel . $title . $target;
$output.= '>';
if (($row->link_image != null) && $show_images) { 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;