PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/bookmark-template.php

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