PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/bookmark-template.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 324 lines | 267 code | 8 blank | 49 comment | 18 complexity | 10c1b1f52695f780ba5d2aff519b2250 MD5 | raw file
  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. if ( '' != $rel ) {
  100. $rel = ' rel="' . esc_attr( $rel ) . '"';
  101. }
  102. $target = $bookmark->link_target;
  103. if ( '' != $target ) {
  104. $target = ' target="' . $target . '"';
  105. }
  106. $output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
  107. $output .= $parsed_args['link_before'];
  108. if ( null != $bookmark->link_image && $parsed_args['show_images'] ) {
  109. if ( strpos( $bookmark->link_image, 'http' ) === 0 ) {
  110. $output .= "<img src=\"$bookmark->link_image\" $alt $title />";
  111. } else { // If it's a relative path.
  112. $output .= '<img src="' . get_option( 'siteurl' ) . "$bookmark->link_image\" $alt $title />";
  113. }
  114. if ( $parsed_args['show_name'] ) {
  115. $output .= " $name";
  116. }
  117. } else {
  118. $output .= $name;
  119. }
  120. $output .= $parsed_args['link_after'];
  121. $output .= '</a>';
  122. if ( $parsed_args['show_updated'] && $bookmark->recently_updated ) {
  123. $output .= '</em>';
  124. }
  125. if ( $parsed_args['show_description'] && '' != $desc ) {
  126. $output .= $parsed_args['between'] . $desc;
  127. }
  128. if ( $parsed_args['show_rating'] ) {
  129. $output .= $parsed_args['between'] . sanitize_bookmark_field(
  130. 'link_rating',
  131. $bookmark->link_rating,
  132. $bookmark->link_id,
  133. 'display'
  134. );
  135. }
  136. $output .= $parsed_args['after'] . "\n";
  137. } // End while.
  138. return $output;
  139. }
  140. /**
  141. * Retrieve or echo all of the bookmarks.
  142. *
  143. * List of default arguments are as follows:
  144. *
  145. * These options define how the Category name will appear before the category
  146. * links are displayed, if 'categorize' is 1. If 'categorize' is 0, then it will
  147. * display for only the 'title_li' string and only if 'title_li' is not empty.
  148. *
  149. * @since 2.1.0
  150. *
  151. * @see _walk_bookmarks()
  152. *
  153. * @param string|array $args {
  154. * Optional. String or array of arguments to list bookmarks.
  155. *
  156. * @type string $orderby How to order the links by. Accepts post fields. Default 'name'.
  157. * @type string $order Whether to order bookmarks in ascending or descending order.
  158. * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
  159. * @type int $limit Amount of bookmarks to display. Accepts 1+ or -1 for all.
  160. * Default -1.
  161. * @type string $category Comma-separated list of category ids to include links from.
  162. * Default empty.
  163. * @type string $category_name Category to retrieve links for by name. Default empty.
  164. * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
  165. * 1|true or 0|false. Default 1|true.
  166. * @type int|bool $show_updated Whether to display the time the bookmark was last updated.
  167. * Accepts 1|true or 0|false. Default 0|false.
  168. * @type int|bool $echo Whether to echo or return the formatted bookmarks. Accepts
  169. * 1|true (echo) or 0|false (return). Default 1|true.
  170. * @type int|bool $categorize Whether to show links listed by category or in a single column.
  171. * Accepts 1|true (by category) or 0|false (one column). Default 1|true.
  172. * @type int|bool $show_description Whether to show the bookmark descriptions. Accepts 1|true or 0|false.
  173. * Default 0|false.
  174. * @type string $title_li What to show before the links appear. Default 'Bookmarks'.
  175. * @type string $title_before The HTML or text to prepend to the $title_li string. Default '<h2>'.
  176. * @type string $title_after The HTML or text to append to the $title_li string. Default '</h2>'.
  177. * @type string $class The CSS class to use for the $title_li. Default 'linkcat'.
  178. * @type string $category_before The HTML or text to prepend to $title_before if $categorize is true.
  179. * String must contain '%id' and '%class' to inherit the category ID and
  180. * the $class argument used for formatting in themes.
  181. * Default '<li id="%id" class="%class">'.
  182. * @type string $category_after The HTML or text to append to $title_after if $categorize is true.
  183. * Default '</li>'.
  184. * @type string $category_orderby How to order the bookmark category based on term scheme if $categorize
  185. * is true. Default 'name'.
  186. * @type string $category_order Whether to order categories in ascending or descending order if
  187. * $categorize is true. Accepts 'ASC' (ascending) or 'DESC' (descending).
  188. * Default 'ASC'.
  189. * }
  190. * @return void|string Void if 'echo' argument is true, HTML list of bookmarks if 'echo' is false.
  191. */
  192. function wp_list_bookmarks( $args = '' ) {
  193. $defaults = array(
  194. 'orderby' => 'name',
  195. 'order' => 'ASC',
  196. 'limit' => -1,
  197. 'category' => '',
  198. 'exclude_category' => '',
  199. 'category_name' => '',
  200. 'hide_invisible' => 1,
  201. 'show_updated' => 0,
  202. 'echo' => 1,
  203. 'categorize' => 1,
  204. 'title_li' => __( 'Bookmarks' ),
  205. 'title_before' => '<h2>',
  206. 'title_after' => '</h2>',
  207. 'category_orderby' => 'name',
  208. 'category_order' => 'ASC',
  209. 'class' => 'linkcat',
  210. 'category_before' => '<li id="%id" class="%class">',
  211. 'category_after' => '</li>',
  212. );
  213. $parsed_args = wp_parse_args( $args, $defaults );
  214. $output = '';
  215. if ( ! is_array( $parsed_args['class'] ) ) {
  216. $parsed_args['class'] = explode( ' ', $parsed_args['class'] );
  217. }
  218. $parsed_args['class'] = array_map( 'sanitize_html_class', $parsed_args['class'] );
  219. $parsed_args['class'] = trim( join( ' ', $parsed_args['class'] ) );
  220. if ( $parsed_args['categorize'] ) {
  221. $cats = get_terms(
  222. array(
  223. 'taxonomy' => 'link_category',
  224. 'name__like' => $parsed_args['category_name'],
  225. 'include' => $parsed_args['category'],
  226. 'exclude' => $parsed_args['exclude_category'],
  227. 'orderby' => $parsed_args['category_orderby'],
  228. 'order' => $parsed_args['category_order'],
  229. 'hierarchical' => 0,
  230. )
  231. );
  232. if ( empty( $cats ) ) {
  233. $parsed_args['categorize'] = false;
  234. }
  235. }
  236. if ( $parsed_args['categorize'] ) {
  237. // Split the bookmarks into ul's for each category.
  238. foreach ( (array) $cats as $cat ) {
  239. $params = array_merge( $parsed_args, array( 'category' => $cat->term_id ) );
  240. $bookmarks = get_bookmarks( $params );
  241. if ( empty( $bookmarks ) ) {
  242. continue;
  243. }
  244. $output .= str_replace(
  245. array( '%id', '%class' ),
  246. array( "linkcat-$cat->term_id", $parsed_args['class'] ),
  247. $parsed_args['category_before']
  248. );
  249. /**
  250. * Filters the category name.
  251. *
  252. * @since 2.2.0
  253. *
  254. * @param string $cat_name The category name.
  255. */
  256. $catname = apply_filters( 'link_category', $cat->name );
  257. $output .= $parsed_args['title_before'];
  258. $output .= $catname;
  259. $output .= $parsed_args['title_after'];
  260. $output .= "\n\t<ul class='xoxo blogroll'>\n";
  261. $output .= _walk_bookmarks( $bookmarks, $parsed_args );
  262. $output .= "\n\t</ul>\n";
  263. $output .= $parsed_args['category_after'] . "\n";
  264. }
  265. } else {
  266. // Output one single list using title_li for the title.
  267. $bookmarks = get_bookmarks( $parsed_args );
  268. if ( ! empty( $bookmarks ) ) {
  269. if ( ! empty( $parsed_args['title_li'] ) ) {
  270. $output .= str_replace(
  271. array( '%id', '%class' ),
  272. array( 'linkcat-' . $parsed_args['category'], $parsed_args['class'] ),
  273. $parsed_args['category_before']
  274. );
  275. $output .= $parsed_args['title_before'];
  276. $output .= $parsed_args['title_li'];
  277. $output .= $parsed_args['title_after'];
  278. $output .= "\n\t<ul class='xoxo blogroll'>\n";
  279. $output .= _walk_bookmarks( $bookmarks, $parsed_args );
  280. $output .= "\n\t</ul>\n";
  281. $output .= $parsed_args['category_after'] . "\n";
  282. } else {
  283. $output .= _walk_bookmarks( $bookmarks, $parsed_args );
  284. }
  285. }
  286. }
  287. /**
  288. * Filters the bookmarks list before it is echoed or returned.
  289. *
  290. * @since 2.5.0
  291. *
  292. * @param string $html The HTML list of bookmarks.
  293. */
  294. $html = apply_filters( 'wp_list_bookmarks', $output );
  295. if ( $parsed_args['echo'] ) {
  296. echo $html;
  297. } else {
  298. return $html;
  299. }
  300. }