[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Types Admin functions. 4 * 5 * @package BuddyPress 6 * @subpackage Core 7 * @since 7.0.0 8 */ 9 10 // Exit if accessed directly. 11 if ( ! defined( 'ABSPATH' ) ) { 12 exit; 13 } 14 15 /** 16 * Get default values for the taxonomy registered metadata. 17 * 18 * @since 7.0.0 19 * 20 * @param string $type_taxonomy The type's taxonomy name. 21 * @return array Default values for the taxonomy registered metadata. 22 */ 23 function bp_core_admin_get_type_default_meta_values( $type_taxonomy ) { 24 $metadata_schema = bp_get_type_metadata_schema( false, $type_taxonomy ); 25 $metadata = wp_list_pluck( $metadata_schema, 'type' ); 26 27 // Set default values according to their schema type. 28 foreach ( $metadata as $meta_key => $meta_value ) { 29 if ( in_array( $meta_value, array( 'boolean', 'integer' ), true ) ) { 30 $metadata[ $meta_key ] = 0; 31 } else { 32 $metadata[ $meta_key ] = ''; 33 } 34 } 35 36 return $metadata; 37 } 38 39 /** 40 * Insert a new type into the database. 41 * 42 * @since 7.0.0 43 * 44 * @param array $args { 45 * Array of arguments describing the object type. 46 * 47 * @type string $taxonomy The Type's taxonomy. Required. 48 * @type string $bp_type_id Unique string identifier for the member type. Required. 49 * @see keys of the array returned by bp_get_type_metadata_schema() for the other arguments. 50 * } 51 * @return integer|WP_Error The Type's term ID on success. A WP_Error object otherwise. 52 */ 53 function bp_core_admin_insert_type( $args = array() ) { 54 $default_args = array( 55 'taxonomy' => '', 56 'bp_type_id' => '', 57 ); 58 59 $args = array_map( 'wp_unslash', $args ); 60 $args = bp_parse_args( 61 $args, 62 $default_args, 63 'admin_insert_type' 64 ); 65 66 if ( ! $args['bp_type_id'] || ! $args['taxonomy'] ) { 67 return new WP_Error( 68 'invalid_type_taxonomy', 69 __( 'The Type ID value is missing', 'buddypress' ), 70 array( 71 'message' => 1, 72 ) 73 ); 74 } 75 76 $type_id = sanitize_title( $args['bp_type_id'] ); 77 $type_taxonomy = sanitize_key( $args['taxonomy'] ); 78 79 /** 80 * Filter here to check for an already existing type. 81 * 82 * @since 7.0.0 83 * 84 * @param boolean $value True if the type exists. False otherwise. 85 * @param string $type_id The Type's ID. 86 */ 87 $type_exists = apply_filters( "{$type_taxonomy}_check_existing_type", false, $type_id ); 88 89 if ( false !== $type_exists ) { 90 return new WP_Error( 91 'type_already_exists', 92 __( 'The Type already exists', 'buddypress' ), 93 array( 94 'message' => 5, 95 ) 96 ); 97 } 98 99 // Get defaulte values for metadata. 100 $metadata = bp_core_admin_get_type_default_meta_values( $type_taxonomy ); 101 102 // Validate metadata 103 $metas = array_filter( array_intersect_key( $args, $metadata ) ); 104 105 // Insert the Type into the database. 106 $type_term_id = bp_insert_term( 107 $type_id, 108 $type_taxonomy, 109 array( 110 'slug' => $type_id, 111 'metas' => $metas, 112 ) 113 ); 114 115 if ( is_wp_error( $type_term_id ) ) { 116 $type_term_id->add_data( 117 array( 118 'message' => 3, 119 ) 120 ); 121 122 return $type_term_id; 123 } 124 125 /** 126 * Hook here to add code once the type has been inserted. 127 * 128 * @since 7.0.0 129 * 130 * @param integer $type_term_id The Type's term_ID. 131 * @param string $type_taxonomy The Type's taxonomy name. 132 * @param string $type_id The Type's ID. 133 */ 134 do_action( 'bp_type_inserted', $type_term_id, $type_taxonomy, $type_id ); 135 136 // Finally return the inserted Type's term ID. 137 return $type_term_id; 138 } 139 140 /** 141 * Update a type into the database. 142 * 143 * @since 7.0.0 144 * 145 * @param array $args { 146 * Array of arguments describing the object type. 147 * 148 * @type string $taxonomy The Type's taxonomy. Required. 149 * @type integer $type_term_id The Type's term ID. Required. 150 * @see keys of the array returned by bp_get_type_metadata_schema() for the other arguments. 151 * } 152 * @return boolean|WP_Error True on success. A WP_Error object otherwise. 153 */ 154 function bp_core_admin_update_type( $args = array() ) { 155 $default_args = array( 156 'taxonomy' => '', 157 'type_term_id' => 0, 158 ); 159 160 $args = array_map( 'wp_unslash', $args ); 161 $args = bp_parse_args( 162 $args, 163 $default_args, 164 'admin_update_type' 165 ); 166 167 if ( ! $args['type_term_id'] || ! $args['taxonomy'] ) { 168 return new WP_Error( 169 'invalid_type_taxonomy', 170 __( 'The Term Type ID value is missing', 'buddypress' ), 171 array( 172 'message' => 10, 173 ) 174 ); 175 } 176 177 $type_term_id = (int) $args['type_term_id']; 178 $type_taxonomy = sanitize_key( $args['taxonomy'] ); 179 180 // Get defaulte values for metadata. 181 $metadata = bp_core_admin_get_type_default_meta_values( $type_taxonomy ); 182 183 // Merge customs with defaults. 184 $metas = bp_parse_args( 185 $args, 186 $metadata 187 ); 188 189 // Validate metadata. 190 $metas = array_intersect_key( $metas, $metadata ); 191 192 foreach ( $metas as $meta_key => $meta_value ) { 193 if ( '' === $meta_value ) { 194 delete_term_meta( $type_term_id, $meta_key ); 195 } else { 196 update_term_meta( $type_term_id, $meta_key, $meta_value ); 197 } 198 } 199 200 /** 201 * Hook here to add code once the type has been updated. 202 * 203 * @since 7.0.0 204 * 205 * @param integer $type_term_id The Type's term_ID. 206 * @param string $type_taxonomy The Type's taxonomy name. 207 */ 208 do_action( 'bp_type_updated', $type_term_id, $type_taxonomy ); 209 210 // Finally informs about the successfull update. 211 return true; 212 } 213 214 /** 215 * Delete a type from the database. 216 * 217 * @since 7.0.0 218 * 219 * @param array $args { 220 * Array of arguments describing the object type. 221 * 222 * @type string $taxonomy The Type's taxonomy. Required. 223 * @type integer $type_term_id The Type's term ID. Required. 224 * } 225 * @return boolean|WP_Error True on success. A WP_Error object otherwise. 226 */ 227 function bp_core_admin_delete_type( $args = array() ) { 228 $default_args = array( 229 'taxonomy' => '', 230 'type_term_id' => 0, 231 ); 232 233 $args = array_map( 'wp_unslash', $args ); 234 $args = bp_parse_args( 235 $args, 236 $default_args, 237 'admin_delete_type' 238 ); 239 240 if ( ! $args['type_term_id'] || ! $args['taxonomy'] ) { 241 return new WP_Error( 242 'invalid_type_taxonomy', 243 __( 'The Term Type ID value is missing', 'buddypress' ), 244 array( 245 'message' => 10, 246 ) 247 ); 248 } 249 250 $type_term_id = (int) $args['type_term_id']; 251 $type_taxonomy = sanitize_key( $args['taxonomy'] ); 252 $type_term = bp_get_term_by( 'id', $type_term_id, $type_taxonomy ); 253 254 if ( ! $type_term ) { 255 return new WP_Error( 256 'type_doesnotexist', 257 __( 'The type was not deleted: it does not exist.', 'buddypress' ), 258 array( 259 'message' => 6, 260 ) 261 ); 262 } 263 264 /** This filter is documented in bp-core/classes/class-bp-admin-types.php */ 265 $registered_by_code_types = apply_filters( "{$type_taxonomy}_registered_by_code", array() ); 266 267 if ( isset( $registered_by_code_types[ $type_term->name ] ) ) { 268 return new WP_Error( 269 'type_register_by_code', 270 __( 'This type is registered using code, deactivate the plugin or remove the custom code before trying to delete it again.', 'buddypress' ), 271 array( 272 'message' => 7, 273 ) 274 ); 275 } 276 277 $deleted = bp_delete_term( $type_term_id, $type_taxonomy ); 278 279 if ( true !== $deleted ) { 280 return new WP_Error( 281 'type_not_deleted', 282 __( 'There was an error while trying to delete this type.', 'buddypress' ), 283 array( 284 'message' => 8, 285 ) 286 ); 287 } 288 289 /** 290 * Hook here to add code once the type has been deleted. 291 * 292 * @since 7.0.0 293 * 294 * @param integer $type_term_id The Type's term_ID. 295 * @param string $type_taxonomy The Type's taxonomy name. 296 */ 297 do_action( 'bp_type_deleted', $type_term_id, $type_taxonomy ); 298 299 // Finally informs about the successfull delete. 300 return true; 301 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 7 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |