| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * BuddyPress XProfile Classes |
|---|
| 5 | * |
|---|
| 6 | * @package BuddyPress |
|---|
| 7 | * @subpackage XProfileClasses |
|---|
| 8 | */ |
|---|
| 9 | |
|---|
| 10 | // Exit if accessed directly |
|---|
| 11 | if ( !defined( 'ABSPATH' ) ) exit; |
|---|
| 12 | |
|---|
| 13 | class BP_XProfile_Group { |
|---|
| 14 | var $id = null; |
|---|
| 15 | var $name; |
|---|
| 16 | var $description; |
|---|
| 17 | var $can_delete; |
|---|
| 18 | var $group_order; |
|---|
| 19 | var $fields; |
|---|
| 20 | |
|---|
| 21 | function __construct( $id = null ) { |
|---|
| 22 | if ( !empty( $id ) ) |
|---|
| 23 | $this->populate( $id ); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | function populate( $id ) { |
|---|
| 27 | global $wpdb, $bp; |
|---|
| 28 | |
|---|
| 29 | $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id = %d", $id ); |
|---|
| 30 | |
|---|
| 31 | if ( !$group = $wpdb->get_row( $sql ) ) |
|---|
| 32 | return false; |
|---|
| 33 | |
|---|
| 34 | $this->id = $group->id; |
|---|
| 35 | $this->name = stripslashes( $group->name ); |
|---|
| 36 | $this->description = stripslashes( $group->description ); |
|---|
| 37 | $this->can_delete = $group->can_delete; |
|---|
| 38 | $this->group_order = $group->group_order; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | function save() { |
|---|
| 42 | global $wpdb, $bp; |
|---|
| 43 | |
|---|
| 44 | $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id ); |
|---|
| 45 | $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id ); |
|---|
| 46 | |
|---|
| 47 | do_action_ref_array( 'xprofile_group_before_save', array( &$this ) ); |
|---|
| 48 | |
|---|
| 49 | if ( $this->id ) |
|---|
| 50 | $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id ); |
|---|
| 51 | else |
|---|
| 52 | $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description ); |
|---|
| 53 | |
|---|
| 54 | if ( is_wp_error( $wpdb->query( $sql ) ) ) |
|---|
| 55 | return false; |
|---|
| 56 | |
|---|
| 57 | // If not set, update the ID in the group object |
|---|
| 58 | if ( ! $this->id ) |
|---|
| 59 | $this->id = $wpdb->insert_id; |
|---|
| 60 | |
|---|
| 61 | do_action_ref_array( 'xprofile_group_after_save', array( &$this ) ); |
|---|
| 62 | |
|---|
| 63 | return $this->id; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | function delete() { |
|---|
| 67 | global $wpdb, $bp; |
|---|
| 68 | |
|---|
| 69 | if ( empty( $this->can_delete ) ) |
|---|
| 70 | return false; |
|---|
| 71 | |
|---|
| 72 | // Delete field group |
|---|
| 73 | if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ) ) ) { |
|---|
| 74 | return false; |
|---|
| 75 | } else { |
|---|
| 76 | |
|---|
| 77 | // Remove the group's fields. |
|---|
| 78 | if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { |
|---|
| 79 | |
|---|
| 80 | // Remove profile data for the groups fields |
|---|
| 81 | for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { |
|---|
| 82 | BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return true; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** Static Methods ********************************************************/ |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * get() |
|---|
| 94 | * |
|---|
| 95 | * Populates the BP_XProfile_Group object with profile field groups, fields, and field data |
|---|
| 96 | * |
|---|
| 97 | * @package BuddyPress XProfile |
|---|
| 98 | * |
|---|
| 99 | * @global $wpdb WordPress DB access object. |
|---|
| 100 | * @global BuddyPress $bp The one true BuddyPress instance |
|---|
| 101 | * |
|---|
| 102 | * @param array $args Takes an array of parameters: |
|---|
| 103 | * 'profile_group_id' - Limit results to a single profile group |
|---|
| 104 | * 'user_id' - Required if you want to load a specific user's data |
|---|
| 105 | * 'hide_empty_groups' - Hide groups without any fields |
|---|
| 106 | * 'hide_empty_fields' - Hide fields where the user has not provided data |
|---|
| 107 | * 'fetch_fields' - Load each group's fields |
|---|
| 108 | * 'fetch_field_data' - Load each field's data. Requires a user_id |
|---|
| 109 | * 'exclude_groups' - Comma-separated list of groups to exclude |
|---|
| 110 | * 'exclude_fields' - Comma-separated list of fields to exclude |
|---|
| 111 | * |
|---|
| 112 | * @return array $groups |
|---|
| 113 | */ |
|---|
| 114 | function get( $args = '' ) { |
|---|
| 115 | global $wpdb, $bp; |
|---|
| 116 | |
|---|
| 117 | $defaults = array( |
|---|
| 118 | 'profile_group_id' => false, |
|---|
| 119 | 'user_id' => bp_displayed_user_id(), |
|---|
| 120 | 'hide_empty_groups' => false, |
|---|
| 121 | 'hide_empty_fields' => false, |
|---|
| 122 | 'fetch_fields' => false, |
|---|
| 123 | 'fetch_field_data' => false, |
|---|
| 124 | 'fetch_visibility_level' => false, |
|---|
| 125 | 'exclude_groups' => false, |
|---|
| 126 | 'exclude_fields' => false |
|---|
| 127 | ); |
|---|
| 128 | |
|---|
| 129 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 130 | extract( $r, EXTR_SKIP ); |
|---|
| 131 | $where_sql = ''; |
|---|
| 132 | |
|---|
| 133 | if ( !empty( $profile_group_id ) ) |
|---|
| 134 | $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id ); |
|---|
| 135 | elseif ( $exclude_groups ) |
|---|
| 136 | $where_sql = $wpdb->prepare( "WHERE g.id NOT IN ({$exclude_groups})"); |
|---|
| 137 | |
|---|
| 138 | if ( !empty( $hide_empty_groups ) ) |
|---|
| 139 | $groups = $wpdb->get_results( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" ); |
|---|
| 140 | else |
|---|
| 141 | $groups = $wpdb->get_results( "SELECT DISTINCT g.* FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" ); |
|---|
| 142 | |
|---|
| 143 | if ( empty( $fetch_fields ) ) |
|---|
| 144 | return $groups; |
|---|
| 145 | |
|---|
| 146 | // Get the group ids |
|---|
| 147 | $group_ids = array(); |
|---|
| 148 | foreach( (array) $groups as $group ) { |
|---|
| 149 | $group_ids[] = $group->id; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | $group_ids = implode( ',', (array) $group_ids ); |
|---|
| 153 | |
|---|
| 154 | if ( empty( $group_ids ) ) |
|---|
| 155 | return $groups; |
|---|
| 156 | |
|---|
| 157 | // Support arrays and comma-separated strings |
|---|
| 158 | $exclude_fields_cs = wp_parse_id_list( $exclude_fields ); |
|---|
| 159 | |
|---|
| 160 | // Visibility - Handled here so as not to be overridden by sloppy use of the |
|---|
| 161 | // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user() |
|---|
| 162 | $exclude_fields_cs = array_merge( $exclude_fields_cs, bp_xprofile_get_hidden_fields_for_user( $user_id ) ); |
|---|
| 163 | $exclude_fields_cs = implode( ',', $exclude_fields_cs ); |
|---|
| 164 | |
|---|
| 165 | if ( !empty( $exclude_fields_cs ) ) { |
|---|
| 166 | $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})"; |
|---|
| 167 | } else { |
|---|
| 168 | $exclude_fields_sql = ''; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | // Fetch the fields |
|---|
| 172 | $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" ); |
|---|
| 173 | |
|---|
| 174 | if ( empty( $fields ) ) |
|---|
| 175 | return $groups; |
|---|
| 176 | |
|---|
| 177 | if ( ! empty( $fetch_field_data ) ) { |
|---|
| 178 | |
|---|
| 179 | // Fetch the field data for the user. |
|---|
| 180 | foreach( (array) $fields as $field ) { |
|---|
| 181 | $field_ids[] = $field->id; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | $field_ids_sql = implode( ',', (array) $field_ids ); |
|---|
| 185 | |
|---|
| 186 | if ( ! empty( $field_ids ) && ! empty( $user_id ) ) { |
|---|
| 187 | $field_data = $wpdb->get_results( $wpdb->prepare( "SELECT id, field_id, value FROM {$bp->profile->table_name_data} WHERE field_id IN ( {$field_ids_sql} ) AND user_id = %d", $user_id ) ); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | // Remove data-less fields, if necessary |
|---|
| 191 | if ( !empty( $hide_empty_fields ) ) { |
|---|
| 192 | |
|---|
| 193 | // Loop through the results and find the fields that have data. |
|---|
| 194 | foreach( (array) $field_data as $data ) { |
|---|
| 195 | |
|---|
| 196 | // Empty fields may contain a serialized empty array |
|---|
| 197 | $maybe_value = maybe_unserialize( $data->value ); |
|---|
| 198 | if ( !empty( $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) { |
|---|
| 199 | // Fields that have data get removed from the list |
|---|
| 200 | unset( $field_ids[$key] ); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | // The remaining members of $field_ids are empty. Remove them. |
|---|
| 205 | foreach( $fields as $field_key => $field ) { |
|---|
| 206 | if ( in_array( $field->id, $field_ids ) ) { |
|---|
| 207 | unset( $fields[$field_key] ); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | // Reset indexes |
|---|
| 212 | $fields = array_values( $fields ); |
|---|
| 213 | |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | // Field data was found |
|---|
| 217 | if ( !empty( $field_data ) && !is_wp_error( $field_data ) ) { |
|---|
| 218 | |
|---|
| 219 | // Loop through fields |
|---|
| 220 | foreach( (array) $fields as $field_key => $field ) { |
|---|
| 221 | |
|---|
| 222 | // Loop throught the data in each field |
|---|
| 223 | foreach( (array) $field_data as $data ) { |
|---|
| 224 | |
|---|
| 225 | // Assign correct data value to the field |
|---|
| 226 | if ( $field->id == $data->field_id ) { |
|---|
| 227 | $fields[$field_key]->data = new stdClass; |
|---|
| 228 | $fields[$field_key]->data->value = $data->value; |
|---|
| 229 | $fields[$field_key]->data->id = $data->id; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | if ( !empty( $fetch_visibility_level ) ) { |
|---|
| 236 | $fields = self::fetch_visibility_level( $user_id, $fields ); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | // Merge the field array back in with the group array |
|---|
| 241 | foreach( (array) $groups as $group ) { |
|---|
| 242 | |
|---|
| 243 | // Indexes may have been shifted after previous deletions, so we get a |
|---|
| 244 | // fresh one each time through the loop |
|---|
| 245 | $index = array_search( $group, $groups ); |
|---|
| 246 | |
|---|
| 247 | foreach( (array) $fields as $field ) { |
|---|
| 248 | if ( $group->id == $field->group_id ) { |
|---|
| 249 | $groups[$index]->fields[] = $field; |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | // When we unset fields above, we may have created empty groups. |
|---|
| 254 | // Remove them, if necessary. |
|---|
| 255 | if ( empty( $group->fields ) && $hide_empty_groups ) { |
|---|
| 256 | unset( $groups[$index] ); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | // Reset indexes |
|---|
| 260 | $groups = array_values( $groups ); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | return $groups; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | function admin_validate() { |
|---|
| 267 | global $message; |
|---|
| 268 | |
|---|
| 269 | /* Validate Form */ |
|---|
| 270 | if ( empty( $_POST['group_name'] ) ) { |
|---|
| 271 | $message = __( 'Please make sure you give the group a name.', 'buddypress' ); |
|---|
| 272 | return false; |
|---|
| 273 | } else { |
|---|
| 274 | return true; |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | function update_position( $field_group_id, $position ) { |
|---|
| 279 | global $wpdb, $bp; |
|---|
| 280 | |
|---|
| 281 | if ( !is_numeric( $position ) ) |
|---|
| 282 | return false; |
|---|
| 283 | |
|---|
| 284 | return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) ); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /** |
|---|
| 288 | * Fetch the field visibility level for the fields returned by the query |
|---|
| 289 | * |
|---|
| 290 | * @since BuddyPress (1.6) |
|---|
| 291 | * |
|---|
| 292 | * @param int $user_id The profile owner's user_id |
|---|
| 293 | * @param array $fields The database results returned by the get() query |
|---|
| 294 | * @return array $fields The database results, with field_visibility added |
|---|
| 295 | */ |
|---|
| 296 | function fetch_visibility_level( $user_id = 0, $fields = array() ) { |
|---|
| 297 | |
|---|
| 298 | // Get the user's visibility level preferences |
|---|
| 299 | $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); |
|---|
| 300 | |
|---|
| 301 | // Get the admin-set preferences |
|---|
| 302 | $admin_set_levels = self::fetch_default_visibility_levels(); |
|---|
| 303 | |
|---|
| 304 | foreach( (array) $fields as $key => $field ) { |
|---|
| 305 | |
|---|
| 306 | // Does the admin allow this field to be customized? |
|---|
| 307 | $allow_custom = empty( $admin_set_levels[$field->id]['allow_custom'] ) || 'allowed' == $admin_set_levels[$field->id]['allow_custom']; |
|---|
| 308 | |
|---|
| 309 | // Look to see if the user has set the visibility for this field |
|---|
| 310 | if ( $allow_custom && isset( $visibility_levels[$field->id] ) ) { |
|---|
| 311 | $field_visibility = $visibility_levels[$field->id]; |
|---|
| 312 | |
|---|
| 313 | // If no admin-set default is saved, fall back on a global default |
|---|
| 314 | } else { |
|---|
| 315 | $field_visibility = !empty( $admin_set_levels[$field->id]['default'] ) ? $admin_set_levels[$field->id]['default'] : apply_filters( 'bp_xprofile_default_visibility_level', 'public' ); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | $fields[$key]->visibility_level = $field_visibility; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | return $fields; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | /** |
|---|
| 325 | * Fetch the admin-set preferences for all fields |
|---|
| 326 | * |
|---|
| 327 | * @since BuddyPress (1.6) |
|---|
| 328 | * |
|---|
| 329 | * @return array $default_visibility_levels An array, keyed by field_id, of default |
|---|
| 330 | * visibility level + allow_custom (whether the admin allows this field to be set by user) |
|---|
| 331 | */ |
|---|
| 332 | function fetch_default_visibility_levels() { |
|---|
| 333 | global $wpdb, $bp; |
|---|
| 334 | |
|---|
| 335 | $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" ); |
|---|
| 336 | |
|---|
| 337 | // Arrange so that the field id is the key and the visibility level the value |
|---|
| 338 | $default_visibility_levels = array(); |
|---|
| 339 | foreach( $levels as $level ) { |
|---|
| 340 | if ( 'default_visibility' == $level->meta_key ) { |
|---|
| 341 | $default_visibility_levels[$level->object_id]['default'] = $level->meta_value; |
|---|
| 342 | } else if ( 'allow_custom_visibility' == $level->meta_key ) { |
|---|
| 343 | $default_visibility_levels[$level->object_id]['allow_custom'] = $level->meta_value; |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | return $default_visibility_levels; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | function render_admin_form() { |
|---|
| 351 | global $message; |
|---|
| 352 | |
|---|
| 353 | if ( empty( $this->id ) ) { |
|---|
| 354 | $title = __( 'Add New Field Group', 'buddypress' ); |
|---|
| 355 | $action = "admin.php?page=bp-profile-setup&mode=add_group"; |
|---|
| 356 | $button = __( 'Create Field Group', 'buddypress' ); |
|---|
| 357 | } else { |
|---|
| 358 | $title = __( 'Edit Field Group', 'buddypress' ); |
|---|
| 359 | $action = "admin.php?page=bp-profile-setup&mode=edit_group&group_id=" . $this->id; |
|---|
| 360 | $button = __( 'Save Changes', 'buddypress' ); |
|---|
| 361 | } ?> |
|---|
| 362 | |
|---|
| 363 | <div class="wrap"> |
|---|
| 364 | |
|---|
| 365 | <?php screen_icon( 'users' ); ?> |
|---|
| 366 | |
|---|
| 367 | <h2><?php echo esc_html( $title ); ?></h2> |
|---|
| 368 | |
|---|
| 369 | <?php if ( !empty( $message ) ) : |
|---|
| 370 | $type = ( 'error' == $type ) ? 'error' : 'updated'; ?> |
|---|
| 371 | |
|---|
| 372 | <div id="message" class="<?php echo esc_attr( $type ); ?> fade"> |
|---|
| 373 | <p><?php echo esc_html( $message ); ?></p> |
|---|
| 374 | </div> |
|---|
| 375 | |
|---|
| 376 | <?php endif; ?> |
|---|
| 377 | |
|---|
| 378 | <form action="<?php echo esc_url( $action ); ?>" method="post"> |
|---|
| 379 | <div id="poststuff"> |
|---|
| 380 | <div id="post-body" class="metabox-holder columns-2"> |
|---|
| 381 | <div id="post-body-content"> |
|---|
| 382 | <div id="titlediv"> |
|---|
| 383 | <div id="titlewrap"> |
|---|
| 384 | <label class="screen-reader-text" id="title-prompt-text" for="title"><?php _e( 'Field Group Title', 'buddypress') ?></label> |
|---|
| 385 | <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" /> |
|---|
| 386 | </div> |
|---|
| 387 | </div> |
|---|
| 388 | |
|---|
| 389 | <div id="postdiv" class="postarea"> |
|---|
| 390 | <div class="postbox"> |
|---|
| 391 | <div id="titlediv"><h3 class="hndle"><?php _e( 'Group Description', 'buddypress' ); ?></h3></div> |
|---|
| 392 | <div class="inside"> |
|---|
| 393 | <textarea name="group_description" id="group_description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea> |
|---|
| 394 | </div> |
|---|
| 395 | </div> |
|---|
| 396 | </div> |
|---|
| 397 | </div> |
|---|
| 398 | <div id="postbox-container-1" class="postbox-container"> |
|---|
| 399 | <div id="side-sortables" class="meta-box-sortables ui-sortable"> |
|---|
| 400 | <div id="submitdiv" class="postbox"> |
|---|
| 401 | <div id="handlediv"><h3 class="hndle"><?php _e( 'Save', 'buddypress' ); ?></h3></div> |
|---|
| 402 | <div class="inside"> |
|---|
| 403 | <div id="submitcomment" class="submitbox"> |
|---|
| 404 | <div id="major-publishing-actions"> |
|---|
| 405 | <div id="delete-action"> |
|---|
| 406 | <a href="admin.php?page=bp-profile-setup" class="submitdelete deletion"><?php _e( 'Cancel', 'buddypress' ); ?></a> |
|---|
| 407 | </div> |
|---|
| 408 | <div id="publishing-action"> |
|---|
| 409 | <input type="submit" name="save_group" value="<?php echo esc_attr( $button ); ?>" class="button-primary"/> |
|---|
| 410 | </div> |
|---|
| 411 | <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" /> |
|---|
| 412 | <div class="clear"></div> |
|---|
| 413 | </div> |
|---|
| 414 | </div> |
|---|
| 415 | </div> |
|---|
| 416 | </div> |
|---|
| 417 | </div> |
|---|
| 418 | </div> |
|---|
| 419 | </div> |
|---|
| 420 | </div> |
|---|
| 421 | </form> |
|---|
| 422 | </div> |
|---|
| 423 | |
|---|
| 424 | <?php |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | class BP_XProfile_Field { |
|---|
| 429 | var $id; |
|---|
| 430 | var $group_id; |
|---|
| 431 | var $parent_id; |
|---|
| 432 | var $type; |
|---|
| 433 | var $name; |
|---|
| 434 | var $description; |
|---|
| 435 | var $is_required; |
|---|
| 436 | var $can_delete; |
|---|
| 437 | var $field_order; |
|---|
| 438 | var $option_order; |
|---|
| 439 | var $order_by; |
|---|
| 440 | var $is_default_option; |
|---|
| 441 | var $default_visibility; |
|---|
| 442 | var $allow_custom_visibility = 'allowed'; |
|---|
| 443 | |
|---|
| 444 | var $data; |
|---|
| 445 | var $message = null; |
|---|
| 446 | var $message_type = 'err'; |
|---|
| 447 | |
|---|
| 448 | function __construct( $id = null, $user_id = null, $get_data = true ) { |
|---|
| 449 | if ( !empty( $id ) ) |
|---|
| 450 | $this->populate( $id, $user_id, $get_data ); |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | function populate( $id, $user_id, $get_data ) { |
|---|
| 454 | global $wpdb, $userdata, $bp; |
|---|
| 455 | |
|---|
| 456 | // @todo Why are we nooping the user_id ? |
|---|
| 457 | $user_id = 0; |
|---|
| 458 | if ( is_null( $user_id ) ) |
|---|
| 459 | $user_id = $userdata->ID; |
|---|
| 460 | |
|---|
| 461 | $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id ); |
|---|
| 462 | |
|---|
| 463 | if ( $field = $wpdb->get_row( $sql ) ) { |
|---|
| 464 | $this->id = $field->id; |
|---|
| 465 | $this->group_id = $field->group_id; |
|---|
| 466 | $this->parent_id = $field->parent_id; |
|---|
| 467 | $this->type = $field->type; |
|---|
| 468 | $this->name = stripslashes( $field->name ); |
|---|
| 469 | $this->description = stripslashes( $field->description ); |
|---|
| 470 | $this->is_required = $field->is_required; |
|---|
| 471 | $this->can_delete = $field->can_delete; |
|---|
| 472 | $this->field_order = $field->field_order; |
|---|
| 473 | $this->option_order = $field->option_order; |
|---|
| 474 | $this->order_by = $field->order_by; |
|---|
| 475 | $this->is_default_option = $field->is_default_option; |
|---|
| 476 | |
|---|
| 477 | if ( $get_data && $user_id ) { |
|---|
| 478 | $this->data = $this->get_field_data( $user_id ); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | $this->default_visibility = bp_xprofile_get_meta( $id, 'field', 'default_visibility' ); |
|---|
| 482 | |
|---|
| 483 | if ( empty( $this->default_visibility ) ) { |
|---|
| 484 | $this->default_visibility = 'public'; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | $this->allow_custom_visibility = 'disabled' == bp_xprofile_get_meta( $id, 'field', 'allow_custom_visibility' ) ? 'disabled' : 'allowed'; |
|---|
| 488 | } |
|---|
| 489 | } |
|---|
| 490 | |
|---|
| 491 | function delete( $delete_data = false ) { |
|---|
| 492 | global $wpdb, $bp; |
|---|
| 493 | |
|---|
| 494 | // Prevent deletion if no ID is present |
|---|
| 495 | // Prevent deletion by url when can_delete is false. |
|---|
| 496 | // Prevent deletion of option 1 since this invalidates fields with options. |
|---|
| 497 | if ( empty( $this->id ) || empty( $this->can_delete ) || ( $this->parent_id && $this->option_order == 1 ) ) |
|---|
| 498 | return false; |
|---|
| 499 | |
|---|
| 500 | if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id ) ) ) |
|---|
| 501 | return false; |
|---|
| 502 | |
|---|
| 503 | // delete the data in the DB for this field |
|---|
| 504 | if ( true === $delete_data ) |
|---|
| 505 | BP_XProfile_ProfileData::delete_for_field( $this->id ); |
|---|
| 506 | |
|---|
| 507 | return true; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | function save() { |
|---|
| 511 | global $wpdb, $bp; |
|---|
| 512 | |
|---|
| 513 | $this->group_id = apply_filters( 'xprofile_field_group_id_before_save', $this->group_id, $this->id ); |
|---|
| 514 | $this->parent_id = apply_filters( 'xprofile_field_parent_id_before_save', $this->parent_id, $this->id ); |
|---|
| 515 | $this->type = apply_filters( 'xprofile_field_type_before_save', $this->type, $this->id ); |
|---|
| 516 | $this->name = apply_filters( 'xprofile_field_name_before_save', $this->name, $this->id ); |
|---|
| 517 | $this->description = apply_filters( 'xprofile_field_description_before_save', $this->description, $this->id ); |
|---|
| 518 | $this->is_required = apply_filters( 'xprofile_field_is_required_before_save', $this->is_required, $this->id ); |
|---|
| 519 | $this->order_by = apply_filters( 'xprofile_field_order_by_before_save', $this->order_by, $this->id ); |
|---|
| 520 | $this->field_order = apply_filters( 'xprofile_field_field_order_before_save', $this->field_order, $this->id ); |
|---|
| 521 | |
|---|
| 522 | do_action_ref_array( 'xprofile_field_before_save', array( $this ) ); |
|---|
| 523 | |
|---|
| 524 | if ( $this->id != null ) { |
|---|
| 525 | $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d, parent_id = 0, type = %s, name = %s, description = %s, is_required = %d, order_by = %s, field_order = %d WHERE id = %d", $this->group_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->id ); |
|---|
| 526 | } else { |
|---|
| 527 | $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, order_by, field_order ) VALUES (%d, %d, %s, %s, %s, %d, %s, %d )", $this->group_id, $this->parent_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order ); |
|---|
| 528 | } |
|---|
| 529 | |
|---|
| 530 | /** |
|---|
| 531 | * Check for null so field options can be changed without changing any other part of the field. |
|---|
| 532 | * The described situation will return 0 here. |
|---|
| 533 | */ |
|---|
| 534 | if ( $wpdb->query( $sql ) !== null ) { |
|---|
| 535 | |
|---|
| 536 | if ( !empty( $this->id ) ) { |
|---|
| 537 | $field_id = $this->id; |
|---|
| 538 | } else { |
|---|
| 539 | $field_id = $wpdb->insert_id; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | // Only do this if we are editing an existing field |
|---|
| 543 | if ( $this->id != null ) { |
|---|
| 544 | |
|---|
| 545 | /** |
|---|
| 546 | * Remove any radio or dropdown options for this |
|---|
| 547 | * field. They will be re-added if needed. |
|---|
| 548 | * This stops orphan options if the user changes a |
|---|
| 549 | * field from a radio button field to a text box. |
|---|
| 550 | */ |
|---|
| 551 | $this->delete_children(); |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | /** |
|---|
| 555 | * Check to see if this is a field with child options. |
|---|
| 556 | * We need to add the options to the db, if it is. |
|---|
| 557 | */ |
|---|
| 558 | if ( 'radio' == $this->type || 'selectbox' == $this->type || 'checkbox' == $this->type || 'multiselectbox' == $this->type ) { |
|---|
| 559 | |
|---|
| 560 | if ( !empty( $this->id ) ) { |
|---|
| 561 | $parent_id = $this->id; |
|---|
| 562 | } else { |
|---|
| 563 | $parent_id = $wpdb->insert_id; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | if ( 'radio' == $this->type ) { |
|---|
| 567 | $post_option = !empty( $_POST['radio_option'] ) ? $_POST['radio_option'] : ''; |
|---|
| 568 | $post_default = !empty( $_POST['isDefault_radio_option'] ) ? $_POST['isDefault_radio_option'] : ''; |
|---|
| 569 | |
|---|
| 570 | $options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'radio' ); |
|---|
| 571 | $defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'radio' ); |
|---|
| 572 | |
|---|
| 573 | } elseif ( 'selectbox' == $this->type ) { |
|---|
| 574 | $post_option = !empty( $_POST['selectbox_option'] ) ? $_POST['selectbox_option'] : ''; |
|---|
| 575 | $post_default = !empty( $_POST['isDefault_selectbox_option'] ) ? $_POST['isDefault_selectbox_option'] : ''; |
|---|
| 576 | |
|---|
| 577 | $options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'selectbox' ); |
|---|
| 578 | $defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'selectbox' ); |
|---|
| 579 | |
|---|
| 580 | } elseif ( 'multiselectbox' == $this->type ) { |
|---|
| 581 | $post_option = !empty( $_POST['multiselectbox_option'] ) ? $_POST['multiselectbox_option'] : ''; |
|---|
| 582 | $post_default = !empty( $_POST['isDefault_multiselectbox_option'] ) ? $_POST['isDefault_multiselectbox_option'] : ''; |
|---|
| 583 | |
|---|
| 584 | $options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'multiselectbox' ); |
|---|
| 585 | $defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'multiselectbox' ); |
|---|
| 586 | |
|---|
| 587 | } elseif ( 'checkbox' == $this->type ) { |
|---|
| 588 | $post_option = !empty( $_POST['checkbox_option'] ) ? $_POST['checkbox_option'] : ''; |
|---|
| 589 | $post_default = !empty( $_POST['isDefault_checkbox_option'] ) ? $_POST['isDefault_checkbox_option'] : ''; |
|---|
| 590 | |
|---|
| 591 | $options = apply_filters( 'xprofile_field_options_before_save', $post_option, 'checkbox' ); |
|---|
| 592 | $defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, 'checkbox' ); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | $counter = 1; |
|---|
| 596 | if ( !empty( $options ) ) { |
|---|
| 597 | foreach ( (array) $options as $option_key => $option_value ) { |
|---|
| 598 | $is_default = 0; |
|---|
| 599 | |
|---|
| 600 | if ( is_array( $defaults ) ) { |
|---|
| 601 | if ( isset( $defaults[$option_key] ) ) |
|---|
| 602 | $is_default = 1; |
|---|
| 603 | } else { |
|---|
| 604 | if ( (int) $defaults == $option_key ) |
|---|
| 605 | $is_default = 1; |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | if ( '' != $option_value ) { |
|---|
| 609 | if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order, is_default_option) VALUES (%d, %d, 'option', %s, '', 0, %d, %d)", $this->group_id, $parent_id, $option_value, $counter, $is_default ) ) ) { |
|---|
| 610 | return false; |
|---|
| 611 | } |
|---|
| 612 | } |
|---|
| 613 | |
|---|
| 614 | $counter++; |
|---|
| 615 | } |
|---|
| 616 | } |
|---|
| 617 | } |
|---|
| 618 | |
|---|
| 619 | do_action_ref_array( 'xprofile_field_after_save', array( $this ) ); |
|---|
| 620 | |
|---|
| 621 | return $field_id; |
|---|
| 622 | } else { |
|---|
| 623 | return false; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | function get_field_data( $user_id ) { |
|---|
| 628 | return new BP_XProfile_ProfileData( $this->id, $user_id ); |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | function get_children( $for_editing = false ) { |
|---|
| 632 | global $wpdb, $bp; |
|---|
| 633 | |
|---|
| 634 | // This is done here so we don't have problems with sql injection |
|---|
| 635 | if ( 'asc' == $this->order_by && empty( $for_editing ) ) { |
|---|
| 636 | $sort_sql = 'ORDER BY name ASC'; |
|---|
| 637 | } elseif ( 'desc' == $this->order_by && empty( $for_editing ) ) { |
|---|
| 638 | $sort_sql = 'ORDER BY name DESC'; |
|---|
| 639 | } else { |
|---|
| 640 | $sort_sql = 'ORDER BY option_order ASC'; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | // This eliminates a problem with getting all fields when there is no id for the object |
|---|
| 644 | if ( empty( $this->id ) ) { |
|---|
| 645 | $parent_id = -1; |
|---|
| 646 | } else { |
|---|
| 647 | $parent_id = $this->id; |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE parent_id = %d AND group_id = %d $sort_sql", $parent_id, $this->group_id ); |
|---|
| 651 | |
|---|
| 652 | if ( !$children = $wpdb->get_results( $sql ) ) |
|---|
| 653 | return false; |
|---|
| 654 | |
|---|
| 655 | return apply_filters( 'bp_xprofile_field_get_children', $children ); |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | function delete_children() { |
|---|
| 659 | global $wpdb, $bp; |
|---|
| 660 | |
|---|
| 661 | $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE parent_id = %d", $this->id ); |
|---|
| 662 | |
|---|
| 663 | $wpdb->query( $sql ); |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | /* Static Functions */ |
|---|
| 667 | |
|---|
| 668 | function get_type( $field_id ) { |
|---|
| 669 | global $wpdb, $bp; |
|---|
| 670 | |
|---|
| 671 | if ( !empty( $field_id ) ) { |
|---|
| 672 | $sql = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ); |
|---|
| 673 | |
|---|
| 674 | if ( !$field_type = $wpdb->get_var( $sql ) ) { |
|---|
| 675 | return false; |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | return $field_type; |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | return false; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | function delete_for_group( $group_id ) { |
|---|
| 685 | global $wpdb, $bp; |
|---|
| 686 | |
|---|
| 687 | if ( !empty( $group_id ) ) { |
|---|
| 688 | $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id ); |
|---|
| 689 | |
|---|
| 690 | if ( $wpdb->get_var( $sql ) === false ) { |
|---|
| 691 | return false; |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | return true; |
|---|
| 695 | } |
|---|
| 696 | |
|---|
| 697 | return false; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | function get_id_from_name( $field_name ) { |
|---|
| 701 | global $wpdb, $bp; |
|---|
| 702 | |
|---|
| 703 | if ( empty( $bp->profile->table_name_fields ) || !isset( $field_name ) ) |
|---|
| 704 | return false; |
|---|
| 705 | |
|---|
| 706 | return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", $field_name ) ); |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | function update_position( $field_id, $position, $field_group_id ) { |
|---|
| 710 | global $wpdb, $bp; |
|---|
| 711 | |
|---|
| 712 | if ( !is_numeric( $position ) || !is_numeric( $field_group_id ) ) |
|---|
| 713 | return false; |
|---|
| 714 | |
|---|
| 715 | // Update $field_id with new $position and $field_group_id |
|---|
| 716 | if ( $parent = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id ) ) ) {; |
|---|
| 717 | |
|---|
| 718 | // Update any children of this $field_id |
|---|
| 719 | $children = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id ) ); |
|---|
| 720 | |
|---|
| 721 | return $parent; |
|---|
| 722 | } |
|---|
| 723 | |
|---|
| 724 | return false; |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | /* ADMIN AREA HTML. |
|---|
| 728 | * TODO: Get this out of here and replace with standard template loops |
|---|
| 729 | */ |
|---|
| 730 | |
|---|
| 731 | /* This function populates the items for radio buttons checkboxes and drop down boxes */ |
|---|
| 732 | function render_admin_form_children() { |
|---|
| 733 | $input_types = array( 'checkbox', 'selectbox', 'multiselectbox', 'radio' ); |
|---|
| 734 | |
|---|
| 735 | foreach ( $input_types as $type ) { |
|---|
| 736 | $default_name = ''; |
|---|
| 737 | |
|---|
| 738 | if ( ( 'multiselectbox' == $type ) || ( 'checkbox' == $type ) ) { |
|---|
| 739 | $default_input = 'checkbox'; |
|---|
| 740 | } else { |
|---|
| 741 | $default_input = 'radio'; |
|---|
| 742 | } |
|---|
| 743 | |
|---|
| 744 | $class = $this->type != $type ? 'display: none;' : ''; |
|---|
| 745 | |
|---|
| 746 | if ( empty( $this->default_visibility ) ) { |
|---|
| 747 | $this->default_visibility = 'public'; |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | ?> |
|---|
| 751 | |
|---|
| 752 | <div id="<?php echo esc_attr( $type ); ?>" class="options-box" style="<?php echo esc_attr( $class ); ?> margin-left: 15px;"> |
|---|
| 753 | <h4><?php _e( 'Please enter options for this Field:', 'buddypress' ); ?></h4> |
|---|
| 754 | <p> |
|---|
| 755 | <label for="sort_order_<?php echo esc_attr( $type ); ?>"><?php _e( 'Sort Order:', 'buddypress' ); ?></label> |
|---|
| 756 | <select name="sort_order_<?php echo esc_attr( $type ); ?>" id="sort_order_<?php echo esc_attr( $type ); ?>" > |
|---|
| 757 | <option value="custom" <?php selected( 'custom', $this->order_by ); ?>><?php _e( 'Custom', 'buddypress' ); ?></option> |
|---|
| 758 | <option value="asc" <?php selected( 'asc', $this->order_by ); ?>><?php _e( 'Ascending', 'buddypress' ); ?></option> |
|---|
| 759 | <option value="desc" <?php selected( 'desc', $this->order_by ); ?>><?php _e( 'Descending', 'buddypress' ); ?></option> |
|---|
| 760 | </select> |
|---|
| 761 | |
|---|
| 762 | <?php if ( !$options = $this->get_children( true ) ) { |
|---|
| 763 | |
|---|
| 764 | $i = 1; |
|---|
| 765 | while ( isset( $_POST[$type . '_option'][$i] ) ) { |
|---|
| 766 | (array) $options[] = (object) array( |
|---|
| 767 | 'id' => -1, |
|---|
| 768 | 'name' => $_POST[$type . '_option'][$i], |
|---|
| 769 | 'is_default_option' => ( ( 'multiselectbox' != $type ) && ( 'checkbox' != $type ) && ( $_POST["isDefault_{$type}_option"] == $i ) ) ? 1 : $_POST["isDefault_{$type}_option"][$i] |
|---|
| 770 | ); |
|---|
| 771 | |
|---|
| 772 | ++$i; |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | debug($options,false); |
|---|
| 776 | if ( !empty( $options ) ) { |
|---|
| 777 | for ( $i = 0, $count = count( $options ); $i < $count; ++$i ) { |
|---|
| 778 | $j = $i + 1; |
|---|
| 779 | |
|---|
| 780 | if ( 'multiselectbox' == $type || 'checkbox' == $type ) |
|---|
| 781 | $default_name = '[' . $j . ']'; ?> |
|---|
| 782 | |
|---|
| 783 | <p class="sortable"> |
|---|
| 784 | <span> Ξ </span> |
|---|
| 785 | <input type="text" name="<?php echo esc_attr( $type ); ?>_option[<?php echo esc_attr( $j ); ?>]" id="<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $j ); ?>" value="<?php echo stripslashes( esc_attr( $options[$i]->name ) ); ?>" /> |
|---|
| 786 | <input type="<?php echo $default_input; ?>" name="isDefault_<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $default_name ); ?>" <?php checked( (int) $options[$i]->is_default_option, true ); ?> value="<?php echo esc_attr( $j ); ?>" /> |
|---|
| 787 | <span><?php _e( 'Default Value', 'buddypress' ); ?></span> |
|---|
| 788 | <a href="<?php echo esc_url( 'admin.php?page=bp-profile-setup&mode=delete_option&option_id=' . $options[$i]->id ); ?>" class="ajax-option-delete" id="delete-<?php echo esc_attr( $options[$i]->id ); ?>">[x]</a> |
|---|
| 789 | </p> |
|---|
| 790 | |
|---|
| 791 | <?php } /* end for */ ?> |
|---|
| 792 | |
|---|
| 793 | <input type="hidden" name="<?php echo esc_attr( $type ); ?>_option_number" id="<?php echo esc_attr( $type ); ?>_option_number" value="<?php echo esc_attr( (int) $j + 1 ); ?>" /> |
|---|
| 794 | |
|---|
| 795 | <?php } else { |
|---|
| 796 | |
|---|
| 797 | if ( 'multiselectbox' == $type || 'checkbox' == $type ) |
|---|
| 798 | $default_name = '[1]'; ?> |
|---|
| 799 | |
|---|
| 800 | <p class="sortable"> |
|---|
| 801 | <span> Ξ </span> |
|---|
| 802 | <input type="text" name="<?php echo esc_attr( $type ); ?>_option[1]" id="<?php echo esc_attr( $type ); ?>_option1" /> |
|---|
| 803 | <input type="<?php echo esc_attr( $default_input ); ?>" name="isDefault_<?php echo esc_attr( $type ); ?>_option<?php echo esc_attr( $default_name ); ?>" id="isDefault_<?php echo esc_attr( $type ); ?>_option" value="1" /> |
|---|
| 804 | <span><?php _e( 'Default Value', 'buddypress' ); ?></span> |
|---|
| 805 | <input type="hidden" name="<?php echo esc_attr( $type ); ?>_option_number" id="<?php echo esc_attr( $type ); ?>_option_number" value="2" /> |
|---|
| 806 | </p> |
|---|
| 807 | |
|---|
| 808 | <?php } /* end if */ ?> |
|---|
| 809 | |
|---|
| 810 | <div id="<?php echo esc_attr( $type ); ?>_more"></div> |
|---|
| 811 | <p><a href="javascript:add_option('<?php echo esc_attr( $type ); ?>')"><?php _e( 'Add Another Option', 'buddypress' ); ?></a></p> |
|---|
| 812 | </div> |
|---|
| 813 | |
|---|
| 814 | <?php } |
|---|
| 815 | } |
|---|
| 816 | |
|---|
| 817 | function render_admin_form( $message = '' ) { |
|---|
| 818 | if ( empty( $this->id ) ) { |
|---|
| 819 | $title = __( 'Add Field', 'buddypress' ); |
|---|
| 820 | $action = "admin.php?page=bp-profile-setup&group_id=" . $this->group_id . "&mode=add_field#tabs-" . $this->group_id; |
|---|
| 821 | |
|---|
| 822 | if ( !empty( $_POST['saveField'] ) ) { |
|---|
| 823 | $this->name = $_POST['title']; |
|---|
| 824 | $this->description = $_POST['description']; |
|---|
| 825 | $this->is_required = $_POST['required']; |
|---|
| 826 | $this->type = $_POST['fieldtype']; |
|---|
| 827 | $this->order_by = $_POST["sort_order_{$this->type}"]; |
|---|
| 828 | $this->field_order = $_POST['field_order']; |
|---|
| 829 | } |
|---|
| 830 | } else { |
|---|
| 831 | $title = __( 'Edit Field', 'buddypress' ); |
|---|
| 832 | $action = "admin.php?page=bp-profile-setup&mode=edit_field&group_id=" . $this->group_id . "&field_id=" . $this->id . "#tabs-" . $this->group_id; |
|---|
| 833 | } ?> |
|---|
| 834 | |
|---|
| 835 | <div class="wrap"> |
|---|
| 836 | <div id="icon-users" class="icon32"><br /></div> |
|---|
| 837 | <h2><?php echo esc_html( $title ); ?></h2> |
|---|
| 838 | <p><?php _e( 'Fields marked * are required', 'buddypress' ) ?></p> |
|---|
| 839 | |
|---|
| 840 | <?php if ( !empty( $message ) ) : ?> |
|---|
| 841 | |
|---|
| 842 | <div id="message" class="error fade"> |
|---|
| 843 | <p><?php echo esc_html( $message ); ?></p> |
|---|
| 844 | </div> |
|---|
| 845 | |
|---|
| 846 | <?php endif; ?> |
|---|
| 847 | |
|---|
| 848 | <form action="<?php echo esc_url( $action ); ?>" method="post"> |
|---|
| 849 | <div id="poststuff"> |
|---|
| 850 | <div id="titlediv"> |
|---|
| 851 | <h3><label for="title"><?php _e( 'Field Title', 'buddypress' ); ?> *</label></h3> |
|---|
| 852 | <div id="titlewrap"> |
|---|
| 853 | <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" style="width:50%" /> |
|---|
| 854 | </div> |
|---|
| 855 | </div> |
|---|
| 856 | |
|---|
| 857 | <div id="titlediv"> |
|---|
| 858 | <h3><label for="description"><?php _e("Field Description", 'buddypress'); ?></label></h3> |
|---|
| 859 | <div id="titlewrap"> |
|---|
| 860 | <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea> |
|---|
| 861 | </div> |
|---|
| 862 | </div> |
|---|
| 863 | |
|---|
| 864 | <?php if ( '0' != $this->can_delete ) : ?> |
|---|
| 865 | |
|---|
| 866 | <div id="titlediv"> |
|---|
| 867 | <h3><label for="required"><?php _e( "Is This Field Required?", 'buddypress' ); ?> *</label></h3> |
|---|
| 868 | <select name="required" id="required" style="width: 30%"> |
|---|
| 869 | <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php _e( 'Not Required', 'buddypress' ); ?></option> |
|---|
| 870 | <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php _e( 'Required', 'buddypress' ); ?></option> |
|---|
| 871 | </select> |
|---|
| 872 | </div> |
|---|
| 873 | |
|---|
| 874 | <div id="titlediv"> |
|---|
| 875 | <h3><label for="fieldtype"><?php _e( 'Field Type', 'buddypress'); ?> *</label></h3> |
|---|
| 876 | <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)" style="width: 30%"> |
|---|
| 877 | <option value="textbox" <?php selected( $this->type, 'textbox' ); ?>><?php _e( 'Text Box', 'buddypress' ); ?></option> |
|---|
| 878 | <option value="textarea" <?php selected( $this->type, 'textarea' ); ?>><?php _e( 'Multi-line Text Box', 'buddypress' ); ?></option> |
|---|
| 879 | <option value="datebox" <?php selected( $this->type, 'datebox' ); ?>><?php _e( 'Date Selector', 'buddypress' ); ?></option> |
|---|
| 880 | <option value="radio" <?php selected( $this->type, 'radio' ); ?>><?php _e( 'Radio Buttons', 'buddypress' ); ?></option> |
|---|
| 881 | <option value="selectbox" <?php selected( $this->type, 'selectbox' ); ?>><?php _e( 'Drop Down Select Box', 'buddypress' ); ?></option> |
|---|
| 882 | <option value="multiselectbox" <?php selected( $this->type, 'multiselectbox' ); ?>><?php _e( 'Multi Select Box', 'buddypress' ); ?></option> |
|---|
| 883 | <option value="checkbox" <?php selected( $this->type, 'checkbox' ); ?>><?php _e( 'Checkboxes', 'buddypress' ); ?></option> |
|---|
| 884 | </select> |
|---|
| 885 | </div> |
|---|
| 886 | |
|---|
| 887 | <?php do_action_ref_array( 'xprofile_field_additional_options', array( $this ) ); ?> |
|---|
| 888 | |
|---|
| 889 | <?php $this->render_admin_form_children(); ?> |
|---|
| 890 | |
|---|
| 891 | <?php else : ?> |
|---|
| 892 | |
|---|
| 893 | <input type="hidden" name="required" id="required" value="1" /> |
|---|
| 894 | <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" /> |
|---|
| 895 | |
|---|
| 896 | <?php endif; |
|---|
| 897 | |
|---|
| 898 | /* The fullname field cannot be hidden */ |
|---|
| 899 | if ( 1 != $this->id ) : ?> |
|---|
| 900 | |
|---|
| 901 | <div id="titlediv"> |
|---|
| 902 | <div id="titlewrap"> |
|---|
| 903 | <h3><label for="default-visibility"><?php _e( 'Default Visibility', 'buddypress' ); ?></label></h3> |
|---|
| 904 | <ul> |
|---|
| 905 | |
|---|
| 906 | <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?> |
|---|
| 907 | |
|---|
| 908 | <li><input type="radio" name="default-visibility" value="<?php echo esc_attr( $level['id'] ) ?>" <?php checked( $this->default_visibility, $level['id'] ); ?>> <?php echo esc_html( $level['label'] ) ?></li> |
|---|
| 909 | |
|---|
| 910 | <?php endforeach ?> |
|---|
| 911 | |
|---|
| 912 | </ul> |
|---|
| 913 | </div> |
|---|
| 914 | |
|---|
| 915 | <div id="titlewrap"> |
|---|
| 916 | <h3><label for="allow-custom-visibility"><?php _e( 'Per-Member Visibility', 'buddypress' ); ?></label></h3> |
|---|
| 917 | <ul> |
|---|
| 918 | <li><input type="radio" name="allow-custom-visibility" value="allowed" <?php checked( $this->allow_custom_visibility, 'allowed' ); ?>> <?php _e( "Let members change this field's visibility", 'buddypress' ); ?></li> |
|---|
| 919 | <li><input type="radio" name="allow-custom-visibility" value="disabled" <?php checked( $this->allow_custom_visibility, 'disabled' ); ?>> <?php _e( 'Enforce the default visibility for all members', 'buddypress' ); ?></li> |
|---|
| 920 | </ul> |
|---|
| 921 | </div> |
|---|
| 922 | </div> |
|---|
| 923 | |
|---|
| 924 | <?php endif ?> |
|---|
| 925 | |
|---|
| 926 | <p class="submit"> |
|---|
| 927 | <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" /> |
|---|
| 928 | <input type="submit" value="<?php _e( 'Save', 'buddypress' ); ?>" name="saveField" id="saveField" style="font-weight: bold" class="button-primary" /> |
|---|
| 929 | <?php _e( 'or', 'buddypress' ); ?> <a href="admin.php?page=bp-profile-setup" class="deletion"><?php _e( 'Cancel', 'buddypress' ); ?></a> |
|---|
| 930 | </p> |
|---|
| 931 | |
|---|
| 932 | </div> |
|---|
| 933 | |
|---|
| 934 | <?php wp_nonce_field( 'xprofile_delete_option' ); ?> |
|---|
| 935 | |
|---|
| 936 | </form> |
|---|
| 937 | </div> |
|---|
| 938 | |
|---|
| 939 | <?php |
|---|
| 940 | } |
|---|
| 941 | |
|---|
| 942 | function admin_validate() { |
|---|
| 943 | global $message; |
|---|
| 944 | |
|---|
| 945 | // Validate Form |
|---|
| 946 | if ( '' == $_POST['title'] || '' == $_POST['required'] || '' == $_POST['fieldtype'] ) { |
|---|
| 947 | $message = __( 'Please make sure you fill out all required fields.', 'buddypress' ); |
|---|
| 948 | return false; |
|---|
| 949 | } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'radio' && empty( $_POST['radio_option'][1] ) ) { |
|---|
| 950 | $message = __( 'Radio button field types require at least one option. Please add options below.', 'buddypress' ); |
|---|
| 951 | return false; |
|---|
| 952 | } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'selectbox' && empty( $_POST['selectbox_option'][1] ) ) { |
|---|
| 953 | $message = __( 'Select box field types require at least one option. Please add options below.', 'buddypress' ); |
|---|
| 954 | return false; |
|---|
| 955 | } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'multiselectbox' && empty( $_POST['multiselectbox_option'][1] ) ) { |
|---|
| 956 | $message = __( 'Select box field types require at least one option. Please add options below.', 'buddypress' ); |
|---|
| 957 | return false; |
|---|
| 958 | } else if ( empty( $_POST['field_file'] ) && $_POST['fieldtype'] == 'checkbox' && empty( $_POST['checkbox_option'][1] ) ) { |
|---|
| 959 | $message = __( 'Checkbox field types require at least one option. Please add options below.', 'buddypress' ); |
|---|
| 960 | return false; |
|---|
| 961 | } else { |
|---|
| 962 | return true; |
|---|
| 963 | } |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | class BP_XProfile_ProfileData { |
|---|
| 969 | var $id; |
|---|
| 970 | var $user_id; |
|---|
| 971 | var $field_id; |
|---|
| 972 | var $value; |
|---|
| 973 | var $last_updated; |
|---|
| 974 | |
|---|
| 975 | function __construct( $field_id = null, $user_id = null ) { |
|---|
| 976 | if ( !empty( $field_id ) ) { |
|---|
| 977 | $this->populate( $field_id, $user_id ); |
|---|
| 978 | } |
|---|
| 979 | } |
|---|
| 980 | |
|---|
| 981 | function populate( $field_id, $user_id ) { |
|---|
| 982 | global $wpdb, $bp; |
|---|
| 983 | |
|---|
| 984 | $sql = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ); |
|---|
| 985 | |
|---|
| 986 | if ( $profiledata = $wpdb->get_row( $sql ) ) { |
|---|
| 987 | $this->id = $profiledata->id; |
|---|
| 988 | $this->user_id = $profiledata->user_id; |
|---|
| 989 | $this->field_id = $profiledata->field_id; |
|---|
| 990 | $this->value = stripslashes( $profiledata->value ); |
|---|
| 991 | $this->last_updated = $profiledata->last_updated; |
|---|
| 992 | } else { |
|---|
| 993 | // When no row is found, we'll need to set these properties manually |
|---|
| 994 | $this->field_id = $field_id; |
|---|
| 995 | $this->user_id = $user_id; |
|---|
| 996 | } |
|---|
| 997 | } |
|---|
| 998 | |
|---|
| 999 | /** |
|---|
| 1000 | * exists () |
|---|
| 1001 | * |
|---|
| 1002 | * Check if there is data already for the user. |
|---|
| 1003 | * |
|---|
| 1004 | * @global object $wpdb |
|---|
| 1005 | * @global array $bp |
|---|
| 1006 | * @return bool |
|---|
| 1007 | */ |
|---|
| 1008 | function exists() { |
|---|
| 1009 | global $wpdb, $bp; |
|---|
| 1010 | |
|---|
| 1011 | $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE user_id = %d AND field_id = %d", $this->user_id, $this->field_id ) ); |
|---|
| 1012 | |
|---|
| 1013 | return apply_filters_ref_array( 'xprofile_data_exists', array( (bool)$retval, $this ) ); |
|---|
| 1014 | } |
|---|
| 1015 | |
|---|
| 1016 | /** |
|---|
| 1017 | * is_valid_field() |
|---|
| 1018 | * |
|---|
| 1019 | * Check if this data is for a valid field. |
|---|
| 1020 | * |
|---|
| 1021 | * @global object $wpdb |
|---|
| 1022 | * @global array $bp |
|---|
| 1023 | * @return bool |
|---|
| 1024 | */ |
|---|
| 1025 | function is_valid_field() { |
|---|
| 1026 | global $wpdb, $bp; |
|---|
| 1027 | |
|---|
| 1028 | $retval = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id = %d", $this->field_id ) ); |
|---|
| 1029 | |
|---|
| 1030 | return apply_filters_ref_array( 'xprofile_data_is_valid_field', array( (bool)$retval, $this ) ); |
|---|
| 1031 | } |
|---|
| 1032 | |
|---|
| 1033 | function save() { |
|---|
| 1034 | global $wpdb, $bp; |
|---|
| 1035 | |
|---|
| 1036 | $this->user_id = apply_filters( 'xprofile_data_user_id_before_save', $this->user_id, $this->id ); |
|---|
| 1037 | $this->field_id = apply_filters( 'xprofile_data_field_id_before_save', $this->field_id, $this->id ); |
|---|
| 1038 | $this->value = apply_filters( 'xprofile_data_value_before_save', $this->value, $this->id ); |
|---|
| 1039 | $this->last_updated = apply_filters( 'xprofile_data_last_updated_before_save', bp_core_current_time(), $this->id ); |
|---|
| 1040 | |
|---|
| 1041 | do_action_ref_array( 'xprofile_data_before_save', array( $this ) ); |
|---|
| 1042 | |
|---|
| 1043 | if ( $this->is_valid_field() ) { |
|---|
| 1044 | if ( $this->exists() && !empty( $this->value ) && strlen( trim( $this->value ) ) ) { |
|---|
| 1045 | $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_data} SET value = %s, last_updated = %s WHERE user_id = %d AND field_id = %d", $this->value, $this->last_updated, $this->user_id, $this->field_id ) ); |
|---|
| 1046 | |
|---|
| 1047 | } else if ( $this->exists() && empty( $this->value ) ) { |
|---|
| 1048 | // Data removed, delete the entry. |
|---|
| 1049 | $result = $this->delete(); |
|---|
| 1050 | |
|---|
| 1051 | } else { |
|---|
| 1052 | $result = $wpdb->query( $wpdb->prepare("INSERT INTO {$bp->profile->table_name_data} (user_id, field_id, value, last_updated) VALUES (%d, %d, %s, %s)", $this->user_id, $this->field_id, $this->value, $this->last_updated ) ); |
|---|
| 1053 | $this->id = $wpdb->insert_id; |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | if ( false === $result ) |
|---|
| 1057 | return false; |
|---|
| 1058 | |
|---|
| 1059 | do_action_ref_array( 'xprofile_data_after_save', array( $this ) ); |
|---|
| 1060 | |
|---|
| 1061 | return true; |
|---|
| 1062 | } |
|---|
| 1063 | |
|---|
| 1064 | return false; |
|---|
| 1065 | } |
|---|
| 1066 | |
|---|
| 1067 | function delete() { |
|---|
| 1068 | global $wpdb, $bp; |
|---|
| 1069 | |
|---|
| 1070 | if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $this->field_id, $this->user_id ) ) ) |
|---|
| 1071 | return false; |
|---|
| 1072 | |
|---|
| 1073 | do_action_ref_array( 'xprofile_data_after_delete', array( $this ) ); |
|---|
| 1074 | return true; |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| 1077 | /** Static Functions **/ |
|---|
| 1078 | |
|---|
| 1079 | /** |
|---|
| 1080 | * BP_XProfile_ProfileData::get_all_for_user() |
|---|
| 1081 | * |
|---|
| 1082 | * Get all of the profile information for a specific user. |
|---|
| 1083 | */ |
|---|
| 1084 | function get_all_for_user( $user_id ) { |
|---|
| 1085 | global $wpdb, $bp; |
|---|
| 1086 | |
|---|
| 1087 | $results = $wpdb->get_results( $wpdb->prepare( "SELECT g.id as field_group_id, g.name as field_group_name, f.id as field_id, f.name as field_name, f.type as field_type, d.value as field_data, u.user_login, u.user_nicename, u.user_email FROM {$bp->profile->table_name_groups} g LEFT JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id INNER JOIN {$bp->profile->table_name_data} d ON f.id = d.field_id LEFT JOIN {$wpdb->users} u ON d.user_id = u.ID WHERE d.user_id = %d AND d.value != ''", $user_id ) ); |
|---|
| 1088 | $profile_data = array(); |
|---|
| 1089 | |
|---|
| 1090 | if ( !empty( $results ) ) { |
|---|
| 1091 | $profile_data['user_login'] = $results[0]->user_login; |
|---|
| 1092 | $profile_data['user_nicename'] = $results[0]->user_nicename; |
|---|
| 1093 | $profile_data['user_email'] = $results[0]->user_email; |
|---|
| 1094 | |
|---|
| 1095 | foreach( (array) $results as $field ) { |
|---|
| 1096 | $profile_data[$field->field_name] = array( |
|---|
| 1097 | 'field_group_id' => $field->field_group_id, |
|---|
| 1098 | 'field_group_name' => $field->field_group_name, |
|---|
| 1099 | 'field_id' => $field->field_id, |
|---|
| 1100 | 'field_type' => $field->field_type, |
|---|
| 1101 | 'field_data' => $field->field_data |
|---|
| 1102 | ); |
|---|
| 1103 | } |
|---|
| 1104 | } |
|---|
| 1105 | |
|---|
| 1106 | return $profile_data; |
|---|
| 1107 | } |
|---|
| 1108 | |
|---|
| 1109 | /** |
|---|
| 1110 | * Get the user's field data id by the id of the xprofile field |
|---|
| 1111 | * |
|---|
| 1112 | * @param int $field_id |
|---|
| 1113 | * @param int $user_id |
|---|
| 1114 | * @return int $fielddata_id |
|---|
| 1115 | */ |
|---|
| 1116 | function get_fielddataid_byid( $field_id, $user_id ) { |
|---|
| 1117 | global $wpdb, $bp; |
|---|
| 1118 | |
|---|
| 1119 | if ( empty( $field_id ) || empty( $user_id ) ) { |
|---|
| 1120 | $fielddata_id = 0; |
|---|
| 1121 | } else { |
|---|
| 1122 | $fielddata_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_id ) ); |
|---|
| 1123 | } |
|---|
| 1124 | |
|---|
| 1125 | return $fielddata_id; |
|---|
| 1126 | } |
|---|
| 1127 | |
|---|
| 1128 | function get_value_byid( $field_id, $user_ids = null ) { |
|---|
| 1129 | global $wpdb, $bp; |
|---|
| 1130 | |
|---|
| 1131 | if ( empty( $user_ids ) ) |
|---|
| 1132 | $user_ids = bp_displayed_user_id(); |
|---|
| 1133 | |
|---|
| 1134 | if ( is_array( $user_ids ) ) { |
|---|
| 1135 | $user_ids = implode( ',', (array) $user_ids ); |
|---|
| 1136 | $data = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, value FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id IN ({$user_ids})", $field_id ) ); |
|---|
| 1137 | } else { |
|---|
| 1138 | $data = $wpdb->get_var( $wpdb->prepare( "SELECT value FROM {$bp->profile->table_name_data} WHERE field_id = %d AND user_id = %d", $field_id, $user_ids ) ); |
|---|
| 1139 | } |
|---|
| 1140 | |
|---|
| 1141 | return $data; |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | function get_value_byfieldname( $fields, $user_id = null ) { |
|---|
| 1145 | global $bp, $wpdb; |
|---|
| 1146 | |
|---|
| 1147 | if ( empty( $fields ) ) |
|---|
| 1148 | return false; |
|---|
| 1149 | |
|---|
| 1150 | if ( empty( $user_id ) ) |
|---|
| 1151 | $user_id = bp_displayed_user_id(); |
|---|
| 1152 | |
|---|
| 1153 | $field_sql = ''; |
|---|
| 1154 | |
|---|
| 1155 | if ( is_array( $fields ) ) { |
|---|
| 1156 | for ( $i = 0, $count = count( $fields ); $i < $count; ++$i ) { |
|---|
| 1157 | if ( $i == 0 ) { |
|---|
| 1158 | $field_sql .= $wpdb->prepare( "AND ( f.name = %s ", $fields[$i] ); |
|---|
| 1159 | } else { |
|---|
| 1160 | $field_sql .= $wpdb->prepare( "OR f.name = %s ", $fields[$i] ); |
|---|
| 1161 | } |
|---|
| 1162 | } |
|---|
| 1163 | |
|---|
| 1164 | $field_sql .= ')'; |
|---|
| 1165 | } else { |
|---|
| 1166 | $field_sql .= $wpdb->prepare( "AND f.name = %s", $fields ); |
|---|
| 1167 | } |
|---|
| 1168 | |
|---|
| 1169 | $sql = $wpdb->prepare( "SELECT d.value, f.name FROM {$bp->profile->table_name_data} d, {$bp->profile->table_name_fields} f WHERE d.field_id = f.id AND d.user_id = %d AND f.parent_id = 0 $field_sql", $user_id ); |
|---|
| 1170 | |
|---|
| 1171 | if ( !$values = $wpdb->get_results( $sql ) ) |
|---|
| 1172 | return false; |
|---|
| 1173 | |
|---|
| 1174 | $new_values = array(); |
|---|
| 1175 | |
|---|
| 1176 | if ( is_array( $fields ) ) { |
|---|
| 1177 | for ( $i = 0, $count = count( $values ); $i < $count; ++$i ) { |
|---|
| 1178 | for ( $j = 0; $j < count( $fields ); $j++ ) { |
|---|
| 1179 | if ( $values[$i]->name == $fields[$j] ) { |
|---|
| 1180 | $new_values[$fields[$j]] = $values[$i]->value; |
|---|
| 1181 | } else if ( !array_key_exists( $fields[$j], $new_values ) ) { |
|---|
| 1182 | $new_values[$fields[$j]] = NULL; |
|---|
| 1183 | } |
|---|
| 1184 | } |
|---|
| 1185 | } |
|---|
| 1186 | } else { |
|---|
| 1187 | $new_values = $values[0]->value; |
|---|
| 1188 | } |
|---|
| 1189 | |
|---|
| 1190 | return $new_values; |
|---|
| 1191 | } |
|---|
| 1192 | |
|---|
| 1193 | function delete_for_field( $field_id ) { |
|---|
| 1194 | global $wpdb, $bp; |
|---|
| 1195 | |
|---|
| 1196 | if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE field_id = %d", $field_id ) ) ) |
|---|
| 1197 | return false; |
|---|
| 1198 | |
|---|
| 1199 | return true; |
|---|
| 1200 | } |
|---|
| 1201 | |
|---|
| 1202 | function get_last_updated( $user_id ) { |
|---|
| 1203 | global $wpdb, $bp; |
|---|
| 1204 | |
|---|
| 1205 | $last_updated = $wpdb->get_var( $wpdb->prepare( "SELECT last_updated FROM {$bp->profile->table_name_data} WHERE user_id = %d ORDER BY last_updated LIMIT 1", $user_id ) ); |
|---|
| 1206 | |
|---|
| 1207 | return $last_updated; |
|---|
| 1208 | } |
|---|
| 1209 | |
|---|
| 1210 | function delete_data_for_user( $user_id ) { |
|---|
| 1211 | global $wpdb, $bp; |
|---|
| 1212 | |
|---|
| 1213 | return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_data} WHERE user_id = %d", $user_id ) ); |
|---|
| 1214 | } |
|---|
| 1215 | |
|---|
| 1216 | function get_random( $user_id, $exclude_fullname ) { |
|---|
| 1217 | global $wpdb, $bp; |
|---|
| 1218 | |
|---|
| 1219 | if ( !empty( $exclude_fullname ) ) |
|---|
| 1220 | $exclude_sql = $wpdb->prepare( " AND pf.id != 1" ); |
|---|
| 1221 | |
|---|
| 1222 | return $wpdb->get_results( $wpdb->prepare( "SELECT pf.type, pf.name, pd.value FROM {$bp->profile->table_name_data} pd INNER JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id AND pd.user_id = %d {$exclude_sql} ORDER BY RAND() LIMIT 1", $user_id ) ); |
|---|
| 1223 | } |
|---|
| 1224 | |
|---|
| 1225 | function get_fullname( $user_id = 0 ) { |
|---|
| 1226 | |
|---|
| 1227 | if ( empty( $user_id ) ) |
|---|
| 1228 | $user_id = bp_displayed_user_id(); |
|---|
| 1229 | |
|---|
| 1230 | $field_name = bp_xprofile_fullname_field_name(); |
|---|
| 1231 | $data = xprofile_get_field_data( $field_name, $user_id ); |
|---|
| 1232 | |
|---|
| 1233 | return $data[$field_name]; |
|---|
| 1234 | } |
|---|
| 1235 | } |
|---|