Skip to:
Content

BuddyPress.org

Ticket #4551: 4551.patch

File 4551.patch, 6.3 KB (added by boonebgorges, 6 years ago)
  • bp-core/bp-core-functions.php

    diff --git bp-core/bp-core-functions.php bp-core/bp-core-functions.php
    index 617abae..0f03f7a 100644
    function bp_esc_sql_order( $order = '' ) { 
    265265}
    266266
    267267/**
     268 * Filter SQL query strings to swap out the 'meta_id' column.
     269 *
     270 * WordPress uses the meta_id column for commentmeta and postmeta, and so
     271 * hardcodes the column name into its *_metadata() functions. BuddyPress, on
     272 * the other hand, uses 'id' for the primary column. To make WP's functions
     273 * usable for BuddyPress, we use this just-in-time filter on 'query' to swap
     274 * 'meta_id' with 'id.
     275 *
     276 * @since BuddyPress (2.0.0)
     277 *
     278 * @access private Do not use.
     279 *
     280 * @param string $q SQL query.
     281 * @return string
     282 */
     283function bp_filter_metaid_column_name( $q ) {
     284        return str_replace( 'meta_id', 'id', $q );
     285}
     286
     287/**
    268288 * Are we running username compatibility mode?
    269289 *
    270290 * @since BuddyPress (1.5.0)
  • bp-groups/bp-groups-functions.php

    diff --git bp-groups/bp-groups-functions.php bp-groups/bp-groups-functions.php
    index 072bdd8..8fe7dfe 100644
    function groups_accept_all_pending_membership_requests( $group_id ) { 
    957957/*** Group Meta ****************************************************/
    958958
    959959function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
    960         global $wpdb, $bp;
    961 
    962         if ( !is_numeric( $group_id ) )
     960        // Legacy - return false if non-int group ID
     961        if ( ! is_numeric( $group_id ) ) {
    963962                return false;
     963        }
    964964
     965        // Legacy - Sanitize keys
    965966        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    966967
    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' );
     968        add_filter( 'query', 'bp_filter_metaid_column_name' );
     969        $retval = delete_metadata( 'group', $group_id, $meta_key, $meta_value, $delete_all );
     970        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    981971
    982972        return true;
    983973}
    984974
    985975function 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;
     976        // Legacy - Sanitize keys
     977        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    992978
    993         if ( !empty($meta_key) ) {
    994                 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     979        add_filter( 'query', 'bp_filter_metaid_column_name' );
     980        $retval = get_metadata( 'group', $group_id, $meta_key, true );
     981        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    995982
    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' );
     983        // Legacy - If fetching all meta for a group, just return values
     984        if ( empty( $meta_key ) ) {
     985                $values = array();
     986                foreach ( (array) $retval as $r ) {
     987                        $values[] = array_pop( $r );
    1000988                }
    1001         } else {
    1002                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
     989                $retval = $values;
    1003990        }
    1004991
    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;
     992        return $retval;
    1018993}
    1019994
    1020995function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
    1021         global $wpdb, $bp;
    1022 
    1023         if ( !is_numeric( $group_id ) )
    1024                 return false;
     996        add_filter( 'query', 'bp_filter_metaid_column_name' );
     997        $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value );
     998        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    1025999
    1026         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    1027 
    1028         if ( is_string( $meta_value ) ) {
    1029                 $meta_value = stripslashes( $meta_value );
     1000        // Legacy - return true if we fall through to add_metadata()
     1001        if ( is_int( $retval ) ) {
     1002                $retval = true;
    10301003        }
    10311004
    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;
     1005        return $retval;
    10471006}
    10481007
    10491008/*** Group Cleanup Functions ****************************************************/
  • bp-groups/bp-groups-loader.php

    diff --git bp-groups/bp-groups-loader.php bp-groups/bp-groups-loader.php
    index 6862000..baa014a 100644
    class BP_Groups_Component extends BP_Component { 
    123123         * @since BuddyPress (1.5)
    124124         */
    125125        public function setup_globals( $args = array() ) {
     126                global $wpdb;
     127
    126128                $bp = buddypress();
    127129
    128130                // Define a slug, if necessary
    class BP_Groups_Component extends BP_Component { 
    149151
    150152                parent::setup_globals( $args );
    151153
     154                $wpdb->groupmeta = $this->table_name_groupmeta;
     155
    152156                /** Single Group Globals **********************************************/
    153157
    154158                // Are we viewing a single group?