mirror of
https://github.com/WordPress/WordPress.git
synced 2025-01-03 15:08:10 +01:00
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:
parent
5c6d00bbfc
commit
e93dd02321
@ -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)
|
switch($orderby) {
|
||||||
$sql .= " LIMIT $limit";
|
case 'length':
|
||||||
//echo $sql;
|
$length = ", CHAR_LENGTH(link_name) AS length";
|
||||||
$results = $wpdb->get_results($sql);
|
break;
|
||||||
if (!$results) {
|
case 'rand':
|
||||||
return;
|
$orderby = 'rand()';
|
||||||
}
|
break;
|
||||||
|
default:
|
||||||
$output = "";
|
$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) {
|
||||||
if ($show_updated && $row->recently_updated) {
|
$output .= get_settings('links_recently_updated_prepend');
|
||||||
$output .= get_settings('links_recently_updated_prepend');
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$the_link = '#';
|
|
||||||
|
|
||||||
if ( !empty($row->link_url) )
|
|
||||||
$the_link = wp_specialchars($row->link_url);
|
|
||||||
$rel = $row->link_rel;
|
|
||||||
|
|
||||||
if ($rel != '') {
|
|
||||||
$rel = " rel='$rel'";
|
|
||||||
}
|
|
||||||
|
|
||||||
$desc = wp_specialchars($row->link_description, ENT_QUOTES);
|
|
||||||
$name = wp_specialchars($row->link_name, ENT_QUOTES);
|
|
||||||
|
|
||||||
$title = $desc;
|
$the_link = '#';
|
||||||
|
if (!empty($row->link_url))
|
||||||
|
$the_link = wp_specialchars($row->link_url);
|
||||||
|
|
||||||
if ($show_updated) {
|
$rel = $row->link_rel;
|
||||||
if (substr($row->link_updated_f,0,2) != '00') {
|
if ($rel != '') {
|
||||||
$title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) .')';
|
$rel = ' rel="' . $rel . '"';
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ('' != $title) {
|
$desc = wp_specialchars($row->link_description, ENT_QUOTES);
|
||||||
$title = " title='$title'";
|
$name = wp_specialchars($row->link_name, ENT_QUOTES);
|
||||||
}
|
$title = $desc;
|
||||||
|
|
||||||
$alt = " alt='$name'";
|
if ($show_updated) {
|
||||||
|
if (substr($row->link_updated_f, 0, 2) != '00') {
|
||||||
$target = $row->link_target;
|
$title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) . ')';
|
||||||
if ('' != $target) {
|
}
|
||||||
$target = " target='$target'";
|
}
|
||||||
}
|
|
||||||
|
if ('' != $title) {
|
||||||
$output.= "<a href='$the_link'";
|
$title = ' title="' . $title . '"';
|
||||||
$output.= $rel . $title . $target;
|
}
|
||||||
$output.= '>';
|
|
||||||
|
$alt = ' alt="' . $name . '"';
|
||||||
if (($row->link_image != null) && $show_images) {
|
|
||||||
|
$target = $row->link_target;
|
||||||
|
if ('' != $target) {
|
||||||
|
$target = ' target="' . $target . '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
$output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
|
||||||
|
|
||||||
|
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>';
|
|
||||||
|
|
||||||
if ($show_updated && $row->recently_updated) {
|
|
||||||
$output.= get_settings('links_recently_updated_append');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($show_description && ($desc != '')) {
|
$output .= '</a>';
|
||||||
$output.= $between.$desc;
|
|
||||||
}
|
if ($show_updated && $row->recently_updated) {
|
||||||
$output.= "$after\n";
|
$output .= get_settings('links_recently_updated_append');
|
||||||
} // end while
|
}
|
||||||
|
|
||||||
if($echo) {
|
if ($show_description && ($desc != '')) {
|
||||||
echo $output;
|
$output .= $between . $desc;
|
||||||
|
}
|
||||||
|
$output .= "$after\n";
|
||||||
|
} // end while
|
||||||
|
|
||||||
|
if ($echo) {
|
||||||
|
echo $output;
|
||||||
} else {
|
} else {
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user