Log in with username/password: lyceum/lyceum

root/trunk/src/lyceum/wp-includes/links.php

Revision 1011, 20.1 kB (checked in by jjb, 3 years ago)

missing global $blog

#753

thanks to Michael Harmer

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