Changeset 5709
- Timestamp:
- 02/11/2012 04:25:41 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-friends/bp-friends-classes.php
r5676 r5709 1 1 <?php 2 2 3 // Exit if accessed directly 3 4 if ( !defined( 'ABSPATH' ) ) exit; … … 16 17 var $friend; 17 18 18 function bp_friends_friendship( $id = null, $is_request = false, $populate_friend_details = true ) { 19 $this->__construct( $id, $is_request, $populate_friend_details ); 20 } 21 22 function __construct( $id = null, $is_request = false, $populate_friend_details = true ) { 19 public function __construct( $id = null, $is_request = false, $populate_friend_details = true ) { 23 20 $this->is_request = $is_request; 24 21 25 if ( $id) {26 $this->id = $id;22 if ( !empty( $id ) ) { 23 $this->id = $id; 27 24 $this->populate_friend_details = $populate_friend_details; 28 25 $this->populate( $this->id ); … … 30 27 } 31 28 32 function populate() {33 global $wpdb, $bp , $creds;29 private function populate() { 30 global $wpdb, $bp; 34 31 35 32 if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) { … … 41 38 } 42 39 43 // if running from ajax. 44 if ( !bp_displayed_user_id() ) 45 $bp->displayed_user->id = $creds['current_userid']; 46 47 if ( $this->populate_friend_details ) { 40 if ( !empty( $this->populate_friend_details ) ) { 48 41 if ( $this->friend_user_id == bp_displayed_user_id() ) { 49 42 $this->friend = new BP_Core_User( $this->initiator_user_id ); … … 54 47 } 55 48 56 function save() {49 public function save() { 57 50 global $wpdb, $bp; 58 51 … … 65 58 do_action_ref_array( 'friends_friendship_before_save', array( &$this ) ); 66 59 67 if ( $this->id ) {68 // Update60 // Update 61 if (!empty( $this->id ) ) { 69 62 $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET initiator_user_id = %d, friend_user_id = %d, is_confirmed = %d, is_limited = %d, date_created = %s ) WHERE id = %d", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created, $this->id ) ); 70 } else { 71 // Save 63 64 // Save 65 } else { 72 66 $result = $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->friends->table_name} ( initiator_user_id, friend_user_id, is_confirmed, is_limited, date_created ) VALUES ( %d, %d, %d, %d, %s )", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created ) ); 73 67 $this->id = $wpdb->insert_id; … … 79 73 } 80 74 81 function delete() {75 public function delete() { 82 76 global $wpdb, $bp; 83 77 … … 85 79 } 86 80 87 /* Static Functions*/88 89 function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {90 global $wpdb, $bp; 91 92 if ( $friend_requests_only) {81 /** Static Methods ********************************************************/ 82 83 public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) { 84 global $wpdb, $bp; 85 86 if ( !empty( $friend_requests_only ) ) { 93 87 $oc_sql = $wpdb->prepare( "AND is_confirmed = 0" ); 94 88 $friend_sql = $wpdb->prepare ( " WHERE friend_user_id = %d", $user_id ); … … 102 96 103 97 for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) { 104 if ( $assoc_arr )98 if ( !empty( $assoc_arr ) ) { 105 99 $fids[] = array( 'user_id' => ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id ); 106 else100 } else { 107 101 $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id; 102 } 108 103 } 109 104 … … 111 106 } 112 107 113 function get_friendship_id( $user_id, $friend_id ) {108 public static function get_friendship_id( $user_id, $friend_id ) { 114 109 global $wpdb, $bp; 115 110 … … 117 112 } 118 113 119 function get_friendship_request_user_ids( $user_id ) {114 public static function get_friendship_request_user_ids( $user_id ) { 120 115 global $wpdb, $bp; 121 116 … … 123 118 } 124 119 125 function total_friend_count( $user_id = 0 ) {126 global $wpdb, $bp; 127 128 if ( !$user_id)120 public static function total_friend_count( $user_id = 0 ) { 121 global $wpdb, $bp; 122 123 if ( empty( $user_id ) ) 129 124 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 130 125 … … 135 130 136 131 // Do not update meta if user has never had friends 137 if ( !$count&& !bp_get_user_meta( $user_id, 'total_friend_count', true ) )132 if ( empty( $count ) && !bp_get_user_meta( $user_id, 'total_friend_count', true ) ) 138 133 return 0; 139 134 140 135 bp_update_user_meta( $user_id, 'total_friend_count', (int)$count ); 141 return (int) $count;142 } 143 144 function search_friends( $filter, $user_id, $limit = null, $page = null ) {136 return (int) $count; 137 } 138 139 public static function search_friends( $filter, $user_id, $limit = null, $page = null ) { 145 140 global $wpdb, $bp; 146 141 147 142 // TODO: Optimize this function. 148 143 149 if ( !$user_id)144 if ( empty( $user_id ) ) 150 145 $user_id = bp_loggedin_user_id(); 151 146 152 147 $filter = like_escape( $wpdb->escape( $filter ) ); 153 148 154 if ( $limit && $page)149 if ( !empty( $limit ) && !empty( $page ) ) 155 150 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 156 151 … … 161 156 $fids = implode( ',', $friend_ids ); 162 157 163 if ( empty( $fids) )158 if ( empty( $fids ) ) 164 159 return false; 165 160 … … 173 168 } 174 169 175 $filtered_friend_ids = $wpdb->get_col( $sql);176 $total_friend_ids = $wpdb->get_var($total_sql);177 178 if ( !$filtered_friend_ids)170 $filtered_friend_ids = $wpdb->get_col( $sql ); 171 $total_friend_ids = $wpdb->get_var( $total_sql ); 172 173 if ( empty( $filtered_friend_ids ) ) 179 174 return false; 180 175 … … 182 177 } 183 178 184 function check_is_friend( $loggedin_userid, $possible_friend_userid ) {185 global $wpdb, $bp; 186 187 if ( !$loggedin_userid || !$possible_friend_userid)179 public static function check_is_friend( $loggedin_userid, $possible_friend_userid ) { 180 global $wpdb, $bp; 181 182 if ( empty( $loggedin_userid ) || empty( $possible_friend_userid ) ) 188 183 return false; 189 184 190 185 $result = $wpdb->get_results( $wpdb->prepare( "SELECT id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)", $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid ) ); 191 186 192 if ( $result) {193 if ( 0 == (int) $result[0]->is_confirmed ) {187 if ( !empty( $result ) ) { 188 if ( 0 == (int) $result[0]->is_confirmed ) { 194 189 return 'pending'; 195 190 } else { … … 201 196 } 202 197 203 function get_bulk_last_active( $user_ids ) {204 global $wpdb , $bp;198 public static function get_bulk_last_active( $user_ids ) { 199 global $wpdb; 205 200 206 201 return $wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key( 'last_activity' ) ) ); 207 202 } 208 203 209 function accept($friendship_id) {204 public static function accept($friendship_id) { 210 205 global $wpdb, $bp; 211 206 … … 213 208 } 214 209 215 function reject($friendship_id) {210 public static function reject($friendship_id) { 216 211 global $wpdb, $bp; 217 212 … … 219 214 } 220 215 221 function search_users( $filter, $user_id, $limit = null, $page = null ) {222 global $wpdb , $bp;216 public static function search_users( $filter, $user_id, $limit = null, $page = null ) { 217 global $wpdb; 223 218 224 219 $filter = like_escape( $wpdb->escape( $filter ) ); 225 220 226 221 $usermeta_table = $wpdb->base_prefix . 'usermeta'; 227 $users_table = $wpdb->base_prefix . 'users';228 229 if ( $limit && $page)222 $users_table = $wpdb->base_prefix . 'users'; 223 224 if ( !empty( $limit ) && !empty( $page ) ) 230 225 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) ); 231 226 … … 239 234 $filtered_fids = $wpdb->get_col($sql); 240 235 241 if ( !$filtered_fids)236 if ( empty( $filtered_fids ) ) 242 237 return false; 243 238 … … 245 240 } 246 241 247 function search_users_count( $filter ) {242 public static function search_users_count( $filter ) { 248 243 global $wpdb, $bp; 249 244 … … 251 246 252 247 $usermeta_table = $wpdb->prefix . 'usermeta'; 253 $users_table = $wpdb->base_prefix . 'users';248 $users_table = $wpdb->base_prefix . 'users'; 254 249 255 250 // filter the user_ids based on the search criteria. … … 262 257 $user_count = $wpdb->get_col($sql); 263 258 264 if ( !$user_count)259 if ( empty( $user_count ) ) 265 260 return false; 266 261 … … 268 263 } 269 264 270 function sort_by_name( $user_ids ) {265 public static function sort_by_name( $user_ids ) { 271 266 global $wpdb, $bp; 272 267 … … 277 272 } 278 273 279 function get_random_friends( $user_id, $total_friends = 5 ) { 280 global $wpdb, $bp; 281 282 $sql = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends ); 283 $results = $wpdb->get_results($sql); 274 public static function get_random_friends( $user_id, $total_friends = 5 ) { 275 global $wpdb, $bp; 276 277 $fids = array(); 278 $sql = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends ); 279 $results = $wpdb->get_results( $sql ); 284 280 285 281 for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) { … … 289 285 // remove duplicates 290 286 if ( count( $fids ) > 0 ) 291 return array_flip( array_flip($fids));287 return array_flip( array_flip( $fids ) ); 292 288 else 293 289 return false; 294 290 } 295 291 296 function get_invitable_friend_count( $user_id, $group_id ) {292 public static function get_invitable_friend_count( $user_id, $group_id ) { 297 293 298 294 // Setup some data we'll use below … … 321 317 } 322 318 323 function get_user_ids_for_friendship( $friendship_id ) {319 public static function get_user_ids_for_friendship( $friendship_id ) { 324 320 global $wpdb, $bp; 325 321 … … 327 323 } 328 324 329 function delete_all_for_user( $user_id ) {325 public static function delete_all_for_user( $user_id ) { 330 326 global $wpdb, $bp; 331 327 … … 345 341 } 346 342 } 343 347 344 ?>
Note: See TracChangeset
for help on using the changeset viewer.