]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/links.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-includes / links.php
1 <?php
2
3 /** function get_linksbyname()
4  ** Gets the links associated with category 'cat_name'.
5  ** Parameters:
6  **   cat_name (default 'noname')  - The category name to use. If no
7  **     match is found uses all
8  **   before (default '')  - the html to output before the link
9  **   after (default '<br />')  - the html to output after the link
10  **   between (default ' ')  - the html to output between the link/image
11  **     and it's description. Not used if no image or show_images == true
12  **   show_images (default true) - whether to show images (if defined).
13  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
14  **     'url', 'description' or 'rating'. Or maybe owner. If you start the
15  **     name with an underscore the order will be reversed.
16  **     You can also specify 'rand' as the order which will return links in a
17  **     random order.
18  **   show_description (default true) - whether to show the description if
19  **     show_images=false/not defined
20  **   show_rating (default false) - show rating stars/chars
21  **   limit (default -1) - Limit to X entries. If not specified, all entries
22  **     are shown.
23  **   show_updated (default 0) - whether to show last updated timestamp
24  */
25 function get_linksbyname($cat_name = "noname", $before = '', $after = '<br />',
26                          $between = " ", $show_images = true, $orderby = 'id',
27                          $show_description = true, $show_rating = false,
28                          $limit = -1, $show_updated = 0) {
29     global $wpdb;
30     $cat_id = -1;
31     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
32     if ($results) {
33         foreach ($results as $result) {
34             $cat_id = $result->cat_id;
35         }
36     }
37     get_links($cat_id, $before, $after, $between, $show_images, $orderby,
38               $show_description, $show_rating, $limit, $show_updated);
39 }
40
41 function bool_from_yn($yn) {
42     if ($yn == 'Y') return 1;
43     return 0;
44 }
45
46 /** function wp_get_linksbyname()
47  ** Gets the links associated with the named category.
48  ** Parameters:
49  **   category (no default)  - The category to use.
50  **/
51 function wp_get_linksbyname($category, $args = '') {
52         global $wpdb;
53
54         $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
55                                                                                                 . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
56                                                                                                 . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_name='$category'");
57
58         if (! $cat) {
59                 return;
60         }
61
62         if (empty($args)) {
63                 if ($cat->sort_desc == 'Y') {
64                         $cat->sort_order = '_'.$cat->sort_order;
65                 }
66                 get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
67                                                         $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
68                                                         bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
69                                                         $cat->list_limit, bool_from_yn($cat->show_updated));
70         } else {
71                 $args = add_query_arg('category', $cat->cat_id, $args);
72                 wp_get_links($args);
73         }
74 } // end wp_get_linksbyname
75
76 /** function wp_get_links()
77  ** Gets the links associated with category n.
78  ** Parameters:
79  **   category (no default)  - The category to use.
80  ** or:
81  **   a query string
82  **/
83 function wp_get_links($args = '') {
84         global $wpdb;
85
86         if (!empty($args) && false === strpos($args, '=')) {
87                 // If args is not a query string, it's a category id.
88                 $category = $args;
89                 $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
90                                                                                                         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
91                                                                                                         . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
92                 if ($cat) {
93                         if ($cat->sort_desc == 'Y') {
94                                 $cat->sort_order = '_'.$cat->sort_order;
95                         }
96                         get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
97                                                                 $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
98                                                                 bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
99                                                                 $cat->list_limit, bool_from_yn($cat->show_updated));
100                 }
101         } else {
102                 parse_str($args);
103
104                 if (! isset($category)) $category = -1;
105                 if (! isset($before)) $before = '';
106                 if (! isset($after)) $after = '<br />';
107                 if (! isset($between))  $between = ' ';
108                 if (! isset($show_images)) $show_images = true;
109                 if (! isset($orderby)) $orderby = 'name';
110                 if (! isset($show_description)) $show_description = true;
111                 if (! isset($show_rating)) $show_rating = false;
112                 if (! isset($limit)) $limit = -1;
113                 if (! isset($show_updated)) $show_updated = 1;
114                 if (! isset($echo)) $echo = true;
115
116                 return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
117         }
118 } // end wp_get_links
119
120 /** function get_links()
121  ** Gets the links associated with category n.
122  ** Parameters:
123  **   category (default -1)  - The category to use. If no category supplied
124  **      uses all
125  **   before (default '')  - the html to output before the link
126  **   after (default '<br />')  - the html to output after the link
127  **   between (default ' ')  - the html to output between the link/image
128  **     and its description. Not used if no image or show_images == true
129  **   show_images (default true) - whether to show images (if defined).
130  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
131  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
132  **     name with an underscore the order will be reversed.
133  **     You can also specify 'rand' as the order which will return links in a
134  **     random order.
135  **   show_description (default true) - whether to show the description if
136  **    show_images=false/not defined .
137  **   show_rating (default false) - show rating stars/chars
138  **   limit (default -1) - Limit to X entries. If not specified, all entries
139  **     are shown.
140  **   show_updated (default 0) - whether to show last updated timestamp
141  **   echo (default true) - whether to echo the results, or return them instead
142  */
143 function 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 {
165                 $recently_updated_test = '';
166         }
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) {
207                 if (!isset($row->recently_updated)) $row->recently_updated = false;
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) {
246                         if (strstr($row->link_image, 'http'))
247                                 $output .= "<img src=\"$row->link_image\" $alt $title />";
248                         else // If it's a relative path
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;
268         } else {
269                 return $output;
270         }
271 }
272
273
274 /** function get_linkobjectsbyname()
275  ** Gets an array of link objects associated with category 'cat_name'.
276  ** Parameters:
277  **   cat_name (default 'noname')  - The category name to use. If no
278  **     match is found uses all
279  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
280  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
281  **     name with an underscore the order will be reversed.
282  **     You can also specify 'rand' as the order which will return links in a
283  **     random order.
284  **   limit (default -1) - Limit to X entries. If not specified, all entries
285  **     are shown.
286  **
287  ** Use this like:
288  ** $links = get_linkobjectsbyname('fred');
289  ** foreach ($links as $link) {
290  **   echo '<li>'.$link->link_name.'</li>';
291  ** }
292  **/
293 function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
294     global $wpdb;
295     $cat_id = -1;
296     $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
297     if ($results) {
298         foreach ($results as $result) {
299             $cat_id = $result->cat_id;
300         }
301     }
302     return get_linkobjects($cat_id, $orderby, $limit);
303 }
304
305 /** function get_linkobjects()
306  ** Gets an array of link objects associated with category n.
307  ** Parameters:
308  **   category (default -1)  - The category to use. If no category supplied
309  **      uses all
310  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
311  **     'url', 'description', or 'rating'. Or maybe owner. If you start the
312  **     name with an underscore the order will be reversed.
313  **     You can also specify 'rand' as the order which will return links in a
314  **     random order.
315  **   limit (default -1) - Limit to X entries. If not specified, all entries
316  **     are shown.
317  **
318  ** Use this like:
319  ** $links = get_linkobjects(1);
320  ** if ($links) {
321  **   foreach ($links as $link) {
322  **     echo '<li>'.$link->link_name.'<br />'.$link->link_description.'</li>';
323  **   }
324  ** }
325  ** Fields are:
326  ** link_id
327  ** link_url
328  ** link_name
329  ** link_image
330  ** link_target
331  ** link_category
332  ** link_description
333  ** link_visible
334  ** link_owner
335  ** link_rating
336  ** link_updated
337  ** link_rel
338  ** link_notes
339  **/
340 function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
341     global $wpdb;
342
343     $sql = "SELECT * FROM $wpdb->links WHERE link_visible = 'Y'";
344     if ($category != -1) {
345         $sql .= " AND link_category = $category ";
346     }
347     if ($orderby == '')
348         $orderby = 'id';
349     if (substr($orderby,0,1) == '_') {
350         $direction = ' DESC';
351         $orderby = substr($orderby,1);
352     }
353     if (strcasecmp('rand',$orderby) == 0) {
354         $orderby = 'rand()';
355     } else {
356         $orderby = " link_" . $orderby;
357     }
358     $sql .= ' ORDER BY ' . $orderby;
359     $sql .= $direction;
360     /* The next 2 lines implement LIMIT TO processing */
361     if ($limit != -1)
362         $sql .= " LIMIT $limit";
363
364     $results = $wpdb->get_results($sql);
365     if ($results) {
366         foreach ($results as $result) {
367             $result->link_url         = $result->link_url;
368             $result->link_name        = $result->link_name;
369             $result->link_description = $result->link_description;
370             $result->link_notes       = $result->link_notes;
371             $newresults[] = $result;
372         }
373     }
374     return $newresults;
375 }
376
377 function get_linkrating($link) {
378     return apply_filters('link_rating', $link->link_rating);
379 }
380
381
382 /** function get_linksbyname_withrating()
383  ** Gets the links associated with category 'cat_name' and display rating stars/chars.
384  ** Parameters:
385  **   cat_name (default 'noname')  - The category name to use. If no
386  **     match is found uses all
387  **   before (default '')  - the html to output before the link
388  **   after (default '<br />')  - the html to output after the link
389  **   between (default ' ')  - the html to output between the link/image
390  **     and it's description. Not used if no image or show_images == true
391  **   show_images (default true) - whether to show images (if defined).
392  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
393  **     'url' or 'description'. Or maybe owner. If you start the
394  **     name with an underscore the order will be reversed.
395  **     You can also specify 'rand' as the order which will return links in a
396  **     random order.
397  **   show_description (default true) - whether to show the description if
398  **     show_images=false/not defined
399  **   limit (default -1) - Limit to X entries. If not specified, all entries
400  **     are shown.
401  **   show_updated (default 0) - whether to show last updated timestamp
402  */
403 function get_linksbyname_withrating($cat_name = "noname", $before = '',
404                                     $after = '<br />', $between = " ",
405                                     $show_images = true, $orderby = 'id',
406                                     $show_description = true, $limit = -1, $show_updated = 0) {
407
408     get_linksbyname($cat_name, $before, $after, $between, $show_images,
409                     $orderby, $show_description, true, $limit, $show_updated);
410 }
411
412 /** function get_links_withrating()
413  ** Gets the links associated with category n and display rating stars/chars.
414  ** Parameters:
415  **   category (default -1)  - The category to use. If no category supplied
416  **      uses all
417  **   before (default '')  - the html to output before the link
418  **   after (default '<br />')  - the html to output after the link
419  **   between (default ' ')  - the html to output between the link/image
420  **     and it's description. Not used if no image or show_images == true
421  **   show_images (default true) - whether to show images (if defined).
422  **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
423  **     'url' or 'description'. Or maybe owner. If you start the
424  **     name with an underscore the order will be reversed.
425  **     You can also specify 'rand' as the order which will return links in a
426  **     random order.
427  **   show_description (default true) - whether to show the description if
428  **    show_images=false/not defined .
429  **   limit (default -1) - Limit to X entries. If not specified, all entries
430  **     are shown.
431  **   show_updated (default 0) - whether to show last updated timestamp
432  */
433 function get_links_withrating($category = -1, $before = '', $after = '<br />',
434                               $between = " ", $show_images = true,
435                               $orderby = 'id', $show_description = true,
436                               $limit = -1, $show_updated = 0) {
437
438     get_links($category, $before, $after, $between, $show_images, $orderby,
439               $show_description, true, $limit, $show_updated);
440 }
441
442 /** function get_linkcatname()
443  ** Gets the name of category n.
444  ** Parameters: id (default 0)  - The category to get. If no category supplied
445  **                uses 0
446  */
447 function get_linkcatname($id = 0) {
448     $id = (int) $id;
449     global $wpdb;
450     $cat_name = '';
451     if ( !empty($id) ) {
452         $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
453     }
454     return $cat_name;
455 }
456
457 /** function get_get_autotoggle()
458  ** Gets the auto_toggle setting of category n.
459  ** Parameters: id (default 0)  - The category to get. If no category supplied
460  **                uses 0
461  */
462 function get_autotoggle($id = 0) {
463     global $wpdb;
464     $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
465     if ('' == $auto_toggle)
466         $auto_toggle = 'N';
467     return $auto_toggle;
468 }
469
470 /** function links_popup_script()
471  ** This function contributed by Fullo -- http://sprite.csr.unibo.it/fullo/
472  ** Show the link to the links popup and the number of links
473  ** Parameters:
474  **   text (default Links)  - the text of the link
475  **   width (default 400)  - the width of the popup window
476  **   height (default 400)  - the height of the popup window
477  **   file (default linkspopup.php) - the page to open in the popup window
478  **   count (default true) - the number of links in the db
479  */
480 function links_popup_script($text = 'Links', $width=400, $height=400,
481                             $file='links.all.php', $count = true) {
482    if ($count == true) {
483       $counts = $wpdb->get_var("SELECT count(*) FROM $wpdb->links");
484    }
485
486    $javascript = "<a href=\"#\" " .
487                  " onclick=\"javascript:window.open('$file?popup=1', '_blank', " .
488                  "'width=$width,height=$height,scrollbars=yes,status=no'); " .
489                  " return false\">";
490    $javascript .= $text;
491
492    if ($count == true) {
493       $javascript .= " ($counts)";
494    }
495
496    $javascript .="</a>\n\n";
497    echo $javascript;
498 }
499
500
501 /*
502  * function get_links_list()
503  *
504  * added by Dougal
505  *
506  * Output a list of all links, listed by category, using the
507  * settings in $wpdb->linkcategories and output it as a nested
508  * HTML unordered list.
509  *
510  * Parameters:
511  *   order (default 'name')  - Sort link categories by 'name' or 'id'
512  *   hide_if_empty (default true)  - Supress listing empty link categories
513  */
514 function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
515         global $wpdb;
516
517         $order = strtolower($order);
518
519         // Handle link category sorting
520         if (substr($order,0,1) == '_') {
521                 $direction = ' DESC';
522                 $order = substr($order,1);
523         }
524
525         // if 'name' wasn't specified, assume 'id':
526         $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
527
528         if (!isset($direction)) $direction = '';
529         // Fetch the link category data as an array of hashesa
530         $cats = $wpdb->get_results("
531                 SELECT DISTINCT link_category, cat_name, show_images, 
532                         show_description, show_rating, show_updated, sort_order, 
533                         sort_desc, list_limit
534                 FROM `$wpdb->links` 
535                 LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
536                 WHERE link_visible =  'Y'
537                         AND list_limit <> 0
538                 ORDER BY $cat_order $direction ", ARRAY_A);
539
540         // Display each category
541         if ($cats) {
542                 foreach ($cats as $cat) {
543                         // Handle each category.
544                         // First, fix the sort_order info
545                         $orderby = $cat['sort_order'];
546                         $orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
547
548                         // Display the category name
549                         echo '  <li id="linkcat-' . $cat['link_category'] . '"><h2>' . $cat['cat_name'] . "</h2>\n\t<ul>\n";
550                         // Call get_links() with all the appropriate params
551                         get_links($cat['link_category'],
552                                 '<li>',"</li>","\n",
553                                 bool_from_yn($cat['show_images']),
554                                 $orderby,
555                                 bool_from_yn($cat['show_description']),
556                                 bool_from_yn($cat['show_rating']),
557                                 $cat['list_limit'],
558                                 bool_from_yn($cat['show_updated']));
559
560                         // Close the last category
561                         echo "\n\t</ul>\n</li>\n";
562                 }
563         }
564 }
565
566 ?>