Ticket #8360: 8360.1.diff
File 8360.1.diff, 12.7 KB (added by , 4 years ago) |
---|
-
src/bp-core/classes/class-bp-core-notification.php
24 24 * 25 25 * @var int 26 26 */ 27 public $id ;27 public $id = 0; 28 28 29 29 /** 30 30 * The ID to which the notification relates to within the component. 31 31 * 32 32 * @var int 33 33 */ 34 public $item_id ;34 public $item_id = 0; 35 35 36 36 /** 37 37 * The secondary ID to which the notification relates to within the component. … … 45 45 * 46 46 * @var int 47 47 */ 48 public $user_id ;48 public $user_id = 0; 49 49 50 50 /** 51 51 * The name of the component that the notification is for. 52 52 * 53 53 * @var string 54 54 */ 55 public $component_name ;55 public $component_name = ''; 56 56 57 57 /** 58 58 * The action within the component which the notification is related to. 59 59 * 60 60 * @var string 61 61 */ 62 public $component_action ;62 public $component_action = ''; 63 63 64 64 /** 65 65 * The date the notification was created. 66 66 * 67 67 * @var string 68 68 */ 69 public $date_notified ;69 public $date_notified = ''; 70 70 71 71 /** 72 72 * Is the notification new or has it already been read. 73 73 * 74 74 * @var boolean 75 75 */ 76 public $is_new ;76 public $is_new = false; 77 77 78 78 /** Public Methods ********************************************************/ 79 79 … … 83 83 * @param int $id ID for the notification. 84 84 */ 85 85 public function __construct( $id = 0 ) { 86 if ( !empty( $id ) ) { 87 $this->id = $id; 88 $this->populate(); 86 87 // Bail if no ID 88 if ( empty( $id ) ) { 89 return; 89 90 } 91 92 $this->id = absint( $id ); 93 $this->populate(); 90 94 } 91 95 92 96 /** … … 102 106 $bp = buddypress(); 103 107 104 108 // Update. 105 if ( !empty( $this->id ) ) { 106 $sql = $wpdb->prepare( "UPDATE {$bp->core->table_name_notifications} SET item_id = %d, secondary_item_id = %d, user_id = %d, component_name = %s, component_action = %d, date_notified = %s, is_new = %d ) WHERE id = %d", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new, $this->id ); 109 if ( ! empty( $this->id ) ) { 110 $query = "UPDATE {$bp->notifications->table_name} SET item_id = %d, secondary_item_id = %d, user_id = %d, component_name = %s, component_action = %d, date_notified = %s, is_new = %d ) WHERE id = %d"; 111 $sql = $wpdb->prepare( $query, $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new, $this->id ); 107 112 108 113 // Save. 109 114 } else { 110 $sql = $wpdb->prepare( "INSERT INTO {$bp->core->table_name_notifications} ( item_id, secondary_item_id, user_id, component_name, component_action, date_notified, is_new ) VALUES ( %d, %d, %d, %s, %s, %s, %d )", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new ); 115 $query = "INSERT INTO {$bp->notifications->table_name} ( item_id, secondary_item_id, user_id, component_name, component_action, date_notified, is_new ) VALUES ( %d, %d, %d, %s, %s, %s, %d )"; 116 $sql = $wpdb->prepare( $query, $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_notified, $this->is_new ); 111 117 } 112 118 113 if ( !$result = $wpdb->query( $sql ) ) 119 $result = $wpdb->query( $sql ); 120 121 if ( empty( $result ) || is_wp_error( $result ) ) { 114 122 return false; 123 } 115 124 116 125 $this->id = $wpdb->insert_id; 117 126 … … 130 139 131 140 $bp = buddypress(); 132 141 133 if ( $notification = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE id = %d", $this->id ) ) ) { 134 $this->item_id = $notification->item_id; 135 $this->secondary_item_id = $notification->secondary_item_id; 136 $this->user_id = $notification->user_id; 137 $this->component_name = $notification->component_name; 138 $this->component_action = $notification->component_action; 139 $this->date_notified = $notification->date_notified; 140 $this->is_new = $notification->is_new; 142 $query = "SELECT * FROM {$bp->notifications->table_name} WHERE id = %d"; 143 $prepare = $wpdb->prepare( $query, $this->id ); 144 $result = $wpdb->get_row( $prepare ); 145 146 if ( ! empty( $result ) ) { 147 $this->item_id = $result->item_id; 148 $this->secondary_item_id = $result->secondary_item_id; 149 $this->user_id = $result->user_id; 150 $this->component_name = $result->component_name; 151 $this->component_action = $result->component_action; 152 $this->date_notified = $result->date_notified; 153 $this->is_new = $result->is_new; 141 154 } 142 155 } 143 156 … … 150 163 * @param int $notification_id Notification ID to check for. 151 164 * @return string 152 165 */ 153 public static function check_access( $user_id , $notification_id) {166 public static function check_access( $user_id = 0, $notification_id = 0 ) { 154 167 global $wpdb; 155 168 156 169 $bp = buddypress(); 157 170 158 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->core->table_name_notifications} WHERE id = %d AND user_id = %d", $notification_id, $user_id ) ); 171 $query = "SELECT COUNT(id) FROM {$bp->notifications->table_name} WHERE id = %d AND user_id = %d"; 172 $prepare = $wpdb->prepare( $query, $notification_id, $user_id ); 173 $result = $wpdb->get_var( $prepare ); 174 175 return $result; 159 176 } 160 177 161 178 /** … … 178 195 ? ' AND is_new = 1 ' 179 196 : ''; 180 197 181 return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->core->table_name_notifications} WHERE user_id = %d {$is_new}", $user_id ) ); 198 $query = "SELECT * FROM {$bp->notifications->table_name} WHERE user_id = %d {$is_new}"; 199 $prepare = $wpdb->prepare( $query, $user_id ); 200 $result = $wpdb->get_results( $prepare ); 201 202 return $result; 182 203 } 183 204 184 205 /** … … 198 219 199 220 $bp = buddypress(); 200 221 201 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE user_id = %d AND component_name = %s AND component_action = %s", $user_id, $component_name, $component_action ) ); 222 $query = "DELETE FROM {$bp->notifications->table_name} WHERE user_id = %d AND component_name = %s AND component_action = %s"; 223 $prepare = $wpdb->prepare( $query, $user_id, $component_name, $component_action ); 224 $result = $wpdb->query( $prepare ); 225 226 return $result; 202 227 } 203 228 204 229 /** … … 212 237 * @param int $item_id The item ID of the notifications we wish to delete. 213 238 * @param string $component_name The name of the component that the notifications we wish to delete. 214 239 * @param string $component_action The action of the component that the notifications we wish to delete. 215 * @param int |bool$secondary_item_id (optional) The secondary item id of the notifications that we wish to240 * @param int $secondary_item_id (optional) The secondary item id of the notifications that we wish to 216 241 * use to delete. 217 242 * @return mixed 218 243 */ 219 public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = false) {244 public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = 0 ) { 220 245 global $wpdb; 221 246 222 247 $bp = buddypress(); 223 248 224 $secondary_item_sql = ! empty( $secondary_item_id )249 $secondary_item_sql = ! empty( $secondary_item_id ) 225 250 ? $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id ) 226 251 : ''; 227 252 228 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE user_id = %d AND item_id = %d AND component_name = %s AND component_action = %s{$secondary_item_sql}", $user_id, $item_id, $component_name, $component_action ) ); 253 $query = "DELETE FROM {$bp->notifications->table_name} WHERE user_id = %d AND item_id = %d AND component_name = %s AND component_action = %s{$secondary_item_sql}"; 254 $prepare = $wpdb->prepare( $query, $user_id, $item_id, $component_name, $component_action ); 255 $result = $wpdb->query( $prepare ); 256 257 return $result; 229 258 } 230 259 231 260 /** … … 245 274 246 275 $bp = buddypress(); 247 276 248 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE item_id = %d AND component_name = %s AND component_action = %s", $user_id, $component_name, $component_action ) ); 277 $query = "DELETE FROM {$bp->notifications->table_name} WHERE item_id = %d AND component_name = %s AND component_action = %s"; 278 $prepare = $wpdb->prepare( $query, $user_id, $component_name, $component_action ); 279 $result = $wpdb->query( $prepare ); 280 281 return $result; 249 282 } 250 283 251 284 /** … … 256 289 * 257 290 * @static 258 291 * 259 * @param string$item_id The item id that they notifications are to be for.292 * @param int $item_id The item id that they notifications are to be for. 260 293 * @param string $component_name The component that the notifications are to be from. 261 294 * @param string $component_action The action that the notifications are to be from. 262 * @param string$secondary_item_id Optional secondary item id that the notifications are to have.295 * @param int $secondary_item_id Optional secondary item id that the notifications are to have. 263 296 * @return mixed 264 297 */ 265 public static function delete_all_by_type( $item_id, $component_name, $component_action , $secondary_item_id) {298 public static function delete_all_by_type( $item_id, $component_name, $component_action = '', $secondary_item_id = 0 ) { 266 299 global $wpdb; 267 300 268 if ( $component_action ) 269 $component_action_sql = $wpdb->prepare( "AND component_action = %s", $component_action ); 270 else 271 $component_action_sql = ''; 301 $component_action_sql = ! empty( $component_action ) 302 ? $wpdb->prepare( "AND component_action = %s", $component_action ) 303 : ''; 272 304 273 if ( $secondary_item_id ) 274 $secondary_item_sql = $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id ); 275 else 276 $secondary_item_sql = ''; 305 $secondary_item_sql = ! empty( $secondary_item_id ) 306 ? $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id ) 307 : ''; 277 308 278 309 $bp = buddypress(); 279 310 280 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->core->table_name_notifications} WHERE item_id = %d AND component_name = %s {$component_action_sql} {$secondary_item_sql}", $item_id, $component_name ) ); 311 $query = "DELETE FROM {$bp->notifications->table_name} WHERE item_id = %d AND component_name = %s {$component_action_sql} {$secondary_item_sql}"; 312 $prepare = $wpdb->prepare( $query, $item_id, $component_name ); 313 $result = $wpdb->query( $prepare ); 314 315 return $result; 281 316 } 282 317 } -
src/bp-core/classes/class-bp-core.php
36 36 } 37 37 38 38 /** 39 * Magic getter. 40 * 41 * This exists specifically for supporting deprecated object vars. 42 * 43 * @since 7.0.0 44 * 45 * @param string $key 46 * @return mixed 47 */ 48 public function __get( $key = '' ) { 49 50 // Backwards compatibility for the original Notifications table var 51 if ( 'table_name_notifications' === $key ) { 52 return bp_is_active( 'notifications' ) 53 ? buddypress()->notifications->table_name 54 : buddypress()->table_prefix . 'bp_notifications'; 55 } 56 57 // Return object var if set, else null 58 return isset( $this->{$key} ) 59 ? $this->{$key} 60 : null; 61 } 62 63 /** 39 64 * Populate the global data needed before BuddyPress can continue. 40 65 * 41 66 * This involves figuring out the currently required, activated, deactivated, … … 249 274 */ 250 275 $bp->grav_default->blog = apply_filters( 'bp_blog_gravatar_default', $bp->grav_default->user ); 251 276 252 // Notifications table. Included here for legacy purposes. Use253 // bp-notifications instead.254 $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications';255 256 277 // Backward compatibility for plugins modifying the legacy bp_nav and bp_options_nav global properties. 257 278 $bp->bp_nav = new BP_Core_BP_Nav_BackCompat(); 258 279 $bp->bp_options_nav = new BP_Core_BP_Options_Nav_BackCompat(); -
src/bp-notifications/classes/class-bp-notifications-notification.php
556 556 * @return bool True if the notification belongs to the user, otherwise 557 557 * false. 558 558 */ 559 public static function check_access( $user_id , $notification_id) {559 public static function check_access( $user_id = 0, $notification_id = 0 ) { 560 560 global $wpdb; 561 561 562 562 $bp = buddypress(); 563 563 564 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->core->table_name_notifications} WHERE id = %d AND user_id = %d", $notification_id, $user_id ) ); 564 $query = "SELECT COUNT(id) FROM {$bp->notifications->table_name} WHERE id = %d AND user_id = %d"; 565 $prepare = $wpdb->prepare( $query, $notification_id, $user_id ); 566 $result = $wpdb->get_var( $prepare ); 567 568 return $result; 565 569 } 566 570 567 571 /**