2007-05-25 03:16:21 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
function add_link() {
|
|
|
|
return edit_link();
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit_link( $link_id = '' ) {
|
|
|
|
if (!current_user_can( 'manage_links' ))
|
|
|
|
wp_die( __( 'Cheatin’ uh?' ));
|
|
|
|
|
|
|
|
$_POST['link_url'] = wp_specialchars( $_POST['link_url'] );
|
|
|
|
$_POST['link_url'] = clean_url($_POST['link_url']);
|
|
|
|
$_POST['link_name'] = wp_specialchars( $_POST['link_name'] );
|
|
|
|
$_POST['link_image'] = wp_specialchars( $_POST['link_image'] );
|
|
|
|
$_POST['link_rss'] = clean_url($_POST['link_rss']);
|
2008-02-20 03:06:03 -05:00
|
|
|
if ( 'N' != $_POST['link_visible'] )
|
|
|
|
$_POST['link_visible'] = 'Y';
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
if ( !empty( $link_id ) ) {
|
|
|
|
$_POST['link_id'] = $link_id;
|
|
|
|
return wp_update_link( $_POST);
|
|
|
|
} else {
|
|
|
|
return wp_insert_link( $_POST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_default_link_to_edit() {
|
|
|
|
if ( isset( $_GET['linkurl'] ) )
|
|
|
|
$link->link_url = clean_url( $_GET['linkurl']);
|
|
|
|
else
|
|
|
|
$link->link_url = '';
|
|
|
|
|
|
|
|
if ( isset( $_GET['name'] ) )
|
|
|
|
$link->link_name = attribute_escape( $_GET['name']);
|
|
|
|
else
|
|
|
|
$link->link_name = '';
|
|
|
|
|
|
|
|
$link->link_visible = 'Y';
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wp_delete_link($link_id) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
do_action('delete_link', $link_id);
|
|
|
|
|
2007-05-27 01:15:18 -04:00
|
|
|
wp_delete_object_term_relationships($link_id, 'link_category');
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-04-14 12:13:25 -04:00
|
|
|
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->links WHERE link_id = %d", $link_id) );
|
2007-06-13 22:25:30 -04:00
|
|
|
|
2007-05-25 03:16:21 -04:00
|
|
|
do_action('deleted_link', $link_id);
|
2007-05-27 15:43:44 -04:00
|
|
|
|
2008-08-27 18:04:12 -04:00
|
|
|
clean_bookmark_cache($link_id);
|
|
|
|
|
2007-05-27 15:43:44 -04:00
|
|
|
return true;
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function wp_get_link_cats($link_id = 0) {
|
|
|
|
|
2007-06-18 20:33:44 -04:00
|
|
|
$cats = wp_get_object_terms($link_id, 'link_category', 'fields=ids');
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
return array_unique($cats);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_link_to_edit( $link_id ) {
|
2008-03-10 01:28:21 -04:00
|
|
|
return get_bookmark( $link_id, OBJECT, 'edit' );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2008-08-27 18:04:12 -04:00
|
|
|
function wp_insert_link($linkdata, $wp_error = false) {
|
2007-05-25 03:16:21 -04:00
|
|
|
global $wpdb, $current_user;
|
|
|
|
|
2007-08-20 18:50:04 -04:00
|
|
|
$defaults = array('link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
|
|
|
|
|
|
|
|
$linkdata = wp_parse_args($linkdata, $defaults);
|
|
|
|
$linkdata = sanitize_bookmark($linkdata, 'db');
|
|
|
|
|
2008-04-16 01:55:27 -04:00
|
|
|
extract(stripslashes_deep($linkdata), EXTR_SKIP);
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
$update = false;
|
|
|
|
|
|
|
|
if ( !empty($link_id) )
|
|
|
|
$update = true;
|
|
|
|
|
2007-08-20 18:50:04 -04:00
|
|
|
if ( trim( $link_name ) == '' )
|
2007-05-25 03:16:21 -04:00
|
|
|
return 0;
|
|
|
|
|
2007-08-20 18:50:04 -04:00
|
|
|
if ( trim( $link_url ) == '' )
|
2007-05-25 03:16:21 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( empty($link_rating) )
|
|
|
|
$link_rating = 0;
|
|
|
|
|
|
|
|
if ( empty($link_image) )
|
|
|
|
$link_image = '';
|
|
|
|
|
|
|
|
if ( empty($link_target) )
|
|
|
|
$link_target = '';
|
|
|
|
|
|
|
|
if ( empty($link_visible) )
|
|
|
|
$link_visible = 'Y';
|
|
|
|
|
|
|
|
if ( empty($link_owner) )
|
|
|
|
$link_owner = $current_user->id;
|
|
|
|
|
|
|
|
if ( empty($link_notes) )
|
|
|
|
$link_notes = '';
|
|
|
|
|
|
|
|
if ( empty($link_description) )
|
|
|
|
$link_description = '';
|
|
|
|
|
|
|
|
if ( empty($link_rss) )
|
|
|
|
$link_rss = '';
|
|
|
|
|
|
|
|
if ( empty($link_rel) )
|
|
|
|
$link_rel = '';
|
|
|
|
|
|
|
|
// Make sure we set a valid category
|
|
|
|
if (0 == count($link_category) || !is_array($link_category)) {
|
|
|
|
$link_category = array(get_option('default_link_category'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $update ) {
|
2008-08-27 18:04:12 -04:00
|
|
|
if ( false === $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_url = %s,
|
2008-08-09 01:36:14 -04:00
|
|
|
link_name = %s, link_image = %s, link_target = %s,
|
|
|
|
link_visible = %s, link_description = %s, link_rating = %s,
|
2008-04-14 12:13:25 -04:00
|
|
|
link_rel = %s, link_notes = %s, link_rss = %s
|
2008-08-27 18:04:12 -04:00
|
|
|
WHERE link_id = %s", $link_url, $link_name, $link_image, $link_target, $link_visible, $link_description, $link_rating, $link_rel, $link_notes, $link_rss, $link_id) ) ) {
|
|
|
|
if ( $wp_error )
|
|
|
|
return new WP_Error('db_update_error', __('Could not update link in the database'), $wpdb->last_error);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
} else {
|
2008-08-27 18:04:12 -04:00
|
|
|
if ( false === $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
|
|
|
$link_url,$link_name, $link_image, $link_target, $link_description, $link_visible, $link_owner, $link_rating, $link_rel, $link_notes, $link_rss) ) ) {
|
|
|
|
if ( $wp_error )
|
|
|
|
return new WP_Error('db_insert_error', __('Could not insert link into the database'), $wpdb->last_error);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_id = (int) $wpdb->insert_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_set_link_cats($link_id, $link_category);
|
|
|
|
|
|
|
|
if ( $update )
|
|
|
|
do_action('edit_link', $link_id);
|
|
|
|
else
|
|
|
|
do_action('add_link', $link_id);
|
|
|
|
|
2008-08-27 18:04:12 -04:00
|
|
|
clean_bookmark_cache($link_id);
|
|
|
|
|
2007-05-25 03:16:21 -04:00
|
|
|
return $link_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wp_set_link_cats($link_id = 0, $link_categories = array()) {
|
|
|
|
// If $link_categories isn't already an array, make it one:
|
|
|
|
if (!is_array($link_categories) || 0 == count($link_categories))
|
|
|
|
$link_categories = array(get_option('default_link_category'));
|
|
|
|
|
|
|
|
$link_categories = array_map('intval', $link_categories);
|
|
|
|
$link_categories = array_unique($link_categories);
|
|
|
|
|
|
|
|
wp_set_object_terms($link_id, $link_categories, 'link_category');
|
2008-08-27 18:04:12 -04:00
|
|
|
|
|
|
|
clean_bookmark_cache($link_id);
|
2007-05-25 03:16:21 -04:00
|
|
|
} // wp_set_link_cats()
|
|
|
|
|
|
|
|
function wp_update_link($linkdata) {
|
|
|
|
$link_id = (int) $linkdata['link_id'];
|
|
|
|
|
|
|
|
$link = get_link($link_id, ARRAY_A);
|
|
|
|
|
|
|
|
// Escape data pulled from DB.
|
|
|
|
$link = add_magic_quotes($link);
|
|
|
|
|
|
|
|
// Passed link category list overwrites existing category list if not empty.
|
|
|
|
if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
|
|
|
|
&& 0 != count($linkdata['link_category']) )
|
|
|
|
$link_cats = $linkdata['link_category'];
|
|
|
|
else
|
|
|
|
$link_cats = $link['link_category'];
|
|
|
|
|
|
|
|
// Merge old and new fields with new fields overwriting old ones.
|
|
|
|
$linkdata = array_merge($link, $linkdata);
|
|
|
|
$linkdata['link_category'] = $link_cats;
|
|
|
|
|
|
|
|
return wp_insert_link($linkdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|