Skip to:
Content

BuddyPress.org

Changeset 7840


Ignore:
Timestamp:
02/11/2014 02:58:50 AM (6 years ago)
Author:
boonebgorges
Message:

Migrate groupmeta functions to the WP metadata API

See #4551

Props boonebgorges, johnjamesjacoby

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-filters.php

    r7741 r7840  
    530530}
    531531add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
     532
     533/**
     534 * Filter SQL query strings to swap out the 'meta_id' column.
     535 *
     536 * WordPress uses the meta_id column for commentmeta and postmeta, and so
     537 * hardcodes the column name into its *_metadata() functions. BuddyPress, on
     538 * the other hand, uses 'id' for the primary column. To make WP's functions
     539 * usable for BuddyPress, we use this just-in-time filter on 'query' to swap
     540 * 'meta_id' with 'id.
     541 *
     542 * @since BuddyPress (2.0.0)
     543 *
     544 * @access private Do not use.
     545 *
     546 * @param string $q SQL query.
     547 * @return string
     548 */
     549function bp_filter_metaid_column_name( $q ) {
     550    return str_replace( 'meta_id', 'id', $q );
     551}
  • trunk/bp-groups/bp-groups-functions.php

    r7624 r7840  
    957957/*** Group Meta ****************************************************/
    958958
    959 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
    960     global $wpdb, $bp;
    961 
    962     if ( !is_numeric( $group_id ) )
    963         return false;
    964 
     959/**
     960 * Delete metadata for a group.
     961 *
     962 * @param int $group_id ID of the group.
     963 * @param string $meta_key The key of the row to delete.
     964 * @param string $meta_value Optional. Metadata value. If specified, only delete
     965 *        metadata entries with this value.
     966 * @param bool $delete_all Optional. If true, delete matching metadata entries
     967 *        for all groups. Default: false.
     968 * @return bool True on success, false on failure.
     969 */
     970function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
     971
     972    // Legacy - return false if non-int group ID
     973    if ( ! is_numeric( $group_id ) ) {
     974        return false;
     975    }
     976
     977    // Legacy - Sanitize keys
    965978    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    966979
    967     if ( is_array( $meta_value ) || is_object( $meta_value ) )
    968         $meta_value = serialize($meta_value);
    969 
    970     $meta_value = trim( $meta_value );
    971 
    972     if ( !$meta_key )
    973         $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
    974     else if ( $meta_value )
    975         $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s AND meta_value = %s", $group_id, $meta_key, $meta_value ) );
    976     else
    977         $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    978 
    979     // Delete the cached object
    980     wp_cache_delete( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
    981 
    982     return true;
    983 }
    984 
    985 function groups_get_groupmeta( $group_id, $meta_key = '') {
    986     global $wpdb, $bp;
    987 
    988     $group_id = (int) $group_id;
    989 
    990     if ( !$group_id )
    991         return false;
    992 
    993     if ( !empty($meta_key) ) {
    994         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    995 
    996         $metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
    997         if ( false === $metas ) {
    998             $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    999             wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $metas, 'bp' );
     980    add_filter( 'query', 'bp_filter_metaid_column_name' );
     981    $retval = delete_metadata( 'group', $group_id, $meta_key, $meta_value, $delete_all );
     982    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     983
     984    return $retval;
     985}
     986
     987/**
     988 * Get a piece of group metadata.
     989 *
     990 * @param int $group_id ID of the group.
     991 * @param string $meta_key Metadata key.
     992 * @param bool $single Optional. If true, return only the first value of the
     993 *        specified meta_key. This parameter has no effect if meta_key is
     994 *        empty.
     995 * @return mixed Metadata value.
     996 */
     997function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) {
     998
     999    // Legacy - Sanitize keys
     1000    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     1001
     1002    add_filter( 'query', 'bp_filter_metaid_column_name' );
     1003    $retval = get_metadata( 'group', $group_id, $meta_key, $single );
     1004    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     1005
     1006    // Legacy - If fetching all meta for a group, just return values
     1007    if ( empty( $meta_key ) ) {
     1008        $values = array();
     1009        foreach ( (array) $retval as $r ) {
     1010            $values[] = array_pop( $r );
    10001011        }
    1001     } else {
    1002         $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
    1003     }
    1004 
    1005     if ( empty( $metas ) ) {
    1006         if ( empty( $meta_key ) )
    1007             return array();
    1008         else
    1009             return '';
    1010     }
    1011 
    1012     $metas = array_map( 'maybe_unserialize', (array) $metas );
    1013 
    1014     if ( 1 == count( $metas ) )
    1015         return $metas[0];
    1016     else
    1017         return $metas;
    1018 }
    1019 
     1012        $retval = $values;
     1013    }
     1014
     1015    return $retval;
     1016}
     1017
     1018/**
     1019 * Update a piece of group metadata.
     1020 *
     1021 * @param int $group_id ID of the group.
     1022 * @param string $meta_key Metadata key.
     1023 * @param mixed $meta_value Value to store.
     1024 * @return bool True on success, false on failure.
     1025 */
    10201026function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
    1021     global $wpdb, $bp;
    1022 
    1023     if ( !is_numeric( $group_id ) )
    1024         return false;
    1025 
    1026     $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    1027 
    1028     if ( is_string( $meta_value ) ) {
    1029         $meta_value = stripslashes( $meta_value );
    1030     }
    1031 
    1032     $meta_value = maybe_serialize( $meta_value );
    1033 
    1034     $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
    1035 
    1036     if ( !$cur )
    1037         $wpdb->query( $wpdb->prepare( "INSERT INTO " . $bp->groups->table_name_groupmeta . " ( group_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $group_id, $meta_key, $meta_value ) );
    1038     else if ( $cur->meta_value != $meta_value )
    1039         $wpdb->query( $wpdb->prepare( "UPDATE " . $bp->groups->table_name_groupmeta . " SET meta_value = %s WHERE group_id = %d AND meta_key = %s", $meta_value, $group_id, $meta_key ) );
    1040     else
    1041         return false;
    1042 
    1043     // Update the cached object and recache
    1044     wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $meta_value, 'bp' );
    1045 
    1046     return true;
     1027
     1028    add_filter( 'query', 'bp_filter_metaid_column_name' );
     1029    $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value );
     1030    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     1031
     1032    // Legacy - return true if we fall through to add_metadata()
     1033    if ( is_int( $retval ) ) {
     1034        $retval = true;
     1035    }
     1036
     1037    return $retval;
    10471038}
    10481039
  • trunk/bp-groups/bp-groups-loader.php

    r7758 r7840  
    137137        );
    138138
     139        // Metadata tables for groups component
     140        $meta_tables = array(
     141            'group' => $bp->table_prefix . 'bp_groups_groupmeta',
     142        );
     143
    139144        // All globals for groups component.
    140145        // Note that global_tables is included in this array.
     
    145150            'notification_callback' => 'groups_format_notifications',
    146151            'search_string'         => __( 'Search Groups...', 'buddypress' ),
    147             'global_tables'         => $global_tables
     152            'global_tables'         => $global_tables,
     153            'meta_tables'           => $meta_tables,
    148154        );
    149155
Note: See TracChangeset for help on using the changeset viewer.