2007-05-25 03:16:21 -04:00
|
|
|
<?php
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* WordPress Bookmark Administration API
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Administration
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
function add_link() {
|
|
|
|
return edit_link();
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $link_id
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
function edit_link( $link_id = '' ) {
|
|
|
|
if (!current_user_can( 'manage_links' ))
|
|
|
|
wp_die( __( 'Cheatin’ uh?' ));
|
|
|
|
|
2009-05-18 11:11:07 -04:00
|
|
|
$_POST['link_url'] = esc_html( $_POST['link_url'] );
|
2009-05-18 12:00:33 -04:00
|
|
|
$_POST['link_url'] = esc_url($_POST['link_url']);
|
2009-05-18 11:11:07 -04:00
|
|
|
$_POST['link_name'] = esc_html( $_POST['link_name'] );
|
|
|
|
$_POST['link_image'] = esc_html( $_POST['link_image'] );
|
2009-05-18 12:00:33 -04:00
|
|
|
$_POST['link_rss'] = esc_url($_POST['link_rss']);
|
2008-11-14 18:01:16 -05:00
|
|
|
if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
|
2008-02-20 03:06:03 -05:00
|
|
|
$_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
function get_default_link_to_edit() {
|
|
|
|
if ( isset( $_GET['linkurl'] ) )
|
2009-05-18 12:00:33 -04:00
|
|
|
$link->link_url = esc_url( $_GET['linkurl']);
|
2007-05-25 03:16:21 -04:00
|
|
|
else
|
|
|
|
$link->link_url = '';
|
|
|
|
|
|
|
|
if ( isset( $_GET['name'] ) )
|
2009-05-05 15:43:53 -04:00
|
|
|
$link->link_name = esc_attr( $_GET['name']);
|
2007-05-25 03:16:21 -04:00
|
|
|
else
|
|
|
|
$link->link_name = '';
|
|
|
|
|
|
|
|
$link->link_visible = 'Y';
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $link_id
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_delete_link( $link_id ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
global $wpdb;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'delete_link', $link_id );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
wp_delete_object_term_relationships( $link_id, 'link_category' );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->links WHERE link_id = %d", $link_id ) );
|
2007-06-13 22:25:30 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'deleted_link', $link_id );
|
2007-05-27 15:43:44 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
clean_bookmark_cache( $link_id );
|
2008-08-27 18:04:12 -04:00
|
|
|
|
2007-05-27 15:43:44 -04:00
|
|
|
return true;
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $link_id
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_get_link_cats( $link_id = 0 ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2009-12-23 10:31:02 -05:00
|
|
|
$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
return array_unique( $cats );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $link_id
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
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-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $linkdata
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_insert_link( $linkdata, $wp_error = false ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
global $wpdb, $current_user;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
$defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
|
2007-08-20 18:50:04 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
$linkdata = wp_parse_args( $linkdata, $defaults );
|
|
|
|
$linkdata = sanitize_bookmark( $linkdata, 'db' );
|
2007-08-20 18:50:04 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
extract( stripslashes_deep( $linkdata ), EXTR_SKIP );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
$update = false;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( !empty( $link_id ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$update = true;
|
|
|
|
|
2009-01-22 17:47:34 -05:00
|
|
|
if ( trim( $link_name ) == '' ) {
|
|
|
|
if ( trim( $link_url ) != '' ) {
|
|
|
|
$link_name = $link_url;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2007-08-20 18:50:04 -04:00
|
|
|
if ( trim( $link_url ) == '' )
|
2007-05-25 03:16:21 -04:00
|
|
|
return 0;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_rating ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_rating = 0;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_image ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_image = '';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_target ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_target = '';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_visible ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_visible = 'Y';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_owner ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_owner = $current_user->id;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_notes ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_notes = '';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_description ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_description = '';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_rss ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_rss = '';
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( empty( $link_rel ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_rel = '';
|
|
|
|
|
|
|
|
// Make sure we set a valid category
|
2008-11-14 18:01:16 -05:00
|
|
|
if ( ! isset( $link_category ) ||0 == count( $link_category ) || !is_array( $link_category ) ) {
|
2008-11-13 02:17:40 -05:00
|
|
|
$link_category = array( get_option( 'default_link_category' ) );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $update ) {
|
2010-02-11 11:17:12 -05:00
|
|
|
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') ) ) {
|
2008-08-27 18:04:12 -04:00
|
|
|
if ( $wp_error )
|
2008-11-13 02:17:40 -05:00
|
|
|
return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
|
2008-08-27 18:04:12 -04:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
} else {
|
2010-02-11 11:17:12 -05:00
|
|
|
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') ) ) {
|
2008-08-27 18:04:12 -04:00
|
|
|
if ( $wp_error )
|
2008-11-13 02:17:40 -05:00
|
|
|
return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
|
2008-08-27 18:04:12 -04:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_id = (int) $wpdb->insert_id;
|
|
|
|
}
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
wp_set_link_cats( $link_id, $link_category );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
if ( $update )
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'edit_link', $link_id );
|
2007-05-25 03:16:21 -04:00
|
|
|
else
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'add_link', $link_id );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
clean_bookmark_cache( $link_id );
|
2008-08-27 18:04:12 -04:00
|
|
|
|
2007-05-25 03:16:21 -04:00
|
|
|
return $link_id;
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $link_id
|
|
|
|
* @param unknown_type $link_categories
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_set_link_cats( $link_id = 0, $link_categories = array() ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
// If $link_categories isn't already an array, make it one:
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( !is_array( $link_categories ) || 0 == count( $link_categories ) )
|
|
|
|
$link_categories = array( get_option( 'default_link_category' ) );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
$link_categories = array_map( 'intval', $link_categories );
|
|
|
|
$link_categories = array_unique( $link_categories );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
wp_set_object_terms( $link_id, $link_categories, 'link_category' );
|
2008-08-27 18:04:12 -04:00
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
clean_bookmark_cache( $link_id );
|
2007-05-25 03:16:21 -04:00
|
|
|
} // wp_set_link_cats()
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
|
|
|
* {@internal Missing Short Description}}
|
|
|
|
*
|
|
|
|
* @since unknown
|
|
|
|
*
|
|
|
|
* @param unknown_type $linkdata
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_update_link( $linkdata ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_id = (int) $linkdata['link_id'];
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
$link = get_link( $link_id, ARRAY_A );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
// Escape data pulled from DB.
|
2008-11-13 02:17:40 -05:00
|
|
|
$link = add_magic_quotes( $link );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
// Passed link category list overwrites existing category list if not empty.
|
2008-11-13 02:17:40 -05:00
|
|
|
if ( isset( $linkdata['link_category'] ) && is_array( $linkdata['link_category'] )
|
|
|
|
&& 0 != count( $linkdata['link_category'] ) )
|
2007-05-25 03:16:21 -04:00
|
|
|
$link_cats = $linkdata['link_category'];
|
|
|
|
else
|
|
|
|
$link_cats = $link['link_category'];
|
|
|
|
|
|
|
|
// Merge old and new fields with new fields overwriting old ones.
|
2008-11-13 02:17:40 -05:00
|
|
|
$linkdata = array_merge( $link, $linkdata );
|
2007-05-25 03:16:21 -04:00
|
|
|
$linkdata['link_category'] = $link_cats;
|
|
|
|
|
2008-11-13 02:17:40 -05:00
|
|
|
return wp_insert_link( $linkdata );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2008-11-14 18:01:16 -05:00
|
|
|
?>
|