PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/bookmark.php

https://gitlab.com/Blueprint-Marketing/WordPress-1
PHP | 408 lines | 242 code | 41 blank | 125 comment | 71 complexity | 9c95612202c6e5be86301285074204de 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. * @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. $bookmarks = $cache[ $key ];
  126. /**
  127. * Filter the returned list of bookmarks.
  128. *
  129. * The first time the hook is evaluated in this file, it returns the cached
  130. * bookmarks list. The second evaluation returns a cached bookmarks list if the
  131. * link category is passed but does not exist. The third evaluation returns
  132. * the full cached results.
  133. *
  134. * @since 2.1.0
  135. *
  136. * @see get_bookmarks()
  137. *
  138. * @param array $bookmarks List of the cached bookmarks.
  139. * @param array $r An array of bookmark query arguments.
  140. */
  141. return apply_filters( 'get_bookmarks', $bookmarks, $r );
  142. }
  143. }
  144. if ( !is_array($cache) )
  145. $cache = array();
  146. $inclusions = '';
  147. if ( !empty($include) ) {
  148. $exclude = ''; //ignore exclude, category, and category_name params if using include
  149. $category = '';
  150. $category_name = '';
  151. $inclinks = preg_split('/[\s,]+/',$include);
  152. if ( count($inclinks) ) {
  153. foreach ( $inclinks as $inclink ) {
  154. if (empty($inclusions))
  155. $inclusions = ' AND ( link_id = ' . intval($inclink) . ' ';
  156. else
  157. $inclusions .= ' OR link_id = ' . intval($inclink) . ' ';
  158. }
  159. }
  160. }
  161. if (!empty($inclusions))
  162. $inclusions .= ')';
  163. $exclusions = '';
  164. if ( !empty($exclude) ) {
  165. $exlinks = preg_split('/[\s,]+/',$exclude);
  166. if ( count($exlinks) ) {
  167. foreach ( $exlinks as $exlink ) {
  168. if (empty($exclusions))
  169. $exclusions = ' AND ( link_id <> ' . intval($exlink) . ' ';
  170. else
  171. $exclusions .= ' AND link_id <> ' . intval($exlink) . ' ';
  172. }
  173. }
  174. }
  175. if (!empty($exclusions))
  176. $exclusions .= ')';
  177. if ( !empty($category_name) ) {
  178. if ( $category = get_term_by('name', $category_name, 'link_category') ) {
  179. $category = $category->term_id;
  180. } else {
  181. $cache[ $key ] = array();
  182. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  183. /** This filter is documented in wp-includes/bookmark.php */
  184. return apply_filters( 'get_bookmarks', array(), $r );
  185. }
  186. }
  187. if ( ! empty($search) ) {
  188. $search = esc_sql( like_escape( $search ) );
  189. $search = " AND ( (link_url LIKE '%$search%') OR (link_name LIKE '%$search%') OR (link_description LIKE '%$search%') ) ";
  190. }
  191. $category_query = '';
  192. $join = '';
  193. if ( !empty($category) ) {
  194. $incategories = preg_split('/[\s,]+/',$category);
  195. if ( count($incategories) ) {
  196. foreach ( $incategories as $incat ) {
  197. if (empty($category_query))
  198. $category_query = ' AND ( tt.term_id = ' . intval($incat) . ' ';
  199. else
  200. $category_query .= ' OR tt.term_id = ' . intval($incat) . ' ';
  201. }
  202. }
  203. }
  204. if (!empty($category_query)) {
  205. $category_query .= ") AND taxonomy = 'link_category'";
  206. $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";
  207. }
  208. if ( $show_updated && get_option('links_recently_updated_time') ) {
  209. $recently_updated_test = ", IF (DATE_ADD(link_updated, INTERVAL " . get_option('links_recently_updated_time') . " MINUTE) >= NOW(), 1,0) as recently_updated ";
  210. } else {
  211. $recently_updated_test = '';
  212. }
  213. $get_updated = ( $show_updated ) ? ', UNIX_TIMESTAMP(link_updated) AS link_updated_f ' : '';
  214. $orderby = strtolower($orderby);
  215. $length = '';
  216. switch ( $orderby ) {
  217. case 'length':
  218. $length = ", CHAR_LENGTH(link_name) AS length";
  219. break;
  220. case 'rand':
  221. $orderby = 'rand()';
  222. break;
  223. case 'link_id':
  224. $orderby = "$wpdb->links.link_id";
  225. break;
  226. default:
  227. $orderparams = array();
  228. foreach ( explode(',', $orderby) as $ordparam ) {
  229. $ordparam = trim($ordparam);
  230. $keys = array( 'link_id', 'link_name', 'link_url', 'link_visible', 'link_rating', 'link_owner', 'link_updated', 'link_notes' );
  231. if ( in_array( 'link_' . $ordparam, $keys ) )
  232. $orderparams[] = 'link_' . $ordparam;
  233. elseif ( in_array( $ordparam, $keys ) )
  234. $orderparams[] = $ordparam;
  235. }
  236. $orderby = implode(',', $orderparams);
  237. }
  238. if ( empty( $orderby ) )
  239. $orderby = 'link_name';
  240. $order = strtoupper( $order );
  241. if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
  242. $order = 'ASC';
  243. $visible = '';
  244. if ( $hide_invisible )
  245. $visible = "AND link_visible = 'Y'";
  246. $query = "SELECT * $length $recently_updated_test $get_updated FROM $wpdb->links $join WHERE 1=1 $visible $category_query";
  247. $query .= " $exclusions $inclusions $search";
  248. $query .= " ORDER BY $orderby $order";
  249. if ($limit != -1)
  250. $query .= " LIMIT $limit";
  251. $results = $wpdb->get_results($query);
  252. $cache[ $key ] = $results;
  253. wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
  254. /** This filter is documented in wp-includes/bookmark.php */
  255. return apply_filters( 'get_bookmarks', $results, $r );
  256. }
  257. /**
  258. * Sanitizes all bookmark fields
  259. *
  260. * @since 2.3.0
  261. *
  262. * @param object|array $bookmark Bookmark row
  263. * @param string $context Optional, default is 'display'. How to filter the
  264. * fields
  265. * @return object|array Same type as $bookmark but with fields sanitized.
  266. */
  267. function sanitize_bookmark($bookmark, $context = 'display') {
  268. $fields = array('link_id', 'link_url', 'link_name', 'link_image', 'link_target', 'link_category',
  269. 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_updated',
  270. 'link_rel', 'link_notes', 'link_rss', );
  271. if ( is_object($bookmark) ) {
  272. $do_object = true;
  273. $link_id = $bookmark->link_id;
  274. } else {
  275. $do_object = false;
  276. $link_id = $bookmark['link_id'];
  277. }
  278. foreach ( $fields as $field ) {
  279. if ( $do_object ) {
  280. if ( isset($bookmark->$field) )
  281. $bookmark->$field = sanitize_bookmark_field($field, $bookmark->$field, $link_id, $context);
  282. } else {
  283. if ( isset($bookmark[$field]) )
  284. $bookmark[$field] = sanitize_bookmark_field($field, $bookmark[$field], $link_id, $context);
  285. }
  286. }
  287. return $bookmark;
  288. }
  289. /**
  290. * Sanitizes a bookmark field
  291. *
  292. * Sanitizes the bookmark fields based on what the field name is. If the field
  293. * has a strict value set, then it will be tested for that, else a more generic
  294. * filtering is applied. After the more strict filter is applied, if the
  295. * $context is 'raw' then the value is immediately return.
  296. *
  297. * Hooks exist for the more generic cases. With the 'edit' context, the
  298. * 'edit_$field' filter will be called and passed the $value and $bookmark_id
  299. * respectively. With the 'db' context, the 'pre_$field' filter is called and
  300. * passed the value. The 'display' context is the final context and has the
  301. * $field has the filter name and is passed the $value, $bookmark_id, and
  302. * $context respectively.
  303. *
  304. * @since 2.3.0
  305. *
  306. * @param string $field The bookmark field
  307. * @param mixed $value The bookmark field value
  308. * @param int $bookmark_id Bookmark ID
  309. * @param string $context How to filter the field value. Either 'raw', 'edit',
  310. * 'attribute', 'js', 'db', or 'display'
  311. * @return mixed The filtered value
  312. */
  313. function sanitize_bookmark_field($field, $value, $bookmark_id, $context) {
  314. switch ( $field ) {
  315. case 'link_id' : // ints
  316. case 'link_rating' :
  317. $value = (int) $value;
  318. break;
  319. case 'link_category' : // array( ints )
  320. $value = array_map('absint', (array) $value);
  321. // We return here so that the categories aren't filtered.
  322. // The 'link_category' filter is for the name of a link category, not an array of a link's link categories
  323. return $value;
  324. break;
  325. case 'link_visible' : // bool stored as Y|N
  326. $value = preg_replace('/[^YNyn]/', '', $value);
  327. break;
  328. case 'link_target' : // "enum"
  329. $targets = array('_top', '_blank');
  330. if ( ! in_array($value, $targets) )
  331. $value = '';
  332. break;
  333. }
  334. if ( 'raw' == $context )
  335. return $value;
  336. if ( 'edit' == $context ) {
  337. /** This filter is documented in wp-includes/post.php */
  338. $value = apply_filters( "edit_$field", $value, $bookmark_id );
  339. if ( 'link_notes' == $field ) {
  340. $value = esc_html( $value ); // textarea_escaped
  341. } else {
  342. $value = esc_attr($value);
  343. }
  344. } else if ( 'db' == $context ) {
  345. /** This filter is documented in wp-includes/post.php */
  346. $value = apply_filters( "pre_$field", $value );
  347. } else {
  348. /** This filter is documented in wp-includes/post.php */
  349. $value = apply_filters( $field, $value, $bookmark_id, $context );
  350. if ( 'attribute' == $context )
  351. $value = esc_attr($value);
  352. else if ( 'js' == $context )
  353. $value = esc_js($value);
  354. }
  355. return $value;
  356. }
  357. /**
  358. * Deletes bookmark cache
  359. *
  360. * @since 2.7.0
  361. * @uses wp_cache_delete() Deletes the contents of 'get_bookmarks'
  362. */
  363. function clean_bookmark_cache( $bookmark_id ) {
  364. wp_cache_delete( $bookmark_id, 'bookmark' );
  365. wp_cache_delete( 'get_bookmarks', 'bookmark' );
  366. clean_object_term_cache( $bookmark_id, 'link');
  367. }