Ticket #4551: 4551.4.patch
| File 4551.4.patch, 9.3 KB (added by , 12 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 ) { 540 540 * 541 541 * @since BuddyPress (1.2) 542 542 * 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 *548 543 * @param int $activity_id ID of the activity item whose metadata is being deleted. 549 544 * @param string $meta_key Optional. The key of the metadata being deleted. If 550 545 * omitted, all metadata associated with the activity … … function bp_activity_total_favorites_for_user( $user_id = 0 ) { 553 548 * deleted if the meta_value matches this parameter. 554 549 * @return bool True on success, false on failure. 555 550 */ 556 function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) { 557 global $wpdb, $bp; 551 function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '', $delete_all = false ) { 558 552 559 553 // Return false if any of the above values are not set 560 if ( !is_numeric( $activity_id ) ) 554 if ( !is_numeric( $activity_id ) ) { 561 555 return false; 556 } 562 557 563 // Sanitize key558 // Legacy - Sanitize keys 564 559 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 565 560 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' ); 571 564 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; 594 566 } 595 567 596 568 /** … … function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' 598 570 * 599 571 * @since BuddyPress (1.2) 600 572 * 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()605 573 * @uses apply_filters() To call the 'bp_activity_get_meta' hook. 606 574 * 607 575 * @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 = '' 610 578 * metadata for the activity item will be fetched. 611 579 * @return mixed The meta value(s) being requested. 612 580 */ 613 function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) { 614 global $wpdb, $bp; 581 function bp_activity_get_meta( $activity_id = 0, $meta_key = '', $single = true ) { 615 582 616 583 // Make sure activity_id is valid 617 if ( empty( $activity_id ) || !is_numeric( $activity_id ) ) 584 if ( empty( $activity_id ) || !is_numeric( $activity_id ) ) { 618 585 return false; 586 } 619 587 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 ); 638 590 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' ); 641 594 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 ); 645 600 } 601 $retval = $values; 646 602 } 647 603 648 // No result so return false649 if ( empty( $metas ) )650 return false;651 652 // Maybe, just maybe... unserialize653 $metas = array_map( 'maybe_unserialize', (array) $metas );654 655 // Return first item in array if only 1, else return all metas found656 $retval = ( 1 == count( $metas ) ? $metas[0] : $metas );657 658 604 // 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 ); 660 606 } 661 607 662 608 /** … … function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) { 664 610 * 665 611 * @since BuddyPress (1.2) 666 612 * 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 *673 613 * @param int $activity_id ID of the activity item whose metadata is being updated. 674 614 * @param string $meta_key Key of the metadata being updated. 675 615 * @param mixed $meta_value Value to be set. 676 616 * @return bool True on success, false on failure. 677 617 */ 678 618 function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) { 679 global $wpdb, $bp;680 619 681 620 // Make sure activity_id is valid 682 if ( !is_numeric( $activity_id ) ) 621 if ( !is_numeric( $activity_id ) ) { 683 622 return false; 623 } 684 624 685 625 // Sanitize key 686 626 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 687 627 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' ); 706 631 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 } 717 636 718 // Victory is ours! 719 return true; 637 return $retval; 720 638 } 721 639 722 640 /** 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 { 95 95 'table_name_meta' => $bp->table_prefix . 'bp_activity_meta', 96 96 ); 97 97 98 // Metadata tables for groups component 99 $meta_tables = array( 100 'activity' => $bp->table_prefix . 'bp_activity_meta' 101 ); 102 98 103 // All globals for activity component. 99 104 // Note that global_tables is included in this array. 100 105 $args = array( 101 106 'slug' => BP_ACTIVITY_SLUG, 102 107 'root_slug' => isset( $bp->pages->activity->slug ) ? $bp->pages->activity->slug : BP_ACTIVITY_SLUG, 103 108 'has_directory' => true, 109 'notification_callback' => 'bp_activity_format_notifications', 104 110 'search_string' => __( 'Search Activity...', 'buddypress' ), 105 111 'global_tables' => $global_tables, 106 ' notification_callback' => 'bp_activity_format_notifications',112 'meta_tables' => $meta_tables 107 113 ); 108 114 109 115 parent::setup_globals( $args );