Ticket #818: links.php

File links.php, 19.1 KB (added by anonymousbugger, 7 years ago)
Line 
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 */
25function 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
41function 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 **/
51function wp_get_linksbyname($category) {
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    if ($cat) {
58        if ($cat->sort_desc == 'Y') {
59            $cat->sort_order = '_'.$cat->sort_order;
60        }
61        get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
62                  $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
63                   bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
64                   $cat->list_limit, bool_from_yn($cat->show_updated));
65    }
66} // end wp_get_linksbyname
67
68/** function wp_get_links()
69 ** Gets the links associated with category n.
70 ** Parameters:
71 **   category (no default)  - The category to use.
72 **/
73function wp_get_links($category) {
74    global $wpdb;
75
76    $cat = $wpdb->get_row("SELECT cat_id, cat_name, auto_toggle, show_images, show_description, "
77         . " show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, "
78         . " text_after_all, list_limit FROM $wpdb->linkcategories WHERE cat_id=$category");
79    if ($cat) {
80        if ($cat->sort_desc == 'Y') {
81            $cat->sort_order = '_'.$cat->sort_order;
82        }
83        get_links($cat->cat_id, $cat->text_before_link, $cat->text_after_all,
84                  $cat->text_after_link, bool_from_yn($cat->show_images), $cat->sort_order,
85                   bool_from_yn($cat->show_description), bool_from_yn($cat->show_rating),
86                   $cat->list_limit, bool_from_yn($cat->show_updated));
87    }
88} // end wp_get_links
89
90/** function get_links()
91 ** Gets the links associated with category n.
92 ** Parameters:
93 **   category (default -1)  - The category to use. If no category supplied
94 **      uses all
95 **   before (default '')  - the html to output before the link
96 **   after (default '<br />')  - the html to output after the link
97 **   between (default ' ')  - the html to output between the link/image
98 **     and it's description. Not used if no image or show_images == true
99 **   show_images (default true) - whether to show images (if defined).
100 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
101 **     'url', 'description', or 'rating'. Or maybe owner. If you start the
102 **     name with an underscore the order will be reversed.
103 **     You can also specify 'rand' as the order which will return links in a
104 **     random order.
105 **   show_description (default true) - whether to show the description if
106 **    show_images=false/not defined .
107 **   show_rating (default false) - show rating stars/chars
108 **   limit (default -1) - Limit to X entries. If not specified, all entries
109 **     are shown.
110 **   show_updated (default 0) - whether to show last updated timestamp
111 */
112function get_links($category = -1, $before = '', $after = '<br />',
113                   $between = ' ', $show_images = true, $orderby = 'name',
114                   $show_description = true, $show_rating = false,
115                   $limit = -1, $show_updated = 1, $echo = true) {
116
117    global $wpdb;
118
119    $direction = ' ASC';
120    $category_query = "";
121    if ($category != -1) {
122        $category_query = " AND link_category = $category ";
123    }
124    if (get_settings('links_recently_updated_time')) {
125        $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL ".get_settings('links_recently_updated_time')." MINUTE) >= NOW(), 1,0) as recently_updated ";
126    } else {
127                $recently_updated_test = '';
128        }
129    if ($show_updated) {
130        $get_updated = ", UNIX_TIMESTAMP(link_updated) AS link_updated_f ";
131    }
132
133    $orderby=strtolower($orderby);
134    if ($orderby == '')
135        $orderby = 'id';
136    if (substr($orderby,0,1) == '_') {
137        $direction = ' DESC';
138        $orderby = substr($orderby,1);
139    }
140
141    switch($orderby) {
142        case 'length':
143        $length = ",CHAR_LENGTH(link_name) AS length";
144        break;
145        case 'rand':
146            $orderby = 'rand()';
147            break;
148        default:
149            $orderby = " link_" . $orderby;
150    }
151
152    if (!isset($length)) {
153                $length = "";
154        }
155
156    $sql = "SELECT link_url, link_name, link_image, link_target,
157            link_description, link_rating, link_rel $length $recently_updated_test $get_updated
158            FROM $wpdb->links
159            WHERE link_visible = 'Y' " .
160           $category_query;
161    $sql .= ' ORDER BY ' . $orderby;
162    $sql .= $direction;
163    /* The next 2 lines implement LIMIT TO processing */
164    if ($limit != -1)
165        $sql .= " LIMIT $limit";
166    //echo $sql;
167    $results = $wpdb->get_results($sql);
168    if (!$results) {
169        return;
170    }
171    foreach ($results as $row) {
172                if (!isset($row->recently_updated)) $row->recently_updated = false;
173        echo($before);
174        if ($show_updated && $row->recently_updated) {
175            echo get_settings('links_recently_updated_prepend');
176        }
177        $the_link = '#';
178        if (($row->link_url != null) && ($row->link_url != '')) {
179            $the_link = wp_specialchars($row->link_url);
180        }
181        $rel = $row->link_rel;
182        if ($rel != '') {
183            $rel = " rel='$rel'";
184        }
185        $desc = wp_specialchars($row->link_description, ENT_QUOTES);
186        $name = wp_specialchars($row->link_name, ENT_QUOTES);
187
188        $title = $desc;
189
190        if ($show_updated) {
191           if (substr($row->link_updated_f,0,2) != '00') {
192                $title .= ' (Last updated ' . date(get_settings('links_updated_date_format'), $row->link_updated_f + (get_settings('gmt_offset') * 3600)) .')';
193            }
194        }
195
196        if ('' != $title) {
197            $title = " title='$title'";
198        }
199
200        $alt = " alt='$name'";
201           
202        $target = $row->link_target;
203        if ('' != $target) {
204            $target = " target='$target'";
205        }
206        echo("<a href='$the_link'");
207        echo($rel . $title . $target);
208        echo('>');
209        if (($row->link_image != null) && $show_images) {
210                        if (strstr($row->link_image, 'http'))
211                                echo "<img src='$row->link_image' $alt $title />";
212                        else // If it's a relative path
213                echo "<img src='" . get_settings('siteurl') . "$row->link_image' $alt $title />";
214        } else {
215            echo($name);
216        }
217        echo('</a>');
218        if ($show_updated && $row->recently_updated) {
219            echo get_settings('links_recently_updated_append');
220        }
221
222        if ($show_description && ($desc != '')) {
223            echo($between.$desc);
224        }
225        echo("$after\n");
226    } // end while
227}
228
229
230/** function get_linkobjectsbyname()
231 ** Gets an array of link objects associated with category 'cat_name'.
232 ** Parameters:
233 **   cat_name (default 'noname')  - The category name to use. If no
234 **     match is found uses all
235 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
236 **     'url', 'description', or 'rating'. Or maybe owner. If you start the
237 **     name with an underscore the order will be reversed.
238 **     You can also specify 'rand' as the order which will return links in a
239 **     random order.
240 **   limit (default -1) - Limit to X entries. If not specified, all entries
241 **     are shown.
242 **
243 ** Use this like:
244 ** $links = get_linkobjectsbyname('fred');
245 ** foreach ($links as $link) {
246 **   echo '<li>'.$link->link_name.'</li>';
247 ** }
248 **/
249function get_linkobjectsbyname($cat_name = "noname" , $orderby = 'name', $limit = -1) {
250    global $wpdb;
251    $cat_id = -1;
252    $results = $wpdb->get_results("SELECT cat_id FROM $wpdb->linkcategories WHERE cat_name='$cat_name'");
253    if ($results) {
254        foreach ($results as $result) {
255            $cat_id = $result->cat_id;
256        }
257    }
258    return get_linkobjects($cat_id, $orderby, $limit);
259}
260
261/** function get_linkobjects()
262 ** Gets an array of link objects associated with category n.
263 ** Parameters:
264 **   category (default -1)  - The category to use. If no category supplied
265 **      uses all
266 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
267 **     'url', 'description', or 'rating'. Or maybe owner. If you start the
268 **     name with an underscore the order will be reversed.
269 **     You can also specify 'rand' as the order which will return links in a
270 **     random order.
271 **   limit (default -1) - Limit to X entries. If not specified, all entries
272 **     are shown.
273 **
274 ** Use this like:
275 ** $links = get_linkobjects(1);
276 ** if ($links) {
277 **   foreach ($links as $link) {
278 **     echo '<li>'.$link->link_name.'<br />'.$link->link_description.'</li>';
279 **   }
280 ** }
281 ** Fields are:
282 ** link_id
283 ** link_url
284 ** link_name
285 ** link_image
286 ** link_target
287 ** link_category
288 ** link_description
289 ** link_visible
290 ** link_owner
291 ** link_rating
292 ** link_updated
293 ** link_rel
294 ** link_notes
295 **/
296function get_linkobjects($category = -1, $orderby = 'name', $limit = -1) {
297    global $wpdb;
298
299    $sql = "SELECT * FROM $wpdb->links WHERE link_visible = 'Y'";
300    if ($category != -1) {
301        $sql .= " AND link_category = $category ";
302    }
303    if ($orderby == '')
304        $orderby = 'id';
305    if (substr($orderby,0,1) == '_') {
306        $direction = ' DESC';
307        $orderby = substr($orderby,1);
308    }
309    if (strcasecmp('rand',$orderby) == 0) {
310        $orderby = 'rand()';
311    } else {
312        $orderby = " link_" . $orderby;
313    }
314    $sql .= ' ORDER BY ' . $orderby;
315    $sql .= $direction;
316    /* The next 2 lines implement LIMIT TO processing */
317    if ($limit != -1)
318        $sql .= " LIMIT $limit";
319
320    $results = $wpdb->get_results($sql);
321    if ($results) {
322        foreach ($results as $result) {
323            $result->link_url         = $result->link_url;
324            $result->link_name        = $result->link_name;
325            $result->link_description = $result->link_description;
326            $result->link_notes       = $result->link_notes;
327            $newresults[] = $result;
328        }
329    }
330    return $newresults;
331}
332
333function get_linkrating($link) {
334    return apply_filters('link_rating', $link->link_rating);
335}
336
337
338/** function get_linksbyname_withrating()
339 ** Gets the links associated with category 'cat_name' and display rating stars/chars.
340 ** Parameters:
341 **   cat_name (default 'noname')  - The category name to use. If no
342 **     match is found uses all
343 **   before (default '')  - the html to output before the link
344 **   after (default '<br />')  - the html to output after the link
345 **   between (default ' ')  - the html to output between the link/image
346 **     and it's description. Not used if no image or show_images == true
347 **   show_images (default true) - whether to show images (if defined).
348 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
349 **     'url' or 'description'. Or maybe owner. If you start the
350 **     name with an underscore the order will be reversed.
351 **     You can also specify 'rand' as the order which will return links in a
352 **     random order.
353 **   show_description (default true) - whether to show the description if
354 **     show_images=false/not defined
355 **   limit (default -1) - Limit to X entries. If not specified, all entries
356 **     are shown.
357 **   show_updated (default 0) - whether to show last updated timestamp
358 */
359function get_linksbyname_withrating($cat_name = "noname", $before = '',
360                                    $after = '<br />', $between = " ",
361                                    $show_images = true, $orderby = 'id',
362                                    $show_description = true, $limit = -1, $show_updated = 0) {
363
364    get_linksbyname($cat_name, $before, $after, $between, $show_images,
365                    $orderby, $show_description, true, $limit, $show_updated);
366}
367
368/** function get_links_withrating()
369 ** Gets the links associated with category n and display rating stars/chars.
370 ** Parameters:
371 **   category (default -1)  - The category to use. If no category supplied
372 **      uses all
373 **   before (default '')  - the html to output before the link
374 **   after (default '<br />')  - the html to output after the link
375 **   between (default ' ')  - the html to output between the link/image
376 **     and it's description. Not used if no image or show_images == true
377 **   show_images (default true) - whether to show images (if defined).
378 **   orderby (default 'id') - the order to output the links. E.g. 'id', 'name',
379 **     'url' or 'description'. Or maybe owner. If you start the
380 **     name with an underscore the order will be reversed.
381 **     You can also specify 'rand' as the order which will return links in a
382 **     random order.
383 **   show_description (default true) - whether to show the description if
384 **    show_images=false/not defined .
385 **   limit (default -1) - Limit to X entries. If not specified, all entries
386 **     are shown.
387 **   show_updated (default 0) - whether to show last updated timestamp
388 */
389function get_links_withrating($category = -1, $before = '', $after = '<br />',
390                              $between = " ", $show_images = true,
391                              $orderby = 'id', $show_description = true,
392                              $limit = -1, $show_updated = 0) {
393
394    get_links($category, $before, $after, $between, $show_images, $orderby,
395              $show_description, true, $limit, $show_updated);
396}
397
398/** function get_linkcatname()
399 ** Gets the name of category n.
400 ** Parameters: id (default 0)  - The category to get. If no category supplied
401 **                uses 0
402 */
403function get_linkcatname($id = 0) {
404    global $wpdb;
405    $cat_name = '';
406    if ('' != $id) {
407        $cat_name = $wpdb->get_var("SELECT cat_name FROM $wpdb->linkcategories WHERE cat_id=$id");
408    }
409    return $cat_name;
410}
411
412/** function get_get_autotoggle()
413 ** Gets the auto_toggle setting of category n.
414 ** Parameters: id (default 0)  - The category to get. If no category supplied
415 **                uses 0
416 */
417function get_autotoggle($id = 0) {
418    global $wpdb;
419    $auto_toggle = $wpdb->get_var("SELECT auto_toggle FROM $wpdb->linkcategories WHERE cat_id=$id");
420    if ('' == $auto_toggle)
421        $auto_toggle = 'N';
422    return $auto_toggle;
423}
424
425/** function links_popup_script()
426 ** This function contributed by Fullo -- http://sprite.csr.unibo.it/fullo/
427 ** Show the link to the links popup and the number of links
428 ** Parameters:
429 **   text (default Links)  - the text of the link
430 **   width (default 400)  - the width of the popup window
431 **   height (default 400)  - the height of the popup window
432 **   file (default linkspopup.php) - the page to open in the popup window
433 **   count (default true) - the number of links in the db
434 */
435function links_popup_script($text = 'Links', $width=400, $height=400,
436                            $file='links.all.php', $count = true) {
437   if ($count == true) {
438      $counts = $wpdb->get_var("SELECT count(*) FROM $wpdb->links");
439   }
440
441   $javascript = "<a href=\"#\" " .
442                 " onclick=\"javascript:window.open('$file?popup=1', '_blank', " .
443                 "'width=$width,height=$height,scrollbars=yes,status=no'); " .
444                 " return false\">";
445   $javascript .= $text;
446
447   if ($count == true) {
448      $javascript .= " ($counts)";
449   }
450
451   $javascript .="</a>\n\n";
452   echo $javascript;
453}
454
455
456/*
457 * function get_links_list()
458 *
459 * added by Dougal
460 *
461 * Output a list of all links, listed by category, using the
462 * settings in $wpdb->linkcategories and output it as a nested
463 * HTML unordered list.
464 *
465 * Parameters:
466 *   order (default 'name')  - Sort link categories by 'name' or 'id'
467 *   hide_if_empty (default true)  - Supress listing empty link categories
468 */
469function get_links_list($order = 'name', $hide_if_empty = 'obsolete') {
470        global $wpdb;
471
472        $order = strtolower($order);
473
474        // Handle link category sorting
475        if (substr($order,0,1) == '_') {
476                $direction = ' DESC';
477                $order = substr($order,1);
478        }
479
480        // if 'name' wasn't specified, assume 'id':
481        $cat_order = ('name' == $order) ? 'cat_name' : 'cat_id';
482
483        if (!isset($direction)) $direction = '';
484        // Fetch the link category data as an array of hashesa
485        $cats = $wpdb->get_results("
486                SELECT DISTINCT link_category, cat_name, show_images,
487                        show_description, show_rating, show_updated, sort_order,
488                        sort_desc, list_limit
489                FROM `$wpdb->links`
490                LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
491                WHERE link_visible =  'Y'
492                        AND list_limit <> 0
493                ORDER BY $cat_order $direction ", ARRAY_A);
494
495        // Display each category
496        if ($cats) {
497                foreach ($cats as $cat) {
498                        // Handle each category.
499                        // First, fix the sort_order info
500                        $orderby = $cat['sort_order'];
501                        $orderby = (bool_from_yn($cat['sort_desc'])?'_':'') . $orderby;
502
503                        // Display the category name
504                        echo '  <li id="'.sanitize_title($cat['cat_name']).'"><h2>' . $cat['cat_name'] . "</h2>\n\t<ul>\n";
505                        // Call get_links() with all the appropriate params
506                        get_links($cat['link_category'],
507                                '<li>',"</li>","\n",
508                                bool_from_yn($cat['show_images']),
509                                $orderby,
510                                bool_from_yn($cat['show_description']),
511                                bool_from_yn($cat['show_rating']),
512                                $cat['list_limit'],
513                                bool_from_yn($cat['show_updated']));
514
515                        // Close the last category
516                        echo "\n\t</ul>\n</li>\n";
517                }
518        }
519}
520
521?>