You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

422 line
13 KiB

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