PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/bookmark.php

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