bookmark.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. *
  13. * @global wpdb $wpdb WordPress database abstraction object.
  14. *
  15. * @param int|stdClass $bookmark
  16. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
  17. * an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT.
  18. * @param string $filter Optional, default is 'raw'.
  19. * @return array|object|null Type returned depends on $output value.
  20. */
  21. function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
  22. global $wpdb;
  23. if ( empty( $bookmark ) ) {
  24. if ( isset( $GLOBALS['link'] ) ) {
  25. $_bookmark = & $GLOBALS['link'];
  26. } else {
  27. $_bookmark = null;
  28. }
  29. } elseif ( is_object( $bookmark ) ) {
  30. wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
  31. $_bookmark = $bookmark;
  32. } else {
  33. if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
  34. $_bookmark = & $GLOBALS['link'];
  35. } else {
  36. $_bookmark = wp_cache_get( $bookmark, 'bookmark' );
  37. if ( ! $_bookmark ) {
  38. $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
  39. if ( $_bookmark ) {
  40. $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
  41. wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
  42. }
  43. }
  44. }
  45. }
  46. if ( ! $_bookmark ) {
  47. return $_bookmark;
  48. }
  49. $_bookmark = sanitize_bookmark( $_bookmark, $filter );
  50. if ( $output == OBJECT ) {
  51. return $_bookmark;
  52. } elseif ( $output == ARRAY_A ) {
  53. return get_object_vars( $_bookmark );
  54. } elseif ( $output == ARRAY_N ) {
  55. return array_values( get_object_vars( $_bookmark ) );
  56. } else {
  57. return $_bookmark;
  58. }
  59. }
  60. /**
  61. * Retrieve single bookmark data item or field.
  62. *
  63. * @since 2.3.0
  64. *
  65. * @param string $field The name of the data field to return
  66. * @param int $bookmark The bookmark ID to get field
  67. * @param string $context Optional. The context of how the field will be used.
  68. * @return string|WP_Error
  69. */
  70. function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
  71. $bookmark = (int) $bookmark;
  72. $bookmark = get_bookmark( $bookmark );
  73. if ( is_wp_error( $bookmark ) ) {
  74. return $bookmark;
  75. }
  76. if ( ! is_object( $bookmark ) ) {
  77. return '';
  78. }
  79. if ( ! isset( $bookmark->$field ) ) {
  80. return '';
  81. }
  82. return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context );
  83. }
  84. /**
  85. * Retrieves the list of bookmarks
  86. *
  87. * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
  88. * that fails, then the query will be built from the arguments and executed. The
  89. * results will be stored to the cache.
  90. *
  91. * @since 2.1.0
  92. *
  93. * @global wpdb $wpdb WordPress database abstraction object.
  94. *
  95. * @param string|array $args {
  96. * Optional. String or array of arguments to retrieve bookmarks.
  97. *
  98. * @type string $orderby How to order the links by. Accepts 'id', 'link_id', 'name', 'link_name',
  99. * 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating',
  100. * 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes',
  101. * 'description', 'link_description', 'length' and 'rand'.
  102. * When `$orderby` is 'length', orders by the character length of
  103. * 'link_name'. Default 'name'.
  104. * @type string $order Whether to order bookmarks in ascending or descending order.
  105. * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
  106. * @type int $limit Amount of bookmarks to display. Accepts any positive number or
  107. * -1 for all. Default -1.
  108. * @type string $category Comma-separated list of category ids to include links from.
  109. * Default empty.
  110. * @type string $category_name Category to retrieve links for by name. Default empty.
  111. * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
  112. * 1|true or 0|false. Default 1|true.
  113. * @type int|bool $show_updated Whether to display the time the bookmark was last updated.
  114. * Accepts 1|true or 0|false. Default 0|false.
  115. * @type string $include Comma-separated list of bookmark IDs to include. Default empty.
  116. * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty.
  117. * @type string $search Search terms. Will be SQL-formatted with wildcards before and after
  118. * and searched in 'link_url', 'link_name' and 'link_description'.
  119. * Default empty.
  120. * }
  121. * @return array List of bookmark row objects.
  122. */
  123. function get_bookmarks( $args = '' ) {
  124. global $wpdb;
  125. $defaults = array(
  126. 'orderby' => 'name',
  127. 'order' => 'ASC',
  128. 'limit' => -1,
  129. 'category' => '',
  130. 'category_name' => '',
  131. 'hide_invisible' => 1,
  132. 'show_updated' => 0,
  133. 'include' => '',
  134. 'exclude' => '',
  135. 'search' => '',
  136. );
  137. $parsed_args = wp_parse_args( $args, $defaults );
  138. $key = md5( serialize( $parsed_args ) );
  139. $cache = wp_cache_get( 'get_bookmarks', 'bookmark' );
  140. if ( 'rand' !== $parsed_args['orderby'] && $cache ) {
  141. if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
  142. $bookmarks = $cache[ $key ];
  143. /**
  144. * Filters the returned list of bookmarks.
  145. *
  146. * The first time the hook is evaluated in this file, it returns the cached
  147. * bookmarks list. The second evaluation returns a cached bookmarks list if the
  148. * link category is passed but does not exist. The third evaluation returns
  149. * the full cached results.
  150. *
  151. * @since 2.1.0
  152. *
  153. * @see get_bookmarks()
  154. *
  155. * @param array $bookmarks List of the cached bookmarks.
  156. * @param array $parsed_args An array of bookmark query arguments.
  157. */
  158. return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args );
  159. }
  160. }
  161. if ( ! is_array( $cache ) ) {
  162. $cache = array();
  163. }
  164. $inclusions = '';
  165. if ( ! empty( $parsed_args['include'] ) ) {
  166. $parsed_args['exclude'] = ''; //ignore exclude, category, and category_name params if using include
  167. $parsed_args['category'] = '';
  168. $parsed_args['category_name'] = '';
  169. $inclinks = wp_parse_id_list( $parsed_args['include'] );
  170. if ( count( $inclinks ) ) {
  171. foreach ( $inclinks as $inclink ) {
  172. if ( empty( $inclusions ) ) {
  173. $inclusions = ' AND ( link_id = ' . $inclink . ' ';
  174. } else {
  175. $inclusions .= ' OR link_id = ' . $inclink . ' ';
  176. }
  177. }
  178. }
  179. }
  180. if ( ! empty( $inclusions ) ) {
  181. $inclusions .= ')';
  182. }
  183. $exclusions = '';
  184. if ( ! empty( $parsed_args['exclude'] ) ) {
  185. $exlinks = wp_parse_id_list( $parsed_args['exclude'] );
  186. if ( count( $exlinks ) ) {
  187. foreach ( $exlinks as $exlink ) {
  188. if ( empty( $exclusions ) ) {
  189. $exclusions = ' AND ( link_id <> ' . $exlink . ' ';
  190. } else {
  191. $exclusions .= ' AND link_id <> ' . $exlink . ' ';
  192. }
  193. }
  194. }
  195. }
  196. if ( ! empty( $exclusions ) ) {
  197. $exclusions .= ')';
  198. }
  199. if ( ! empty( $parsed_args['category_name'] ) ) {
  200. $parsed_args['category'] = get_term_by( 'name', $parsed_args['category_name'], 'link_category' );
  201. if ( $parsed_args['category'] ) {
  202. $parsed_args['category'] = $parsed_args['category']->term_id;
  203. } else {
  204. $cache[ $key ] = array();
  205. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  206. /** This filter is documented in wp-includes/bookmark.php */
  207. return apply_filters( 'get_bookmarks', array(), $parsed_args );
  208. }
  209. }
  210. $search = '';
  211. if ( ! empty( $parsed_args['search'] ) ) {
  212. $like = '%' . $wpdb->esc_like( $parsed_args['search'] ) . '%';
  213. $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like );
  214. }
  215. $category_query = '';
  216. $join = '';
  217. if ( ! empty( $parsed_args['category'] ) ) {
  218. $incategories = wp_parse_id_list( $parsed_args['category'] );
  219. if ( count( $incategories ) ) {
  220. foreach ( $incategories as $incat ) {
  221. if ( empty( $category_query ) ) {
  222. $category_query = ' AND ( tt.term_id = ' . $incat . ' ';
  223. } else {
  224. $category_query .= ' OR tt.term_id = ' . $incat . ' ';
  225. }
  226. }
  227. }
  228. }
  229. if ( ! empty( $category_query ) ) {
  230. $category_query .= ") AND taxonomy = 'link_category'";
  231. $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";
  232. }
  233. if ( $parsed_args['show_updated'] ) {
  234. $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ';
  235. } else {
  236. $recently_updated_test = '';
  237. }
  238. $get_updated = ( $parsed_args['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
  239. $orderby = strtolower( $parsed_args['orderby'] );
  240. $length = '';
  241. switch ( $orderby ) {
  242. case 'length':
  243. $length = ', CHAR_LENGTH(link_name) AS length';
  244. break;
  245. case 'rand':
  246. $orderby = 'rand()';
  247. break;
  248. case 'link_id':
  249. $orderby = "$wpdb->links.link_id";
  250. break;
  251. default:
  252. $orderparams = array();
  253. $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
  254. foreach ( explode( ',', $orderby ) as $ordparam ) {
  255. $ordparam = trim( $ordparam );
  256. if ( in_array( 'link_' . $ordparam, $keys ) ) {
  257. $orderparams[] = 'link_' . $ordparam;
  258. } elseif ( in_array( $ordparam, $keys ) ) {
  259. $orderparams[] = $ordparam;
  260. }
  261. }
  262. $orderby = implode( ',', $orderparams );
  263. }
  264. if ( empty( $orderby ) ) {
  265. $orderby = 'link_name';
  266. }
  267. $order = strtoupper( $parsed_args['order'] );
  268. if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
  269. $order = 'ASC';
  270. }
  271. $visible = '';
  272. if ( $parsed_args['hide_invisible'] ) {
  273. $visible = "AND link_visible = 'Y'";
  274. }
  275. $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
  276. $query .= " $exclusions $inclusions $search";
  277. $query .= " ORDER BY $orderby $order";
  278. if ( $parsed_args['limit'] != -1 ) {
  279. $query .= ' LIMIT ' . $parsed_args['limit'];
  280. }
  281. $results = $wpdb->get_results( $query );
  282. if ( 'rand()' !== $orderby ) {
  283. $cache[ $key ] = $results;
  284. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  285. }
  286. /** This filter is documented in wp-includes/bookmark.php */
  287. return apply_filters( 'get_bookmarks', $results, $parsed_args );
  288. }
  289. /**
  290. * Sanitizes all bookmark fields
  291. *
  292. * @since 2.3.0
  293. *
  294. * @param stdClass|array $bookmark Bookmark row
  295. * @param string $context Optional, default is 'display'. How to filter the
  296. * fields
  297. * @return stdClass|array Same type as $bookmark but with fields sanitized.
  298. */
  299. function sanitize_bookmark( $bookmark, $context = 'display' ) {
  300. $fields = array(
  301. 'link_id',
  302. 'link_url',
  303. 'link_name',
  304. 'link_image',
  305. 'link_target',
  306. 'link_category',
  307. 'link_description',
  308. 'link_visible',
  309. 'link_owner',
  310. 'link_rating',
  311. 'link_updated',
  312. 'link_rel',
  313. 'link_notes',
  314. 'link_rss',
  315. );
  316. if ( is_object( $bookmark ) ) {
  317. $do_object = true;
  318. $link_id = $bookmark->link_id;
  319. } else {
  320. $do_object = false;
  321. $link_id = $bookmark['link_id'];
  322. }
  323. foreach ( $fields as $field ) {
  324. if ( $do_object ) {
  325. if ( isset( $bookmark->$field ) ) {
  326. $bookmark->$field = sanitize_bookmark_field( $field, $bookmark->$field, $link_id, $context );
  327. }
  328. } else {
  329. if ( isset( $bookmark[ $field ] ) ) {
  330. $bookmark[ $field ] = sanitize_bookmark_field( $field, $bookmark[ $field ], $link_id, $context );
  331. }
  332. }
  333. }
  334. return $bookmark;
  335. }
  336. /**
  337. * Sanitizes a bookmark field.
  338. *
  339. * Sanitizes the bookmark fields based on what the field name is. If the field
  340. * has a strict value set, then it will be tested for that, else a more generic
  341. * filtering is applied. After the more strict filter is applied, if the `$context`
  342. * is 'raw' then the value is immediately return.
  343. *
  344. * Hooks exist for the more generic cases. With the 'edit' context, the {@see 'edit_$field'}
  345. * filter will be called and passed the `$value` and `$bookmark_id` respectively.
  346. *
  347. * With the 'db' context, the {@see 'pre_$field'} filter is called and passed the value.
  348. * The 'display' context is the final context and has the `$field` has the filter name
  349. * and is passed the `$value`, `$bookmark_id`, and `$context`, respectively.
  350. *
  351. * @since 2.3.0
  352. *
  353. * @param string $field The bookmark field.
  354. * @param mixed $value The bookmark field value.
  355. * @param int $bookmark_id Bookmark ID.
  356. * @param string $context How to filter the field value. Accepts 'raw', 'edit', 'attribute',
  357. * 'js', 'db', or 'display'
  358. * @return mixed The filtered value.
  359. */
  360. function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
  361. switch ( $field ) {
  362. case 'link_id': // ints
  363. case 'link_rating':
  364. $value = (int) $value;
  365. break;
  366. case 'link_category': // array( ints )
  367. $value = array_map( 'absint', (array) $value );
  368. // We return here so that the categories aren't filtered.
  369. // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
  370. return $value;
  371. case 'link_visible': // bool stored as Y|N
  372. $value = preg_replace( '/[^YNyn]/', '', $value );
  373. break;
  374. case 'link_target': // "enum"
  375. $targets = array( '_top', '_blank' );
  376. if ( ! in_array( $value, $targets ) ) {
  377. $value = '';
  378. }
  379. break;
  380. }
  381. if ( 'raw' == $context ) {
  382. return $value;
  383. }
  384. if ( 'edit' == $context ) {
  385. /** This filter is documented in wp-includes/post.php */
  386. $value = apply_filters( "edit_{$field}", $value, $bookmark_id );
  387. if ( 'link_notes' == $field ) {
  388. $value = esc_html( $value ); // textarea_escaped
  389. } else {
  390. $value = esc_attr( $value );
  391. }
  392. } elseif ( 'db' == $context ) {
  393. /** This filter is documented in wp-includes/post.php */
  394. $value = apply_filters( "pre_{$field}", $value );
  395. } else {
  396. /** This filter is documented in wp-includes/post.php */
  397. $value = apply_filters( "{$field}", $value, $bookmark_id, $context );
  398. if ( 'attribute' == $context ) {
  399. $value = esc_attr( $value );
  400. } elseif ( 'js' == $context ) {
  401. $value = esc_js( $value );
  402. }
  403. }
  404. return $value;
  405. }
  406. /**
  407. * Deletes the bookmark cache.
  408. *
  409. * @since 2.7.0
  410. *
  411. * @param int $bookmark_id Bookmark ID.
  412. */
  413. function clean_bookmark_cache( $bookmark_id ) {
  414. wp_cache_delete( $bookmark_id, 'bookmark' );
  415. wp_cache_delete( 'get_bookmarks', 'bookmark' );
  416. clean_object_term_cache( $bookmark_id, 'link' );
  417. }