[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * WordPress Taxonomy Administration API.
|
---|
| 4 | *
|
---|
| 5 | * @package WordPress
|
---|
| 6 | * @subpackage Administration
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | //
|
---|
| 10 | // Category
|
---|
| 11 | //
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * {@internal Missing Short Description}}
|
---|
| 15 | *
|
---|
| 16 | * @since unknown
|
---|
| 17 | *
|
---|
| 18 | * @param unknown_type $cat_name
|
---|
| 19 | * @return unknown
|
---|
| 20 | */
|
---|
| 21 | function category_exists($cat_name, $parent = 0) {
|
---|
| 22 | $id = is_term($cat_name, 'category', $parent);
|
---|
| 23 | if ( is_array($id) )
|
---|
| 24 | $id = $id['term_id'];
|
---|
| 25 | return $id;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * {@internal Missing Short Description}}
|
---|
| 30 | *
|
---|
| 31 | * @since unknown
|
---|
| 32 | *
|
---|
| 33 | * @param unknown_type $id
|
---|
| 34 | * @return unknown
|
---|
| 35 | */
|
---|
| 36 | function get_category_to_edit( $id ) {
|
---|
| 37 | $category = get_category( $id, OBJECT, 'edit' );
|
---|
| 38 | return $category;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * {@internal Missing Short Description}}
|
---|
| 43 | *
|
---|
| 44 | * @since unknown
|
---|
| 45 | *
|
---|
| 46 | * @param unknown_type $cat_name
|
---|
| 47 | * @param unknown_type $parent
|
---|
| 48 | * @return unknown
|
---|
| 49 | */
|
---|
| 50 | function wp_create_category( $cat_name, $parent = 0 ) {
|
---|
| 51 | if ( $id = category_exists($cat_name) )
|
---|
| 52 | return $id;
|
---|
| 53 |
|
---|
| 54 | return wp_insert_category( array('cat_name' => $cat_name, 'category_parent' => $parent) );
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * {@internal Missing Short Description}}
|
---|
| 59 | *
|
---|
| 60 | * @since unknown
|
---|
| 61 | *
|
---|
| 62 | * @param unknown_type $categories
|
---|
| 63 | * @param unknown_type $post_id
|
---|
| 64 | * @return unknown
|
---|
| 65 | */
|
---|
| 66 | function wp_create_categories($categories, $post_id = '') {
|
---|
| 67 | $cat_ids = array ();
|
---|
| 68 | foreach ($categories as $category) {
|
---|
| 69 | if ($id = category_exists($category))
|
---|
| 70 | $cat_ids[] = $id;
|
---|
| 71 | else
|
---|
| 72 | if ($id = wp_create_category($category))
|
---|
| 73 | $cat_ids[] = $id;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | if ($post_id)
|
---|
| 77 | wp_set_post_categories($post_id, $cat_ids);
|
---|
| 78 |
|
---|
| 79 | return $cat_ids;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * {@internal Missing Short Description}}
|
---|
| 84 | *
|
---|
| 85 | * @since unknown
|
---|
| 86 | *
|
---|
| 87 | * @param unknown_type $cat_ID
|
---|
| 88 | * @return unknown
|
---|
| 89 | */
|
---|
| 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 | */
|
---|
| 110 | function wp_insert_category($catarr, $wp_error = false) {
|
---|
| 111 | $cat_defaults = array('cat_ID' => 0, 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '');
|
---|
| 112 | $catarr = wp_parse_args($catarr, $cat_defaults);
|
---|
| 113 | extract($catarr, EXTR_SKIP);
|
---|
| 114 |
|
---|
| 115 | if ( trim( $cat_name ) == '' ) {
|
---|
| 116 | if ( ! $wp_error )
|
---|
| 117 | return 0;
|
---|
| 118 | else
|
---|
| 119 | return new WP_Error( 'cat_name', __('You did not enter a category name.') );
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | $cat_ID = (int) $cat_ID;
|
---|
| 123 |
|
---|
| 124 | // Are we updating or creating?
|
---|
| 125 | if ( !empty ($cat_ID) )
|
---|
| 126 | $update = true;
|
---|
| 127 | else
|
---|
| 128 | $update = false;
|
---|
| 129 |
|
---|
| 130 | $name = $cat_name;
|
---|
| 131 | $description = $category_description;
|
---|
| 132 | $slug = $category_nicename;
|
---|
| 133 | $parent = $category_parent;
|
---|
| 134 |
|
---|
| 135 | $parent = (int) $parent;
|
---|
| 136 | if ( $parent < 0 )
|
---|
| 137 | $parent = 0;
|
---|
| 138 |
|
---|
| 139 | if ( empty($parent) || !category_exists( $parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $parent) ) )
|
---|
| 140 | $parent = 0;
|
---|
| 141 |
|
---|
| 142 | $args = compact('name', 'slug', 'parent', 'description');
|
---|
| 143 |
|
---|
| 144 | if ( $update )
|
---|
| 145 | $cat_ID = wp_update_term($cat_ID, 'category', $args);
|
---|
| 146 | else
|
---|
| 147 | $cat_ID = wp_insert_term($cat_name, 'category', $args);
|
---|
| 148 |
|
---|
| 149 | if ( is_wp_error($cat_ID) ) {
|
---|
| 150 | if ( $wp_error )
|
---|
| 151 | return $cat_ID;
|
---|
| 152 | else
|
---|
| 153 | return 0;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return $cat_ID['term_id'];
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
| 160 | * {@internal Missing Short Description}}
|
---|
| 161 | *
|
---|
| 162 | * @since unknown
|
---|
| 163 | *
|
---|
| 164 | * @param unknown_type $catarr
|
---|
| 165 | * @return unknown
|
---|
| 166 | */
|
---|
| 167 | function wp_update_category($catarr) {
|
---|
| 168 | $cat_ID = (int) $catarr['cat_ID'];
|
---|
| 169 |
|
---|
| 170 | if ( isset($catarr['category_parent']) && ($cat_ID == $catarr['category_parent']) )
|
---|
| 171 | return false;
|
---|
| 172 |
|
---|
| 173 | // First, get all of the original fields
|
---|
| 174 | $category = get_category($cat_ID, ARRAY_A);
|
---|
| 175 |
|
---|
| 176 | // Escape data pulled from DB.
|
---|
| 177 | $category = add_magic_quotes($category);
|
---|
| 178 |
|
---|
| 179 | // Merge old and new fields with new fields overwriting old ones.
|
---|
| 180 | $catarr = array_merge($category, $catarr);
|
---|
| 181 |
|
---|
| 182 | return wp_insert_category($catarr);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | //
|
---|
| 186 | // Tags
|
---|
| 187 | //
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * {@internal Missing Short Description}}
|
---|
| 191 | *
|
---|
| 192 | * @since unknown
|
---|
| 193 | *
|
---|
| 194 | * @param unknown_type $post_id
|
---|
| 195 | * @return unknown
|
---|
| 196 | */
|
---|
| 197 | function get_tags_to_edit( $post_id, $taxonomy = 'post_tag' ) {
|
---|
| 198 | return get_terms_to_edit( $post_id, $taxonomy);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * {@internal Missing Short Description}}
|
---|
| 203 | *
|
---|
| 204 | * @since unknown
|
---|
| 205 | *
|
---|
| 206 | * @param unknown_type $post_id
|
---|
| 207 | * @return unknown
|
---|
| 208 | */
|
---|
| 209 | function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) {
|
---|
| 210 | $post_id = (int) $post_id;
|
---|
| 211 | if ( !$post_id )
|
---|
| 212 | return false;
|
---|
| 213 |
|
---|
| 214 | $tags = wp_get_post_terms($post_id, $taxonomy, array());
|
---|
| 215 |
|
---|
| 216 | if ( !$tags )
|
---|
| 217 | return false;
|
---|
| 218 |
|
---|
| 219 | if ( is_wp_error($tags) )
|
---|
| 220 | return $tags;
|
---|
| 221 |
|
---|
| 222 | foreach ( $tags as $tag )
|
---|
| 223 | $tag_names[] = $tag->name;
|
---|
| 224 | $tags_to_edit = join( ',', $tag_names );
|
---|
| 225 | $tags_to_edit = esc_attr( $tags_to_edit );
|
---|
| 226 | $tags_to_edit = apply_filters( 'terms_to_edit', $tags_to_edit, $taxonomy );
|
---|
| 227 |
|
---|
| 228 | return $tags_to_edit;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * {@internal Missing Short Description}}
|
---|
| 233 | *
|
---|
| 234 | * @since unknown
|
---|
| 235 | *
|
---|
| 236 | * @param unknown_type $tag_name
|
---|
| 237 | * @return unknown
|
---|
| 238 | */
|
---|
| 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') {
|
---|
| 264 | if ( $id = is_term($tag_name, $taxonomy) )
|
---|
| 265 | return $id;
|
---|
| 266 |
|
---|
| 267 | return wp_insert_term($tag_name, $taxonomy);
|
---|
| 268 | }
|
---|