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