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