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