wp/wp-admin/includes/taxonomy.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    20  * @param int|string $cat_name Category name.
    20  * @param int|string $cat_name Category name.
    21  * @param int        $parent   Optional. ID of parent term.
    21  * @param int        $parent   Optional. ID of parent term.
    22  * @return mixed
    22  * @return mixed
    23  */
    23  */
    24 function category_exists( $cat_name, $parent = null ) {
    24 function category_exists( $cat_name, $parent = null ) {
    25 	$id = term_exists($cat_name, 'category', $parent);
    25 	$id = term_exists( $cat_name, 'category', $parent );
    26 	if ( is_array($id) )
    26 	if ( is_array( $id ) ) {
    27 		$id = $id['term_id'];
    27 		$id = $id['term_id'];
       
    28 	}
    28 	return $id;
    29 	return $id;
    29 }
    30 }
    30 
    31 
    31 /**
    32 /**
    32  * Get category object for given ID and 'edit' filter context.
    33  * Get category object for given ID and 'edit' filter context.
    50  * @param int|string $cat_name
    51  * @param int|string $cat_name
    51  * @param int        $parent
    52  * @param int        $parent
    52  * @return int|WP_Error
    53  * @return int|WP_Error
    53  */
    54  */
    54 function wp_create_category( $cat_name, $parent = 0 ) {
    55 function wp_create_category( $cat_name, $parent = 0 ) {
    55 	if ( $id = category_exists($cat_name, $parent) )
    56 	if ( $id = category_exists( $cat_name, $parent ) ) {
    56 		return $id;
    57 		return $id;
    57 
    58 	}
    58 	return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
    59 
       
    60 	return wp_insert_category(
       
    61 		array(
       
    62 			'cat_name'        => $cat_name,
       
    63 			'category_parent' => $parent,
       
    64 		)
       
    65 	);
    59 }
    66 }
    60 
    67 
    61 /**
    68 /**
    62  * Create categories for the given post.
    69  * Create categories for the given post.
    63  *
    70  *
    64  * @since 2.0.0
    71  * @since 2.0.0
    65  *
    72  *
    66  * @param array $categories List of categories to create.
    73  * @param string[] $categories Array of category names to create.
    67  * @param int   $post_id    Optional. The post ID. Default empty.
    74  * @param int      $post_id    Optional. The post ID. Default empty.
    68  * @return array List of categories to create for the given post.
    75  * @return array List of categories to create for the given post.
    69  */
    76  */
    70 function wp_create_categories( $categories, $post_id = '' ) {
    77 function wp_create_categories( $categories, $post_id = '' ) {
    71 	$cat_ids = array ();
    78 	$cat_ids = array();
    72 	foreach ( $categories as $category ) {
    79 	foreach ( $categories as $category ) {
    73 		if ( $id = category_exists( $category ) ) {
    80 		if ( $id = category_exists( $category ) ) {
    74 			$cat_ids[] = $id;
    81 			$cat_ids[] = $id;
    75 		} elseif ( $id = wp_create_category( $category ) ) {
    82 		} elseif ( $id = wp_create_category( $category ) ) {
    76 			$cat_ids[] = $id;
    83 			$cat_ids[] = $id;
    77 		}
    84 		}
    78 	}
    85 	}
    79 
    86 
    80 	if ( $post_id )
    87 	if ( $post_id ) {
    81 		wp_set_post_categories($post_id, $cat_ids);
    88 		wp_set_post_categories( $post_id, $cat_ids );
       
    89 	}
    82 
    90 
    83 	return $cat_ids;
    91 	return $cat_ids;
    84 }
    92 }
    85 
    93 
    86 /**
    94 /**
   104  * @param bool  $wp_error Optional. Default false.
   112  * @param bool  $wp_error Optional. Default false.
   105  * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
   113  * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
   106  *                    depending on param $wp_error.
   114  *                    depending on param $wp_error.
   107  */
   115  */
   108 function wp_insert_category( $catarr, $wp_error = false ) {
   116 function wp_insert_category( $catarr, $wp_error = false ) {
   109 	$cat_defaults = array( 'cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '' );
   117 	$cat_defaults = array(
   110 	$catarr = wp_parse_args( $catarr, $cat_defaults );
   118 		'cat_ID'               => 0,
       
   119 		'taxonomy'             => 'category',
       
   120 		'cat_name'             => '',
       
   121 		'category_description' => '',
       
   122 		'category_nicename'    => '',
       
   123 		'category_parent'      => '',
       
   124 	);
       
   125 	$catarr       = wp_parse_args( $catarr, $cat_defaults );
   111 
   126 
   112 	if ( trim( $catarr['cat_name'] ) == '' ) {
   127 	if ( trim( $catarr['cat_name'] ) == '' ) {
   113 		if ( ! $wp_error ) {
   128 		if ( ! $wp_error ) {
   114 			return 0;
   129 			return 0;
   115 		} else {
   130 		} else {
   118 	}
   133 	}
   119 
   134 
   120 	$catarr['cat_ID'] = (int) $catarr['cat_ID'];
   135 	$catarr['cat_ID'] = (int) $catarr['cat_ID'];
   121 
   136 
   122 	// Are we updating or creating?
   137 	// Are we updating or creating?
   123 	$update = ! empty ( $catarr['cat_ID'] );
   138 	$update = ! empty( $catarr['cat_ID'] );
   124 
   139 
   125 	$name = $catarr['cat_name'];
   140 	$name        = $catarr['cat_name'];
   126 	$description = $catarr['category_description'];
   141 	$description = $catarr['category_description'];
   127 	$slug = $catarr['category_nicename'];
   142 	$slug        = $catarr['category_nicename'];
   128 	$parent = (int) $catarr['category_parent'];
   143 	$parent      = (int) $catarr['category_parent'];
   129 	if ( $parent < 0 ) {
   144 	if ( $parent < 0 ) {
   130 		$parent = 0;
   145 		$parent = 0;
   131 	}
   146 	}
   132 
   147 
   133 	if ( empty( $parent )
   148 	if ( empty( $parent )
   134 		|| ! term_exists( $parent, $catarr['taxonomy'] )
   149 		|| ! term_exists( $parent, $catarr['taxonomy'] )
   135 		|| ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
   150 		|| ( $catarr['cat_ID'] && term_is_ancestor_of( $catarr['cat_ID'], $parent, $catarr['taxonomy'] ) ) ) {
   136 		$parent = 0;
   151 		$parent = 0;
   137 	}
   152 	}
   138 
   153 
   139 	$args = compact('name', 'slug', 'parent', 'description');
   154 	$args = compact( 'name', 'slug', 'parent', 'description' );
   140 
   155 
   141 	if ( $update ) {
   156 	if ( $update ) {
   142 		$catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
   157 		$catarr['cat_ID'] = wp_update_term( $catarr['cat_ID'], $catarr['taxonomy'], $args );
   143 	} else {
   158 	} else {
   144 		$catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
   159 		$catarr['cat_ID'] = wp_insert_term( $catarr['cat_name'], $catarr['taxonomy'], $args );
   163  * @since 2.0.0
   178  * @since 2.0.0
   164  *
   179  *
   165  * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
   180  * @param array $catarr The 'cat_ID' value is required. All other keys are optional.
   166  * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
   181  * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure.
   167  */
   182  */
   168 function wp_update_category($catarr) {
   183 function wp_update_category( $catarr ) {
   169 	$cat_ID = (int) $catarr['cat_ID'];
   184 	$cat_ID = (int) $catarr['cat_ID'];
   170 
   185 
   171 	if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
   186 	if ( isset( $catarr['category_parent'] ) && ( $cat_ID == $catarr['category_parent'] ) ) {
   172 		return false;
   187 		return false;
       
   188 	}
   173 
   189 
   174 	// First, get all of the original fields
   190 	// First, get all of the original fields
   175 	$category = get_term( $cat_ID, 'category', ARRAY_A );
   191 	$category = get_term( $cat_ID, 'category', ARRAY_A );
   176 	_make_cat_compat( $category );
   192 	_make_cat_compat( $category );
   177 
   193 
   178 	// Escape data pulled from DB.
   194 	// Escape data pulled from DB.
   179 	$category = wp_slash($category);
   195 	$category = wp_slash( $category );
   180 
   196 
   181 	// Merge old and new fields with new fields overwriting old ones.
   197 	// Merge old and new fields with new fields overwriting old ones.
   182 	$catarr = array_merge($category, $catarr);
   198 	$catarr = array_merge( $category, $catarr );
   183 
   199 
   184 	return wp_insert_category($catarr);
   200 	return wp_insert_category( $catarr );
   185 }
   201 }
   186 
   202 
   187 //
   203 //
   188 // Tags
   204 // Tags
   189 //
   205 //
   194  * @since 2.3.0
   210  * @since 2.3.0
   195  *
   211  *
   196  * @param int|string $tag_name
   212  * @param int|string $tag_name
   197  * @return mixed
   213  * @return mixed
   198  */
   214  */
   199 function tag_exists($tag_name) {
   215 function tag_exists( $tag_name ) {
   200 	return term_exists($tag_name, 'post_tag');
   216 	return term_exists( $tag_name, 'post_tag' );
   201 }
   217 }
   202 
   218 
   203 /**
   219 /**
   204  * Add a new tag to the database if it does not already exist.
   220  * Add a new tag to the database if it does not already exist.
   205  *
   221  *
   206  * @since 2.3.0
   222  * @since 2.3.0
   207  *
   223  *
   208  * @param int|string $tag_name
   224  * @param int|string $tag_name
   209  * @return array|WP_Error
   225  * @return array|WP_Error
   210  */
   226  */
   211 function wp_create_tag($tag_name) {
   227 function wp_create_tag( $tag_name ) {
   212 	return wp_create_term( $tag_name, 'post_tag');
   228 	return wp_create_term( $tag_name, 'post_tag' );
   213 }
   229 }
   214 
   230 
   215 /**
   231 /**
   216  * Get comma-separated list of tags available to edit.
   232  * Get comma-separated list of tags available to edit.
   217  *
   233  *
   220  * @param int    $post_id
   236  * @param int    $post_id
   221  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   237  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   222  * @return string|bool|WP_Error
   238  * @return string|bool|WP_Error
   223  */
   239  */
   224 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   240 function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   225 	return get_terms_to_edit( $post_id, $taxonomy);
   241 	return get_terms_to_edit( $post_id, $taxonomy );
   226 }
   242 }
   227 
   243 
   228 /**
   244 /**
   229  * Get comma-separated list of terms available to edit for the given post ID.
   245  * Get comma-separated list of terms available to edit for the given post ID.
   230  *
   246  *
   234  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   250  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   235  * @return string|bool|WP_Error
   251  * @return string|bool|WP_Error
   236  */
   252  */
   237 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   253 function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
   238 	$post_id = (int) $post_id;
   254 	$post_id = (int) $post_id;
   239 	if ( !$post_id )
   255 	if ( ! $post_id ) {
   240 		return false;
   256 		return false;
       
   257 	}
   241 
   258 
   242 	$terms = get_object_term_cache( $post_id, $taxonomy );
   259 	$terms = get_object_term_cache( $post_id, $taxonomy );
   243 	if ( false === $terms ) {
   260 	if ( false === $terms ) {
   244 		$terms = wp_get_object_terms( $post_id, $taxonomy );
   261 		$terms = wp_get_object_terms( $post_id, $taxonomy );
   245 		wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
   262 		wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
   263 	 *
   280 	 *
   264 	 * @since 2.8.0
   281 	 * @since 2.8.0
   265 	 *
   282 	 *
   266 	 * @see get_terms_to_edit()
   283 	 * @see get_terms_to_edit()
   267 	 *
   284 	 *
   268 	 * @param array  $terms_to_edit An array of terms.
   285 	 * @param string $terms_to_edit A comma-separated list of term names.
   269 	 * @param string $taxonomy     The taxonomy for which to retrieve terms. Default 'post_tag'.
   286 	 * @param string $taxonomy      The taxonomy name for which to retrieve terms.
   270 	 */
   287 	 */
   271 	$terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
   288 	$terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy );
   272 
   289 
   273 	return $terms_to_edit;
   290 	return $terms_to_edit;
   274 }
   291 }
   280  *
   297  *
   281  * @param int|string $tag_name
   298  * @param int|string $tag_name
   282  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   299  * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
   283  * @return array|WP_Error
   300  * @return array|WP_Error
   284  */
   301  */
   285 function wp_create_term($tag_name, $taxonomy = 'post_tag') {
   302 function wp_create_term( $tag_name, $taxonomy = 'post_tag' ) {
   286 	if ( $id = term_exists($tag_name, $taxonomy) )
   303 	if ( $id = term_exists( $tag_name, $taxonomy ) ) {
   287 		return $id;
   304 		return $id;
   288 
   305 	}
   289 	return wp_insert_term($tag_name, $taxonomy);
   306 
   290 }
   307 	return wp_insert_term( $tag_name, $taxonomy );
       
   308 }