PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/wp-includes/bookmark.php

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