Skip to:
Content

BuddyPress.org

Ticket #4551: 4551.4.patch

File 4551.4.patch, 9.3 KB (added by boonebgorges, 6 years ago)
  • bp-activity/bp-activity-functions.php

    diff --git bp-activity/bp-activity-functions.php bp-activity/bp-activity-functions.php
    index 9c69b88..1013180 100644
    function bp_activity_total_favorites_for_user( $user_id = 0 ) { 
    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
    function bp_activity_total_favorites_for_user( $user_id = 0 ) { 
    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 );
    568 
    569         // Trim off whitespace
    570         $meta_value = trim( $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' );
    571564
    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/**
    function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' 
    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.
    function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' 
    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 ) ) {
    622 
    623                 // Sanitize key
    624                 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    625 
    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' );
    633                 }
    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 ) );
     588        // Legacy - Sanitize keys
     589        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    638590
    639                 if ( !empty( $metas ) ) {
    640                         $metas = array_map( 'maybe_unserialize', (array) $metas );
     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' );
    641594
    642                         foreach( $metas as $mkey => $mvalue ) {
    643                                 wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $mkey, $mvalue, 'bp' );
    644                         }
     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 );
    645600                }
     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/**
    function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) { 
    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 );
    691         }
    692 
    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 ) );
     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' );
    706631
    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' );
     632        // Legacy - return true if we fall through to add_metadata()
     633        if ( is_int( $retval ) ) {
     634                $retval = true;
     635        }
    717636
    718         // Victory is ours!
    719         return true;
     637        return $retval;
    720638}
    721639
    722640/** Clean up *****************************************************************/
  • bp-activity/bp-activity-loader.php

    diff --git bp-activity/bp-activity-loader.php bp-activity/bp-activity-loader.php
    index 0312f2f..00f08d4 100644
    class BP_Activity_Component extends BP_Component { 
    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 );