Make WordPress Core


Ignore:
Timestamp:
08/20/2005 01:46:18 AM (20 years ago)
Author:
ryan
Message:

get_links() purtification. Props MaThIbUs. fixes #1244

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/links.php

    r2656 r2795  
    126126 **   after (default '<br />')  - the html to output after the link
    127127 **   between (default ' ')  - the html to output between the link/image
    128  **     and it's description. Not used if no image or show_images == true
     128 **     and its description. Not used if no image or show_images == true
    129129 **   show_images (default true) - whether to show images (if defined).
    130130 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
     
    139139 **     are shown.
    140140 **   show_updated (default 0) - whether to show last updated timestamp
    141  */
    142 function get_links($category = -1, $before = '', $after = '<br />',
    143                    $between = ' ', $show_images = true, $orderby = 'name',
    144                    $show_description = true, $show_rating = false,
    145                    $limit = -1, $show_updated = 1, $echo = true) {
    146 
    147     global $wpdb;
    148 
    149     $direction = ' ASC';
    150     $category_query = "";
    151     if ($category != -1) {
    152         $category_query = " AND link_category = $category ";
    153     }
    154     if (get_settings('links_recently_updated_time')) {
    155         $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL ".get_settings('links_recently_updated_time')." MINUTE) >= NOW(), 1,0) as recently_updated ";
    156     } else {
     141 **   echo (default true) - whether to echo the results, or return them instead
     142 */
     143function get_links($category = -1,
     144            $before = '',
     145            $after = '<br />',
     146            $between = ' ',
     147            $show_images = true,
     148            $orderby = 'name',
     149            $show_description = true,
     150            $show_rating = false,
     151            $limit = -1,
     152            $show_updated = 1,
     153            $echo = true) {
     154
     155    global $wpdb;
     156
     157    $direction = ' ASC';
     158    $category_query = '';
     159    if ($category != -1) {
     160        $category_query = " AND link_category = $category ";
     161    }
     162    if (get_settings('links_recently_updated_time')) {
     163        $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_settings('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
     164    } else {
    157165        $recently_updated_test = '';
    158166    }
    159     if ($show_updated) {
    160         $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
    161     }
    162 
    163     $orderby=strtolower($orderby);
    164     if ($orderby == '')
    165         $orderby = 'id';
    166     if (substr($orderby,0,1) == '_') {
    167         $direction = ' DESC';
    168         $orderby = substr($orderby,1);
    169     }
    170 
    171     switch($orderby) {
    172         case 'length':
    173         $length = ",CHAR_LENGTH(link_name) AS length";
    174         break;
    175         case 'rand':
    176             $orderby = 'rand()';
    177             break;
    178         default:
    179             $orderby = " link_" . $orderby;
    180     }
    181 
    182     if (!isset($length)) {
    183         $length = "";
    184     }
    185 
    186     $sql = "SELECT link_url, link_name, link_image, link_target,
    187             link_description, link_rating, link_rel $length $recently_updated_test $get_updated
    188             FROM $wpdb->links
    189             WHERE link_visible = 'Y' " .
    190            $category_query;
    191     $sql .= ' ORDER BY ' . $orderby;
    192     $sql .= $direction;
    193     /* The next 2 lines implement LIMIT TO processing */
    194     if ($limit != -1)
    195         $sql .= " LIMIT $limit";
    196     //echo $sql;
    197     $results = $wpdb->get_results($sql);
    198     if (!$results) {
    199         return;
    200     }
    201    
    202     $output = "";
    203    
    204     foreach ($results as $row) {
     167    if ($show_updated) {
     168        $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
     169    }
     170
     171    $orderby = strtolower($orderby);
     172    if ($orderby == '')
     173        $orderby = 'id';
     174    if (substr($orderby, 0, 1) == '_') {
     175        $direction = ' DESC';
     176        $orderby = substr($orderby, 1);
     177    }
     178
     179    switch($orderby) {
     180        case 'length':
     181        $length = ", CHAR_LENGTH(link_name) AS length";
     182        break;
     183        case 'rand':
     184            $orderby = 'rand()';
     185            break;
     186        default:
     187            $orderby = " link_" . $orderby;
     188    }
     189
     190    if (!isset($length)) {
     191        $length = '';
     192    }
     193
     194    $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;
     195    $sql .= ' ORDER BY ' . $orderby . $direction;
     196    /* The next 2 lines implement LIMIT TO processing */
     197    if ($limit != -1)
     198        $sql .= " LIMIT $limit";
     199    $results = $wpdb->get_results($sql);
     200    if (!$results) {
     201        return;
     202    }
     203
     204    $output = '';
     205
     206    foreach ($results as $row) {
    205207        if (!isset($row->recently_updated)) $row->recently_updated = false;
    206         $output .= ($before);
    207        
    208         if ($show_updated && $row->recently_updated) {
    209             $output .= get_settings('links_recently_updated_prepend');
    210         }
    211        
    212         $the_link = '#';
    213        
    214         if ( !empty($row->link_url) )
    215             $the_link = wp_specialchars($row->link_url);
    216         $rel = $row->link_rel;
    217        
    218         if ($rel != '') {
    219             $rel = " rel='$rel'";
    220         }
    221        
    222         $desc = wp_specialchars($row->link_description, ENT_QUOTES);
    223         $name = wp_specialchars($row->link_name, ENT_QUOTES);
    224 
    225         $title = $desc;
    226 
    227         if ($show_updated) {
    228            if (substr($row->link_updated_f,0,2) != '00') {
    229                 $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) .')';
    230             }
    231         }
    232 
    233         if ('' != $title) {
    234             $title = " title='$title'";
    235         }
    236 
    237         $alt = " alt='$name'";
    238            
    239         $target = $row->link_target;
    240         if ('' != $target) {
    241             $target = " target='$target'";
    242         }
    243        
    244         $output.= "<a href='$the_link'";
    245         $output.= $rel . $title . $target;
    246         $output.= '>';
    247        
    248         if (($row->link_image != null) && $show_images) {
     208            $output .= $before;
     209        if ($show_updated && $row->recently_updated) {
     210            $output .= get_settings('links_recently_updated_prepend');
     211        }
     212
     213        $the_link = '#';
     214        if (!empty($row->link_url))
     215            $the_link = wp_specialchars($row->link_url);
     216
     217        $rel = $row->link_rel;
     218        if ($rel != '') {
     219            $rel = ' rel="' . $rel . '"';
     220        }
     221
     222        $desc = wp_specialchars($row->link_description, ENT_QUOTES);
     223        $name = wp_specialchars($row->link_name, ENT_QUOTES);
     224        $title = $desc;
     225
     226        if ($show_updated) {
     227            if (substr($row->link_updated_f, 0, 2) != '00') {
     228                $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) . ')';
     229            }
     230        }
     231
     232        if ('' != $title) {
     233            $title = ' title="' . $title . '"';
     234        }
     235
     236        $alt = ' alt="' . $name . '"';
     237
     238        $target = $row->link_target;
     239        if ('' != $target) {
     240            $target = ' target="' . $target . '"';
     241        }
     242
     243        $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
     244
     245        if (($row->link_image != null) && $show_images) {
    249246            if (strstr($row->link_image, 'http'))
    250                 $output.= "<img src='$row->link_image' $alt $title />";
     247                $output .= "<img src=\"$row->link_image\" $alt $title />";
    251248            else // If it's a relative path
    252                 $output.= "<img src='" . get_settings('siteurl') . "$row->link_image' $alt $title />";
    253         } else {
    254             $output.= $name;
    255         }
    256        
    257         $output.= '</a>';
    258        
    259         if ($show_updated && $row->recently_updated) {
    260             $output.= get_settings('links_recently_updated_append');
    261         }
    262 
    263         if ($show_description && ($desc != '')) {
    264             $output.= $between.$desc;
    265         }
    266         $output.= "$after\n";
    267     } // end while
    268    
    269     if($echo) {
    270         echo $output;
     249                $output .= "<img src=\"" . get_settings('siteurl') . "$row->link_image\" $alt $title />";
     250        } else {
     251            $output .= $name;
     252        }
     253
     254        $output .= '</a>';
     255
     256        if ($show_updated && $row->recently_updated) {
     257            $output .= get_settings('links_recently_updated_append');
     258        }
     259
     260        if ($show_description && ($desc != '')) {
     261            $output .= $between . $desc;
     262        }
     263        $output .= "$after\n";
     264    } // end while
     265
     266    if ($echo) {
     267        echo $output;
    271268    } else {
    272269        return $output;
Note: See TracChangeset for help on using the changeset viewer.