Ticket #4551: 4551.2.patch
File 4551.2.patch, 10.2 KB (added by , 11 years ago) |
---|
-
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 );