PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/bookmark.php

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