Edit File by line
/home/barbar84/public_h.../wp-admin/includes
File: taxonomy.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress Taxonomy Administration API.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Administration
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
//
[8] Fix | Delete
// Category.
[9] Fix | Delete
//
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Check whether a category exists.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 2.0.0
[15] Fix | Delete
*
[16] Fix | Delete
* @see term_exists()
[17] Fix | Delete
*
[18] Fix | Delete
* @param int|string $cat_name Category name.
[19] Fix | Delete
* @param int $parent Optional. ID of parent term.
[20] Fix | Delete
* @return mixed
[21] Fix | Delete
*/
[22] Fix | Delete
function category_exists( $cat_name, $parent = null ) {
[23] Fix | Delete
$id = term_exists( $cat_name, 'category', $parent );
[24] Fix | Delete
if ( is_array( $id ) ) {
[25] Fix | Delete
$id = $id['term_id'];
[26] Fix | Delete
}
[27] Fix | Delete
return $id;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Get category object for given ID and 'edit' filter context.
[32] Fix | Delete
*
[33] Fix | Delete
* @since 2.0.0
[34] Fix | Delete
*
[35] Fix | Delete
* @param int $id
[36] Fix | Delete
* @return object
[37] Fix | Delete
*/
[38] Fix | Delete
function get_category_to_edit( $id ) {
[39] Fix | Delete
$category = get_term( $id, 'category', OBJECT, 'edit' );
[40] Fix | Delete
_make_cat_compat( $category );
[41] Fix | Delete
return $category;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Add a new category to the database if it does not already exist.
[46] Fix | Delete
*
[47] Fix | Delete
* @since 2.0.0
[48] Fix | Delete
*
[49] Fix | Delete
* @param int|string $cat_name
[50] Fix | Delete
* @param int $parent
[51] Fix | Delete
* @return int|WP_Error
[52] Fix | Delete
*/
[53] Fix | Delete
function wp_create_category( $cat_name, $parent = 0 ) {
[54] Fix | Delete
$id = category_exists( $cat_name, $parent );
[55] Fix | Delete
if ( $id ) {
[56] Fix | Delete
return $id;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
return wp_insert_category(
[60] Fix | Delete
array(
[61] Fix | Delete
'cat_name' => $cat_name,
[62] Fix | Delete
'category_parent' => $parent,
[63] Fix | Delete
)
[64] Fix | Delete
);
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Create categories for the given post.
[69] Fix | Delete
*
[70] Fix | Delete
* @since 2.0.0
[71] Fix | Delete
*
[72] Fix | Delete
* @param string[] $categories Array of category names to create.
[73] Fix | Delete
* @param int $post_id Optional. The post ID. Default empty.
[74] Fix | Delete
* @return int[] Array of IDs of categories assigned to the given post.
[75] Fix | Delete
*/
[76] Fix | Delete
function wp_create_categories( $categories, $post_id = '' ) {
[77] Fix | Delete
$cat_ids = array();
[78] Fix | Delete
foreach ( $categories as $category ) {
[79] Fix | Delete
$id = category_exists( $category );
[80] Fix | Delete
if ( $id ) {
[81] Fix | Delete
$cat_ids[] = $id;
[82] Fix | Delete
} else {
[83] Fix | Delete
$id = wp_create_category( $category );
[84] Fix | Delete
if ( $id ) {
[85] Fix | Delete
$cat_ids[] = $id;
[86] Fix | Delete
}
[87] Fix | Delete
}
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
if ( $post_id ) {
[91] Fix | Delete
wp_set_post_categories( $post_id, $cat_ids );
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
return $cat_ids;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
/**
[98] Fix | Delete
* Updates an existing Category or creates a new Category.
[99] Fix | Delete
*
[100] Fix | Delete
* @since 2.0.0
[101] Fix | Delete
* @since 2.5.0 $wp_error parameter was added.
[102] Fix | Delete
* @since 3.0.0 The 'taxonomy' argument was added.
[103] Fix | Delete
*
[104] Fix | Delete
* @param array $catarr {
[105] Fix | Delete
* Array of arguments for inserting a new category.
[106] Fix | Delete
*
[107] Fix | Delete
* @type int $cat_ID Category ID. A non-zero value updates an existing category.
[108] Fix | Delete
* Default 0.
[109] Fix | Delete
* @type string $taxonomy Taxonomy slug. Default 'category'.
[110] Fix | Delete
* @type string $cat_name Category name. Default empty.
[111] Fix | Delete
* @type string $category_description Category description. Default empty.
[112] Fix | Delete
* @type string $category_nicename Category nice (display) name. Default empty.
[113] Fix | Delete
* @type int|string $category_parent Category parent ID. Default empty.
[114] Fix | Delete
* }
[115] Fix | Delete
* @param bool $wp_error Optional. Default false.
[116] Fix | Delete
* @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
[117] Fix | Delete
* depending on param $wp_error.
[118] Fix | Delete
*/
[119] Fix | Delete
function wp_insert_category( $catarr, $wp_error = false ) {
[120] Fix | Delete
$cat_defaults = array(
[121] Fix | Delete
'cat_ID' => 0,
[122] Fix | Delete
'taxonomy' => 'category',
[123] Fix | Delete
'cat_name' => '',
[124] Fix | Delete
'category_description' => '',
[125] Fix | Delete
'category_nicename' => '',
[126] Fix | Delete
'category_parent' => '',
[127] Fix | Delete
);
[128] Fix | Delete
$catarr = wp_parse_args( $catarr, $cat_defaults );
[129] Fix | Delete
[130] Fix | Delete
if ( '' === trim( $catarr['cat_name'] ) ) {
[131] Fix | Delete
if ( ! $wp_error ) {
[132] Fix | Delete
return 0;
[133] Fix | Delete
} else {
[134] Fix | Delete
return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );
[135] Fix | Delete
}
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
$catarr['cat_ID'] = (int) $catarr['cat_ID'];
[139] Fix | Delete
[140] Fix | Delete
// Are we updating or creating?
[141] Fix | Delete
$update = ! empty( $catarr['cat_ID'] );
[142] Fix | Delete
[143] Fix | Delete
$name = $catarr['cat_name'];
[144] Fix | Delete
$description = $catarr['category_description'];
[145] Fix | Delete
$slug = $catarr['category_nicename'];
[146] Fix | Delete
$parent = (int) $catarr['category_parent'];
[147] Fix | Delete
if ( $parent < 0 ) {
[148] Fix | Delete
$parent = 0;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
if ( empty( $parent )
[152] Fix | Delete
|| ! term_exists( $parent, $catarr['taxonomy'] )
[153] Fix | Delete
|| ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
[154] Fix | Delete
$parent = 0;
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
$args = compact( 'name', 'slug', 'parent', 'description' );
[158] Fix | Delete
[159] Fix | Delete
if ( $update ) {
[160] Fix | Delete
$catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
[161] Fix | Delete
} else {
[162] Fix | Delete
$catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
if ( is_wp_error( $catarr['cat_ID'] ) ) {
[166] Fix | Delete
if ( $wp_error ) {
[167] Fix | Delete
return $catarr['cat_ID'];
[168] Fix | Delete
} else {
[169] Fix | Delete
return 0;
[170] Fix | Delete
}
[171] Fix | Delete
}
[172] Fix | Delete
return $catarr['cat_ID']['term_id'];
[173] Fix | Delete
}
[174] Fix | Delete
[175] Fix | Delete
/**
[176] Fix | Delete
* Aliases wp_insert_category() with minimal args.
[177] Fix | Delete
*
[178] Fix | Delete
* If you want to update only some fields of an existing category, call this
[179] Fix | Delete
* function with only the new values set inside $catarr.
[180] Fix | Delete
*
[181] Fix | Delete
* @since 2.0.0
[182] Fix | Delete
*
[183] Fix | Delete
* @param array $catarr The 'cat_ID' value is required. All other keys are optional.
[184] Fix | Delete
* @return int|false The ID number of the new or updated Category on success. Zero or FALSE on failure.
[185] Fix | Delete
*/
[186] Fix | Delete
function wp_update_category( $catarr ) {
[187] Fix | Delete
$cat_ID = (int) $catarr['cat_ID'];
[188] Fix | Delete
[189] Fix | Delete
if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) {
[190] Fix | Delete
return false;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
// First, get all of the original fields.
[194] Fix | Delete
$category = get_term( $cat_ID, 'category', ARRAY_A );
[195] Fix | Delete
_make_cat_compat( $category );
[196] Fix | Delete
[197] Fix | Delete
// Escape data pulled from DB.
[198] Fix | Delete
$category = wp_slash( $category );
[199] Fix | Delete
[200] Fix | Delete
// Merge old and new fields with new fields overwriting old ones.
[201] Fix | Delete
$catarr = array_merge( $category, $catarr );
[202] Fix | Delete
[203] Fix | Delete
return wp_insert_category( $catarr );
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
//
[207] Fix | Delete
// Tags.
[208] Fix | Delete
//
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* Check whether a post tag with a given name exists.
[212] Fix | Delete
*
[213] Fix | Delete
* @since 2.3.0
[214] Fix | Delete
*
[215] Fix | Delete
* @param int|string $tag_name
[216] Fix | Delete
* @return mixed
[217] Fix | Delete
*/
[218] Fix | Delete
function tag_exists( $tag_name ) {
[219] Fix | Delete
return term_exists( $tag_name, 'post_tag' );
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Add a new tag to the database if it does not already exist.
[224] Fix | Delete
*
[225] Fix | Delete
* @since 2.3.0
[226] Fix | Delete
*
[227] Fix | Delete
* @param int|string $tag_name
[228] Fix | Delete
* @return array|WP_Error
[229] Fix | Delete
*/
[230] Fix | Delete
function wp_create_tag( $tag_name ) {
[231] Fix | Delete
return wp_create_term( $tag_name, 'post_tag' );
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
/**
[235] Fix | Delete
* Get comma-separated list of tags available to edit.
[236] Fix | Delete
*
[237] Fix | Delete
* @since 2.3.0
[238] Fix | Delete
*
[239] Fix | Delete
* @param int $post_id
[240] Fix | Delete
* @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
[241] Fix | Delete
* @return string|false|WP_Error
[242] Fix | Delete
*/
[243] Fix | Delete
function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
[244] Fix | Delete
return get_terms_to_edit( $post_id, $taxonomy );
[245] Fix | Delete
}
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Get comma-separated list of terms available to edit for the given post ID.
[249] Fix | Delete
*
[250] Fix | Delete
* @since 2.8.0
[251] Fix | Delete
*
[252] Fix | Delete
* @param int $post_id
[253] Fix | Delete
* @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
[254] Fix | Delete
* @return string|false|WP_Error
[255] Fix | Delete
*/
[256] Fix | Delete
function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
[257] Fix | Delete
$post_id = (int) $post_id;
[258] Fix | Delete
if ( ! $post_id ) {
[259] Fix | Delete
return false;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$terms = get_object_term_cache( $post_id, $taxonomy );
[263] Fix | Delete
if ( false === $terms ) {
[264] Fix | Delete
$terms = wp_get_object_terms( $post_id, $taxonomy );
[265] Fix | Delete
wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
if ( ! $terms ) {
[269] Fix | Delete
return false;
[270] Fix | Delete
}
[271] Fix | Delete
if ( is_wp_error( $terms ) ) {
[272] Fix | Delete
return $terms;
[273] Fix | Delete
}
[274] Fix | Delete
$term_names = array();
[275] Fix | Delete
foreach ( $terms as $term ) {
[276] Fix | Delete
$term_names[] = $term->name;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
$terms_to_edit = esc_attr( implode( ',', $term_names ) );
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Filters the comma-separated list of terms available to edit.
[283] Fix | Delete
*
[284] Fix | Delete
* @since 2.8.0
[285] Fix | Delete
*
[286] Fix | Delete
* @see get_terms_to_edit()
[287] Fix | Delete
*
[288] Fix | Delete
* @param string $terms_to_edit A comma-separated list of term names.
[289] Fix | Delete
* @param string $taxonomy The taxonomy name for which to retrieve terms.
[290] Fix | Delete
*/
[291] Fix | Delete
$terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
[292] Fix | Delete
[293] Fix | Delete
return $terms_to_edit;
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
/**
[297] Fix | Delete
* Add a new term to the database if it does not already exist.
[298] Fix | Delete
*
[299] Fix | Delete
* @since 2.8.0
[300] Fix | Delete
*
[301] Fix | Delete
* @param string $tag_name The term name.
[302] Fix | Delete
* @param string $taxonomy Optional. The taxonomy within which to create the term. Default 'post_tag'.
[303] Fix | Delete
* @return array|WP_Error
[304] Fix | Delete
*/
[305] Fix | Delete
function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
[306] Fix | Delete
$id = term_exists( $tag_name, $taxonomy );
[307] Fix | Delete
if ( $id ) {
[308] Fix | Delete
return $id;
[309] Fix | Delete
}
[310] Fix | Delete
[311] Fix | Delete
return wp_insert_term( $tag_name, $taxonomy );
[312] Fix | Delete
}
[313] Fix | Delete
[314] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function