author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 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 |
* @since 2.1.0 |
|
23 |
* @access private |
|
24 |
* |
|
5 | 25 |
* @param array $bookmarks List of bookmarks to traverse. |
26 |
* @param string|array $args { |
|
27 |
* Optional. Bookmarks arguments. |
|
28 |
* |
|
29 |
* @type int|bool $show_updated Whether to show the time the bookmark was last updated. |
|
30 |
* Accepts 1|true or 0|false. Default 0|false. |
|
31 |
* @type int|bool $show_description Whether to show the bookmakr description. Accepts 1|true, |
|
32 |
* Accepts 1|true or 0|false. Default 0|false. |
|
33 |
* @type int|bool $show_images Whether to show the link image if available. Accepts 1|true |
|
34 |
* or 0|false. Default 1|true. |
|
35 |
* @type int|bool $show_name Whether to show link name if available. Accepts 1|true or |
|
36 |
* 0|false. Default 0|false. |
|
37 |
* @type string $before The HTML or text to prepend to each bookmark. Default `<li>`. |
|
38 |
* @type string $after The HTML or text to append to each bookmark. Default `</li>`. |
|
39 |
* @type string $link_before The HTML or text to prepend to each bookmark inside the anchor |
|
40 |
* tags. Default empty. |
|
41 |
* @type string $link_after The HTML or text to append to each bookmark inside the anchor |
|
42 |
* tags. Default empty. |
|
43 |
* @type string $between The string for use in between the link, description, and image. |
|
44 |
* Default "\n". |
|
45 |
* @type int|bool $show_rating Whether to show the link rating. Accepts 1|true or 0|false. |
|
46 |
* Default 0|false. |
|
47 |
* |
|
48 |
* } |
|
0 | 49 |
* @return string Formatted output in HTML |
50 |
*/ |
|
5 | 51 |
function _walk_bookmarks( $bookmarks, $args = '' ) { |
0 | 52 |
$defaults = array( |
53 |
'show_updated' => 0, 'show_description' => 0, |
|
54 |
'show_images' => 1, 'show_name' => 0, |
|
55 |
'before' => '<li>', 'after' => '</li>', 'between' => "\n", |
|
56 |
'show_rating' => 0, 'link_before' => '', 'link_after' => '' |
|
57 |
); |
|
58 |
||
59 |
$r = wp_parse_args( $args, $defaults ); |
|
60 |
||
61 |
$output = ''; // Blank string to start with. |
|
62 |
||
63 |
foreach ( (array) $bookmarks as $bookmark ) { |
|
5 | 64 |
if ( ! isset( $bookmark->recently_updated ) ) { |
0 | 65 |
$bookmark->recently_updated = false; |
5 | 66 |
} |
67 |
$output .= $r['before']; |
|
68 |
if ( $r['show_updated'] && $bookmark->recently_updated ) { |
|
69 |
$output .= '<em>'; |
|
70 |
} |
|
0 | 71 |
$the_link = '#'; |
5 | 72 |
if ( ! empty( $bookmark->link_url ) ) { |
73 |
$the_link = esc_url( $bookmark->link_url ); |
|
74 |
} |
|
75 |
$desc = esc_attr( sanitize_bookmark_field( 'link_description', $bookmark->link_description, $bookmark->link_id, 'display' ) ); |
|
76 |
$name = esc_attr( sanitize_bookmark_field( 'link_name', $bookmark->link_name, $bookmark->link_id, 'display' ) ); |
|
0 | 77 |
$title = $desc; |
78 |
||
5 | 79 |
if ( $r['show_updated'] ) { |
80 |
if ( '00' != substr( $bookmark->link_updated_f, 0, 2 ) ) { |
|
0 | 81 |
$title .= ' ('; |
5 | 82 |
$title .= sprintf( |
83 |
__('Last updated: %s'), |
|
84 |
date( |
|
85 |
get_option( 'links_updated_date_format' ), |
|
86 |
$bookmark->link_updated_f + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) |
|
87 |
) |
|
88 |
); |
|
0 | 89 |
$title .= ')'; |
90 |
} |
|
5 | 91 |
} |
92 |
$alt = ' alt="' . $name . ( $r['show_description'] ? ' ' . $title : '' ) . '"'; |
|
0 | 93 |
|
5 | 94 |
if ( '' != $title ) { |
0 | 95 |
$title = ' title="' . $title . '"'; |
5 | 96 |
} |
0 | 97 |
$rel = $bookmark->link_rel; |
5 | 98 |
if ( '' != $rel ) { |
0 | 99 |
$rel = ' rel="' . esc_attr($rel) . '"'; |
5 | 100 |
} |
0 | 101 |
$target = $bookmark->link_target; |
5 | 102 |
if ( '' != $target ) { |
0 | 103 |
$target = ' target="' . $target . '"'; |
5 | 104 |
} |
0 | 105 |
$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>'; |
106 |
||
5 | 107 |
$output .= $r['link_before']; |
0 | 108 |
|
5 | 109 |
if ( $bookmark->link_image != null && $r['show_images'] ) { |
110 |
if ( strpos( $bookmark->link_image, 'http' ) === 0 ) { |
|
0 | 111 |
$output .= "<img src=\"$bookmark->link_image\" $alt $title />"; |
5 | 112 |
} else { // If it's a relative path |
0 | 113 |
$output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />"; |
5 | 114 |
} |
115 |
if ( $r['show_name'] ) { |
|
0 | 116 |
$output .= " $name"; |
5 | 117 |
} |
0 | 118 |
} else { |
119 |
$output .= $name; |
|
120 |
} |
|
121 |
||
5 | 122 |
$output .= $r['link_after']; |
0 | 123 |
|
124 |
$output .= '</a>'; |
|
125 |
||
5 | 126 |
if ( $r['show_updated'] && $bookmark->recently_updated ) { |
127 |
$output .= '</em>'; |
|
128 |
} |
|
129 |
||
130 |
if ( $r['show_description'] && '' != $desc ) { |
|
131 |
$output .= $r['between'] . $desc; |
|
132 |
} |
|
0 | 133 |
|
5 | 134 |
if ( $r['show_rating'] ) { |
135 |
$output .= $r['between'] . sanitize_bookmark_field( |
|
136 |
'link_rating', |
|
137 |
$bookmark->link_rating, |
|
138 |
$bookmark->link_id, |
|
139 |
'display' |
|
140 |
); |
|
141 |
} |
|
142 |
$output .= $r['after'] . "\n"; |
|
0 | 143 |
} // end while |
144 |
||
145 |
return $output; |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Retrieve or echo all of the bookmarks. |
|
150 |
* |
|
151 |
* List of default arguments are as follows: |
|
152 |
* |
|
153 |
* These options define how the Category name will appear before the category |
|
154 |
* links are displayed, if 'categorize' is 1. If 'categorize' is 0, then it will |
|
155 |
* display for only the 'title_li' string and only if 'title_li' is not empty. |
|
156 |
* |
|
157 |
* @since 2.1.0 |
|
5 | 158 |
* |
159 |
* @see _walk_bookmarks() |
|
160 |
* |
|
161 |
* @param string|array $args { |
|
162 |
* Optional. String or array of arguments to list bookmarks. |
|
0 | 163 |
* |
5 | 164 |
* @type string $orderby How to order the links by. Accepts post fields. Default 'name'. |
165 |
* @type string $order Whether to order bookmarks in ascending or descending order. |
|
166 |
* Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'. |
|
167 |
* @type int $limit Amount of bookmarks to display. Accepts 1+ or -1 for all. |
|
168 |
* Default -1. |
|
169 |
* @type string $category Comma-separated list of category ids to include links from. |
|
170 |
* Default empty. |
|
171 |
* @type string $category_name Category to retrieve links for by name. Default empty. |
|
172 |
* @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts |
|
173 |
* 1|true or 0|false. Default 1|true. |
|
174 |
* @type int|bool $show_updated Whether to display the time the bookmark was last updated. |
|
175 |
* Accepts 1|true or 0|false. Default 0|false. |
|
176 |
* @type int|bool $echo Whether to echo or return the formatted bookmarks. Accepts |
|
177 |
* 1|true (echo) or 0|false (return). Default 1|true. |
|
178 |
* @type int|bool $categorize Whether to show links listed by category or in a single column. |
|
179 |
* Accepts 1|true (by category) or 0|false (one column). Default 1|true. |
|
180 |
* @type int|bool $show_description Whether to show the bookmark descriptions. Accepts 1|true or 0|false. |
|
181 |
* Default 0|false. |
|
182 |
* @type string $title_li What to show before the links appear. Default 'Bookmarks'. |
|
183 |
* @type string $title_before The HTML or text to prepend to the $title_li string. Default '<h2>'. |
|
184 |
* @type string $title_after The HTML or text to append to the $title_li string. Default '</h2>'. |
|
185 |
* @type string $class The CSS class to use for the $title_li. Default 'linkcat'. |
|
186 |
* @type string $category_before The HTML or text to prepend to $title_before if $categorize is true. |
|
187 |
* String must contain '%id' and '%class' to inherit the category ID and |
|
188 |
* the $class argument used for formatting in themes. |
|
189 |
* Default '<li id="%id" class="%class">'. |
|
190 |
* @type string $category_after The HTML or text to append to $title_after if $categorize is true. |
|
191 |
* Default '</li>'. |
|
192 |
* @type string $category_orderby How to order the bookmark category based on term scheme if $categorize |
|
193 |
* is true. Default 'name'. |
|
194 |
* @type string $category_order Whether to order categories in ascending or descending order if |
|
195 |
* $categorize is true. Accepts 'ASC' (ascending) or 'DESC' (descending). |
|
196 |
* Default 'ASC'. |
|
197 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
* @return string|void Will only return if echo option is set to not echo. Default is not return anything. |
0 | 199 |
*/ |
5 | 200 |
function wp_list_bookmarks( $args = '' ) { |
0 | 201 |
$defaults = array( |
202 |
'orderby' => 'name', 'order' => 'ASC', |
|
203 |
'limit' => -1, 'category' => '', 'exclude_category' => '', |
|
204 |
'category_name' => '', 'hide_invisible' => 1, |
|
205 |
'show_updated' => 0, 'echo' => 1, |
|
206 |
'categorize' => 1, 'title_li' => __('Bookmarks'), |
|
207 |
'title_before' => '<h2>', 'title_after' => '</h2>', |
|
208 |
'category_orderby' => 'name', 'category_order' => 'ASC', |
|
209 |
'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">', |
|
210 |
'category_after' => '</li>' |
|
211 |
); |
|
212 |
||
213 |
$r = wp_parse_args( $args, $defaults ); |
|
214 |
||
215 |
$output = ''; |
|
216 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
if ( ! is_array( $r['class'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
$r['class'] = explode( ' ', $r['class'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
220 |
$r['class'] = array_map( 'sanitize_html_class', $r['class'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
$r['class'] = trim( join( ' ', $r['class'] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
|
5 | 223 |
if ( $r['categorize'] ) { |
224 |
$cats = get_terms( 'link_category', array( |
|
225 |
'name__like' => $r['category_name'], |
|
226 |
'include' => $r['category'], |
|
227 |
'exclude' => $r['exclude_category'], |
|
228 |
'orderby' => $r['category_orderby'], |
|
229 |
'order' => $r['category_order'], |
|
230 |
'hierarchical' => 0 |
|
231 |
) ); |
|
232 |
if ( empty( $cats ) ) { |
|
233 |
$r['categorize'] = false; |
|
234 |
} |
|
0 | 235 |
} |
236 |
||
5 | 237 |
if ( $r['categorize'] ) { |
0 | 238 |
// Split the bookmarks into ul's for each category |
239 |
foreach ( (array) $cats as $cat ) { |
|
5 | 240 |
$params = array_merge( $r, array( 'category' => $cat->term_id ) ); |
241 |
$bookmarks = get_bookmarks( $params ); |
|
242 |
if ( empty( $bookmarks ) ) { |
|
0 | 243 |
continue; |
5 | 244 |
} |
245 |
$output .= str_replace( |
|
246 |
array( '%id', '%class' ), |
|
247 |
array( "linkcat-$cat->term_id", $r['class'] ), |
|
248 |
$r['category_before'] |
|
249 |
); |
|
0 | 250 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* Filters the bookmarks category name. |
0 | 252 |
* |
253 |
* @since 2.2.0 |
|
254 |
* |
|
5 | 255 |
* @param string $cat_name The category name of bookmarks. |
0 | 256 |
*/ |
257 |
$catname = apply_filters( 'link_category', $cat->name ); |
|
258 |
||
5 | 259 |
$output .= $r['title_before']; |
260 |
$output .= $catname; |
|
261 |
$output .= $r['title_after']; |
|
262 |
$output .= "\n\t<ul class='xoxo blogroll'>\n"; |
|
263 |
$output .= _walk_bookmarks( $bookmarks, $r ); |
|
264 |
$output .= "\n\t</ul>\n"; |
|
265 |
$output .= $r['category_after'] . "\n"; |
|
0 | 266 |
} |
267 |
} else { |
|
268 |
//output one single list using title_li for the title |
|
5 | 269 |
$bookmarks = get_bookmarks( $r ); |
0 | 270 |
|
5 | 271 |
if ( ! empty( $bookmarks ) ) { |
272 |
if ( ! empty( $r['title_li'] ) ) { |
|
273 |
$output .= str_replace( |
|
274 |
array( '%id', '%class' ), |
|
275 |
array( "linkcat-" . $r['category'], $r['class'] ), |
|
276 |
$r['category_before'] |
|
277 |
); |
|
278 |
$output .= $r['title_before']; |
|
279 |
$output .= $r['title_li']; |
|
280 |
$output .= $r['title_after']; |
|
281 |
$output .= "\n\t<ul class='xoxo blogroll'>\n"; |
|
282 |
$output .= _walk_bookmarks( $bookmarks, $r ); |
|
283 |
$output .= "\n\t</ul>\n"; |
|
284 |
$output .= $r['category_after'] . "\n"; |
|
0 | 285 |
} else { |
5 | 286 |
$output .= _walk_bookmarks( $bookmarks, $r ); |
0 | 287 |
} |
288 |
} |
|
289 |
} |
|
290 |
||
291 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
* Filters the bookmarks list before it is echoed or returned. |
0 | 293 |
* |
294 |
* @since 2.5.0 |
|
295 |
* |
|
5 | 296 |
* @param string $html The HTML list of bookmarks. |
0 | 297 |
*/ |
5 | 298 |
$html = apply_filters( 'wp_list_bookmarks', $output ); |
0 | 299 |
|
5 | 300 |
if ( ! $r['echo'] ) { |
301 |
return $html; |
|
302 |
} |
|
303 |
echo $html; |
|
0 | 304 |
} |