]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/bookmark-template.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / bookmark-template.php
1 <?php
2 /**
3  * Bookmark Template Functions for usage in Themes
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * _walk_bookmarks() - The formatted output of a list of bookmarks
11  *
12  * The $bookmarks array must contain bookmark objects and will be iterated over
13  * to retrieve the bookmark to be used in the output.
14  *
15  * The output is formatted as HTML with no way to change that format. However, what
16  * is between, before, and after can be changed. The link itself will be HTML.
17  *
18  * This function is used internally by wp_list_bookmarks() and should not be used by
19  * themes.
20  *
21  * The defaults for overwriting are:
22  * 'show_updated' - Default is 0 (integer). Will show the time of when the bookmark was last updated.
23  * 'show_description' - Default is 0 (integer). Whether to show the description of the bookmark.
24  * 'show_images' - Default is 1 (integer). Whether to show link image if available.
25  * 'before' - Default is '<li>' (string). The html or text to prepend to each bookmarks.
26  * 'after' - Default is '</li>' (string). The html or text to append to each bookmarks.
27  * 'between' - Default is '\n' (string). The string for use in between the link, description, and image.
28  * 'show_rating' - Default is 0 (integer). Whether to show the link rating.
29  *
30  * @since 2.1
31  * @access private
32  * @usedby wp_list_bookmarks()
33  *
34  * @param array $bookmarks List of bookmarks to traverse
35  * @param string|array $args Optional. Overwrite the defaults.
36  * @return string Formatted output in HTML
37  */
38 function _walk_bookmarks($bookmarks, $args = '' ) {
39         $defaults = array(
40                 'show_updated' => 0, 'show_description' => 0,
41                 'show_images' => 1, 'before' => '<li>',
42                 'after' => '</li>', 'between' => "\n",
43                 'show_rating' => 0
44         );
45
46         $r = wp_parse_args( $args, $defaults );
47         extract( $r, EXTR_SKIP );
48
49         $output = ''; // Blank string to start with.
50
51         foreach ( (array) $bookmarks as $bookmark ) {
52                 if ( !isset($bookmark->recently_updated) )
53                         $bookmark->recently_updated = false;
54                 $output .= $before;
55                 if ( $show_updated && $bookmark->recently_updated )
56                         $output .= get_option('links_recently_updated_prepend');
57
58                 $the_link = '#';
59                 if ( !empty($bookmark->link_url) )
60                         $the_link = clean_url($bookmark->link_url);
61
62                 $rel = $bookmark->link_rel;
63                 if ( '' != $rel )
64                         $rel = ' rel="' . $rel . '"';
65
66                 $desc = attribute_escape(sanitize_bookmark_field('link_description', $bookmark->link_description, $bookmark->link_id, 'display'));
67                 $name = attribute_escape(sanitize_bookmark_field('link_name', $bookmark->link_name, $bookmark->link_id, 'display'));
68                 $title = $desc;
69
70                 if ( $show_updated )
71                         if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
72                                 $title .= ' ';
73                                 $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * 3600)));
74                                 $title .= ')';
75                         }
76
77                 if ( '' != $title )
78                         $title = ' title="' . $title . '"';
79
80                 $alt = ' alt="' . $name . '"';
81
82                 $target = $bookmark->link_target;
83                 if ( '' != $target )
84                         $target = ' target="' . $target . '"';
85
86                 $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
87
88                 if ( $bookmark->link_image != null && $show_images ) {
89                         if ( strpos($bookmark->link_image, 'http') !== false )
90                                 $output .= "<img src=\"$bookmark->link_image\" $alt $title />";
91                         else // If it's a relative path
92                                 $output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
93                 } else {
94                         $output .= $name;
95                 }
96
97                 $output .= '</a>';
98
99                 if ( $show_updated && $bookmark->recently_updated )
100                         $output .= get_option('links_recently_updated_append');
101
102                 if ( $show_description && '' != $desc )
103                         $output .= $between . $desc;
104
105                 if ($show_rating) {
106                         $output .= $between . get_linkrating($bookmark);
107                 }
108
109                 $output .= "$after\n";
110         } // end while
111
112         return $output;
113 }
114
115 /**
116  * wp_list_bookmarks() - Retrieve or echo all of the bookmarks
117  *
118  * List of default arguments are as follows:
119  * 'orderby' - Default is 'name' (string). How to order the links by. String is based off of the bookmark scheme.
120  * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either ascending or descending order.
121  * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to display.
122  * 'category' - Default is empty string (string). Include the links in what category ID(s).
123  * 'category_name' - Default is empty string (string). Get links by category name.
124  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide links marked as 'invisible'.
125  * 'show_updated' - Default is 0 (integer). Will show the time of when the bookmark was last updated.
126  * 'echo' - Default is 1 (integer). Whether to echo (default) or return the formatted bookmarks.
127  * 'categorize' - Default is 1 (integer). Whether to show links listed by category (default) or show links in one column.
128  *
129  * These options define how the Category name will appear before the category links are displayed, if 'categorize' is 1.
130  * If 'categorize' is 0, then it will display for only the 'title_li' string and only if 'title_li' is not empty.
131  * 'title_li' - Default is 'Bookmarks' (translatable string). What to show before the links appear.
132  * 'title_before' - Default is '<h2>' (string). The HTML or text to show before the 'title_li' string.
133  * 'title_after' - Default is '</h2>' (string). The HTML or text to show after the 'title_li' string.
134  * 'class' - Default is 'linkcat' (string). The CSS class to use for the 'title_li'.
135  *
136  * 'category_before' - Default is '<li id="%id" class="%class">'. String must contain '%id' and '%class' to get
137  * the id of the category and the 'class' argument. These are used for formatting in themes. Argument will be displayed
138  * before the 'title_before' argument.
139  * 'category_after' - Default is '</li>' (string). The HTML or text that will appear after the list of links.
140  *
141  * These are only used if 'categorize' is set to 1 or true.
142  * 'category_orderby' - Default is 'name'. How to order the bookmark category based on term scheme.
143  * 'category_order' - Default is 'ASC'. Set the order by either ASC (ascending) or DESC (descending).
144  *
145  * @see _walk_bookmarks() For other arguments that can be set in this function and passed to _walk_bookmarks().
146  * @see get_bookmarks() For other arguments that can be set in this function and passed to get_bookmarks().
147  *
148  * @since 2.1
149  * @uses _list_bookmarks() Used to iterate over all of the bookmarks and return the html
150  * @uses get_terms() Gets all of the categories that are for links.
151  *
152  * @param string|array $args Optional. Overwrite the defaults of the function
153  * @return string|null Will only return if echo option is set to not echo. Default is not return anything.
154  */
155 function wp_list_bookmarks($args = '') {
156         $defaults = array(
157                 'orderby' => 'name', 'order' => 'ASC',
158                 'limit' => -1, 'category' => '',
159                 'category_name' => '', 'hide_invisible' => 1,
160                 'show_updated' => 0, 'echo' => 1,
161                 'categorize' => 1, 'title_li' => __('Bookmarks'),
162                 'title_before' => '<h2>', 'title_after' => '</h2>',
163                 'category_orderby' => 'name', 'category_order' => 'ASC',
164                 'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
165                 'category_after' => '</li>'
166         );
167
168         $r = wp_parse_args( $args, $defaults );
169         extract( $r, EXTR_SKIP );
170
171         $output = '';
172
173         if ( $categorize ) {
174                 //Split the bookmarks into ul's for each category
175                 $cats = get_terms('link_category', "category_name=$category_name&include=$category&orderby=$category_orderby&order=$category_order&hierarchical=0");
176
177                 foreach ( (array) $cats as $cat ) {
178                         $params = array_merge($r, array('category'=>$cat->term_id));
179                         $bookmarks = get_bookmarks($params);
180                         if ( empty($bookmarks) )
181                                 continue;
182                         $output .= str_replace(array('%id', '%class'), array("linkcat-$cat->term_id", $class), $category_before);
183                         $catname = apply_filters( "link_category", $cat->name );
184                         $output .= "$title_before$catname$title_after\n\t<ul>\n";
185                         $output .= _walk_bookmarks($bookmarks, $r);
186                         $output .= "\n\t</ul>\n$category_after\n";
187                 }
188         } else {
189                 //output one single list using title_li for the title
190                 $bookmarks = get_bookmarks($r);
191
192                 if ( !empty($bookmarks) ) {
193                         if ( !empty( $title_li ) ){
194                                 $output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
195                                 $output .= "$title_before$title_li$title_after\n\t<ul>\n";
196                                 $output .= _walk_bookmarks($bookmarks, $r);
197                                 $output .= "\n\t</ul>\n$category_after\n";
198                         } else {
199                                 $output .= _walk_bookmarks($bookmarks, $r);
200                         }
201                 }
202         }
203
204         $output = apply_filters( 'wp_list_bookmarks', $output );
205
206         if ( !$echo )
207                 return $output;
208         echo $output;
209 }
210
211 ?>