PageRenderTime 66ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/whooz.org/wp-includes/bookmark.php

https://github.com/rukku/whooz
PHP | 346 lines | 189 code | 42 blank | 115 comment | 55 complexity | 45f9d03dd9b647228cf5f5b890c80e9c MD5 | raw file
  1. <?php
  2. /**
  3. * Link/Bookmark API
  4. *
  5. * @package WordPress
  6. * @subpackage Bookmark
  7. */
  8. /**
  9. * Retrieve Bookmark data based on ID
  10. *
  11. * @since 2.1
  12. * @uses $wpdb Database Object
  13. *
  14. * @param int $bookmark_id
  15. * @param string $output Optional. Either OBJECT, ARRAY_N, or ARRAY_A constant
  16. * @param string $filter Optional, default is 'raw'.
  17. * @return array|object Type returned depends on $output value.
  18. */
  19. function get_bookmark($bookmark_id, $output = OBJECT, $filter = 'raw') {
  20. global $wpdb;
  21. $link = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark_id));
  22. $link->link_category = array_unique( wp_get_object_terms($link->link_id, 'link_category', 'fields=ids') );
  23. $link = sanitize_bookmark($link, $filter);
  24. if ( $output == OBJECT ) {
  25. return $link;
  26. } elseif ( $output == ARRAY_A ) {
  27. return get_object_vars($link);
  28. } elseif ( $output == ARRAY_N ) {
  29. return array_values(get_object_vars($link));
  30. } else {
  31. return $link;
  32. }
  33. }
  34. /**
  35. * Retrieve single bookmark data item or field.
  36. *
  37. * @since 2.3
  38. * @uses get_bookmark() Gets bookmark object using $bookmark as ID
  39. * @uses sanitize_bookmark_field() Sanitizes Bookmark field based on $context.
  40. *
  41. * @param string $field The name of the data field to return
  42. * @param int $bookmark The bookmark ID to get field
  43. * @param string $context Optional. The context of how the field will be used.
  44. * @return string
  45. */
  46. function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
  47. $bookmark = (int) $bookmark;
  48. $bookmark = get_bookmark( $bookmark );
  49. if ( is_wp_error($bookmark) )
  50. return $bookmark;
  51. if ( !is_object($bookmark) )
  52. return '';
  53. if ( !isset($bookmark->$field) )
  54. return '';
  55. return sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
  56. }
  57. /**
  58. * Retrieve bookmark data based on ID.
  59. *
  60. * @since 2.0
  61. * @deprecated Use get_bookmark()
  62. * @see get_bookmark()
  63. *
  64. * @param int $bookmark_id ID of link
  65. * @param string $output Either OBJECT, ARRAY_N, or ARRAY_A
  66. * @return object|array
  67. */
  68. function get_link($bookmark_id, $output = OBJECT, $filter = 'raw') {
  69. return get_bookmark($bookmark_id, $output, $filter);
  70. }
  71. /**
  72. * Retrieves the list of bookmarks
  73. *
  74. * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
  75. * that fails, then the query will be built from the arguments and executed. The
  76. * results will be stored to the cache.
  77. *
  78. * List of default arguments are as follows:
  79. * 'orderby' - Default is 'name' (string). How to order the links by. String is
  80. * based off of the bookmark scheme.
  81. * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
  82. * ascending or descending order.
  83. * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
  84. * display.
  85. * 'category' - Default is empty string (string). Include the links in what
  86. * category ID(s).
  87. * 'category_name' - Default is empty string (string). Get links by category
  88. * name.
  89. * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
  90. * links marked as 'invisible'.
  91. * 'show_updated' - Default is 0 (integer). Will show the time of when the
  92. * bookmark was last updated.
  93. * 'include' - Default is empty string (string). Include other categories
  94. * separated by commas.
  95. * 'exclude' - Default is empty string (string). Exclude other categories
  96. * separated by commas.
  97. *
  98. * @since 2.1
  99. * @uses $wpdb Database Object
  100. * @link http://codex.wordpress.org/Template_Tags/get_bookmarks
  101. *
  102. * @param string|array $args List of arguments to overwrite the defaults
  103. * @return array List of bookmark row objects
  104. */
  105. function get_bookmarks($args = '') {
  106. global $wpdb;
  107. $defaults = array(
  108. 'orderby' => 'name', 'order' => 'ASC',
  109. 'limit' => -1, 'category' => '',
  110. 'category_name' => '', 'hide_invisible' => 1,
  111. 'show_updated' => 0, 'include' => '',
  112. 'exclude' => '', 'search' => ''
  113. );
  114. $r = wp_parse_args( $args, $defaults );
  115. extract( $r, EXTR_SKIP );
  116. $key = md5( serialize( $r ) );
  117. if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) )
  118. if ( isset( $cache[ $key ] ) )
  119. return apply_filters('get_bookmarks', $cache[ $key ], $r );
  120. $inclusions = '';
  121. if ( !empty($include) ) {
  122. $exclude = ''; //ignore exclude, category, and category_name params if using include
  123. $category = '';
  124. $category_name = '';
  125. $inclinks = preg_split('/[\s,]+/',$include);
  126. if ( count($inclinks) ) {
  127. foreach ( $inclinks as $inclink ) {
  128. if (empty($inclusions))
  129. $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
  130. else
  131. $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
  132. }
  133. }
  134. }
  135. if (!empty($inclusions))
  136. $inclusions .= ')';
  137. $exclusions = '';
  138. if ( !empty($exclude) ) {
  139. $exlinks = preg_split('/[\s,]+/',$exclude);
  140. if ( count($exlinks) ) {
  141. foreach ( $exlinks as $exlink ) {
  142. if (empty($exclusions))
  143. $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
  144. else
  145. $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
  146. }
  147. }
  148. }
  149. if (!empty($exclusions))
  150. $exclusions .= ')';
  151. if ( ! empty($category_name) ) {
  152. if ( $category = get_term_by('name', $category_name, 'link_category') )
  153. $category = $category->term_id;
  154. }
  155. if ( ! empty($search) ) {
  156. $search = like_escape($search);
  157. $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
  158. }
  159. $category_query = '';
  160. $join = '';
  161. if ( !empty($category) ) {
  162. $incategories = preg_split('/[\s,]+/',$category);
  163. if ( count($incategories) ) {
  164. foreach ( $incategories as $incat ) {
  165. if (empty($category_query))
  166. $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
  167. else
  168. $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
  169. }
  170. }
  171. }
  172. if (!empty($category_query)) {
  173. $category_query .= ") AND taxonomy = 'link_category'";
  174. $join = " INNER JOIN $wpdb->term_relationships AS tr ON ($wpdb->links.link_id = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id";
  175. }
  176. if (get_option('links_recently_updated_time')) {
  177. $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
  178. } else {
  179. $recently_updated_test = '';
  180. }
  181. $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
  182. $orderby = strtolower($orderby);
  183. $length = '';
  184. switch ($orderby) {
  185. case 'length':
  186. $length = ", CHAR_LENGTH(link_name) AS length";
  187. break;
  188. case 'rand':
  189. $orderby = 'rand()';
  190. break;
  191. default:
  192. $orderby = "link_" . $orderby;
  193. }
  194. if ( 'link_id' == $orderby )
  195. $orderby = "$wpdb->links.link_id";
  196. $visible = '';
  197. if ( $hide_invisible )
  198. $visible = "AND link_visible = 'Y'";
  199. $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
  200. $query .= " $exclusions $inclusions $search";
  201. $query .= " ORDER BY $orderby $order";
  202. if ($limit != -1)
  203. $query .= " LIMIT $limit";
  204. $results = $wpdb->get_results($query);
  205. $cache[ $key ] = $results;
  206. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  207. return apply_filters('get_bookmarks', $results, $r);
  208. }
  209. /**
  210. * Sanitizes all bookmark fields
  211. *
  212. * @since 2.3
  213. *
  214. * @param object|array $bookmark Bookmark row
  215. * @param string $context Optional, default is 'display'. How to filter the
  216. * fields
  217. * @return object|array Same type as $bookmark but with fields sanitized.
  218. */
  219. function sanitize_bookmark($bookmark, $context = 'display') {
  220. $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
  221. 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
  222. 'link_rel', 'link_notes', 'link_rss', );
  223. $do_object = false;
  224. if ( is_object($bookmark) )
  225. $do_object = true;
  226. foreach ( $fields as $field ) {
  227. if ( $do_object )
  228. $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $bookmark->link_id, $context);
  229. else
  230. $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $bookmark['link_id'], $context);
  231. }
  232. return $bookmark;
  233. }
  234. /**
  235. * Sanitizes a bookmark field
  236. *
  237. * Sanitizes the bookmark fields based on what the field name is. If the field
  238. * has a strict value set, then it will be tested for that, else a more generic
  239. * filtering is applied. After the more strict filter is applied, if the
  240. * $context is 'raw' then the value is immediately return.
  241. *
  242. * Hooks exist for the more generic cases. With the 'edit' context, the
  243. * 'edit_$field' filter will be called and passed the $value and $bookmark_id
  244. * respectively. With the 'db' context, the 'pre_$field' filter is called and
  245. * passed the value. The 'display' context is the final context and has the
  246. * $field has the filter name and is passed the $value, $bookmark_id, and
  247. * $context respectively.
  248. *
  249. * @since 2.3
  250. *
  251. * @param string $field The bookmark field
  252. * @param mixed $value The bookmark field value
  253. * @param int $bookmark_id Bookmark ID
  254. * @param string $context How to filter the field value. Either 'raw', 'edit',
  255. * 'attribute', 'js', 'db', or 'display'
  256. * @return mixed The filtered value
  257. */
  258. function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
  259. $int_fields = array('link_id', 'link_rating');
  260. if ( in_array($field, $int_fields) )
  261. $value = (int) $value;
  262. $yesno = array('link_visible');
  263. if ( in_array($field, $yesno) )
  264. $value = preg_replace('/[^YNyn]/', '', $value);
  265. if ( 'link_target' == $field ) {
  266. $targets = array('_top', '_blank');
  267. if ( ! in_array($value, $targets) )
  268. $value = '';
  269. }
  270. if ( 'raw' == $context )
  271. return $value;
  272. if ( 'edit' == $context ) {
  273. $format_to_edit = array('link_notes');
  274. $value = apply_filters("edit_$field", $value, $bookmark_id);
  275. if ( in_array($field, $format_to_edit) ) {
  276. $value = format_to_edit($value);
  277. } else {
  278. $value = attribute_escape($value);
  279. }
  280. } else if ( 'db' == $context ) {
  281. $value = apply_filters("pre_$field", $value);
  282. } else {
  283. // Use display filters by default.
  284. $value = apply_filters($field, $value, $bookmark_id, $context);
  285. }
  286. if ( 'attribute' == $context )
  287. $value = attribute_escape($value);
  288. else if ( 'js' == $context )
  289. $value = js_escape($value);
  290. return $value;
  291. }
  292. /**
  293. * Deletes entire bookmark cache
  294. *
  295. * @since 2.1
  296. * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
  297. */
  298. function delete_get_bookmark_cache() {
  299. wp_cache_delete( 'get_bookmarks', 'bookmark' );
  300. }
  301. add_action( 'add_link', 'delete_get_bookmark_cache' );
  302. add_action( 'edit_link', 'delete_get_bookmark_cache' );
  303. add_action( 'delete_link', 'delete_get_bookmark_cache' );
  304. ?>