WordPress/wp-admin/includes/bookmark.php
Ryan Boren cc5ed3a485 Change all core API to expect unslashed rather than slashed arguments.
The exceptions to this are update_post_meta() and add_post_meta() which are often used by plugins in POST handlers and will continue accepting slashed data for now.

Introduce wp_upate_post_meta() and wp_add_post_meta() as unslashed alternatives to update_post_meta() and add_post_meta(). These functions could become methods in WP_Post so don't use them too heavily yet.

Remove all escape() calls from wp_xmlrpc_server. Now that core expects unslashed data this is no longer needed.

Remove addslashes(), addslashes_gpc(), add_magic_quotes() calls on data being prepared for handoff to core functions that until now expected slashed data. Adding slashes in no longer necessary.

Introduce wp_unslash() and use to it remove slashes from GPCS data before using it in core API. Almost every instance of stripslashes() in core should now be wp_unslash(). In the future (a release or three) when GPCS is no longer slashed, wp_unslash() will stop stripping slashes and simply return what is passed. At this point wp_unslash() calls can be removed from core.

Introduce wp_slash() for slashing GPCS data. This will also turn into a noop once GPCS is no longer slashed. wp_slash() should almost never be used. It is mainly of use in unit tests.

Plugins should use wp_unslash() on data being passed to core API.

Plugins should no longer slash data being passed to core. So when you get_post() and then wp_insert_post() the post data from get_post() no longer needs addslashes(). Most plugins were not bothering with this. They will magically start doing the right thing. Unfortunately, those few souls who did it properly will now have to avoid calling addslashes() for 3.6 and newer.

Use wp_kses_post() and wp_kses_data(), which expect unslashed data, instead of wp_filter_post_kses() and wp_filter_kses(), which expect slashed data. Filters are no longer passed slashed data.

Remove many no longer necessary calls to $wpdb->escape() and esc_sql().

In wp_get_referer() and wp_get_original_referer(), return unslashed data.

Remove old stripslashes() calls from WP_Widget::update() handlers. These haven't been necessary since WP_Widget.

Switch several queries over to prepare().

Expect something to break.

Props alexkingorg
see #21767


git-svn-id: http://core.svn.wordpress.org/trunk@23416 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-02-14 22:51:06 +00:00

288 lines
7.5 KiB
PHP

<?php
/**
* WordPress Bookmark Administration API
*
* @package WordPress
* @subpackage Administration
*/
/**
* Add a link to using values provided in $_POST.
*
* @since 2.0.0
*
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
*/
function add_link() {
return edit_link();
}
/**
* Update or insert a link using values provided in $_POST.
*
* @since 2.0.0
*
* @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.
*/
function edit_link( $link_id = 0 ) {
if ( !current_user_can( 'manage_links' ) )
wp_die( __( 'Cheatin&#8217; uh?' ) );
$_POST['link_url'] = esc_html( $_POST['link_url'] );
$_POST['link_url'] = esc_url($_POST['link_url']);
$_POST['link_name'] = esc_html( $_POST['link_name'] );
$_POST['link_image'] = esc_html( $_POST['link_image'] );
$_POST['link_rss'] = esc_url($_POST['link_rss']);
if ( !isset($_POST['link_visible']) || 'N' != $_POST['link_visible'] )
$_POST['link_visible'] = 'Y';
if ( !empty( $link_id ) ) {
$_POST['link_id'] = $link_id;
return wp_update_link( wp_unslash( $_POST ) );
} else {
return wp_insert_link( wp_unslash( $_POST ) );
}
}
/**
* Retrieve the default link for editing.
*
* @since 2.0.0
*
* @return object Default link
*/
function get_default_link_to_edit() {
$link = new stdClass;
if ( isset( $_GET['linkurl'] ) )
$link->link_url = esc_url( $_GET['linkurl'] );
else
$link->link_url = '';
if ( isset( $_GET['name'] ) )
$link->link_name = esc_attr( $_GET['name'] );
else
$link->link_name = '';
$link->link_visible = 'Y';
return $link;
}
/**
* Delete link specified from database
*
* @since 2.0.0
*
* @param int $link_id ID of the link to delete
* @return bool True
*/
function wp_delete_link( $link_id ) {
global $wpdb;
do_action( 'delete_link', $link_id );
wp_delete_object_term_relationships( $link_id, 'link_category' );
$wpdb->delete( $wpdb->links, array( 'link_id' => $link_id ) );
do_action( 'deleted_link', $link_id );
clean_bookmark_cache( $link_id );
return true;
}
/**
* Retrieves the link categories associated with the link specified.
*
* @since 2.1.0
*
* @param int $link_id Link ID to look up
* @return array The requested link's categories
*/
function wp_get_link_cats( $link_id = 0 ) {
$cats = wp_get_object_terms( $link_id, 'link_category', array('fields' => 'ids') );
return array_unique( $cats );
}
/**
* Retrieve link data based on ID.
*
* @since 2.0.0
*
* @param int $link_id ID of link to retrieve
* @return object Link for editing
*/
function get_link_to_edit( $link_id ) {
return get_bookmark( $link_id, OBJECT, 'edit' );
}
/**
* This function inserts/updates links into/in the database.
*
* @since 2.0.0
*
* @param array $linkdata Elements that make up the link to insert.
* @param bool $wp_error Optional. If true return WP_Error object on failure.
* @return int|WP_Error Value 0 or WP_Error on failure. The link ID on success.
*/
function wp_insert_link( $linkdata, $wp_error = false ) {
global $wpdb;
$defaults = array( 'link_id' => 0, 'link_name' => '', 'link_url' => '', 'link_rating' => 0 );
$linkdata = wp_parse_args( $linkdata, $defaults );
$linkdata = sanitize_bookmark( $linkdata, 'db' );
extract( $linkdata, EXTR_SKIP );
$update = false;
if ( !empty( $link_id ) )
$update = true;
if ( trim( $link_name ) == '' ) {
if ( trim( $link_url ) != '' ) {
$link_name = $link_url;
} else {
return 0;
}
}
if ( trim( $link_url ) == '' )
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 = get_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 ( ! isset( $link_category ) || 0 == count( $link_category ) || !is_array( $link_category ) ) {
$link_category = array( get_option( 'default_link_category' ) );
}
if ( $update ) {
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 )
return new WP_Error( 'db_update_error', __( 'Could not update link in the database' ), $wpdb->last_error );
else
return 0;
}
} else {
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 )
return new WP_Error( 'db_insert_error', __( 'Could not insert link into the database' ), $wpdb->last_error );
else
return 0;
}
$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 );
clean_bookmark_cache( $link_id );
return $link_id;
}
/**
* Update link with the specified link categories.
*
* @since 2.1.0
*
* @param int $link_id ID of link to update
* @param array $link_categories Array of categories to
*/
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' );
clean_bookmark_cache( $link_id );
}
/**
* Update a link in the database.
*
* @since 2.0.0
*
* @param array $linkdata Link data to update.
* @return int|WP_Error Value 0 or WP_Error on failure. The updated link ID on success.
*/
function wp_update_link( $linkdata ) {
$link_id = (int) $linkdata['link_id'];
$link = get_bookmark( $link_id, ARRAY_A );
// 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 );
}
/**
* @since 3.5.0
* @access private
*/
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&amp;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.' ) );
}
add_action( 'admin_page_access_denied', 'wp_link_manager_disabled_message' );