Skip to:
Content

BuddyPress.org

Ticket #5193: bp-messages-classes.php.02.patch

File bp-messages-classes.php.02.patch, 17.3 KB (added by slaFFik, 10 years ago)
  • src/bp-messages/bp-messages-classes.php

     
    109109         *
    110110         * @since BuddyPress (1.0.0)
    111111         *
    112          * @param int $thread_id The message thread ID.
     112         * @param bool|int $thread_id The message thread ID.
    113113         * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'.
    114114         */
    115115        public function __construct( $thread_id = false, $order = 'ASC' ) {
     
    127127         *
    128128         * @param int $thread_id The message thread ID.
    129129         * @param string $order The order to sort the messages. Either 'ASC' or 'DESC'.
     130         * @return bool False on failure
    130131         */
    131132        public function populate( $thread_id, $order ) {
    132133                global $wpdb, $bp;
     
    135136                        $order= 'ASC';
    136137                }
    137138
    138                 $this->messages_order = $order;
     139                $this->messages_order = apply_filters( 'messages_thread_populate_order', $order );
    139140                $this->thread_id      = $thread_id;
    140141
    141142                if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id ) ) ) {
     
    146147                        $this->sender_ids[$message->sender_id] = $message->sender_id;
    147148                }
    148149
     150                $this->sender_ids = apply_filters( 'messages_thread_populate_sender_ids', $this->sender_ids );
     151
    149152                // Fetch the recipients
    150                 $this->recipients = $this->get_recipients();
     153                $this->recipients = apply_filters( 'messages_thread_populate_recipients', $this->get_recipients() );
    151154
    152155                // Get the unread count for the logged in user
    153156                if ( isset( $this->recipients[bp_loggedin_user_id()] ) ) {
    154                         $this->unread_count = $this->recipients[bp_loggedin_user_id()]->unread_count;
     157                        $this->unread_count = apply_filters( 'messages_thread_populate_unread_count', $this->recipients[bp_loggedin_user_id()]->unread_count );
    155158                }
    156159        }
    157160
     
    190193                $recipients = array();
    191194                $results    = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) );
    192195
    193                 foreach ( (array) $results as $recipient )
     196                foreach ( (array) $results as $recipient ) {
    194197                        $recipients[$recipient->user_id] = $recipient;
     198                }
    195199
    196                 return $recipients;
     200                return apply_filters( 'messages_thread_get_recipients', $recipients, $this->thread_id );
    197201        }
    198202
    199203        /** Static Functions ******************************************************/
     
    209213        public static function delete( $thread_id ) {
    210214                global $wpdb, $bp;
    211215
     216                do_action( 'messages_thread_delete_thread_before', $thread_id );
     217
    212218                // Mark messages as deleted
    213219                $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, bp_loggedin_user_id() ) );
    214220
     
    250256        public static function get_current_threads_for_user( $user_id, $box = 'inbox', $type = 'all', $limit = null, $page = null, $search_terms = '' ) {
    251257                global $wpdb, $bp;
    252258
    253                 $user_id_sql = $pag_sql = $type_sql = $search_sql = '';
     259                $user_id_sql  = $pag_sql = $type_sql = $search_sql = $total_threads = '';
     260                $thread_ids   = array();
     261                $box              = apply_filters( 'messages_thread_current_threads_box', $box );
     262                $type             = apply_filters( 'messages_thread_current_threads_type', $type );
     263                $search_terms = apply_filters( 'messages_thread_current_threads_search_terms', $search_terms );
    254264
    255265                if ( $limit && $page ) {
    256266                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
     
    262272                        $type_sql = " AND r.unread_count = 0 ";
    263273                }
    264274
     275                $type_sql = apply_filters( 'messages_thread_current_threads_type_sql', $type_sql );
     276
    265277                if ( ! empty( $search_terms ) ) {
    266278                        $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';
    267279                        $search_sql        = $wpdb->prepare( "AND ( subject LIKE %s OR message LIKE %s )", $search_terms_like, $search_terms_like );
    268280                }
    269281
     282                $search_sql = apply_filters( 'messages_thread_current_threads_search_sql', $search_sql );
     283
    270284                if ( 'sentbox' == $box ) {
    271                         $user_id_sql = $wpdb->prepare( 'm.sender_id = %d', $user_id );
    272                         $thread_ids  = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
     285                        $user_id_sql   = $wpdb->prepare( 'm.sender_id = %d', $user_id );
     286                        $thread_ids    = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    273287                        $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} " );
    274                 } else {
    275                         $user_id_sql = $wpdb->prepare( 'r.user_id = %d', $user_id );
    276                         $thread_ids = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
     288                } elseif ( 'inbox' == $box ) {
     289                        $user_id_sql   = $wpdb->prepare( 'r.user_id = %d', $user_id );
     290                        $thread_ids    = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    277291                        $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql}" );
    278292                }
    279293
     294                $thread_ids    = apply_filters( 'messages_thread_current_threads_thread_ids',    $thread_ids );
     295                $total_threads = apply_filters( 'messages_thread_current_threads_total_threads', $total_threads );
     296
    280297                if ( empty( $thread_ids ) ) {
    281298                        return false;
    282299                }
     
    293310                        $threads[] = new BP_Messages_Thread( $thread_id );
    294311                }
    295312
    296                 return array( 'threads' => &$threads, 'total' => (int) $total_threads );
     313                return apply_filters( 'messages_thread_current_threads', array( 'threads' => &$threads, 'total' => (int) $total_threads ) );
    297314        }
    298315
    299316        /**
     
    306323        public static function mark_as_read( $thread_id ) {
    307324                global $wpdb, $bp;
    308325
    309                 $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
     326                $user_id = bp_loggedin_user_id();
     327
     328                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id );
    310329                $wpdb->query($sql);
    311330
    312                 wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     331                do_action( 'messages_thread_marked_as_read', $user_id, $thread_id );
     332
     333                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    313334        }
    314335
    315336        /**
     
    322343        public static function mark_as_unread( $thread_id ) {
    323344                global $wpdb, $bp;
    324345
    325                 $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
     346                $user_id = bp_loggedin_user_id();
     347
     348                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id );
    326349                $wpdb->query($sql);
    327350
    328                 wp_cache_delete( bp_loggedin_user_id(), 'bp_messages_unread_count' );
     351                do_action( 'messages_thread_marked_as_unread', $user_id, $thread_id );
     352
     353                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    329354        }
    330355
    331356        /**
     
    343368        public static function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) {
    344369                global $wpdb, $bp;
    345370
    346                 $exclude_sender = '';
    347                 if ( $box != 'sentbox' )
    348                         $exclude_sender = ' AND sender_only != 1';
     371                $exclude_sender_sql = $type_sql = '';
    349372
    350                 if ( $type == 'unread' )
     373                if ( $box != 'sentbox' ) {
     374                        $exclude_sender_sql = ' AND sender_only != 1';
     375                }
     376
     377                $exclude_sender_sql = apply_filters( 'messages_thread_total_threads_for_user_exclude_sender_sql', $exclude_sender_sql, $user_id );
     378
     379                if ( $type == 'unread' ) {
    351380                        $type_sql = " AND unread_count != 0 ";
    352                 else if ( $type == 'read' )
     381                } else if ( $type == 'read' ) {
    353382                        $type_sql = " AND unread_count = 0 ";
     383                }
    354384
    355                 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0{$exclude_sender} {$type_sql}", $user_id ) );
     385                $type_sql = apply_filters( 'messages_thread_total_threads_for_user_type_sql', $type_sql, $user_id );
     386
     387                return apply_filters( 'messages_thread_total_threads_for_user', (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0{$exclude_sender_sql} {$type_sql}", $user_id ) ), $user_id, $box, $type );
    356388        }
    357389
    358390        /**
     
    361393         * @since BuddyPress (1.0.0)
    362394         *
    363395         * @param int $thread_id The message thread ID.
    364          * @param bool
     396         * @return bool
    365397         */
    366398        public static function user_is_sender( $thread_id ) {
    367399                global $wpdb, $bp;
     
    434466                if ( empty( $user_id ) )
    435467                        $user_id = bp_loggedin_user_id();
    436468
    437                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0 AND user_id = %d", $thread_id, $user_id ) );
     469                return apply_filters( 'messages_thread_check_access', $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND is_deleted = 0 AND user_id = %d", $thread_id, $user_id ) ), $thread_id, $user_id );
    438470        }
    439471
    440472        /**
     
    455487
    456488                $bp = buddypress();
    457489
    458                 return $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) );
     490                return apply_filters( 'messages_thread_is_valid', $wpdb->get_var( $wpdb->prepare( "SELECT thread_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d LIMIT 1", $thread_id ) ), $thread_id );
    459491        }
    460492
    461493        /**
     
    468500         *
    469501         * @since BuddyPress (1.0.0)
    470502         *
    471          * @param object $recipients Object containing the message recipients.
     503         * @param array $recipients Array containing the message recipients (array of objects).
    472504         * @return string
    473505         */
    474506        public static function get_recipient_links( $recipients ) {
    475                 if ( count( $recipients ) >= 5 )
    476                         return sprintf( __( '%s Recipients', 'buddypress' ), number_format_i18n( count( $recipients ) ) );
     507                if ( count( $recipients ) >= 5 ) {
     508                        return sprintf(__('%s Recipients', 'buddypress'), number_format_i18n(count($recipients)));
     509                }
    477510
    478511                $recipient_links = array();
    479512
     
    487520                        $recipient_links[] = $recipient_link;
    488521                }
    489522
     523                $recipient_links = apply_filters( 'messages_thread_recipient_links', $recipient_links );
     524
    490525                return implode( ', ', (array) $recipient_links );
    491526        }
    492527
     
    607642
    608643                if ( $message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id ) ) ) {
    609644                        $this->id        = $message->id;
    610                         $this->thread_id = $message->thread_id;
    611                         $this->sender_id = $message->sender_id;
    612                         $this->subject   = $message->subject;
    613                         $this->message   = $message->message;
    614                         $this->date_sent = $message->date_sent;
     645                        $this->thread_id = apply_filters( 'messages_message_populate_thread_id', $message->thread_id );
     646                        $this->sender_id = apply_filters( 'messages_message_populate_sender_id', $message->sender_id );
     647                        $this->subject   = apply_filters( 'messages_message_populate_subject',   $message->subject   );
     648                        $this->message   = apply_filters( 'messages_message_populate_message',   $message->message   );
     649                        $this->date_sent = apply_filters( 'messages_message_populate_date_sent', $message->date_sent );
    615650                }
    616651        }
    617652
     
    633668                do_action_ref_array( 'messages_message_before_save', array( &$this ) );
    634669
    635670                // Make sure we have at least one recipient before sending.
    636                 if ( empty( $this->recipients ) )
     671                if ( empty( $this->recipients ) ) {
    637672                        return false;
     673                }
    638674
    639675                $new_thread = false;
    640676
     
    645681                }
    646682
    647683                // First insert the message into the messages table
    648                 if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( thread_id, sender_id, subject, message, date_sent ) VALUES ( %d, %d, %s, %s, %s )", $this->thread_id, $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) )
     684                if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( thread_id, sender_id, subject, message, date_sent ) VALUES ( %d, %d, %s, %s, %s )", $this->thread_id, $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) ) {
    649685                        return false;
     686                }
    650687
    651688                $this->id = $wpdb->insert_id;
    652689
     
    681718         */
    682719        public function get_recipients() {
    683720                global $bp, $wpdb;
    684                 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) );
     721                return apply_filters( 'messages_message_get_recipients', $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) ), $this->thread_id );
    685722        }
    686723
    687724        /** Static Functions **************************************************/
     
    693730         * @return array
    694731         */
    695732        public static function get_recipient_ids( $recipient_usernames ) {
    696                 if ( !$recipient_usernames )
     733                if ( !$recipient_usernames ) {
    697734                        return false;
     735                }
    698736
     737                $recipient_ids = array();
     738
    699739                if ( is_array( $recipient_usernames ) ) {
    700740                        for ( $i = 0, $count = count( $recipient_usernames ); $i < $count; ++$i ) {
    701741                                if ( $rid = bp_core_get_userid( trim($recipient_usernames[$i]) ) ) {
     
    704744                        }
    705745                }
    706746
    707                 return $recipient_ids;
     747                return apply_filters( 'messages_message_get_recipient_ids', $recipient_ids, $recipient_usernames );
    708748        }
    709749
    710750        /**
     
    715755         */
    716756        public static function get_last_sent_for_user( $thread_id ) {
    717757                global $wpdb, $bp;
    718                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND thread_id = %d ORDER BY date_sent DESC LIMIT 1", bp_loggedin_user_id(), $thread_id ) );
     758                return apply_filters( 'messages_message_last_sent_for_user', $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND thread_id = %d ORDER BY date_sent DESC LIMIT 1", bp_loggedin_user_id(), $thread_id ) ), $thread_id );
    719759        }
    720760
    721761        /**
     
    790830         * Constructor.
    791831         *
    792832         * @since BuddyPress (1.0.0)
     833         * @param null $id
    793834         */
    794835        public function __construct( $id = null ) {
    795836                if ( $id ) {
     
    811852                $notice = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ) );
    812853
    813854                if ( $notice ) {
    814                         $this->subject   = $notice->subject;
    815                         $this->message   = $notice->message;
    816                         $this->date_sent = $notice->date_sent;
    817                         $this->is_active = $notice->is_active;
     855                        $this->subject   = apply_filters( 'messages_notice_populate_subject',   $notice->subject   );
     856                        $this->message   = apply_filters( 'messages_notice_populate_message',   $notice->message   );
     857                        $this->date_sent = apply_filters( 'messages_notice_populate_date_sent', $notice->date_sent );
     858                        $this->is_active = apply_filters( 'messages_notice_populate_is_active', $notice->is_active );
    818859                }
    819860        }
    820861
     
    865906         * @return bool
    866907         */
    867908        public function activate() {
    868                 $this->is_active = 1;
     909                $this->is_active = apply_filters( 'messages_notice_activate', 1 );
    869910                return (bool) $this->save();
    870911        }
    871912
     
    877918         * @return bool
    878919         */
    879920        public function deactivate() {
    880                 $this->is_active = 0;
     921                $this->is_active = apply_filters( 'messages_notice_deactivate', 0 );
    881922                return (bool) $this->save();
    882923        }
    883924
     
    899940                        return false;
    900941                }
    901942
     943                do_action( 'messages_notice_after_delete' );
     944
    902945                return true;
    903946        }
    904947
     
    911954         *
    912955         * @since BuddyPress (1.0.0)
    913956         *
    914          * @param array $data {
     957         * @param array $args {
    915958         *     Array of parameters.
    916959         *     @type int $pag_num Number of notices per page. Defaults to 20.
    917960         *     @type int $pag_page The page number.  Defaults to 1.
     
    926969                        'pag_page' => 1   // Page number
    927970                ) );
    928971
     972                $r = apply_filters( 'messages_notice_get_notices_arg', $r);
     973
    929974                $limit_sql = '';
    930975                if ( (int) $r['pag_num'] >= 0 ) {
    931976                        $limit_sql = $wpdb->prepare( "LIMIT %d, %d", (int) ( ( $r['pag_page'] - 1 ) * $r['pag_num'] ), (int) $r['pag_num'] );
     
    948993
    949994                $notice_count = $wpdb->get_var( "SELECT COUNT(id) FROM " . $bp->messages->table_name_notices );
    950995
    951                 return $notice_count;
     996                return apply_filters( 'messages_notice_total_count', $notice_count );
    952997        }
    953998
    954999        /**