Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/15/2020 04:33:31 PM (6 years ago)
Author:
imath
Message:

Core: use only one global to store the Notifications DB table name

  • Deprecate $bp->core->table_name_notifications.
  • Add a magic getter to BP_Core, with a fallback if the Notifications component isn't active.
  • Audit the related queries and method params, and makes sure they work as intended, and update them to our latest coding standards.

Props johnjamesjacoby

Fixes #8360

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-core-notification.php

    r10487 r12753  
    2525         * @var int
    2626         */
    27         public $id;
     27        public $id = 0;
    2828
    2929        /**
     
    3232         * @var int
    3333         */
    34         public $item_id;
     34        public $item_id = 0;
    3535
    3636        /**
     
    4646         * @var int
    4747         */
    48         public $user_id;
     48        public $user_id = 0;
    4949
    5050        /**
     
    5353         * @var string
    5454         */
    55         public $component_name;
     55        public $component_name = '';
    5656
    5757        /**
     
    6060         * @var string
    6161         */
    62         public $component_action;
     62        public $component_action = '';
    6363
    6464        /**
     
    6767         * @var string
    6868         */
    69         public $date_notified;
     69        public $date_notified = '';
    7070
    7171        /**
     
    7474         * @var boolean
    7575         */
    76         public $is_new;
     76        public $is_new = false;
    7777
    7878        /** Public Methods ********************************************************/
     
    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
     
    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;
     
    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        }
     
    151164         * @return string
    152165         */
    153         public static function check_access( $user_id, $notification_id ) {
    154                 global $wpdb;
    155 
    156                 $bp = buddypress();
    157 
    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 ) );
     166        public static function check_access( $user_id = 0, $notification_id = 0 ) {
     167                global $wpdb;
     168
     169                $bp = buddypress();
     170
     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
     
    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
     
    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
     
    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 ) {
    220                 global $wpdb;
    221 
    222                 $bp = buddypress();
    223 
    224                 $secondary_item_sql = !empty( $secondary_item_id )
     244        public static function delete_for_user_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id = 0 ) {
     245                global $wpdb;
     246
     247                $bp = buddypress();
     248
     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
     
    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
     
    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 ) {
    266                 global $wpdb;
    267 
    268                 if ( $component_action )
    269                         $component_action_sql = $wpdb->prepare( "AND component_action = %s", $component_action );
    270                 else
    271                         $component_action_sql = '';
    272 
    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 = '';
    277 
    278                 $bp = buddypress();
    279 
    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 ) );
     298        public static function delete_all_by_type( $item_id, $component_name, $component_action = '', $secondary_item_id = 0 ) {
     299                global $wpdb;
     300
     301                $component_action_sql = ! empty( $component_action )
     302                        ? $wpdb->prepare( "AND component_action = %s", $component_action )
     303                        : '';
     304
     305                $secondary_item_sql = ! empty( $secondary_item_id )
     306                        ? $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id )
     307                        : '';
     308
     309                $bp = buddypress();
     310
     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}
Note: See TracChangeset for help on using the changeset viewer.