Ticket #4551: 4551.3.patch
File 4551.3.patch, 19.0 KB (added by , 11 years ago) |
---|
-
bp-activity/bp-activity-functions.php
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 … … 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 ); 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' ); 568 564 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; 594 566 } 595 567 596 568 /** … … 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. … … 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 for621 if ( !empty( $meta_key ) ) {588 // Legacy - Sanitize keys 589 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 622 590 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' ); 625 594 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 ); 633 600 } 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; 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 /** … … 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 ); 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; 691 635 } 692 636 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; 720 638 } 721 639 722 640 /** Clean up *****************************************************************/ -
bp-activity/bp-activity-loader.php
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 ); -
bp-core/bp-core-component.php
107 107 */ 108 108 public $root_slug = ''; 109 109 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 110 128 /** Methods ***************************************************************/ 111 129 112 130 /** … … 197 215 'has_directory' => false, 198 216 'notification_callback' => '', 199 217 'search_string' => '', 200 'global_tables' => '' 218 'global_tables' => '', 219 'meta_tables' => '' 201 220 ) ); 202 221 203 222 // Slug used for permalink URI chunk after root … … 215 234 // Notifications callback 216 235 $this->notification_callback = apply_filters( 'bp_' . $this->id . '_notification_callback', $r['notification_callback'] ); 217 236 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 } 220 241 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'] ); 228 245 } 229 246 230 247 /** BuddyPress ********************************************************/ … … 444 461 } 445 462 446 463 /** 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 /** 447 526 * Set up the component post types. 448 527 * 449 528 * @since BuddyPress (1.5.0) -
bp-core/bp-core-filters.php
529 529 return $menu_item; 530 530 } 531 531 add_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 */ 549 function bp_filter_metaid_column_name( $q ) { 550 return str_replace( 'meta_id', 'id', $q ); 551 } -
bp-groups/bp-groups-functions.php
956 956 957 957 /*** Group Meta ****************************************************/ 958 958 959 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) { 960 global $wpdb, $bp; 959 function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) { 961 960 962 if ( !is_numeric( $group_id ) ) 961 // Legacy - return false if non-int group ID 962 if ( ! is_numeric( $group_id ) ) { 963 963 return false; 964 } 964 965 966 // Legacy - Sanitize keys 965 967 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 966 968 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' ); 969 972 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; 983 974 } 984 975 985 function groups_get_groupmeta( $group_id, $meta_key = '') { 986 global $wpdb, $bp; 976 function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) { 987 977 988 $group_id = (int) $group_id; 978 // Legacy - Sanitize keys 979 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key ); 989 980 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' ); 992 984 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 ); 1000 990 } 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; 1003 992 } 1004 993 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; 1018 995 } 1019 996 1020 997 function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) { 1021 global $wpdb, $bp;1022 998 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' ); 1025 1002 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; 1030 1006 } 1031 1007 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; 1047 1009 } 1048 1010 1049 1011 /*** Group Cleanup Functions ****************************************************/ -
bp-groups/bp-groups-loader.php
136 136 'table_name_groupmeta' => $bp->table_prefix . 'bp_groups_groupmeta' 137 137 ); 138 138 139 // Metadata tables for groups component 140 $meta_tables = array( 141 'group' => $bp->table_prefix . 'bp_groups_groupmeta' 142 ); 143 139 144 // All globals for groups component. 140 145 // Note that global_tables is included in this array. 141 146 $args = array( … … 144 149 'has_directory' => true, 145 150 'notification_callback' => 'groups_format_notifications', 146 151 'search_string' => __( 'Search Groups...', 'buddypress' ), 147 'global_tables' => $global_tables 152 'global_tables' => $global_tables, 153 'meta_tables' => $meta_tables 148 154 ); 149 155 150 156 parent::setup_globals( $args );