3 /** function get_linksbyname()
4 ** Gets the links associated with category 'cat_name'.
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
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
23 ** show_updated (default 0) - whether to show last updated timestamp
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) {
31 $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
33 foreach ($results as $result) {
34 $cat_id = $result->cat_id;
37 get_links($cat_id, $before, $after, $between, $show_images, $orderby,
38 $show_description, $show_rating, $limit, $show_updated);
41 function bool_from_yn($yn) {
42 if ($yn == 'Y') return 1;
46 /** function wp_get_linksbyname()
47 ** Gets the links associated with the named category.
49 ** category (no default) - The category to use.
51 function wp_get_linksbyname($category, $args = '') {
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'");
63 if ($cat->sort_desc == 'Y') {
64 $cat->sort_order = '_'.$cat->sort_order;
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));
71 $args = add_query_arg('category', $cat->cat_id, $args);
74 } // end wp_get_linksbyname
76 /** function wp_get_links()
77 ** Gets the links associated with category n.
79 ** category (no default) - The category to use.
83 function wp_get_links($args = '') {
86 if (!empty($args) && false === strpos($args, '=')) {
87 // If args is not a query string, it's a category id.
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");
93 if ($cat->sort_desc == 'Y') {
94 $cat->sort_order = '_'.$cat->sort_order;
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));
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;
116 return get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $echo);
118 } // end wp_get_links
120 /** function get_links()
121 ** Gets the links associated with category n.
123 ** category (default -1) - The category to use. If no category supplied
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
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
140 ** show_updated (default 0) - whether to show last updated timestamp
141 ** echo (default true) - whether to echo the results, or return them instead
143 function get_links($category = -1,
149 $show_description = true,
150 $show_rating = false,
158 $category_query = '';
159 if ($category != -1) {
160 $category_query = " AND link_category = $category ";
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 ";
165 $recently_updated_test = '';
168 $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
171 $orderby = strtolower($orderby);
174 if (substr($orderby, 0, 1) == '_') {
175 $direction = ' DESC';
176 $orderby = substr($orderby, 1);
181 $length = ", CHAR_LENGTH(link_name) AS length";
187 $orderby = " link_" . $orderby;
190 if (!isset($length)) {
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 */
198 $sql .= " LIMIT $limit";
199 $results = $wpdb->get_results($sql);
206 foreach ($results as $row) {
207 if (!isset($row->recently_updated)) $row->recently_updated = false;
209 if ($show_updated && $row->recently_updated) {
210 $output .= get_settings('links_recently_updated_prepend');
214 if (!empty($row->link_url))
215 $the_link = wp_specialchars($row->link_url);
217 $rel = $row->link_rel;
219 $rel = ' rel="' . $rel . '"';
222 $desc = wp_specialchars($row->link_description, ENT_QUOTES);
223 $name = wp_specialchars($row->link_name, ENT_QUOTES);
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)) . ')';
233 $title = ' title="' . $title . '"';
236 $alt = ' alt="' . $name . '"';
238 $target = $row->link_target;
240 $target = ' target="' . $target . '"';
243 $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
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 />";
256 if ($show_updated && $row->recently_updated) {
257 $output .= get_settings('links_recently_updated_append');
260 if ($show_description && ($desc != '')) {
261 $output .= $between . $desc;
263 $output .= "$after\n";
274 /** function get_linkobjectsbyname()
275 ** Gets an array of link objects associated with category 'cat_name'.
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
284 ** limit (default -1) - Limit to X entries. If not specified, all entries
288 ** $links = get_linkobjectsbyname('fred');
289 ** foreach ($links as $link) {
290 ** echo '<li>'.$link->link_name.'</li>';
293 function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
296 $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
298 foreach ($results as $result) {
299 $cat_id = $result->cat_id;
302 return get_linkobjects($cat_id, $orderby, $limit);
305 /** function get_linkobjects()
306 ** Gets an array of link objects associated with category n.
308 ** category (default -1) - The category to use. If no category supplied
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
315 ** limit (default -1) - Limit to X entries. If not specified, all entries
319 ** $links = get_linkobjects(1);
321 ** foreach ($links as $link) {
322 ** echo '<li>'.$link->link_name.'<br />'.$link->link_description.'</li>';
340 function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
343 $sql = "SELECT * FROM $wpdb->links WHERE link_visible = 'Y'";
344 if ($category != -1) {
345 $sql .= " AND link_category = $category ";
349 if (substr($orderby,0,1) == '_') {
350 $direction = ' DESC';
351 $orderby = substr($orderby,1);
353 if (strcasecmp('rand',$orderby) == 0) {
356 $orderby = " link_" . $orderby;
358 $sql .= ' ORDER BY ' . $orderby;
360 /* The next 2 lines implement LIMIT TO processing */
362 $sql .= " LIMIT $limit";
364 $results = $wpdb->get_results($sql);
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;
377 function get_linkrating($link) {
378 return apply_filters('link_rating', $link->link_rating);
382 /** function get_linksbyname_withrating()
383 ** Gets the links associated with category 'cat_name' and display rating stars/chars.
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
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
401 ** show_updated (default 0) - whether to show last updated timestamp
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) {
408 get_linksbyname($cat_name, $before, $after, $between, $show_images,
409 $orderby, $show_description, true, $limit, $show_updated);
412 /** function get_links_withrating()
413 ** Gets the links associated with category n and display rating stars/chars.
415 ** category (default -1) - The category to use. If no category supplied
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
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
431 ** show_updated (default 0) - whether to show last updated timestamp
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) {
438 get_links($category, $before, $after, $between, $show_images, $orderby,
439 $show_description, true, $limit, $show_updated);
442 /** function get_linkcatname()
443 ** Gets the name of category n.
444 ** Parameters: id (default 0) - The category to get. If no category supplied
447 function get_linkcatname($id = 0) {
452 $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
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
462 function get_autotoggle($id = 0) {
464 $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
465 if ('' == $auto_toggle)
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
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
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");
486 $javascript = "<a href=\"#\" " .
487 " onclick=\"javascript:window.open('$file?popup=1', '_blank', " .
488 "'width=$width,height=$height,scrollbars=yes,status=no'); " .
490 $javascript .= $text;
492 if ($count == true) {
493 $javascript .= " ($counts)";
496 $javascript .="</a>\n\n";
502 * function get_links_list()
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.
511 * order (default 'name') - Sort link categories by 'name' or 'id'
512 * hide_if_empty (default true) - Supress listing empty link categories
514 function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
517 $order = strtolower($order);
519 // Handle link category sorting
520 if (substr($order,0,1) == '_') {
521 $direction = ' DESC';
522 $order = substr($order,1);
525 // if 'name' wasn't specified, assume 'id':
526 $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
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
535 LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
536 WHERE link_visible = 'Y'
538 ORDER BY $cat_order $direction ", ARRAY_A);
540 // Display each category
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;
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'],
553 bool_from_yn($cat['show_images']),
555 bool_from_yn($cat['show_description']),
556 bool_from_yn($cat['show_rating']),
558 bool_from_yn($cat['show_updated']));
560 // Close the last category
561 echo "\n\t</ul>\n</li>\n";