Skip to:
Content

BuddyPress.org

Ticket #8360: 8360.1.diff

File 8360.1.diff, 12.7 KB (added by johnjamesjacoby, 4 years ago)
  • src/bp-core/classes/class-bp-core-notification.php

     
    2424         *
    2525         * @var int
    2626         */
    27         public $id;
     27        public $id = 0;
    2828
    2929        /**
    3030         * The ID to which the notification relates to within the component.
    3131         *
    3232         * @var int
    3333         */
    34         public $item_id;
     34        public $item_id = 0;
    3535
    3636        /**
    3737         * The secondary ID to which the notification relates to within the component.
     
    4545         *
    4646         * @var int
    4747         */
    48         public $user_id;
     48        public $user_id = 0;
    4949
    5050        /**
    5151         * The name of the component that the notification is for.
    5252         *
    5353         * @var string
    5454         */
    55         public $component_name;
     55        public $component_name = '';
    5656
    5757        /**
    5858         * The action within the component which the notification is related to.
    5959         *
    6060         * @var string
    6161         */
    62         public $component_action;
     62        public $component_action = '';
    6363
    6464        /**
    6565         * The date the notification was created.
    6666         *
    6767         * @var string
    6868         */
    69         public $date_notified;
     69        public $date_notified = '';
    7070
    7171        /**
    7272         * Is the notification new or has it already been read.
    7373         *
    7474         * @var boolean
    7575         */
    76         public $is_new;
     76        public $is_new = false;
    7777
    7878        /** Public Methods ********************************************************/
    7979
     
    8383         * @param int $id ID for the notification.
    8484         */
    8585        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;
    8990                }
     91
     92                $this->id = absint( $id );
     93                $this->populate();
    9094        }
    9195
    9296        /**
     
    102106                $bp = buddypress();
    103107
    104108                // 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 );
    107112
    108113                // Save.
    109114                } 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 );
    111117                }
    112118
    113                 if ( !$result = $wpdb->query( $sql ) )
     119                $result = $wpdb->query( $sql );
     120
     121                if ( empty( $result ) || is_wp_error( $result ) ) {
    114122                        return false;
     123                }
    115124
    116125                $this->id = $wpdb->insert_id;
    117126
     
    130139
    131140                $bp = buddypress();
    132141
    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;
    141154                }
    142155        }
    143156
     
    150163         * @param int $notification_id Notification ID to check for.
    151164         * @return string
    152165         */
    153         public static function check_access( $user_id, $notification_id ) {
     166        public static function check_access( $user_id = 0, $notification_id = 0 ) {
    154167                global $wpdb;
    155168
    156169                $bp = buddypress();
    157170
    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;
    159176        }
    160177
    161178        /**
     
    178195                        ? ' AND is_new = 1 '
    179196                        : '';
    180197
    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;
    182203        }
    183204
    184205        /**
     
    198219
    199220                $bp = buddypress();
    200221
    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;
    202227        }
    203228
    204229        /**
     
    212237         * @param int      $item_id           The item ID of the notifications we wish to delete.
    213238         * @param string   $component_name    The name of the component that the notifications we wish to delete.
    214239         * @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 to
     240         * @param int      $secondary_item_id (optional) The secondary item id of the notifications that we wish to
    216241         *                                    use to delete.
    217242         * @return mixed
    218243         */
    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 ) {
    220245                global $wpdb;
    221246
    222247                $bp = buddypress();
    223248
    224                 $secondary_item_sql = !empty( $secondary_item_id )
     249                $secondary_item_sql = ! empty( $secondary_item_id )
    225250                        ? $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id )
    226251                        : '';
    227252
    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;
    229258        }
    230259
    231260        /**
     
    245274
    246275                $bp = buddypress();
    247276
    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;
    249282        }
    250283
    251284        /**
     
    256289         *
    257290         * @static
    258291         *
    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.
    260293         * @param string $component_name    The component that the notifications are to be from.
    261294         * @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.
    263296         * @return mixed
    264297         */
    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 ) {
    266299                global $wpdb;
    267300
    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                        : '';
    272304
    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                        : '';
    277308
    278309                $bp = buddypress();
    279310
    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;
    281316        }
    282317}
  • src/bp-core/classes/class-bp-core.php

     
    3636        }
    3737
    3838        /**
     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        /**
    3964         * Populate the global data needed before BuddyPress can continue.
    4065         *
    4166         * This involves figuring out the currently required, activated, deactivated,
     
    249274                 */
    250275                $bp->grav_default->blog  = apply_filters( 'bp_blog_gravatar_default',  $bp->grav_default->user );
    251276
    252                 // Notifications table. Included here for legacy purposes. Use
    253                 // bp-notifications instead.
    254                 $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications';
    255 
    256277                // Backward compatibility for plugins modifying the legacy bp_nav and bp_options_nav global properties.
    257278                $bp->bp_nav         = new BP_Core_BP_Nav_BackCompat();
    258279                $bp->bp_options_nav = new BP_Core_BP_Options_Nav_BackCompat();
  • src/bp-notifications/classes/class-bp-notifications-notification.php

     
    556556         * @return bool True if the notification belongs to the user, otherwise
    557557         *              false.
    558558         */
    559         public static function check_access( $user_id, $notification_id ) {
     559        public static function check_access( $user_id = 0, $notification_id = 0 ) {
    560560                global $wpdb;
    561561
    562562                $bp = buddypress();
    563563
    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;
    565569        }
    566570
    567571        /**