PageRenderTime 30ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-admin/includes/bookmark.php

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