web/wp-admin/includes/taxonomy.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    11 //
    11 //
    12 
    12 
    13 /**
    13 /**
    14  * {@internal Missing Short Description}}
    14  * {@internal Missing Short Description}}
    15  *
    15  *
    16  * @since unknown
    16  * @since 2.0.0
    17  *
    17  *
    18  * @param unknown_type $cat_name
    18  * @param unknown_type $cat_name
    19  * @return unknown
    19  * @return unknown
    20  */
    20  */
    21 function category_exists($cat_name, $parent = 0) {
    21 function category_exists($cat_name, $parent = 0) {
    22 	$id = is_term($cat_name, 'category', $parent);
    22 	$id = term_exists($cat_name, 'category', $parent);
    23 	if ( is_array($id) )
    23 	if ( is_array($id) )
    24 		$id = $id['term_id'];
    24 		$id = $id['term_id'];
    25 	return $id;
    25 	return $id;
    26 }
    26 }
    27 
    27 
    28 /**
    28 /**
    29  * {@internal Missing Short Description}}
    29  * {@internal Missing Short Description}}
    30  *
    30  *
    31  * @since unknown
    31  * @since 2.0.0
    32  *
    32  *
    33  * @param unknown_type $id
    33  * @param unknown_type $id
    34  * @return unknown
    34  * @return unknown
    35  */
    35  */
    36 function get_category_to_edit( $id ) {
    36 function get_category_to_edit( $id ) {
    39 }
    39 }
    40 
    40 
    41 /**
    41 /**
    42  * {@internal Missing Short Description}}
    42  * {@internal Missing Short Description}}
    43  *
    43  *
    44  * @since unknown
    44  * @since 2.0.0
    45  *
    45  *
    46  * @param unknown_type $cat_name
    46  * @param unknown_type $cat_name
    47  * @param unknown_type $parent
    47  * @param unknown_type $parent
    48  * @return unknown
    48  * @return unknown
    49  */
    49  */
    50 function wp_create_category( $cat_name, $parent = 0 ) {
    50 function wp_create_category( $cat_name, $parent = 0 ) {
    51 	if ( $id = category_exists($cat_name) )
    51 	if ( $id = category_exists($cat_name, $parent) )
    52 		return $id;
    52 		return $id;
    53 
    53 
    54 	return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
    54 	return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
    55 }
    55 }
    56 
    56 
    57 /**
    57 /**
    58  * {@internal Missing Short Description}}
    58  * {@internal Missing Short Description}}
    59  *
    59  *
    60  * @since unknown
    60  * @since 2.0.0
    61  *
    61  *
    62  * @param unknown_type $categories
    62  * @param unknown_type $categories
    63  * @param unknown_type $post_id
    63  * @param unknown_type $post_id
    64  * @return unknown
    64  * @return unknown
    65  */
    65  */
    71 		else
    71 		else
    72 			if ($id = wp_create_category($category))
    72 			if ($id = wp_create_category($category))
    73 				$cat_ids[] = $id;
    73 				$cat_ids[] = $id;
    74 	}
    74 	}
    75 
    75 
    76 	if ($post_id)
    76 	if ( $post_id )
    77 		wp_set_post_categories($post_id, $cat_ids);
    77 		wp_set_post_categories($post_id, $cat_ids);
    78 
    78 
    79 	return $cat_ids;
    79 	return $cat_ids;
    80 }
    80 }
    81 
    81 
    82 /**
    82 /**
    83  * {@internal Missing Short Description}}
    83  * Updates an existing Category or creates a new Category.
    84  *
    84  *
    85  * @since unknown
    85  * @since 2.0.0
    86  *
    86  *
    87  * @param unknown_type $cat_ID
    87  * @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.
    88  * @return unknown
    88  * @param bool $wp_error Optional, since 2.5.0. Set this to true if the caller handles WP_Error return values.
    89  */
    89  * @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.
    90 function wp_delete_category($cat_ID) {
       
    91 	$cat_ID = (int) $cat_ID;
       
    92 	$default = get_option('default_category');
       
    93 
       
    94 	// Don't delete the default cat
       
    95 	if ( $cat_ID == $default )
       
    96 		return 0;
       
    97 
       
    98 	return wp_delete_term($cat_ID, 'category', array('default' => $default));
       
    99 }
       
   100 
       
   101 /**
       
   102  * {@internal Missing Short Description}}
       
   103  *
       
   104  * @since unknown
       
   105  *
       
   106  * @param unknown_type $catarr
       
   107  * @param unknown_type $wp_error
       
   108  * @return unknown
       
   109  */
    90  */
   110 function wp_insert_category($catarr, $wp_error = false) {
    91 function wp_insert_category($catarr, $wp_error = false) {
   111 	$cat_defaults = array('cat_ID' => 0, 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
    92 	$cat_defaults = array('cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
   112 	$catarr = wp_parse_args($catarr, $cat_defaults);
    93 	$catarr = wp_parse_args($catarr, $cat_defaults);
   113 	extract($catarr, EXTR_SKIP);
    94 	extract($catarr, EXTR_SKIP);
   114 
    95 
   115 	if ( trim( $cat_name ) == '' ) {
    96 	if ( trim( $cat_name ) == '' ) {
   116 		if ( ! $wp_error )
    97 		if ( ! $wp_error )
   134 
   115 
   135 	$parent = (int) $parent;
   116 	$parent = (int) $parent;
   136 	if ( $parent < 0 )
   117 	if ( $parent < 0 )
   137 		$parent = 0;
   118 		$parent = 0;
   138 
   119 
   139 	if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
   120 	if ( empty( $parent ) || ! term_exists( $parent, $taxonomy ) || ( $cat_ID && term_is_ancestor_of( $cat_ID, $parent, $taxonomy ) ) )
   140 		$parent = 0;
   121 		$parent = 0;
   141 
   122 
   142 	$args = compact('name', 'slug', 'parent', 'description');
   123 	$args = compact('name', 'slug', 'parent', 'description');
   143 
   124 
   144 	if ( $update )
   125 	if ( $update )
   145 		$cat_ID = wp_update_term($cat_ID, 'category', $args);
   126 		$cat_ID = wp_update_term($cat_ID, $taxonomy, $args);
   146 	else
   127 	else
   147 		$cat_ID = wp_insert_term($cat_name, 'category', $args);
   128 		$cat_ID = wp_insert_term($cat_name, $taxonomy, $args);
   148 
   129 
   149 	if ( is_wp_error($cat_ID) ) {
   130 	if ( is_wp_error($cat_ID) ) {
   150 		if ( $wp_error )
   131 		if ( $wp_error )
   151 			return $cat_ID;
   132 			return $cat_ID;
   152 		else
   133 		else
   155 
   136 
   156 	return $cat_ID['term_id'];
   137 	return $cat_ID['term_id'];
   157 }
   138 }
   158 
   139 
   159 /**
   140 /**
   160  * {@internal Missing Short Description}}
   141  * Aliases wp_insert_category() with minimal args.
   161  *
   142  *
   162  * @since unknown
   143  * If you want to update only some fields of an existing category, call this
   163  *
   144  * function with only the new values set inside $catarr.
   164  * @param unknown_type $catarr
   145  *
   165  * @return unknown
   146  * @since 2.0.0
       
   147  *
       
   148  * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
       
   149  * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
   166  */
   150  */
   167 function wp_update_category($catarr) {
   151 function wp_update_category($catarr) {
   168 	$cat_ID = (int) $catarr['cat_ID'];
   152 	$cat_ID = (int) $catarr['cat_ID'];
   169 
   153 
   170 	if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
   154 	if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
   187 //
   171 //
   188 
   172 
   189 /**
   173 /**
   190  * {@internal Missing Short Description}}
   174  * {@internal Missing Short Description}}
   191  *
   175  *
   192  * @since unknown
   176  * @since 2.3.0
       
   177  *
       
   178  * @param unknown_type $tag_name
       
   179  * @return unknown
       
   180  */
       
   181 function tag_exists($tag_name) {
       
   182 	return term_exists($tag_name, 'post_tag');
       
   183 }
       
   184 
       
   185 /**
       
   186  * {@internal Missing Short Description}}
       
   187  *
       
   188  * @since 2.3.0
       
   189  *
       
   190  * @param unknown_type $tag_name
       
   191  * @return unknown
       
   192  */
       
   193 function wp_create_tag($tag_name) {
       
   194 	return wp_create_term( $tag_name, 'post_tag');
       
   195 }
       
   196 
       
   197 /**
       
   198  * {@internal Missing Short Description}}
       
   199  *
       
   200  * @since 2.3.0
   193  *
   201  *
   194  * @param unknown_type $post_id
   202  * @param unknown_type $post_id
   195  * @return unknown
   203  * @return unknown
   196  */
   204  */
   197 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   205 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   199 }
   207 }
   200 
   208 
   201 /**
   209 /**
   202  * {@internal Missing Short Description}}
   210  * {@internal Missing Short Description}}
   203  *
   211  *
   204  * @since unknown
   212  * @since 2.8.0
   205  *
   213  *
   206  * @param unknown_type $post_id
   214  * @param unknown_type $post_id
   207  * @return unknown
   215  * @return unknown
   208  */
   216  */
   209 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   217 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   229 }
   237 }
   230 
   238 
   231 /**
   239 /**
   232  * {@internal Missing Short Description}}
   240  * {@internal Missing Short Description}}
   233  *
   241  *
   234  * @since unknown
   242  * @since 2.8.0
   235  *
   243  *
   236  * @param unknown_type $tag_name
   244  * @param unknown_type $tag_name
   237  * @return unknown
   245  * @return unknown
   238  */
   246  */
   239 function tag_exists($tag_name) {
       
   240 	return is_term($tag_name, 'post_tag');
       
   241 }
       
   242 
       
   243 /**
       
   244  * {@internal Missing Short Description}}
       
   245  *
       
   246  * @since unknown
       
   247  *
       
   248  * @param unknown_type $tag_name
       
   249  * @return unknown
       
   250  */
       
   251 function wp_create_tag($tag_name) {
       
   252 	return wp_create_term( $tag_name, 'post_tag');
       
   253 }
       
   254 
       
   255 /**
       
   256  * {@internal Missing Short Description}}
       
   257  *
       
   258  * @since unknown
       
   259  *
       
   260  * @param unknown_type $tag_name
       
   261  * @return unknown
       
   262  */
       
   263 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
   247 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
   264 	if ( $id = is_term($tag_name, $taxonomy) )
   248 	if ( $id = term_exists($tag_name, $taxonomy) )
   265 		return $id;
   249 		return $id;
   266 
   250 
   267 	return wp_insert_term($tag_name, $taxonomy);
   251 	return wp_insert_term($tag_name, $taxonomy);
   268 }
   252 }