Log in with username/password: lyceum/lyceum

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

Revision 1006, 20.0 kB (checked in by jjb, 3 years ago)

Synchronizing with WordPress 2.0.6

http://source.ibiblio.org/trac/lyceum/milestone/0.46

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