WordPress/wp-admin/includes/taxonomy.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

250 lines
5.5 KiB
PHP

<?php
/**
* WordPress Taxonomy Administration API.
*
* @package WordPress
* @subpackage Administration
*/
//
// Category
//
/**
* {@internal Missing Short Description}}
*
* @since 2.0.0
*
* @param unknown_type $cat_name
* @return unknown
*/
function category_exists($cat_name, $parent = 0) {
$id = term_exists($cat_name, 'category', $parent);
if ( is_array($id) )
$id = $id['term_id'];
return $id;
}
/**
* {@internal Missing Short Description}}
*
* @since 2.0.0
*
* @param unknown_type $id
* @return unknown
*/
function get_category_to_edit( $id ) {
$category = get_category( $id, OBJECT, 'edit' );
return $category;
}
/**
* {@internal Missing Short Description}}
*
* @since 2.0.0
*
* @param unknown_type $cat_name
* @param unknown_type $parent
* @return unknown
*/
function wp_create_category( $cat_name, $parent = 0 ) {
if ( $id = category_exists($cat_name, $parent) )
return $id;
return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
}
/**
* {@internal Missing Short Description}}
*
* @since 2.0.0
*
* @param unknown_type $categories
* @param unknown_type $post_id
* @return unknown
*/
function wp_create_categories($categories, $post_id = '') {
$cat_ids = array ();
foreach ($categories as $category) {
if ($id = category_exists($category))
$cat_ids[] = $id;
else
if ($id = wp_create_category($category))
$cat_ids[] = $id;
}
if ( $post_id )
wp_set_post_categories($post_id, $cat_ids);
return $cat_ids;
}
/**
* Updates an existing Category or creates a new Category.
*
* @since 2.0.0
*
* @param mixed $catarr See defaults below. Set 'cat_ID' to a non-zero value to update an existing category. The 'taxonomy' key was added in 3.0.0.
* @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
* @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure, depending on param $wp_error.
*/
function wp_insert_category($catarr, $wp_error = false) {
$cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
$catarr = wp_parse_args($catarr, $cat_defaults);
extract($catarr, EXTR_SKIP);
if ( trim( $cat_name ) == '' ) {
if ( ! $wp_error )
return 0;
else
return new WP_Error( 'cat_name', __('You did not enter a category name.') );
}
$cat_ID = (int) $cat_ID;
// Are we updating or creating?
if ( !empty ($cat_ID) )
$update = true;
else
$update = false;
$name = $cat_name;
$description = $category_description;
$slug = $category_nicename;
$parent = $category_parent;
$parent = (int) $parent;
if ( $parent < 0 )
$parent = 0;
if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
$parent = 0;
$args = compact('name', 'slug', 'parent', 'description');
if ( $update )
$cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
else
$cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
if ( is_wp_error($cat_ID) ) {
if ( $wp_error )
return $cat_ID;
else
return 0;
}
return $cat_ID['term_id'];
}
/**
* Aliases wp_insert_category() with minimal args.
*
* If you want to update only some fields of an existing category, call this
* function with only the new values set inside $catarr.
*
* @since 2.0.0
*
* @param array $catarr The 'cat_ID' value is required. All other keys are optional.
* @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
*/
function wp_update_category($catarr) {
$cat_ID = (int) $catarr['cat_ID'];
if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
return false;
// First, get all of the original fields
$category = get_category($cat_ID, ARRAY_A);
// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge($category, $catarr);
return wp_insert_category($catarr);
}
//
// Tags
//
/**
* {@internal Missing Short Description}}
*
* @since 2.3.0
*
* @param unknown_type $tag_name
* @return unknown
*/
function tag_exists($tag_name) {
return term_exists($tag_name, 'post_tag');
}
/**
* {@internal Missing Short Description}}
*
* @since 2.3.0
*
* @param unknown_type $tag_name
* @return unknown
*/
function wp_create_tag($tag_name) {
return wp_create_term( $tag_name, 'post_tag');
}
/**
* {@internal Missing Short Description}}
*
* @since 2.3.0
*
* @param unknown_type $post_id
* @return unknown
*/
function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
return get_terms_to_edit( $post_id, $taxonomy);
}
/**
* {@internal Missing Short Description}}
*
* @since 2.8.0
*
* @param unknown_type $post_id
* @return unknown
*/
function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
$post_id = (int) $post_id;
if ( !$post_id )
return false;
$tags = wp_get_post_terms($post_id, $taxonomy, array());
if ( !$tags )
return false;
if ( is_wp_error($tags) )
return $tags;
foreach ( $tags as $tag )
$tag_names[] = $tag->name;
$tags_to_edit = join( ',', $tag_names );
$tags_to_edit = esc_attr( $tags_to_edit );
$tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
return $tags_to_edit;
}
/**
* {@internal Missing Short Description}}
*
* @since 2.8.0
*
* @param unknown_type $tag_name
* @return unknown
*/
function wp_create_term($tag_name, $taxonomy = 'post_tag') {
if ( $id = term_exists($tag_name, $taxonomy) )
return $id;
return wp_insert_term($tag_name, $taxonomy);
}