PageRenderTime 73ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/bookmark.php

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