PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/bookmark.php

https://bitbucket.org/Wallynm/iptb
PHP | 269 lines | 134 code | 53 blank | 82 comment | 43 complexity | d74583b139fffa1ee61614cdf9a4cbf3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * WordPress Bookmark Administration API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Add a link to using values provided in $_POST.
  10. *
  11. * @since 2.0.0
  12. *
  13. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
  14. */
  15. function add_link() {
  16. return edit_link();
  17. }
  18. /**
  19. * Update or insert a link using values provided in $_POST.
  20. *
  21. * @since 2.0.0
  22. *
  23. * @param int $link_id Optional. ID of the link to edit.
  24. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
  25. */
  26. function edit_link( $link_id = 0 ) {
  27. if ( !current_user_can( 'manage_links' ) )
  28. wp_die( __( 'Cheatin&#8217; uh?' ) );
  29. $_POST['link_url'] = esc_html( $_POST['link_url'] );
  30. $_POST['link_url'] = esc_url($_POST['link_url']);
  31. $_POST['link_name'] = esc_html( $_POST['link_name'] );
  32. $_POST['link_image'] = esc_html( $_POST['link_image'] );
  33. $_POST['link_rss'] = esc_url($_POST['link_rss']);
  34. if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
  35. $_POST['link_visible'] = 'Y';
  36. if ( !empty( $link_id ) ) {
  37. $_POST['link_id'] = $link_id;
  38. return wp_update_link( $_POST );
  39. } else {
  40. return wp_insert_link( $_POST );
  41. }
  42. }
  43. /**
  44. * Retrieve the default link for editing.
  45. *
  46. * @since 2.0.0
  47. *
  48. * @return object Default link
  49. */
  50. function get_default_link_to_edit() {
  51. if ( isset( $_GET['linkurl'] ) )
  52. $link->link_url = esc_url( $_GET['linkurl'] );
  53. else
  54. $link->link_url = '';
  55. if ( isset( $_GET['name'] ) )
  56. $link->link_name = esc_attr( $_GET['name'] );
  57. else
  58. $link->link_name = '';
  59. $link->link_visible = 'Y';
  60. return $link;
  61. }
  62. /**
  63. * Delete link specified from database
  64. *
  65. * @since 2.0.0
  66. *
  67. * @param int $link_id ID of the link to delete
  68. * @return bool True
  69. */
  70. function wp_delete_link( $link_id ) {
  71. global $wpdb;
  72. do_action( 'delete_link', $link_id );
  73. wp_delete_object_term_relationships( $link_id, 'link_category' );
  74. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) );
  75. do_action( 'deleted_link', $link_id );
  76. clean_bookmark_cache( $link_id );
  77. return true;
  78. }
  79. /**
  80. * Retrieves the link categories associated with the link specified.
  81. *
  82. * @since 2.1.0
  83. *
  84. * @param int $link_id Link ID to look up
  85. * @return array The requested link's categories
  86. */
  87. function wp_get_link_cats( $link_id = 0 ) {
  88. $cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
  89. return array_unique( $cats );
  90. }
  91. /**
  92. * Retrieve link data based on ID.
  93. *
  94. * @since 2.0.0
  95. *
  96. * @param int $link_id ID of link to retrieve
  97. * @return object Link for editing
  98. */
  99. function get_link_to_edit( $link_id ) {
  100. return get_bookmark( $link_id, OBJECT, 'edit' );
  101. }
  102. /**
  103. * This function inserts/updates links into/in the database.
  104. *
  105. * @since 2.0.0
  106. *
  107. * @param array $linkdata Elements that make up the link to insert.
  108. * @param bool $wp_error Optional. If true return WP_Error object on failure.
  109. * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
  110. */
  111. function wp_insert_link( $linkdata, $wp_error = false ) {
  112. global $wpdb;
  113. $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
  114. $linkdata = wp_parse_args( $linkdata, $defaults );
  115. $linkdata = sanitize_bookmark( $linkdata, 'db' );
  116. extract( stripslashes_deep( $linkdata ), EXTR_SKIP );
  117. $update = false;
  118. if ( !empty( $link_id ) )
  119. $update = true;
  120. if ( trim( $link_name ) == '' ) {
  121. if ( trim( $link_url ) != '' ) {
  122. $link_name = $link_url;
  123. } else {
  124. return 0;
  125. }
  126. }
  127. if ( trim( $link_url ) == '' )
  128. return 0;
  129. if ( empty( $link_rating ) )
  130. $link_rating = 0;
  131. if ( empty( $link_image ) )
  132. $link_image = '';
  133. if ( empty( $link_target ) )
  134. $link_target = '';
  135. if ( empty( $link_visible ) )
  136. $link_visible = 'Y';
  137. if ( empty( $link_owner ) )
  138. $link_owner = get_current_user_id();
  139. if ( empty( $link_notes ) )
  140. $link_notes = '';
  141. if ( empty( $link_description ) )
  142. $link_description = '';
  143. if ( empty( $link_rss ) )
  144. $link_rss = '';
  145. if ( empty( $link_rel ) )
  146. $link_rel = '';
  147. // Make sure we set a valid category
  148. if ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $link_category ) ) {
  149. $link_category = array( get_option( 'default_link_category' ) );
  150. }
  151. if ( $update ) {
  152. 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') ) ) {
  153. if ( $wp_error )
  154. return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
  155. else
  156. return 0;
  157. }
  158. } else {
  159. 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') ) ) {
  160. if ( $wp_error )
  161. return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
  162. else
  163. return 0;
  164. }
  165. $link_id = (int) $wpdb->insert_id;
  166. }
  167. wp_set_link_cats( $link_id, $link_category );
  168. if ( $update )
  169. do_action( 'edit_link', $link_id );
  170. else
  171. do_action( 'add_link', $link_id );
  172. clean_bookmark_cache( $link_id );
  173. return $link_id;
  174. }
  175. /**
  176. * Update link with the specified link categories.
  177. *
  178. * @since 2.1.0
  179. *
  180. * @param int $link_id ID of link to update
  181. * @param array $link_categories Array of categories to
  182. */
  183. function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
  184. // If $link_categories isn't already an array, make it one:
  185. if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
  186. $link_categories = array( get_option( 'default_link_category' ) );
  187. $link_categories = array_map( 'intval', $link_categories );
  188. $link_categories = array_unique( $link_categories );
  189. wp_set_object_terms( $link_id, $link_categories, 'link_category' );
  190. clean_bookmark_cache( $link_id );
  191. }
  192. /**
  193. * Update a link in the database.
  194. *
  195. * @since 2.0.0
  196. *
  197. * @param array $linkdata Link data to update.
  198. * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
  199. */
  200. function wp_update_link( $linkdata ) {
  201. $link_id = (int) $linkdata['link_id'];
  202. $link = get_bookmark( $link_id, ARRAY_A );
  203. // Escape data pulled from DB.
  204. $link = add_magic_quotes( $link );
  205. // Passed link category list overwrites existing category list if not empty.
  206. if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
  207. && 0 != count( $linkdata['link_category'] ) )
  208. $link_cats = $linkdata['link_category'];
  209. else
  210. $link_cats = $link['link_category'];
  211. // Merge old and new fields with new fields overwriting old ones.
  212. $linkdata = array_merge( $link, $linkdata );
  213. $linkdata['link_category'] = $link_cats;
  214. return wp_insert_link( $linkdata );
  215. }
  216. ?>