Skip to:
Content

BuddyPress.org

Changeset 7854


Ignore:
Timestamp:
02/11/2014 08:22:05 PM (11 years ago)
Author:
boonebgorges
Message:

Migrate activity meta functions to the WP metadata API.

See #4551

Props boonebgorges, johnjamesjacoby

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-cache.php

    r7771 r7854  
    3131        'object_type'      => $bp->activity->id,
    3232        'object_column'    => 'activity_id',
     33        'cache_group'      => 'activity_meta',
    3334        'meta_table'       => $bp->activity->table_name_meta,
    3435        'cache_key_prefix' => 'bp_activity_meta'
     
    3637
    3738    bp_update_meta_cache( $cache_args );
    38 }
    39 
    40 /**
    41  * Clear the cache for all metadata of a given activity.
    42  *
    43  * @param int $activity_id
    44  */
    45 function bp_activity_clear_meta_cache_for_activity( $activity_id ) {
    46     global $wp_object_cache;
    47 
    48     if ( is_object( $wp_object_cache ) && ! empty( $wp_object_cache->cache['bp'] ) ) {
    49         foreach ( $wp_object_cache->cache['bp'] as $ckey => $cvalue ) {
    50             if ( 0 === strpos( $ckey, 'bp_activity_meta_' . $activity_id ) ) {
    51                 wp_cache_delete( $ckey, 'bp' );
    52             }
    53         }
    54     }
    5539}
    5640
  • trunk/bp-activity/bp-activity-classes.php

    r7815 r7854  
    825825     */
    826826    public static function delete_activity_meta_entries( $activity_ids = array() ) {
    827         global $bp, $wpdb;
    828 
    829         $activity_ids = implode( ',', wp_parse_id_list( $activity_ids ) );
    830 
    831         foreach ( (array) $activity_ids as $activity_id ) {
    832             bp_activity_clear_meta_cache_for_activity( $activity_id );
    833         }
    834 
    835         return $wpdb->query( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id IN ({$activity_ids})" );
     827        $activity_ids = wp_parse_id_list( $activity_ids );
     828
     829        foreach ( $activity_ids as $activity_id ) {
     830            bp_activity_delete_meta( $activity_id );
     831        }
     832
     833        return true;
    836834    }
    837835
  • trunk/bp-activity/bp-activity-functions.php

    r7852 r7854  
    557557    global $wpdb, $bp;
    558558
    559     // Return false if any of the above values are not set
    560     if ( !is_numeric( $activity_id ) )
    561         return false;
    562 
    563     // Sanitize key
     559    // Legacy - Return false if any of the above values are not set
     560    if ( ! is_numeric( $activity_id ) ) {
     561        return false;
     562    }
     563
     564    // Legacy - Sanitize key
    564565    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    565566
    566     if ( is_array( $meta_value ) || is_object( $meta_value ) )
    567         $meta_value = serialize( $meta_value );
    568 
    569     // Trim off whitespace
     567    // Legacy - Trim off whitespace
    570568    $meta_value = trim( $meta_value );
    571569
    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     if ( $meta_key ) {
    586         wp_cache_delete( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, 'bp' );
     570    // Legacy - if no meta_key is passed, delete all for the item
     571    if ( empty( $meta_key ) ) {
     572        $all_meta = bp_activity_get_meta( $activity_id );
     573        $keys     = ! empty( $all_meta ) ? wp_list_pluck( $all_meta, 'meta_key' ) : array();
    587574    } else {
    588         bp_activity_clear_meta_cache_for_activity( $activity_id );
    589     }
    590 
    591     // Success
    592     if ( !is_wp_error( $retval ) )
    593         return true;
    594 
    595     // Fail
    596     else
    597         return false;
     575        $keys = array( $meta_key );
     576    }
     577
     578    add_filter( 'query', 'bp_filter_metaid_column_name' );
     579    foreach ( $keys as $key ) {
     580        $retval = delete_metadata( 'activity', $activity_id, $key, $meta_value );
     581    }
     582    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     583
     584    return $retval;
    598585}
    599586
     
    603590 * @since BuddyPress (1.2)
    604591 *
    605  * @global object $wpdb WordPress database access object.
    606  * @global object $bp BuddyPress global settings.
    607  * @uses wp_cache_get()
    608  * @uses wp_cache_set()
    609592 * @uses apply_filters() To call the 'bp_activity_get_meta' hook.
    610593 *
     
    615598 * @return mixed The meta value(s) being requested.
    616599 */
    617 function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) {
    618     global $wpdb, $bp;
     600function bp_activity_get_meta( $activity_id = 0, $meta_key = '', $single = true ) {
    619601
    620602    // Make sure activity_id is valid
    621     if ( empty( $activity_id ) || !is_numeric( $activity_id ) )
    622         return false;
    623 
    624     // We have a key to look for
    625     if ( !empty( $meta_key ) ) {
    626 
    627         // Sanitize key
    628         $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    629 
    630         // Check cache
    631         if ( !$metas = wp_cache_get( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, 'bp' ) ) {
    632             // No cache so hit the DB
    633             $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 ) );
    634 
    635             // Set cache
    636             wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, $metas, 'bp' );
     603    if ( empty( $activity_id ) || ! is_numeric( $activity_id ) ) {
     604        return false;
     605    }
     606
     607    // Legacy - Sanitize keys
     608    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     609
     610    add_filter( 'query', 'bp_filter_metaid_column_name' );
     611    $retval = get_metadata( 'activity', $activity_id, $meta_key, $single );
     612    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     613
     614    // Legacy - If fetching all meta for a group, just return the first
     615    // of each found value
     616    if ( empty( $meta_key ) ) {
     617        $values = array();
     618        foreach ( (array) $retval as $rkey => $rvalue ) {
     619            $found = new stdClass;
     620            $found->meta_key = $rkey;
     621            $found->meta_value = array_pop( array_reverse( $rvalue ) );
     622            $values[] = $found;
    637623        }
    638624
    639     // No key so get all for activity_id
    640     } else {
    641         $metas = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
    642 
    643         if ( !empty( $metas ) ) {
    644             $metas = array_map( 'maybe_unserialize', (array) $metas );
    645 
    646             foreach( $metas as $mkey => $mvalue ) {
    647                 wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $mkey, $mvalue, 'bp' );
    648             }
     625        // If nothing was found, return false
     626        if ( empty( $values ) ) {
     627            $retval = false;
     628        } else {
     629            $retval = $values;
    649630        }
    650631    }
    651632
    652     // No result so return false
    653     if ( empty( $metas ) )
    654         return false;
    655 
    656     // Maybe, just maybe... unserialize
    657     $metas = array_map( 'maybe_unserialize', (array) $metas );
    658 
    659     // Return first item in array if only 1, else return all metas found
    660     $retval = ( 1 == count( $metas ) ? $metas[0] : $metas );
     633    // Legacy - On failure, expect false, not an empty string
     634    if ( '' === $retval ) {
     635        $retval = false;
     636    }
    661637
    662638    // Filter result before returning
    663     return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key );
     639    return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key, $single );
    664640}
    665641
     
    668644 *
    669645 * @since BuddyPress (1.2)
    670  *
    671  * @global object $wpdb WordPress database access object.
    672  * @global object $bp BuddyPress global settings.
    673  * @uses maybe_serialize()
    674  * @uses bp_activity_delete_meta()
    675  * @uses wp_cache_set()
    676646 *
    677647 * @param int $activity_id ID of the activity item whose metadata is being updated.
     
    681651 */
    682652function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) {
    683     global $wpdb, $bp;
    684 
    685     // Make sure activity_id is valid
    686     if ( !is_numeric( $activity_id ) )
    687         return false;
    688 
    689     // Sanitize key
     653
     654    // Legacy - Make sure activity_id is valid
     655    if ( ! is_numeric( $activity_id ) ) {
     656        return false;
     657    }
     658
     659    // Legacy - Sanitize key
    690660    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    691661
    692     // Sanitize value
    693     if ( is_string( $meta_value ) ) {
    694         $meta_value = stripslashes( $meta_value );
    695     }
    696 
    697     // Maybe, just maybe... serialize
    698     $meta_value = maybe_serialize( $meta_value );
    699 
    700     // If value is false, delete the meta key
    701     if ( false === $meta_value )
    702         return bp_activity_delete_meta( $activity_id, $meta_key );
    703 
    704     // See if meta key exists for activity_id
    705     $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 ) );
    706 
    707     // Meta key does not exist so INSERT
    708     if ( empty( $cur ) )
    709         $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 ) );
    710 
    711     // Meta key exists, so UPDATE
    712     else if ( $cur->meta_value != $meta_value )
    713         $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 ) );
    714 
    715     // Weirdness, so return false
    716     else
    717         return false;
    718 
    719     // Set cache
    720     wp_cache_set( 'bp_activity_meta_' . $activity_id . '_' . $meta_key, $meta_value, 'bp' );
    721 
    722     // Victory is ours!
    723     return true;
     662    add_filter( 'query', 'bp_filter_metaid_column_name' );
     663    $retval = update_metadata( 'activity', $activity_id, $meta_key, $meta_value );
     664    remove_filter( 'query', 'bp_filter_metaid_column_name' );
     665
     666    // Legacy - return true if we fall through to add_metadata()
     667    if ( is_int( $retval ) ) {
     668        $retval = true;
     669    }
     670
     671    return $retval;
    724672}
    725673
  • trunk/bp-activity/bp-activity-loader.php

    r7756 r7854  
    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.
     
    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
  • trunk/bp-loader.php

    r7823 r7854  
    454454            require( $this->plugin_dir . 'bp-core/deprecated/1.6.php' );
    455455            require( $this->plugin_dir . 'bp-core/deprecated/1.7.php' );
     456            require( $this->plugin_dir . 'bp-core/deprecated/2.0.php' );
    456457        }
    457458    }
Note: See TracChangeset for help on using the changeset viewer.