PageRenderTime 61ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

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

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