PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/bookmark.php

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