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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Add a link to using values provided in $_POST.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
function add_link() {
|
|
|
|
return edit_link();
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Update or insert a link using values provided in $_POST.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param int $link_id Optional. ID of the link to edit.
|
|
|
|
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2010-09-04 22:45:39 -04:00
|
|
|
function edit_link( $link_id = 0 ) {
|
|
|
|
if ( !current_user_can( 'manage_links' ) )
|
2014-11-16 01:16:22 -05:00
|
|
|
wp_die( __( 'Cheatin’ uh?' ), 403 );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
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;
|
2013-03-01 11:28:40 -05:00
|
|
|
return wp_update_link( $_POST );
|
2007-05-25 03:16:21 -04:00
|
|
|
} else {
|
2013-03-01 11:28:40 -05:00
|
|
|
return wp_insert_link( $_POST );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Retrieve the default link for editing.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2014-11-03 02:09:23 -05:00
|
|
|
* @return stdClass Default link
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2007-05-25 03:16:21 -04:00
|
|
|
function get_default_link_to_edit() {
|
2012-03-22 01:07:27 -04:00
|
|
|
$link = new stdClass;
|
2007-05-25 03:16:21 -04:00
|
|
|
if ( isset( $_GET['linkurl'] ) )
|
2013-03-01 12:00:25 -05:00
|
|
|
$link->link_url = esc_url( wp_unslash( $_GET['linkurl'] ) );
|
2007-05-25 03:16:21 -04:00
|
|
|
else
|
|
|
|
$link->link_url = '';
|
|
|
|
|
|
|
|
if ( isset( $_GET['name'] ) )
|
2013-03-01 12:00:25 -05:00
|
|
|
$link->link_name = esc_attr( wp_unslash( $_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
|
|
|
/**
|
2013-10-06 22:19:09 -04:00
|
|
|
* Delete link specified from database.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2015-05-28 17:41:30 -04:00
|
|
|
* @global wpdb $wpdb
|
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param int $link_id ID of the link to delete
|
2015-05-29 16:17:26 -04:00
|
|
|
* @return true
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_delete_link( $link_id ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
global $wpdb;
|
2013-10-06 22:19:09 -04:00
|
|
|
/**
|
|
|
|
* Fires before a link is deleted.
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
*
|
|
|
|
* @param int $link_id ID of the link to delete.
|
|
|
|
*/
|
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
|
|
|
|
2012-03-24 11:24:31 -04:00
|
|
|
$wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
|
2013-10-06 22:19:09 -04:00
|
|
|
/**
|
|
|
|
* Fires after a link has been deleted.
|
|
|
|
*
|
|
|
|
* @since 2.2.0
|
|
|
|
*
|
|
|
|
* @param int $link_id ID of the deleted link.
|
|
|
|
*/
|
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
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Retrieves the link categories associated with the link specified.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.1.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param int $link_id Link ID to look up
|
|
|
|
* @return array The requested link's categories
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_get_link_cats( $link_id = 0 ) {
|
2009-12-23 10:31:02 -05:00
|
|
|
$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
|
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
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Retrieve link data based on ID.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param int $link_id ID of link to retrieve
|
|
|
|
* @return object Link for editing
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
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
|
|
|
/**
|
2010-10-19 03:48:22 -04:00
|
|
|
* This function inserts/updates links into/in the database.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2015-05-28 17:41:30 -04:00
|
|
|
* @global wpdb $wpdb
|
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param array $linkdata Elements that make up the link to insert.
|
2015-05-29 16:17:26 -04:00
|
|
|
* @param bool $wp_error Optional. If true return WP_Error object on failure.
|
2010-09-04 22:45:39 -04:00
|
|
|
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
function wp_insert_link( $linkdata, $wp_error = false ) {
|
2010-06-24 11:01:29 -04:00
|
|
|
global $wpdb;
|
2007-05-25 03:16:21 -04:00
|
|
|
|
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
|
|
|
|
2014-05-14 22:28:14 -04:00
|
|
|
$args = wp_parse_args( $linkdata, $defaults );
|
2014-05-18 05:56:15 -04:00
|
|
|
$r = wp_unslash( sanitize_bookmark( $args, 'db' ) );
|
|
|
|
|
|
|
|
$link_id = $r['link_id'];
|
|
|
|
$link_name = $r['link_name'];
|
|
|
|
$link_url = $r['link_url'];
|
2007-08-20 18:50:04 -04:00
|
|
|
|
2007-05-25 03:16:21 -04:00
|
|
|
$update = false;
|
2014-05-18 05:56:15 -04:00
|
|
|
if ( ! empty( $link_id ) ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
$update = true;
|
2014-05-14 22:28:14 -04:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2014-05-18 05:56:15 -04:00
|
|
|
if ( trim( $link_name ) == '' ) {
|
|
|
|
if ( trim( $link_url ) != '' ) {
|
|
|
|
$link_name = $link_url;
|
2009-01-22 17:47:34 -05:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2014-05-18 05:56:15 -04:00
|
|
|
if ( trim( $link_url ) == '' ) {
|
2007-05-25 03:16:21 -04:00
|
|
|
return 0;
|
2014-05-14 22:28:14 -04:00
|
|
|
}
|
2014-05-18 05:56:15 -04:00
|
|
|
|
|
|
|
$link_rating = ( ! empty( $r['link_rating'] ) ) ? $r['link_rating'] : 0;
|
|
|
|
$link_image = ( ! empty( $r['link_image'] ) ) ? $r['link_image'] : '';
|
|
|
|
$link_target = ( ! empty( $r['link_target'] ) ) ? $r['link_target'] : '';
|
|
|
|
$link_visible = ( ! empty( $r['link_visible'] ) ) ? $r['link_visible'] : 'Y';
|
|
|
|
$link_owner = ( ! empty( $r['link_owner'] ) ) ? $r['link_owner'] : get_current_user_id();
|
|
|
|
$link_notes = ( ! empty( $r['link_notes'] ) ) ? $r['link_notes'] : '';
|
|
|
|
$link_description = ( ! empty( $r['link_description'] ) ) ? $r['link_description'] : '';
|
|
|
|
$link_rss = ( ! empty( $r['link_rss'] ) ) ? $r['link_rss'] : '';
|
|
|
|
$link_rel = ( ! empty( $r['link_rel'] ) ) ? $r['link_rel'] : '';
|
|
|
|
$link_category = ( ! empty( $r['link_category'] ) ) ? $r['link_category'] : array();
|
2007-05-25 03:16:21 -04:00
|
|
|
|
|
|
|
// Make sure we set a valid category
|
2014-05-18 05:56:15 -04:00
|
|
|
if ( ! is_array( $link_category ) || 0 == count( $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 ) {
|
2014-05-14 22:28:14 -04: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' ) ) ) {
|
|
|
|
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 );
|
2014-05-14 22:28:14 -04:00
|
|
|
} else {
|
2008-08-27 18:04:12 -04:00
|
|
|
return 0;
|
2014-05-14 22:28:14 -04:00
|
|
|
}
|
2008-08-27 18:04:12 -04:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
} else {
|
2014-05-14 22:28:14 -04: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' ) ) ) {
|
|
|
|
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 );
|
2014-05-14 22:28:14 -04:00
|
|
|
} else {
|
2008-08-27 18:04:12 -04:00
|
|
|
return 0;
|
2014-05-14 22:28:14 -04:00
|
|
|
}
|
2008-08-27 18:04:12 -04:00
|
|
|
}
|
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
|
|
|
|
2013-10-06 22:19:09 -04:00
|
|
|
if ( $update ) {
|
|
|
|
/**
|
|
|
|
* Fires after a link was updated in the database.
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
*
|
|
|
|
* @param int $link_id ID of the link that was updated.
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'edit_link', $link_id );
|
2013-10-06 22:19:09 -04:00
|
|
|
} else {
|
|
|
|
/**
|
|
|
|
* Fires after a link was added to the database.
|
|
|
|
*
|
|
|
|
* @since 2.0.0
|
|
|
|
*
|
|
|
|
* @param int $link_id ID of the link that was added.
|
|
|
|
*/
|
2008-11-13 02:17:40 -05:00
|
|
|
do_action( 'add_link', $link_id );
|
2013-10-06 22:19:09 -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
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Update link with the specified link categories.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.1.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param int $link_id ID of link to update
|
2010-10-19 03:48:22 -04:00
|
|
|
* @param array $link_categories Array of categories to
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
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 );
|
2010-09-04 22:45:39 -04:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2008-10-01 21:03:26 -04:00
|
|
|
/**
|
2010-09-04 22:45:39 -04:00
|
|
|
* Update a link in the database.
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @since 2.0.0
|
2008-10-01 21:03:26 -04:00
|
|
|
*
|
2010-09-04 22:45:39 -04:00
|
|
|
* @param array $linkdata Link data to update.
|
|
|
|
* @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
|
2008-10-01 21:03:26 -04:00
|
|
|
*/
|
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'];
|
|
|
|
|
2010-02-13 05:35:10 -05:00
|
|
|
$link = get_bookmark( $link_id, ARRAY_A );
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2013-03-01 11:28:40 -05:00
|
|
|
// Escape data pulled from DB.
|
2013-03-01 12:00:25 -05:00
|
|
|
$link = wp_slash( $link );
|
2013-03-01 11:28:40 -05:00
|
|
|
|
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
|
|
|
}
|
2012-11-26 19:20:27 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @since 3.5.0
|
|
|
|
* @access private
|
2015-05-28 17:41:30 -04:00
|
|
|
*
|
|
|
|
* @global string $pagenow
|
2012-11-26 19:20:27 -05:00
|
|
|
*/
|
|
|
|
function wp_link_manager_disabled_message() {
|
|
|
|
global $pagenow;
|
|
|
|
if ( 'link-manager.php' != $pagenow && 'link-add.php' != $pagenow && 'link.php' != $pagenow )
|
|
|
|
return;
|
|
|
|
|
|
|
|
add_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
|
|
|
|
$really_can_manage_links = current_user_can( 'manage_links' );
|
|
|
|
remove_filter( 'pre_option_link_manager_enabled', '__return_true', 100 );
|
|
|
|
|
|
|
|
if ( $really_can_manage_links && current_user_can( 'install_plugins' ) ) {
|
|
|
|
$link = network_admin_url( 'plugin-install.php?tab=search&s=Link+Manager' );
|
|
|
|
wp_die( sprintf( __( 'If you are looking to use the link manager, please install the <a href="%s">Link Manager</a> plugin.' ), $link ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_die( __( 'You do not have sufficient permissions to edit the links for this site.' ) );
|
|
|
|
}
|