BP_Messages_Thread_Template

Message Thread Template Class

Description

Source

File: bp-messages/classes/class-bp-messages-thread-template.php

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class BP_Messages_Thread_Template {
 
    /**
     * The loop iterator.
     *
     * @var int
     */
    public $current_message = -1;
 
    /**
     * Number of messages returned by the paged query.
     *
     * @var int
     */
    public $message_count = 0;
 
    /**
     * The message object currently being iterated on.
     *
     * @var object
     */
    public $message;
 
    /**
     * Thread that the current messages belong to.
     *
     * @var BP_Messages_Thread
     */
    public $thread;
 
    /**
     * A flag for whether the loop is currently being iterated.
     *
     * @var bool
     */
    public $in_the_loop = false;
 
    /**
     * The page number being requested.
     *
     * @var int
     */
    public $pag_page = 1;
 
    /**
     * The number of items being requested per page.
     *
     * @var int
     */
    public $pag_num = 10;
 
    /**
     * An HTML string containing pagination links.
     *
     * @var string
     */
    public $pag_links = '';
 
    /**
     * The total number of messages matching the query.
     *
     * @var int
     */
    public $total_message_count = 0;
 
    /**
     * Constructor method.
     *
     * @see BP_Messages_Thread::populate() for full parameter info.
     *
     * @param int    $thread_id ID of the message thread to display.
     * @param string $order     Order to show the thread's messages in.
     * @param array  $args      Array of arguments for the query.
     */
    public function __construct( $thread_id = 0, $order = 'DESC', $args = array() ) {
        $this->thread        = new BP_Messages_Thread( $thread_id, $order, $args );
        $this->message_count = count( $this->thread->messages );
    }
 
    /**
     * Whether there are messages available in the loop.
     *
     * @see bp_thread_has_messages()
     *
     * @return bool True if there are items in the loop, otherwise false.
     */
    public function has_messages() {
        if ( ! empty( $this->message_count ) ) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Set up the next member and iterate index.
     *
     * @return object The next member to iterate over.
     */
    public function next_message() {
        $this->current_message++;
        $this->message = $this->thread->messages[ $this->current_message ];
 
        return $this->message;
    }
 
    /**
     * Rewind the messages and reset message index.
     */
    public function rewind_messages() {
        $this->current_message = -1;
        if ( $this->message_count > 0 ) {
            $this->message = $this->thread->messages[0];
        }
    }
 
    /**
     * Whether there are messages left in the loop to iterate over.
     *
     * This method is used by {@link bp_thread_messages()} as part of the
     * while loop that controls iteration inside the messages loop, eg:
     *     while ( bp_thread_messages() ) { ...
     *
     * @see bp_thread_messages()
     *
     * @return bool True if there are more messages to show, otherwise false.
     */
    public function messages() {
        if ( ( $this->current_message + 1 ) < $this->message_count ) {
            return true;
        } elseif ( ( $this->current_message + 1 ) === $this->message_count ) {
 
            /**
             * Fires when at the end of messages to iterate over.
             *
             * @since BuddyPress 1.1.0
             */
            do_action( 'thread_loop_end' );
            // Do some cleaning up after the loop.
            $this->rewind_messages();
        }
 
        $this->in_the_loop = false;
        return false;
    }
 
    /**
     * Set up the current message inside the loop.
     *
     * Used by {@link bp_thread_the_message()} to set up the current
     * message data while looping, so that template tags used during
     * that iteration make reference to the current message.
     *
     * @see bp_thread_the_message()
     */
    public function the_message() {
        $this->in_the_loop = true;
        $this->message     = $this->next_message();
 
        // Loop has just started.
        if ( 0 === $this->current_message ) {
 
            /**
             * Fires if at the start of the message loop.
             *
             * @since BuddyPress 1.1.0
             */
            do_action( 'thread_loop_start' );
        }
    }
}

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.