PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/bookmark-template.php

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 257 lines | 112 code | 30 blank | 115 comment | 35 complexity | 854d97261d64d486c6fe0e084e547410 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-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. * The defaults for overwriting are:
  22. * 'show_updated' - Default is 0 (integer). Will show the time of when the
  23. * bookmark was last updated.
  24. * 'show_description' - Default is 0 (integer). Whether to show the description
  25. * of the bookmark.
  26. * 'show_images' - Default is 1 (integer). Whether to show link image if
  27. * available.
  28. * 'show_name' - Default is 0 (integer). Whether to show link name if
  29. * available.
  30. * 'before' - Default is '<li>' (string). The html or text to prepend to each
  31. * bookmarks.
  32. * 'after' - Default is '</li>' (string). The html or text to append to each
  33. * bookmarks.
  34. * 'link_before' - Default is '' (string). The html or text to prepend to each
  35. * bookmarks inside the <a> tag.
  36. * 'link_after' - Default is '' (string). The html or text to append to each
  37. * bookmarks inside the <a> tag.
  38. * 'between' - Default is '\n' (string). The string for use in between the link,
  39. * description, and image.
  40. * 'show_rating' - Default is 0 (integer). Whether to show the link rating.
  41. *
  42. * @since 2.1.0
  43. * @access private
  44. *
  45. * @param array $bookmarks List of bookmarks to traverse
  46. * @param string|array $args Optional. Overwrite the defaults.
  47. * @return string Formatted output in HTML
  48. */
  49. function _walk_bookmarks($bookmarks, $args = '' ) {
  50. $defaults = array(
  51. 'show_updated' => 0, 'show_description' => 0,
  52. 'show_images' => 1, 'show_name' => 0,
  53. 'before' => '<li>', 'after' => '</li>', 'between' => "\n",
  54. 'show_rating' => 0, 'link_before' => '', 'link_after' => ''
  55. );
  56. $r = wp_parse_args( $args, $defaults );
  57. extract( $r, EXTR_SKIP );
  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. $output .= $before;
  63. if ( $show_updated && $bookmark->recently_updated )
  64. $output .= get_option('links_recently_updated_prepend');
  65. $the_link = '#';
  66. if ( !empty($bookmark->link_url) )
  67. $the_link = esc_url($bookmark->link_url);
  68. $desc = esc_attr(sanitize_bookmark_field('link_description', $bookmark->link_description, $bookmark->link_id, 'display'));
  69. $name = esc_attr(sanitize_bookmark_field('link_name', $bookmark->link_name, $bookmark->link_id, 'display'));
  70. $title = $desc;
  71. if ( $show_updated )
  72. if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
  73. $title .= ' (';
  74. $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)));
  75. $title .= ')';
  76. }
  77. $alt = ' alt="' . $name . ( $show_description ? ' ' . $title : '' ) . '"';
  78. if ( '' != $title )
  79. $title = ' title="' . $title . '"';
  80. $rel = $bookmark->link_rel;
  81. if ( '' != $rel )
  82. $rel = ' rel="' . esc_attr($rel) . '"';
  83. $target = $bookmark->link_target;
  84. if ( '' != $target )
  85. $target = ' target="' . $target . '"';
  86. $output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
  87. $output .= $link_before;
  88. if ( $bookmark->link_image != null && $show_images ) {
  89. if ( strpos($bookmark->link_image, 'http') === 0 )
  90. $output .= "<img src=\"$bookmark->link_image\" $alt $title />";
  91. else // If it's a relative path
  92. $output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
  93. if ( $show_name )
  94. $output .= " $name";
  95. } else {
  96. $output .= $name;
  97. }
  98. $output .= $link_after;
  99. $output .= '</a>';
  100. if ( $show_updated && $bookmark->recently_updated )
  101. $output .= get_option('links_recently_updated_append');
  102. if ( $show_description && '' != $desc )
  103. $output .= $between . $desc;
  104. if ( $show_rating )
  105. $output .= $between . sanitize_bookmark_field('link_rating', $bookmark->link_rating, $bookmark->link_id, 'display');
  106. $output .= "$after\n";
  107. } // end while
  108. return $output;
  109. }
  110. /**
  111. * Retrieve or echo all of the bookmarks.
  112. *
  113. * List of default arguments are as follows:
  114. * 'orderby' - Default is 'name' (string). How to order the links by. String is
  115. * based off of the bookmark scheme.
  116. * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
  117. * ascending or descending order.
  118. * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
  119. * display.
  120. * 'category' - Default is empty string (string). Include the links in what
  121. * category ID(s).
  122. * 'category_name' - Default is empty string (string). Get links by category
  123. * name.
  124. * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
  125. * links marked as 'invisible'.
  126. * 'show_updated' - Default is 0 (integer). Will show the time of when the
  127. * bookmark was last updated.
  128. * 'echo' - Default is 1 (integer). Whether to echo (default) or return the
  129. * formatted bookmarks.
  130. * 'categorize' - Default is 1 (integer). Whether to show links listed by
  131. * category (default) or show links in one column.
  132. * 'show_description' - Default is 0 (integer). Whether to show the description
  133. * of the bookmark.
  134. *
  135. * These options define how the Category name will appear before the category
  136. * links are displayed, if 'categorize' is 1. If 'categorize' is 0, then it will
  137. * display for only the 'title_li' string and only if 'title_li' is not empty.
  138. * 'title_li' - Default is 'Bookmarks' (translatable string). What to show
  139. * before the links appear.
  140. * 'title_before' - Default is '<h2>' (string). The HTML or text to show before
  141. * the 'title_li' string.
  142. * 'title_after' - Default is '</h2>' (string). The HTML or text to show after
  143. * the 'title_li' string.
  144. * 'class' - Default is 'linkcat' (string). The CSS class to use for the
  145. * 'title_li'.
  146. *
  147. * 'category_before' - Default is '<li id="%id" class="%class">'. String must
  148. * contain '%id' and '%class' to get
  149. * the id of the category and the 'class' argument. These are used for
  150. * formatting in themes.
  151. * Argument will be displayed before the 'title_before' argument.
  152. * 'category_after' - Default is '</li>' (string). The HTML or text that will
  153. * appear after the list of links.
  154. *
  155. * These are only used if 'categorize' is set to 1 or true.
  156. * 'category_orderby' - Default is 'name'. How to order the bookmark category
  157. * based on term scheme.
  158. * 'category_order' - Default is 'ASC'. Set the order by either ASC (ascending)
  159. * or DESC (descending).
  160. *
  161. * @see _walk_bookmarks() For other arguments that can be set in this function
  162. * and passed to _walk_bookmarks().
  163. * @see get_bookmarks() For other arguments that can be set in this function and
  164. * passed to get_bookmarks().
  165. * @link http://codex.wordpress.org/Template_Tags/wp_list_bookmarks
  166. *
  167. * @since 2.1.0
  168. * @uses _walk_bookmarks() Used to iterate over all of the bookmarks and return
  169. * the html
  170. * @uses get_terms() Gets all of the categories that are for links.
  171. *
  172. * @param string|array $args Optional. Overwrite the defaults of the function
  173. * @return string|null Will only return if echo option is set to not echo.
  174. * Default is not return anything.
  175. */
  176. function wp_list_bookmarks($args = '') {
  177. $defaults = array(
  178. 'orderby' => 'name', 'order' => 'ASC',
  179. 'limit' => -1, 'category' => '', 'exclude_category' => '',
  180. 'category_name' => '', 'hide_invisible' => 1,
  181. 'show_updated' => 0, 'echo' => 1,
  182. 'categorize' => 1, 'title_li' => __('Bookmarks'),
  183. 'title_before' => '<h2>', 'title_after' => '</h2>',
  184. 'category_orderby' => 'name', 'category_order' => 'ASC',
  185. 'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
  186. 'category_after' => '</li>'
  187. );
  188. $r = wp_parse_args( $args, $defaults );
  189. extract( $r, EXTR_SKIP );
  190. $output = '';
  191. if ( $categorize ) {
  192. $cats = get_terms( 'link_category', array( 'name__like' => $category_name, 'include' => $category, 'exclude' => $exclude_category, 'orderby' => $category_orderby, 'order' => $category_order, 'hierarchical' => 0 ) );
  193. if ( empty( $cats ) )
  194. $categorize = false;
  195. }
  196. if ( $categorize ) {
  197. // Split the bookmarks into ul's for each category
  198. foreach ( (array) $cats as $cat ) {
  199. $params = array_merge($r, array('category'=>$cat->term_id));
  200. $bookmarks = get_bookmarks($params);
  201. if ( empty($bookmarks) )
  202. continue;
  203. $output .= str_replace(array('%id', '%class'), array("linkcat-$cat->term_id", $class), $category_before);
  204. $catname = apply_filters( "link_category", $cat->name );
  205. $output .= "$title_before$catname$title_after\n\t<ul class='xoxo blogroll'>\n";
  206. $output .= _walk_bookmarks($bookmarks, $r);
  207. $output .= "\n\t</ul>\n$category_after\n";
  208. }
  209. } else {
  210. //output one single list using title_li for the title
  211. $bookmarks = get_bookmarks($r);
  212. if ( !empty($bookmarks) ) {
  213. if ( !empty( $title_li ) ){
  214. $output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
  215. $output .= "$title_before$title_li$title_after\n\t<ul class='xoxo blogroll'>\n";
  216. $output .= _walk_bookmarks($bookmarks, $r);
  217. $output .= "\n\t</ul>\n$category_after\n";
  218. } else {
  219. $output .= _walk_bookmarks($bookmarks, $r);
  220. }
  221. }
  222. }
  223. $output = apply_filters( 'wp_list_bookmarks', $output );
  224. if ( !$echo )
  225. return $output;
  226. echo $output;
  227. }