Skip to:
Content

BuddyPress.org

Ticket #4551: 4551.3.patch

File 4551.3.patch, 19.0 KB (added by johnjamesjacoby, 11 years ago)

Same as 2, but also converts Activity component

  • bp-activity/bp-activity-functions.php

     
    540540 *
    541541 * @since BuddyPress (1.2)
    542542 *
    543  * @global object $wpdb WordPress database access object.
    544  * @global object $bp BuddyPress global settings.
    545  * @uses wp_cache_delete()
    546  * @uses is_wp_error()
    547  *
    548543 * @param int $activity_id ID of the activity item whose metadata is being deleted.
    549544 * @param string $meta_key Optional. The key of the metadata being deleted. If
    550545 *                         omitted, all metadata associated with the activity
     
    553548 *                           deleted if the meta_value matches this parameter.
    554549 * @return bool True on success, false on failure.
    555550 */
    556 function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) {
    557         global $wpdb, $bp;
     551function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '', $delete_all = false ) {
    558552
    559553        // Return false if any of the above values are not set
    560         if ( !is_numeric( $activity_id ) )
     554        if ( !is_numeric( $activity_id ) ) {
    561555                return false;
     556        }
    562557
    563         // Sanitize key
     558        // Legacy - Sanitize keys
    564559        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    565560
    566         if ( is_array( $meta_value ) || is_object( $meta_value ) )
    567                 $meta_value = serialize( $meta_value );
     561        add_filter( 'query', 'bp_filter_metaid_column_name' );
     562        $retval = delete_metadata( 'activity', $activity_id, $meta_key, $meta_value, $delete_all );
     563        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    568564
    569         // Trim off whitespace
    570         $meta_value = trim( $meta_value );
    571 
    572         // Delete all for activity_id
    573         if ( empty( $meta_key ) )
    574                 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
    575 
    576         // Delete only when all match
    577         else if ( $meta_value )
    578                 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s AND meta_value = %s", $activity_id, $meta_key, $meta_value ) );
    579 
    580         // Delete only when activity_id and meta_key match
    581         else
    582                 $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
    583 
    584         // Delete cache entry
    585         wp_cache_delete( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, 'bp' );
    586 
    587         // Success
    588         if ( !is_wp_error( $retval ) )
    589                 return true;
    590 
    591         // Fail
    592         else
    593                 return false;
     565        return $retval;
    594566}
    595567
    596568/**
     
    598570 *
    599571 * @since BuddyPress (1.2)
    600572 *
    601  * @global object $wpdb WordPress database access object.
    602  * @global object $bp BuddyPress global settings.
    603  * @uses wp_cache_get()
    604  * @uses wp_cache_set()
    605573 * @uses apply_filters() To call the 'bp_activity_get_meta' hook.
    606574 *
    607575 * @param int $activity_id ID of the activity item whose metadata is being requseted.
     
    610578 *                         metadata for the activity item will be fetched.
    611579 * @return mixed The meta value(s) being requested.
    612580 */
    613 function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) {
    614         global $wpdb, $bp;
     581function bp_activity_get_meta( $activity_id = 0, $meta_key = '', $single = true ) {
    615582
    616583        // Make sure activity_id is valid
    617         if ( empty( $activity_id ) || !is_numeric( $activity_id ) )
     584        if ( empty( $activity_id ) || !is_numeric( $activity_id ) ) {
    618585                return false;
     586        }
    619587
    620         // We have a key to look for
    621         if ( !empty( $meta_key ) ) {
     588        // Legacy - Sanitize keys
     589        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    622590
    623                 // Sanitize key
    624                 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     591        add_filter( 'query', 'bp_filter_metaid_column_name' );
     592        $retval = get_metadata( 'activity', $activity_id, $meta_key, $single );
     593        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    625594
    626                 // Check cache
    627                 if ( !$metas = wp_cache_get( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, 'bp' ) ) {
    628                         // No cache so hit the DB
    629                         $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
    630 
    631                         // Set cache
    632                         wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, $metas, 'bp' );
     595        // Legacy - If fetching all meta for a group, just return values
     596        if ( empty( $meta_key ) ) {
     597                $values = array();
     598                foreach ( (array) $retval as $r ) {
     599                        $values[] = array_pop( $r );
    633600                }
    634 
    635         // No key so get all for activity_id
    636         } else {
    637                 $metas = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
    638 
    639                 if ( !empty( $metas ) ) {
    640                         $metas = array_map( 'maybe_unserialize', (array) $metas );
    641 
    642                         foreach( $metas as $mkey => $mvalue ) {
    643                                 wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $mkey, $mvalue, 'bp' );
    644                         }
    645                 }
     601                $retval = $values;
    646602        }
    647603
    648         // No result so return false
    649         if ( empty( $metas ) )
    650                 return false;
    651 
    652         // Maybe, just maybe... unserialize
    653         $metas = array_map( 'maybe_unserialize', (array) $metas );
    654 
    655         // Return first item in array if only 1, else return all metas found
    656         $retval = ( 1 == count( $metas ) ? $metas[0] : $metas );
    657 
    658604        // Filter result before returning
    659         return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key );
     605        return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key, $single );
    660606}
    661607
    662608/**
     
    664610 *
    665611 * @since BuddyPress (1.2)
    666612 *
    667  * @global object $wpdb WordPress database access object.
    668  * @global object $bp BuddyPress global settings.
    669  * @uses maybe_serialize()
    670  * @uses bp_activity_delete_meta()
    671  * @uses wp_cache_set()
    672  *
    673613 * @param int $activity_id ID of the activity item whose metadata is being updated.
    674614 * @param string $meta_key Key of the metadata being updated.
    675615 * @param mixed $meta_value Value to be set.
    676616 * @return bool True on success, false on failure.
    677617 */
    678618function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) {
    679         global $wpdb, $bp;
    680619
    681620        // Make sure activity_id is valid
    682         if ( !is_numeric( $activity_id ) )
     621        if ( !is_numeric( $activity_id ) ) {
    683622                return false;
     623        }
    684624
    685625        // Sanitize key
    686626        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    687627
    688         // Sanitize value
    689         if ( is_string( $meta_value ) ) {
    690                 $meta_value = stripslashes( $meta_value );
     628        add_filter( 'query', 'bp_filter_metaid_column_name' );
     629        $retval = update_metadata( 'activity', $activity_id, $meta_key, $meta_value );
     630        remove_filter( 'query', 'bp_filter_metaid_column_name' );
     631
     632        // Legacy - return true if we fall through to add_metadata()
     633        if ( is_int( $retval ) ) {
     634                $retval = true;
    691635        }
    692636
    693         // Maybe, just maybe... serialize
    694         $meta_value = maybe_serialize( $meta_value );
    695 
    696         // If value is false, delete the meta key
    697         if ( false === $meta_value )
    698                 return bp_activity_delete_meta( $activity_id, $meta_key );
    699 
    700         // See if meta key exists for activity_id
    701         $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
    702 
    703         // Meta key does not exist so INSERT
    704         if ( empty( $cur ) )
    705                 $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ) );
    706 
    707         // Meta key exists, so UPDATE
    708         else if ( $cur->meta_value != $meta_value )
    709                 $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name_meta} SET meta_value = %s WHERE activity_id = %d AND meta_key = %s", $meta_value, $activity_id, $meta_key ) );
    710 
    711         // Weirdness, so return false
    712         else
    713                 return false;
    714 
    715         // Set cache
    716         wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, $meta_value, 'bp' );
    717 
    718         // Victory is ours!
    719         return true;
     637        return $retval;
    720638}
    721639
    722640/** Clean up *****************************************************************/
  • bp-activity/bp-activity-loader.php

     
    9595                        'table_name_meta' => $bp->table_prefix . 'bp_activity_meta',
    9696                );
    9797
     98                // Metadata tables for groups component
     99                $meta_tables = array(
     100                        'activity' => $bp->table_prefix . 'bp_activity_meta'
     101                );
     102
    98103                // All globals for activity component.
    99104                // Note that global_tables is included in this array.
    100105                $args = array(
    101106                        'slug'                  => BP_ACTIVITY_SLUG,
    102107                        'root_slug'             => isset( $bp->pages->activity->slug ) ? $bp->pages->activity->slug : BP_ACTIVITY_SLUG,
    103108                        'has_directory'         => true,
     109                        'notification_callback' => 'bp_activity_format_notifications',
    104110                        'search_string'         => __( 'Search Activity...', 'buddypress' ),
    105111                        'global_tables'         => $global_tables,
    106                         'notification_callback' => 'bp_activity_format_notifications',
     112                        'meta_tables'           => $meta_tables
    107113                );
    108114
    109115                parent::setup_globals( $args );
  • bp-core/bp-core-component.php

     
    107107         */
    108108        public $root_slug = '';
    109109
     110        /**
     111         * Metadata tables for the component (if applicable)
     112         *
     113         * @since BuddyPress (2.0.0)
     114         *
     115         * @var string
     116         */
     117        public $meta_tables = array();
     118
     119        /**
     120         * Global tables for the component (if applicable)
     121         *
     122         * @since BuddyPress (2.0.0)
     123         *
     124         * @var string
     125         */
     126        public $global_tables = array();
     127
    110128        /** Methods ***************************************************************/
    111129
    112130        /**
     
    197215                        'has_directory'         => false,
    198216                        'notification_callback' => '',
    199217                        'search_string'         => '',
    200                         'global_tables'         => ''
     218                        'global_tables'         => '',
     219                        'meta_tables'           => ''
    201220                ) );
    202221
    203222                // Slug used for permalink URI chunk after root
     
    215234                // Notifications callback
    216235                $this->notification_callback = apply_filters( 'bp_' . $this->id . '_notification_callback', $r['notification_callback'] );
    217236
    218                 // Set up global table names
    219                 if ( !empty( $r['global_tables'] ) ) {
     237                // Set the global table names, if applicable
     238                if ( ! empty( $r['global_tables'] ) ) {
     239                        $this->register_global_tables( $r['global_tables'] );
     240                }
    220241
    221                         // This filter allows for component-specific filtering of table names
    222                         // To filter *all* tables, use the 'bp_core_get_table_prefix' filter instead
    223                         $r['global_tables'] = apply_filters( 'bp_' . $this->id . '_global_tables', $r['global_tables'] );
    224 
    225                         foreach ( $r['global_tables'] as $global_name => $table_name ) {
    226                                 $this->$global_name = $table_name;
    227                         }
     242                // Set the metadata table, if applicable
     243                if ( ! empty( $r['meta_tables'] ) ) {
     244                        $this->register_meta_tables( $r['meta_tables'] );
    228245                }
    229246
    230247                /** BuddyPress ********************************************************/
     
    444461        }
    445462
    446463        /**
     464         * Use this to register new global tables for your component, so that it
     465         * may use WordPress's database API.
     466         *
     467         * @since BuddyPress (2.0.0)
     468         *
     469         * @param array $tables
     470         */
     471        public function register_global_tables( $tables = array() ) {
     472
     473                // This filter allows for component-specific filtering of table names
     474                // To filter *all* tables, use the 'bp_core_get_table_prefix' filter instead
     475                $tables = apply_filters( 'bp_' . $this->id . '_global_tables', $tables );
     476
     477                /**
     478                 * Add the name of each global table to WPDB to allow BuddyPress
     479                 * components to play nicely with the WordPress metadata API.
     480                 */
     481                if ( !empty( $tables ) && is_array( $tables ) ) {
     482                        foreach( $tables as $global_name => $table_name ) {
     483                                $this->$global_name = $table_name;
     484                        }
     485
     486                        // Keep a record of the metadata tables in the component
     487                        $this->global_tables = $tables;
     488                }
     489
     490                do_action( 'bp_' . $this->id . '_register_global_tables' );
     491        }
     492
     493        /**
     494         * Use this to register new metadata tables for your component, so that it
     495         * may use WordPress's core metadata API.
     496         *
     497         * @since BuddyPress (2.0.0)
     498         *
     499         * @global object $wpdb
     500         * @param array $tables
     501         */
     502        public function register_meta_tables( $tables = array() ) {
     503                global $wpdb;
     504
     505                // This filter allows for component-specific filtering of table names
     506                // To filter *all* tables, use the 'bp_core_get_table_prefix' filter instead
     507                $tables = apply_filters( 'bp_' . $this->id . '_meta_tables', $tables );
     508
     509                /**
     510                 * Add the name of each metadata table to WPDB to allow BuddyPress
     511                 * components to play nicely with the WordPress metadata API.
     512                 */
     513                if ( !empty( $tables ) && is_array( $tables ) ) {
     514                        foreach( $tables as $meta_prefix => $table_name ) {
     515                                $wpdb->{$meta_prefix . 'meta'} = $table_name;
     516                        }
     517
     518                        // Keep a record of the metadata tables in the component
     519                        $this->meta_tables = $tables;
     520                }
     521
     522                do_action( 'bp_' . $this->id . '_register_meta_tables' );
     523        }
     524
     525        /**
    447526         * Set up the component post types.
    448527         *
    449528         * @since BuddyPress (1.5.0)
  • bp-core/bp-core-filters.php

     
    529529        return $menu_item;
    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}
  • bp-groups/bp-groups-functions.php

     
    956956
    957957/*** Group Meta ****************************************************/
    958958
    959 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
    960         global $wpdb, $bp;
     959function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
    961960
    962         if ( !is_numeric( $group_id ) )
     961        // Legacy - return false if non-int group ID
     962        if ( ! is_numeric( $group_id ) ) {
    963963                return false;
     964        }
    964965
     966        // Legacy - Sanitize keys
    965967        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    966968
    967         if ( is_array( $meta_value ) || is_object( $meta_value ) )
    968                 $meta_value = serialize($meta_value);
     969        add_filter( 'query', 'bp_filter_metaid_column_name' );
     970        $retval = delete_metadata( 'group', $group_id, $meta_key, $meta_value, $delete_all );
     971        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    969972
    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;
     973        return $retval;
    983974}
    984975
    985 function groups_get_groupmeta( $group_id, $meta_key = '') {
    986         global $wpdb, $bp;
     976function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) {
    987977
    988         $group_id = (int) $group_id;
     978        // Legacy - Sanitize keys
     979        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    989980
    990         if ( !$group_id )
    991                 return false;
     981        add_filter( 'query', 'bp_filter_metaid_column_name' );
     982        $retval = get_metadata( 'group', $group_id, $meta_key, $single );
     983        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    992984
    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' );
     985        // Legacy - If fetching all meta for a group, just return values
     986        if ( empty( $meta_key ) ) {
     987                $values = array();
     988                foreach ( (array) $retval as $r ) {
     989                        $values[] = array_pop( $r );
    1000990                }
    1001         } else {
    1002                 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
     991                $retval = $values;
    1003992        }
    1004993
    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;
     994        return $retval;
    1018995}
    1019996
    1020997function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
    1021         global $wpdb, $bp;
    1022998
    1023         if ( !is_numeric( $group_id ) )
    1024                 return false;
     999        add_filter( 'query', 'bp_filter_metaid_column_name' );
     1000        $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value );
     1001        remove_filter( 'query', 'bp_filter_metaid_column_name' );
    10251002
    1026         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    1027 
    1028         if ( is_string( $meta_value ) ) {
    1029                 $meta_value = stripslashes( $meta_value );
     1003        // Legacy - return true if we fall through to add_metadata()
     1004        if ( is_int( $retval ) ) {
     1005                $retval = true;
    10301006        }
    10311007
    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;
     1008        return $retval;
    10471009}
    10481010
    10491011/*** Group Cleanup Functions ****************************************************/
  • bp-groups/bp-groups-loader.php

     
    136136                        'table_name_groupmeta' => $bp->table_prefix . 'bp_groups_groupmeta'
    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.
    141146                $args = array(
     
    144149                        'has_directory'         => true,
    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
    150156                parent::setup_globals( $args );