3 * WordPress Bookmark Administration API
6 * @subpackage Administration
10 * Add a link to using values provided in $_POST.
14 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
21 * Update or insert a link using values provided in $_POST.
25 * @param int $link_id Optional. ID of the link to edit.
26 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
28 function edit_link( $link_id = 0 ) {
29 if ( !current_user_can( 'manage_links' ) )
30 wp_die( __( 'Cheatin’ uh?' ), 403 );
32 $_POST['link_url'] = esc_html( $_POST['link_url'] );
33 $_POST['link_url'] = esc_url($_POST['link_url']);
34 $_POST['link_name'] = esc_html( $_POST['link_name'] );
35 $_POST['link_image'] = esc_html( $_POST['link_image'] );
36 $_POST['link_rss'] = esc_url($_POST['link_rss']);
37 if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
38 $_POST['link_visible'] = 'Y';
40 if ( !empty( $link_id ) ) {
41 $_POST['link_id'] = $link_id;
42 return wp_update_link( $_POST );
44 return wp_insert_link( $_POST );
49 * Retrieve the default link for editing.
53 * @return stdClass Default link
55 function get_default_link_to_edit() {
57 if ( isset( $_GET['linkurl'] ) )
58 $link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
62 if ( isset( $_GET['name'] ) )
63 $link->link_name = esc_attr( wp_unslash( $_GET['name'] ) );
65 $link->link_name = '';
67 $link->link_visible = 'Y';
73 * Delete link specified from database.
79 * @param int $link_id ID of the link to delete
82 function wp_delete_link( $link_id ) {
85 * Fires before a link is deleted.
89 * @param int $link_id ID of the link to delete.
91 do_action( 'delete_link', $link_id );
93 wp_delete_object_term_relationships( $link_id, 'link_category' );
95 $wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
97 * Fires after a link has been deleted.
101 * @param int $link_id ID of the deleted link.
103 do_action( 'deleted_link', $link_id );
105 clean_bookmark_cache( $link_id );
111 * Retrieves the link categories associated with the link specified.
115 * @param int $link_id Link ID to look up
116 * @return array The requested link's categories
118 function wp_get_link_cats( $link_id = 0 ) {
119 $cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
120 return array_unique( $cats );
124 * Retrieve link data based on ID.
128 * @param int $link_id ID of link to retrieve
129 * @return object Link for editing
131 function get_link_to_edit( $link_id ) {
132 return get_bookmark( $link_id, OBJECT, 'edit' );
136 * This function inserts/updates links into/in the database.
142 * @param array $linkdata Elements that make up the link to insert.
143 * @param bool $wp_error Optional. If true return WP_Error object on failure.
144 * @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
146 function wp_insert_link( $linkdata, $wp_error = false ) {
149 $defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
151 $args = wp_parse_args( $linkdata, $defaults );
152 $r = wp_unslash( sanitize_bookmark( $args, 'db' ) );
154 $link_id = $r['link_id'];
155 $link_name = $r['link_name'];
156 $link_url = $r['link_url'];
159 if ( ! empty( $link_id ) ) {
163 if ( trim( $link_name ) == '' ) {
164 if ( trim( $link_url ) != '' ) {
165 $link_name = $link_url;
171 if ( trim( $link_url ) == '' ) {
175 $link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0;
176 $link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : '';
177 $link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : '';
178 $link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y';
179 $link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id();
180 $link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : '';
181 $link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
182 $link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
183 $link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
184 $link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();
186 // Make sure we set a valid category
187 if ( ! is_array( $link_category ) || 0 == count( $link_category ) ) {
188 $link_category = array( get_option( 'default_link_category' ) );
192 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' ) ) ) {
194 return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
200 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' ) ) ) {
202 return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
207 $link_id = (int) $wpdb->insert_id;
210 wp_set_link_cats( $link_id, $link_category );
214 * Fires after a link was updated in the database.
218 * @param int $link_id ID of the link that was updated.
220 do_action( 'edit_link', $link_id );
223 * Fires after a link was added to the database.
227 * @param int $link_id ID of the link that was added.
229 do_action( 'add_link', $link_id );
231 clean_bookmark_cache( $link_id );
237 * Update link with the specified link categories.
241 * @param int $link_id ID of link to update
242 * @param array $link_categories Array of categories to
244 function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
245 // If $link_categories isn't already an array, make it one:
246 if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
247 $link_categories = array( get_option( 'default_link_category' ) );
249 $link_categories = array_map( 'intval', $link_categories );
250 $link_categories = array_unique( $link_categories );
252 wp_set_object_terms( $link_id, $link_categories, 'link_category' );
254 clean_bookmark_cache( $link_id );
258 * Update a link in the database.
262 * @param array $linkdata Link data to update.
263 * @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
265 function wp_update_link( $linkdata ) {
266 $link_id = (int) $linkdata['link_id'];
268 $link = get_bookmark( $link_id, ARRAY_A );
270 // Escape data pulled from DB.
271 $link = wp_slash( $link );
273 // Passed link category list overwrites existing category list if not empty.
274 if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
275 && 0 != count( $linkdata['link_category'] ) )
276 $link_cats = $linkdata['link_category'];
278 $link_cats = $link['link_category'];
280 // Merge old and new fields with new fields overwriting old ones.
281 $linkdata = array_merge( $link, $linkdata );
282 $linkdata['link_category'] = $link_cats;
284 return wp_insert_link( $linkdata );
291 * @global string $pagenow
293 function wp_link_manager_disabled_message() {
295 if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow )
298 add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
299 $really_can_manage_links = current_user_can( 'manage_links' );
300 remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
302 if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
303 $link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' );
304 wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
307 wp_die( __( 'You do not have sufficient permissions to edit the links for this site.' ) );