Skip to:
Content

BuddyPress.org

Changeset 3346


Ignore:
Timestamp:
11/03/2010 11:14:43 PM (14 years ago)
Author:
boonebgorges
Message:

Adds meta functions for xprofile field groups, fields, and individual data items. Fixes #671

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/admin/bp-core-schema.php

    r2882 r3346  
    228228                KEY is_required (is_required)
    229229               ) {$charset_collate};";
    230 
     230       
    231231    $sql[] = "CREATE TABLE {$wpdb->base_prefix}bp_xprofile_data (
    232232                id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
     
    239239               ) {$charset_collate};";
    240240
     241    $sql[] = "CREATE TABLE {$wpdb->base_prefix}bp_xprofile_meta (
     242                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     243                object_id bigint(20) NOT NULL,
     244                object_type varchar(150) NOT NULL,
     245                meta_key varchar(255) DEFAULT NULL,
     246                meta_value longtext DEFAULT NULL,
     247                KEY object_id (object_id),
     248                KEY meta_key (meta_key)
     249               ) {$charset_collate};";
     250
    241251    dbDelta( $sql );
    242252
  • trunk/bp-xprofile.php

    r3331 r3346  
    3434    $bp->profile->table_name_groups = $bp->table_prefix . 'bp_xprofile_groups';
    3535    $bp->profile->table_name_fields = $bp->table_prefix . 'bp_xprofile_fields';
     36    $bp->profile->table_name_meta   = $bp->table_prefix . 'bp_xprofile_meta';
    3637
    3738    $bp->profile->format_notification_function = 'xprofile_format_notifications';
     
    909910add_action( 'make_spam_user', 'xprofile_remove_data' );
    910911
     912/*** XProfile Meta ****************************************************/
     913
     914function bp_xprofile_delete_meta( $object_id, $object_type, $meta_key = false, $meta_value = false ) {
     915    global $wpdb, $bp;
     916
     917    $object_id = (int) $object_id;
     918
     919    if ( !$object_id )
     920        return false;
     921   
     922    if ( !isset( $object_type ) )
     923        return false;
     924   
     925    if ( !in_array( $object_type, array( 'group', 'field', 'data' ) ) )
     926        return false;
     927
     928    $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
     929
     930    if ( is_array($meta_value) || is_object($meta_value) )
     931        $meta_value = serialize($meta_value);
     932
     933    $meta_value = trim( $meta_value );
     934
     935    if ( !$meta_key ) {
     936        $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s", $object_id, $object_type ) );
     937    } else if ( $meta_value ) {
     938        $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s AND meta_key = %s AND meta_value = %s", $object_id, $object_type, $meta_key, $meta_value ) );
     939    } else {
     940        $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s AND meta_key = %s", $object_id, $object_type, $meta_key ) );
     941    }
     942
     943    /* Delete the cached object */
     944    wp_cache_delete( 'bp_xprofile_meta_' . $object_type . '_' . $object_id . '_' . $meta_key, 'bp' );
     945
     946    return true;
     947}
     948
     949function bp_xprofile_get_meta( $object_id, $object_type, $meta_key = '') {
     950    global $wpdb, $bp;
     951
     952    $object_id = (int) $object_id;
     953
     954    if ( !$object_id )
     955        return false;
     956   
     957    if ( !isset( $object_type ) )
     958        return false;
     959   
     960    if ( !in_array( $object_type, array( 'group', 'field', 'data' ) ) )
     961        return false;
     962
     963    if ( !empty($meta_key) ) {
     964        $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
     965
     966        if ( !$metas = wp_cache_get( 'bp_xprofile_meta_' . $object_type . '_' . $object_id . '_' . $meta_key, 'bp' ) ) {
     967            $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s AND meta_key = %s", $object_id, $object_type, $meta_key) );
     968            wp_cache_set( 'bp_xprofile_meta_' . $object_type . '_' . $object_id . '_' . $meta_key, $metas, 'bp' );
     969        }
     970    } else {
     971        $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s", $object_id, $object_type ) );
     972    }
     973
     974    if ( empty($metas) ) {
     975        if ( empty($meta_key) )
     976            return array();
     977        else
     978            return '';
     979    }
     980
     981    $metas = array_map('maybe_unserialize', (array)$metas);
     982
     983    if ( 1 == count($metas) )
     984        return $metas[0];
     985    else
     986        return $metas;
     987}
     988
     989function bp_xprofile_update_meta( $object_id, $object_type, $meta_key, $meta_value ) {
     990    global $wpdb, $bp;
     991
     992    $object_id = (int) $object_id;
     993
     994    if ( !$object_id )
     995        return false;
     996   
     997    if ( !isset( $object_type ) )
     998        return false;
     999   
     1000    if ( !in_array( $object_type, array( 'group', 'field', 'data' ) ) )
     1001        return false;
     1002       
     1003    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     1004
     1005    if ( is_string($meta_value) )
     1006        $meta_value = stripslashes($wpdb->escape($meta_value));
     1007
     1008    $meta_value = maybe_serialize($meta_value);
     1009
     1010    if (empty($meta_value)) {
     1011        return bp_xprofile_delete_meta( $object_id, $object_type, $meta_key );
     1012    }
     1013   
     1014    $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp->profile->table_name_meta . " WHERE object_id = %d AND object_type = %s AND meta_key = %s", $object_id, $object_type, $meta_key ) );
     1015
     1016    if ( !$cur ) {
     1017        $wpdb->query( $wpdb->prepare( "INSERT INTO " . $bp->profile->table_name_meta . " ( object_id, object_type, meta_key, meta_value ) VALUES ( %d, %s, %s, %s )", $object_id, $object_type,  $meta_key, $meta_value ) );
     1018    } else if ( $cur->meta_value != $meta_value ) {
     1019        $wpdb->query( $wpdb->prepare( "UPDATE " . $bp->profile->table_name_meta . " SET meta_value = %s WHERE object_id = %d AND object_type = %s AND meta_key = %s", $meta_value, $object_id, $object_type, $meta_key ) );
     1020    } else {
     1021        return false;
     1022    }
     1023
     1024    /* Update the cached object and recache */
     1025    wp_cache_set( 'bp_xprofile_meta_' . $object_type . '_' . $object_id . '_' . $meta_key, $meta_value, 'bp' );
     1026
     1027    return true;
     1028}
     1029
     1030function bp_xprofile_update_fieldgroup_meta( $field_group_id, $meta_key, $meta_value ) {
     1031    return bp_xprofile_update_meta( $field_group_id, 'group', $meta_key, $meta_value );
     1032}
     1033
     1034function bp_xprofile_update_field_meta( $field_id, $meta_key, $meta_value ) {
     1035    return bp_xprofile_update_meta( $field_id, 'field', $meta_key, $meta_value );   
     1036}
     1037
     1038function bp_xprofile_update_fielddata_meta( $field_data_id, $meta_key, $meta_value ) {
     1039    return bp_xprofile_update_meta( $field_data_id, 'data', $meta_key, $meta_value );   
     1040}
    9111041
    9121042/********************************************************************************
Note: See TracChangeset for help on using the changeset viewer.