bookmark.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * Link/Bookmark API
  4. *
  5. * @package WordPress
  6. * @subpackage Bookmark
  7. */
  8. /**
  9. * Retrieves 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
  17. * correspond to an stdClass object, an associative array, or a numeric array,
  18. * respectively. Default OBJECT.
  19. * @param string $filter Optional. How to sanitize bookmark fields. Default 'raw'.
  20. * @return array|object|null Type returned depends on $output value.
  21. */
  22. function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {
  23. global $wpdb;
  24. if ( empty( $bookmark ) ) {
  25. if ( isset( $GLOBALS['link'] ) ) {
  26. $_bookmark = & $GLOBALS['link'];
  27. } else {
  28. $_bookmark = null;
  29. }
  30. } elseif ( is_object( $bookmark ) ) {
  31. wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );
  32. $_bookmark = $bookmark;
  33. } else {
  34. if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id == $bookmark ) ) {
  35. $_bookmark = & $GLOBALS['link'];
  36. } else {
  37. $_bookmark = wp_cache_get( $bookmark, 'bookmark' );
  38. if ( ! $_bookmark ) {
  39. $_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );
  40. if ( $_bookmark ) {
  41. $_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );
  42. wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );
  43. }
  44. }
  45. }
  46. }
  47. if ( ! $_bookmark ) {
  48. return $_bookmark;
  49. }
  50. $_bookmark = sanitize_bookmark( $_bookmark, $filter );
  51. if ( OBJECT === $output ) {
  52. return $_bookmark;
  53. } elseif ( ARRAY_A === $output ) {
  54. return get_object_vars( $_bookmark );
  55. } elseif ( ARRAY_N === $output ) {
  56. return array_values( get_object_vars( $_bookmark ) );
  57. } else {
  58. return $_bookmark;
  59. }
  60. }
  61. /**
  62. * Retrieves single bookmark data item or field.
  63. *
  64. * @since 2.3.0
  65. *
  66. * @param string $field The name of the data field to return.
  67. * @param int $bookmark The bookmark ID to get field.
  68. * @param string $context Optional. The context of how the field will be used.
  69. * @return string|WP_Error
  70. */
  71. function get_bookmark_field( $field, $bookmark, $context = 'display' ) {
  72. $bookmark = (int) $bookmark;
  73. $bookmark = get_bookmark( $bookmark );
  74. if ( is_wp_error( $bookmark ) ) {
  75. return $bookmark;
  76. }
  77. if ( ! is_object( $bookmark ) ) {
  78. return '';
  79. }
  80. if ( ! isset( $bookmark->$field ) ) {
  81. return '';
  82. }
  83. return sanitize_bookmark_field( $field, $bookmark->$field, $bookmark->link_id, $context );
  84. }
  85. /**
  86. * Retrieves the list of bookmarks.
  87. *
  88. * Attempts to retrieve from the cache first based on MD5 hash of arguments. If
  89. * that fails, then the query will be built from the arguments and executed. The
  90. * results will be stored to the cache.
  91. *
  92. * @since 2.1.0
  93. *
  94. * @global wpdb $wpdb WordPress database abstraction object.
  95. *
  96. * @param string|array $args {
  97. * Optional. String or array of arguments to retrieve bookmarks.
  98. *
  99. * @type string $orderby How to order the links by. Accepts 'id', 'link_id', 'name', 'link_name',
  100. * 'url', 'link_url', 'visible', 'link_visible', 'rating', 'link_rating',
  101. * 'owner', 'link_owner', 'updated', 'link_updated', 'notes', 'link_notes',
  102. * 'description', 'link_description', 'length' and 'rand'.
  103. * When `$orderby` is 'length', orders by the character length of
  104. * 'link_name'. Default 'name'.
  105. * @type string $order Whether to order bookmarks in ascending or descending order.
  106. * Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
  107. * @type int $limit Amount of bookmarks to display. Accepts any positive number or
  108. * -1 for all. Default -1.
  109. * @type string $category Comma-separated list of category IDs to include links from.
  110. * Default empty.
  111. * @type string $category_name Category to retrieve links for by name. Default empty.
  112. * @type int|bool $hide_invisible Whether to show or hide links marked as 'invisible'. Accepts
  113. * 1|true or 0|false. Default 1|true.
  114. * @type int|bool $show_updated Whether to display the time the bookmark was last updated.
  115. * Accepts 1|true or 0|false. Default 0|false.
  116. * @type string $include Comma-separated list of bookmark IDs to include. Default empty.
  117. * @type string $exclude Comma-separated list of bookmark IDs to exclude. Default empty.
  118. * @type string $search Search terms. Will be SQL-formatted with wildcards before and after
  119. * and searched in 'link_url', 'link_name' and 'link_description'.
  120. * Default empty.
  121. * }
  122. * @return object[] List of bookmark row objects.
  123. */
  124. function get_bookmarks( $args = '' ) {
  125. global $wpdb;
  126. $defaults = array(
  127. 'orderby' => 'name',
  128. 'order' => 'ASC',
  129. 'limit' => -1,
  130. 'category' => '',
  131. 'category_name' => '',
  132. 'hide_invisible' => 1,
  133. 'show_updated' => 0,
  134. 'include' => '',
  135. 'exclude' => '',
  136. 'search' => '',
  137. );
  138. $parsed_args = wp_parse_args( $args, $defaults );
  139. $key = md5( serialize( $parsed_args ) );
  140. $cache = wp_cache_get( 'get_bookmarks', 'bookmark' );
  141. if ( 'rand' !== $parsed_args['orderby'] && $cache ) {
  142. if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
  143. $bookmarks = $cache[ $key ];
  144. /**
  145. * Filters the returned list of bookmarks.
  146. *
  147. * The first time the hook is evaluated in this file, it returns the cached
  148. * bookmarks list. The second evaluation returns a cached bookmarks list if the
  149. * link category is passed but does not exist. The third evaluation returns
  150. * the full cached results.
  151. *
  152. * @since 2.1.0
  153. *
  154. * @see get_bookmarks()
  155. *
  156. * @param array $bookmarks List of the cached bookmarks.
  157. * @param array $parsed_args An array of bookmark query arguments.
  158. */
  159. return apply_filters( 'get_bookmarks', $bookmarks, $parsed_args );
  160. }
  161. }
  162. if ( ! is_array( $cache ) ) {
  163. $cache = array();
  164. }
  165. $inclusions = '';
  166. if ( ! empty( $parsed_args['include'] ) ) {
  167. $parsed_args['exclude'] = ''; // Ignore exclude, category, and category_name params if using include.
  168. $parsed_args['category'] = '';
  169. $parsed_args['category_name'] = '';
  170. $inclinks = wp_parse_id_list( $parsed_args['include'] );
  171. if ( count( $inclinks ) ) {
  172. foreach ( $inclinks as $inclink ) {
  173. if ( empty( $inclusions ) ) {
  174. $inclusions = ' AND ( link_id = ' . $inclink . ' ';
  175. } else {
  176. $inclusions .= ' OR link_id = ' . $inclink . ' ';
  177. }
  178. }
  179. }
  180. }
  181. if ( ! empty( $inclusions ) ) {
  182. $inclusions .= ')';
  183. }
  184. $exclusions = '';
  185. if ( ! empty( $parsed_args['exclude'] ) ) {
  186. $exlinks = wp_parse_id_list( $parsed_args['exclude'] );
  187. if ( count( $exlinks ) ) {
  188. foreach ( $exlinks as $exlink ) {
  189. if ( empty( $exclusions ) ) {
  190. $exclusions = ' AND ( link_id <> ' . $exlink . ' ';
  191. } else {
  192. $exclusions .= ' AND link_id <> ' . $exlink . ' ';
  193. }
  194. }
  195. }
  196. }
  197. if ( ! empty( $exclusions ) ) {
  198. $exclusions .= ')';
  199. }
  200. if ( ! empty( $parsed_args['category_name'] ) ) {
  201. $parsed_args['category'] = get_term_by( 'name', $parsed_args['category_name'], 'link_category' );
  202. if ( $parsed_args['category'] ) {
  203. $parsed_args['category'] = $parsed_args['category']->term_id;
  204. } else {
  205. $cache[ $key ] = array();
  206. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  207. /** This filter is documented in wp-includes/bookmark.php */
  208. return apply_filters( 'get_bookmarks', array(), $parsed_args );
  209. }
  210. }
  211. $search = '';
  212. if ( ! empty( $parsed_args['search'] ) ) {
  213. $like = '%' . $wpdb->esc_like( $parsed_args['search'] ) . '%';
  214. $search = $wpdb->prepare( ' AND ( (link_url LIKE %s) OR (link_name LIKE %s) OR (link_description LIKE %s) ) ', $like, $like, $like );
  215. }
  216. $category_query = '';
  217. $join = '';
  218. if ( ! empty( $parsed_args['category'] ) ) {
  219. $incategories = wp_parse_id_list( $parsed_args['category'] );
  220. if ( count( $incategories ) ) {
  221. foreach ( $incategories as $incat ) {
  222. if ( empty( $category_query ) ) {
  223. $category_query = ' AND ( tt.term_id = ' . $incat . ' ';
  224. } else {
  225. $category_query .= ' OR tt.term_id = ' . $incat . ' ';
  226. }
  227. }
  228. }
  229. }
  230. if ( ! empty( $category_query ) ) {
  231. $category_query .= ") AND taxonomy = 'link_category'";
  232. $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";
  233. }
  234. if ( $parsed_args['show_updated'] ) {
  235. $recently_updated_test = ', IF (DATE_ADD(link_updated, INTERVAL 120 MINUTE) >= NOW(), 1,0) as recently_updated ';
  236. } else {
  237. $recently_updated_test = '';
  238. }
  239. $get_updated = ( $parsed_args['show_updated'] ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
  240. $orderby = strtolower( $parsed_args['orderby'] );
  241. $length = '';
  242. switch ( $orderby ) {
  243. case 'length':
  244. $length = ', CHAR_LENGTH(link_name) AS length';
  245. break;
  246. case 'rand':
  247. $orderby = 'rand()';
  248. break;
  249. case 'link_id':
  250. $orderby = "$wpdb->links.link_id";
  251. break;
  252. default:
  253. $orderparams = array();
  254. $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes', 'link_description' );
  255. foreach ( explode( ',', $orderby ) as $ordparam ) {
  256. $ordparam = trim( $ordparam );
  257. if ( in_array( 'link_' . $ordparam, $keys, true ) ) {
  258. $orderparams[] = 'link_' . $ordparam;
  259. } elseif ( in_array( $ordparam, $keys, true ) ) {
  260. $orderparams[] = $ordparam;
  261. }
  262. }
  263. $orderby = implode( ',', $orderparams );
  264. }
  265. if ( empty( $orderby ) ) {
  266. $orderby = 'link_name';
  267. }
  268. $order = strtoupper( $parsed_args['order'] );
  269. if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) {
  270. $order = 'ASC';
  271. }
  272. $visible = '';
  273. if ( $parsed_args['hide_invisible'] ) {
  274. $visible = "AND link_visible = 'Y'";
  275. }
  276. $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
  277. $query .= " $exclusions $inclusions $search";
  278. $query .= " ORDER BY $orderby $order";
  279. if ( -1 != $parsed_args['limit'] ) {
  280. $query .= ' LIMIT ' . absint( $parsed_args['limit'] );
  281. }
  282. $results = $wpdb->get_results( $query );
  283. if ( 'rand()' !== $orderby ) {
  284. $cache[ $key ] = $results;
  285. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  286. }
  287. /** This filter is documented in wp-includes/bookmark.php */
  288. return apply_filters( 'get_bookmarks', $results, $parsed_args );
  289. }
  290. /**
  291. * Sanitizes all bookmark fields.
  292. *
  293. * @since 2.3.0
  294. *
  295. * @param stdClass|array $bookmark Bookmark row.
  296. * @param string $context Optional. How to filter the fields. Default 'display'.
  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', 'db',
  357. * 'display', 'attribute', or 'js'. Default 'display'.
  358. * @return mixed The filtered value.
  359. */
  360. function sanitize_bookmark_field( $field, $value, $bookmark_id, $context ) {
  361. $int_fields = array( 'link_id', 'link_rating' );
  362. if ( in_array( $field, $int_fields, true ) ) {
  363. $value = (int) $value;
  364. }
  365. switch ( $field ) {
  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, true ) ) {
  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. // Restore the type for integer fields after esc_attr().
  405. if ( in_array( $field, $int_fields, true ) ) {
  406. $value = (int) $value;
  407. }
  408. return $value;
  409. }
  410. /**
  411. * Deletes the bookmark cache.
  412. *
  413. * @since 2.7.0
  414. *
  415. * @param int $bookmark_id Bookmark ID.
  416. */
  417. function clean_bookmark_cache( $bookmark_id ) {
  418. wp_cache_delete( $bookmark_id, 'bookmark' );
  419. wp_cache_delete( 'get_bookmarks', 'bookmark' );
  420. clean_object_term_cache( $bookmark_id, 'link' );
  421. }