PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/bookmark.php

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