/wp-admin/includes/bookmark.php

  1. <?php 
  2. /** 
  3. * WordPress Bookmark Administration API 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. */ 
  8.  
  9. /** 
  10. * Add a link to using values provided in $_POST. 
  11. * 
  12. * @since 2.0.0 
  13. * 
  14. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success. 
  15. */ 
  16. function add_link() { 
  17. return edit_link(); 
  18.  
  19. /** 
  20. * Updates or inserts a link using values provided in $_POST. 
  21. * 
  22. * @since 2.0.0 
  23. * 
  24. * @param int $link_id Optional. ID of the link to edit. Default 0. 
  25. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success. 
  26. */ 
  27. function edit_link( $link_id = 0 ) { 
  28. if ( ! current_user_can( 'manage_links' ) ) { 
  29. wp_die( 
  30. '<h1>' . __( 'Cheatin’ uh?' ) . '</h1>' . 
  31. '<p>' . __( 'Sorry, you are not allowed to edit the links for this site.' ) . '</p>',  
  32. 403 
  33. ); 
  34.  
  35. $_POST['link_url'] = esc_html( $_POST['link_url'] ); 
  36. $_POST['link_url'] = esc_url($_POST['link_url']); 
  37. $_POST['link_name'] = esc_html( $_POST['link_name'] ); 
  38. $_POST['link_image'] = esc_html( $_POST['link_image'] ); 
  39. $_POST['link_rss'] = esc_url($_POST['link_rss']); 
  40. if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] ) 
  41. $_POST['link_visible'] = 'Y'; 
  42.  
  43. if ( !empty( $link_id ) ) { 
  44. $_POST['link_id'] = $link_id; 
  45. return wp_update_link( $_POST ); 
  46. } else { 
  47. return wp_insert_link( $_POST ); 
  48.  
  49. /** 
  50. * Retrieves the default link for editing. 
  51. * 
  52. * @since 2.0.0 
  53. * 
  54. * @return stdClass Default link object. 
  55. */ 
  56. function get_default_link_to_edit() { 
  57. $link = new stdClass; 
  58. if ( isset( $_GET['linkurl'] ) ) 
  59. $link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) ); 
  60. else 
  61. $link->link_url = ''; 
  62.  
  63. if ( isset( $_GET['name'] ) ) 
  64. $link->link_name = esc_attr( wp_unslash( $_GET['name'] ) ); 
  65. else 
  66. $link->link_name = ''; 
  67.  
  68. $link->link_visible = 'Y'; 
  69.  
  70. return $link; 
  71.  
  72. /** 
  73. * Deletes a specified link from the database. 
  74. * 
  75. * @since 2.0.0 
  76. * 
  77. * @global wpdb $wpdb WordPress database abstraction object. 
  78. * 
  79. * @param int $link_id ID of the link to delete 
  80. * @return true Always true. 
  81. */ 
  82. function wp_delete_link( $link_id ) { 
  83. global $wpdb; 
  84. /** 
  85. * Fires before a link is deleted. 
  86. * 
  87. * @since 2.0.0 
  88. * 
  89. * @param int $link_id ID of the link to delete. 
  90. */ 
  91. do_action( 'delete_link', $link_id ); 
  92.  
  93. wp_delete_object_term_relationships( $link_id, 'link_category' ); 
  94.  
  95. $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) ); 
  96.  
  97. /** 
  98. * Fires after a link has been deleted. 
  99. * 
  100. * @since 2.2.0 
  101. * 
  102. * @param int $link_id ID of the deleted link. 
  103. */ 
  104. do_action( 'deleted_link', $link_id ); 
  105.  
  106. clean_bookmark_cache( $link_id ); 
  107.  
  108. return true; 
  109.  
  110. /** 
  111. * Retrieves the link categories associated with the link specified. 
  112. * 
  113. * @since 2.1.0 
  114. * 
  115. * @param int $link_id Link ID to look up 
  116. * @return array The requested link's categories 
  117. */ 
  118. function wp_get_link_cats( $link_id = 0 ) { 
  119. $cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') ); 
  120. return array_unique( $cats ); 
  121.  
  122. /** 
  123. * Retrieves link data based on its ID. 
  124. * 
  125. * @since 2.0.0 
  126. * 
  127. * @param int|stdClass $link Link ID or object to retrieve. 
  128. * @return object Link object for editing. 
  129. */ 
  130. function get_link_to_edit( $link ) { 
  131. return get_bookmark( $link, OBJECT, 'edit' ); 
  132.  
  133. /** 
  134. * Inserts/updates links into/in the database. 
  135. * 
  136. * @since 2.0.0 
  137. * 
  138. * @global wpdb $wpdb WordPress database abstraction object. 
  139. * 
  140. * @param array $linkdata Elements that make up the link to insert. 
  141. * @param bool $wp_error Optional. Whether to return a WP_Error object on failure. Default false. 
  142. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success. 
  143. */ 
  144. function wp_insert_link( $linkdata, $wp_error = false ) { 
  145. global $wpdb; 
  146.  
  147. $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 ); 
  148.  
  149. $args = wp_parse_args( $linkdata, $defaults ); 
  150. $r = wp_unslash( sanitize_bookmark( $args, 'db' ) ); 
  151.  
  152. $link_id = $r['link_id']; 
  153. $link_name = $r['link_name']; 
  154. $link_url = $r['link_url']; 
  155.  
  156. $update = false; 
  157. if ( ! empty( $link_id ) ) { 
  158. $update = true; 
  159.  
  160. if ( trim( $link_name ) == '' ) { 
  161. if ( trim( $link_url ) != '' ) { 
  162. $link_name = $link_url; 
  163. } else { 
  164. return 0; 
  165.  
  166. if ( trim( $link_url ) == '' ) { 
  167. return 0; 
  168.  
  169. $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0; 
  170. $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : ''; 
  171. $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : ''; 
  172. $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y'; 
  173. $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id(); 
  174. $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : ''; 
  175. $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : ''; 
  176. $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : ''; 
  177. $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : ''; 
  178. $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array(); 
  179.  
  180. // Make sure we set a valid category. 
  181. if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) { 
  182. $link_category = array( get_option( 'default_link_category' ) ); 
  183.  
  184. if ( $update ) { 
  185. if ( false === $wpdb->update( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ), compact( 'link_id' ) ) ) { 
  186. if ( $wp_error ) { 
  187. return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error ); 
  188. } else { 
  189. return 0; 
  190. } else { 
  191. if ( false === $wpdb->insert( $wpdb->links, compact( 'link_url', 'link_name', 'link_image', 'link_target', 'link_description', 'link_visible', 'link_owner', 'link_rating', 'link_rel', 'link_notes', 'link_rss' ) ) ) { 
  192. if ( $wp_error ) { 
  193. return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error ); 
  194. } else { 
  195. return 0; 
  196. $link_id = (int) $wpdb->insert_id; 
  197.  
  198. wp_set_link_cats( $link_id, $link_category ); 
  199.  
  200. if ( $update ) { 
  201. /** 
  202. * Fires after a link was updated in the database. 
  203. * 
  204. * @since 2.0.0 
  205. * 
  206. * @param int $link_id ID of the link that was updated. 
  207. */ 
  208. do_action( 'edit_link', $link_id ); 
  209. } else { 
  210. /** 
  211. * Fires after a link was added to the database. 
  212. * 
  213. * @since 2.0.0 
  214. * 
  215. * @param int $link_id ID of the link that was added. 
  216. */ 
  217. do_action( 'add_link', $link_id ); 
  218. clean_bookmark_cache( $link_id ); 
  219.  
  220. return $link_id; 
  221.  
  222. /** 
  223. * Update link with the specified link categories. 
  224. * 
  225. * @since 2.1.0 
  226. * 
  227. * @param int $link_id ID of the link to update. 
  228. * @param array $link_categories Array of link categories to add the link to. 
  229. */ 
  230. function wp_set_link_cats( $link_id = 0, $link_categories = array() ) { 
  231. // If $link_categories isn't already an array, make it one: 
  232. if ( !is_array( $link_categories ) || 0 == count( $link_categories ) ) 
  233. $link_categories = array( get_option( 'default_link_category' ) ); 
  234.  
  235. $link_categories = array_map( 'intval', $link_categories ); 
  236. $link_categories = array_unique( $link_categories ); 
  237.  
  238. wp_set_object_terms( $link_id, $link_categories, 'link_category' ); 
  239.  
  240. clean_bookmark_cache( $link_id ); 
  241.  
  242. /** 
  243. * Updates a link in the database. 
  244. * 
  245. * @since 2.0.0 
  246. * 
  247. * @param array $linkdata Link data to update. 
  248. * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success. 
  249. */ 
  250. function wp_update_link( $linkdata ) { 
  251. $link_id = (int) $linkdata['link_id']; 
  252.  
  253. $link = get_bookmark( $link_id, ARRAY_A ); 
  254.  
  255. // Escape data pulled from DB. 
  256. $link = wp_slash( $link ); 
  257.  
  258. // Passed link category list overwrites existing category list if not empty. 
  259. if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] ) 
  260. && 0 != count( $linkdata['link_category'] ) ) 
  261. $link_cats = $linkdata['link_category']; 
  262. else 
  263. $link_cats = $link['link_category']; 
  264.  
  265. // Merge old and new fields with new fields overwriting old ones. 
  266. $linkdata = array_merge( $link, $linkdata ); 
  267. $linkdata['link_category'] = $link_cats; 
  268.  
  269. return wp_insert_link( $linkdata ); 
  270.  
  271. /** 
  272. * Outputs the 'disabled' message for the WordPress Link Manager. 
  273. * 
  274. * @since 3.5.0 
  275. * @access private 
  276. * 
  277. * @global string $pagenow 
  278. */ 
  279. function wp_link_manager_disabled_message() { 
  280. global $pagenow; 
  281. if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow ) 
  282. return; 
  283.  
  284. add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 ); 
  285. $really_can_manage_links = current_user_can( 'manage_links' ); 
  286. remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 ); 
  287.  
  288. if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) { 
  289. $link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' ); 
  290. wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) ); 
  291.  
  292. wp_die( __( 'Sorry, you are not allowed to edit the links for this site.' ) ); 
.