PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/bookmark.php

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