PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-includes/bookmark.php

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