bookmark-template.php 13 KB

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