]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/bookmark-template.php
WordPress 3.8.2
[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  * 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,
16  * what is between, before, and after can be changed. The link itself will be
17  * HTML.
18  *
19  * This function is used internally by wp_list_bookmarks() and should not be
20  * used by themes.
21  *
22  * The defaults for overwriting are:
23  * 'show_updated' - Default is 0 (integer). Will show the time of when the
24  *              bookmark was last updated.
25  * 'show_description' - Default is 0 (integer). Whether to show the description
26  *              of the bookmark.
27  * 'show_images' - Default is 1 (integer). Whether to show link image if
28  *              available.
29  * 'show_name' - Default is 0 (integer). Whether to show link name if
30  *              available.
31  * 'before' - Default is '<li>' (string). The html or text to prepend to each
32  *              bookmarks.
33  * 'after' - Default is '</li>' (string). The html or text to append to each
34  *              bookmarks.
35  * 'link_before' - Default is '' (string). The html or text to prepend to each
36  *              bookmarks inside the <a> tag.
37  * 'link_after' - Default is '' (string). The html or text to append to each
38  *              bookmarks inside the <a> tag.
39  * 'between' - Default is '\n' (string). The string for use in between the link,
40  *              description, and image.
41  * 'show_rating' - Default is 0 (integer). Whether to show the link rating.
42  *
43  * @since 2.1.0
44  * @access private
45  *
46  * @param array $bookmarks List of bookmarks to traverse
47  * @param string|array $args Optional. Overwrite the defaults.
48  * @return string Formatted output in HTML
49  */
50 function _walk_bookmarks($bookmarks, $args = '' ) {
51         $defaults = array(
52                 'show_updated' => 0, 'show_description' => 0,
53                 'show_images' => 1, 'show_name' => 0,
54                 'before' => '<li>', 'after' => '</li>', 'between' => "\n",
55                 'show_rating' => 0, 'link_before' => '', 'link_after' => ''
56         );
57
58         $r = wp_parse_args( $args, $defaults );
59         extract( $r, EXTR_SKIP );
60
61         $output = ''; // Blank string to start with.
62
63         foreach ( (array) $bookmarks as $bookmark ) {
64                 if ( !isset($bookmark->recently_updated) )
65                         $bookmark->recently_updated = false;
66                 $output .= $before;
67                 if ( $show_updated && $bookmark->recently_updated )
68                         $output .= get_option('links_recently_updated_prepend');
69
70                 $the_link = '#';
71                 if ( !empty($bookmark->link_url) )
72                         $the_link = esc_url($bookmark->link_url);
73
74                 $desc = esc_attr(sanitize_bookmark_field('link_description', $bookmark->link_description, $bookmark->link_id, 'display'));
75                 $name = esc_attr(sanitize_bookmark_field('link_name', $bookmark->link_name, $bookmark->link_id, 'display'));
76                 $title = $desc;
77
78                 if ( $show_updated )
79                         if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
80                                 $title .= ' (';
81                                 $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)));
82                                 $title .= ')';
83                         }
84
85                 $alt = ' alt="' . $name . ( $show_description ? ' ' . $title : '' ) . '"';
86
87                 if ( '' != $title )
88                         $title = ' title="' . $title . '"';
89
90                 $rel = $bookmark->link_rel;
91                 if ( '' != $rel )
92                         $rel = ' rel="' . esc_attr($rel) . '"';
93
94                 $target = $bookmark->link_target;
95                 if ( '' != $target )
96                         $target = ' target="' . $target . '"';
97
98                 $output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
99
100                 $output .= $link_before;
101
102                 if ( $bookmark->link_image != null && $show_images ) {
103                         if ( strpos($bookmark->link_image, 'http') === 0 )
104                                 $output .= "<img src=\"$bookmark->link_image\" $alt $title />";
105                         else // If it's a relative path
106                                 $output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
107
108                         if ( $show_name )
109                                 $output .= " $name";
110                 } else {
111                         $output .= $name;
112                 }
113
114                 $output .= $link_after;
115
116                 $output .= '</a>';
117
118                 if ( $show_updated && $bookmark->recently_updated )
119                         $output .= get_option('links_recently_updated_append');
120
121                 if ( $show_description && '' != $desc )
122                         $output .= $between . $desc;
123
124                 if ( $show_rating )
125                         $output .= $between . sanitize_bookmark_field('link_rating', $bookmark->link_rating, $bookmark->link_id, 'display');
126
127                 $output .= "$after\n";
128         } // end while
129
130         return $output;
131 }
132
133 /**
134  * Retrieve or echo all of the bookmarks.
135  *
136  * List of default arguments are as follows:
137  * 'orderby' - Default is 'name' (string). How to order the links by. String is
138  *              based off of the bookmark scheme.
139  * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
140  *              ascending or descending order.
141  * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
142  *              display.
143  * 'category' - Default is empty string (string). Include the links in what
144  *              category ID(s).
145  * 'category_name' - Default is empty string (string). Get links by category
146  *              name.
147  * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
148  *              links marked as 'invisible'.
149  * 'show_updated' - Default is 0 (integer). Will show the time of when the
150  *              bookmark was last updated.
151  * 'echo' - Default is 1 (integer). Whether to echo (default) or return the
152  *              formatted bookmarks.
153  * 'categorize' - Default is 1 (integer). Whether to show links listed by
154  *              category (default) or show links in one column.
155  * 'show_description' - Default is 0 (integer). Whether to show the description
156  *              of the bookmark.
157  *
158  * These options define how the Category name will appear before the category
159  * links are displayed, if 'categorize' is 1. If 'categorize' is 0, then it will
160  * display for only the 'title_li' string and only if 'title_li' is not empty.
161  * 'title_li' - Default is 'Bookmarks' (translatable string). What to show
162  *              before the links appear.
163  * 'title_before' - Default is '<h2>' (string). The HTML or text to show before
164  *              the 'title_li' string.
165  * 'title_after' - Default is '</h2>' (string). The HTML or text to show after
166  *              the 'title_li' string.
167  * 'class' - Default is 'linkcat' (string). The CSS class to use for the
168  *              'title_li'.
169  *
170  * 'category_before' - Default is '<li id="%id" class="%class">'. String must
171  *              contain '%id' and '%class' to get
172  * the id of the category and the 'class' argument. These are used for
173  *              formatting in themes.
174  * Argument will be displayed before the 'title_before' argument.
175  * 'category_after' - Default is '</li>' (string). The HTML or text that will
176  *              appear after the list of links.
177  *
178  * These are only used if 'categorize' is set to 1 or true.
179  * 'category_orderby' - Default is 'name'. How to order the bookmark category
180  *              based on term scheme.
181  * 'category_order' - Default is 'ASC'. Set the order by either ASC (ascending)
182  *              or DESC (descending).
183  *
184  * @see _walk_bookmarks() For other arguments that can be set in this function
185  *              and passed to _walk_bookmarks().
186  * @see get_bookmarks() For other arguments that can be set in this function and
187  *              passed to get_bookmarks().
188  * @link http://codex.wordpress.org/Template_Tags/wp_list_bookmarks
189  *
190  * @since 2.1.0
191  * @uses _walk_bookmarks() Used to iterate over all of the bookmarks and return
192  *              the html
193  * @uses get_terms() Gets all of the categories that are for links.
194  *
195  * @param string|array $args Optional. Overwrite the defaults of the function
196  * @return string|null Will only return if echo option is set to not echo.
197  *              Default is not return anything.
198  */
199 function wp_list_bookmarks($args = '') {
200         $defaults = array(
201                 'orderby' => 'name', 'order' => 'ASC',
202                 'limit' => -1, 'category' => '', 'exclude_category' => '',
203                 'category_name' => '', 'hide_invisible' => 1,
204                 'show_updated' => 0, 'echo' => 1,
205                 'categorize' => 1, 'title_li' => __('Bookmarks'),
206                 'title_before' => '<h2>', 'title_after' => '</h2>',
207                 'category_orderby' => 'name', 'category_order' => 'ASC',
208                 'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
209                 'category_after' => '</li>'
210         );
211
212         $r = wp_parse_args( $args, $defaults );
213         extract( $r, EXTR_SKIP );
214
215         $output = '';
216
217         if ( $categorize ) {
218                 $cats = get_terms( 'link_category', array( 'name__like' => $category_name, 'include' => $category, 'exclude' => $exclude_category, 'orderby' => $category_orderby, 'order' => $category_order, 'hierarchical' => 0 ) );
219                 if ( empty( $cats ) )
220                         $categorize = false;
221         }
222
223         if ( $categorize ) {
224                 // Split the bookmarks into ul's for each category
225                 foreach ( (array) $cats as $cat ) {
226                         $params = array_merge($r, array('category'=>$cat->term_id));
227                         $bookmarks = get_bookmarks($params);
228                         if ( empty($bookmarks) )
229                                 continue;
230                         $output .= str_replace(array('%id', '%class'), array("linkcat-$cat->term_id", $class), $category_before);
231                         /**
232                          * Filter the bookmarks category name.
233                          *
234                          * @since 2.2.0
235                          *
236                          * @param string $cat->name The category name of bookmarks.
237                          */
238                         $catname = apply_filters( 'link_category', $cat->name );
239
240                         $output .= "$title_before$catname$title_after\n\t<ul class='xoxo blogroll'>\n";
241                         $output .= _walk_bookmarks($bookmarks, $r);
242                         $output .= "\n\t</ul>\n$category_after\n";
243                 }
244         } else {
245                 //output one single list using title_li for the title
246                 $bookmarks = get_bookmarks($r);
247
248                 if ( !empty($bookmarks) ) {
249                         if ( !empty( $title_li ) ){
250                                 $output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
251                                 $output .= "$title_before$title_li$title_after\n\t<ul class='xoxo blogroll'>\n";
252                                 $output .= _walk_bookmarks($bookmarks, $r);
253                                 $output .= "\n\t</ul>\n$category_after\n";
254                         } else {
255                                 $output .= _walk_bookmarks($bookmarks, $r);
256                         }
257                 }
258         }
259
260         /**
261          * Filter the bookmarks list before it is echoed or returned.
262          *
263          * @since 2.5.0
264          *
265          * @param string $output The HTML list of bookmarks.
266          */
267         $output = apply_filters( 'wp_list_bookmarks', $output );
268
269         if ( !$echo )
270                 return $output;
271         echo $output;
272 }